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 2020/09/10 11:45:38 UTC

[GitHub] [arrow] alamb commented on a change in pull request #8144: ARROW-9950: [Rust] [DataFusion] Made UDFs usable without a registry

alamb commented on a change in pull request #8144:
URL: https://github.com/apache/arrow/pull/8144#discussion_r486271643



##########
File path: rust/datafusion/examples/simple_udf.rs
##########
@@ -111,20 +111,28 @@ fn main() -> Result<()> {
         pow,
     );
 
-    // finally, register the UDF
-    ctx.register_udf(pow);
+    // at this point, we can use it or register it, depending on the use-case:
+    // * if the UDF is expected to be used throughout the program in different contexts,
+    //   we can register it, and call it later:
+    ctx.register_udf(pow.clone()); // clone is only required in this example because we show both usages
 
-    // at this point, we can use it. Note that the code below can be in a
-    // scope on which we do not have access to `pow`.
+    // * if the UDF is expected to be used directly in the scope, `.call` it directly:
+    let expr = pow.call(vec![col("a"), col("b")]);
 
     // get a DataFrame from the context
     let df = ctx.table("t")?;
 
-    // get the udf registry.
-    let f = df.registry();
-
-    // equivalent to `'SELECT pow(a, b) FROM t'`
-    let df = df.select(vec![f.udf("pow", vec![col("a"), col("b")])?])?;
+    // if we do not have `pow` in the scope and we registered it, we can get it from the registry
+    let pow = df.registry().udf("pow")?;
+    // equivalent to expr
+    let expr1 = pow.call(vec![col("a"), col("b")]);
+

Review comment:
       ```suggestion
   assert_eq!(expr, expr1);
   ```

##########
File path: rust/datafusion/src/logical_plan/mod.rs
##########
@@ -1133,8 +1133,8 @@ pub trait FunctionRegistry {
     /// Set of all available udfs.
     fn udfs(&self) -> HashSet<String>;
 
-    /// Constructs a logical expression with a call to the udf.
-    fn udf(&self, name: &str, args: Vec<Expr>) -> Result<Expr>;
+    /// Returns a reference to the udf named `name`.
+    fn udf(&self, name: &str) -> Result<&ScalarUDF>;

Review comment:
       👍 




----------------------------------------------------------------
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.

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