Skip to content

Commit 4aaf05a

Browse files
craig[bot]andy-kimball
andcommitted
Merge #37057
37057: sql: Fix issues causing failures in SQLSmith r=andy-kimball a=andy-kimball There are (at least) 3 issues that are currently causing the SQLSmith nightly test to fail: 1. Issue #36830 - panic: windowNode can't be run in local mode 2. Panic when calling builtin functions with ANY parameters 3. Panic when json_build_object is called with DBitArray datum This commit fixes #2 and #3: 2. SQLSmith now generates a random datum type when it encounters an ANY param. 3. Add DBitArray to the list of datums handled by the json_build_object function. In addition, the fix for #2 exposed a SQLSmith bug, where it ws unable to parse the type names of existing tables. The fix is to change typeFromName to use parser.ParseType. After these fixes, and once issue #36830 is fixed, the SQLSmith tests should start passing again in nightlies. Release note (sql change): Fix panic when json_build_object is called with BIT/VARBIT values. Co-authored-by: Andrew Kimball <andyk@cockroachlabs.com>
2 parents 4fa5318 + 408634f commit 4aaf05a

File tree

4 files changed

+16
-26
lines changed

4 files changed

+16
-26
lines changed

pkg/internal/sqlsmith/type.go

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,16 @@
1515
package sqlsmith
1616

1717
import (
18-
"fmt"
19-
"strings"
20-
18+
"github.com/cockroachdb/cockroach/pkg/sql/parser"
19+
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
2120
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
2221
"github.com/cockroachdb/cockroach/pkg/sql/types"
2322
)
2423

25-
var typeNames = func() map[string]*types.T {
26-
m := map[string]*types.T{
27-
"int4": types.Int,
28-
"int8": types.Int,
29-
"int8[]": types.IntArray,
30-
"float4": types.Float,
31-
"float8": types.Float,
32-
}
33-
for _, T := range types.OidToType {
34-
m[T.SQLStandardName()] = T
35-
m[T.String()] = T
36-
}
37-
return m
38-
}()
39-
4024
func typeFromName(name string) *types.T {
41-
// Fill in any collated string type names we see.
42-
if sp := strings.Split(name, "STRING COLLATE "); len(sp) == 2 {
43-
typeNames[strings.ToLower(name)] = types.MakeCollatedString(types.String, sp[1])
44-
}
45-
typ, ok := typeNames[strings.ToLower(name)]
46-
if !ok {
47-
panic(fmt.Errorf("unknown type name: %s", name))
25+
typ, err := parser.ParseType(name)
26+
if err != nil {
27+
panic(pgerror.AssertionFailedf("failed to parse type: %v", name))
4828
}
4929
return typ
5030
}

pkg/sql/logictest/testdata/logic_test/json_builtins

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,12 @@ SELECT json_build_object(json_object_keys('{"x":3, "y":4}'::JSON), 2)
481481
{"x": 2}
482482
{"y": 2}
483483

484+
# Regression for panic when bit array is passed as argument.
485+
query T
486+
SELECT json_build_object('a', '0100110'::varbit)
487+
----
488+
{"a": "0100110"}
489+
484490
# even number of arguments
485491
query error pq: json_build_object\(\): argument list must have even number of elements
486492
SELECT json_build_object(1,2,3)

pkg/sql/sem/builtins/builtins.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4473,7 +4473,9 @@ func asJSONBuildObjectKey(d tree.Datum) (string, error) {
44734473
return string(*t), nil
44744474
case *tree.DCollatedString:
44754475
return t.Contents, nil
4476-
case *tree.DBool, *tree.DInt, *tree.DFloat, *tree.DDecimal, *tree.DTimestamp, *tree.DTimestampTZ, *tree.DDate, *tree.DUuid, *tree.DInterval, *tree.DBytes, *tree.DIPAddr, *tree.DOid, *tree.DTime:
4476+
case *tree.DBool, *tree.DInt, *tree.DFloat, *tree.DDecimal, *tree.DTimestamp, *tree.DTimestampTZ,
4477+
*tree.DDate, *tree.DUuid, *tree.DInterval, *tree.DBytes, *tree.DIPAddr, *tree.DOid,
4478+
*tree.DTime, *tree.DBitArray:
44774479
return tree.AsStringWithFlags(d, tree.FmtBareStrings), nil
44784480
default:
44794481
return "", pgerror.AssertionFailedf("unexpected type %T for key value", d)

pkg/sql/sqlbase/testutils.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ func RandDatumWithNullChance(rng *rand.Rand, typ *types.T, nullChance int) tree.
199199
}
200200
}
201201
return arr
202+
case types.AnyFamily:
203+
return RandDatumWithNullChance(rng, RandType(rng), nullChance)
202204
default:
203205
panic(fmt.Sprintf("invalid type %v", typ.DebugString()))
204206
}

0 commit comments

Comments
 (0)