Skip to content

Expression Simplification forExpr::Case expressions #1693

@alamb

Description

@alamb

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 
END

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

Also 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'
END

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

  1. When there is a literal true in the cases preceded by 0 or 1 literal falses or nulls, use that.
  2. When all the cases are false, --> the otherwise

Additional context
See https://github.com/influxdata/influxdb_iox/pull/3557 for more details

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions