jayzhan211 commented on code in PR #27783:
URL: https://github.com/apache/datafusion/pull/27783#discussion_r4719954759
##########
datafusion/optimizer/src/decorrelate.rs:
##########
@@ -837,6 +837,9 @@ impl PullUpCorrelatedExpr {
// `f_down` checked that the pulled up filters do not read an
// unnested column.
LogicalPlan::Unnest(_) => self.pass_through_pulled_up_cols(plan),
+ LogicalPlan::Join(_) | LogicalPlan::AsOfJoin(_) => {
Review Comment:
Nothing tests the `AsOfJoin` part of this arm: removing it keeps every test
passing, and its schema is rebuilt through `AsOfJoin::try_new` rather than the
`Join` path. This query fails with the schema error on main and returns the
rows below with this PR. Could you add it next to the other cases?
```
query II
SELECT o.a, (
SELECT count(*) FROM (SELECT a AS x FROM t27691_i WHERE a = o.a) e
ASOF JOIN t27691_u u MATCH_CONDITION (e.x * 10 >= u.b)
) FROM t27691 o ORDER BY o.a;
----
1 1
2 2
4 0
```
##########
datafusion/optimizer/src/decorrelate.rs:
##########
@@ -837,6 +837,9 @@ impl PullUpCorrelatedExpr {
// `f_down` checked that the pulled up filters do not read an
// unnested column.
LogicalPlan::Unnest(_) => self.pass_through_pulled_up_cols(plan),
+ LogicalPlan::Join(_) | LogicalPlan::AsOfJoin(_) => {
Review Comment:
@tlfua thanks for the fix! With this arm, an `EXISTS`, `NOT EXISTS` or `IN`
subquery whose derived table is an aggregate without `GROUP BY` now returns
wrong rows where main returned the schema error. For `EXISTS`, `build_join`
retries the pull up with count bug handling off, so the
`collected_count_expr_map` check in `pass_through_pulled_up_cols` never fires,
and the regrouped aggregate gives no row for an outer row without a match:
```sql
SELECT o.a FROM t o WHERE EXISTS (
SELECT 1 FROM (SELECT count(*) c FROM i WHERE a = o.a) e, u
) ORDER BY o.a;
-- t = 1, 2, 4; i = 1, 2, 2, 3; u = 10, 20
-- expected 1, 2, 4; this PR: 1, 2
```
`NOT EXISTS` returns `4` (expected none), and `o.a - 4 IN (SELECT e.c ...)`
returns nothing (expected `4`). Checking `pulled_up_scalar_agg` instead is too
broad: an uncorrelated `(SELECT count(*) c FROM u) z` in the other join input
sets it too, and that would stop a query that works today. A flag that is set
only when the pull up adds a correlated column to an empty `GROUP BY`, checked
in the shared helper, keeps these correlated (it also fixes the same bug for an
`Unnest`, which main already has):
```diff
@@ pub struct PullUpCorrelatedExpr {
pub pulled_up_scalar_agg: bool,
+ /// Whether the pull up added a correlated column to the `GROUP BY` of
an
+ /// aggregate that had none. Unlike [`Self::pulled_up_scalar_agg`], an
+ /// uncorrelated aggregate visited after a correlated filter leaves it
unset.
+ regrouped_scalar_agg: bool,
@@ pub fn new() -> Self {
pulled_up_scalar_agg: false,
+ regrouped_scalar_agg: false,
@@ LogicalPlan::Aggregate(aggregate)
if aggregate.group_expr.is_empty() {
self.pulled_up_scalar_agg = true;
+ self.regrouped_scalar_agg |=
!local_correlated_cols.is_empty();
}
@@ fn pass_through_pulled_up_cols(
- if plan
- .inputs()
- .iter()
- .any(|input| self.collected_count_expr_map.contains_key(*input))
+ // `EXISTS` retries the pull up without count bug handling, so
+ // `collected_count_expr_map` can be empty while such an aggregate
is
+ // below `plan`.
+ if self.regrouped_scalar_agg
+ || plan
+ .inputs()
+ .iter()
+ .any(|input|
self.collected_count_expr_map.contains_key(*input))
{
```
With this, `cargo test -p datafusion-optimizer` and the full sqllogictest
suite pass. Removing either new condition fails one of the tests below.
<details>
<summary>Tests for subquery.slt (after the IN case), with the expected
results in comments</summary>
```
# An aggregate without GROUP BY below the join returns a row for every outer
# row, so the subquery stays correlated. Expected: 1, 2, 4.
query error
SELECT o.a FROM t27691 o WHERE EXISTS (
SELECT 1 FROM (SELECT count(*) c FROM t27691_i WHERE a = o.a) e, t27691_u u
) ORDER BY o.a;
----
DataFusion error: This feature is not implemented: Physical plan does not
support logical expression Exists(Exists { subquery: <subquery>, negated: false
})
# Expected: 4 (its count is 0).
query error
SELECT o.a FROM t27691 o WHERE o.a - 4 IN (
SELECT e.c FROM (SELECT count(*) c FROM t27691_i WHERE a = o.a) e,
t27691_u u
) ORDER BY o.a;
----
DataFusion error: This feature is not implemented: Physical plan does not
support logical expression InSubquery(InSubquery { expr: BinaryExpr(BinaryExpr
{ left: Cast(Cast { expr: Column(Column { relation: Some(Bare { table: "t27691"
}), name: "a" }), field: Field { name: "", data_type: Int64, nullable: true }
}), op: Minus, right: Literal(Int64(4), None) }), subquery: <subquery>,
negated: false })
# The same holds for an Unnest. Expected: 1, 2, 4.
query error
SELECT o.a FROM t27691 o WHERE EXISTS (
SELECT unnest(make_array(c, c))
FROM (SELECT count(*) c FROM t27691_i WHERE a = o.a) e
) ORDER BY o.a;
----
DataFusion error: This feature is not implemented: Physical plan does not
support logical expression Exists(Exists { subquery: <subquery>, negated: false
})
# An aggregate without GROUP BY that reads no outer column does not.
query I
SELECT o.a FROM t27691 o WHERE EXISTS (
SELECT 1 FROM (SELECT a AS x FROM t27691_i WHERE a = o.a) e,
(SELECT count(*) c FROM t27691_u) z
) ORDER BY o.a;
----
1
2
```
</details>
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]