- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.7k
Description
Is your feature request related to a problem or challenge? Please describe what you are trying to do.
In certain situations , IOx is likely going to make predicates that look like the following
CASE 
  WHEN col IS NULL THEN '' 
  ELSE col 
ENDthat basically map null to the empty string
When applying them to certain specialized chunks (or row groups) we will know there are no NULLs in the col or all NULLs and thus we will end up rewriting it to something like
CASE 
  WHEN true THEN '' 
  ELSE col 
ENDAlso in general, when applying other simplifications / constant folding I can imagine other situations where CASE can be folded such as
CASE
  WHEN extract(day) from now() = 0 THEN 'Monday'
  WHEN extract(day) from now() = 1 THEN 'Tuesday'
  WHEN extract(day) from now() = 2 THEN 'Wednesday'
  WHEN extract(day) from now() = 3 THEN 'Monday'
  ...
  ELSE 'other day'
ENDSo I think it is worth adding into DataFusion generally
Describe the solution you'd like
I would like to add cases to the rewrite rules here:
https://github.com/apache/arrow-datafusion/blob/03075d5f4b3fdfd8f82144fcd409418832a4bf69/datafusion/src/optimizer/simplify_expressions.rs#L440-L454
Some rules I can think of are:
- When there is a literal truein thecasespreceded by 0 or 1 literal falses or nulls, use that.
- When all the cases are false, --> the otherwise
Additional context
See https://github.com/influxdata/influxdb_iox/pull/3557 for more details