Skip to content

Apache DataFusion SQL Query Engine

Simplify Between expression to Eq (#14994)

Jay Zhanc61e7e597f80
diff --git a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
index e20580df4c..840c108905 100644
--- a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
+++ b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
@@ -942,6 +942,27 @@ impl<S: SimplifyInfo> TreeNodeRewriter for Simplifier<'_, S> {
op: And,
right,
}) if is_op_with(Or, &left, &right) => Transformed::yes(*right),
+ // A >= constant AND constant <= A --> A = constant
+ Expr::BinaryExpr(BinaryExpr {
+ left,
+ op: And,
+ right,
+ }) if can_reduce_to_equal_statement(&left, &right) => {
+ if let Expr::BinaryExpr(BinaryExpr {
+ left: left_left,
+ right: left_right,
+ ..
+ }) = *left
+ {
+ Transformed::yes(Expr::BinaryExpr(BinaryExpr {
+ left: left_left,
+ op: Eq,
+ right: left_right,
+ }))
+ } else {
+ return internal_err!("can_reduce_to_equal_statement should only be called with a BinaryExpr");
+ }
+ }
//
// Rules for Multiply
diff --git a/datafusion/optimizer/src/simplify_expressions/utils.rs b/datafusion/optimizer/src/simplify_expressions/utils.rs
index c30c3631c1..cf182175e4 100644
--- a/datafusion/optimizer/src/simplify_expressions/utils.rs
+++ b/datafusion/optimizer/src/simplify_expressions/utils.rs
@@ -214,6 +214,25 @@ pub fn is_op_with(target_op: Operator, haystack: &Expr, needle: &Expr) -> bool {
matches!(haystack, Expr::BinaryExpr(BinaryExpr { left, op, right }) if op == &target_op && (needle == left.as_ref() || needle == right.as_ref()) && !needle.is_volatile())
}
+pub fn can_reduce_to_equal_statement(haystack: &Expr, needle: &Expr) -> bool {
+ match (haystack, needle) {
+ // a >= constant and constant <= a => a = constant
+ (
+ Expr::BinaryExpr(BinaryExpr {
+ left,
+ op: Operator::GtEq,
+ right,
+ }),
+ Expr::BinaryExpr(BinaryExpr {
+ left: n_left,
+ op: Operator::LtEq,
+ right: n_right,
+ }),
+ ) if left == n_left && right == n_right => true,
+ _ => false,
+ }
+}
+
/// returns true if `not_expr` is !`expr` (not)
pub fn is_not_of(not_expr: &Expr, expr: &Expr) -> bool {
matches!(not_expr, Expr::Not(inner) if expr == inner.as_ref())
diff --git a/datafusion/sqllogictest/test_files/simplify_expr.slt b/datafusion/sqllogictest/test_files/simplify_expr.slt
new file mode 100644
index 0000000000..d10e603ea5
--- /dev/null
+++ b/datafusion/sqllogictest/test_files/simplify_expr.slt
@@ -0,0 +1,34 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+statement count 0
+create table t(a int) as values (1);
+
+# test between simplification
+query TT
+explain select a from t where a BETWEEN 3 and 3
+----
+logical_plan
+01)Filter: t.a = Int32(3)
+02)--TableScan: t projection=[a]
+physical_plan
+01)CoalesceBatchesExec: target_batch_size=8192
+02)--FilterExec: a@0 = 3
+03)----DataSourceExec: partitions=1, partition_sizes=[1]
+
+statement count 0
+drop table t;