Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
33084: sql: deflake partition test again r=jordanlewis a=jordanlewis

The RandDatum function could still occasionally return datums with
invalid column types, such as arrays with unknown-typed elements. This
should fix things once and for all, and cleans up the code a bit as
well.

Closes cockroachdb#32889.

Release note: None

Co-authored-by: Jordan Lewis <jordanthelewis@gmail.com>
  • Loading branch information
craig[bot] and jordanlewis committed Dec 13, 2018
2 parents c800fc3 + ba953e4 commit 7d0deef
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/sem/tree/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (ctx *FmtCtx) FormatNode(n NodeFormatter) {
ctx.WriteString(":::")
colType, err := coltypes.DatumTypeToColumnType(typ)
if err != nil {
panic(err)
panic(fmt.Sprintf("invalid datatype %v", typ))
}
colType.Format(ctx.Buffer, f.EncodeFlags())
}
Expand Down
32 changes: 16 additions & 16 deletions pkg/sql/sqlbase/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,7 @@ func RandDatumWithNullChance(rng *rand.Rand, typ ColumnType, nullChance int) tre
return tree.DNull
case ColumnType_ARRAY:
if typ.ArrayContents == nil {
var contentsTyp ColumnType
LOOP:
for {
contentsTyp = RandColumnType(rng)
switch contentsTyp.SemanticType {
// Can't have an array of an array.
case ColumnType_ARRAY, ColumnType_JSONB:
default:
break LOOP
}
}
var contentsTyp = RandArrayContentsColumnType(rng)
typ.ArrayContents = &contentsTyp.SemanticType
typ.Locale = contentsTyp.Locale
}
Expand Down Expand Up @@ -260,20 +250,30 @@ func RandCollationLocale(rng *rand.Rand) *string {

// RandColumnType returns a random ColumnType value.
func RandColumnType(rng *rand.Rand) ColumnType {
typ := ColumnType{SemanticType: columnSemanticTypes[rng.Intn(len(columnSemanticTypes))]}
return randColumnType(rng, columnSemanticTypes)
}

// RandArrayContentsColumnType returns a random ColumnType that's guaranteed
// to be valid to use as the contents of an array.
func RandArrayContentsColumnType(rng *rand.Rand) ColumnType {
return randColumnType(rng, arrayElemSemanticTypes)
}

func randColumnType(rng *rand.Rand, types []ColumnType_SemanticType) ColumnType {
typ := ColumnType{SemanticType: types[rng.Intn(len(types))]}
if typ.SemanticType == ColumnType_BIT {
typ.Width = int32(rng.Intn(50))
}
if typ.SemanticType == ColumnType_COLLATEDSTRING {
typ.Locale = RandCollationLocale(rng)
}
if typ.SemanticType == ColumnType_ARRAY {
typ.ArrayContents = &arrayElemSemanticTypes[rng.Intn(len(arrayElemSemanticTypes))]
if *typ.ArrayContents == ColumnType_COLLATEDSTRING {
inner := RandArrayContentsColumnType(rng)
if inner.SemanticType == ColumnType_COLLATEDSTRING {
// TODO(justin): change this when collated arrays are supported.
s := ColumnType_STRING
typ.ArrayContents = &s
inner.SemanticType = ColumnType_STRING
}
typ.ArrayContents = &inner.SemanticType
}
if typ.SemanticType == ColumnType_TUPLE {
// Generate tuples between 0 and 4 datums in length
Expand Down

0 comments on commit 7d0deef

Please sign in to comment.