Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor: refactor simplify negate #3213

Merged
merged 2 commits into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions datafusion/expr/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ 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> {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

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),
_ => None,
Copy link
Contributor

Choose a reason for hiding this comment

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

I would like to suggest replacing _ with an an explicit enumeration of all the other operators, like

Operator::LeftShift | OperatiorRightShift => None

The reason is so that as new operators are added, the compiler will point us at this code and we will get another chance to avoid bugs like https://github.com/apache/arrow-datafusion/pull/3167/files#r946145318

}
}
}

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