|
| 1 | +<!--- |
| 2 | + Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + or more contributor license agreements. See the NOTICE file |
| 4 | + distributed with this work for additional information |
| 5 | + regarding copyright ownership. The ASF licenses this file |
| 6 | + to you under the Apache License, Version 2.0 (the |
| 7 | + "License"); you may not use this file except in compliance |
| 8 | + with the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, |
| 13 | + software distributed under the License is distributed on an |
| 14 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + KIND, either express or implied. See the License for the |
| 16 | + specific language governing permissions and limitations |
| 17 | + under the License. |
| 18 | +--> |
| 19 | + |
| 20 | +# Prepared Statements |
| 21 | + |
| 22 | +The `PREPARE` statement allows for the creation and storage of a SQL statement with placeholder arguments. |
| 23 | + |
| 24 | +The prepared statements can then be executed repeatedly in an efficient manner. |
| 25 | + |
| 26 | +**SQL Example** |
| 27 | + |
| 28 | +Create a prepared statement `greater_than` that selects all records where column "a" is greater than the parameter: |
| 29 | + |
| 30 | +```sql |
| 31 | +PREPARE greater_than(INT) AS SELECT * FROM example WHERE a > $1; |
| 32 | +``` |
| 33 | + |
| 34 | +The prepared statement can then be executed with parameters as needed: |
| 35 | + |
| 36 | +```sql |
| 37 | +EXECUTE greater_than(20); |
| 38 | +``` |
| 39 | + |
| 40 | +**Rust Example** |
| 41 | + |
| 42 | +```rust |
| 43 | +use datafusion::prelude::*; |
| 44 | + |
| 45 | +#[tokio::main] |
| 46 | +async fn main() -> datafusion::error::Result<()> { |
| 47 | + // Register the table |
| 48 | + let ctx = SessionContext::new(); |
| 49 | + ctx.register_csv("example", "tests/data/example.csv", CsvReadOptions::new()).await?; |
| 50 | + |
| 51 | + // Create the prepared statement `greater_than` |
| 52 | + let prepare_sql = "PREPARE greater_than(INT) AS SELECT * FROM example WHERE a > $1"; |
| 53 | + ctx.sql(prepare_sql).await?; |
| 54 | + |
| 55 | + // Execute the prepared statement `greater_than` |
| 56 | + let execute_sql = "EXECUTE greater_than(20)"; |
| 57 | + let df = ctx.sql(execute_sql).await?; |
| 58 | + |
| 59 | + // Execute and print results |
| 60 | + df.show().await?; |
| 61 | + Ok(()) |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Inferred Types |
| 66 | + |
| 67 | +If the parameter type is not specified, it can be inferred at execution time: |
| 68 | + |
| 69 | +**SQL Example** |
| 70 | + |
| 71 | +Create the prepared statement `greater_than` |
| 72 | + |
| 73 | +```sql |
| 74 | +PREPARE greater_than AS SELECT * FROM example WHERE a > $1; |
| 75 | +``` |
| 76 | + |
| 77 | +Execute the prepared statement `greater_than` |
| 78 | + |
| 79 | +```sql |
| 80 | +EXECUTE greater_than(20); |
| 81 | +``` |
| 82 | + |
| 83 | +**Rust Example** |
| 84 | + |
| 85 | +```rust |
| 86 | +# use datafusion::prelude::*; |
| 87 | +# #[tokio::main] |
| 88 | +# async fn main() -> datafusion::error::Result<()> { |
| 89 | +# let ctx = SessionContext::new(); |
| 90 | +# ctx.register_csv("example", "tests/data/example.csv", CsvReadOptions::new()).await?; |
| 91 | +# |
| 92 | + // Create the prepared statement `greater_than` |
| 93 | + let prepare_sql = "PREPARE greater_than AS SELECT * FROM example WHERE a > $1"; |
| 94 | + ctx.sql(prepare_sql).await?; |
| 95 | + |
| 96 | + // Execute the prepared statement `greater_than` |
| 97 | + let execute_sql = "EXECUTE greater_than(20)"; |
| 98 | + let df = ctx.sql(execute_sql).await?; |
| 99 | +# |
| 100 | +# Ok(()) |
| 101 | +# } |
| 102 | +``` |
| 103 | + |
| 104 | +## Positional Arguments |
| 105 | + |
| 106 | +In the case of multiple parameters, prepared statements can use positional arguments: |
| 107 | + |
| 108 | +**SQL Example** |
| 109 | + |
| 110 | +Create the prepared statement `greater_than` |
| 111 | + |
| 112 | +```sql |
| 113 | +PREPARE greater_than(INT, DOUBLE) AS SELECT * FROM example WHERE a > $1 AND b > $2; |
| 114 | +``` |
| 115 | + |
| 116 | +Execute the prepared statement `greater_than` |
| 117 | + |
| 118 | +```sql |
| 119 | +EXECUTE greater_than(20, 23.3); |
| 120 | +``` |
| 121 | + |
| 122 | +**Rust Example** |
| 123 | + |
| 124 | +```rust |
| 125 | +# use datafusion::prelude::*; |
| 126 | +# #[tokio::main] |
| 127 | +# async fn main() -> datafusion::error::Result<()> { |
| 128 | +# let ctx = SessionContext::new(); |
| 129 | +# ctx.register_csv("example", "tests/data/example.csv", CsvReadOptions::new()).await?; |
| 130 | + // Create the prepared statement `greater_than` |
| 131 | + let prepare_sql = "PREPARE greater_than(INT, DOUBLE) AS SELECT * FROM example WHERE a > $1 AND b > $2"; |
| 132 | + ctx.sql(prepare_sql).await?; |
| 133 | + |
| 134 | + // Execute the prepared statement `greater_than` |
| 135 | + let execute_sql = "EXECUTE greater_than(20, 23.3)"; |
| 136 | + let df = ctx.sql(execute_sql).await?; |
| 137 | +# Ok(()) |
| 138 | +# } |
| 139 | +``` |
0 commit comments