petern48 commented on code in PR #17602:
URL: https://github.com/apache/datafusion/pull/17602#discussion_r2353355705


##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1400,13 +1400,40 @@ 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
             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_)
+                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())) =>
+            {
+                if let Some(i) = when_then_expr
+                    .iter()
+                    .position(|(when, _)| is_true(when.as_ref()))
+                {

Review Comment:
   Removing the condition guard results in making the case rule below this one 
inaccessible, so we can't just remove the `.any()` condition here. An 
alternative approach would be to move all of this logic into the same block as 
the rule below this, but the code wouldn't be so clean. I feel like iterating 
over the `when_then_expr` vector twice for now isn't too bad since these 
shouldn't really get too long, but I'm happy to hear other thoughts.



-- 
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]

Reply via email to