github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/col/typeconv/typeconv.go (about) 1 // Copyright 2020 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 typeconv 12 13 import ( 14 "fmt" 15 "time" 16 17 "github.com/cockroachdb/apd/v3" 18 "github.com/cockroachdb/cockroachdb-parser/pkg/sql/types" 19 "github.com/cockroachdb/cockroachdb-parser/pkg/util/duration" 20 "github.com/cockroachdb/cockroachdb-parser/pkg/util/json" 21 ) 22 23 // DatumVecCanonicalTypeFamily is the "canonical" type family of all types that 24 // are physically represented by coldata.DatumVec. 25 var DatumVecCanonicalTypeFamily = types.Family(1000000) 26 27 // TypeFamilyToCanonicalTypeFamily converts all type families to their 28 // "canonical" counterparts. "Canonical" type families are representatives 29 // from a set of "equivalent" type families where "equivalence" means having 30 // the same physical representation. 31 // 32 // All type families that do not have an optimized physical representation are 33 // handled by using tree.Datums, and such types are mapped to 34 // DatumVecCanonicalTypeFamily. 35 func TypeFamilyToCanonicalTypeFamily(family types.Family) types.Family { 36 switch family { 37 case types.BoolFamily: 38 return types.BoolFamily 39 case types.BytesFamily, types.StringFamily, types.UuidFamily, types.EncodedKeyFamily, types.EnumFamily: 40 // Note that by using Bytes family as the canonical one for other type 41 // families we allow the execution engine to evaluate invalid operations 42 // (e.g. the concat binary operation between a UUID and an enum "has" 43 // the execution engine support). However, it's not a big deal since the 44 // type-checking for validity of operations is done before the query 45 // reaches the execution engine. 46 return types.BytesFamily 47 case types.DecimalFamily: 48 return types.DecimalFamily 49 case types.JsonFamily: 50 return types.JsonFamily 51 case types.IntFamily, types.DateFamily: 52 return types.IntFamily 53 case types.FloatFamily: 54 return types.FloatFamily 55 case types.TimestampTZFamily, types.TimestampFamily: 56 return types.TimestampTZFamily 57 case types.IntervalFamily: 58 return types.IntervalFamily 59 default: 60 // TODO(yuzefovich): consider adding native support for 61 // types.UnknownFamily. 62 return DatumVecCanonicalTypeFamily 63 } 64 } 65 66 // ToCanonicalTypeFamilies converts typs to the corresponding canonical type 67 // families. 68 func ToCanonicalTypeFamilies(typs []*types.T) []types.Family { 69 families := make([]types.Family, len(typs)) 70 for i := range typs { 71 families[i] = TypeFamilyToCanonicalTypeFamily(typs[i].Family()) 72 } 73 return families 74 } 75 76 // UnsafeFromGoType returns the type for a Go value, if applicable. Shouldn't 77 // be used at runtime. This method is unsafe because multiple logical types can 78 // be represented by the same physical type. Types that are backed by DatumVec 79 // are *not* supported by this function. 80 func UnsafeFromGoType(v interface{}) *types.T { 81 switch t := v.(type) { 82 case int16: 83 return types.Int2 84 case int32: 85 return types.Int4 86 case int, int64: 87 return types.Int 88 case bool: 89 return types.Bool 90 case float64: 91 return types.Float 92 case []byte: 93 return types.Bytes 94 case string: 95 return types.String 96 case apd.Decimal: 97 return types.Decimal 98 case time.Time: 99 return types.TimestampTZ 100 case duration.Duration: 101 return types.Interval 102 case json.JSON: 103 return types.Jsonb 104 default: 105 panic(fmt.Sprintf("type %s not supported yet", t)) 106 } 107 } 108 109 // TypesSupportedNatively contains types that are supported natively by the 110 // vectorized engine. 111 var TypesSupportedNatively []*types.T 112 113 func init() { 114 for _, t := range types.Scalar { 115 if TypeFamilyToCanonicalTypeFamily(t.Family()) == DatumVecCanonicalTypeFamily { 116 continue 117 } 118 if t.Family() == types.IntFamily { 119 TypesSupportedNatively = append(TypesSupportedNatively, types.Int2) 120 TypesSupportedNatively = append(TypesSupportedNatively, types.Int4) 121 } 122 TypesSupportedNatively = append(TypesSupportedNatively, t) 123 } 124 }