-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add documentation and usability for prepared parameters #7785
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
530a433 to
b94ed82
Compare
b94ed82 to
f79bbe4
Compare
datafusion/core/src/dataframe.rs
Outdated
| /// .sql("SELECT a FROM example WHERE b = $1") | ||
| /// .await? | ||
| /// // replace $1 with value 2 | ||
| /// .with_param_values(vec![ScalarValue::from(2i64)])? |
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.
this tests the DataFrame API and fails without the changes to LogicalPlan::with_param_values
|
|
||
| impl LogicalPlan { | ||
| /// applies collect to any subqueries in the plan | ||
| /// applies `op` to any subqueries in the plan |
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.
drive by cleanups
|
|
||
| /// Find all [`Expr::Placeholder`] in anthis, and try | ||
| /// to infer their [`DataType`] from the context of their use. | ||
| pub fn infer_placeholder_types(self, schema: &DFSchema) -> Result<Expr> { |
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.
This was moved from the datafusion-sql module as it is not SQL specific, but generic to expressions
| "".into(), | ||
| Some(DataType::Int32), | ||
| )))) | ||
| .filter(col("id").eq(placeholder(""))) |
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.
here is an example of the new placeholder function being much nicer to use
|
cc @NGA-TRAN |
NGA-TRAN
left a comment
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.
Thanks so much @alamb for the cleanup and document work
| /// ); | ||
| /// # 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.
❤️
| #[test] | ||
| fn test_non_prepare_statement_should_infer_types() { | ||
| // Non prepared statements (like SELECT) should also have their parameter types inferred | ||
| let sql = "SELECT 1 + $1"; |
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.
Nice
|
@avantgardnerio might you have time to review (and approve) this PR? I can't merge it without an approval from another committer. |
avantgardnerio
left a comment
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.
Seems minor and innocuous, and adds a test 👍
Co-authored-by: jakevin <jakevingoo@gmail.com>
|
Thanks @jackwener and @avantgardnerio and @NGA-TRAN |
Which issue does this PR close?
Closes #7776
related to #7776 and #4539
Rationale for this change
While reviewing #7776, I noticed that the parameter values were not well documented. So I started trying to write documentation examples and in so doing found several usability papercuts
that I I also fixed.
What changes are included in this PR?
LogicalPlan::with_param_valuesand YYY with examplesplaceholdertoexpr_fnfunction to more easily construct placeholdersinfer_placeholder_valuesto Expr, and make it public (so it can be used outside of the context of a SQL statement)LogicalPlan::with_param_valuesto also substitute parameter values for nonPREPAREstatements as wellAre these changes tested?
Yes, new tests / doc comments
Are there any user-facing changes?
Better docs
Note it is still not possible to use PREPARE / EXCUTE from SQL but I think this PR completes the backend code to make it work