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

sql: Fix issues causing failures in SQLSmith #37057

Merged
merged 1 commit into from
Apr 24, 2019
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
30 changes: 5 additions & 25 deletions pkg/internal/sqlsmith/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/json_builtins
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/sql/sem/builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/sqlbase/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand Down