github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/internal/sqlsmith/type.go (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package sqlsmith 12 13 import ( 14 "github.com/cockroachdb/cockroach/pkg/sql/parser" 15 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 16 "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" 17 "github.com/cockroachdb/cockroach/pkg/sql/types" 18 "github.com/cockroachdb/errors" 19 ) 20 21 func typeFromName(name string) *types.T { 22 typRef, err := parser.ParseType(name) 23 if err != nil { 24 panic(errors.AssertionFailedf("failed to parse type: %v", name)) 25 } 26 return tree.MustBeStaticallyKnownType(typRef) 27 } 28 29 // pickAnyType returns a concrete type if typ is types.Any or types.AnyArray, 30 // otherwise typ. 31 func (s *Smither) pickAnyType(typ *types.T) *types.T { 32 switch typ.Family() { 33 case types.AnyFamily: 34 typ = s.randType() 35 case types.ArrayFamily: 36 if typ.ArrayContents().Family() == types.AnyFamily { 37 typ = sqlbase.RandArrayContentsType(s.rnd) 38 } 39 } 40 return typ 41 } 42 43 func (s *Smither) randScalarType() *types.T { 44 return sqlbase.RandScalarType(s.rnd) 45 } 46 47 func (s *Smither) randType() *types.T { 48 return sqlbase.RandType(s.rnd) 49 } 50 51 func (s *Smither) makeDesiredTypes() []*types.T { 52 var typs []*types.T 53 for { 54 typs = append(typs, s.randType()) 55 if s.d6() < 2 || !s.canRecurse() { 56 break 57 } 58 } 59 return typs 60 }