Skip to content

Commit

Permalink
executor: fix panic when limit is too large (#7936)
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros authored Oct 18, 2018
1 parent de931ca commit 1d3bb7b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
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
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

0 comments on commit 1d3bb7b

Please sign in to comment.