Skip to content

Commit

Permalink
colexec: fix IN operator with unsorted tuple
Browse files Browse the repository at this point in the history
The vectorized implementation of an `element IN tuple` expression
assumes that the contents of `tuple` are sorted by the optimizer. Based
on this assumption, it performs a binary search instead of a linear
search.

However, the assumption that the optimizer sorts all tuples is
incorrect. For example, there are cases where the contents of a tuple
are not known at planning-time, so the tuple cannot be sorted.
Performing a binary search with an unsorted tuple causes incorrect query
results.

Now, the vectorized engine sorts tuple contents if they are not already
sorted.

Fixes cockroachdb#68979

Release justification: This commit fixes a bug with the IN operator that
causes incorrect results.

Release note (bug fix): A bug has been fixed which caused incorrect
evaluation of the `IN` operator when the tuple on the right-hand-side
of the operator included a subquery, like
`a IN ('foo', (SELECT s FROM t), 'bar')`.
  • Loading branch information
mgartner committed Aug 31, 2021
1 parent e946415 commit 88d2aba
Show file tree
Hide file tree
Showing 5 changed files with 481 additions and 26 deletions.
4 changes: 2 additions & 2 deletions pkg/sql/colexec/execgen/cmd/execgen/select_in_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func genSelectIn(inputFileContents string, wr io.Writer) error {
)
s := r.Replace(inputFileContents)

assignEq := makeFunctionRegex("_COMPARE", 5)
s = assignEq.ReplaceAllString(s, makeTemplateFunctionCall("Compare", 5))
compare := makeFunctionRegex("_COMPARE", 5)
s = compare.ReplaceAllString(s, makeTemplateFunctionCall("Compare", 5))

s = replaceManipulationFuncs(s)

Expand Down
Loading

0 comments on commit 88d2aba

Please sign in to comment.