Skip to content

Commit 144f155

Browse files
authored
fix: wrong simplification for >= >, <= < (#18222)
## Which issue does this PR close? - Closes #18214. ## Rationale for this change In find_most_restrictive_predicate, operation is ignored, but it should be considered when boundaries are equal. ## What changes are included in this PR? check the operator, when boundaries are equal. ## Are these changes tested? UT ## Are there any user-facing changes? No
1 parent be85bf4 commit 144f155

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

datafusion/optimizer/src/simplify_expressions/simplify_predicates.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn find_most_restrictive_predicate(
194194
let mut best_value: Option<&ScalarValue> = None;
195195

196196
for (idx, pred) in predicates.iter().enumerate() {
197-
if let Expr::BinaryExpr(BinaryExpr { left, op: _, right }) = pred {
197+
if let Expr::BinaryExpr(BinaryExpr { left, op, right }) = pred {
198198
// Extract the literal value based on which side has it
199199
let scalar_value = match (right.as_literal(), left.as_literal()) {
200200
(Some(scalar), _) => Some(scalar),
@@ -207,8 +207,12 @@ fn find_most_restrictive_predicate(
207207
let comparison = scalar.try_cmp(current_best)?;
208208
let is_better = if find_greater {
209209
comparison == std::cmp::Ordering::Greater
210+
|| (comparison == std::cmp::Ordering::Equal
211+
&& op == &Operator::Gt)
210212
} else {
211213
comparison == std::cmp::Ordering::Less
214+
|| (comparison == std::cmp::Ordering::Equal
215+
&& op == &Operator::Lt)
212216
};
213217

214218
if is_better {

datafusion/sqllogictest/test_files/simplify_predicates.slt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,5 +230,17 @@ logical_plan
230230
01)Filter: test_data.int_col > Int32(5) AND test_data.int_col > Int32(6) OR test_data.float_col < Float32(10) AND test_data.float_col < Float32(8)
231231
02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col]
232232

233+
234+
query TT
235+
EXPLAIN SELECT * FROM (
236+
SELECT * FROM test_data
237+
WHERE int_col > 1 AND int_col < 10
238+
) WHERE int_col >= 1 AND int_col <= 10;
239+
----
240+
logical_plan
241+
01)Filter: test_data.int_col > Int32(1) AND test_data.int_col < Int32(10)
242+
02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col]
243+
244+
233245
statement ok
234246
set datafusion.explain.logical_plan_only=false;

0 commit comments

Comments
 (0)