kosiew commented on code in PR #24490:
URL: https://github.com/apache/datafusion/pull/24490#discussion_r3940334233


##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -747,6 +747,105 @@ impl ConstEvaluator {
     }
 }
 
+/// Position of the single pushdown-eligible CASE argument; every other
+/// argument must be a metadata-free literal.
+fn case_pushdown_target(args: &[Expr]) -> Option<usize> {
+    let mut case_position = None;
+    for (position, arg) in args.iter().enumerate() {
+        match arg {
+            Expr::Literal(_, None) => {}
+            Expr::Case(case) if case_pushdown_eligible(case) => {
+                if case_position.replace(position).is_some() {
+                    return None;
+                }
+            }
+            _ => return None,
+        }
+    }
+    case_position
+}
+
+fn case_pushdown_eligible(case: &Case) -> bool {
+    if case.expr.is_some() || case.when_then_expr.is_empty() {
+        return false;
+    }
+    let mut output_type: Option<DataType> = None;
+    let mut literal_of_common_type = |e: &Expr| -> bool {
+        match e {
+            Expr::Literal(s, None) => {
+                let data_type = s.data_type();
+                if data_type == DataType::Null {
+                    return true;
+                }
+                match &output_type {
+                    Some(t) => *t == data_type,
+                    None => {
+                        output_type = Some(data_type);
+                        true
+                    }
+                }
+            }

Review Comment:
   One small suggestion: this guard checks the data type, metadata, and 
nullability, but intentionally doesn't check the field name because 
`SimplifyExpressions` restores the original name through `NamePreserver`. Could 
we add a regression at that boundary for `f(CASE ...)` without an explicit 
alias? I think that would make this ownership clear and protect the behavior if 
the surrounding rewrite machinery changes later.



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