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

Unary Complement execution has different results when the parameters are different #76943

Merged
merged 1 commit into from
Mar 2, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/operator
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,47 @@ SELECT |/ -1.0::float

query error cannot take square root of a negative number
SELECT |/ -1.0::decimal

query I
SELECT ~-1;
----
0

query I
SELECT ~0;
----
-1

query I
SELECT ~1;
----
-2

query I
SELECT ~2;
----
-3

query T
SELECT ~B'0';
----
1

query T
SELECT ~B'1';
----
0

statement error lexical error
SELECT ~B'2';

statement error ambiguous unary operator
SELECT ~'0';

statement error ambiguous unary operator
SELECT ~'1';

query I
SELECT ~2;
----
-3
11 changes: 11 additions & 0 deletions pkg/sql/sem/tree/overload.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,13 +750,24 @@ func typeCheckOverloadedExprs(
// The fourth heuristic is to prefer candidates that accepts the "best"
// mutual type in the resolvable type set of all constants.
if bestConstType, ok := commonConstantType(s.exprs, s.constIdxs); ok {
// In case all overloads are filtered out at this step,
// keep track of previous overload indexes to return ambiguous error (>1 overloads)
// instead of unsupported error (0 overloads) when applicable.
prevOverloadIdxs := s.overloadIdxs
for _, i := range s.constIdxs {
s.overloadIdxs = filterOverloads(s.overloads, s.overloadIdxs,
func(o overloadImpl) bool {
return o.params().GetAt(i).Equivalent(bestConstType)
})
}
if ok, typedExprs, fns, err := checkReturn(ctx, semaCtx, &s); ok {
if len(fns) == 0 {
var overloadImpls []overloadImpl
for i := range prevOverloadIdxs {
overloadImpls = append(overloadImpls, s.overloads[i])
}
return typedExprs, overloadImpls, err
}
return typedExprs, fns, err
}
if homogeneousTyp != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/sem/tree/overload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func TestTypeCheckOverloadedExprs(t *testing.T) {
{nil, []Expr{intConst("1")}, []overloadImpl{unaryIntFn, unaryIntFn}, ambiguous, false},
{nil, []Expr{intConst("1")}, []overloadImpl{unaryIntFn, unaryFloatFn}, unaryIntFn, false},
{nil, []Expr{decConst("1.0")}, []overloadImpl{unaryIntFn, unaryDecimalFn}, unaryDecimalFn, false},
{nil, []Expr{decConst("1.0")}, []overloadImpl{unaryIntFn, unaryFloatFn}, unsupported, false},
{nil, []Expr{decConst("1.0")}, []overloadImpl{unaryIntFn, unaryFloatFn}, ambiguous, false},
{nil, []Expr{intConst("1")}, []overloadImpl{unaryIntFn, binaryIntFn}, unaryIntFn, false},
{nil, []Expr{intConst("1")}, []overloadImpl{unaryFloatFn, unaryStringFn}, unaryFloatFn, false},
{nil, []Expr{intConst("1")}, []overloadImpl{unaryStringFn, binaryIntFn}, unsupported, false},
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestTypeCheckOverloadedExprs(t *testing.T) {
{nil, []Expr{NewDInt(1), placeholder(1)}, []overloadImpl{binaryIntFn, binaryIntDateFn}, binaryIntFn, false},
{nil, []Expr{NewDFloat(1), placeholder(1)}, []overloadImpl{binaryIntFn, binaryIntDateFn}, unsupported, false},
{nil, []Expr{intConst("1"), placeholder(1)}, []overloadImpl{binaryIntFn, binaryIntDateFn}, binaryIntFn, false},
{nil, []Expr{decConst("1.0"), placeholder(1)}, []overloadImpl{binaryIntFn, binaryIntDateFn}, unsupported, false}, // Limitation.
{nil, []Expr{decConst("1.0"), placeholder(1)}, []overloadImpl{binaryIntFn, binaryIntDateFn}, ambiguous, false}, // Limitation.
{types.Date, []Expr{NewDInt(1), placeholder(1)}, []overloadImpl{binaryIntFn, binaryIntDateFn}, binaryIntDateFn, false},
{types.Date, []Expr{NewDFloat(1), placeholder(1)}, []overloadImpl{binaryIntFn, binaryIntDateFn}, unsupported, false},
{types.Date, []Expr{intConst("1"), placeholder(1)}, []overloadImpl{binaryIntFn, binaryIntDateFn}, binaryIntDateFn, false},
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/sem/tree/type_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func TestTypeCheckError(t *testing.T) {
expr string
expected string
}{
{`'1' + '2'`, `unsupported binary operator:`},
{`'1' + '2'`, `ambiguous binary operator:`},
{`'a' + 0`, `unsupported binary operator:`},
{`1.1 # 3.1`, `unsupported binary operator:`},
{`~0.1`, `unsupported unary operator:`},
Expand Down