Skip to content

Commit 3d85fad

Browse files
authored
Optimizer: do not simplify != NULL (#43434) (#43442)
ref #43407
1 parent bc8eaad commit 3d85fad

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

planner/core/casetest/testdata/predicate_simplification_in.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"select 1 from t A, t B where A.f <> 3 and B.f in (1,2,3) and A.f <> 1 and A.f <> 2 -- on different columns. No simplification should be done.",
2828
"select 1 from t A, t B where B.f <> 2 and A.f <> 3 and B.f in (1,2,3) and A.f in (3,1,4) and A.f <> 1 and A.f <> 2 -- simplification for two columns.",
2929
"select f from ts use index() where f <> '1' and f in ('1','2','3') -- Simple case with string type",
30-
"select count(*) cnt from ts where f <> '1' and f in ('1','2','3') group by a having cnt > 100 -- aggregate "
30+
"select count(*) cnt from ts where f <> '1' and f in ('1','2','3') group by a having cnt > 100 -- aggregate ",
31+
"select f from t where f <> NULL and f in (1,2,3) -- Special case of NULL with no simplification.",
32+
"select f from t where f != NULL and f in (NULL,2,3) -- Special case of NULL with no simplification."
3133
]
3234
}
3335
]

planner/core/casetest/testdata/predicate_simplification_out.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@
185185
" └─Selection 20.00 cop[tikv] in(test.ts.f, \"2\", \"3\")",
186186
" └─TableFullScan 10000.00 cop[tikv] table:ts keep order:false, stats:pseudo"
187187
]
188+
},
189+
{
190+
"SQL": "select f from t where f <> NULL and f in (1,2,3) -- Special case of NULL with no simplification.",
191+
"Plan": [
192+
"TableReader 0.00 root data:Selection",
193+
"└─Selection 0.00 cop[tikv] in(test.t.f, 1, 2, 3), ne(test.t.f, NULL)",
194+
" └─TableFullScan 10000.00 cop[tikv] table:t keep order:false, stats:pseudo"
195+
]
196+
},
197+
{
198+
"SQL": "select f from t where f != NULL and f in (NULL,2,3) -- Special case of NULL with no simplification.",
199+
"Plan": [
200+
"TableReader 0.00 root data:Selection",
201+
"└─Selection 0.00 cop[tikv] in(test.t.f, NULL, 2, 3), ne(test.t.f, NULL)",
202+
" └─TableFullScan 10000.00 cop[tikv] table:t keep order:false, stats:pseudo"
203+
]
188204
}
189205
]
190206
}

planner/core/rule_predicate_simplification.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ func updateInPredicate(inPredicate expression.Expression, notEQPredicate express
8888
}
8989
v := inPredicate.(*expression.ScalarFunction)
9090
notEQValue := notEQPredicate.(*expression.ScalarFunction).GetArgs()[1].(*expression.Constant)
91+
// do not simplify != NULL since it is always false.
92+
if notEQValue.Value.IsNull() {
93+
return inPredicate, true
94+
}
9195
newValues := make([]expression.Expression, 0, len(v.GetArgs()))
9296
var lastValue *expression.Constant
9397
for _, element := range v.GetArgs() {

0 commit comments

Comments
 (0)