Skip to content

Commit 04747f4

Browse files
committed
chore: try_eliminate_in_subquery skip if left has join or child expr is nullable
Signed-off-by: Kould <[email protected]>
1 parent f4181f4 commit 04747f4

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

โ€Žsrc/query/sql/src/planner/optimizer/decorrelate/subquery_rewriter.rsโ€Ž

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,10 @@ impl SubqueryRewriter {
570570
else {
571571
return Ok(None);
572572
};
573+
// `xx in null` != `where xx = null`
574+
if left_column.column.data_type.is_nullable() {
575+
return Ok(None);
576+
}
573577
let (Some(left_table_index), Some(right_table_index)) = (
574578
left_column.column.table_index,
575579
right_expr_binding.table_index,
@@ -583,6 +587,11 @@ impl SubqueryRewriter {
583587
) else {
584588
return Ok(None);
585589
};
590+
// when left is a join, filtering becomes complicated. If in subquery is eliminated,
591+
// the filter will be pushed down to Scan before join, resulting in inaccurate data results.
592+
if left.has_join() {
593+
return Ok(None);
594+
}
586595
if left_source_binding != right_source_binding {
587596
return Ok(None);
588597
}
@@ -592,10 +601,9 @@ impl SubqueryRewriter {
592601
let left_columns = guard.columns_by_table_index(left_table_index);
593602
let right_columns = guard.columns_by_table_index(right_table_index);
594603
let left_table = guard.table(left_table_index);
604+
let right_table = guard.table(right_table_index);
595605
// filter table function
596-
if left_table.database() == "system"
597-
|| guard.table(right_table_index).database() == "system"
598-
{
606+
if left_table.database() == "system" || right_table.database() == "system" {
599607
return Ok(None);
600608
}
601609
let left_source_table_index = guard

โ€Žsrc/query/sql/src/planner/optimizer/s_expr.rsโ€Ž

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,13 @@ impl SExpr {
426426
}
427427
self.children.iter().any(|child| child.has_merge_exchange())
428428
}
429+
#[recursive::recursive]
430+
pub fn has_join(&self) -> bool {
431+
if let RelOperator::Join(_) = self.plan.as_ref() {
432+
return true;
433+
}
434+
self.children.iter().any(|child| child.has_join())
435+
}
429436
}
430437

431438
fn find_subquery(rel_op: &RelOperator) -> bool {

โ€Žtests/sqllogictests/suites/mode/standalone/explain/explain.testโ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,10 +1330,10 @@ statement ok
13301330
drop table t2;
13311331

13321332
statement ok
1333-
create OR REPLACE table t1(a int, b int, c varchar(20));
1333+
create OR REPLACE table t1(a int not null, b int, c varchar(20));
13341334

13351335
statement ok
1336-
create OR REPLACE table t2(a int, b int, c varchar(20));
1336+
create OR REPLACE table t2(a int not null, b int, c varchar(20));
13371337

13381338
# eliminate subquery
13391339
query T
@@ -1350,7 +1350,7 @@ Filter
13501350
โ”œโ”€โ”€ read size: 0
13511351
โ”œโ”€โ”€ partitions total: 0
13521352
โ”œโ”€โ”€ partitions scanned: 0
1353-
โ”œโ”€โ”€ push downs: [filters: [and_filters(t1.c (#2) = 'D', t1.b (#1) = 7)], limit: NONE]
1353+
โ”œโ”€โ”€ push downs: [filters: [and_filters(t1.b (#1) = 7, t1.c (#2) = 'D')], limit: NONE]
13541354
โ””โ”€โ”€ estimated rows: 0.00
13551355

13561356

โ€Žtests/sqllogictests/suites/query/subquery.testโ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -944,10 +944,10 @@ WHERE t1.i1 = (
944944
1
945945

946946
statement ok
947-
create or replace table t1(a int, b int, c varchar(20));
947+
create or replace table t1(a int not null, b int, c varchar(20));
948948

949949
statement ok
950-
create or replace table t2(a int, b int, c varchar(20));
950+
create or replace table t2(a int not null, b int, c varchar(20));
951951

952952
statement ok
953953
insert into t1 (a, b, c) values (1, 7, 'D'), (2, 7, 'D'), (3, 7, 'D'), (4, 8, 'D'), (5, 7, 'E'), (6, 8, 'E');

0 commit comments

Comments
ย (0)