You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "JasonLi-cn (via GitHub)" <gi...@apache.org> on 2023/11/15 01:56:54 UTC

[PR] feat: refactor udf/udaf/udwf ReturnType [arrow-datafusion]

JasonLi-cn opened a new pull request, #8183:
URL: https://github.com/apache/arrow-datafusion/pull/8183

   ## 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 https://github.com/apache/arrow-datafusion/issues/8182
   
   ## 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.  
   -->
   
   In some cases, I need to determine the output type of the function based on the input arguments of the function, but I cannot do this at present. This is because the ReturnTypeFunction provides only the data types of the input parameters.
   
   ## 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.
   -->
   
   - Add `ReturnTypeFactory` trait
   - `ReturnTypeFunction` impl `ReturnTypeFactory`
   - Change UDF, UDAF, UDWF's `return_type` from `ReturnTypeFunction` into `ReturnTypeFactory`
   
   ## Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   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


Re: [PR] feat: refactor udf/udaf/udwf ReturnType [arrow-datafusion]

Posted by "JasonLi-cn (via GitHub)" <gi...@apache.org>.
JasonLi-cn closed pull request #8183: feat: refactor udf/udaf/udwf ReturnType
URL: https://github.com/apache/arrow-datafusion/pull/8183


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


Re: [PR] feat: refactor udf/udaf/udwf ReturnType [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #8183:
URL: https://github.com/apache/arrow-datafusion/pull/8183#issuecomment-2051433153

   FOr anyone following along, this was implemented in a different way, see https://github.com/apache/arrow-datafusion/issues/8624


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


Re: [PR] feat: refactor udf/udaf/udwf ReturnType [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #8183:
URL: https://github.com/apache/arrow-datafusion/pull/8183#discussion_r1394805720


##########
datafusion/expr/src/function.rs:
##########
@@ -36,10 +36,32 @@ use strum::IntoEnumIterator;
 pub type ScalarFunctionImplementation =
     Arc<dyn Fn(&[ColumnarValue]) -> Result<ColumnarValue> + Send + Sync>;
 
+/// Constant argument, (arg index, constant value).
+pub type ConstantArg = (usize, ScalarValue);
+
+/// Factory that returns the functions's return type given the input argument types and constant arguments
+pub trait ReturnTypeFactory: Send + Sync {
+    fn infer(
+        &self,
+        input_types: &[DataType],
+        constant_args: &[ConstantArg],
+    ) -> Result<Arc<DataType>>;
+}
+
 /// Factory that returns the functions's return type given the input argument types
 pub type ReturnTypeFunction =

Review Comment:
   I wonder if you considered changing the `ReturnTypeFunction` to something more like
   ```
       Arc<dyn Fn(&[Expr], &dyn ExprSchemable) -> Result<Arc<DataType>> + Send + Sync>;
   ```
   
   Now that we have made the fields of `ScalarUDF` non pub (in https://github.com/apache/arrow-datafusion/pull/8079) I think we have much greater leeway to improve the API



##########
datafusion/expr/src/function.rs:
##########
@@ -36,10 +36,32 @@ use strum::IntoEnumIterator;
 pub type ScalarFunctionImplementation =
     Arc<dyn Fn(&[ColumnarValue]) -> Result<ColumnarValue> + Send + Sync>;
 
+/// Constant argument, (arg index, constant value).
+pub type ConstantArg = (usize, ScalarValue);
+
+/// Factory that returns the functions's return type given the input argument types and constant arguments
+pub trait ReturnTypeFactory: Send + Sync {
+    fn infer(
+        &self,
+        input_types: &[DataType],
+        constant_args: &[ConstantArg],
+    ) -> Result<Arc<DataType>>;
+}
+
 /// Factory that returns the functions's return type given the input argument types
 pub type ReturnTypeFunction =

Review Comment:
   Perhaps we could introduce the `FunctionImplementation` trait as proposed here: https://github.com/apache/arrow-datafusion/pull/8046/files#diff-8a327db2db945bcf6ca2b4229885532feae127e94a450600d3fac6ecdc0eeb3fR141 
   
   with the more general return types. Something like this perhaps:
   
   ```rust
   /// Convenience trait for implementing ScalarUDF. See [`ScalarUDF::new_from_impl()`]
   pub trait FunctionImplementation {
       /// Returns this function's name
       fn name(&self) -> &str;
   
       /// Returns this function's signature
       fn signature(&self) -> &Signature;
   
       /// return the return type of this function given the types of the arguments
       fn return_type(&self, arg_types: &[DataType]) -> Result<DataType>;
   
       /// return the return type of this function given the actual arguments. This is
       /// used to implement functions where the return type is a function of the actual
       /// arguments
       fn return_type_from_args(&self, args: &[Expr], schemabe: &dyn ExprSchemable) -> Result<DataType> {
         // default impl would call `Self::return_type`
         todo!()
       }
   
   
       /// Invoke the function on `args`, returning the appropriate result
       fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue>;
   }
   ```
   



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


Re: [PR] feat: refactor udf/udaf/udwf ReturnType [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #8183:
URL: https://github.com/apache/arrow-datafusion/pull/8183#issuecomment-1816947186

   marking as draft as I think we are working on some other items now


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


Re: [PR] feat: refactor udf/udaf/udwf ReturnType [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #8183:
URL: https://github.com/apache/arrow-datafusion/pull/8183#issuecomment-1813292957

   Another idea @JasonLi-cn  might be to pursue this idea https://github.com/apache/arrow-datafusion/issues/8051 to "specialize" the function based on its arguments (and in this case the specialization could potentially also include the constants 🤔 )


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


Re: [PR] feat: refactor udf/udaf/udwf ReturnType [arrow-datafusion]

Posted by "JasonLi-cn (via GitHub)" <gi...@apache.org>.
JasonLi-cn commented on PR #8183:
URL: https://github.com/apache/arrow-datafusion/pull/8183#issuecomment-1818450082

   > marking as draft as I think @JasonLi-cn is working on feedback now
   
   Ok, I will try to complete this task


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