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

builtins: fix recently introduced bitmask fns with mixed type arguments #109043

Merged
merged 1 commit into from
Aug 18, 2023
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
31 changes: 31 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/builtin_function
Original file line number Diff line number Diff line change
Expand Up @@ -4166,3 +4166,34 @@ query T
SELECT bitmask_xor('xffffffffffffffffff', '010');
----
111111111111111111111111111111111111111111111111111111111111111111111101

# Regression test for mixed-type arguments (#108976).
query T
SELECT bitmask_or('101'::STRING, B'001'::VARBIT);
----
101

query T
SELECT bitmask_or(B'001'::VARBIT, '101'::STRING);
----
101

query T
SELECT bitmask_and('101'::STRING, B'001'::VARBIT);
----
001

query T
SELECT bitmask_and(B'001'::VARBIT, '101'::STRING);
----
001

query T
SELECT bitmask_xor('101'::STRING, B'001'::VARBIT);
----
100

query T
SELECT bitmask_xor(B'001'::VARBIT, '101'::STRING);
----
100
42 changes: 24 additions & 18 deletions pkg/sql/sem/builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -8187,13 +8187,14 @@ specified store on the node it's run from. One of 'mvccGC', 'merge', 'split',
{Name: "b", Typ: types.String},
},
ReturnType: tree.FixedReturnType(types.VarBit),
Fn: func(_ context.Context, _ *eval.Context, a *tree.DBitArray, b string) (tree.Datum, error) {
bBitArray, err := bitarray.Parse(b)
Fn: func(_ context.Context, _ *eval.Context, datums tree.Datums) (tree.Datum, error) {
a, b := datums[0], datums[1]
bBitArray, err := bitarray.Parse(string(tree.MustBeDString(b)))
if err != nil {
return nil, err
}

return bitmaskOr(a.BitArray.String(), bBitArray.String())
return bitmaskOr(a.(*tree.DBitArray).BitArray.String(), bBitArray.String())
},
Info: "Calculates bitwise OR value of unsigned bit arrays 'a' and 'b' that may have different lengths.",
Volatility: volatility.Immutable,
Expand All @@ -8204,13 +8205,14 @@ specified store on the node it's run from. One of 'mvccGC', 'merge', 'split',
{Name: "b", Typ: types.VarBit},
},
ReturnType: tree.FixedReturnType(types.VarBit),
Fn: func(_ context.Context, _ *eval.Context, a string, b *tree.DBitArray) (tree.Datum, error) {
aBitArray, err := bitarray.Parse(a)
Fn: func(_ context.Context, _ *eval.Context, datums tree.Datums) (tree.Datum, error) {
a, b := datums[0], datums[1]
aBitArray, err := bitarray.Parse(string(tree.MustBeDString(a)))
if err != nil {
return nil, err
}

return bitmaskOr(aBitArray.String(), b.BitArray.String())
return bitmaskOr(aBitArray.String(), b.(*tree.DBitArray).BitArray.String())
},
Info: "Calculates bitwise OR value of unsigned bit arrays 'a' and 'b' that may have different lengths.",
Volatility: volatility.Immutable,
Expand Down Expand Up @@ -8250,13 +8252,14 @@ specified store on the node it's run from. One of 'mvccGC', 'merge', 'split',
{Name: "b", Typ: types.String},
},
ReturnType: tree.FixedReturnType(types.VarBit),
Fn: func(_ context.Context, _ *eval.Context, a *tree.DBitArray, b string) (tree.Datum, error) {
bBitArray, err := bitarray.Parse(b)
Fn: func(_ context.Context, _ *eval.Context, datums tree.Datums) (tree.Datum, error) {
a, b := datums[0], datums[1]
bBitArray, err := bitarray.Parse(string(tree.MustBeDString(b)))
if err != nil {
return nil, err
}

return bitmaskAnd(a.BitArray.String(), bBitArray.String())
return bitmaskAnd(a.(*tree.DBitArray).BitArray.String(), bBitArray.String())
},
Info: "Calculates bitwise AND value of unsigned bit arrays 'a' and 'b' that may have different lengths.",
Volatility: volatility.Immutable,
Expand All @@ -8267,13 +8270,14 @@ specified store on the node it's run from. One of 'mvccGC', 'merge', 'split',
{Name: "b", Typ: types.VarBit},
},
ReturnType: tree.FixedReturnType(types.VarBit),
Fn: func(_ context.Context, _ *eval.Context, a string, b *tree.DBitArray) (tree.Datum, error) {
aBitArray, err := bitarray.Parse(a)
Fn: func(_ context.Context, _ *eval.Context, datums tree.Datums) (tree.Datum, error) {
a, b := datums[0], datums[1]
aBitArray, err := bitarray.Parse(string(tree.MustBeDString(a)))
if err != nil {
return nil, err
}

return bitmaskAnd(aBitArray.String(), b.BitArray.String())
return bitmaskAnd(aBitArray.String(), b.(*tree.DBitArray).BitArray.String())
},
Info: "Calculates bitwise AND value of unsigned bit arrays 'a' and 'b' that may have different lengths.",
Volatility: volatility.Immutable,
Expand Down Expand Up @@ -8313,13 +8317,14 @@ specified store on the node it's run from. One of 'mvccGC', 'merge', 'split',
{Name: "b", Typ: types.String},
},
ReturnType: tree.FixedReturnType(types.VarBit),
Fn: func(_ context.Context, _ *eval.Context, a *tree.DBitArray, b string) (tree.Datum, error) {
bBitArray, err := bitarray.Parse(b)
Fn: func(_ context.Context, _ *eval.Context, datums tree.Datums) (tree.Datum, error) {
a, b := datums[0], datums[1]
bBitArray, err := bitarray.Parse(string(tree.MustBeDString(b)))
if err != nil {
return nil, err
}

return bitmaskXor(a.BitArray.String(), bBitArray.String())
return bitmaskXor(a.(*tree.DBitArray).BitArray.String(), bBitArray.String())
},
Info: "Calculates bitwise XOR value of unsigned bit arrays 'a' and 'b' that may have different lengths.",
Volatility: volatility.Immutable,
Expand All @@ -8330,13 +8335,14 @@ specified store on the node it's run from. One of 'mvccGC', 'merge', 'split',
{Name: "b", Typ: types.VarBit},
},
ReturnType: tree.FixedReturnType(types.VarBit),
Fn: func(_ context.Context, _ *eval.Context, a string, b *tree.DBitArray) (tree.Datum, error) {
aBitArray, err := bitarray.Parse(a)
Fn: func(_ context.Context, _ *eval.Context, datums tree.Datums) (tree.Datum, error) {
a, b := datums[0], datums[1]
aBitArray, err := bitarray.Parse(string(tree.MustBeDString(a)))
if err != nil {
return nil, err
}

return bitmaskXor(aBitArray.String(), b.BitArray.String())
return bitmaskXor(aBitArray.String(), b.(*tree.DBitArray).BitArray.String())
},
Info: "Calculates bitwise XOR value of unsigned bit arrays 'a' and 'b' that may have different lengths.",
Volatility: volatility.Immutable,
Expand Down