Skip to content

Commit

Permalink
colexec: fix evaluation of the IN expression with INT2 and INT4 types
Browse files Browse the repository at this point in the history
This commit fixes a possible incorrect result of evaluation of the IN
expression where the left side is INT2 or INT4 type and the right side
has integers outside of the range of the left side's type. We do so by
upcasting the "filter" row to INT8. Note that we already do the
appropriate upcast in the comparison function, so the incorrect results
could only be produced due to the "filter row" no longer being sorted in
case a value overflows.

Release note (bug fix): CockroachDB could previously incorrectly
evaluate IN expressions that had INT2 or INT4 type on the left side and
values outside of the range of the left side on the right side. The bug
has been present since at least 21.1 and is now fixed.
  • Loading branch information
yuzefovich committed Apr 7, 2024
1 parent ff52bbf commit 797c679
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
1 change: 1 addition & 0 deletions pkg/sql/colexec/execgen/cmd/execgen/select_in_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func genSelectIn(inputFileContents string, wr io.Writer) error {
"_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}",
"_TYPE_WIDTH", typeWidthReplacement,
"_GOTYPESLICE", "{{.GoTypeSliceName}}",
"_GOTYPE_UPCAST_INT", `{{if or (eq .VecMethod "Int16") (eq .VecMethod "Int32")}}int64{{else}}{{.GoType}}{{end}}`,
"_GOTYPE", "{{.GoType}}",
"_TYPE", "{{.VecMethod}}",
"TemplateType", "{{.VecMethod}}",
Expand Down
30 changes: 16 additions & 14 deletions pkg/sql/colexec/select_in.eg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions pkg/sql/colexec/select_in_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var (
// {{/*

type _GOTYPESLICE interface{}
type _GOTYPE_UPCAST_INT interface{}
type _GOTYPE interface{}
type _TYPE interface{}

Expand Down Expand Up @@ -146,7 +147,7 @@ func GetInOperator(

type selectInOp_TYPE struct {
colexecop.OneInputHelper
filterRow []_GOTYPE
filterRow []_GOTYPE_UPCAST_INT
colIdx int
hasNulls bool
negate bool
Expand All @@ -157,7 +158,7 @@ var _ colexecop.Operator = &selectInOp_TYPE{}
type projectInOp_TYPE struct {
colexecop.OneInputHelper
allocator *colmem.Allocator
filterRow []_GOTYPE
filterRow []_GOTYPE_UPCAST_INT
colIdx int
outputIdx int
hasNulls bool
Expand All @@ -168,27 +169,32 @@ var _ colexecop.Operator = &projectInOp_TYPE{}

func fillDatumRow_TYPE(
evalCtx *eval.Context, t *types.T, datumTuple *tree.DTuple,
) ([]_GOTYPE, bool) {
) ([]_GOTYPE_UPCAST_INT, bool) {
// Sort the contents of the tuple, if they are not already sorted.
datumTuple.Normalize(evalCtx)

// {{if or (eq .VecMethod "Int16") (eq .VecMethod "Int32")}}
// Ensure that we always upcast all integer types.
conv := colconv.GetDatumToPhysicalFn(types.Int)
//{{else}}
conv := colconv.GetDatumToPhysicalFn(t)
var result []_GOTYPE
// {{end}}
var result []_GOTYPE_UPCAST_INT
hasNulls := false
for _, d := range datumTuple.D {
if d == tree.DNull {
hasNulls = true
} else {
convRaw := conv(d)
converted := convRaw.(_GOTYPE)
converted := convRaw.(_GOTYPE_UPCAST_INT)
result = append(result, converted)
}
}
return result, hasNulls
}

func cmpIn_TYPE(
targetElem _GOTYPE, targetCol _GOTYPESLICE, filterRow []_GOTYPE, hasNulls bool,
targetElem _GOTYPE, targetCol _GOTYPESLICE, filterRow []_GOTYPE_UPCAST_INT, hasNulls bool,
) comparisonResult {
// Filter row input was already sorted in fillDatumRow_TYPE, so we can
// perform a binary search.
Expand Down
11 changes: 11 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/select
Original file line number Diff line number Diff line change
Expand Up @@ -938,3 +938,14 @@ ORDER BY 1 DESC
----
5ebfedee-0dcf-41e6-a315-5fa0b51b9883 2 1999-11-30 23:59:58 +0000 +0000
5ebfedee-0dcf-41e6-a315-5fa0b51b9882 1 1999-11-30 23:59:59 +0000 +0000

# Regression test for incorrect evaluation of the IN filter due to integer
# overflow when working with INT4 type (#102864).
statement ok
CREATE TABLE t102864 (c INT4);
INSERT INTO t102864 (c) VALUES (0);

query I
SELECT c FROM t102864 WHERE c IN (0, 862827606027206657::INT8);
----
0

0 comments on commit 797c679

Please sign in to comment.