Skip to content

Commit

Permalink
executor: fix panic when limit is too large (#7936) (#8004)
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros authored and zz-jason committed Oct 23, 2018
1 parent eaa8b3a commit 62c1241
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 @@ -641,6 +641,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 @@ -222,7 +222,7 @@ func (e *SortExec) keyChunksLess(i, j int) bool {
type TopNExec struct {
SortExec
limit *plan.PhysicalLimit
totalLimit int
totalLimit uint64

chkHeap *topNChunkHeap
}
Expand Down Expand Up @@ -298,7 +298,7 @@ func (e *TopNExec) Open(ctx context.Context) error {
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 @@ -326,7 +326,7 @@ func (e *TopNExec) loadChunksUntilTotalLimit(ctx context.Context) error {
e.rowChunks = chunk.NewList(e.retTypes(), 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].newChunk()
err := e.children[0].Next(ctx, srcChk)
if err != nil {
Expand Down Expand Up @@ -354,7 +354,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 62c1241

Please sign in to comment.