Skip to content

Commit ac0ec27

Browse files
committed
reorgnize file
1 parent fc32e8e commit ac0ec27

File tree

5 files changed

+57
-30
lines changed

5 files changed

+57
-30
lines changed

datafusion/optimizer/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ pub mod push_down_limit;
6161
pub mod replace_distinct_aggregate;
6262
pub mod scalar_subquery_to_join;
6363
pub mod simplify_expressions;
64-
mod simplify_predicates;
6564
pub mod single_distinct_to_groupby;
6665
pub mod utils;
6766

datafusion/optimizer/src/push_down_filter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use datafusion_expr::{
4040
};
4141

4242
use crate::optimizer::ApplyOrder;
43-
use crate::simplify_predicates::simplify_predicates;
43+
use crate::simplify_expressions::simplify_predicates;
4444
use crate::utils::{has_all_column_refs, is_restrict_null_predicate};
4545
use crate::{OptimizerConfig, OptimizerRule};
4646

datafusion/optimizer/src/simplify_expressions/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ mod guarantees;
2323
mod inlist_simplifier;
2424
mod regex;
2525
pub mod simplify_exprs;
26+
mod simplify_predicates;
2627
mod unwrap_cast;
2728
mod utils;
2829

@@ -31,6 +32,7 @@ pub use datafusion_expr::simplify::{SimplifyContext, SimplifyInfo};
3132

3233
pub use expr_simplifier::*;
3334
pub use simplify_exprs::*;
35+
pub use simplify_predicates::simplify_predicates;
3436

3537
// Export for test in datafusion/core/tests/optimizer_integration.rs
3638
pub use guarantees::GuaranteeRewriter;

datafusion/optimizer/src/simplify_predicates.rs renamed to datafusion/optimizer/src/simplify_expressions/simplify_predicates.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use std::collections::BTreeMap;
4141
///
4242
/// # Returns
4343
/// A `Result` containing a vector of simplified `Expr` predicates.
44-
pub(crate) fn simplify_predicates(predicates: Vec<Expr>) -> Result<Vec<Expr>> {
44+
pub fn simplify_predicates(predicates: Vec<Expr>) -> Result<Vec<Expr>> {
4545
// Early return for simple cases
4646
if predicates.len() <= 1 {
4747
return Ok(predicates);
@@ -132,34 +132,38 @@ fn simplify_column_predicates(predicates: Vec<Expr>) -> Result<Vec<Expr>> {
132132

133133
let mut result = Vec::new();
134134

135-
// If we have equality predicates, they're the most restrictive
136135
if !eq_predicates.is_empty() {
137-
if eq_predicates.len() > 1 {
138-
result.push(Expr::Literal(ScalarValue::Boolean(Some(false)), None));
136+
// If there are many equality predicates, we can only keep one if they are all the same
137+
// If they are not the same, add false to the result
138+
if eq_predicates.len() == 1
139+
|| eq_predicates.iter().all(|e| e == &eq_predicates[0])
140+
{
141+
result.push(eq_predicates.pop().unwrap());
139142
} else {
140-
result.push(eq_predicates[0].clone());
143+
// If they are not the same, add a false predicate
144+
result.push(Expr::Literal(ScalarValue::Boolean(Some(false)), None));
141145
}
142-
} else {
143-
// Handle all greater-than-style predicates (keep the most restrictive - highest value)
144-
if !greater_predicates.is_empty() {
145-
if let Some(most_restrictive) =
146-
find_most_restrictive_predicate(&greater_predicates, true)?
147-
{
148-
result.push(most_restrictive);
149-
} else {
150-
result.extend(greater_predicates);
151-
}
146+
}
147+
148+
// Handle all greater-than-style predicates (keep the most restrictive - highest value)
149+
if !greater_predicates.is_empty() {
150+
if let Some(most_restrictive) =
151+
find_most_restrictive_predicate(&greater_predicates, true)?
152+
{
153+
result.push(most_restrictive);
154+
} else {
155+
result.extend(greater_predicates);
152156
}
157+
}
153158

154-
// Handle all less-than-style predicates (keep the most restrictive - lowest value)
155-
if !less_predicates.is_empty() {
156-
if let Some(most_restrictive) =
157-
find_most_restrictive_predicate(&less_predicates, false)?
158-
{
159-
result.push(most_restrictive);
160-
} else {
161-
result.extend(less_predicates);
162-
}
159+
// Handle all less-than-style predicates (keep the most restrictive - lowest value)
160+
if !less_predicates.is_empty() {
161+
if let Some(most_restrictive) =
162+
find_most_restrictive_predicate(&less_predicates, false)?
163+
{
164+
result.push(most_restrictive);
165+
} else {
166+
result.extend(less_predicates);
163167
}
164168
}
165169

datafusion/sqllogictest/test_files/simplify_predicates.slt

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,35 @@ logical_plan
7070
01)Filter: test_data.float_col < Float32(8) AND test_data.int_col > Int32(6)
7171
02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col]
7272

73+
# x = 7 AND x = 7 should simplify to x = 7
74+
query TT
75+
EXPLAIN SELECT * FROM test_data WHERE int_col = 7 AND int_col = 7;
76+
----
77+
logical_plan
78+
01)Filter: test_data.int_col = Int32(7)
79+
02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col]
80+
81+
# x = 7 AND x = 6 should simplify to false
82+
query TT
83+
EXPLAIN SELECT * FROM test_data WHERE int_col = 7 AND int_col = 6;
84+
----
85+
logical_plan EmptyRelation
86+
87+
# TODO: x = 7 AND x < 2 should simplify to false
88+
query TT
89+
EXPLAIN SELECT * FROM test_data WHERE int_col = 7 AND int_col < 2;
90+
----
91+
logical_plan
92+
01)Filter: test_data.int_col = Int32(7) AND test_data.int_col < Int32(2)
93+
02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col]
7394

74-
# x = 7 AND x > 5 should simplify to x = 7
95+
96+
# TODO: x = 7 AND x > 5 should simplify to x = 7
7597
query TT
7698
EXPLAIN SELECT * FROM test_data WHERE int_col = 7 AND int_col > 5;
7799
----
78100
logical_plan
79-
01)Filter: test_data.int_col = Int32(7)
101+
01)Filter: test_data.int_col = Int32(7) AND test_data.int_col > Int32(5)
80102
02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col]
81103

82104
# str_col > 'apple' AND str_col > 'banana' should simplify to str_col > 'banana'
@@ -148,7 +170,7 @@ logical_plan
148170
07)------Filter: test_data2.value < Int32(50) AND test_data2.id > Int32(10)
149171
08)--------TableScan: test_data2 projection=[id, value]
150172

151-
# Case 13: Handling negated predicates
173+
# Handling negated predicates
152174
# NOT (x < 10) AND NOT (x < 5) should simplify to NOT (x < 10)
153175
query TT
154176
EXPLAIN SELECT * FROM test_data WHERE NOT (int_col < 10) AND NOT (int_col < 5);
@@ -198,7 +220,7 @@ logical_plan
198220
01)Filter: (test_data.int_col > Int32(5) OR test_data.float_col < Float32(10)) AND (test_data.int_col > Int32(6) OR test_data.float_col < Float32(8))
199221
02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col]
200222

201-
# Case 20: Combination of AND and OR with simplifiable predicates
223+
# Combination of AND and OR with simplifiable predicates
202224
query TT
203225
EXPLAIN SELECT * FROM test_data
204226
WHERE (int_col > 5 AND int_col > 6)

0 commit comments

Comments
 (0)