From 6699294cb778c8e1f04399b9b957fa850a3b60b4 Mon Sep 17 00:00:00 2001 From: Chongchen Chen Date: Wed, 22 Oct 2025 22:08:21 +0800 Subject: [PATCH] fix: wrong simplification for >= >, <= < --- .../src/simplify_expressions/simplify_predicates.rs | 6 +++++- .../sqllogictest/test_files/simplify_predicates.slt | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/datafusion/optimizer/src/simplify_expressions/simplify_predicates.rs b/datafusion/optimizer/src/simplify_expressions/simplify_predicates.rs index 131404e60706..e811ce731310 100644 --- a/datafusion/optimizer/src/simplify_expressions/simplify_predicates.rs +++ b/datafusion/optimizer/src/simplify_expressions/simplify_predicates.rs @@ -194,7 +194,7 @@ fn find_most_restrictive_predicate( let mut best_value: Option<&ScalarValue> = None; for (idx, pred) in predicates.iter().enumerate() { - if let Expr::BinaryExpr(BinaryExpr { left, op: _, right }) = pred { + if let Expr::BinaryExpr(BinaryExpr { left, op, right }) = pred { // Extract the literal value based on which side has it let scalar_value = match (right.as_literal(), left.as_literal()) { (Some(scalar), _) => Some(scalar), @@ -207,8 +207,12 @@ fn find_most_restrictive_predicate( let comparison = scalar.try_cmp(current_best)?; let is_better = if find_greater { comparison == std::cmp::Ordering::Greater + || (comparison == std::cmp::Ordering::Equal + && op == &Operator::Gt) } else { comparison == std::cmp::Ordering::Less + || (comparison == std::cmp::Ordering::Equal + && op == &Operator::Lt) }; if is_better { diff --git a/datafusion/sqllogictest/test_files/simplify_predicates.slt b/datafusion/sqllogictest/test_files/simplify_predicates.slt index 31ce1efd21c7..c2a21ea7103c 100644 --- a/datafusion/sqllogictest/test_files/simplify_predicates.slt +++ b/datafusion/sqllogictest/test_files/simplify_predicates.slt @@ -230,5 +230,17 @@ logical_plan 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) 02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] + +query TT +EXPLAIN SELECT * FROM ( + SELECT * FROM test_data + WHERE int_col > 1 AND int_col < 10 +) WHERE int_col >= 1 AND int_col <= 10; +---- +logical_plan +01)Filter: test_data.int_col > Int32(1) AND test_data.int_col < Int32(10) +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] + + statement ok set datafusion.explain.logical_plan_only=false;