alamb commented on code in PR #5820:
URL: https://github.com/apache/arrow-datafusion/pull/5820#discussion_r1156127545


##########
datafusion/optimizer/src/type_coercion.rs:
##########
@@ -638,19 +606,130 @@ fn coerce_agg_exprs_for_signature(
         .collect::<Result<Vec<_>>>()
 }
 
+fn coerce_case_expression(case: Case, schema: &DFSchemaRef) -> Result<Case> {
+    // Given expressions like:
+    //
+    // CASE a1
+    //   WHEN a2 THEN b1
+    //   WHEN a3 THEN b2
+    //   ELSE b3
+    // END
+    //
+    // or:
+    //
+    // CASE
+    //   WHEN x1 THEN b1
+    //   WHEN x2 THEN b2
+    //   ELSE b3
+    // END
+    //
+    // Then all aN (a1, a2, a3) must be converted to a common data type in the 
first example
+    // (case-when expression coercion)
+    //
+    // All xN (x1, x2) must be converted to a boolean data type in the second 
example
+    // (when-boolean expression coercion)
+    //
+    // And all bN (b1, b2, b3) must be converted to a common data type in both 
examples
+    // (then-else expression coercion)
+    //
+    // If any fail to find and cast to a common/specific data type, will 
return error
+    //
+    // Note that case-when and when-boolean expression coercions are mutually 
exclusive
+    // Only one or the other can occur for a case expression, whilst then-else 
expression coercion will always occur
+
+    // prepare types
+    let case_type = case
+        .expr
+        .as_ref()
+        .map(|expr| expr.get_type(&schema))
+        .transpose()?;
+    let then_types = case
+        .when_then_expr
+        .iter()
+        .map(|(_when, then)| then.get_type(&schema))
+        .collect::<Result<Vec<_>>>()?;
+    let else_type = case
+        .else_expr
+        .as_ref()
+        .map(|expr| expr.get_type(&schema))
+        .transpose()?;
+
+    // find common coercible types

Review Comment:
   I found this code really easy to read. Thank you @Jefffrey 



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

Reply via email to