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

executor: fix panic when limit is too large #7936

Merged
merged 3 commits into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,10 @@ func (s *testSuite) TestSelectOrderBy(c *C) {
r = tk.MustQuery("select * from select_order_test order by name, id limit 1 offset 100;")
r.Check(testkit.Rows())

// Test limit exceeds int range.
r = tk.MustQuery("select id from select_order_test order by name, id limit 18446744073709551615;")
r.Check(testkit.Rows("1", "2"))

// Test multiple field
r = tk.MustQuery("select id, name from select_order_test where id = 1 group by id, name limit 1 offset 0;")
r.Check(testkit.Rows("1 hello"))
Expand Down
8 changes: 4 additions & 4 deletions executor/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (e *SortExec) keyChunksLess(i, j int) bool {
type TopNExec struct {
SortExec
limit *plannercore.PhysicalLimit
totalLimit int
totalLimit uint64

chkHeap *topNChunkHeap
}
Expand Down Expand Up @@ -307,7 +307,7 @@ func (e *TopNExec) Next(ctx context.Context, chk *chunk.Chunk) error {
}
chk.Reset()
if !e.fetched {
e.totalLimit = int(e.limit.Offset + e.limit.Count)
e.totalLimit = e.limit.Offset + e.limit.Count
winoros marked this conversation as resolved.
Show resolved Hide resolved
e.Idx = int(e.limit.Offset)
err := e.loadChunksUntilTotalLimit(ctx)
if err != nil {
Expand Down Expand Up @@ -335,7 +335,7 @@ func (e *TopNExec) loadChunksUntilTotalLimit(ctx context.Context) error {
e.rowChunks = chunk.NewList(e.retTypes(), e.initCap, e.maxChunkSize)
e.rowChunks.GetMemTracker().AttachTo(e.memTracker)
e.rowChunks.GetMemTracker().SetLabel("rowChunks")
for e.rowChunks.Len() < e.totalLimit {
for uint64(e.rowChunks.Len()) < e.totalLimit {
srcChk := e.children[0].newFirstChunk()
err := e.children[0].Next(ctx, srcChk)
if err != nil {
Expand Down Expand Up @@ -363,7 +363,7 @@ const topNCompactionFactor = 4

func (e *TopNExec) executeTopN(ctx context.Context) error {
heap.Init(e.chkHeap)
for len(e.rowPtrs) > e.totalLimit {
for uint64(len(e.rowPtrs)) > e.totalLimit {
// The number of rows we loaded may exceeds total limit, remove greatest rows by Pop.
heap.Pop(e.chkHeap)
}
Expand Down