aweltsch commented on PR #14283:
URL: https://github.com/apache/datafusion/pull/14283#issuecomment-2613791296

   I also looked into this a little bit. Here's my observations:
   The `data_type` of a `CaseExpr` is the first non-null `data_type`. In the 
`expr_or_expr` specialization this is only the same type as the else expression 
if the _then_expr is null_. My impression is that @jayzhan211 's suggestion to 
handle only the null value would thus make sense.
   
   I personally would have applied the transformation
   ```rust
           let e = &self.when_then_expr[0].1;
           let then_expr = try_cast(Arc::clone(e), &batch.schema(), 
return_type.clone())?;
   
           let then_value = then_expr
               .evaluate_selection(batch, &when_value)?
               .into_array(batch.num_rows())?;
   ```
   since to me this seems more obvious what's the intended goal, i.e. "make 
sure that the then_expr returns the correct datatype for the case expression" 
and seems to be a little bit more generic.
   
   If the suggested fix here is to be accepted I would rather remove the 
`expr_or_expr` specialization again, because from what I have seen the current 
suggestion regresses on the `case_when: expr_or_expr` benchmark.
   
   Also: I was surprised to see that I can't reproduce this issue when using 
datafusion-cli, so from an end-to-end perspective the type coercion seems to 
work somehow :thinking: 
   This is what I did:
   ```sql
   > CREATE TABLE batch2(a int[]) AS VALUES ([1, 2, 3]), (null);
   > SELECT CASE WHEN a IS NULL THEN NULL ELSE a END FROM batch2;
   +--------------------------------------------------------+
   | CASE WHEN batch2.a IS NULL THEN NULL ELSE batch2.a END |
   +--------------------------------------------------------+
   | [1, 2, 3]                                              |
   | NULL                                                   |
   +--------------------------------------------------------+
   ```


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