Skip to content

Commit

Permalink
sql: Fix issues causing failures in SQLSmith
Browse files Browse the repository at this point in the history
There are (at least) 3 issues that are currently causing the SQLSmith nightly
test to fail:

1. Issue cockroachdb#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 cockroachdb#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.
  • Loading branch information
andy-kimball committed Apr 23, 2019
1 parent 8c4ba4b commit 408634f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
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

0 comments on commit 408634f

Please sign in to comment.