alamb opened a new issue, #17448:
URL: https://github.com/apache/datafusion/issues/17448

   ### Is your feature request related to a problem or challenge?
   
   While reviewing https://github.com/apache/datafusion/pull/17357 I noticed 
that expressions like
   
   ```sql
   CASE WHEN true THEN 1 ELSE x END
   ```
   
   Could be simplified to 
   
   ```sql
   1
   ```
   
   but were not 
   
   Here is a complete example:
   
   ```sql
   > create table foo(x int) as values (1), (2), (3);
   0 row(s) fetched.
   Elapsed 0.004 seconds.
   
   > explain format indent select CASE WHEN true THEN 1 ELSE x END from foo;
   
+---------------+----------------------------------------------------------------------------------------------------------------------------------+
   | plan_type     | plan                                                       
                                                                      |
   
+---------------+----------------------------------------------------------------------------------------------------------------------------------+
   | logical_plan  | Projection: CASE WHEN Boolean(true) THEN Int64(1) ELSE 
CAST(foo.x AS Int64) END                                                  |
   |               |   TableScan: foo projection=[x]                            
                                                                      |
   | physical_plan | ProjectionExec: expr=[CASE WHEN true THEN 1 ELSE CAST(x@0 
AS Int64) END as CASE WHEN Boolean(true) THEN Int64(1) ELSE foo.x END] |
   |               |   DataSourceExec: partitions=1, partition_sizes=[1]        
                                                                      |
   |               |                                                            
                                                                      |
   
+---------------+----------------------------------------------------------------------------------------------------------------------------------+
   2 row(s) fetched.
   Elapsed 0.001 seconds.
   ```
   
   I expect to see the case expression simplified
   
   It does get the correct answer
   
   ```sql
   > select CASE WHEN true THEN 1 ELSE x END from foo;
   +------------------------------------------------------+
   | CASE WHEN Boolean(true) THEN Int64(1) ELSE foo.x END |
   +------------------------------------------------------+
   | 1                                                    |
   | 1                                                    |
   | 1                                                    |
   +------------------------------------------------------+
   3 row(s) fetched.
   Elapsed 0.001 seconds.
   ```
   
   
   ### Describe the solution you'd like
   
   I would like expressions that can be simplified to
   
   ```sql
   CASE 
     WHEN true THEN <expr>
      ...
   ```
   
   Can be simplified to 
   ```sql
   <expr>
   ```
   
   
   
   ### Describe alternatives you've considered
   
   I think we can just extend the existing rules for CASE simplification here:
   
   
https://github.com/apache/datafusion/blob/ec4413e7d7b41c381c98eda909e5ab650be4c5e5/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs#L1398-L1400
   
   ### Additional context
   
   _No response_


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