Skip to content
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

Add documentation and usability for prepared parameters #7785

Merged
merged 3 commits into from
Oct 16, 2023

Conversation

alamb
Copy link
Contributor

@alamb alamb commented Oct 10, 2023

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?

  1. Doc comments for LogicalPlan::with_param_values and YYY with examples
  2. Add placeholder to expr_fn function to more easily construct placeholders
  3. Move infer_placeholder_values to Expr, and make it public (so it can be used outside of the context of a SQL statement)
  4. Change LogicalPlan::with_param_values to also substitute parameter values for non PREPARE statements as well

Are 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

@github-actions github-actions bot added sql SQL Planner logical-expr Logical plan and expressions core Core DataFusion crate labels Oct 10, 2023
@alamb alamb force-pushed the alamb/prepared_statement_docs branch from 530a433 to b94ed82 Compare October 10, 2023 13:07
@alamb alamb force-pushed the alamb/prepared_statement_docs branch from b94ed82 to f79bbe4 Compare October 10, 2023 13:11
@alamb alamb mentioned this pull request Oct 10, 2023
5 tasks
/// .sql("SELECT a FROM example WHERE b = $1")
/// .await?
/// // replace $1 with value 2
/// .with_param_values(vec![ScalarValue::from(2i64)])?
Copy link
Contributor Author

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

@@ -1060,7 +1089,7 @@ impl LogicalPlan {
}

impl LogicalPlan {
/// applies collect to any subqueries in the plan
/// applies `op` to any subqueries in the plan
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive by cleanups

datafusion/expr/src/expr_fn.rs Show resolved Hide resolved

/// 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> {
Copy link
Contributor Author

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("")))
Copy link
Contributor Author

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

@alamb alamb marked this pull request as ready for review October 10, 2023 13:29
@alamb alamb self-assigned this Oct 10, 2023
@alamb
Copy link
Contributor Author

alamb commented Oct 10, 2023

cc @NGA-TRAN

Copy link
Contributor

@NGA-TRAN NGA-TRAN left a 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(())
/// # }
/// ```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

datafusion/expr/src/expr_fn.rs Show resolved Hide resolved
#[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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@alamb
Copy link
Contributor Author

alamb commented Oct 13, 2023

@avantgardnerio might you have time to review (and approve) this PR? I can't merge it without an approval from another committer.

Copy link
Contributor

@avantgardnerio avantgardnerio left a 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>
@alamb
Copy link
Contributor Author

alamb commented Oct 16, 2023

Thanks @jackwener and @avantgardnerio and @NGA-TRAN

@alamb alamb merged commit d33595a into apache:main Oct 16, 2023
23 checks passed
@alamb alamb deleted the alamb/prepared_statement_docs branch October 16, 2023 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate logical-expr Logical plan and expressions sql SQL Planner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Is it possible to pass query parameters in a SQL query?
4 participants