This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git
The following commit(s) were added to refs/heads/master by this push:
new 72487cfb0 minor: refactor simplify negate (#3213)
72487cfb0 is described below
commit 72487cfb0305f1d3c40d179ba963d73d57a79d48
Author: jakevin <[email protected]>
AuthorDate: Sun Aug 21 18:43:08 2022 +0800
minor: refactor simplify negate (#3213)
* minor: refactor simplify negate
* *: remove _
---
datafusion/expr/src/operator.rs | 35 +++++++++++++
datafusion/optimizer/src/simplify_expressions.rs | 65 +++---------------------
2 files changed, 41 insertions(+), 59 deletions(-)
diff --git a/datafusion/expr/src/operator.rs b/datafusion/expr/src/operator.rs
index f2cdb3555..a5b752c64 100644
--- a/datafusion/expr/src/operator.rs
+++ b/datafusion/expr/src/operator.rs
@@ -79,6 +79,41 @@ pub enum Operator {
StringConcat,
}
+impl Operator {
+ /// If the operator can be negated, return the negated operator
+ /// otherwise return None
+ pub fn negate(&self) -> Option<Operator> {
+ match self {
+ Operator::Eq => Some(Operator::NotEq),
+ Operator::NotEq => Some(Operator::Eq),
+ Operator::Lt => Some(Operator::GtEq),
+ Operator::LtEq => Some(Operator::Gt),
+ Operator::Gt => Some(Operator::LtEq),
+ Operator::GtEq => Some(Operator::Lt),
+ Operator::Like => Some(Operator::NotLike),
+ Operator::NotLike => Some(Operator::Like),
+ Operator::IsDistinctFrom => Some(Operator::IsNotDistinctFrom),
+ Operator::IsNotDistinctFrom => Some(Operator::IsDistinctFrom),
+ Operator::Plus
+ | Operator::Minus
+ | Operator::Multiply
+ | Operator::Divide
+ | Operator::Modulo
+ | Operator::And
+ | Operator::Or
+ | Operator::RegexMatch
+ | Operator::RegexIMatch
+ | Operator::RegexNotMatch
+ | Operator::RegexNotIMatch
+ | Operator::BitwiseAnd
+ | Operator::BitwiseOr
+ | Operator::BitwiseShiftRight
+ | Operator::BitwiseShiftLeft
+ | Operator::StringConcat => None,
+ }
+ }
+}
+
impl fmt::Display for Operator {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let display = match &self {
diff --git a/datafusion/optimizer/src/simplify_expressions.rs
b/datafusion/optimizer/src/simplify_expressions.rs
index 8bb829024..34ec24fb6 100644
--- a/datafusion/optimizer/src/simplify_expressions.rs
+++ b/datafusion/optimizer/src/simplify_expressions.rs
@@ -190,67 +190,14 @@ fn as_bool_lit(expr: Expr) -> Option<bool> {
fn negate_clause(expr: Expr) -> Expr {
match expr {
Expr::BinaryExpr { left, op, right } => {
- match op {
- // not (A = B) ===> (A <> B)
- Operator::Eq => Expr::BinaryExpr {
- left,
- op: Operator::NotEq,
- right,
- },
- // not (A <> B) ===> (A = B)
- Operator::NotEq => Expr::BinaryExpr {
- left,
- op: Operator::Eq,
- right,
- },
- // not (A < B) ===> (A >= B)
- Operator::Lt => Expr::BinaryExpr {
- left,
- op: Operator::GtEq,
- right,
- },
- // not (A <= B) ===> (A > B)
- Operator::LtEq => Expr::BinaryExpr {
- left,
- op: Operator::Gt,
- right,
- },
- // not (A > B) ===> (A <= B)
- Operator::Gt => Expr::BinaryExpr {
- left,
- op: Operator::LtEq,
- right,
- },
- // not (A >= B) ===> (A < B)
- Operator::GtEq => Expr::BinaryExpr {
+ if let Some(negated_op) = op.negate() {
+ return Expr::BinaryExpr {
left,
- op: Operator::Lt,
+ op: negated_op,
right,
- },
- // not (A like 'B') ===> (A not like 'B')
- Operator::Like => Expr::BinaryExpr {
- left,
- op: Operator::NotLike,
- right,
- },
- // not (A not like 'B') ===> (A like 'B')
- Operator::NotLike => Expr::BinaryExpr {
- left,
- op: Operator::Like,
- right,
- },
- // not (A is distinct from B) ===> (A is not distinct from B)
- Operator::IsDistinctFrom => Expr::BinaryExpr {
- left,
- op: Operator::IsNotDistinctFrom,
- right,
- },
- // not (A is not distinct from B) ===> (A is distinct from B)
- Operator::IsNotDistinctFrom => Expr::BinaryExpr {
- left,
- op: Operator::IsDistinctFrom,
- right,
- },
+ };
+ }
+ match op {
// not (A and B) ===> (not A) or (not B)
Operator::And => {
let left = negate_clause(*left);