You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/02/03 02:02:55 UTC

[GitHub] [arrow-datafusion] HaoYang670 opened a new pull request #1734: Create built-in scalar functions programmatically

HaoYang670 opened a new pull request #1734:
URL: https://github.com/apache/arrow-datafusion/pull/1734


   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #1718.
   Re #1727.
   
    # Rationale for this change
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   We create a cleaner way to call built-in scalar functions by calling `call_builtin_scalar_fn`.
    
   # What changes are included in this PR?
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are there any user-facing changes?
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   A new function `call_builtin_scalar_fn`
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-datafusion] HaoYang670 commented on a change in pull request #1734: Create built-in scalar functions programmatically

Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on a change in pull request #1734:
URL: https://github.com/apache/arrow-datafusion/pull/1734#discussion_r800000256



##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -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),
+    }

Review comment:
       Thank you @Jimexist ! 
   
   And alamb 's opinion of the trade-off between macro and function call is here: 
   https://github.com/apache/arrow-datafusion/issues/1718#issuecomment-1026239024




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-datafusion] HaoYang670 commented on a change in pull request #1734: Create built-in scalar functions programmatically

Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on a change in pull request #1734:
URL: https://github.com/apache/arrow-datafusion/pull/1734#discussion_r800000256



##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -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),
+    }

Review comment:
       Agree with you @Jimexist ! 
   
   And alamb 's opinion of the trade-off between macro and function call is here: 
   https://github.com/apache/arrow-datafusion/issues/1718#issuecomment-1026239024




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-datafusion] alamb commented on a change in pull request #1734: Create built-in scalar functions programmatically

Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #1734:
URL: https://github.com/apache/arrow-datafusion/pull/1734#discussion_r798933169



##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -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),
+    }

Review comment:
       One way to write this more idiomatically is
   
   ```suggestion
       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)

##########
File path: datafusion/src/logical_plan/mod.rs
##########
@@ -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,

Review comment:
       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 🤔 

##########
File path: datafusion/src/optimizer/simplify_expressions.rs
##########
@@ -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 =

Review comment:
       well that is certainly nicer looking 👍 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-datafusion] HaoYang670 commented on a change in pull request #1734: Create built-in scalar functions programmatically

Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on a change in pull request #1734:
URL: https://github.com/apache/arrow-datafusion/pull/1734#discussion_r800338088



##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -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),
+    }

Review comment:
       Is it needed to implement both?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-datafusion] HaoYang670 commented on a change in pull request #1734: Create built-in scalar functions programmatically

Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on a change in pull request #1734:
URL: https://github.com/apache/arrow-datafusion/pull/1734#discussion_r800370911



##########
File path: datafusion/src/logical_plan/mod.rs
##########
@@ -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,

Review comment:
       Thank you, I will change it




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-datafusion] alamb commented on a change in pull request #1734: Create built-in scalar functions programmatically

Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #1734:
URL: https://github.com/apache/arrow-datafusion/pull/1734#discussion_r800584834



##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -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),
+    }

Review comment:
       No I don't think so. If we want a macro we can always add that as a follow on PR. 
   
   Thanks @HaoYang670  !




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-datafusion] Jimexist commented on a change in pull request #1734: Create built-in scalar functions programmatically

Posted by GitBox <gi...@apache.org>.
Jimexist commented on a change in pull request #1734:
URL: https://github.com/apache/arrow-datafusion/pull/1734#discussion_r799970574



##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -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),
+    }

Review comment:
       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




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-datafusion] houqp commented on a change in pull request #1734: Create built-in scalar functions programmatically

Posted by GitBox <gi...@apache.org>.
houqp commented on a change in pull request #1734:
URL: https://github.com/apache/arrow-datafusion/pull/1734#discussion_r799992435



##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -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),
+    }

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




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-datafusion] alamb merged pull request #1734: Create built-in scalar functions programmatically

Posted by GitBox <gi...@apache.org>.
alamb merged pull request #1734:
URL: https://github.com/apache/arrow-datafusion/pull/1734


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-datafusion] houqp commented on a change in pull request #1734: Create built-in scalar functions programmatically

Posted by GitBox <gi...@apache.org>.
houqp commented on a change in pull request #1734:
URL: https://github.com/apache/arrow-datafusion/pull/1734#discussion_r799992435



##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -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),
+    }

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




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org