Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-23.1: colexec: fix evaluation of the IN expression with INT2 and INT4 types #121955

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -862,3 +862,14 @@ SELECT -488 OF FROM t;
----
of
-488

# 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