Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions datafusion/sqllogictest/test_files/simplify_predicates.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this produces FilterExec: int_col@0 >= 1 AND int_col@0 <= 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;