You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/12/21 18:38:40 UTC
[GitHub] [arrow-datafusion] avantgardnerio opened a new pull request, #4701: Infer prepared statement parameter types
avantgardnerio opened a new pull request, #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701
# Which issue does this PR close?
Closes #4683.
# Rationale for this change
Described in issue.
# What changes are included in this PR?
1. Code in sql_to_rel to infer parameter type
2. A test for a particular type of statement with parameters
3. A method to get the inferred types
# Are these changes tested?
[x]
# Are there any user-facing changes?
FlightSQL implementers can respond with types.
# Additional Info
I'm opening this as a draft PR to discuss general approach. If folks are on board with it, I'll add support for a bunch more statement types and more tests.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#discussion_r1054829813
##########
datafusion/sql/src/planner.rs:
##########
@@ -970,6 +970,45 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
}
}
+ fn infer_placeholder_types(&self, expr: &mut Expr, schema: &DFSchema) -> Result<()> {
+ match expr {
+ Expr::BinaryExpr(BinaryExpr {
+ ref mut left,
+ ref mut right,
+ ..
+ }) => {
+ self.infer_placeholder_types(left, schema)?;
+ self.infer_placeholder_types(right, schema)?;
+ let lt = left.get_type(schema);
+ let rt = right.get_type(schema);
+ match (left, rt) {
+ (
+ box Expr::Placeholder {
Review Comment:
I think using an `ExprRewriter` https://docs.rs/datafusion-expr/15.0.0/datafusion_expr/expr_rewriter/trait.ExprRewriter.html# would let you do what you want -- rather than getting a `mut &` you get the entire `expr` by value (and can then rewrite it as you like).
Bonus points is that it will handle the recursion for you
Here is an example of using it:
https://github.com/apache/arrow-datafusion/blob/bfef10541f2a989f76f1b98532a8aa0bcde04617/datafusion/expr/src/logical_plan/plan.rs#L615-L648
You could perhaps destructure / rewrite the BinaryExpr then
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#discussion_r1069123054
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -128,7 +128,14 @@ impl ExprSchemable for Expr {
Expr::Like { .. } | Expr::ILike { .. } | Expr::SimilarTo { .. } => {
Ok(DataType::Boolean)
}
- Expr::Placeholder { data_type, .. } => Ok(data_type.clone()),
+ Expr::Placeholder { data_type, .. } => {
+ let data_type = data_type.clone().ok_or_else(|| {
+ DataFusionError::Internal(
+ "Placeholder type could not be resolved".to_owned(),
+ )
+ })?;
+ Ok(data_type)
Review Comment:
```suggestion
data_type.clone().ok_or_else(|| {
DataFusionError::Internal(
"Placeholder type could not be resolved".to_owned(),
)
})
```
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -603,6 +604,73 @@ impl LogicalPlan {
Ok(new_plan)
}
+ /// Walk the logical plan, find any `PlaceHolder` tokens, and return a map of their IDs and DataTypes
+ pub fn get_parameter_types(
+ &self,
+ ) -> Result<HashMap<String, Option<DataType>>, DataFusionError> {
+ struct ParamTypeVisitor {
+ param_types: HashMap<String, Option<DataType>>,
+ }
+
+ struct ExprParamTypeVisitor {
+ param_types: HashMap<String, Option<DataType>>,
+ }
+
+ impl ExpressionVisitor for ExprParamTypeVisitor {
+ fn pre_visit(
+ mut self,
+ expr: &Expr,
+ ) -> datafusion_common::Result<Recursion<Self>>
+ where
+ Self: ExpressionVisitor,
+ {
+ if let Expr::Placeholder { id, data_type } = expr {
+ let _ = self.param_types.insert(id.clone(), data_type.clone());
+ }
+ Ok(Recursion::Continue(self))
+ }
+ }
+
+ impl PlanVisitor for ParamTypeVisitor {
Review Comment:
I suspect you could `impl PlanVisitor` and `impl ExprVisitor` for `ParamTypeVisitor` rather than having multiple visitors and having to copy items from the hash maps
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -603,6 +604,73 @@ impl LogicalPlan {
Ok(new_plan)
}
+ /// Walk the logical plan, find any `PlaceHolder` tokens, and return a map of their IDs and DataTypes
+ pub fn get_parameter_types(
+ &self,
+ ) -> Result<HashMap<String, Option<DataType>>, DataFusionError> {
+ struct ParamTypeVisitor {
+ param_types: HashMap<String, Option<DataType>>,
+ }
+
+ struct ExprParamTypeVisitor {
+ param_types: HashMap<String, Option<DataType>>,
+ }
+
+ impl ExpressionVisitor for ExprParamTypeVisitor {
+ fn pre_visit(
+ mut self,
+ expr: &Expr,
+ ) -> datafusion_common::Result<Recursion<Self>>
+ where
+ Self: ExpressionVisitor,
+ {
+ if let Expr::Placeholder { id, data_type } = expr {
+ let _ = self.param_types.insert(id.clone(), data_type.clone());
+ }
+ Ok(Recursion::Continue(self))
+ }
+ }
+
+ impl PlanVisitor for ParamTypeVisitor {
+ type Error = DataFusionError;
+
+ fn pre_visit(&mut self, plan: &LogicalPlan) -> Result<bool, Self::Error> {
+ let mut visitor = ExprParamTypeVisitor {
+ param_types: Default::default(),
+ };
+ match plan {
Review Comment:
There are probably more plan types (like Aggregates for example) that could have references to parameter values
I poked around for a "visit all `Expr` in a plan" preexisting code and could not find one. So longer term I think it would be good to move this code somewhere more general (maybe near `ExpressionVisitor`):
```rust
/// visit all expressions in all nodes of the LogicalPlan
fn visit_plan_exprs(plan: &LogicalPlan, visitor: ExpressionVisitor) {
...
}
However think it is ok to start with this approach of something very specialized for parameter type inference and then refactor into something more general as a follow on PR. I also think we can track adding support for parameters in other plan types with a ticket as well if you don't want to support the entire thing in the first PR
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -603,6 +604,73 @@ impl LogicalPlan {
Ok(new_plan)
}
+ /// Walk the logical plan, find any `PlaceHolder` tokens, and return a map of their IDs and DataTypes
+ pub fn get_parameter_types(
+ &self,
+ ) -> Result<HashMap<String, Option<DataType>>, DataFusionError> {
+ struct ParamTypeVisitor {
+ param_types: HashMap<String, Option<DataType>>,
+ }
+
+ struct ExprParamTypeVisitor {
+ param_types: HashMap<String, Option<DataType>>,
+ }
+
+ impl ExpressionVisitor for ExprParamTypeVisitor {
+ fn pre_visit(
+ mut self,
+ expr: &Expr,
+ ) -> datafusion_common::Result<Recursion<Self>>
+ where
+ Self: ExpressionVisitor,
+ {
+ if let Expr::Placeholder { id, data_type } = expr {
+ let _ = self.param_types.insert(id.clone(), data_type.clone());
Review Comment:
Should we error here if the parameter was previously referenced with a different datatype 🤔 (aka don't ignore `_`?)
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -128,7 +128,14 @@ impl ExprSchemable for Expr {
Expr::Like { .. } | Expr::ILike { .. } | Expr::SimilarTo { .. } => {
Ok(DataType::Boolean)
}
- Expr::Placeholder { data_type, .. } => Ok(data_type.clone()),
+ Expr::Placeholder { data_type, .. } => {
+ let data_type = data_type.clone().ok_or_else(|| {
+ DataFusionError::Internal(
+ "Placeholder type could not be resolved".to_owned(),
+ )
+ })?;
+ Ok(data_type)
Review Comment:
very minor style suggestion
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -128,7 +128,14 @@ impl ExprSchemable for Expr {
Expr::Like { .. } | Expr::ILike { .. } | Expr::SimilarTo { .. } => {
Ok(DataType::Boolean)
}
- Expr::Placeholder { data_type, .. } => Ok(data_type.clone()),
+ Expr::Placeholder { data_type, .. } => {
+ let data_type = data_type.clone().ok_or_else(|| {
+ DataFusionError::Internal(
+ "Placeholder type could not be resolved".to_owned(),
+ )
+ })?;
+ Ok(data_type)
Review Comment:
I also think this error might be better "Plan" or "Unsupported" -- it isn't really an internal (unexpected) error type
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] avantgardnerio commented on a diff in pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on code in PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#discussion_r1069735231
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -603,6 +604,73 @@ impl LogicalPlan {
Ok(new_plan)
}
+ /// Walk the logical plan, find any `PlaceHolder` tokens, and return a map of their IDs and DataTypes
+ pub fn get_parameter_types(
+ &self,
+ ) -> Result<HashMap<String, Option<DataType>>, DataFusionError> {
+ struct ParamTypeVisitor {
+ param_types: HashMap<String, Option<DataType>>,
+ }
+
+ struct ExprParamTypeVisitor {
+ param_types: HashMap<String, Option<DataType>>,
+ }
+
+ impl ExpressionVisitor for ExprParamTypeVisitor {
+ fn pre_visit(
+ mut self,
+ expr: &Expr,
+ ) -> datafusion_common::Result<Recursion<Self>>
+ where
+ Self: ExpressionVisitor,
+ {
+ if let Expr::Placeholder { id, data_type } = expr {
+ let _ = self.param_types.insert(id.clone(), data_type.clone());
+ }
+ Ok(Recursion::Continue(self))
+ }
+ }
+
+ impl PlanVisitor for ParamTypeVisitor {
Review Comment:
I can, but it doesn't seem to simplify things very much, since the signature of `ExprVisitable` is awkward requiring ownership to be transferred around vs mutation:
```
fn accept<V: ExpressionVisitor>(&self, visitor: V) -> Result<V> {
```
Since I only have a mutable reference to `self` in `pre_visit()` I can't re-use a single instance I don't think:
```
fn pre_visit(&mut self, plan: &LogicalPlan) -> Result<bool, Self::Error> {
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
alamb commented on PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#issuecomment-1382719331
> What could it infer from except those?
I was thinking stuff like `$1 LIKE 'foo'` for example
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] avantgardnerio commented on a diff in pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on code in PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#discussion_r1054822250
##########
datafusion/sql/src/lib.rs:
##########
@@ -1,3 +1,4 @@
+#![feature(box_patterns)]
Review Comment:
oh, these made it into stable, awesome!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] avantgardnerio commented on pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#issuecomment-1379591769
Filter & join were all I could find for our test dataset of SQL queries, and those both work now. I'm undrafting this and marking it ready for review.
I'll do a follow-on one for insert, update, and delete :fearful:
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] avantgardnerio merged pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
avantgardnerio merged PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] avantgardnerio commented on pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#issuecomment-1383210408
> a) Extract the "walk all exprs in a plan" code to its own function
> b) Cover the remaining LogicalPlan types in "walk all exprs in a plan"
I think these are done now.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] ursabot commented on pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#issuecomment-1396123254
Benchmark runs are scheduled for baseline = 64fa312ecc5f32294e70fd7389e18cb41f25e732 and contender = e6a050058bd704f73b38106b7abf21dc4539eebc. e6a050058bd704f73b38106b7abf21dc4539eebc is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/79324ef27c714db0914ffd1a30cf2fb2...3ff11d0e49df4d6fb45ef0ae53c2ead6/)
[Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/7d60d93ca5c44d5081c63d01eb2caac0...ed7c429e3b0c4cba9f89dbfdcfccd277/)
[Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/f6e6e9afae9245eba9463d06b5fbe42b...daf0263f054a46b08aa886890e6e7d74/)
[Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/c79c8d2cb9a949238df2708b87ff411f...ff2b3d7dd8eb4bdc85619121333c3ef3/)
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] avantgardnerio commented on pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#issuecomment-1361859327
@alamb @andygrove @NGA-TRAN Please review!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] avantgardnerio commented on a diff in pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on code in PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#discussion_r1054712829
##########
datafusion/sql/src/lib.rs:
##########
@@ -1,3 +1,4 @@
+#![feature(box_patterns)]
Review Comment:
I wasn't sure how to do it without this... I'm open to advice.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] avantgardnerio commented on a diff in pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on code in PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#discussion_r1054822250
##########
datafusion/sql/src/lib.rs:
##########
@@ -1,3 +1,4 @@
+#![feature(box_patterns)]
Review Comment:
oh, these made it into stable, awesome!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] avantgardnerio commented on pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#issuecomment-1362091709
> this is looking very nice
Sounds like you're okay with the general approach then. I'll implement a bunch of other inference types and undraft the PR :+1:
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] avantgardnerio commented on a diff in pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on code in PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#discussion_r1054714984
##########
datafusion/expr/src/expr.rs:
##########
@@ -218,7 +218,7 @@ pub enum Expr {
/// The identifier of the parameter (e.g, $1 or $foo)
id: String,
/// The type the parameter will be filled in with
- data_type: DataType,
+ data_type: Option<DataType>,
Review Comment:
Make the data type optional, since we might not know it during initial conversion from SQL ast.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] avantgardnerio commented on a diff in pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on code in PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#discussion_r1069736791
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -603,6 +604,73 @@ impl LogicalPlan {
Ok(new_plan)
}
+ /// Walk the logical plan, find any `PlaceHolder` tokens, and return a map of their IDs and DataTypes
+ pub fn get_parameter_types(
+ &self,
+ ) -> Result<HashMap<String, Option<DataType>>, DataFusionError> {
+ struct ParamTypeVisitor {
+ param_types: HashMap<String, Option<DataType>>,
+ }
+
+ struct ExprParamTypeVisitor {
+ param_types: HashMap<String, Option<DataType>>,
+ }
+
+ impl ExpressionVisitor for ExprParamTypeVisitor {
+ fn pre_visit(
+ mut self,
+ expr: &Expr,
+ ) -> datafusion_common::Result<Recursion<Self>>
+ where
+ Self: ExpressionVisitor,
+ {
+ if let Expr::Placeholder { id, data_type } = expr {
+ let _ = self.param_types.insert(id.clone(), data_type.clone());
Review Comment:
I think it's valid to have multiple `$1`s in a single statement. Maybe we should check the type and error if it differs?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] avantgardnerio commented on a diff in pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on code in PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#discussion_r1069727176
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -603,6 +604,73 @@ impl LogicalPlan {
Ok(new_plan)
}
+ /// Walk the logical plan, find any `PlaceHolder` tokens, and return a map of their IDs and DataTypes
+ pub fn get_parameter_types(
+ &self,
+ ) -> Result<HashMap<String, Option<DataType>>, DataFusionError> {
+ struct ParamTypeVisitor {
+ param_types: HashMap<String, Option<DataType>>,
+ }
+
+ struct ExprParamTypeVisitor {
+ param_types: HashMap<String, Option<DataType>>,
+ }
+
+ impl ExpressionVisitor for ExprParamTypeVisitor {
+ fn pre_visit(
+ mut self,
+ expr: &Expr,
+ ) -> datafusion_common::Result<Recursion<Self>>
+ where
+ Self: ExpressionVisitor,
+ {
+ if let Expr::Placeholder { id, data_type } = expr {
+ let _ = self.param_types.insert(id.clone(), data_type.clone());
+ }
+ Ok(Recursion::Continue(self))
+ }
+ }
+
+ impl PlanVisitor for ParamTypeVisitor {
+ type Error = DataFusionError;
+
+ fn pre_visit(&mut self, plan: &LogicalPlan) -> Result<bool, Self::Error> {
+ let mut visitor = ExprParamTypeVisitor {
+ param_types: Default::default(),
+ };
+ match plan {
Review Comment:
> if you don't want to support the entire thing in the first PR
I think that would be best.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] avantgardnerio commented on a diff in pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on code in PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#discussion_r1054714428
##########
datafusion/sql/src/planner.rs:
##########
@@ -970,6 +970,45 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
}
}
+ fn infer_placeholder_types(&self, expr: &mut Expr, schema: &DFSchema) -> Result<()> {
+ match expr {
+ Expr::BinaryExpr(BinaryExpr {
+ ref mut left,
+ ref mut right,
+ ..
+ }) => {
+ self.infer_placeholder_types(left, schema)?;
+ self.infer_placeholder_types(right, schema)?;
+ let lt = left.get_type(schema);
+ let rt = right.get_type(schema);
+ match (left, rt) {
+ (
+ box Expr::Placeholder {
Review Comment:
The `box` keyword let me destructure the box in a mutable way, which let me avoid having to implement walking every type of possible expression and regenerating the whole tree. I'm open to advice on how to do this without using an experimental feature.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#discussion_r1054832858
##########
datafusion/sql/src/planner.rs:
##########
@@ -970,6 +970,46 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
}
}
+ /// Find all `PlaceHolder` tokens in a logical plan, and try to infer their type from context
+ fn infer_placeholder_types(&self, expr: &mut Expr, schema: &DFSchema) -> Result<()> {
+ match expr {
+ Expr::BinaryExpr(BinaryExpr {
+ ref mut left,
+ ref mut right,
+ ..
+ }) => {
+ self.infer_placeholder_types(left, schema)?;
+ self.infer_placeholder_types(right, schema)?;
+ let lt = left.get_type(schema);
+ let rt = right.get_type(schema);
+ match (left, rt) {
Review Comment:
I was thinking in general this logic is only correct for "comparison" binary operators. It works well for
```
a > $1
```
However, I couldn't come up with an example where it wouldn't work (even `Operator::Like` would result in the other argument being a string.
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -128,7 +128,12 @@ impl ExprSchemable for Expr {
Expr::Like { .. } | Expr::ILike { .. } | Expr::SimilarTo { .. } => {
Ok(DataType::Boolean)
}
- Expr::Placeholder { data_type, .. } => Ok(data_type.clone()),
+ Expr::Placeholder { data_type, .. } => {
+ let data_type = data_type.clone().ok_or_else(|| {
+ DataFusionError::Internal("Placeholder type not resolved".to_owned())
Review Comment:
```suggestion
DataFusionError::Internal("Placeholder type could not be resolved".to_owned())
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] avantgardnerio commented on a diff in pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on code in PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#discussion_r1070653525
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -603,6 +604,73 @@ impl LogicalPlan {
Ok(new_plan)
}
+ /// Walk the logical plan, find any `PlaceHolder` tokens, and return a map of their IDs and DataTypes
+ pub fn get_parameter_types(
+ &self,
+ ) -> Result<HashMap<String, Option<DataType>>, DataFusionError> {
+ struct ParamTypeVisitor {
+ param_types: HashMap<String, Option<DataType>>,
+ }
+
+ struct ExprParamTypeVisitor {
+ param_types: HashMap<String, Option<DataType>>,
+ }
+
+ impl ExpressionVisitor for ExprParamTypeVisitor {
+ fn pre_visit(
+ mut self,
+ expr: &Expr,
+ ) -> datafusion_common::Result<Recursion<Self>>
+ where
+ Self: ExpressionVisitor,
+ {
+ if let Expr::Placeholder { id, data_type } = expr {
+ let _ = self.param_types.insert(id.clone(), data_type.clone());
+ }
+ Ok(Recursion::Continue(self))
+ }
+ }
+
+ impl PlanVisitor for ParamTypeVisitor {
+ type Error = DataFusionError;
+
+ fn pre_visit(&mut self, plan: &LogicalPlan) -> Result<bool, Self::Error> {
+ let mut visitor = ExprParamTypeVisitor {
+ param_types: Default::default(),
+ };
+ match plan {
Review Comment:
> a) Extract the "walk all exprs in a plan" code to its own function
> b) Cover the remaining LogicalPlan types in "walk all exprs in a plan"
I think these are done now.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
alamb commented on PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#issuecomment-1381051431
I plan to review this tomorrow.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] avantgardnerio commented on pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#issuecomment-1382451542
> walk all logical plan nodes yet
I'll file a ticket for that.
> other than binary expressions
What could it infer from except those?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on pull request #4701: Infer prepared statement parameter types
Posted by GitBox <gi...@apache.org>.
alamb commented on PR #4701:
URL: https://github.com/apache/arrow-datafusion/pull/4701#issuecomment-1386015088
FWIW I plan to go over this once more hopefully later today or tomorrow and if it hasn't been merged yet do so. I think it is good enough to go in as is and we can iterate on it in subsequent PRs
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org