Skip to content

Commit 9bcc2ee

Browse files
authored
Merge pull request #9116 from TCeason/ISSUE-9083/where_logic_fix
fix: prewhere_column empty and predicate is not const will return empty
2 parents 3856c54 + 8d87239 commit 9bcc2ee

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/query/sql/src/planner/optimizer/heuristic/prewhere_optimization.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ impl PrewhereOptimizer {
115115
get.prewhere = if prewhere_pred.is_empty() {
116116
None
117117
} else {
118+
if prewhere_columns.is_empty() {
119+
// select a, b from t1 where 's' not in ('x', 'b', 'c', 'd') or to_nullable(null);
120+
// get the smallest column in (a,b). (maybe can get the smallest column in t1)
121+
let used = self.metadata.read().find_smallest_column(
122+
get.columns.iter().copied().collect::<Vec<_>>().as_slice(),
123+
);
124+
prewhere_columns.insert(used);
125+
}
118126
Some(Prewhere {
119127
output_columns: get.columns.clone(),
120128
prewhere_columns,

tests/logictest/suites/base/03_dml/03_0005_select_filter

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,31 @@ select * from t1 where id in (1,3) or null order by id;
5858
statement ok
5959
DROP TABLE t1;
6060

61+
statement ok
62+
DROP DATABASE IF EXISTS databend6;
63+
64+
statement ok
65+
CREATE DATABASE databend6;
66+
67+
statement ok
68+
CREATE TABLE databend6.t0(c0INT INT32 NOT NULL DEFAULT(1456832334));
69+
70+
statement ok
71+
INSERT INTO databend6.t0(c0int) VALUES (1388388634), (-1943680716);
72+
73+
statement query I
74+
SELECT t0.c0int FROM databend6.t0 WHERE ((((((true)or(('s' NOT IN ('oE', '70', '3r', 'k', '9aRgze')))))AND(true)))or(((NULL)<=(NULL))));
75+
76+
----
77+
1388388634
78+
-1943680716
79+
80+
statement query I
81+
SELECT t0.c0int FROM databend6.t0 WHERE 's' NOT IN ('oE', '70', '3r', 'k', '9aRgze') or to_nullable(null) and to_nullable(true) or to_nullable(false);
82+
83+
----
84+
1388388634
85+
-1943680716
86+
87+
statement ok
88+
DROP DATABASE IF EXISTS databend6;

0 commit comments

Comments
 (0)