-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Refactor: Unify Expr::ScalarFunction and Expr::ScalarUDF, introduce unresolved functions by name
#8258
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
Refactor: Unify Expr::ScalarFunction and Expr::ScalarUDF, introduce unresolved functions by name
#8258
Changes from 3 commits
5b6b98a
6a7ee87
f0ffeac
f59edb9
a6997ec
e9f04e5
71bb177
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -148,10 +148,8 @@ pub enum Expr { | |||||
| TryCast(TryCast), | ||||||
| /// A sort expression, that can be used to sort values. | ||||||
| Sort(Sort), | ||||||
| /// Represents the call of a built-in scalar function with a set of arguments. | ||||||
| /// Represents the call of a scalar function with a set of arguments. | ||||||
| ScalarFunction(ScalarFunction), | ||||||
| /// Represents the call of a user-defined scalar function with arguments. | ||||||
| ScalarUDF(ScalarUDF), | ||||||
| /// Represents the call of an aggregate built-in function with arguments. | ||||||
| AggregateFunction(AggregateFunction), | ||||||
| /// Represents the call of a window function with arguments. | ||||||
|
|
@@ -338,37 +336,55 @@ impl Between { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||||||
| /// Defines which implementation of a function for DataFusion to call. | ||||||
| pub enum ScalarFunctionDefinition { | ||||||
| /// Resolved to a `BuiltinScalarFunction` | ||||||
| /// There is plan to migrate `BuiltinScalarFunction` to UDF-based implementation (issue#8045) | ||||||
| /// This variant is planned to be removed in long term | ||||||
| BuiltIn(built_in_function::BuiltinScalarFunction), | ||||||
| /// Resolved to a user defined function | ||||||
| UDF(Arc<crate::ScalarUDF>), | ||||||
| /// A scalar function constructed with name. This variant can not be executed directly | ||||||
| /// and instead must be resolved to one of the other variants prior to physical planning. | ||||||
| Name(Arc<str>), | ||||||
| } | ||||||
|
|
||||||
| /// ScalarFunction expression invokes a built-in scalar function | ||||||
| #[derive(Clone, PartialEq, Eq, Hash, Debug)] | ||||||
| pub struct ScalarFunction { | ||||||
| /// The function | ||||||
| pub fun: built_in_function::BuiltinScalarFunction, | ||||||
| pub func_def: ScalarFunctionDefinition, | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the major change |
||||||
| /// List of expressions to feed to the functions as arguments | ||||||
| pub args: Vec<Expr>, | ||||||
| } | ||||||
|
|
||||||
| impl ScalarFunctionDefinition { | ||||||
| /// Function's name for display | ||||||
| pub fn name(&self) -> String { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to allow the callsites to decide if they needed to make the string copy -- so passing back
Suggested change
|
||||||
| match self { | ||||||
| ScalarFunctionDefinition::BuiltIn(builtin_func) => builtin_func.to_string(), | ||||||
| ScalarFunctionDefinition::UDF(udf) => udf.name().to_string(), | ||||||
| ScalarFunctionDefinition::Name(func_name) => func_name.as_ref().to_string(), | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl ScalarFunction { | ||||||
| /// Create a new ScalarFunction expression | ||||||
| pub fn new(fun: built_in_function::BuiltinScalarFunction, args: Vec<Expr>) -> Self { | ||||||
| Self { fun, args } | ||||||
| Self { | ||||||
| func_def: ScalarFunctionDefinition::BuiltIn(fun), | ||||||
| args, | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the major change |
||||||
|
|
||||||
| /// ScalarUDF expression invokes a user-defined scalar function [`ScalarUDF`] | ||||||
| /// | ||||||
| /// [`ScalarUDF`]: crate::ScalarUDF | ||||||
| #[derive(Clone, PartialEq, Eq, Hash, Debug)] | ||||||
| pub struct ScalarUDF { | ||||||
| /// The function | ||||||
| pub fun: Arc<crate::ScalarUDF>, | ||||||
| /// List of expressions to feed to the functions as arguments | ||||||
| pub args: Vec<Expr>, | ||||||
| } | ||||||
|
|
||||||
| impl ScalarUDF { | ||||||
| /// Create a new ScalarUDF expression | ||||||
| pub fn new(fun: Arc<crate::ScalarUDF>, args: Vec<Expr>) -> Self { | ||||||
| Self { fun, args } | ||||||
| /// Create a new ScalarFunction expression with a user-defined function (UDF) | ||||||
| pub fn new_udf(udf: Arc<crate::ScalarUDF>, args: Vec<Expr>) -> Self { | ||||||
| Self { | ||||||
| func_def: ScalarFunctionDefinition::UDF(udf), | ||||||
| args, | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -736,7 +752,6 @@ impl Expr { | |||||
| Expr::Placeholder(_) => "Placeholder", | ||||||
| Expr::ScalarFunction(..) => "ScalarFunction", | ||||||
| Expr::ScalarSubquery { .. } => "ScalarSubquery", | ||||||
| Expr::ScalarUDF(..) => "ScalarUDF", | ||||||
| Expr::ScalarVariable(..) => "ScalarVariable", | ||||||
| Expr::Sort { .. } => "Sort", | ||||||
| Expr::TryCast { .. } => "TryCast", | ||||||
|
|
@@ -1198,11 +1213,8 @@ impl fmt::Display for Expr { | |||||
| write!(f, " NULLS LAST") | ||||||
| } | ||||||
| } | ||||||
| Expr::ScalarFunction(func) => { | ||||||
| fmt_function(f, &func.fun.to_string(), false, &func.args, true) | ||||||
| } | ||||||
| Expr::ScalarUDF(ScalarUDF { fun, args }) => { | ||||||
| fmt_function(f, fun.name(), false, args, true) | ||||||
| Expr::ScalarFunction(ScalarFunction { func_def, args }) => { | ||||||
| fmt_function(f, &func_def.name(), false, args, true) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for example, this callsite doesn't need an owned |
||||||
| } | ||||||
| Expr::WindowFunction(WindowFunction { | ||||||
| fun, | ||||||
|
|
@@ -1534,11 +1546,8 @@ fn create_name(e: &Expr) -> Result<String> { | |||||
| } | ||||||
| } | ||||||
| } | ||||||
| Expr::ScalarFunction(func) => { | ||||||
| create_function_name(&func.fun.to_string(), false, &func.args) | ||||||
| } | ||||||
| Expr::ScalarUDF(ScalarUDF { fun, args }) => { | ||||||
| create_function_name(fun.name(), false, args) | ||||||
| Expr::ScalarFunction(ScalarFunction { func_def, args }) => { | ||||||
| create_function_name(&func_def.name(), false, args) | ||||||
| } | ||||||
| Expr::WindowFunction(WindowFunction { | ||||||
| fun, | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.