github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/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"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/types"
    19  	"github.com/cockroachdb/cockroach/pkg/util/duration"
    20  )
    21  
    22  // DatumVecCanonicalTypeFamily is the "canonical" type family of all types that
    23  // are physically represented by coldata.DatumVec.
    24  var DatumVecCanonicalTypeFamily = types.Family(1000000)
    25  
    26  // TypeFamilyToCanonicalTypeFamily converts all type families to their
    27  // "canonical" counterparts. "Canonical" type families are representatives
    28  // from a set of "equivalent" type families where "equivalence" means having
    29  // the same physical representation.
    30  //
    31  // All type families that do not have an optimized physical representation are
    32  // handled by using tree.Datums, and such types are mapped to
    33  // DatumVecCanonicalTypeFamily.
    34  func TypeFamilyToCanonicalTypeFamily(family types.Family) types.Family {
    35  	switch family {
    36  	case types.BoolFamily:
    37  		return types.BoolFamily
    38  	case types.BytesFamily, types.StringFamily, types.UuidFamily:
    39  		return types.BytesFamily
    40  	case types.DecimalFamily:
    41  		return types.DecimalFamily
    42  	case types.IntFamily, types.DateFamily, types.OidFamily:
    43  		return types.IntFamily
    44  	case types.FloatFamily:
    45  		return types.FloatFamily
    46  	case types.TimestampTZFamily, types.TimestampFamily:
    47  		return types.TimestampTZFamily
    48  	case types.IntervalFamily:
    49  		return types.IntervalFamily
    50  	default:
    51  		// TODO(yuzefovich): consider adding native support for
    52  		// types.UnknownFamily.
    53  		return DatumVecCanonicalTypeFamily
    54  	}
    55  }
    56  
    57  // ToCanonicalTypeFamilies converts typs to the corresponding canonical type
    58  // families.
    59  func ToCanonicalTypeFamilies(typs []*types.T) []types.Family {
    60  	families := make([]types.Family, len(typs))
    61  	for i := range typs {
    62  		families[i] = TypeFamilyToCanonicalTypeFamily(typs[i].Family())
    63  	}
    64  	return families
    65  }
    66  
    67  // UnsafeFromGoType returns the type for a Go value, if applicable. Shouldn't
    68  // be used at runtime. This method is unsafe because multiple logical types can
    69  // be represented by the same physical type. Types that are backed by DatumVec
    70  // are *not* supported by this function.
    71  func UnsafeFromGoType(v interface{}) *types.T {
    72  	switch t := v.(type) {
    73  	case int16:
    74  		return types.Int2
    75  	case int32:
    76  		return types.Int4
    77  	case int, int64:
    78  		return types.Int
    79  	case bool:
    80  		return types.Bool
    81  	case float64:
    82  		return types.Float
    83  	case []byte:
    84  		return types.Bytes
    85  	case string:
    86  		return types.String
    87  	case apd.Decimal:
    88  		return types.Decimal
    89  	case time.Time:
    90  		return types.TimestampTZ
    91  	case duration.Duration:
    92  		return types.Interval
    93  	default:
    94  		panic(fmt.Sprintf("type %s not supported yet", t))
    95  	}
    96  }