Add documentation and usability for prepared parameters#7785
Merged
Conversation
530a433 to
b94ed82
Compare
b94ed82 to
f79bbe4
Compare
5 tasks
alamb
commented
Oct 10, 2023
| /// .sql("SELECT a FROM example WHERE b = $1") | ||
| /// .await? | ||
| /// // replace $1 with value 2 | ||
| /// .with_param_values(vec![ScalarValue::from(2i64)])? |
Contributor
Author
There was a problem hiding this comment.
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 |
|
|
||
| /// 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> { |
Contributor
Author
There was a problem hiding this comment.
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(""))) |
Contributor
Author
There was a problem hiding this comment.
here is an example of the new placeholder function being much nicer to use
Contributor
Author
|
cc @NGA-TRAN |
NGA-TRAN
approved these changes
Oct 13, 2023
| /// ); | ||
| /// # Ok(()) | ||
| /// # } | ||
| /// ``` |
| #[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"; |
Contributor
Author
|
@avantgardnerio might you have time to review (and approve) this PR? I can't merge it without an approval from another committer. |
avantgardnerio
approved these changes
Oct 15, 2023
avantgardnerio
left a comment
Contributor
There was a problem hiding this comment.
Seems minor and innocuous, and adds a test 👍
jackwener
approved these changes
Oct 16, 2023
jackwener
reviewed
Oct 16, 2023
jackwener
approved these changes
Oct 16, 2023
Co-authored-by: jakevin <jakevingoo@gmail.com>
Contributor
Author
|
Thanks @jackwener and @avantgardnerio and @NGA-TRAN |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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