Skip to content

Eliminate OFFSET 0 in the logical plan optimization #3355

@HaoYang670

Description

@HaoYang670

Is your feature request related to a problem or challenge? Please describe what you are trying to do.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

❯ create view v as select 1 as a, 2 as b, 3 as c;
0 rows in set. Query took 0.010 seconds.
❯ explain select * from v offset 0 limit 2;
+---------------+-------------------------------------------------------+
| plan_type     | plan                                                  |
+---------------+-------------------------------------------------------+
| logical_plan  | Limit: skip=0, fetch=2                                |
|               |   Projection: #v.a, #v.b, #v.c                        |
|               |     TableScan: v projection=[a, b, c], fetch=2        |
| physical_plan | GlobalLimitExec: skip=0, fetch=2                      |
|               |   ProjectionExec: expr=[a@0 as a, b@1 as b, c@2 as c] |
|               |     ProjectionExec: expr=[1 as a, 2 as b, 3 as c]     |
|               |       EmptyExec: produce_one_row=true                 |
|               |                                                       |
+---------------+-------------------------------------------------------+
❯ explain select * from v limit 2;
+---------------+-------------------------------------------------------+
| plan_type     | plan                                                  |
+---------------+-------------------------------------------------------+
| logical_plan  | Limit: skip=None, fetch=2                             |
|               |   Projection: #v.a, #v.b, #v.c                        |
|               |     TableScan: v projection=[a, b, c], fetch=2        |
| physical_plan | GlobalLimitExec: skip=None, fetch=2                   |
|               |   ProjectionExec: expr=[a@0 as a, b@1 as b, c@2 as c] |
|               |     ProjectionExec: expr=[1 as a, 2 as b, 3 as c]     |
|               |       EmptyExec: produce_one_row=true                 |
|               |                                                       |
+---------------+-------------------------------------------------------+

I'd like explain select * from v offset 0 limit 2 and explain select * from v limit 2; to have same logical plan.

Also, in eliminate_limit.rs, we should eliminate the Limit plan when fetch = None and skip = 0 or skip = None.
https://github.com/apache/arrow-datafusion/blob/master/datafusion/optimizer/src/eliminate_limit.rs#L75-L91
Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
We could not do this.

Additional context
Add any other context or screenshots about the feature request here.

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