-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Merged
Merged
Refactor: Unify Expr::ScalarFunction
and Expr::ScalarUDF
, introduce unresolved functions by name
#8258
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5b6b98a
Refactor Expr::ScalarFunction
2010YOUY01 6a7ee87
Remove Expr::ScalarUDF
2010YOUY01 f0ffeac
review comments
2010YOUY01 f59edb9
make name() return &str
2010YOUY01 a6997ec
fix fmt
2010YOUY01 e9f04e5
Merge branch 'main' into refactor-expr
2010YOUY01 71bb177
fix after merge
2010YOUY01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,61 @@ 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 { | ||
fun: built_in_function::BuiltinScalarFunction, | ||
name: Arc<str>, | ||
}, | ||
/// 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, | ||
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) -> &str { | ||
match self { | ||
ScalarFunctionDefinition::BuiltIn { name, .. } => name.as_ref(), | ||
ScalarFunctionDefinition::UDF(udf) => udf.name(), | ||
ScalarFunctionDefinition::Name(func_name) => func_name.as_ref(), | ||
} | ||
} | ||
} | ||
|
||
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, | ||
name: Arc::from(fun.to_string()), | ||
}, | ||
args, | ||
} | ||
} | ||
} | ||
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 +758,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 +1219,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) | ||
} | ||
Expr::WindowFunction(WindowFunction { | ||
fun, | ||
|
@@ -1534,11 +1552,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, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.