diff --git a/pkg/internal/sqlsmith/type.go b/pkg/internal/sqlsmith/type.go index 9f09ae0d2635..462d5293e6fb 100644 --- a/pkg/internal/sqlsmith/type.go +++ b/pkg/internal/sqlsmith/type.go @@ -15,36 +15,16 @@ package sqlsmith import ( - "fmt" - "strings" - + "github.com/cockroachdb/cockroach/pkg/sql/parser" + "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" "github.com/cockroachdb/cockroach/pkg/sql/types" ) -var typeNames = func() map[string]*types.T { - m := map[string]*types.T{ - "int4": types.Int, - "int8": types.Int, - "int8[]": types.IntArray, - "float4": types.Float, - "float8": types.Float, - } - for _, T := range types.OidToType { - m[T.SQLStandardName()] = T - m[T.String()] = T - } - return m -}() - func typeFromName(name string) *types.T { - // Fill in any collated string type names we see. - if sp := strings.Split(name, "STRING COLLATE "); len(sp) == 2 { - typeNames[strings.ToLower(name)] = types.MakeCollatedString(types.String, sp[1]) - } - typ, ok := typeNames[strings.ToLower(name)] - if !ok { - panic(fmt.Errorf("unknown type name: %s", name)) + typ, err := parser.ParseType(name) + if err != nil { + panic(pgerror.AssertionFailedf("failed to parse type: %v", name)) } return typ } diff --git a/pkg/sql/logictest/testdata/logic_test/json_builtins b/pkg/sql/logictest/testdata/logic_test/json_builtins index 6ef7262d7703..517b3e41c52d 100644 --- a/pkg/sql/logictest/testdata/logic_test/json_builtins +++ b/pkg/sql/logictest/testdata/logic_test/json_builtins @@ -481,6 +481,12 @@ SELECT json_build_object(json_object_keys('{"x":3, "y":4}'::JSON), 2) {"x": 2} {"y": 2} +# Regression for panic when bit array is passed as argument. +query T +SELECT json_build_object('a', '0100110'::varbit) +---- +{"a": "0100110"} + # even number of arguments query error pq: json_build_object\(\): argument list must have even number of elements SELECT json_build_object(1,2,3) diff --git a/pkg/sql/sem/builtins/builtins.go b/pkg/sql/sem/builtins/builtins.go index 225738348b2f..de7e361e1b15 100644 --- a/pkg/sql/sem/builtins/builtins.go +++ b/pkg/sql/sem/builtins/builtins.go @@ -4473,7 +4473,9 @@ func asJSONBuildObjectKey(d tree.Datum) (string, error) { return string(*t), nil case *tree.DCollatedString: return t.Contents, nil - 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: + 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, *tree.DBitArray: return tree.AsStringWithFlags(d, tree.FmtBareStrings), nil default: return "", pgerror.AssertionFailedf("unexpected type %T for key value", d) diff --git a/pkg/sql/sqlbase/testutils.go b/pkg/sql/sqlbase/testutils.go index f5e9d52aae6b..ce60d6d467e8 100644 --- a/pkg/sql/sqlbase/testutils.go +++ b/pkg/sql/sqlbase/testutils.go @@ -199,6 +199,8 @@ func RandDatumWithNullChance(rng *rand.Rand, typ *types.T, nullChance int) tree. } } return arr + case types.AnyFamily: + return RandDatumWithNullChance(rng, RandType(rng), nullChance) default: panic(fmt.Sprintf("invalid type %v", typ.DebugString())) }