liukun4515 commented on code in PR #2819:
URL: https://github.com/apache/arrow-datafusion/pull/2819#discussion_r912441908


##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -291,12 +292,67 @@ impl PhysicalExpr for CaseExpr {
 /// Create a CASE expression
 pub fn case(
     expr: Option<Arc<dyn PhysicalExpr>>,
-    when_thens: &[WhenThen],
+    when_thens: Vec<WhenThen>,
     else_expr: Option<Arc<dyn PhysicalExpr>>,
+    input_schema: &Schema,
 ) -> Result<Arc<dyn PhysicalExpr>> {
+    // all the result of then and else should be convert to a common data type,
+    // if they can be coercible to a common data type, return error.
+    let coerce_type = get_case_common_type(&when_thens, else_expr.clone(), 
input_schema);
+    let (when_thens, else_expr) = match coerce_type {
+        None => Err(DataFusionError::Plan(format!(
+            "Can't get a common type for then {:?} and else {:?} expression",
+            when_thens, else_expr
+        ))),
+        Some(data_type) => {
+            // cast then expr
+            let left = when_thens
+                .into_iter()
+                .map(|(when, then)| {
+                    let then = try_cast(then, input_schema, 
data_type.clone()).unwrap();
+                    (when, then)
+                })
+                .collect::<Vec<WhenThen>>();
+            let right = match else_expr {
+                None => None,
+                Some(expr) => Some(try_cast(expr, input_schema, 
data_type.clone())?),
+            };
+            Ok((left, right))
+        }
+    }?;
+
     Ok(Arc::new(CaseExpr::try_new(expr, when_thens, else_expr)?))
 }
 
+fn get_case_common_type(
+    when_thens: &[WhenThen],
+    else_expr: Option<Arc<dyn PhysicalExpr>>,

Review Comment:
   The reference is not work for this.



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