-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: Simplify CASE WHEN true THEN expr to expr #17450
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -1399,6 +1399,18 @@ impl<S: SimplifyInfo> TreeNodeRewriter for Simplifier<'_, S> { | |||||||
// Rules for Case | ||||||||
// | ||||||||
|
||||||||
// CASE WHEN true THEN A ... END --> A | ||||||||
Expr::Case(Case { | ||||||||
expr: None, | ||||||||
when_then_expr, | ||||||||
else_expr: _, | ||||||||
}) if !when_then_expr.is_empty() | ||||||||
&& matches!(when_then_expr[0].0.as_ref(), | ||||||||
Expr::Literal(ScalarValue::Boolean(Some(true)), _)) => | ||||||||
{ | ||||||||
Transformed::yes((*when_then_expr[0].1).clone()) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be good to avoid this clone too, How about something like this:
Suggested change
|
||||||||
} | ||||||||
|
||||||||
// CASE | ||||||||
// WHEN X THEN A | ||||||||
// WHEN Y THEN B | ||||||||
|
@@ -3552,6 +3564,61 @@ mod tests { | |||||||
); | ||||||||
} | ||||||||
|
||||||||
#[test] | ||||||||
fn simplify_expr_case_when_true() { | ||||||||
// CASE WHEN true THEN 1 ELSE x END --> 1 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we please add some negative tests too -- like |
||||||||
assert_eq!( | ||||||||
simplify(Expr::Case(Case::new( | ||||||||
None, | ||||||||
vec![( | ||||||||
Box::new(lit(true)), | ||||||||
Box::new(lit(1)), | ||||||||
)], | ||||||||
Some(Box::new(col("x"))), | ||||||||
))), | ||||||||
lit(1) | ||||||||
); | ||||||||
|
||||||||
// CASE WHEN true THEN col("a") ELSE col("b") END --> col("a") | ||||||||
assert_eq!( | ||||||||
simplify(Expr::Case(Case::new( | ||||||||
None, | ||||||||
vec![( | ||||||||
Box::new(lit(true)), | ||||||||
Box::new(col("a")), | ||||||||
)], | ||||||||
Some(Box::new(col("b"))), | ||||||||
))), | ||||||||
col("a") | ||||||||
); | ||||||||
|
||||||||
// CASE WHEN true THEN col("a") WHEN col("x") > 5 THEN col("b") ELSE col("c") END --> col("a") | ||||||||
assert_eq!( | ||||||||
simplify(Expr::Case(Case::new( | ||||||||
None, | ||||||||
vec![ | ||||||||
(Box::new(lit(true)), Box::new(col("a"))), | ||||||||
(Box::new(col("x").gt(lit(5))), Box::new(col("b"))), | ||||||||
], | ||||||||
Some(Box::new(col("c"))), | ||||||||
))), | ||||||||
col("a") | ||||||||
); | ||||||||
|
||||||||
// CASE WHEN true THEN col("a") END --> col("a") (no else clause) | ||||||||
assert_eq!( | ||||||||
simplify(Expr::Case(Case::new( | ||||||||
None, | ||||||||
vec![( | ||||||||
Box::new(lit(true)), | ||||||||
Box::new(col("a")), | ||||||||
)], | ||||||||
None, | ||||||||
))), | ||||||||
col("a") | ||||||||
); | ||||||||
} | ||||||||
|
||||||||
fn distinct_from(left: impl Into<Expr>, right: impl Into<Expr>) -> Expr { | ||||||||
Expr::BinaryExpr(BinaryExpr { | ||||||||
left: Box::new(left.into()), | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we please use
is_true
to follow the pattern in the rest of this method?datafusion/datafusion/optimizer/src/simplify_expressions/utils.rs
Lines 193 to 198 in 4b9a468