-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: support prepare statement #4490
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
Changes from all commits
f7408fc
ff0dc10
5a50288
e5ec877
678a30b
e04da97
860f85d
ba8ad0f
67cbfdd
001251c
5d78097
1344395
87787da
199b657
cdb328c
58a3117
30705d9
87a3523
91e104a
dd6c3e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # 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. | ||
|
|
||
| ########## | ||
| ## Prepare Statement Tests | ||
| ########## | ||
|
|
||
| statement ok | ||
| create table person (id int, first_name varchar, last_name varchar, age int, state varchar, salary double, birthday timestamp, "😀" int) as values (1, 'jane', 'smith', 20, 'MA', 100000.45, '2000-11-12T00:00:00'::timestamp, 99); | ||
|
|
||
| query C rowsort | ||
| select * from person; | ||
| ---- | ||
| 1 jane smith 20 MA 100000.45 2000-11-12T00:00:00.000000000 99 | ||
|
|
||
| # Error due to syntax and semantic violation | ||
|
|
||
| # Syntax error: no name specified after the keyword prepare | ||
| statement error | ||
| PREPARE AS SELECT id, age FROM person WHERE age = $foo; | ||
|
|
||
| # param following a non-number, $foo, not supported | ||
| statement error | ||
| PREPARE my_plan(INT) AS SELECT id, age FROM person WHERE age = $foo; | ||
|
|
||
| # not specify table hence cannot specify columns | ||
| statement error | ||
| PREPARE my_plan(INT) AS SELECT id + $1; | ||
|
|
||
| # not specify data types for all params | ||
| statement error | ||
| PREPARE my_plan(INT) AS SELECT 1 + $1 + $2; | ||
|
|
||
| # cannot use IS param | ||
| statement error | ||
| PREPARE my_plan(INT) AS SELECT id, age FROM person WHERE age is $1; | ||
|
|
||
| # ####################### | ||
| # TODO: all the errors below should work ok after we store the prepare logical plan somewhere | ||
| statement error | ||
| PREPARE my_plan(STRING, STRING) AS SELECT * FROM (VALUES(1, $1), (2, $2)) AS t (num, letter); | ||
|
|
||
| statement error | ||
| PREPARE my_plan(INT) AS SELECT id, age FROM person WHERE age = 10; | ||
|
|
||
| statement error | ||
| PREPARE my_plan AS SELECT id, age FROM person WHERE age = 10; | ||
|
|
||
| statement error | ||
| PREPARE my_plan(INT) AS SELECT $1; | ||
|
|
||
| statement error | ||
| PREPARE my_plan(INT) AS SELECT 1 + $1; | ||
|
|
||
| statement error | ||
| PREPARE my_plan(INT, DOUBLE) AS SELECT 1 + $1 + $2; | ||
|
|
||
| statement error | ||
| PREPARE my_plan(INT) AS SELECT id, age FROM person WHERE age = $1; | ||
|
|
||
| statement error | ||
| PREPARE my_plan(INT, STRING, DOUBLE, INT, DOUBLE, STRING) AS SELECT id, age, $6 FROM person WHERE age IN ($1, $4) AND salary > $3 and salary < $5 OR first_name < $2"; | ||
|
|
||
| statement error | ||
| PREPARE my_plan(INT, DOUBLE, DOUBLE, DOUBLE) AS SELECT id, SUM(age) FROM person WHERE salary > $2 GROUP BY id HAVING sum(age) < $1 AND SUM(age) > 10 OR SUM(age) in ($3, $4); | ||
|
|
||
| statement error | ||
| PREPARE my_plan(STRING, STRING) AS SELECT * FROM (VALUES(1, $1), (2, $2)) AS t (num, letter); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -244,6 +244,14 @@ pub enum Expr { | |
| /// List of grouping set expressions. Only valid in the context of an aggregate | ||
| /// GROUP BY expression list | ||
| GroupingSet(GroupingSet), | ||
| /// A place holder for parameters in a prepared statement | ||
| /// (e.g. `$foo` or `$1`) | ||
| Placeholder { | ||
| /// The identifier of the parameter (e.g, $1 or $foo) | ||
| id: String, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| /// The type the parameter will be filled in with | ||
| data_type: DataType, | ||
| }, | ||
| } | ||
|
|
||
| /// Binary expression | ||
|
|
@@ -528,6 +536,7 @@ impl Expr { | |
| Expr::Literal(..) => "Literal", | ||
| Expr::Negative(..) => "Negative", | ||
| Expr::Not(..) => "Not", | ||
| Expr::Placeholder { .. } => "Placeholder", | ||
| Expr::QualifiedWildcard { .. } => "QualifiedWildcard", | ||
| Expr::ScalarFunction { .. } => "ScalarFunction", | ||
| Expr::ScalarSubquery { .. } => "ScalarSubquery", | ||
|
|
@@ -980,6 +989,7 @@ impl fmt::Debug for Expr { | |
| ) | ||
| } | ||
| }, | ||
| Expr::Placeholder { id, .. } => write!(f, "{}", id), | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -1263,6 +1273,7 @@ fn create_name(e: &Expr) -> Result<String> { | |
| Expr::QualifiedWildcard { .. } => Err(DataFusionError::Internal( | ||
| "Create name does not support qualified wildcard".to_string(), | ||
| )), | ||
| Expr::Placeholder { id, .. } => Ok((*id).to_string()), | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,6 +110,8 @@ pub enum LogicalPlan { | |
| Distinct(Distinct), | ||
| /// Set a Variable | ||
| SetVariable(SetVariable), | ||
| /// Prepare a statement | ||
| Prepare(Prepare), | ||
alamb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| impl LogicalPlan { | ||
|
|
@@ -136,6 +138,7 @@ impl LogicalPlan { | |
| LogicalPlan::CreateExternalTable(CreateExternalTable { schema, .. }) => { | ||
| schema | ||
| } | ||
| LogicalPlan::Prepare(Prepare { input, .. }) => input.schema(), | ||
| LogicalPlan::Explain(explain) => &explain.schema, | ||
| LogicalPlan::Analyze(analyze) => &analyze.schema, | ||
| LogicalPlan::Extension(extension) => extension.node.schema(), | ||
|
|
@@ -203,8 +206,9 @@ impl LogicalPlan { | |
| | LogicalPlan::Sort(Sort { input, .. }) | ||
| | LogicalPlan::CreateMemoryTable(CreateMemoryTable { input, .. }) | ||
| | LogicalPlan::CreateView(CreateView { input, .. }) | ||
| | LogicalPlan::Filter(Filter { input, .. }) => input.all_schemas(), | ||
| LogicalPlan::Distinct(Distinct { input, .. }) => input.all_schemas(), | ||
| | LogicalPlan::Filter(Filter { input, .. }) | ||
| | LogicalPlan::Distinct(Distinct { input, .. }) | ||
| | LogicalPlan::Prepare(Prepare { input, .. }) => input.all_schemas(), | ||
| LogicalPlan::DropTable(_) | ||
| | LogicalPlan::DropView(_) | ||
| | LogicalPlan::SetVariable(_) => vec![], | ||
|
|
@@ -273,7 +277,8 @@ impl LogicalPlan { | |
| | LogicalPlan::Analyze(_) | ||
| | LogicalPlan::Explain(_) | ||
| | LogicalPlan::Union(_) | ||
| | LogicalPlan::Distinct(_) => { | ||
| | LogicalPlan::Distinct(_) | ||
| | LogicalPlan::Prepare(_) => { | ||
| vec![] | ||
| } | ||
| } | ||
|
|
@@ -302,7 +307,8 @@ impl LogicalPlan { | |
| LogicalPlan::Explain(explain) => vec![&explain.plan], | ||
| LogicalPlan::Analyze(analyze) => vec![&analyze.input], | ||
| LogicalPlan::CreateMemoryTable(CreateMemoryTable { input, .. }) | ||
| | LogicalPlan::CreateView(CreateView { input, .. }) => { | ||
| | LogicalPlan::CreateView(CreateView { input, .. }) | ||
| | LogicalPlan::Prepare(Prepare { input, .. }) => { | ||
| vec![input] | ||
| } | ||
| // plans without inputs | ||
|
|
@@ -450,9 +456,8 @@ impl LogicalPlan { | |
| input.accept(visitor)? | ||
| } | ||
| LogicalPlan::CreateMemoryTable(CreateMemoryTable { input, .. }) | ||
| | LogicalPlan::CreateView(CreateView { input, .. }) => { | ||
| input.accept(visitor)? | ||
| } | ||
| | LogicalPlan::CreateView(CreateView { input, .. }) | ||
| | LogicalPlan::Prepare(Prepare { input, .. }) => input.accept(visitor)?, | ||
| LogicalPlan::Extension(extension) => { | ||
| for input in extension.node.inputs() { | ||
| if !input.accept(visitor)? { | ||
|
|
@@ -963,6 +968,11 @@ impl LogicalPlan { | |
| LogicalPlan::Analyze { .. } => write!(f, "Analyze"), | ||
| LogicalPlan::Union(_) => write!(f, "Union"), | ||
| LogicalPlan::Extension(e) => e.node.fmt_for_explain(f), | ||
| LogicalPlan::Prepare(Prepare { | ||
| name, data_types, .. | ||
| }) => { | ||
| write!(f, "Prepare: {:?} {:?} ", name, data_types) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -1373,6 +1383,18 @@ pub struct CreateExternalTable { | |
| pub options: HashMap<String, String>, | ||
| } | ||
|
|
||
| /// Prepare a statement but do not execute it. Prepare statements can have 0 or more | ||
| /// `Expr::Placeholder` expressions that are filled in during execution | ||
| #[derive(Clone)] | ||
| pub struct Prepare { | ||
| /// The name of the statement | ||
| pub name: String, | ||
| /// Data types of the parameters ([`Expr::Placeholder`]) | ||
| pub data_types: Vec<DataType>, | ||
| /// The logical plan of the statements | ||
| pub input: Arc<LogicalPlan>, | ||
| } | ||
|
|
||
|
Comment on lines
+1393
to
+1397
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume the data types Vec size is the same with the place holders in the input plan, but is there any check for this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The data types of the placeholders are from the data types of the Vec here so they match. We do check if the Vec contains enough params, too. However, there are flexibility:
However, I am working on #4550 that convert Prepare Logical Plan to a logical plan with all placeholders replaced with actual values. There, I will throw error if the data types provided do not work. We follow the same behavior of Postgres |
||
| /// Produces a relation with string representations of | ||
| /// various parts of the plan | ||
| #[derive(Clone)] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,8 +22,8 @@ use crate::expr_visitor::{ExprVisitable, ExpressionVisitor, Recursion}; | |
| use crate::logical_plan::builder::build_join_schema; | ||
| use crate::logical_plan::{ | ||
| Aggregate, Analyze, CreateMemoryTable, CreateView, Distinct, Extension, Filter, Join, | ||
| Limit, Partitioning, Projection, Repartition, Sort, Subquery, SubqueryAlias, Union, | ||
| Values, Window, | ||
| Limit, Partitioning, Prepare, Projection, Repartition, Sort, Subquery, SubqueryAlias, | ||
| Union, Values, Window, | ||
| }; | ||
| use crate::{Cast, Expr, ExprSchemable, LogicalPlan, LogicalPlanBuilder}; | ||
| use arrow::datatypes::{DataType, TimeUnit}; | ||
|
|
@@ -126,7 +126,8 @@ impl ExpressionVisitor for ColumnNameVisitor<'_> { | |
| | Expr::ScalarSubquery(_) | ||
| | Expr::Wildcard | ||
| | Expr::QualifiedWildcard { .. } | ||
| | Expr::GetIndexedField { .. } => {} | ||
| | Expr::GetIndexedField { .. } | ||
| | Expr::Placeholder { .. } => {} | ||
| } | ||
| Ok(Recursion::Continue(self)) | ||
| } | ||
|
|
@@ -579,6 +580,13 @@ pub fn from_plan( | |
|
|
||
| Ok(plan.clone()) | ||
| } | ||
| LogicalPlan::Prepare(Prepare { | ||
| name, data_types, .. | ||
| }) => Ok(LogicalPlan::Prepare(Prepare { | ||
| name: name.clone(), | ||
| data_types: data_types.clone(), | ||
| input: Arc::new(inputs[0].clone()), | ||
| })), | ||
|
Comment on lines
+586
to
+589
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible and allowed here that the method passed in a totally different input plan ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the code we implement, the answer is no unless there are bugs |
||
| LogicalPlan::EmptyRelation(_) | ||
| | LogicalPlan::TableScan { .. } | ||
| | LogicalPlan::CreateExternalTable(_) | ||
|
|
||
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.
Since we do not store the prepare logical plan anywhere and still throw error when we try to generate physical plan for it, this .stl tests are not that useful yet but they will be valuable after those are done
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.
Perhaps I can add a subtask to implement it under #4539 -- then in DataFusion we normally leave a link to the ticket as a comment
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.
All corresponding prepare tests are added here. They will be available for testing (mean many
statement errorwill becomestatement ok) after we store the prepare logical plan and not throw error