jonahgao commented on code in PR #11542:
URL: https://github.com/apache/datafusion/pull/11542#discussion_r1685888821


##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -112,7 +112,22 @@ impl ExprSchemable for Expr {
             Expr::OuterReferenceColumn(ty, _) => Ok(ty.clone()),
             Expr::ScalarVariable(ty, _) => Ok(ty.clone()),
             Expr::Literal(l) => Ok(l.data_type()),
-            Expr::Case(case) => case.when_then_expr[0].1.get_type(schema),
+            // Expr::Case(case) => case.when_then_expr[0].1.get_type(schema),
+            Expr::Case(case) => {
+                let then_type = case.when_then_expr[0].1.get_type(schema)?;
+                if !then_type.is_null() {
+                    return Ok(then_type);
+                }
+
+                let else_type = case
+                    .else_expr
+                    .as_ref()
+                    .map_or(Ok(DataType::Null), |e| e.get_type(schema))?;
+                if !else_type.is_null() {
+                    return Ok(else_type);
+                }
+                Ok(DataType::Int64)

Review Comment:
   This is because `type_coercion` will convert their types to `Utf8`.
   ```sh
   | logical_plan after type_coercion                           | Projection: 
arrow_typeof(CASE CAST(example.data AS Int64) WHEN Int64(1) THEN CAST(NULL AS 
Utf8) WHEN Int64(2) THEN Utf8("FOO") ELSE CAST(NULL AS Utf8) END)               
                                                                                
                                                                                
     |
   |                                                            |   TableScan: 
example
   ```
   You can check this 
[test](https://github.com/jonahgao/datafusion/blob/d9ff0cafd01614fe850ef4c12ba67761613336e8/datafusion/expr/src/expr_schema.rs#L677).
 It does not perform type coercion.



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to