Repository navigation
Closed
Labels
enhancementNew feature or requestNew feature or request
Description
Is your feature request related to a problem or challenge?
As previously discussed in #7110 #7752 : We want to define Scalar Functions outside the core to reduce Datafusion Core binary size, and also make UDF management easy.
But the current BuiltinScalarFunction and ScalarUDF interfaces are not capable to do this:
Problems with BuiltinScalarFunction
- built-in functions are implemented with
Enum BuiltinScalarFunction, and function implementations likereturn_type()are large methods that match every enum variant. Adding a new function requires modifications in multiple places (not easy to add functions), also we can't define scalar functions outside the core.
Problems with ScalarUDF
- Now
ScalarUDFs are represented with a struct, it can not cover all the functionalities of existing built-in functions - Defining a new
ScalarUDFrequires constructing a struct in an imperative way (see examples/simple_udf.rs), this way is not ergonomic especially when you have to manage large number of functions in a separate crate.
So we would like to introduce a new interface trait ScalarFunctionDef: it can define a function declaratively and in one place (easier than Enum BuiltinScalarFunction's adding arms for pattern matching in multiple places, and struct ScalarUDF's imperative way of defining a new function.
After introducing the new interface we can gradually migrating existing built-in functions to the new one, the old UDF interface create_udf() can keep unchanged.
Describe the solution you'd like
Objective
- Define a new interface
trait ScalarFunctionDefto solve the above-mentioned issue - Now built-in functions are using
Enum BuiltinScalarFunctionas the underlying execution mechanism in the core code, and scalar UDFs are usingStruct ScalarUDF, both will be moving toScalarFunctionDefas the unified internal representation.
Implementation Plan
- Introducing new interface
ScalarFunctionDefand replace the old built-in functions' execution code
1.1 Replace SQL execution code withScalarFunctionDef
1.2 Change other relevant execution components, like the Logical Expression Constructor for Scalar Functions, to be compatible withScalarFunctionDef. - Migrate old UDF Implementations:
Struct ScalarUDF->ScalarFunctionDef - Function package management interface
...and migrating existing functions
Describe alternatives you've considered
No response
Additional context
No response
Reacted by Andrew Lamb
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request
Activity
viirya commentedon Oct 30, 2023
Out of curiosity to read this ticket. For such as proposal to change important APIs, it will be better to make this issue more trackable and understandable for all who are interested. Could you add what existing APIs lack of and why they cannot meet the requirement into the description? Just only a sentence saying "they are not capable to do this" is not enough and too rough.
2010YOUY01 commentedon Oct 30, 2023
Sure, I updated the description with the summary of previous discussions
[-]Moving to the new Scalar Function interface[/-][+]Epic: A new Scalar Function interface[/+][-]Epic: A new Scalar Function interface[/-][+][Epic] A new Scalar Function interface[/+]alamb commentedon Oct 31, 2023
Here is an example of what a NEW api might look like
Existing API
(BTW this took me non trivial time to get the types to line up and compile correctly)
Here is what such a function could look like as a trait, which I think is much more approachable to new users (in addition to being much more easily separated out, as @2010YOUY01 mentions above)
alamb commentedon Nov 3, 2023
Here is a slightly different proposal (basically extend ScalarUDF to support all features of
BuiltInScalarFunctionand then removeBuiltInScalarFunction): #8039alamb commentedon Nov 3, 2023
Here is a slightly different proposal: #8045
alamb commentedon Nov 6, 2023
@2010YOUY01 what do you think about closing this ticket / unifying on #8045 ?