Skip to content

Commit

Permalink
Merge pull request cockroachdb#42843 from yuzefovich/backport19.2-42831
Browse files Browse the repository at this point in the history
release-19.2: colexec: fix a bug with top K sort not handling K > 1024
  • Loading branch information
yuzefovich authored Nov 27, 2019
2 parents 97051e3 + 9c3b22b commit a49cb8e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
6 changes: 3 additions & 3 deletions pkg/sql/colexec/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ func TestSort(t *testing.T) {
func TestSortRandomized(t *testing.T) {
defer leaktest.AfterTest(t)()
rng, _ := randutil.NewPseudoRand()
nTups := 1025
k := uint16(4)
maxCols := 5
nTups := coldata.BatchSize()*2 + 1
k := uint16(rng.Intn(int(nTups))) + 1
maxCols := 3
// TODO(yuzefovich): randomize types as well.
typs := make([]coltypes.T, maxCols)
for i := range typs {
Expand Down
9 changes: 5 additions & 4 deletions pkg/sql/colexec/sorttopk.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,11 @@ func (t *topKSorter) emit() coldata.Batch {
vec.Copy(
coldata.CopySliceArgs{
SliceArgs: coldata.SliceArgs{
ColType: t.inputTypes[i],
Src: t.topK.ColVec(i),
Sel: t.sel,
SrcEndIdx: uint64(toEmit),
ColType: t.inputTypes[i],
Src: t.topK.ColVec(i),
Sel: t.sel,
SrcStartIdx: uint64(t.emitted),
SrcEndIdx: uint64(t.emitted + toEmit),
},
},
)
Expand Down
15 changes: 15 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/vectorize
Original file line number Diff line number Diff line change
Expand Up @@ -991,3 +991,18 @@ NULL NULL 1
NULL 1 1
1 NULL 0
1 1 2

# Regression test for #42816 - top K sort when K is greated than
# coldata.BatchSize().
statement ok
CREATE TABLE t_42816 (a int); INSERT INTO t_42816 SELECT * FROM generate_series(0, 1025)

query I
SELECT * FROM t_42816 ORDER BY a OFFSET 1020 LIMIT 10
----
1020
1021
1022
1023
1024
1025

0 comments on commit a49cb8e

Please sign in to comment.