alamb commented on code in PR #17835:
URL: https://github.com/apache/datafusion/pull/17835#discussion_r2394632426
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1436,33 +1436,59 @@ impl<S: SimplifyInfo> TreeNodeRewriter for
Simplifier<'_, S> {
// CASE WHEN true THEN A ... END --> A
// CASE WHEN X THEN A WHEN TRUE THEN B ... END --> CASE WHEN X
THEN A ELSE B END
+ // CASE WHEN false THEN A END --> NULL
+ // CASE WHEN false THEN A ELSE B END --> B
+ // CASE WHEN X THEN A WHEN false THEN B END --> CASE WHEN X THEN A
ELSE B END
Expr::Case(Case {
expr: None,
mut when_then_expr,
- else_expr: _,
+ mut else_expr,
// if let guard is not stabilized so we can't use it yet:
https://github.com/rust-lang/rust/issues/51114
// Once it's supported we can avoid searching through
when_then_expr twice in the below .any() and .position() calls
// }) if let Some(i) = when_then_expr.iter().position(|(when,
_)| is_true(when.as_ref())) => {
}) if when_then_expr
.iter()
- .any(|(when, _)| is_true(when.as_ref())) =>
+ .any(|(when, _)| is_true(when.as_ref()) ||
is_false(when.as_ref())) =>
{
- let i = when_then_expr
- .iter()
- .position(|(when, _)| is_true(when.as_ref()))
- .unwrap();
- let (_, then_) = when_then_expr.swap_remove(i);
- // CASE WHEN true THEN A ... END --> A
- if i == 0 {
- return Ok(Transformed::yes(*then_));
+ let mut remove_indices =
Vec::with_capacity(when_then_expr.len());
+ let out_type = info.get_data_type(&when_then_expr[0].1)?;
Review Comment:
It took me moment to convince myself that we did not need to gate on
`!when_then_expr.empty()` to ensure `when_then_expr[0]` doesn't panic -- and
that is because `.any()` needs at least one expr to evaluate to true.
TLDR this is fine, I am just recording my thought process for anyone else
who is interested
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -3810,53 +3826,53 @@ mod tests {
#[test]
fn simplify_expr_case_when_first_true() {
- // CASE WHEN true THEN 1 ELSE x END --> 1
+ // CASE WHEN true THEN 1 ELSE c1 END --> 1
assert_eq!(
simplify(Expr::Case(Case::new(
None,
vec![(Box::new(lit(true)), Box::new(lit(1)),)],
- Some(Box::new(col("x"))),
+ Some(Box::new(col("c1"))),
))),
lit(1)
);
- // CASE WHEN true THEN col("a") ELSE col("b") END --> col("a")
+ // CASE WHEN true THEN col('a') ELSE col('b') END --> col('a')
Review Comment:
I was worried about this change, as it seems to potentially change the
intent of the test -- to use literals rather than column references.
However, I see the issue is that the DataTypes need to match and after
spending some time rewriting these tests to use column references rather than
literals I think the literals are fine
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]