github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/colencoding/value_encoding.go (about)

     1  // Copyright 2018 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 colencoding
    12  
    13  import (
    14  	"time"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/col/coldata"
    17  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/types"
    20  	"github.com/cockroachdb/cockroach/pkg/util/duration"
    21  	"github.com/cockroachdb/cockroach/pkg/util/encoding"
    22  	"github.com/cockroachdb/cockroach/pkg/util/uuid"
    23  )
    24  
    25  // DecodeTableValueToCol decodes a value encoded by EncodeTableValue, writing
    26  // the result to the idx'th position of the input exec.Vec.
    27  // See the analog in sqlbase/column_type_encoding.go.
    28  func DecodeTableValueToCol(
    29  	da *sqlbase.DatumAlloc,
    30  	vec coldata.Vec,
    31  	idx int,
    32  	typ encoding.Type,
    33  	dataOffset int,
    34  	valTyp *types.T,
    35  	b []byte,
    36  ) ([]byte, error) {
    37  	// NULL is special because it is a valid value for any type.
    38  	if typ == encoding.Null {
    39  		vec.Nulls().SetNull(idx)
    40  		return b[dataOffset:], nil
    41  	}
    42  	// Bool is special because the value is stored in the value tag.
    43  	if valTyp.Family() != types.BoolFamily {
    44  		b = b[dataOffset:]
    45  	}
    46  	return decodeUntaggedDatumToCol(da, vec, idx, valTyp, b)
    47  }
    48  
    49  // decodeUntaggedDatum is used to decode a Datum whose type is known,
    50  // and which doesn't have a value tag (either due to it having been
    51  // consumed already or not having one in the first place). It writes the result
    52  // to the idx'th position of the input coldata.Vec.
    53  //
    54  // This is used to decode datums encoded using value encoding.
    55  //
    56  // If t is types.Bool, the value tag must be present, as its value is encoded in
    57  // the tag directly.
    58  // See the analog in sqlbase/column_type_encoding.go.
    59  func decodeUntaggedDatumToCol(
    60  	da *sqlbase.DatumAlloc, vec coldata.Vec, idx int, t *types.T, buf []byte,
    61  ) ([]byte, error) {
    62  	var err error
    63  	switch t.Family() {
    64  	case types.BoolFamily:
    65  		var b bool
    66  		// A boolean's value is encoded in its tag directly, so we don't have an
    67  		// "Untagged" version of this function.
    68  		buf, b, err = encoding.DecodeBoolValue(buf)
    69  		vec.Bool()[idx] = b
    70  	case types.BytesFamily, types.StringFamily:
    71  		var data []byte
    72  		buf, data, err = encoding.DecodeUntaggedBytesValue(buf)
    73  		vec.Bytes().Set(idx, data)
    74  	case types.DateFamily, types.OidFamily:
    75  		var i int64
    76  		buf, i, err = encoding.DecodeUntaggedIntValue(buf)
    77  		vec.Int64()[idx] = i
    78  	case types.DecimalFamily:
    79  		buf, err = encoding.DecodeIntoUntaggedDecimalValue(&vec.Decimal()[idx], buf)
    80  	case types.FloatFamily:
    81  		var f float64
    82  		buf, f, err = encoding.DecodeUntaggedFloatValue(buf)
    83  		vec.Float64()[idx] = f
    84  	case types.IntFamily:
    85  		var i int64
    86  		buf, i, err = encoding.DecodeUntaggedIntValue(buf)
    87  		switch t.Width() {
    88  		case 16:
    89  			vec.Int16()[idx] = int16(i)
    90  		case 32:
    91  			vec.Int32()[idx] = int32(i)
    92  		default:
    93  			// Pre-2.1 BIT was using INT encoding with arbitrary sizes.
    94  			// We map these to 64-bit INT now. See #34161.
    95  			vec.Int64()[idx] = i
    96  		}
    97  	case types.UuidFamily:
    98  		var data uuid.UUID
    99  		buf, data, err = encoding.DecodeUntaggedUUIDValue(buf)
   100  		// TODO(yuzefovich): we could peek inside the encoding package to skip a
   101  		// couple of conversions.
   102  		if err != nil {
   103  			return buf, err
   104  		}
   105  		vec.Bytes().Set(idx, data.GetBytes())
   106  	case types.TimestampFamily, types.TimestampTZFamily:
   107  		var t time.Time
   108  		buf, t, err = encoding.DecodeUntaggedTimeValue(buf)
   109  		vec.Timestamp()[idx] = t
   110  	case types.IntervalFamily:
   111  		var d duration.Duration
   112  		buf, d, err = encoding.DecodeUntaggedDurationValue(buf)
   113  		vec.Interval()[idx] = d
   114  	// Types backed by tree.Datums.
   115  	default:
   116  		var d tree.Datum
   117  		d, buf, err = sqlbase.DecodeUntaggedDatum(da, t, buf)
   118  		if err != nil {
   119  			return buf, err
   120  		}
   121  		vec.Datum().Set(idx, d)
   122  	}
   123  	return buf, err
   124  }