Skip to content
Merged
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
80 changes: 80 additions & 0 deletions datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,16 @@ impl<S: SimplifyInfo> TreeNodeRewriter for Simplifier<'_, S> {
// Rules for Case
//

// CASE WHEN true THEN A ... END --> A
Expr::Case(Case {
expr: None,
mut when_then_expr,
else_expr: _,
}) if !when_then_expr.is_empty() && is_true(when_then_expr[0].0.as_ref()) => {
let (_, then_) = when_then_expr.swap_remove(0);
Transformed::yes(*then_)
}

// CASE
// WHEN X THEN A
// WHEN Y THEN B
Expand Down Expand Up @@ -3552,6 +3562,76 @@ mod tests {
);
}

#[test]
fn simplify_expr_case_when_true() {
// CASE WHEN true THEN 1 ELSE x END --> 1
Copy link
Contributor

Choose a reason for hiding this comment

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

can we please add some negative tests too -- like CASE WHEN a THEN 1 ELSE 2 END?

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")
);

// Negative test: CASE WHEN a THEN 1 ELSE 2 END should not be simplified
let expr = Expr::Case(Case::new(
None,
vec![(Box::new(col("a")), Box::new(lit(1)))],
Some(Box::new(lit(2))),
));
assert_eq!(simplify(expr.clone()), expr);

// Negative test: CASE WHEN false THEN 1 ELSE 2 END should not use this rule
let expr = Expr::Case(Case::new(
None,
vec![(Box::new(lit(false)), Box::new(lit(1)))],
Some(Box::new(lit(2))),
));
assert_ne!(simplify(expr), lit(1));

// Negative test: CASE WHEN col("x") > 5 THEN 1 ELSE 2 END should not be simplified
let expr = Expr::Case(Case::new(
None,
vec![(Box::new(col("x").gt(lit(5))), Box::new(lit(1)))],
Some(Box::new(lit(2))),
));
assert_eq!(simplify(expr.clone()), expr);
}

fn distinct_from(left: impl Into<Expr>, right: impl Into<Expr>) -> Expr {
Expr::BinaryExpr(BinaryExpr {
left: Box::new(left.into()),
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/projection.slt
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ physical_plan
statement ok
drop table t;

# Regression test for
# Regression test for
# https://github.com/apache/datafusion/issues/17513

query I
Expand Down
6 changes: 3 additions & 3 deletions datafusion/sqllogictest/test_files/select.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1656,10 +1656,10 @@ query TT
explain select coalesce(1, y/x), coalesce(2, y/x) from t;
----
logical_plan
01)Projection: CASE WHEN Boolean(true) THEN Int64(1) ELSE CAST(t.y / t.x AS Int64) END AS coalesce(Int64(1),t.y / t.x), CASE WHEN Boolean(true) THEN Int64(2) ELSE CAST(t.y / t.x AS Int64) END AS coalesce(Int64(2),t.y / t.x)
02)--TableScan: t projection=[x, y]
01)Projection: Int64(1) AS coalesce(Int64(1),t.y / t.x), Int64(2) AS coalesce(Int64(2),t.y / t.x)
02)--TableScan: t projection=[]
physical_plan
01)ProjectionExec: expr=[CASE WHEN true THEN 1 ELSE CAST(y@1 / x@0 AS Int64) END as coalesce(Int64(1),t.y / t.x), CASE WHEN true THEN 2 ELSE CAST(y@1 / x@0 AS Int64) END as coalesce(Int64(2),t.y / t.x)]
01)ProjectionExec: expr=[1 as coalesce(Int64(1),t.y / t.x), 2 as coalesce(Int64(2),t.y / t.x)]
02)--DataSourceExec: partitions=1, partition_sizes=[1]

query TT
Expand Down