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


##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -112,7 +112,23 @@ 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) => {
+                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))?;
+
+                match (then_type.clone(), else_type.clone()) {

Review Comment:
   ```suggestion
                   match (&then_type, &else_type) {
   ```
   I think we can avoid these clones.



##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -112,7 +112,23 @@ 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) => {
+                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))?;
+
+                match (then_type.clone(), else_type.clone()) {
+                    (DataType::Null, DataType::Null) => Ok(DataType::Int64),

Review Comment:
   There might be a problem with the following expression.
   ```sql
   CASE data WHEN 1 THEN NULL WHEN 2 THEN 3.3 ELSE NULL END;
   ```
   Its type being float would be more reasonable.
   
   Another more general fix might be to ensure that type coercion has already 
been performed before calling `get_type`, but I am not sure if that can be 
achieved. Therefore, the current solution is okay for me.
   



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