Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create built-in scalar functions programmatically #1734

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions datafusion/src/logical_plan/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2189,6 +2189,20 @@ pub fn exprlist_to_fields<'a>(
expr.into_iter().map(|e| e.to_field(input_schema)).collect()
}

/// Calls a named built in function
/// ```
/// use datafusion::logical_plan::*;
///
/// // create the expression sin(x) < 0.2
/// let expr = call_builtin_scalar_fn("sin", vec![col("x")]).unwrap().lt(lit(0.2));
/// ```
pub fn call_builtin_scalar_fn(name: impl AsRef<str>, args: Vec<Expr>) -> Result<Expr> {
match name.as_ref().parse::<functions::BuiltinScalarFunction>() {
Ok(fun) => Ok(Expr::ScalarFunction { fun, args }),
Err(e) => Err(e),
}
Comment on lines +2246 to +2249
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way to write this more idiomatically is

Suggested change
match name.as_ref().parse::<functions::BuiltinScalarFunction>() {
Ok(fun) => Ok(Expr::ScalarFunction { fun, args }),
Err(e) => Err(e),
}
name.as_ref().parse::<functions::BuiltinScalarFunction>()
.map(|fun| Expr::ScalarFunction { fun, args }),
}

(not required, I am just pointing it out because it took me a while to get my head around working with Option and Results, and we are all learning together)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if it makes sense to make it a macro rather than a function call so that nonexisteng built-in functions will be caught during compile time not runtime

Copy link
Member

@houqp houqp Feb 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, call_builtin_scalar_fn!(ToTimestamp, vec![lit("2020-09-08T12:00:00+00:00")]) is as readable.

Copy link
Contributor Author

@HaoYang670 HaoYang670 Feb 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @jimexist !

And alamb 's opinion of the trade-off between macro and function call is here:
#1718 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it needed to implement both?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I don't think so. If we want a macro we can always add that as a follow on PR.

Thanks @HaoYang670 !

}

#[cfg(test)]
mod tests {
use super::super::{col, lit, when};
Expand Down
22 changes: 11 additions & 11 deletions datafusion/src/logical_plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ pub use dfschema::{DFField, DFSchema, DFSchemaRef, ToDFSchema};
pub use display::display_schema;
pub use expr::{
abs, acos, and, approx_distinct, approx_percentile_cont, array, ascii, asin, atan,
avg, binary_expr, bit_length, btrim, case, ceil, character_length, chr, col,
columnize_expr, combine_filters, concat, concat_ws, cos, count, count_distinct,
create_udaf, create_udf, date_part, date_trunc, digest, exp, exprlist_to_fields,
floor, in_list, initcap, left, length, lit, lit_timestamp_nano, ln, log10, log2,
lower, lpad, ltrim, max, md5, min, normalize_col, normalize_cols, now, octet_length,
or, random, regexp_match, regexp_replace, repeat, replace, replace_col, reverse,
rewrite_sort_cols_by_aggs, right, round, rpad, rtrim, sha224, sha256, sha384, sha512,
signum, sin, split_part, sqrt, starts_with, strpos, substr, sum, tan, to_hex,
translate, trim, trunc, unalias, unnormalize_col, unnormalize_cols, upper, when,
Column, Expr, ExprRewriter, ExpressionVisitor, Literal, Recursion, RewriteRecursion,
SimplifyInfo,
avg, binary_expr, bit_length, btrim, call_builtin_scalar_fn, case, ceil,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing all these other names, what would you think of changing the name from call_builtin_scalar_fn to call_fn to make it less verbose 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I will change it

character_length, chr, col, columnize_expr, combine_filters, concat, concat_ws, cos,
count, count_distinct, create_udaf, create_udf, date_part, date_trunc, digest, exp,
exprlist_to_fields, floor, in_list, initcap, left, length, lit, lit_timestamp_nano,
ln, log10, log2, lower, lpad, ltrim, max, md5, min, normalize_col, normalize_cols,
now, octet_length, or, random, regexp_match, regexp_replace, repeat, replace,
replace_col, reverse, rewrite_sort_cols_by_aggs, right, round, rpad, rtrim, sha224,
sha256, sha384, sha512, signum, sin, split_part, sqrt, starts_with, strpos, substr,
sum, tan, to_hex, translate, trim, trunc, unalias, unnormalize_col, unnormalize_cols,
upper, when, Column, Expr, ExprRewriter, ExpressionVisitor, Literal, Recursion,
RewriteRecursion, SimplifyInfo,
};
pub use extension::UserDefinedLogicalNode;
pub use operators::Operator;
Expand Down
50 changes: 16 additions & 34 deletions datafusion/src/optimizer/simplify_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,8 +735,8 @@ mod tests {
use super::*;
use crate::assert_contains;
use crate::logical_plan::{
and, binary_expr, col, create_udf, lit, lit_timestamp_nano, DFField, Expr,
LogicalPlanBuilder,
and, binary_expr, call_builtin_scalar_fn, col, create_udf, lit,
lit_timestamp_nano, DFField, Expr, LogicalPlanBuilder,
};
use crate::physical_plan::functions::{make_scalar_function, BuiltinScalarFunction};
use crate::physical_plan::udf::ScalarUDF;
Expand Down Expand Up @@ -1010,46 +1010,34 @@ mod tests {
#[test]
fn test_const_evaluator_scalar_functions() {
// concat("foo", "bar") --> "foobar"
let expr = Expr::ScalarFunction {
args: vec![lit("foo"), lit("bar")],
fun: BuiltinScalarFunction::Concat,
};
let expr =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well that is certainly nicer looking 👍

call_builtin_scalar_fn("concat", vec![lit("foo"), lit("bar")]).unwrap();
test_evaluate(expr, lit("foobar"));

// ensure arguments are also constant folded
// concat("foo", concat("bar", "baz")) --> "foobarbaz"
let concat1 = Expr::ScalarFunction {
args: vec![lit("bar"), lit("baz")],
fun: BuiltinScalarFunction::Concat,
};
let expr = Expr::ScalarFunction {
args: vec![lit("foo"), concat1],
fun: BuiltinScalarFunction::Concat,
};
let concat1 =
call_builtin_scalar_fn("concat", vec![lit("bar"), lit("baz")]).unwrap();
let expr = call_builtin_scalar_fn("concat", vec![lit("foo"), concat1]).unwrap();
test_evaluate(expr, lit("foobarbaz"));

// Check non string arguments
// to_timestamp("2020-09-08T12:00:00+00:00") --> timestamp(1599566400000000000i64)
let expr = Expr::ScalarFunction {
args: vec![lit("2020-09-08T12:00:00+00:00")],
fun: BuiltinScalarFunction::ToTimestamp,
};
let expr = call_builtin_scalar_fn(
"to_timestamp",
vec![lit("2020-09-08T12:00:00+00:00")],
)
.unwrap();
test_evaluate(expr, lit_timestamp_nano(1599566400000000000i64));

// check that non foldable arguments are folded
// to_timestamp(a) --> to_timestamp(a) [no rewrite possible]
let expr = Expr::ScalarFunction {
args: vec![col("a")],
fun: BuiltinScalarFunction::ToTimestamp,
};
let expr = call_builtin_scalar_fn("to_timestamp", vec![col("a")]).unwrap();
test_evaluate(expr.clone(), expr);

// check that non foldable arguments are folded
// to_timestamp(a) --> to_timestamp(a) [no rewrite possible]
let expr = Expr::ScalarFunction {
args: vec![col("a")],
fun: BuiltinScalarFunction::ToTimestamp,
};
let expr = call_builtin_scalar_fn("to_timestamp", vec![col("a")]).unwrap();
test_evaluate(expr.clone(), expr);

// volatile / stable functions should not be evaluated
Expand Down Expand Up @@ -1090,10 +1078,7 @@ mod tests {
}

fn now_expr() -> Expr {
Expr::ScalarFunction {
args: vec![],
fun: BuiltinScalarFunction::Now,
}
call_builtin_scalar_fn("now", vec![]).unwrap()
}

fn cast_to_int64_expr(expr: Expr) -> Expr {
Expand All @@ -1104,10 +1089,7 @@ mod tests {
}

fn to_timestamp_expr(arg: impl Into<String>) -> Expr {
Expr::ScalarFunction {
args: vec![lit(arg.into())],
fun: BuiltinScalarFunction::ToTimestamp,
}
call_builtin_scalar_fn("to_timestamp", vec![lit(arg.into())]).unwrap()
}

#[test]
Expand Down