You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2025/09/16 11:09:25 UTC

[I] Further simplify CASE ... WHEN expressions [datafusion]

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

   ### Is your feature request related to a problem or challenge?
   
   One form of Case statements look like
   ```sql
   CASE 
     WHEN <expr> THEN <expr>
     WHEN <expr> THEN <expr>
     ...
      ELSE
   END
   ```
   
   If there is a `WHEN true` then all subsequent case statements can not execute. So for example
   
   ```sql
   CASE 
     WHEN x=y THEN 1 
     WHEN true THEN 2 
     ELSE 3    -- can not be hit
   END
   ```
   
   Simplified to 
   ```sql
   CASE 
     WHEN x=y THEN 1 
     ELSE 2 
   END
   ```
   
   And likewise
   
   ```sql
   CASE 
     WHEN x=y THEN 1 
     WHEN y=z THEN 2 
     WHEN true THEN 3 
     WHEN a=b THEN 5 -- can not be hit
     ELSE 5    -- can not be hit
   END
   ```
   
   Simplified to 
   ```sql
   CASE 
     WHEN x=y THEN 1 
     WHEN y=z THEN 2 
     ELSE 3
   END
   ```
   
   
   ### Describe the solution you'd like
   
   Implement the above simplifcation / optimization along with tests:
   1. Unit tests in the simplifier
   2. sqllogictests 
   
   ### Describe alternatives you've considered
   
   in https://github.com/apache/datafusion/pull/17450 @EeshanBembi  implemented one form of this simplification and I think it could be extended fairly straightforwardly:
   
   
   You can see this by running an explain plan (see that the name is changed to `1`):
   
   ```sql
   > explain select CASE WHEN true then 1 else 2 END;
   +---------------+-------------------------------+
   | plan_type     | plan                          |
   +---------------+-------------------------------+
   | physical_plan | ┌───────────────────────────┐ |
   |               | │       ProjectionExec      │ |
   |               | │    --------------------   │ |
   |               | │  CASE WHEN Boolean(true)  │ |
   |               | │     THEN Int64(1) ELSE    │ |
   |               | │        Int64(2) END:      │ |
   |               | │             1             │ |
   |               | └─────────────┬─────────────┘ |
   |               | ┌─────────────┴─────────────┐ |
   |               | │     PlaceholderRowExec    │ |
   |               | └───────────────────────────┘ |
   |               |                               |
   +---------------+-------------------------------+
   1 row(s) fetched.
   Elapsed 0.002 seconds.
   ```
   
   However, it doesn't handle the case
   
   ### 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-unsubscribe@datafusion.apache.org.apache.org

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


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


Re: [I] Further simplify CASE ... WHEN expressions [datafusion]

Posted by "petern48 (via GitHub)" <gi...@apache.org>.
petern48 commented on issue #17589:
URL: https://github.com/apache/datafusion/issues/17589#issuecomment-3299412220

   take


-- 
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-unsubscribe@datafusion.apache.org

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


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


Re: [I] Further simplify CASE ... WHEN expressions [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #17589:
URL: https://github.com/apache/datafusion/issues/17589#issuecomment-3297886085

   This is well sepcified and there is an example PR so I think it would make agood first PR


-- 
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-unsubscribe@datafusion.apache.org

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


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


Re: [I] Further simplify CASE ... WHEN expressions [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb closed issue #17589: Further simplify CASE ... WHEN expressions
URL: https://github.com/apache/datafusion/issues/17589


-- 
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-unsubscribe@datafusion.apache.org

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


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