move the type coercion to the beginning of the optimizer rule and support type coercion for subquery#3636
Conversation
datafusion/core/tests/sql/joins.rs
Outdated
|
|
||
| #[tokio::test] | ||
| #[ignore] | ||
| // https://github.com/apache/arrow-datafusion/issues/3565 |
There was a problem hiding this comment.
skip the test, #3565
@AssHero want to take it
There was a problem hiding this comment.
the issue has been fixed, so I remove the comment and #[ignore]
type coercion to the beginning of the optimizer ruletype coercion to the beginning of the optimizer rule and support type coercion for subquery
|
@andygrove @alamb @Dandandan PTAL |
be39550 to
7bd00d7
Compare
Codecov Report
@@ Coverage Diff @@
## master #3636 +/- ##
==========================================
- Coverage 86.07% 85.98% -0.10%
==========================================
Files 300 300
Lines 56337 56478 +141
==========================================
+ Hits 48494 48563 +69
- Misses 7843 7915 +72
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
alamb
left a comment
There was a problem hiding this comment.
Thanks @liukun4515 -- I left some comments about the subquery changes. Overall I love where this is heading
| // Simplify expressions first to maximize the chance | ||
| // of applying other optimizations | ||
| Arc::new(SimplifyExpressions::new()), | ||
| Arc::new(PreCastLitInComparisonExpressions::new()), |
There was a problem hiding this comment.
Now that we have TypeCoercion I wonder if we still need PreCastLitInComparisonExpressions
Appears to be a subset of what TypeCoercion is doing
There was a problem hiding this comment.
Though perhaps this is something similar to what you have described in #3622
There was a problem hiding this comment.
The PreCastLitInComparisonExpressions will be invalid when we move the type coercion to the front of PreCastLitInComparisonExpressions.
In the next pr, I will do #3622 and move the type coercion to the front of the PreCastLitInComparisonExpressions
I think type coercion just do one thing which is make type compatible in all operation, but the PreCastLitInComparisonExpressions or #3622 is to reduce cast for column expr or other expr instead of adding the cast to the literal. This will reduce the cast effort of the runtime.
| ]]; | ||
| assert_eq!(expected, actual); | ||
|
|
||
| // Also, expect same result with lowercase explain |
There was a problem hiding this comment.
This comment seems to be out of date -- the different is not just just explain vs EXPLAIN it is also the explicit cast that was added
| Filter: #part.p_name LIKE Utf8("forest%") | ||
| TableScan: part projection=[p_partkey, p_name], partial_filters=[#part.p_name LIKE Utf8("forest%")] | ||
| Projection: #lineitem.l_partkey, #lineitem.l_suppkey, Decimal128(Some(50000000000000000),38,17) * CAST(#SUM(lineitem.l_quantity) AS Decimal128(38, 17)) AS __value, alias=__sq_3 | ||
| Projection: #lineitem.l_partkey, #lineitem.l_suppkey, CAST(Float64(0.5) AS Decimal128(38, 17)) * CAST(#SUM(lineitem.l_quantity) AS Decimal128(38, 17)) AS __value, alias=__sq_3 |
There was a problem hiding this comment.
This seems like a regression somehow -- the CAST hasn't been evaluated into a constant
There was a problem hiding this comment.
It's the desired result.
I think the evaluating of cast is done in the SimplifyExpressions rule, but the rule don't support the subquery now.
In this pr, we move the SimplifyExpressions to the front of the some rule about subquery, so the optimization of SimplifyExpressions can't apply some expr which is in the subquery.
If we want to do the evaluation cast for the expr in the subquery, there are two way:
- add additional
SimplifyExpressionsin the tail of the optimizer rules - support subquery case for
SimplifyExpressionsrule.
If we don't want the regression in this pr, I can use the method of 1 to fix it temporarily.
@alamb
There was a problem hiding this comment.
I think we should avoid regressions -- so perhaps we can do 1 as a temporary workaround and then support subquery in SimplifyExpressons as a follow on PR (I am trying to get some time next week to help out in some of these issues)
There was a problem hiding this comment.
Ok, I will recovery them in the follow up pr
| Aggregate: groupBy=[[#lineitem.l_partkey, #lineitem.l_suppkey]], aggr=[[SUM(#lineitem.l_quantity)]] | ||
| Filter: #lineitem.l_shipdate >= Date32("8766") | ||
| TableScan: lineitem projection=[l_partkey, l_suppkey, l_quantity, l_shipdate], partial_filters=[#lineitem.l_shipdate >= Date32("8766")]"# | ||
| Filter: #lineitem.l_shipdate >= CAST(Utf8("1994-01-01") AS Date32) |
There was a problem hiding this comment.
likewise, this isn't right because it should have been evaluated to a constant
| }; | ||
| fn optimize_internal( | ||
| // use the external schema to handle the correlated subqueries case | ||
| externel_schema: &DFSchema, |
There was a problem hiding this comment.
| externel_schema: &DFSchema, | |
| external_schema: &DFSchema, |
| fn mutate(&mut self, expr: Expr) -> Result<Expr> { | ||
| match expr { | ||
| Expr::ScalarSubquery(Subquery { subquery }) => { | ||
| let mut optimizer_config = OptimizerConfig::new(); |
There was a problem hiding this comment.
I think we should pass through the same OptimizerConfig (so it has the same context needed to evaluate now() rather than creating new ones
There was a problem hiding this comment.
After we move the type coercion out of the optimizer framework, we don't need the parameter of OptimizerConfig to iterate the plan when doing the type coercion.
I think the new of OptimizerConfig will not affect the rule of type coercion, because the rule of type coercion don't use the OptimizerConfig to generate some values.
There was a problem hiding this comment.
I think when we complete this issue #3582, this parameter will be dropped.
We don't need to concern this.
| } | ||
| Expr::Exists { subquery, negated } => { | ||
| let mut optimizer_config = OptimizerConfig::new(); | ||
| let new_plan = optimize_internal( |
There was a problem hiding this comment.
I don't understand why the outer query's schema needs to be passed while optimizing the subquery. I would expect that we would recursively optimize the subquery. The subquery doesn't have the same schema as its containing query 🤔
There was a problem hiding this comment.
As in I think this should be more like:
let optimizer = TypeCoercion::new();
let new_plan = optimizer.optimize(&subquery.subquery, &mut optimizer_config)There was a problem hiding this comment.
I also want this, but there are some correlated subquery, like the comments.
I have added the comments about the usage of external_schema schema
// merge the outer schema for correlated subqueries
// like case:
// select t2.c2 from t1 where t1.c1 in (select t2.c1 from t2 where t2.c2=t1.c3)
For the subquery select t2.c1 from t2 where t2.c2= t1.c3, if we don't know the schema about the t1, we can't get the data type for t1.c3 and can't do the type coercion for t2.c2=t1.c3.
This is way that I find, do you @andygrove @alamb have any thoughts about this?
There was a problem hiding this comment.
I think I would need to try it myself
| let result = expr.rewrite(&mut rewriter)?; | ||
| assert_eq!(expected, result); | ||
| Ok(()) | ||
| // TODO add more test for this |
There was a problem hiding this comment.
I agree some more tests would be good
| let expr = is_true( | ||
| lit(ScalarValue::Int32(Some(12))).eq(lit(ScalarValue::Int64(Some(13)))), | ||
| ); |
There was a problem hiding this comment.
I wonder if you can write these tests like this to make them easier to read?
| let expr = is_true( | |
| lit(ScalarValue::Int32(Some(12))).eq(lit(ScalarValue::Int64(Some(13)))), | |
| ); | |
| let expr = is_true( | |
| lit(12i32).eq(lit(12i64), | |
| ); |
There was a problem hiding this comment.
lit(12i32).eq(lit(13i64)),
| // of applying other optimizations | ||
| Arc::new(SimplifyExpressions::new()), | ||
| Arc::new(PreCastLitInComparisonExpressions::new()), | ||
| Arc::new(TypeCoercion::new()), |
alamb
left a comment
There was a problem hiding this comment.
I think this PR is good enough to merge and we should keep hacking on it in a follow on PR
|
Benchmark runs are scheduled for baseline = 9af2337 and contender = 29b8bbd. 29b8bbd is a master commit associated with this PR. Results will be available as each benchmark for each run completes. |
Which issue does this PR close?
part of #3582
Closed #3557
Rationale for this change
What changes are included in this PR?
Are there any user-facing changes?