Skip to content

Add limit offset support for cross shard queries - #3738

Merged
sougou merged 9 commits into
vitessio:masterfrom
tinyspeck:offset-support-kind-of
Mar 13, 2018
Merged

sougou merged 9 commits into
vitessio:masterfrom
tinyspeck:offset-support-kind-of

Conversation

@rafael

@rafael rafael commented Mar 9, 2018 •

Copy link
Copy Markdown
Contributor

Description

This commit adds support to limit offset queries that have order by clauses eg:

   select id from users order by id limit 1 offset 1

Implementation modifies queries at runtime to send to all shards LIMIT + OFFSET and then relies on route primitive returning results in order. With these results it calculates the proper limit offset that needs to return in the response.

Giving this, there is an important caveat that it's important to keep in mind:
Vitess enforces a max number of rows returned per query (defaults to 10k).
This effectively means that if a shard has more than LIMIT + OFFSET rows,
that query will fail.

However this should work for most of the common cases and makes Vitess more flexible
and SQL compatible.

@rafael
rafael requested review from demmer and sougou March 9, 2018 01:14
Rafael Chacon added 3 commits March 8, 2018 17:16
This commit adds to support to limit offset queries that have order by clauses eg:

   select id from users order by id limit 1 offset 1

Implementation modifies queries at runtime to send to all shards LIMIT + OFFSET and then relies on route primitive returning results in order. With this it calculates the proper limit offset that it needs to return in the response.

Giving this there is an important caveat that it's important to keep in mind:
Vitess enforces a max number of rows returned per query (defaults to 10k).
This effectively means that if a shard has more than LIMIT + OFFSET rows,
that query will fail.

However this should work for most of the common cases and makes Vitess more flexible
and SQL compatible.

Signed-off-by: Rafael Chacon <rafael@slack-corp.com>
Signed-off-by: Rafael Chacon <rafael@slack-corp.com>
Signed-off-by: Rafael Chacon <rafael@slack-corp.com>
@rafael
rafael force-pushed the offset-support-kind-of branch from 914068a to 1ff3267 Compare March 9, 2018 01:17

@sougou sougou left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

vtgate v3 tests are also failing.

Comment thread data/test/vtgate/select_cases.txt Outdated
"Name": "user",
"Sharded": true
},
"Query": "select user_id from music order by user_id asc limit 20",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be limit 30. However, given that 10 and 20 could be bind vars, the limit cannot be pre-computed at planbuilder time.

So, one option is to generate something limit :__upper_limit, and compute the value for this bind var as count+limit at execution time.

Comment thread go/vt/vtgate/planbuilder/limit.go Outdated
@@ -127,6 +123,15 @@ func (l *limit) SetLimit(limit *sqlparser.Limit) error {
}
l.elimit.Count = pv
l.input.SetUpperLimit(count)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here you should push the bind var instead.

* Per feedback, update limit in planner to be a bind var when offset is present.

Signed-off-by: Rafael Chacon <rafael@slack-corp.com>
@rafael
rafael force-pushed the offset-support-kind-of branch from 3c2f465 to 1710009 Compare March 9, 2018 18:31
Comment thread go/vt/vtgate/engine/limit.go Outdated
Count sqltypes.PlanValue
Input Primitive
Count sqltypes.PlanValue
Offset *sqltypes.PlanValue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Better not to use a pointer. Use Offset.IsNull instead.

Comment thread go/vt/vtgate/engine/limit.go Outdated
}
// When offset is present, this bind var is set by the planner so we
// can hijack the limit value
if l.Offset != nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Use IsNull.

}
}

func TestLimitOffsetExecute(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it's better to add tests for those corner cases where count+offset==len, etc. That will give you the confidence on the off-by-one bugs, but will also prevent future bugs.

Comment thread go/vt/vtgate/planbuilder/limit.go Outdated
l.input.SetUpperLimit(count)

offset, ok := limit.Offset.(*sqlparser.SQLVal)
if ok {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Better to move this at the beginning and make the non-offset part as the else (or make this block return early).
Otherwise, we'll be calling SetUpperLimit twice.

Comment thread go/vt/vtgate/planbuilder/postprocess.go Outdated
}
lb := newLimit(bldr)
if limit != nil && limit.Offset != nil && len(orderBy) == 0 {
return nil, errors.New("unsupported: offset limit for cross-shard queries")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This error should be offset limit for cross-shard queries without order by right?
I think if we want to enforce this, we should apply the rule even if offset is not specified.

I'm thinking we should not enforce this. If people don't want ordering, it's their problem.

@sougou

sougou commented Mar 10, 2018

Copy link
Copy Markdown
Contributor

I'm also thinking it's not worth special-casing the simple limit case for the bind var. I think the code will be simpler if we always pushed __upper_limit down.

@rafael
rafael force-pushed the offset-support-kind-of branch from bdf4ecb to 5f181e2 Compare March 11, 2018 19:27
* Per review, simplify the code by always pushing __upper_limit to the route.
* Do not enforce order.

Signed-off-by: Rafael Chacon <rafael@slack-corp.com>
@rafael
rafael force-pushed the offset-support-kind-of branch from 5f181e2 to 5fdebe0 Compare March 11, 2018 19:36
Rafael Chacon added 2 commits March 11, 2018 12:46
Signed-off-by: Rafael Chacon <rafael@slack-corp.com>
Signed-off-by: Rafael Chacon <rafael@slack-corp.com>
@rafael
rafael force-pushed the offset-support-kind-of branch from 7684aee to 220742c Compare March 11, 2018 21:30
…Execute

Signed-off-by: Rafael Chacon <rafael@slack-corp.com>
@rafael
rafael force-pushed the offset-support-kind-of branch from 220742c to a8c07da Compare March 12, 2018 04:04

@sougou sougou left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Change is good. Some final nits.

return err
}
l.elimit.Offset = pv
case nil:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This case can be skipped.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Clarification: the case is needed, but it can be a no-op.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Comment thread go/vt/vtgate/planbuilder/postprocess.go Outdated
}

func pushLimit(limit *sqlparser.Limit, bldr builder) (builder, error) {
func pushLimit(limit *sqlparser.Limit, orderBy sqlparser.OrderBy, bldr builder) (builder, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No need to pass orderBy any more.

Signed-off-by: Rafael Chacon <rafael@slack-corp.com>
@rafael

rafael commented Mar 13, 2018

Copy link
Copy Markdown
Contributor Author

Thanks @sougou ! Just addressed these last comments.

@sougou
sougou merged commit bd3c9c4 into vitessio:master Mar 13, 2018
@rafael
rafael deleted the offset-support-kind-of branch March 13, 2018 20:57
systay pushed a commit that referenced this pull request Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants