Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Limit / offset bug fixes #774

Merged
merged 8 commits into from
Jul 3, 2019
Merged

Conversation

zachmu
Copy link
Contributor

@zachmu zachmu commented Jun 27, 2019

Prior to this PR, the following query returns no rows on a table with many:

SELECT i FROM mytable LIMIT 1 OFFSET 1;

In fact, no queries combining offset and limit return correct results before this PR.

This is because the parser creates an offset node that wraps a limit node, and the limit node's counter is incremented for every row the offset node skips, so that when the offset node is done skipping rows, the wrapped limit node returns fewer rows than asked for.

The fix is to add the offset to the row count when constructing the limit node. This is much simpler than adding ResetCount() or similar, but has the disadvantage of making the plan slightly more difficult to understand to a naive reader, best illustrated by this modified test, where the limit node has a limit of 7 instead of 2 as expressed in the query:

`SELECT foo, bar FROM foo LIMIT 2 OFFSET 5;`: 
     plan.NewOffset(5,
	plan.NewLimit(7, plan.NewProject(
		[]sql.Expression{
			expression.NewUnresolvedColumn("foo"),
			expression.NewUnresolvedColumn("bar"),
		},
		plan.NewUnresolvedTable("foo", ""),
	)),
),

Also included in this PR, queries with negative limits or offsets now return an error.

… override

Signed-off-by: Zach Musgrave <zach@liquidata.co>
…counted against the limit.

Signed-off-by: Zach Musgrave <zach@liquidata.co>
Signed-off-by: Zach Musgrave <zach@liquidata.co>
Signed-off-by: Zach Musgrave <zach@liquidata.co>
Signed-off-by: Zach Musgrave <zach@liquidata.co>
Signed-off-by: Zach Musgrave <zach@liquidata.co>
Signed-off-by: Zach Musgrave <zach@liquidata.co>
@zachmu
Copy link
Contributor Author

zachmu commented Jun 27, 2019

Sorry the commit history is so messy here, missed a -s on one of my commits and had to scramble to fix it :p

@ajnavarro ajnavarro requested a review from a team June 28, 2019 09:24
Copy link
Contributor

@erizocosmico erizocosmico left a comment

Choose a reason for hiding this comment

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

Good catch!

@zachmu
Copy link
Contributor Author

zachmu commented Jun 28, 2019

Thanks for the review, friends! As soon as you merge this in I have another batch of bug fixes incoming.

@erizocosmico
Copy link
Contributor

I have been thinking about this and perhaps there could be a simpler solution, which would just be adding the offset node before the limit node. What do you think?

…en offset and limit nodes.

Signed-off-by: Zach Musgrave <zach@liquidata.co>
@zachmu
Copy link
Contributor Author

zachmu commented Jun 28, 2019

Good suggestion, Miguel. Much simpler. Please review and merge at your convenience.

@ajnavarro
Copy link
Contributor

Thanks a lot for the contribution!

@ajnavarro ajnavarro merged commit 50afd84 into src-d:master Jul 3, 2019
@Hydrocharged Hydrocharged deleted the zachmu/to-contribute branch September 16, 2019 19:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants