-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add documentation for prepare statements. #14639
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,3 +35,4 @@ SQL Reference | |
| special_functions | ||
| sql_status | ||
| write_options | ||
| prepared_statements | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| <!--- | ||
| Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, | ||
| software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations | ||
| under the License. | ||
| --> | ||
|
|
||
| # Prepared Statements | ||
|
|
||
| The `PREPARE` statement allows for the creation and storage of a SQL statement with placeholder arguments. | ||
|
|
||
| The prepared statements can then be executed repeatedly in an efficient manner. | ||
|
|
||
| **SQL Example** | ||
|
|
||
| Create a prepared statement `greater_than` that selects all records where column "a" is greater than the parameter: | ||
|
|
||
| ```sql | ||
| PREPARE greater_than(INT) AS SELECT * FROM example WHERE a > $1; | ||
| ``` | ||
|
|
||
| The prepared statement can then be executed with parameters as needed: | ||
|
|
||
| ```sql | ||
| EXECUTE greater_than(20); | ||
| ``` | ||
|
|
||
| **Rust Example** | ||
|
|
||
| ```rust | ||
| use datafusion::prelude::*; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> datafusion::error::Result<()> { | ||
| // Register the table | ||
| let ctx = SessionContext::new(); | ||
| ctx.register_csv("example", "tests/data/example.csv", CsvReadOptions::new()).await?; | ||
|
|
||
| // Create the prepared statement `greater_than` | ||
| let prepare_sql = "PREPARE greater_than(INT) AS SELECT * FROM example WHERE a > $1"; | ||
| ctx.sql(prepare_sql).await?; | ||
|
|
||
| // Execute the prepared statement `greater_than` | ||
| let execute_sql = "EXECUTE greater_than(20)"; | ||
| let df = ctx.sql(execute_sql).await?; | ||
|
|
||
| // Execute and print results | ||
| df.show().await?; | ||
| Ok(()) | ||
| } | ||
| ``` | ||
|
|
||
| ## Inferred Types | ||
|
|
||
| If the parameter type is not specified, it can be inferred at execution time: | ||
|
|
||
| **SQL Example** | ||
|
|
||
| Create the prepared statement `greater_than` | ||
|
|
||
| ```sql | ||
| PREPARE greater_than AS SELECT * FROM example WHERE a > $1; | ||
| ``` | ||
|
|
||
| Execute the prepared statement `greater_than` | ||
|
|
||
| ```sql | ||
| EXECUTE greater_than(20); | ||
| ``` | ||
|
|
||
| **Rust Example** | ||
|
|
||
| ```rust | ||
| # use datafusion::prelude::*; | ||
| # #[tokio::main] | ||
| # async fn main() -> datafusion::error::Result<()> { | ||
| # let ctx = SessionContext::new(); | ||
| # ctx.register_csv("example", "tests/data/example.csv", CsvReadOptions::new()).await?; | ||
| # | ||
| // Create the prepared statement `greater_than` | ||
| let prepare_sql = "PREPARE greater_than AS SELECT * FROM example WHERE a > $1"; | ||
| ctx.sql(prepare_sql).await?; | ||
|
|
||
| // Execute the prepared statement `greater_than` | ||
| let execute_sql = "EXECUTE greater_than(20)"; | ||
| let df = ctx.sql(execute_sql).await?; | ||
| # | ||
| # Ok(()) | ||
| # } | ||
| ``` | ||
|
|
||
| ## Positional Arguments | ||
|
|
||
| In the case of multiple parameters, prepared statements can use positional arguments: | ||
|
|
||
| **SQL Example** | ||
|
|
||
| Create the prepared statement `greater_than` | ||
|
|
||
| ```sql | ||
| PREPARE greater_than(INT, DOUBLE) AS SELECT * FROM example WHERE a > $1 AND b > $2; | ||
| ``` | ||
|
|
||
| Execute the prepared statement `greater_than` | ||
|
|
||
| ```sql | ||
| EXECUTE greater_than(20, 23.3); | ||
| ``` | ||
|
|
||
| **Rust Example** | ||
|
|
||
| ```rust | ||
| # use datafusion::prelude::*; | ||
| # #[tokio::main] | ||
| # async fn main() -> datafusion::error::Result<()> { | ||
| # let ctx = SessionContext::new(); | ||
| # ctx.register_csv("example", "tests/data/example.csv", CsvReadOptions::new()).await?; | ||
| // Create the prepared statement `greater_than` | ||
| let prepare_sql = "PREPARE greater_than(INT, DOUBLE) AS SELECT * FROM example WHERE a > $1 AND b > $2"; | ||
| ctx.sql(prepare_sql).await?; | ||
|
|
||
| // Execute the prepared statement `greater_than` | ||
| let execute_sql = "EXECUTE greater_than(20, 23.3)"; | ||
| let df = ctx.sql(execute_sql).await?; | ||
| # Ok(()) | ||
| # } | ||
| ``` | ||
Oops, something went wrong.
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.
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.
In order to test that the doc examples run (and thus that they compile and remain working) we need to add an entry to
core/src/lib.rslike this:Then you can run the doc tests like this:
cargo test --doc -p datafusion -- prepared_statementSince I had the code checked out anyways I went ahead and pushed the code to test the examples (and make those tests pass) to this PR