Skip to content

Commit

Permalink
Merge #46456
Browse files Browse the repository at this point in the history
46456: colexec: fix a seek error with interleaved tables in the cfetcher r=jordanlewis a=rohany

Fixes #46140.

This PR fixes a bug where the cfetcher was using an incorrect
predicate to skip interleaved child rows when performing
a reverse scan.

Release justification: bug fix
Release note (bug fix): Fix a bug where the vectorized engine
could sometimes give an incorrect result when reading from
interleaved parents or children.

Co-authored-by: Rohan Yadav <[email protected]>
  • Loading branch information
craig[bot] and rohany committed Mar 24, 2020
2 parents 0e98864 + f327ada commit d53a78b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
21 changes: 15 additions & 6 deletions pkg/sql/colexec/cfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,10 @@ const (
// -> fetchNextKVWithUnfinishedRow
stateDecodeFirstKVOfRow

// stateSeekPrefix is the state of skipping all keys that sort before a
// prefix. s.machine.seekPrefix must be set to the prefix to seek to.
// state[1] must be set, and seekPrefix will transition to that state once it
// finds the first key with that prefix.
// stateSeekPrefix is the state of skipping all keys that sort before
// (or after, in the case of a reverse scan) a prefix. s.machine.seekPrefix
// must be set to the prefix to seek to. state[1] must be set, and seekPrefix
// will transition to that state once it finds the first key with that prefix.
// 1. fetch next kv into nextKV buffer
// 2. kv doesn't match seek prefix?
// -> seekPrefix
Expand Down Expand Up @@ -768,9 +768,18 @@ func (rf *cFetcher) nextBatch(ctx context.Context) (coldata.Batch, error) {
rf.machine.state[1] = stateEmitLastBatch
break
}
// The order we perform the comparison in depends on whether we are
// performing a reverse scan or not. If we are performing a reverse
// scan, then we want to seek until we find a key less than seekPrefix.
var comparison int
if rf.reverse {
comparison = bytes.Compare(rf.machine.seekPrefix, kv.Key)
} else {
comparison = bytes.Compare(kv.Key, rf.machine.seekPrefix)
}
// TODO(jordan): if nextKV returns newSpan = true, set the new span
// prefix and indicate that it needs decoding.
if bytes.Compare(kv.Key, rf.machine.seekPrefix) >= 0 {
// prefix and indicate that it needs decoding.
if comparison >= 0 {
rf.machine.nextKV = kv
break
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/vectorize
Original file line number Diff line number Diff line change
Expand Up @@ -1175,3 +1175,16 @@ SELECT * FROM mixed_type_a AS a JOIN mixed_type_b AS b ON a.a = b.a AND a.b < (n

statement ok
RESET vectorize

# Regression for 46140.
statement ok
DROP TABLE IF EXISTS t0, t1;
CREATE TABLE t0(c0 INT);
CREATE TABLE t1(c0 BOOL) INTERLEAVE IN PARENT t0(rowid);
INSERT INTO t0(c0) VALUES (0);
INSERT INTO t1(rowid, c0) VALUES(0, TRUE)

query I
SELECT max(t1.rowid) FROM t1 WHERE t1.c0
----
0

0 comments on commit d53a78b

Please sign in to comment.