Skip to content

Commit

Permalink
minor: refactor simplify negate
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwener committed Aug 20, 2022
1 parent e0a9fa3 commit f05a21f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 59 deletions.
22 changes: 22 additions & 0 deletions datafusion/expr/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@ 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::And => Some(Operator::Or),
Operator::Or => Some(Operator::And),
Operator::Like => Some(Operator::NotLike),
Operator::NotLike => Some(Operator::Like),
Operator::IsDistinctFrom => Some(Operator::IsNotDistinctFrom),
Operator::IsNotDistinctFrom => Some(Operator::IsDistinctFrom),
_ => None,
}
}
}

impl fmt::Display for Operator {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let display = match &self {
Expand Down
65 changes: 6 additions & 59 deletions datafusion/optimizer/src/simplify_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f05a21f

Please sign in to comment.