Skip to content

Add documentation and usability for prepared parameters#7785

Merged
alamb merged 3 commits into
apache:mainfrom
alamb:alamb/prepared_statement_docs
Oct 16, 2023
Merged

Add documentation and usability for prepared parameters#7785
alamb merged 3 commits into
apache:mainfrom
alamb:alamb/prepared_statement_docs

Conversation

@alamb

@alamb alamb commented Oct 10, 2023

Copy link
Copy Markdown
Contributor

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
Comment thread 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)])?

Copy link
Copy Markdown
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


impl LogicalPlan {
/// applies collect to any subqueries in the plan
/// applies `op` to any subqueries in the plan

Copy link
Copy Markdown
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

Comment thread datafusion/expr/src/expr_fn.rs

/// 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
Copy Markdown
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
Copy Markdown
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

alamb commented Oct 10, 2023

Copy link
Copy Markdown
Contributor Author

cc @NGA-TRAN

@NGA-TRAN NGA-TRAN left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

❤️

Comment thread datafusion/expr/src/expr_fn.rs
#[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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice

@alamb alamb requested a review from avantgardnerio October 13, 2023 15:10
@alamb

alamb commented Oct 13, 2023

Copy link
Copy Markdown
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 avantgardnerio left a comment

Copy link
Copy Markdown
Contributor

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 👍

Comment thread datafusion/expr/src/expr.rs Outdated
Co-authored-by: jakevin <jakevingoo@gmail.com>
@alamb

alamb commented Oct 16, 2023

Copy link
Copy Markdown
Contributor Author

Thanks @jackwener and @avantgardnerio and @NGA-TRAN

@alamb alamb merged commit d33595a into apache:main Oct 16, 2023
@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