Skip to content

Implement constant folding for CAST #417

@alamb

Description

@alamb

Is your feature request related to a problem or challenge? Please describe what you are trying to do.
When pushing predicates down into various storage systems, it is common for them to support single column predicates of the form

const OP column

SQL allows users to write predicates against columns using whatever format they deem useful, and inserts the appropriate CAST functions where needed.

For example

echo "1" > /tmp/foo.csv
cargo run -p datafusion-cli

And then run:

> CREATE EXTERNAL TABLE foo(ts double)
STORED AS CSV
LOCATION '/tmp/foo.csv';
0 rows in set. Query took 0 seconds.
> explain verbose select * from foo where ts < 5;
+-----------------------------------------+--------------------------------------------------------------------------+
| plan_type                               | plan                                                                     |
+-----------------------------------------+--------------------------------------------------------------------------+
| logical_plan                            | Projection: #ts                                                          |
|                                         |   Filter: #ts Lt Int64(5)                                                |
|                                         |     TableScan: foo projection=None                                       |
| logical_plan after projection_push_down | Projection: #ts                                                          |
|                                         |   Filter: #ts Lt Int64(5)                                                |
|                                         |     TableScan: foo projection=Some([0])                                  |
| logical_plan after projection_push_down | Projection: #ts                                                          |
|                                         |   Filter: #ts Lt Int64(5)                                                |
|                                         |     TableScan: foo projection=Some([0])                                  |
| physical_plan                           | ProjectionExec: expr=[ts]                                                |
|                                         |   FilterExec: ts < CAST(5 AS Float64)                                    |
|                                         |     CsvExec: source=Path(/tmp/foo.csv: [/tmp/foo.csv]), has_header=false |
+-----------------------------------------+--------------------------------------------------------------------------+
4 rows in set. Query took 0 seconds.

You can see that the filter that was pushed down was ts < CAST(5 AS Float64) which is correct, but it will likely not be recognized by some storage systems (such as IOx).

Describe the solution you'd like
What I would like to see is the expression rewriter pass rewrite ts < CAST(5 AS Float64) into ts < 5.0 (aka do the cast at plan time)

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