Repository navigation
Add limit offset support for cross shard queries - #3738
Conversation
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>
914068a to
1ff3267
Compare
sougou
left a comment
There was a problem hiding this comment.
vtgate v3 tests are also failing.
| "Name": "user", | ||
| "Sharded": true | ||
| }, | ||
| "Query": "select user_id from music order by user_id asc limit 20", |
There was a problem hiding this comment.
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.
| @@ -127,6 +123,15 @@ func (l *limit) SetLimit(limit *sqlparser.Limit) error { | |||
| } | |||
| l.elimit.Count = pv | |||
| l.input.SetUpperLimit(count) | |||
There was a problem hiding this comment.
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>
3c2f465 to
1710009
Compare
| Count sqltypes.PlanValue | ||
| Input Primitive | ||
| Count sqltypes.PlanValue | ||
| Offset *sqltypes.PlanValue |
There was a problem hiding this comment.
Better not to use a pointer. Use Offset.IsNull instead.
| } | ||
| // When offset is present, this bind var is set by the planner so we | ||
| // can hijack the limit value | ||
| if l.Offset != nil { |
| } | ||
| } | ||
|
|
||
| func TestLimitOffsetExecute(t *testing.T) { |
There was a problem hiding this comment.
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.
| l.input.SetUpperLimit(count) | ||
|
|
||
| offset, ok := limit.Offset.(*sqlparser.SQLVal) | ||
| if ok { |
There was a problem hiding this comment.
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.
| } | ||
| lb := newLimit(bldr) | ||
| if limit != nil && limit.Offset != nil && len(orderBy) == 0 { | ||
| return nil, errors.New("unsupported: offset limit for cross-shard queries") |
There was a problem hiding this comment.
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.
|
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 |
bdf4ecb to
5f181e2
Compare
* 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>
5f181e2 to
5fdebe0
Compare
Signed-off-by: Rafael Chacon <rafael@slack-corp.com>
Signed-off-by: Rafael Chacon <rafael@slack-corp.com>
7684aee to
220742c
Compare
…Execute Signed-off-by: Rafael Chacon <rafael@slack-corp.com>
220742c to
a8c07da
Compare
sougou
left a comment
There was a problem hiding this comment.
Change is good. Some final nits.
| return err | ||
| } | ||
| l.elimit.Offset = pv | ||
| case nil: |
There was a problem hiding this comment.
Clarification: the case is needed, but it can be a no-op.
| } | ||
|
|
||
| func pushLimit(limit *sqlparser.Limit, bldr builder) (builder, error) { | ||
| func pushLimit(limit *sqlparser.Limit, orderBy sqlparser.OrderBy, bldr builder) (builder, error) { |
There was a problem hiding this comment.
No need to pass orderBy any more.
Signed-off-by: Rafael Chacon <rafael@slack-corp.com>
|
Thanks @sougou ! Just addressed these last comments. |
Description
This commit adds support to limit offset queries that have order by clauses eg:
Implementation modifies queries at runtime to send to all shards
LIMIT + OFFSETand 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 + OFFSETrows,that query will fail.
However this should work for most of the common cases and makes Vitess more flexible
and SQL compatible.