-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: support named variables & defaults for CREATE FUNCTION
#18450
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
Conversation
| // Validate parameter style | ||
| if let Some(ref fields) = arg_types { | ||
| let count_positional = | ||
| fields.iter().filter(|f| f.name() == "").count(); | ||
| if !(count_positional == 0 || count_positional == fields.len()) { | ||
| return plan_err!("All function arguments must use either named or positional style."); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note I'm not sure this is actually necessary, but it made it easier to reason about the changes. If we think this is valuable I can look at relaxing this constraint.
| // FIXME: This branch is shared by params from PREPARE and CREATE FUNCTION, but | ||
| // only CREATE FUNCTION currently supports named params. For now, we rewrite | ||
| // these to positional params. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note I explored doing this without rewriting to positional params, but I couldn't see a path forward without either:
- Adding a way to distinguish between prepared statement vs SQL UDF context
- Supporting named params in prepared statements (not currently supported in sqlparser AFAICT)
|
Test failure seems like a flake: |
CREATE FUNCTION
datafusion/core/tests/user_defined/user_defined_scalar_functions.rs
Outdated
Show resolved
Hide resolved
datafusion/core/tests/user_defined/user_defined_scalar_functions.rs
Outdated
Show resolved
Hide resolved
93314e6 to
8ddaae0
Compare
|
Thanks for the feedback @Jefffrey, updated |
| assert!(expected.starts_with(&err.strip_backtrace())); | ||
| Ok(()) | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test case with positional parameter that is not existing, e.g. $5 where there are less than 5 arguments.
I have the feeling that it will fail with index out of bounds error at https://github.com/apache/datafusion/pull/18450/files#diff-647d2e08b4d044bf63b35f9e23092ba9673b80b1568e8f3abffd7f909552ea1aR999
You need to add a check similar to if placeholder_position < defaults.len() {...} around it and return an error in the else clause
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Proposed a fix for the DEFAULT syntax bug upstream: apache/datafusion-sqlparser-rs#2091
8ddaae0 to
0330adb
Compare
Which issue does this PR close?
$1and support default values inCREATE FUNCTION#17887.Rationale for this change
See linked issue above
What changes are included in this PR?
CreateFunctionCreateFunctionparameter names are now preserved from the parse treePlaceholderScalarFunctionWrapperto handle defaultsAre these changes tested?
Yes, see added / adjusted unit tests
Are there any user-facing changes?
Yes, if the approach here is acceptable we should update the SQL UDF examples in
datafusion-examples/examples/function_factory.rs(TODO).Also, note that due to ambiguity between
PREPAREandCREATE FUNCTIONparam context one error message now has reduced fidelity.This can now be triggered either by using named params in a prepared statement or when referencing an undefined named param in a SQL UDF. New message:
Finally, there are two additional user-facing errors when planning
CreateFunction: