Skip to content

Commit

Permalink
distsqlrun: fix lookup join with limit
Browse files Browse the repository at this point in the history
Previously, lookup join used its processRowHelper in an inappropriate
place (not directly before returning a row) which caused the final rows
in a limit query to get omitted in certain cases. Correct this problem.

Release note (bug fix): lookup joins no longer omit rows in certain
circumstances during limit queries.
  • Loading branch information
jordanlewis committed Oct 1, 2018
1 parent d088e36 commit 54cd53c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
15 changes: 6 additions & 9 deletions pkg/sql/distsqlrun/joinreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,9 @@ func (jr *joinReader) performLookup() (joinReaderState, *ProducerMetadata) {
return jrStateUnknown, jr.DrainHelper()
}
if renderedRow != nil {
if row := jr.ProcessRowHelper(renderedRow); row != nil {
jr.toEmit = append(jr.toEmit, jr.out.rowAlloc.CopyRow(row))
if jr.emitted != nil {
jr.emitted[inputRowIdx] = true
}
jr.toEmit = append(jr.toEmit, jr.out.rowAlloc.CopyRow(renderedRow))
if jr.emitted != nil {
jr.emitted[inputRowIdx] = true
}
}
}
Expand All @@ -524,10 +522,8 @@ func (jr *joinReader) collectUnmatched() joinReaderState {
if jr.joinType == sqlbase.LeftOuterJoin {
for i := 0; i < len(jr.inputRows); i++ {
if !jr.emitted[i] {
if renderedRow := jr.renderUnmatchedRow(jr.inputRows[i], leftSide); renderedRow != nil {
if row := jr.ProcessRowHelper(renderedRow); row != nil {
jr.toEmit = append(jr.toEmit, jr.out.rowAlloc.CopyRow(row))
}
if row := jr.renderUnmatchedRow(jr.inputRows[i], leftSide); row != nil {
jr.toEmit = append(jr.toEmit, jr.out.rowAlloc.CopyRow(row))
}
}
}
Expand All @@ -551,6 +547,7 @@ func (jr *joinReader) emitRow() (joinReaderState, sqlbase.EncDatumRow, *Producer
}
row := jr.toEmit[0]
jr.toEmit = jr.toEmit[1:]
row = jr.ProcessRowHelper(row)
return jrEmittingRows, row, nil
}

Expand Down
23 changes: 23 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/distsql_lookup_join
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,26 @@ SELECT t1.c, t2.d FROM multiples t1 LEFT JOIN multiples@bc t2 ON t1.c = t2.b WHE
----
6 12
12 24

# Regression test for #30812: make sure that lookup joins with limit don't
# omit the final row.

statement ok
CREATE TABLE t_30182_l (id INT PRIMARY KEY, name TEXT NOT NULL)

statement ok
CREATE TABLE t_30182_r (id INT PRIMARY KEY, name TEXT NOT NULL, l_id int)

statement ok
INSERT INTO t_30182_l (id, name) values(1, 'a')

statement ok
INSERT INTO t_30182_r values(1, 'foo', 1);

query ITI
SELECT r.* FROM t_30182_r r
JOIN t_30182_l l on r.l_id = l.id
WHERE r.name = 'foo'
LIMIT 1
----
1 foo 1

0 comments on commit 54cd53c

Please sign in to comment.