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

Cache the arguments type to avoid recomputing for ScalarUDF::return_type_from_exprs #9518

Closed
jayzhan211 opened this issue Mar 9, 2024 · 0 comments · Fixed by #9522
Closed
Labels
enhancement New feature or request

Comments

@jayzhan211
Copy link
Contributor

jayzhan211 commented Mar 9, 2024

Is your feature request related to a problem or challenge?

In #9504, we have an issue that select array_ndims([[[[[[[[[[[[[[[[[[[[[1]]]]]]]]]]]]]]]]]]]]]) is taking so long time compare with the main branch.

I found out that we always calculate the arguments type every time, which is really expensive for the expression like above.

https://github.com/apache/arrow-datafusion/blob/5537572820977b38719e2253f601a159deef5bc6/datafusion/expr/src/expr_schema.rs#L141-L156

UDF now always go through return_type_from_exprs

https://github.com/apache/arrow-datafusion/blob/5537572820977b38719e2253f601a159deef5bc6/datafusion/expr/src/udf.rs#L306-L316

Always recompute types here.

I test it locally that after avoiding the re-computation, at least this query gets super fast as the main branch does.

Describe the solution you'd like

  1. We can cache the result for the same arguments and schema pair, but we need to check the matching every time too.
  2. We can add another argument args_types: &[DataType] for return_type_from_exprs. If the args_types is empty, we calculate the types, otherwise we skip it.
  3. We can compute the return type along with udf creation 🤔

https://github.com/apache/arrow-datafusion/blob/5537572820977b38719e2253f601a159deef5bc6/datafusion/sql/src/expr/function.rs#L69-L72

We have schema for ScalarFunction::new_udf, it is possible

It looks like 3 is better if the schema is usually fixed, otherwise 2. 1 if we need to re-compute args_types in many place, but it doesn't seem to be true for now.

Describe alternatives you've considered

Other better design

Additional context

To reproduce, you can play around on commit 970dd7d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant