Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support limit push down for offset_plan #2566

Merged
merged 9 commits into from
May 20, 2022
Merged

Conversation

Ted-Jiang
Copy link
Member

Which issue does this PR close?

Closes #2550.

like in pg

postgres=# explain analyze  select * from users limit 5 offset 2;
                                                 QUERY PLAN
------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.05..0.17 rows=5 width=80) (actual time=0.059..0.167 rows=5 loops=1)
   ->  Seq Scan on users  (cost=0.00..239.02 rows=10002 width=80) (actual time=0.007..0.050 rows=7 loops=1)
 Planning Time: 0.050 ms
 Execution Time: 0.231 ms
(4 rows)

postgres=# explain analyze  select * from users limit 5 offset 7;
                                                 QUERY PLAN
-------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.17..0.29 rows=5 width=80) (actual time=0.117..0.226 rows=5 loops=1)
   ->  Seq Scan on users  (cost=0.00..239.02 rows=10002 width=80) (actual time=0.013..0.082 rows=12 loops=1)
 Planning Time: 0.060 ms
 Execution Time: 0.350 ms
(4 rows)

if we have limit a + offset b will push limit a+b to tableScan

Rationale for this change

What changes are included in this PR?

Are there any user-facing changes?

@github-actions github-actions bot added the datafusion Changes in the datafusion crate label May 18, 2022

self.limit(plan, query.limit)
//make limit as offset's input will enable limit push down simply
self.offset(plan, query.offset)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is an offset in sql, we will put it as root node in plan tree. So it will easily push new_limit to limit operator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to apply the offset before the limit here?

It looks like we don't currently have a test with both a non-zero offset and a limit. Could you add one here so we can see what the plan looks like?

Per https://www.postgresql.org/docs/current/queries-limit.html If both OFFSET and LIMIT appear, then OFFSET rows are skipped before starting to count the LIMIT rows that are returned. and I am not sure this change will ensure that

Copy link
Member Author

@Ted-Jiang Ted-Jiang May 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

form the explain from pg

postgres=# explain analyze  select * from users limit 5 offset 7;
                                                 QUERY PLAN
-------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.17..0.29 rows=5 width=80) (actual time=0.117..0.226 rows=5 loops=1)
   ->  Seq Scan on users  (cost=0.00..239.02 rows=10002 width=80) (actual time=0.013..0.082 rows=12 loops=1)
 Planning Time: 0.060 ms
 Execution Time: 0.350 ms
(4 rows)

I think in PG, the limit operator has a param with offset, in DF we separate it. So we need apply offset after limit a+b

add test #2566 (comment)

@Ted-Jiang Ted-Jiang marked this pull request as draft May 20, 2022 06:19
@Ted-Jiang Ted-Jiang marked this pull request as ready for review May 20, 2022 07:41
Comment on lines +4864 to +4886
#[test]
fn test_offset_after_limit_with_limit_push() {
let sql = "select id from person where person.id > 100 LIMIT 5 OFFSET 3;";
let expected = "Offset: 3\
\n Limit: 8\
\n Projection: #person.id\
\n Filter: #person.id > Int64(100)\
\n TableScan: person projection=None";

quick_test_with_limit_pushdown(sql, expected);
}

#[test]
fn test_offset_before_limit_with_limit_push() {
let sql = "select id from person where person.id > 100 OFFSET 3 LIMIT 5;";
let expected = "Offset: 3\
\n Limit: 8\
\n Projection: #person.id\
\n Filter: #person.id > Int64(100)\
\n TableScan: person projection=None";
quick_test_with_limit_pushdown(sql, expected);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! That makes it much clearer

@andygrove andygrove merged commit 753e6f8 into apache:master May 20, 2022
gandronchik pushed a commit to cube-js/arrow-datafusion that referenced this pull request Aug 30, 2022
* support offset push down

* change the limit and offset order

* add test for join and subquery

* fmt

* Apply suggestions from code review

Co-authored-by: Andy Grove <andygrove73@gmail.com>

* add sql test in planner.rs

* add sql test in planner.rs

* fix clippy

Co-authored-by: Andy Grove <andygrove73@gmail.com>
gandronchik pushed a commit to cube-js/arrow-datafusion that referenced this pull request Aug 31, 2022
* support offset push down

* change the limit and offset order

* add test for join and subquery

* fmt

* Apply suggestions from code review

Co-authored-by: Andy Grove <andygrove73@gmail.com>

* add sql test in planner.rs

* add sql test in planner.rs

* fix clippy

Co-authored-by: Andy Grove <andygrove73@gmail.com>
gandronchik pushed a commit to cube-js/arrow-datafusion that referenced this pull request Sep 2, 2022
* support offset push down

* change the limit and offset order

* add test for join and subquery

* fmt

* Apply suggestions from code review

Co-authored-by: Andy Grove <andygrove73@gmail.com>

* add sql test in planner.rs

* add sql test in planner.rs

* fix clippy

Co-authored-by: Andy Grove <andygrove73@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
datafusion Changes in the datafusion crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update limit pushdown rule to support offsets
2 participants