Skip to content

Commit 3079441

Browse files
authored
fix(optimizer): PlanCorrelatedIdFinder should be aware of agg filter (risingwavelabs#8667)
1 parent d557a6c commit 3079441

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/frontend/planner_test/tests/testdata/subquery_expr_correlated.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,23 @@
755755
├─LogicalAgg { group_key: [strings.v1], aggs: [] }
756756
| └─LogicalScan { table: strings, columns: [strings.v1] }
757757
└─LogicalScan { table: strings, columns: [strings.v1] }
758+
- name: issue 7574 correlated input in agg filter in having
759+
sql: |
760+
CREATE TABLE strings(v1 VARCHAR);
761+
SELECT (SELECT 1 FROM strings HAVING COUNT(v1) FILTER (WHERE t.v1 < 'b') > 2) FROM strings AS t;
762+
optimized_logical_plan_for_batch: |
763+
LogicalJoin { type: LeftOuter, on: IsNotDistinctFrom(strings.v1, strings.v1), output: [1:Int32] }
764+
├─LogicalScan { table: strings, columns: [strings.v1] }
765+
└─LogicalProject { exprs: [strings.v1, 1:Int32] }
766+
└─LogicalFilter { predicate: (count(strings.v1) filter((strings.v1 < 'b':Varchar)) > 2:Int32) }
767+
└─LogicalAgg { group_key: [strings.v1], aggs: [count(strings.v1) filter((strings.v1 < 'b':Varchar))] }
768+
└─LogicalJoin { type: LeftOuter, on: IsNotDistinctFrom(strings.v1, strings.v1), output: [strings.v1, strings.v1] }
769+
├─LogicalAgg { group_key: [strings.v1], aggs: [] }
770+
| └─LogicalScan { table: strings, columns: [strings.v1] }
771+
└─LogicalJoin { type: Inner, on: true, output: all }
772+
├─LogicalAgg { group_key: [strings.v1], aggs: [] }
773+
| └─LogicalScan { table: strings, columns: [strings.v1] }
774+
└─LogicalScan { table: strings, columns: [strings.v1] }
758775
- name: Existential join on outer join with correlated condition
759776
sql: |
760777
create table t1(x int, y int);

src/frontend/src/optimizer/plan_visitor/plan_correlated_id_finder.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use std::collections::HashSet;
1616

1717
use crate::expr::{CorrelatedId, CorrelatedInputRef, ExprVisitor};
18-
use crate::optimizer::plan_node::{LogicalFilter, LogicalJoin, LogicalProject, PlanTreeNode};
18+
use crate::optimizer::plan_node::{
19+
LogicalAgg, LogicalFilter, LogicalJoin, LogicalProject, PlanTreeNode,
20+
};
1921
use crate::optimizer::plan_visitor::PlanVisitor;
2022
use crate::PlanRef;
2123

@@ -37,8 +39,8 @@ impl PlanCorrelatedIdFinder {
3739
}
3840

3941
impl PlanVisitor<()> for PlanCorrelatedIdFinder {
40-
/// `correlated_input_ref` can only appear in `LogicalProject`, `LogicalFilter` and
41-
/// `LogicalJoin` now.
42+
/// `correlated_input_ref` can only appear in `LogicalProject`, `LogicalFilter`,
43+
/// `LogicalJoin` or the `filter` clause of `PlanAggCall` of `LogicalAgg` now.
4244
4345
fn merge(_: (), _: ()) {}
4446

@@ -71,6 +73,18 @@ impl PlanVisitor<()> for PlanCorrelatedIdFinder {
7173
.into_iter()
7274
.for_each(|input| self.visit(input));
7375
}
76+
77+
fn visit_logical_agg(&mut self, plan: &LogicalAgg) {
78+
let mut finder = ExprCorrelatedIdFinder::default();
79+
plan.agg_calls()
80+
.iter()
81+
.for_each(|agg_call| agg_call.filter.visit_expr(&mut finder));
82+
self.correlated_id_set.extend(finder.correlated_id_set);
83+
84+
plan.inputs()
85+
.into_iter()
86+
.for_each(|input| self.visit(input));
87+
}
7488
}
7589

7690
#[derive(Default)]

0 commit comments

Comments
 (0)