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

Simplify associative expressions with references #11733

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 32 additions & 0 deletions datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,17 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
//
Expr::Negative(inner) => Transformed::yes(distribute_negation(*inner)),

//
// Rules for Associativity
//
Expr::BinaryExpr(BinaryExpr {
left,
op: op @ (Operator::Plus | Operator::Multiply),
right,
}) if can_rearrange_literals(&left, op, &right) => {
Transformed::yes(rearrange_literals(*left, op, *right))
}

//
// Rules for Case
//
Expand Down Expand Up @@ -3189,6 +3200,27 @@ mod tests {
);
}

#[test]
fn simplify_associative() {
// i + 1 + 2 => i + 3
assert_eq!(simplify(col("c3") + lit(1) + lit(2)), (col("c3") + lit(3)));

// (i + 1) + 2 => i + 3
assert_eq!(
simplify((col("c3") + lit(1)) + lit(2)),
(col("c3") + lit(3))
);

// i * 2 * 3 => i * 6
assert_eq!(simplify(col("c3") * lit(2) * lit(3)), (col("c3") * lit(6)));

// (i * 2) * 3 => i * 6
assert_eq!(
simplify((col("c3") * lit(2)) * lit(3)),
(col("c3") * lit(6))
);
}

#[test]
fn simplify_expr_case_when_then_else() {
// CASE WHEN c2 != false THEN "ok" == "not_ok" ELSE c2 == true
Expand Down
42 changes: 42 additions & 0 deletions datafusion/optimizer/src/simplify_expressions/utils.rs
Copy link
Contributor Author

@tinfoil-knight tinfoil-knight Jul 30, 2024

Choose a reason for hiding this comment

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

Doesn't handle all possible positions currently for an associative expression.

Eg:

(i + 1) + 2; i + 1 + 2 will resolve to i + 3

But something like these won't
(1 + i) + 2
2 + (i + 1)
2 + (1 + i)
1 + 2 + i

Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,45 @@ pub fn distribute_negation(expr: Expr) -> Expr {
_ => Expr::Negative(Box::new(expr)),
}
}

pub fn can_rearrange_literals(left: &Expr, op: Operator, right: &Expr) -> bool {
if let Expr::BinaryExpr(BinaryExpr {
left: l_left,
op: l_op,
right: l_right,
}) = left
{
if l_op == &op
&& matches!(**l_left, Expr::Column(_))
&& matches!(**l_right, Expr::Literal(_))
&& matches!(right, Expr::Literal(_))
{
return true;
}
};
false
}

pub fn rearrange_literals(left: Expr, op: Operator, right: Expr) -> Expr {
if let Expr::BinaryExpr(BinaryExpr {
left: l_left,
op: l_op,
right: l_right,
}) = &left
{
if l_op == &op
&& matches!(**l_left, Expr::Column(_))
&& matches!(**l_right, Expr::Literal(_))
&& matches!(right, Expr::Literal(_))
{
let right_expr = Expr::BinaryExpr(BinaryExpr {
left: l_right.clone(),
op,
right: Box::new(right),
});
return Expr::BinaryExpr(BinaryExpr::new(l_left.clone(), op, Box::new(right_expr)));
}
};

Expr::BinaryExpr(BinaryExpr::new(Box::new(left), op, Box::new(right)))
}
Loading