github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/value/value.go (about)

     1  package value
     2  
     3  import (
     4  	"encoding/binary"
     5  	"fmt"
     6  	"math/big"
     7  	"reflect"
     8  	"sort"
     9  	"strconv"
    10  	"time"
    11  
    12  	"github.com/google/uuid"
    13  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
    14  
    15  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
    16  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/decimal"
    17  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
    18  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
    19  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
    20  )
    21  
    22  type Value interface {
    23  	Type() types.Type
    24  	Yql() string
    25  
    26  	castTo(dst interface{}) error
    27  	toYDB(a *allocator.Allocator) *Ydb.Value
    28  }
    29  
    30  func ToYDB(v Value, a *allocator.Allocator) *Ydb.TypedValue {
    31  	tv := a.TypedValue()
    32  
    33  	tv.Type = v.Type().ToYDB(a)
    34  	tv.Value = v.toYDB(a)
    35  
    36  	return tv
    37  }
    38  
    39  // BigEndianUint128 builds a big-endian uint128 value.
    40  func BigEndianUint128(hi, lo uint64) (v [16]byte) {
    41  	binary.BigEndian.PutUint64(v[0:8], hi)
    42  	binary.BigEndian.PutUint64(v[8:16], lo)
    43  
    44  	return v
    45  }
    46  
    47  func FromYDB(t *Ydb.Type, v *Ydb.Value) Value {
    48  	vv, err := fromYDB(t, v)
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  
    53  	return vv
    54  }
    55  
    56  func nullValueFromYDB(x *Ydb.Value, t types.Type) (_ Value, ok bool) {
    57  	for {
    58  		switch xx := x.GetValue().(type) {
    59  		case *Ydb.Value_NestedValue:
    60  			x = xx.NestedValue
    61  		case *Ydb.Value_NullFlagValue:
    62  			switch tt := t.(type) {
    63  			case types.Optional:
    64  				return NullValue(tt.InnerType()), true
    65  			case types.Void:
    66  				return VoidValue(), true
    67  			default:
    68  				return nil, false
    69  			}
    70  		default:
    71  			return nil, false
    72  		}
    73  	}
    74  }
    75  
    76  func primitiveValueFromYDB(t types.Primitive, v *Ydb.Value) (Value, error) {
    77  	switch t {
    78  	case types.Bool:
    79  		return BoolValue(v.GetBoolValue()), nil
    80  
    81  	case types.Int8:
    82  		return Int8Value(int8(v.GetInt32Value())), nil
    83  
    84  	case types.Int16:
    85  		return Int16Value(int16(v.GetInt32Value())), nil
    86  
    87  	case types.Int32:
    88  		return Int32Value(v.GetInt32Value()), nil
    89  
    90  	case types.Int64:
    91  		return Int64Value(v.GetInt64Value()), nil
    92  
    93  	case types.Uint8:
    94  		return Uint8Value(uint8(v.GetUint32Value())), nil
    95  
    96  	case types.Uint16:
    97  		return Uint16Value(uint16(v.GetUint32Value())), nil
    98  
    99  	case types.Uint32:
   100  		return Uint32Value(v.GetUint32Value()), nil
   101  
   102  	case types.Uint64:
   103  		return Uint64Value(v.GetUint64Value()), nil
   104  
   105  	case types.Date:
   106  		return DateValue(v.GetUint32Value()), nil
   107  
   108  	case types.Datetime:
   109  		return DatetimeValue(v.GetUint32Value()), nil
   110  
   111  	case types.Interval:
   112  		return IntervalValue(v.GetInt64Value()), nil
   113  
   114  	case types.Timestamp:
   115  		return TimestampValue(v.GetUint64Value()), nil
   116  
   117  	case types.Float:
   118  		return FloatValue(v.GetFloatValue()), nil
   119  
   120  	case types.Double:
   121  		return DoubleValue(v.GetDoubleValue()), nil
   122  
   123  	case types.Text:
   124  		return TextValue(v.GetTextValue()), nil
   125  
   126  	case types.YSON:
   127  		switch vv := v.GetValue().(type) {
   128  		case *Ydb.Value_TextValue:
   129  			return YSONValue(xstring.ToBytes(vv.TextValue)), nil
   130  		case *Ydb.Value_BytesValue:
   131  			return YSONValue(vv.BytesValue), nil
   132  		default:
   133  			return nil, xerrors.WithStackTrace(fmt.Errorf("uncovered YSON internal type: %T", vv))
   134  		}
   135  
   136  	case types.JSON:
   137  		return JSONValue(v.GetTextValue()), nil
   138  
   139  	case types.JSONDocument:
   140  		return JSONDocumentValue(v.GetTextValue()), nil
   141  
   142  	case types.DyNumber:
   143  		return DyNumberValue(v.GetTextValue()), nil
   144  
   145  	case types.TzDate:
   146  		return TzDateValue(v.GetTextValue()), nil
   147  
   148  	case types.TzDatetime:
   149  		return TzDatetimeValue(v.GetTextValue()), nil
   150  
   151  	case types.TzTimestamp:
   152  		return TzTimestampValue(v.GetTextValue()), nil
   153  
   154  	case types.Bytes:
   155  		return BytesValue(v.GetBytesValue()), nil
   156  
   157  	case types.UUID:
   158  		return UUIDValue(BigEndianUint128(v.GetHigh_128(), v.GetLow_128())), nil
   159  
   160  	default:
   161  		return nil, xerrors.WithStackTrace(fmt.Errorf("uncovered primitive type: %T", t))
   162  	}
   163  }
   164  
   165  func fromYDB(t *Ydb.Type, v *Ydb.Value) (Value, error) {
   166  	tt := types.TypeFromYDB(t)
   167  
   168  	if vv, ok := nullValueFromYDB(v, tt); ok {
   169  		return vv, nil
   170  	}
   171  
   172  	switch ttt := tt.(type) {
   173  	case types.Primitive:
   174  		return primitiveValueFromYDB(ttt, v)
   175  
   176  	case types.Void:
   177  		return VoidValue(), nil
   178  
   179  	case types.Null:
   180  		return NullValue(tt), nil
   181  
   182  	case *types.Decimal:
   183  		return DecimalValue(BigEndianUint128(v.GetHigh_128(), v.GetLow_128()), ttt.Precision(), ttt.Scale()), nil
   184  
   185  	case types.Optional:
   186  		t = t.GetType().(*Ydb.Type_OptionalType).OptionalType.GetItem()
   187  		if nestedValue, ok := v.GetValue().(*Ydb.Value_NestedValue); ok {
   188  			return OptionalValue(FromYDB(t, nestedValue.NestedValue)), nil
   189  		}
   190  
   191  		return OptionalValue(FromYDB(t, v)), nil
   192  
   193  	case *types.List:
   194  		return ListValue(func() []Value {
   195  			vv := make([]Value, len(v.GetItems()))
   196  			a := allocator.New()
   197  			defer a.Free()
   198  			for i, vvv := range v.GetItems() {
   199  				vv[i] = FromYDB(ttt.ItemType().ToYDB(a), vvv)
   200  			}
   201  
   202  			return vv
   203  		}()...), nil
   204  
   205  	case *types.Tuple:
   206  		return TupleValue(func() []Value {
   207  			vv := make([]Value, len(v.GetItems()))
   208  			a := allocator.New()
   209  			defer a.Free()
   210  			for i, vvv := range v.GetItems() {
   211  				vv[i] = FromYDB(ttt.ItemType(i).ToYDB(a), vvv)
   212  			}
   213  
   214  			return vv
   215  		}()...), nil
   216  
   217  	case *types.Struct:
   218  		return StructValue(func() []StructValueField {
   219  			vv := make([]StructValueField, len(v.GetItems()))
   220  			a := allocator.New()
   221  			defer a.Free()
   222  			for i, vvv := range v.GetItems() {
   223  				vv[i] = StructValueField{
   224  					Name: ttt.Field(i).Name,
   225  					V:    FromYDB(ttt.Field(i).T.ToYDB(a), vvv),
   226  				}
   227  			}
   228  
   229  			return vv
   230  		}()...), nil
   231  
   232  	case *types.Dict:
   233  		return DictValue(func() []DictValueField {
   234  			vv := make([]DictValueField, len(v.GetPairs()))
   235  			a := allocator.New()
   236  			defer a.Free()
   237  			for i, vvv := range v.GetPairs() {
   238  				vv[i] = DictValueField{
   239  					K: FromYDB(ttt.KeyType().ToYDB(a), vvv.GetKey()),
   240  					V: FromYDB(ttt.ValueType().ToYDB(a), vvv.GetPayload()),
   241  				}
   242  			}
   243  
   244  			return vv
   245  		}()...), nil
   246  
   247  	case *types.Set:
   248  		return SetValue(func() []Value {
   249  			vv := make([]Value, len(v.GetPairs()))
   250  			a := allocator.New()
   251  			defer a.Free()
   252  			for i, vvv := range v.GetPairs() {
   253  				vv[i] = FromYDB(ttt.ItemType().ToYDB(a), vvv.GetKey())
   254  			}
   255  
   256  			return vv
   257  		}()...), nil
   258  
   259  	case *types.VariantStruct:
   260  		a := allocator.New()
   261  		defer a.Free()
   262  
   263  		return VariantValueStruct(
   264  			FromYDB(
   265  				ttt.Struct.Field(int(v.GetVariantIndex())).T.ToYDB(a),
   266  				v.GetValue().(*Ydb.Value_NestedValue).NestedValue,
   267  			),
   268  			ttt.Struct.Field(int(v.GetVariantIndex())).Name,
   269  			ttt.Struct,
   270  		), nil
   271  
   272  	case *types.VariantTuple:
   273  		a := allocator.New()
   274  		defer a.Free()
   275  
   276  		return VariantValueTuple(
   277  			FromYDB(
   278  				ttt.Tuple.ItemType(int(v.GetVariantIndex())).ToYDB(a),
   279  				v.GetValue().(*Ydb.Value_NestedValue).NestedValue,
   280  			),
   281  			v.GetVariantIndex(),
   282  			ttt.Tuple,
   283  		), nil
   284  
   285  	default:
   286  		return nil, xerrors.WithStackTrace(fmt.Errorf("uncovered type: %T", ttt))
   287  	}
   288  }
   289  
   290  type boolValue bool
   291  
   292  func (v boolValue) castTo(dst interface{}) error {
   293  	switch vv := dst.(type) {
   294  	case *bool:
   295  		*vv = bool(v)
   296  
   297  		return nil
   298  	case *string:
   299  		*vv = strconv.FormatBool(bool(v))
   300  
   301  		return nil
   302  	default:
   303  		return xerrors.WithStackTrace(fmt.Errorf(
   304  			"%w '%+v' (type '%s') to '%T' destination",
   305  			ErrCannotCast, v, v.Type().Yql(), vv,
   306  		))
   307  	}
   308  }
   309  
   310  func (v boolValue) Yql() string {
   311  	return strconv.FormatBool(bool(v))
   312  }
   313  
   314  func (boolValue) Type() types.Type {
   315  	return types.Bool
   316  }
   317  
   318  func (v boolValue) toYDB(a *allocator.Allocator) *Ydb.Value {
   319  	vv := a.Bool()
   320  
   321  	vv.BoolValue = bool(v)
   322  
   323  	vvv := a.Value()
   324  	vvv.Value = vv
   325  
   326  	return vvv
   327  }
   328  
   329  func BoolValue(v bool) boolValue {
   330  	return boolValue(v)
   331  }
   332  
   333  type dateValue uint32
   334  
   335  func (v dateValue) castTo(dst interface{}) error {
   336  	switch vv := dst.(type) {
   337  	case *time.Time:
   338  		*vv = DateToTime(uint32(v))
   339  
   340  		return nil
   341  	case *uint64:
   342  		*vv = uint64(v)
   343  
   344  		return nil
   345  	case *int64:
   346  		*vv = int64(v)
   347  
   348  		return nil
   349  	case *int32:
   350  		*vv = int32(v)
   351  
   352  		return nil
   353  	default:
   354  		return xerrors.WithStackTrace(fmt.Errorf(
   355  			"%w '%+v' (type '%s') to '%T' destination",
   356  			ErrCannotCast, v, v.Type().Yql(), vv,
   357  		))
   358  	}
   359  }
   360  
   361  func (v dateValue) Yql() string {
   362  	return fmt.Sprintf("%s(%q)", v.Type().Yql(), DateToTime(uint32(v)).UTC().Format(LayoutDate))
   363  }
   364  
   365  func (dateValue) Type() types.Type {
   366  	return types.Date
   367  }
   368  
   369  func (v dateValue) toYDB(a *allocator.Allocator) *Ydb.Value {
   370  	vv := a.Uint32()
   371  
   372  	vv.Uint32Value = uint32(v)
   373  
   374  	vvv := a.Value()
   375  	vvv.Value = vv
   376  
   377  	return vvv
   378  }
   379  
   380  // DateValue returns ydb date value by given days since Epoch
   381  func DateValue(v uint32) dateValue {
   382  	return dateValue(v)
   383  }
   384  
   385  func DateValueFromTime(t time.Time) dateValue {
   386  	return dateValue(uint64(t.Sub(epoch)/time.Second) / secondsPerDay)
   387  }
   388  
   389  type datetimeValue uint32
   390  
   391  func (v datetimeValue) castTo(dst interface{}) error {
   392  	switch vv := dst.(type) {
   393  	case *time.Time:
   394  		*vv = DatetimeToTime(uint32(v))
   395  
   396  		return nil
   397  	case *uint64:
   398  		*vv = uint64(v)
   399  
   400  		return nil
   401  	case *int64:
   402  		*vv = int64(v)
   403  
   404  		return nil
   405  	case *uint32:
   406  		*vv = uint32(v)
   407  
   408  		return nil
   409  	default:
   410  		return xerrors.WithStackTrace(fmt.Errorf(
   411  			"%w '%+v' (type '%s') to '%T' destination",
   412  			ErrCannotCast, v, v.Type().Yql(), vv,
   413  		))
   414  	}
   415  }
   416  
   417  func (v datetimeValue) Yql() string {
   418  	return fmt.Sprintf("%s(%q)", v.Type().Yql(), DatetimeToTime(uint32(v)).UTC().Format(LayoutDatetime))
   419  }
   420  
   421  func (datetimeValue) Type() types.Type {
   422  	return types.Datetime
   423  }
   424  
   425  func (v datetimeValue) toYDB(a *allocator.Allocator) *Ydb.Value {
   426  	vv := a.Uint32()
   427  	vv.Uint32Value = uint32(v)
   428  
   429  	vvv := a.Value()
   430  	vvv.Value = vv
   431  
   432  	return vvv
   433  }
   434  
   435  // DatetimeValue makes ydb datetime value from seconds since Epoch
   436  func DatetimeValue(v uint32) datetimeValue {
   437  	return datetimeValue(v)
   438  }
   439  
   440  func DatetimeValueFromTime(t time.Time) datetimeValue {
   441  	return datetimeValue(t.Unix())
   442  }
   443  
   444  var _ DecimalValuer = (*decimalValue)(nil)
   445  
   446  type decimalValue struct {
   447  	value     [16]byte
   448  	innerType *types.Decimal
   449  }
   450  
   451  func (v *decimalValue) Value() [16]byte {
   452  	return v.value
   453  }
   454  
   455  func (v *decimalValue) Precision() uint32 {
   456  	return v.innerType.Precision()
   457  }
   458  
   459  func (v *decimalValue) Scale() uint32 {
   460  	return v.innerType.Scale()
   461  }
   462  
   463  type DecimalValuer interface {
   464  	Value() [16]byte
   465  	Precision() uint32
   466  	Scale() uint32
   467  }
   468  
   469  func (v *decimalValue) castTo(dst interface{}) error {
   470  	return xerrors.WithStackTrace(fmt.Errorf(
   471  		"%w '%+v' to '%T' destination",
   472  		ErrCannotCast, v, dst,
   473  	))
   474  }
   475  
   476  func (v *decimalValue) Yql() string {
   477  	buffer := xstring.Buffer()
   478  	defer buffer.Free()
   479  	buffer.WriteString(v.innerType.Name())
   480  	buffer.WriteByte('(')
   481  	buffer.WriteByte('"')
   482  	s := decimal.FromBytes(v.value[:], v.innerType.Precision(), v.innerType.Scale()).String()
   483  	buffer.WriteString(s[:len(s)-int(v.innerType.Scale())] + "." + s[len(s)-int(v.innerType.Scale()):])
   484  	buffer.WriteByte('"')
   485  	buffer.WriteByte(',')
   486  	buffer.WriteString(strconv.FormatUint(uint64(v.innerType.Precision()), 10))
   487  	buffer.WriteByte(',')
   488  	buffer.WriteString(strconv.FormatUint(uint64(v.innerType.Scale()), 10))
   489  	buffer.WriteByte(')')
   490  
   491  	return buffer.String()
   492  }
   493  
   494  func (v *decimalValue) Type() types.Type {
   495  	return v.innerType
   496  }
   497  
   498  func (v *decimalValue) toYDB(a *allocator.Allocator) *Ydb.Value {
   499  	var bytes [16]byte
   500  	if v != nil {
   501  		bytes = v.value
   502  	}
   503  	vv := a.Low128()
   504  	vv.Low_128 = binary.BigEndian.Uint64(bytes[8:16])
   505  
   506  	vvv := a.Value()
   507  	vvv.High_128 = binary.BigEndian.Uint64(bytes[0:8])
   508  	vvv.Value = vv
   509  
   510  	return vvv
   511  }
   512  
   513  func DecimalValueFromBigInt(v *big.Int, precision, scale uint32) *decimalValue {
   514  	b := decimal.BigIntToByte(v, precision, scale)
   515  
   516  	return DecimalValue(b, precision, scale)
   517  }
   518  
   519  func DecimalValue(v [16]byte, precision, scale uint32) *decimalValue {
   520  	return &decimalValue{
   521  		value: v,
   522  		innerType: types.NewDecimal(
   523  			precision,
   524  			scale,
   525  		),
   526  	}
   527  }
   528  
   529  type (
   530  	DictValueField struct {
   531  		K Value
   532  		V Value
   533  	}
   534  	dictValue struct {
   535  		t      types.Type
   536  		values []DictValueField
   537  	}
   538  )
   539  
   540  func (v *dictValue) DictValues() map[Value]Value {
   541  	values := make(map[Value]Value, len(v.values))
   542  	for i := range v.values {
   543  		values[v.values[i].K] = v.values[i].V
   544  	}
   545  
   546  	return values
   547  }
   548  
   549  func (v *dictValue) castTo(dst interface{}) error {
   550  	return xerrors.WithStackTrace(fmt.Errorf(
   551  		"%w '%+v' to '%T' destination",
   552  		ErrCannotCast, v, dst,
   553  	))
   554  }
   555  
   556  func (v *dictValue) Yql() string {
   557  	buffer := xstring.Buffer()
   558  	defer buffer.Free()
   559  	buffer.WriteByte('{')
   560  	for i := range v.values {
   561  		if i != 0 {
   562  			buffer.WriteByte(',')
   563  		}
   564  		buffer.WriteString(v.values[i].K.Yql())
   565  		buffer.WriteByte(':')
   566  		buffer.WriteString(v.values[i].V.Yql())
   567  	}
   568  	buffer.WriteByte('}')
   569  
   570  	return buffer.String()
   571  }
   572  
   573  func (v *dictValue) Type() types.Type {
   574  	return v.t
   575  }
   576  
   577  func (v *dictValue) toYDB(a *allocator.Allocator) *Ydb.Value {
   578  	var values []DictValueField
   579  	if v != nil {
   580  		values = v.values
   581  	}
   582  	vvv := a.Value()
   583  
   584  	for i := range values {
   585  		pair := a.Pair()
   586  
   587  		pair.Key = values[i].K.toYDB(a)
   588  		pair.Payload = values[i].V.toYDB(a)
   589  
   590  		vvv.Pairs = append(vvv.GetPairs(), pair)
   591  	}
   592  
   593  	return vvv
   594  }
   595  
   596  func DictValue(values ...DictValueField) *dictValue {
   597  	sort.Slice(values, func(i, j int) bool {
   598  		return values[i].K.Yql() < values[j].K.Yql()
   599  	})
   600  	var t types.Type
   601  	switch {
   602  	case len(values) > 0:
   603  		t = types.NewDict(values[0].K.Type(), values[0].V.Type())
   604  	default:
   605  		t = types.NewEmptyDict()
   606  	}
   607  
   608  	return &dictValue{
   609  		t:      t,
   610  		values: values,
   611  	}
   612  }
   613  
   614  type doubleValue struct {
   615  	value float64
   616  }
   617  
   618  func (v *doubleValue) castTo(dst interface{}) error {
   619  	switch vv := dst.(type) {
   620  	case *string:
   621  		*vv = strconv.FormatFloat(v.value, 'f', -1, 64)
   622  
   623  		return nil
   624  	case *[]byte:
   625  		*vv = xstring.ToBytes(strconv.FormatFloat(v.value, 'f', -1, 64))
   626  
   627  		return nil
   628  	case *float64:
   629  		*vv = v.value
   630  
   631  		return nil
   632  	default:
   633  		return xerrors.WithStackTrace(fmt.Errorf(
   634  			"%w '%+v' (type '%s') to '%T' destination",
   635  			ErrCannotCast, v, v.Type().Yql(), vv,
   636  		))
   637  	}
   638  }
   639  
   640  func (v *doubleValue) Yql() string {
   641  	return fmt.Sprintf("%s(\"%v\")", v.Type().Yql(), v.value)
   642  }
   643  
   644  func (*doubleValue) Type() types.Type {
   645  	return types.Double
   646  }
   647  
   648  func (v *doubleValue) toYDB(a *allocator.Allocator) *Ydb.Value {
   649  	vv := a.Double()
   650  	if v != nil {
   651  		vv.DoubleValue = v.value
   652  	}
   653  
   654  	vvv := a.Value()
   655  	vvv.Value = vv
   656  
   657  	return vvv
   658  }
   659  
   660  func DoubleValue(v float64) *doubleValue {
   661  	return &doubleValue{value: v}
   662  }
   663  
   664  type dyNumberValue string
   665  
   666  func (v dyNumberValue) castTo(dst interface{}) error {
   667  	switch vv := dst.(type) {
   668  	case *string:
   669  		*vv = string(v)
   670  
   671  		return nil
   672  	case *[]byte:
   673  		*vv = xstring.ToBytes(string(v))
   674  
   675  		return nil
   676  	default:
   677  		return xerrors.WithStackTrace(fmt.Errorf(
   678  			"%w '%+v' (type '%s') to '%T' destination",
   679  			ErrCannotCast, v, v.Type().Yql(), vv,
   680  		))
   681  	}
   682  }
   683  
   684  func (v dyNumberValue) Yql() string {
   685  	return fmt.Sprintf("%s(%q)", v.Type().Yql(), string(v))
   686  }
   687  
   688  func (dyNumberValue) Type() types.Type {
   689  	return types.DyNumber
   690  }
   691  
   692  func (v dyNumberValue) toYDB(a *allocator.Allocator) *Ydb.Value {
   693  	vv := a.Text()
   694  	vv.TextValue = string(v)
   695  
   696  	vvv := a.Value()
   697  	vvv.Value = vv
   698  
   699  	return vvv
   700  }
   701  
   702  func DyNumberValue(v string) dyNumberValue {
   703  	return dyNumberValue(v)
   704  }
   705  
   706  type floatValue struct {
   707  	value float32
   708  }
   709  
   710  func (v *floatValue) castTo(dst interface{}) error {
   711  	switch vv := dst.(type) {
   712  	case *string:
   713  		*vv = strconv.FormatFloat(float64(v.value), 'f', -1, 32)
   714  
   715  		return nil
   716  	case *[]byte:
   717  		*vv = xstring.ToBytes(strconv.FormatFloat(float64(v.value), 'f', -1, 32))
   718  
   719  		return nil
   720  	case *float64:
   721  		*vv = float64(v.value)
   722  
   723  		return nil
   724  	case *float32:
   725  		*vv = v.value
   726  
   727  		return nil
   728  	default:
   729  		return xerrors.WithStackTrace(fmt.Errorf(
   730  			"%w '%+v' (type '%s') to '%T' destination",
   731  			ErrCannotCast, v, v.Type().Yql(), vv,
   732  		))
   733  	}
   734  }
   735  
   736  func (v *floatValue) Yql() string {
   737  	return fmt.Sprintf("%s(\"%v\")", v.Type().Yql(), v.value)
   738  }
   739  
   740  func (*floatValue) Type() types.Type {
   741  	return types.Float
   742  }
   743  
   744  func (v *floatValue) toYDB(a *allocator.Allocator) *Ydb.Value {
   745  	vv := a.Float()
   746  	if v != nil {
   747  		vv.FloatValue = v.value
   748  	}
   749  
   750  	vvv := a.Value()
   751  	vvv.Value = vv
   752  
   753  	return vvv
   754  }
   755  
   756  func FloatValue(v float32) *floatValue {
   757  	return &floatValue{value: v}
   758  }
   759  
   760  type int8Value int8
   761  
   762  func (v int8Value) castTo(dst interface{}) error {
   763  	switch vv := dst.(type) {
   764  	case *string:
   765  		*vv = strconv.FormatInt(int64(v), 10)
   766  
   767  		return nil
   768  	case *[]byte:
   769  		*vv = xstring.ToBytes(strconv.FormatInt(int64(v), 10))
   770  
   771  		return nil
   772  	case *int64:
   773  		*vv = int64(v)
   774  
   775  		return nil
   776  	case *int32:
   777  		*vv = int32(v)
   778  
   779  		return nil
   780  	case *int16:
   781  		*vv = int16(v)
   782  
   783  		return nil
   784  	case *int8:
   785  		*vv = int8(v)
   786  
   787  		return nil
   788  	case *float64:
   789  		*vv = float64(v)
   790  
   791  		return nil
   792  	case *float32:
   793  		*vv = float32(v)
   794  
   795  		return nil
   796  	default:
   797  		return xerrors.WithStackTrace(fmt.Errorf(
   798  			"%w '%+v' (type '%s') to '%T' destination",
   799  			ErrCannotCast, v, v.Type().Yql(), vv,
   800  		))
   801  	}
   802  }
   803  
   804  func (v int8Value) Yql() string {
   805  	return strconv.FormatUint(uint64(v), 10) + "t"
   806  }
   807  
   808  func (int8Value) Type() types.Type {
   809  	return types.Int8
   810  }
   811  
   812  func (v int8Value) toYDB(a *allocator.Allocator) *Ydb.Value {
   813  	vv := a.Int32()
   814  	vv.Int32Value = int32(v)
   815  
   816  	vvv := a.Value()
   817  	vvv.Value = vv
   818  
   819  	return vvv
   820  }
   821  
   822  func Int8Value(v int8) int8Value {
   823  	return int8Value(v)
   824  }
   825  
   826  type int16Value int16
   827  
   828  func (v int16Value) castTo(dst interface{}) error {
   829  	switch vv := dst.(type) {
   830  	case *string:
   831  		*vv = strconv.FormatInt(int64(v), 10)
   832  
   833  		return nil
   834  	case *[]byte:
   835  		*vv = xstring.ToBytes(strconv.FormatInt(int64(v), 10))
   836  
   837  		return nil
   838  	case *int64:
   839  		*vv = int64(v)
   840  
   841  		return nil
   842  	case *int32:
   843  		*vv = int32(v)
   844  
   845  		return nil
   846  	case *int16:
   847  		*vv = int16(v)
   848  
   849  		return nil
   850  	case *float64:
   851  		*vv = float64(v)
   852  
   853  		return nil
   854  	case *float32:
   855  		*vv = float32(v)
   856  
   857  		return nil
   858  	default:
   859  		return xerrors.WithStackTrace(fmt.Errorf(
   860  			"%w '%+v' (type '%s') to '%T' destination",
   861  			ErrCannotCast, v, v.Type().Yql(), vv,
   862  		))
   863  	}
   864  }
   865  
   866  func (v int16Value) Yql() string {
   867  	return strconv.FormatUint(uint64(v), 10) + "s"
   868  }
   869  
   870  func (int16Value) Type() types.Type {
   871  	return types.Int16
   872  }
   873  
   874  func (v int16Value) toYDB(a *allocator.Allocator) *Ydb.Value {
   875  	vv := a.Int32()
   876  	vv.Int32Value = int32(v)
   877  
   878  	vvv := a.Value()
   879  	vvv.Value = vv
   880  
   881  	return vvv
   882  }
   883  
   884  func Int16Value(v int16) int16Value {
   885  	return int16Value(v)
   886  }
   887  
   888  type int32Value int32
   889  
   890  func (v int32Value) castTo(dst interface{}) error {
   891  	switch vv := dst.(type) {
   892  	case *string:
   893  		*vv = strconv.FormatInt(int64(v), 10)
   894  
   895  		return nil
   896  	case *[]byte:
   897  		*vv = xstring.ToBytes(strconv.FormatInt(int64(v), 10))
   898  
   899  		return nil
   900  	case *int64:
   901  		*vv = int64(v)
   902  
   903  		return nil
   904  	case *int:
   905  		*vv = int(v)
   906  
   907  		return nil
   908  	case *int32:
   909  		*vv = int32(v)
   910  
   911  		return nil
   912  	case *float64:
   913  		*vv = float64(v)
   914  
   915  		return nil
   916  	case *float32:
   917  		*vv = float32(v)
   918  
   919  		return nil
   920  	default:
   921  		return xerrors.WithStackTrace(fmt.Errorf(
   922  			"%w '%+v' (type '%s') to '%T' destination",
   923  			ErrCannotCast, v, v.Type().Yql(), vv,
   924  		))
   925  	}
   926  }
   927  
   928  func (v int32Value) Yql() string {
   929  	return strconv.FormatInt(int64(v), 10)
   930  }
   931  
   932  func (int32Value) Type() types.Type {
   933  	return types.Int32
   934  }
   935  
   936  func (v int32Value) toYDB(a *allocator.Allocator) *Ydb.Value {
   937  	vv := a.Int32()
   938  	vv.Int32Value = int32(v)
   939  
   940  	vvv := a.Value()
   941  	vvv.Value = vv
   942  
   943  	return vvv
   944  }
   945  
   946  func Int32Value(v int32) int32Value {
   947  	return int32Value(v)
   948  }
   949  
   950  type int64Value int64
   951  
   952  func (v int64Value) castTo(dst interface{}) error {
   953  	switch vv := dst.(type) {
   954  	case *string:
   955  		*vv = strconv.FormatInt(int64(v), 10)
   956  
   957  		return nil
   958  	case *[]byte:
   959  		*vv = xstring.ToBytes(strconv.FormatInt(int64(v), 10))
   960  
   961  		return nil
   962  	case *int64:
   963  		*vv = int64(v)
   964  
   965  		return nil
   966  	case *float64:
   967  		*vv = float64(v)
   968  
   969  		return nil
   970  	default:
   971  		return xerrors.WithStackTrace(fmt.Errorf(
   972  			"%w '%+v' (type '%s') to '%T' destination",
   973  			ErrCannotCast, v, v.Type().Yql(), vv,
   974  		))
   975  	}
   976  }
   977  
   978  func (v int64Value) Yql() string {
   979  	return strconv.FormatUint(uint64(v), 10) + "l"
   980  }
   981  
   982  func (int64Value) Type() types.Type {
   983  	return types.Int64
   984  }
   985  
   986  func (v int64Value) toYDB(a *allocator.Allocator) *Ydb.Value {
   987  	vv := a.Int64()
   988  	vv.Int64Value = int64(v)
   989  
   990  	vvv := a.Value()
   991  	vvv.Value = vv
   992  
   993  	return vvv
   994  }
   995  
   996  func Int64Value(v int64) int64Value {
   997  	return int64Value(v)
   998  }
   999  
  1000  type intervalValue int64
  1001  
  1002  func (v intervalValue) castTo(dst interface{}) error {
  1003  	switch vv := dst.(type) {
  1004  	case *time.Duration:
  1005  		*vv = IntervalToDuration(int64(v))
  1006  
  1007  		return nil
  1008  	case *int64:
  1009  		*vv = int64(v)
  1010  
  1011  		return nil
  1012  	default:
  1013  		return xerrors.WithStackTrace(fmt.Errorf(
  1014  			"%w '%+v' (type '%s') to '%T' destination",
  1015  			ErrCannotCast, v, v.Type().Yql(), vv,
  1016  		))
  1017  	}
  1018  }
  1019  
  1020  func (v intervalValue) Yql() string {
  1021  	buffer := xstring.Buffer()
  1022  	defer buffer.Free()
  1023  	buffer.WriteString(v.Type().Yql())
  1024  	buffer.WriteByte('(')
  1025  	buffer.WriteByte('"')
  1026  	d := IntervalToDuration(int64(v))
  1027  	if d < 0 {
  1028  		buffer.WriteByte('-')
  1029  		d = -d
  1030  	}
  1031  	buffer.WriteByte('P')
  1032  	if days := d / time.Hour / 24; days > 0 {
  1033  		d -= days * time.Hour * 24 //nolint:durationcheck
  1034  		buffer.WriteString(strconv.FormatInt(int64(days), 10))
  1035  		buffer.WriteByte('D')
  1036  	}
  1037  	if d > 0 {
  1038  		buffer.WriteByte('T')
  1039  	}
  1040  	if hours := d / time.Hour; hours > 0 {
  1041  		d -= hours * time.Hour //nolint:durationcheck
  1042  		buffer.WriteString(strconv.FormatInt(int64(hours), 10))
  1043  		buffer.WriteByte('H')
  1044  	}
  1045  	if minutes := d / time.Minute; minutes > 0 {
  1046  		d -= minutes * time.Minute //nolint:durationcheck
  1047  		buffer.WriteString(strconv.FormatInt(int64(minutes), 10))
  1048  		buffer.WriteByte('M')
  1049  	}
  1050  	if d > 0 {
  1051  		seconds := float64(d) / float64(time.Second)
  1052  		fmt.Fprintf(buffer, "%0.6f", seconds)
  1053  		buffer.WriteByte('S')
  1054  	}
  1055  	buffer.WriteByte('"')
  1056  	buffer.WriteByte(')')
  1057  
  1058  	return buffer.String()
  1059  }
  1060  
  1061  func (intervalValue) Type() types.Type {
  1062  	return types.Interval
  1063  }
  1064  
  1065  func (v intervalValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  1066  	vv := a.Int64()
  1067  	vv.Int64Value = int64(v)
  1068  
  1069  	vvv := a.Value()
  1070  	vvv.Value = vv
  1071  
  1072  	return vvv
  1073  }
  1074  
  1075  // IntervalValue makes Value from given microseconds value
  1076  func IntervalValue(v int64) intervalValue {
  1077  	return intervalValue(v)
  1078  }
  1079  
  1080  func IntervalValueFromDuration(v time.Duration) intervalValue {
  1081  	return intervalValue(durationToMicroseconds(v))
  1082  }
  1083  
  1084  type jsonValue string
  1085  
  1086  func (v jsonValue) castTo(dst interface{}) error {
  1087  	switch vv := dst.(type) {
  1088  	case *string:
  1089  		*vv = string(v)
  1090  
  1091  		return nil
  1092  	case *[]byte:
  1093  		*vv = xstring.ToBytes(string(v))
  1094  
  1095  		return nil
  1096  	default:
  1097  		return xerrors.WithStackTrace(fmt.Errorf(
  1098  			"%w '%+v' (type '%s') to '%T' destination",
  1099  			ErrCannotCast, v, v.Type().Yql(), vv,
  1100  		))
  1101  	}
  1102  }
  1103  
  1104  func (v jsonValue) Yql() string {
  1105  	return fmt.Sprintf("%s(@@%s@@)", v.Type().Yql(), string(v))
  1106  }
  1107  
  1108  func (jsonValue) Type() types.Type {
  1109  	return types.JSON
  1110  }
  1111  
  1112  func (v jsonValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  1113  	vv := a.Text()
  1114  	vv.TextValue = string(v)
  1115  
  1116  	vvv := a.Value()
  1117  	vvv.Value = vv
  1118  
  1119  	return vvv
  1120  }
  1121  
  1122  func JSONValue(v string) jsonValue {
  1123  	return jsonValue(v)
  1124  }
  1125  
  1126  type jsonDocumentValue string
  1127  
  1128  func (v jsonDocumentValue) castTo(dst interface{}) error {
  1129  	switch vv := dst.(type) {
  1130  	case *string:
  1131  		*vv = string(v)
  1132  
  1133  		return nil
  1134  	case *[]byte:
  1135  		*vv = xstring.ToBytes(string(v))
  1136  
  1137  		return nil
  1138  	default:
  1139  		return xerrors.WithStackTrace(fmt.Errorf(
  1140  			"%w '%+v' (type '%s') to '%T' destination",
  1141  			ErrCannotCast, v, v.Type().Yql(), vv,
  1142  		))
  1143  	}
  1144  }
  1145  
  1146  func (v jsonDocumentValue) Yql() string {
  1147  	return fmt.Sprintf("%s(@@%s@@)", v.Type().Yql(), string(v))
  1148  }
  1149  
  1150  func (jsonDocumentValue) Type() types.Type {
  1151  	return types.JSONDocument
  1152  }
  1153  
  1154  func (v jsonDocumentValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  1155  	vv := a.Text()
  1156  	vv.TextValue = string(v)
  1157  
  1158  	vvv := a.Value()
  1159  	vvv.Value = vv
  1160  
  1161  	return vvv
  1162  }
  1163  
  1164  func JSONDocumentValue(v string) jsonDocumentValue {
  1165  	return jsonDocumentValue(v)
  1166  }
  1167  
  1168  type listValue struct {
  1169  	t     types.Type
  1170  	items []Value
  1171  }
  1172  
  1173  func (v *listValue) ListItems() []Value {
  1174  	return v.items
  1175  }
  1176  
  1177  func (v *listValue) castTo(dst interface{}) error {
  1178  	return xerrors.WithStackTrace(fmt.Errorf(
  1179  		"%w '%+v' (type '%s') to '%T' destination",
  1180  		ErrCannotCast, v, v.Type().Yql(), dst,
  1181  	))
  1182  }
  1183  
  1184  func (v *listValue) Yql() string {
  1185  	buffer := xstring.Buffer()
  1186  	defer buffer.Free()
  1187  	buffer.WriteByte('[')
  1188  	for i, item := range v.items {
  1189  		if i != 0 {
  1190  			buffer.WriteByte(',')
  1191  		}
  1192  		buffer.WriteString(item.Yql())
  1193  	}
  1194  	buffer.WriteByte(']')
  1195  
  1196  	return buffer.String()
  1197  }
  1198  
  1199  func (v *listValue) Type() types.Type {
  1200  	return v.t
  1201  }
  1202  
  1203  func (v *listValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  1204  	var items []Value
  1205  	if v != nil {
  1206  		items = v.items
  1207  	}
  1208  	vvv := a.Value()
  1209  
  1210  	for _, vv := range items {
  1211  		vvv.Items = append(vvv.GetItems(), vv.toYDB(a))
  1212  	}
  1213  
  1214  	return vvv
  1215  }
  1216  
  1217  func ListValue(items ...Value) *listValue {
  1218  	var t types.Type
  1219  	switch {
  1220  	case len(items) > 0:
  1221  		t = types.NewList(items[0].Type())
  1222  	default:
  1223  		t = types.NewEmptyList()
  1224  	}
  1225  
  1226  	return &listValue{
  1227  		t:     t,
  1228  		items: items,
  1229  	}
  1230  }
  1231  
  1232  type setValue struct {
  1233  	t     types.Type
  1234  	items []Value
  1235  }
  1236  
  1237  func (v *setValue) castTo(dst interface{}) error {
  1238  	return xerrors.WithStackTrace(fmt.Errorf(
  1239  		"%w '%+v' to '%T' destination",
  1240  		ErrCannotCast, v, dst,
  1241  	))
  1242  }
  1243  
  1244  func (v *setValue) Yql() string {
  1245  	buffer := xstring.Buffer()
  1246  	defer buffer.Free()
  1247  	buffer.WriteByte('{')
  1248  	for i, item := range v.items {
  1249  		if i != 0 {
  1250  			buffer.WriteByte(',')
  1251  		}
  1252  		buffer.WriteString(item.Yql())
  1253  	}
  1254  	buffer.WriteByte('}')
  1255  
  1256  	return buffer.String()
  1257  }
  1258  
  1259  func (v *setValue) Type() types.Type {
  1260  	return v.t
  1261  }
  1262  
  1263  func (v *setValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  1264  	vvv := a.Value()
  1265  
  1266  	for _, vv := range v.items {
  1267  		pair := a.Pair()
  1268  
  1269  		pair.Key = vv.toYDB(a)
  1270  		pair.Payload = _voidValue
  1271  
  1272  		vvv.Pairs = append(vvv.GetPairs(), pair)
  1273  	}
  1274  
  1275  	return vvv
  1276  }
  1277  
  1278  func SetValue(items ...Value) *setValue {
  1279  	sort.Slice(items, func(i, j int) bool {
  1280  		return items[i].Yql() < items[j].Yql()
  1281  	})
  1282  
  1283  	var t types.Type
  1284  	switch {
  1285  	case len(items) > 0:
  1286  		t = types.NewSet(items[0].Type())
  1287  	default:
  1288  		t = types.EmptySet()
  1289  	}
  1290  
  1291  	return &setValue{
  1292  		t:     t,
  1293  		items: items,
  1294  	}
  1295  }
  1296  
  1297  func NullValue(t types.Type) *optionalValue {
  1298  	return &optionalValue{
  1299  		innerType: types.NewOptional(t),
  1300  		value:     nil,
  1301  	}
  1302  }
  1303  
  1304  type optionalValue struct {
  1305  	innerType types.Type
  1306  	value     Value
  1307  }
  1308  
  1309  func (v *optionalValue) castTo(dst interface{}) error {
  1310  	ptr := reflect.ValueOf(dst)
  1311  	if ptr.Kind() != reflect.Pointer {
  1312  		return xerrors.WithStackTrace(fmt.Errorf("%w: '%s'", errDestinationTypeIsNotAPointer, ptr.Kind().String()))
  1313  	}
  1314  
  1315  	inner := reflect.Indirect(ptr)
  1316  
  1317  	if inner.Kind() != reflect.Pointer {
  1318  		if v.value == nil {
  1319  			if ptr.CanAddr() {
  1320  				ptr.SetZero()
  1321  			}
  1322  
  1323  			return nil
  1324  		}
  1325  
  1326  		if err := v.value.castTo(ptr.Interface()); err != nil {
  1327  			return xerrors.WithStackTrace(err)
  1328  		}
  1329  
  1330  		return nil
  1331  	}
  1332  
  1333  	if v.value == nil {
  1334  		inner.SetZero()
  1335  
  1336  		return nil
  1337  	}
  1338  
  1339  	inner.Set(reflect.New(inner.Type().Elem()))
  1340  
  1341  	if err := v.value.castTo(inner.Interface()); err != nil {
  1342  		return xerrors.WithStackTrace(err)
  1343  	}
  1344  
  1345  	return nil
  1346  }
  1347  
  1348  func (v *optionalValue) Yql() string {
  1349  	if v.value == nil {
  1350  		return fmt.Sprintf("Nothing(%s)", v.Type().Yql())
  1351  	}
  1352  
  1353  	return fmt.Sprintf("Just(%s)", v.value.Yql())
  1354  }
  1355  
  1356  func (v *optionalValue) Type() types.Type {
  1357  	return v.innerType
  1358  }
  1359  
  1360  func (v *optionalValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  1361  	vv := a.Value()
  1362  	if _, opt := v.value.(*optionalValue); opt {
  1363  		vvv := a.Nested()
  1364  		vvv.NestedValue = v.value.toYDB(a)
  1365  		vv.Value = vvv
  1366  	} else {
  1367  		if v.value != nil {
  1368  			vv = v.value.toYDB(a)
  1369  		} else {
  1370  			vv.Value = a.NullFlag()
  1371  		}
  1372  	}
  1373  
  1374  	return vv
  1375  }
  1376  
  1377  func OptionalValue(v Value) *optionalValue {
  1378  	return &optionalValue{
  1379  		innerType: types.NewOptional(v.Type()),
  1380  		value:     v,
  1381  	}
  1382  }
  1383  
  1384  type (
  1385  	StructValueField struct {
  1386  		Name string
  1387  		V    Value
  1388  	}
  1389  	structValue struct {
  1390  		t      types.Type
  1391  		fields []StructValueField
  1392  	}
  1393  )
  1394  
  1395  func (v *structValue) StructFields() map[string]Value {
  1396  	fields := make(map[string]Value, len(v.fields))
  1397  	for i := range v.fields {
  1398  		fields[v.fields[i].Name] = v.fields[i].V
  1399  	}
  1400  
  1401  	return fields
  1402  }
  1403  
  1404  func (v *structValue) castTo(dst interface{}) error {
  1405  	return xerrors.WithStackTrace(fmt.Errorf(
  1406  		"%w '%+v' to '%T' destination",
  1407  		ErrCannotCast, v, dst,
  1408  	))
  1409  }
  1410  
  1411  func (v *structValue) Yql() string {
  1412  	buffer := xstring.Buffer()
  1413  	defer buffer.Free()
  1414  	buffer.WriteString("<|")
  1415  	for i := range v.fields {
  1416  		if i != 0 {
  1417  			buffer.WriteByte(',')
  1418  		}
  1419  		buffer.WriteString("`" + v.fields[i].Name + "`:")
  1420  		buffer.WriteString(v.fields[i].V.Yql())
  1421  	}
  1422  	buffer.WriteString("|>")
  1423  
  1424  	return buffer.String()
  1425  }
  1426  
  1427  func (v *structValue) Type() types.Type {
  1428  	return v.t
  1429  }
  1430  
  1431  func (v *structValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  1432  	vvv := a.Value()
  1433  
  1434  	for i := range v.fields {
  1435  		vvv.Items = append(vvv.GetItems(), v.fields[i].V.toYDB(a))
  1436  	}
  1437  
  1438  	return vvv
  1439  }
  1440  
  1441  func StructValue(fields ...StructValueField) *structValue {
  1442  	sort.Slice(fields, func(i, j int) bool {
  1443  		return fields[i].Name < fields[j].Name
  1444  	})
  1445  	structFields := make([]types.StructField, 0, len(fields))
  1446  	for i := range fields {
  1447  		structFields = append(structFields, types.StructField{
  1448  			Name: fields[i].Name,
  1449  			T:    fields[i].V.Type(),
  1450  		})
  1451  	}
  1452  
  1453  	return &structValue{
  1454  		t:      types.NewStruct(structFields...),
  1455  		fields: fields,
  1456  	}
  1457  }
  1458  
  1459  type timestampValue uint64
  1460  
  1461  func (v timestampValue) castTo(dst interface{}) error {
  1462  	switch vv := dst.(type) {
  1463  	case *time.Time:
  1464  		*vv = TimestampToTime(uint64(v))
  1465  
  1466  		return nil
  1467  	case *uint64:
  1468  		*vv = uint64(v)
  1469  
  1470  		return nil
  1471  	default:
  1472  		return xerrors.WithStackTrace(fmt.Errorf(
  1473  			"%w '%+v' (type '%s') to '%T' destination",
  1474  			ErrCannotCast, v, v.Type().Yql(), vv,
  1475  		))
  1476  	}
  1477  }
  1478  
  1479  func (v timestampValue) Yql() string {
  1480  	return fmt.Sprintf("%s(%q)", v.Type().Yql(), TimestampToTime(uint64(v)).UTC().Format(LayoutTimestamp))
  1481  }
  1482  
  1483  func (timestampValue) Type() types.Type {
  1484  	return types.Timestamp
  1485  }
  1486  
  1487  func (v timestampValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  1488  	vv := a.Uint64()
  1489  	vv.Uint64Value = uint64(v)
  1490  
  1491  	vvv := a.Value()
  1492  	vvv.Value = vv
  1493  
  1494  	return vvv
  1495  }
  1496  
  1497  // TimestampValue makes ydb timestamp value by given microseconds since Epoch
  1498  func TimestampValue(v uint64) timestampValue {
  1499  	return timestampValue(v)
  1500  }
  1501  
  1502  func TimestampValueFromTime(t time.Time) timestampValue {
  1503  	return timestampValue(t.Sub(epoch) / time.Microsecond)
  1504  }
  1505  
  1506  type tupleValue struct {
  1507  	t     types.Type
  1508  	items []Value
  1509  }
  1510  
  1511  func (v *tupleValue) TupleItems() []Value {
  1512  	return v.items
  1513  }
  1514  
  1515  func (v *tupleValue) castTo(dst interface{}) error {
  1516  	if len(v.items) == 1 {
  1517  		return v.items[0].castTo(dst)
  1518  	}
  1519  
  1520  	return xerrors.WithStackTrace(fmt.Errorf(
  1521  		"%w '%+v' to '%T' destination",
  1522  		ErrCannotCast, v, dst,
  1523  	))
  1524  }
  1525  
  1526  func (v *tupleValue) Yql() string {
  1527  	buffer := xstring.Buffer()
  1528  	defer buffer.Free()
  1529  	buffer.WriteByte('(')
  1530  	for i, item := range v.items {
  1531  		if i != 0 {
  1532  			buffer.WriteByte(',')
  1533  		}
  1534  		buffer.WriteString(item.Yql())
  1535  	}
  1536  	buffer.WriteByte(')')
  1537  
  1538  	return buffer.String()
  1539  }
  1540  
  1541  func (v *tupleValue) Type() types.Type {
  1542  	return v.t
  1543  }
  1544  
  1545  func (v *tupleValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  1546  	var items []Value
  1547  	if v != nil {
  1548  		items = v.items
  1549  	}
  1550  	vvv := a.Value()
  1551  
  1552  	for _, vv := range items {
  1553  		vvv.Items = append(vvv.GetItems(), vv.toYDB(a))
  1554  	}
  1555  
  1556  	return vvv
  1557  }
  1558  
  1559  func TupleValue(values ...Value) *tupleValue {
  1560  	tupleItems := make([]types.Type, 0, len(values))
  1561  	for _, v := range values {
  1562  		tupleItems = append(tupleItems, v.Type())
  1563  	}
  1564  
  1565  	return &tupleValue{
  1566  		t:     types.NewTuple(tupleItems...),
  1567  		items: values,
  1568  	}
  1569  }
  1570  
  1571  type tzDateValue string
  1572  
  1573  func (v tzDateValue) castTo(dst interface{}) error {
  1574  	switch vv := dst.(type) {
  1575  	case *string:
  1576  		*vv = string(v)
  1577  
  1578  		return nil
  1579  	case *[]byte:
  1580  		*vv = xstring.ToBytes(string(v))
  1581  
  1582  		return nil
  1583  	default:
  1584  		return xerrors.WithStackTrace(fmt.Errorf(
  1585  			"%w '%+v' (type '%s') to '%T' destination",
  1586  			ErrCannotCast, v, v.Type().Yql(), vv,
  1587  		))
  1588  	}
  1589  }
  1590  
  1591  func (v tzDateValue) Yql() string {
  1592  	return fmt.Sprintf("%s(%q)", v.Type().Yql(), string(v))
  1593  }
  1594  
  1595  func (tzDateValue) Type() types.Type {
  1596  	return types.TzDate
  1597  }
  1598  
  1599  func (v tzDateValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  1600  	vv := a.Text()
  1601  	vv.TextValue = string(v)
  1602  
  1603  	vvv := a.Value()
  1604  	vvv.Value = vv
  1605  
  1606  	return vvv
  1607  }
  1608  
  1609  func TzDateValue(v string) tzDateValue {
  1610  	return tzDateValue(v)
  1611  }
  1612  
  1613  func TzDateValueFromTime(t time.Time) tzDateValue {
  1614  	return tzDateValue(t.Format(LayoutDate))
  1615  }
  1616  
  1617  type tzDatetimeValue string
  1618  
  1619  func (v tzDatetimeValue) castTo(dst interface{}) error {
  1620  	switch vv := dst.(type) {
  1621  	case *string:
  1622  		*vv = string(v)
  1623  
  1624  		return nil
  1625  	case *[]byte:
  1626  		*vv = xstring.ToBytes(string(v))
  1627  
  1628  		return nil
  1629  	default:
  1630  		return xerrors.WithStackTrace(fmt.Errorf(
  1631  			"%w '%+v' (type '%s') to '%T' destination",
  1632  			ErrCannotCast, v, v.Type().Yql(), vv,
  1633  		))
  1634  	}
  1635  }
  1636  
  1637  func (v tzDatetimeValue) Yql() string {
  1638  	return fmt.Sprintf("%s(%q)", v.Type().Yql(), string(v))
  1639  }
  1640  
  1641  func (tzDatetimeValue) Type() types.Type {
  1642  	return types.TzDatetime
  1643  }
  1644  
  1645  func (v tzDatetimeValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  1646  	vv := a.Text()
  1647  	vv.TextValue = string(v)
  1648  
  1649  	vvv := a.Value()
  1650  	vvv.Value = vv
  1651  
  1652  	return vvv
  1653  }
  1654  
  1655  func TzDatetimeValue(v string) tzDatetimeValue {
  1656  	return tzDatetimeValue(v)
  1657  }
  1658  
  1659  func TzDatetimeValueFromTime(t time.Time) tzDatetimeValue {
  1660  	return tzDatetimeValue(t.Format(LayoutDatetime))
  1661  }
  1662  
  1663  type tzTimestampValue string
  1664  
  1665  func (v tzTimestampValue) castTo(dst interface{}) error {
  1666  	switch vv := dst.(type) {
  1667  	case *string:
  1668  		*vv = string(v)
  1669  
  1670  		return nil
  1671  	case *[]byte:
  1672  		*vv = xstring.ToBytes(string(v))
  1673  
  1674  		return nil
  1675  	default:
  1676  		return xerrors.WithStackTrace(fmt.Errorf(
  1677  			"%w '%+v' (type '%s') to '%T' destination",
  1678  			ErrCannotCast, v, v.Type().Yql(), vv,
  1679  		))
  1680  	}
  1681  }
  1682  
  1683  func (v tzTimestampValue) Yql() string {
  1684  	return fmt.Sprintf("%s(%q)", v.Type().Yql(), string(v))
  1685  }
  1686  
  1687  func (tzTimestampValue) Type() types.Type {
  1688  	return types.TzTimestamp
  1689  }
  1690  
  1691  func (v tzTimestampValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  1692  	vv := a.Text()
  1693  	vv.TextValue = string(v)
  1694  
  1695  	vvv := a.Value()
  1696  	vvv.Value = vv
  1697  
  1698  	return vvv
  1699  }
  1700  
  1701  func TzTimestampValue(v string) tzTimestampValue {
  1702  	return tzTimestampValue(v)
  1703  }
  1704  
  1705  func TzTimestampValueFromTime(t time.Time) tzTimestampValue {
  1706  	return tzTimestampValue(t.Format(LayoutTimestamp))
  1707  }
  1708  
  1709  type uint8Value uint8
  1710  
  1711  func (v uint8Value) castTo(dst interface{}) error {
  1712  	switch vv := dst.(type) {
  1713  	case *string:
  1714  		*vv = strconv.FormatInt(int64(v), 10)
  1715  
  1716  		return nil
  1717  	case *[]byte:
  1718  		*vv = xstring.ToBytes(strconv.FormatInt(int64(v), 10))
  1719  
  1720  		return nil
  1721  	case *uint64:
  1722  		*vv = uint64(v)
  1723  
  1724  		return nil
  1725  	case *int64:
  1726  		*vv = int64(v)
  1727  
  1728  		return nil
  1729  	case *uint32:
  1730  		*vv = uint32(v)
  1731  
  1732  		return nil
  1733  	case *int32:
  1734  		*vv = int32(v)
  1735  
  1736  		return nil
  1737  	case *uint16:
  1738  		*vv = uint16(v)
  1739  
  1740  		return nil
  1741  	case *int16:
  1742  		*vv = int16(v)
  1743  
  1744  		return nil
  1745  	case *uint8:
  1746  		*vv = uint8(v)
  1747  
  1748  		return nil
  1749  	case *float64:
  1750  		*vv = float64(v)
  1751  
  1752  		return nil
  1753  	case *float32:
  1754  		*vv = float32(v)
  1755  
  1756  		return nil
  1757  	default:
  1758  		return xerrors.WithStackTrace(fmt.Errorf(
  1759  			"%w '%+v' (type '%s') to '%T' destination",
  1760  			ErrCannotCast, v, v.Type().Yql(), vv,
  1761  		))
  1762  	}
  1763  }
  1764  
  1765  func (v uint8Value) Yql() string {
  1766  	return strconv.FormatUint(uint64(v), 10) + "ut"
  1767  }
  1768  
  1769  func (uint8Value) Type() types.Type {
  1770  	return types.Uint8
  1771  }
  1772  
  1773  func (v uint8Value) toYDB(a *allocator.Allocator) *Ydb.Value {
  1774  	vv := a.Uint32()
  1775  	vv.Uint32Value = uint32(v)
  1776  
  1777  	vvv := a.Value()
  1778  	vvv.Value = vv
  1779  
  1780  	return vvv
  1781  }
  1782  
  1783  func Uint8Value(v uint8) uint8Value {
  1784  	return uint8Value(v)
  1785  }
  1786  
  1787  type uint16Value uint16
  1788  
  1789  func (v uint16Value) castTo(dst interface{}) error {
  1790  	switch vv := dst.(type) {
  1791  	case *string:
  1792  		*vv = strconv.FormatInt(int64(v), 10)
  1793  
  1794  		return nil
  1795  	case *[]byte:
  1796  		*vv = xstring.ToBytes(strconv.FormatInt(int64(v), 10))
  1797  
  1798  		return nil
  1799  	case *uint64:
  1800  		*vv = uint64(v)
  1801  
  1802  		return nil
  1803  	case *int64:
  1804  		*vv = int64(v)
  1805  
  1806  		return nil
  1807  	case *uint32:
  1808  		*vv = uint32(v)
  1809  
  1810  		return nil
  1811  	case *int32:
  1812  		*vv = int32(v)
  1813  
  1814  		return nil
  1815  	case *uint16:
  1816  		*vv = uint16(v)
  1817  
  1818  		return nil
  1819  	case *float32:
  1820  		*vv = float32(v)
  1821  
  1822  		return nil
  1823  	case *float64:
  1824  		*vv = float64(v)
  1825  
  1826  		return nil
  1827  	default:
  1828  		return xerrors.WithStackTrace(fmt.Errorf(
  1829  			"%w '%+v' (type '%s') to '%T' destination",
  1830  			ErrCannotCast, v, v.Type().Yql(), vv,
  1831  		))
  1832  	}
  1833  }
  1834  
  1835  func (v uint16Value) Yql() string {
  1836  	return strconv.FormatUint(uint64(v), 10) + "us"
  1837  }
  1838  
  1839  func (uint16Value) Type() types.Type {
  1840  	return types.Uint16
  1841  }
  1842  
  1843  func (v uint16Value) toYDB(a *allocator.Allocator) *Ydb.Value {
  1844  	vv := a.Uint32()
  1845  	vv.Uint32Value = uint32(v)
  1846  
  1847  	vvv := a.Value()
  1848  	vvv.Value = vv
  1849  
  1850  	return vvv
  1851  }
  1852  
  1853  func Uint16Value(v uint16) uint16Value {
  1854  	return uint16Value(v)
  1855  }
  1856  
  1857  type uint32Value uint32
  1858  
  1859  func (v uint32Value) castTo(dst interface{}) error {
  1860  	switch vv := dst.(type) {
  1861  	case *string:
  1862  		*vv = strconv.FormatInt(int64(v), 10)
  1863  
  1864  		return nil
  1865  	case *[]byte:
  1866  		*vv = xstring.ToBytes(strconv.FormatInt(int64(v), 10))
  1867  
  1868  		return nil
  1869  	case *uint64:
  1870  		*vv = uint64(v)
  1871  
  1872  		return nil
  1873  	case *int64:
  1874  		*vv = int64(v)
  1875  
  1876  		return nil
  1877  	case *uint32:
  1878  		*vv = uint32(v)
  1879  
  1880  		return nil
  1881  	case *float64:
  1882  		*vv = float64(v)
  1883  
  1884  		return nil
  1885  	default:
  1886  		return xerrors.WithStackTrace(fmt.Errorf(
  1887  			"%w '%+v' (type '%s') to '%T' destination",
  1888  			ErrCannotCast, v, v.Type().Yql(), vv,
  1889  		))
  1890  	}
  1891  }
  1892  
  1893  func (v uint32Value) Yql() string {
  1894  	return strconv.FormatUint(uint64(v), 10) + "u"
  1895  }
  1896  
  1897  func (uint32Value) Type() types.Type {
  1898  	return types.Uint32
  1899  }
  1900  
  1901  func (v uint32Value) toYDB(a *allocator.Allocator) *Ydb.Value {
  1902  	vv := a.Uint32()
  1903  	vv.Uint32Value = uint32(v)
  1904  
  1905  	vvv := a.Value()
  1906  	vvv.Value = vv
  1907  
  1908  	return vvv
  1909  }
  1910  
  1911  func Uint32Value(v uint32) uint32Value {
  1912  	return uint32Value(v)
  1913  }
  1914  
  1915  type uint64Value uint64
  1916  
  1917  func (v uint64Value) castTo(dst interface{}) error {
  1918  	switch vv := dst.(type) {
  1919  	case *string:
  1920  		*vv = strconv.FormatInt(int64(v), 10)
  1921  
  1922  		return nil
  1923  	case *[]byte:
  1924  		*vv = xstring.ToBytes(strconv.FormatInt(int64(v), 10))
  1925  
  1926  		return nil
  1927  	case *uint64:
  1928  		*vv = uint64(v)
  1929  
  1930  		return nil
  1931  	default:
  1932  		return xerrors.WithStackTrace(fmt.Errorf(
  1933  			"%w '%+v' (type '%s') to '%T' destination",
  1934  			ErrCannotCast, v, v.Type().Yql(), vv,
  1935  		))
  1936  	}
  1937  }
  1938  
  1939  func (v uint64Value) Yql() string {
  1940  	return strconv.FormatUint(uint64(v), 10) + "ul"
  1941  }
  1942  
  1943  func (uint64Value) Type() types.Type {
  1944  	return types.Uint64
  1945  }
  1946  
  1947  func (v uint64Value) toYDB(a *allocator.Allocator) *Ydb.Value {
  1948  	vv := a.Uint64()
  1949  	vv.Uint64Value = uint64(v)
  1950  
  1951  	vvv := a.Value()
  1952  	vvv.Value = vv
  1953  
  1954  	return vvv
  1955  }
  1956  
  1957  func Uint64Value(v uint64) uint64Value {
  1958  	return uint64Value(v)
  1959  }
  1960  
  1961  type textValue string
  1962  
  1963  func (v textValue) castTo(dst interface{}) error {
  1964  	switch vv := dst.(type) {
  1965  	case *string:
  1966  		*vv = string(v)
  1967  
  1968  		return nil
  1969  	case *[]byte:
  1970  		*vv = xstring.ToBytes(string(v))
  1971  
  1972  		return nil
  1973  	default:
  1974  		return xerrors.WithStackTrace(fmt.Errorf(
  1975  			"%w '%+v' (type '%s') to '%T' destination",
  1976  			ErrCannotCast, v, v.Type().Yql(), vv,
  1977  		))
  1978  	}
  1979  }
  1980  
  1981  func (v textValue) Yql() string {
  1982  	return fmt.Sprintf("%qu", string(v))
  1983  }
  1984  
  1985  func (textValue) Type() types.Type {
  1986  	return types.Text
  1987  }
  1988  
  1989  func (v textValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  1990  	vv := a.Text()
  1991  	vv.TextValue = string(v)
  1992  
  1993  	vvv := a.Value()
  1994  	vvv.Value = vv
  1995  
  1996  	return vvv
  1997  }
  1998  
  1999  func TextValue(v string) textValue {
  2000  	return textValue(v)
  2001  }
  2002  
  2003  type uuidValue struct {
  2004  	value [16]byte
  2005  }
  2006  
  2007  func (v *uuidValue) castTo(dst interface{}) error {
  2008  	switch vv := dst.(type) {
  2009  	case *string:
  2010  		*vv = string(v.value[:])
  2011  
  2012  		return nil
  2013  	case *[]byte:
  2014  		*vv = v.value[:]
  2015  
  2016  		return nil
  2017  	case *[16]byte:
  2018  		*vv = v.value
  2019  
  2020  		return nil
  2021  	default:
  2022  		return xerrors.WithStackTrace(fmt.Errorf(
  2023  			"%w '%+v' (type '%s') to '%T' destination",
  2024  			ErrCannotCast, v, v.Type().Yql(), vv,
  2025  		))
  2026  	}
  2027  }
  2028  
  2029  func (v *uuidValue) Yql() string {
  2030  	buffer := xstring.Buffer()
  2031  	defer buffer.Free()
  2032  	buffer.WriteString(v.Type().Yql())
  2033  	buffer.WriteByte('(')
  2034  	buffer.WriteByte('"')
  2035  	buffer.WriteString(uuid.UUID(v.value).String())
  2036  	buffer.WriteByte('"')
  2037  	buffer.WriteByte(')')
  2038  
  2039  	return buffer.String()
  2040  }
  2041  
  2042  func (*uuidValue) Type() types.Type {
  2043  	return types.UUID
  2044  }
  2045  
  2046  func (v *uuidValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  2047  	var bytes [16]byte
  2048  	if v != nil {
  2049  		bytes = v.value
  2050  	}
  2051  	vv := a.Low128()
  2052  	vv.Low_128 = binary.BigEndian.Uint64(bytes[8:16])
  2053  
  2054  	vvv := a.Value()
  2055  	vvv.High_128 = binary.BigEndian.Uint64(bytes[0:8])
  2056  	vvv.Value = vv
  2057  
  2058  	return vvv
  2059  }
  2060  
  2061  func UUIDValue(v [16]byte) *uuidValue {
  2062  	return &uuidValue{value: v}
  2063  }
  2064  
  2065  type variantValue struct {
  2066  	innerType types.Type
  2067  	value     Value
  2068  	idx       uint32
  2069  }
  2070  
  2071  func (v *variantValue) Variant() (name string, index uint32) {
  2072  	switch t := v.innerType.(type) {
  2073  	case *types.VariantStruct:
  2074  		return t.Field(int(v.idx)).Name, v.idx
  2075  	default:
  2076  		return "", v.idx
  2077  	}
  2078  }
  2079  
  2080  func (v *variantValue) Value() Value {
  2081  	return v.value
  2082  }
  2083  
  2084  func (v *variantValue) castTo(dst interface{}) error {
  2085  	return v.value.castTo(dst)
  2086  }
  2087  
  2088  func (v *variantValue) Yql() string {
  2089  	buffer := xstring.Buffer()
  2090  	defer buffer.Free()
  2091  	buffer.WriteString("Variant(")
  2092  	buffer.WriteString(v.value.Yql())
  2093  	buffer.WriteByte(',')
  2094  	switch t := v.innerType.(type) {
  2095  	case *types.VariantStruct:
  2096  		fmt.Fprintf(buffer, "%q", t.Field(int(v.idx)).Name)
  2097  	case *types.VariantTuple:
  2098  		fmt.Fprintf(buffer, "\""+strconv.FormatUint(uint64(v.idx), 10)+"\"")
  2099  	}
  2100  	buffer.WriteByte(',')
  2101  	buffer.WriteString(v.Type().Yql())
  2102  	buffer.WriteByte(')')
  2103  
  2104  	return buffer.String()
  2105  }
  2106  
  2107  func (v *variantValue) Type() types.Type {
  2108  	return v.innerType
  2109  }
  2110  
  2111  func (v *variantValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  2112  	vvv := a.Value()
  2113  
  2114  	nested := a.Nested()
  2115  	nested.NestedValue = v.value.toYDB(a)
  2116  
  2117  	vvv.Value = nested
  2118  	vvv.VariantIndex = v.idx
  2119  
  2120  	return vvv
  2121  }
  2122  
  2123  func VariantValueTuple(v Value, idx uint32, t types.Type) *variantValue {
  2124  	if tt, has := t.(*types.Tuple); has {
  2125  		t = types.NewVariantTuple(tt.InnerTypes()...)
  2126  	}
  2127  
  2128  	return &variantValue{
  2129  		innerType: t,
  2130  		value:     v,
  2131  		idx:       idx,
  2132  	}
  2133  }
  2134  
  2135  func VariantValueStruct(v Value, name string, t types.Type) *variantValue {
  2136  	var idx int
  2137  	switch tt := t.(type) {
  2138  	case *types.Struct:
  2139  		fields := tt.Fields()
  2140  		sort.Slice(fields, func(i, j int) bool {
  2141  			return fields[i].Name < fields[j].Name
  2142  		})
  2143  		idx = sort.Search(len(fields), func(i int) bool {
  2144  			return fields[i].Name >= name
  2145  		})
  2146  		t = types.NewVariantStruct(fields...)
  2147  	case *types.VariantStruct:
  2148  		fields := tt.Fields()
  2149  		sort.Slice(fields, func(i, j int) bool {
  2150  			return fields[i].Name < fields[j].Name
  2151  		})
  2152  		idx = sort.Search(len(fields), func(i int) bool {
  2153  			return fields[i].Name >= name
  2154  		})
  2155  	}
  2156  
  2157  	return &variantValue{
  2158  		innerType: t,
  2159  		value:     v,
  2160  		idx:       uint32(idx),
  2161  	}
  2162  }
  2163  
  2164  type voidValue struct{}
  2165  
  2166  func (v voidValue) castTo(dst interface{}) error {
  2167  	return xerrors.WithStackTrace(fmt.Errorf(
  2168  		"%w '%s' to '%T' destination",
  2169  		ErrCannotCast, v.Type().Yql(), dst,
  2170  	))
  2171  }
  2172  
  2173  func (v voidValue) Yql() string {
  2174  	return v.Type().Yql() + "()"
  2175  }
  2176  
  2177  var (
  2178  	_voidValueType = types.Void{}
  2179  	_voidValue     = &Ydb.Value{
  2180  		Value: new(Ydb.Value_NullFlagValue),
  2181  	}
  2182  )
  2183  
  2184  func (voidValue) Type() types.Type {
  2185  	return _voidValueType
  2186  }
  2187  
  2188  func (voidValue) toYDB(*allocator.Allocator) *Ydb.Value {
  2189  	return _voidValue
  2190  }
  2191  
  2192  func VoidValue() voidValue {
  2193  	return voidValue{}
  2194  }
  2195  
  2196  type ysonValue []byte
  2197  
  2198  func (v ysonValue) castTo(dst interface{}) error {
  2199  	switch vv := dst.(type) {
  2200  	case *string:
  2201  		*vv = xstring.FromBytes(v)
  2202  
  2203  		return nil
  2204  	case *[]byte:
  2205  		*vv = v
  2206  
  2207  		return nil
  2208  	default:
  2209  		return xerrors.WithStackTrace(fmt.Errorf(
  2210  			"%w '%+v' (type '%s') to '%T' destination",
  2211  			ErrCannotCast, v, v.Type().Yql(), vv,
  2212  		))
  2213  	}
  2214  }
  2215  
  2216  func (v ysonValue) Yql() string {
  2217  	return fmt.Sprintf("%s(%q)", v.Type().Yql(), string(v))
  2218  }
  2219  
  2220  func (ysonValue) Type() types.Type {
  2221  	return types.YSON
  2222  }
  2223  
  2224  func (v ysonValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  2225  	vv := a.Bytes()
  2226  	if v != nil {
  2227  		vv.BytesValue = v
  2228  	}
  2229  
  2230  	vvv := a.Value()
  2231  	vvv.Value = vv
  2232  
  2233  	return vvv
  2234  }
  2235  
  2236  func YSONValue(v []byte) ysonValue {
  2237  	return v
  2238  }
  2239  
  2240  func zeroPrimitiveValue(t types.Primitive) Value {
  2241  	switch t {
  2242  	case types.Bool:
  2243  		return BoolValue(false)
  2244  
  2245  	case types.Int8:
  2246  		return Int8Value(0)
  2247  
  2248  	case types.Uint8:
  2249  		return Uint8Value(0)
  2250  
  2251  	case types.Int16:
  2252  		return Int16Value(0)
  2253  
  2254  	case types.Uint16:
  2255  		return Uint16Value(0)
  2256  
  2257  	case types.Int32:
  2258  		return Int32Value(0)
  2259  
  2260  	case types.Uint32:
  2261  		return Uint32Value(0)
  2262  
  2263  	case types.Int64:
  2264  		return Int64Value(0)
  2265  
  2266  	case types.Uint64:
  2267  		return Uint64Value(0)
  2268  
  2269  	case types.Float:
  2270  		return FloatValue(0)
  2271  
  2272  	case types.Double:
  2273  		return DoubleValue(0)
  2274  
  2275  	case types.Date:
  2276  		return DateValue(0)
  2277  
  2278  	case types.Datetime:
  2279  		return DatetimeValue(0)
  2280  
  2281  	case types.Timestamp:
  2282  		return TimestampValue(0)
  2283  
  2284  	case types.Interval:
  2285  		return IntervalValue(0)
  2286  
  2287  	case types.Text:
  2288  		return TextValue("")
  2289  
  2290  	case types.YSON:
  2291  		return YSONValue([]byte(""))
  2292  
  2293  	case types.JSON:
  2294  		return JSONValue("")
  2295  
  2296  	case types.JSONDocument:
  2297  		return JSONDocumentValue("")
  2298  
  2299  	case types.DyNumber:
  2300  		return DyNumberValue("")
  2301  
  2302  	case types.TzDate:
  2303  		return TzDateValue("")
  2304  
  2305  	case types.TzDatetime:
  2306  		return TzDatetimeValue("")
  2307  
  2308  	case types.TzTimestamp:
  2309  		return TzTimestampValue("")
  2310  
  2311  	case types.Bytes:
  2312  		return BytesValue([]byte{})
  2313  
  2314  	case types.UUID:
  2315  		return UUIDValue([16]byte{})
  2316  
  2317  	default:
  2318  		panic(fmt.Sprintf("uncovered primitive type '%T'", t))
  2319  	}
  2320  }
  2321  
  2322  func ZeroValue(t types.Type) Value {
  2323  	switch t := t.(type) {
  2324  	case types.Primitive:
  2325  		return zeroPrimitiveValue(t)
  2326  
  2327  	case types.Optional:
  2328  		return NullValue(t.InnerType())
  2329  
  2330  	case *types.Void:
  2331  		return VoidValue()
  2332  
  2333  	case *types.List, *types.EmptyList:
  2334  		return &listValue{
  2335  			t: t,
  2336  		}
  2337  	case *types.Set:
  2338  		return &setValue{
  2339  			t: t,
  2340  		}
  2341  	case *types.Dict:
  2342  		return &dictValue{
  2343  			t: t.ValueType(),
  2344  		}
  2345  	case *types.EmptyDict:
  2346  		return &dictValue{
  2347  			t: t,
  2348  		}
  2349  	case *types.Tuple:
  2350  		return TupleValue(func() []Value {
  2351  			innerTypes := t.InnerTypes()
  2352  			values := make([]Value, len(innerTypes))
  2353  			for i, tt := range innerTypes {
  2354  				values[i] = ZeroValue(tt)
  2355  			}
  2356  
  2357  			return values
  2358  		}()...)
  2359  	case *types.Struct:
  2360  		return StructValue(func() []StructValueField {
  2361  			fields := t.Fields()
  2362  			values := make([]StructValueField, len(fields))
  2363  			for i := range fields {
  2364  				values[i] = StructValueField{
  2365  					Name: fields[i].Name,
  2366  					V:    ZeroValue(fields[i].T),
  2367  				}
  2368  			}
  2369  
  2370  			return values
  2371  		}()...)
  2372  	case *types.Decimal:
  2373  		return DecimalValue([16]byte{}, 22, 9)
  2374  
  2375  	default:
  2376  		panic(fmt.Sprintf("type '%T' have not a zero value", t))
  2377  	}
  2378  }
  2379  
  2380  type bytesValue []byte
  2381  
  2382  func (v bytesValue) castTo(dst interface{}) error {
  2383  	switch vv := dst.(type) {
  2384  	case *string:
  2385  		*vv = xstring.FromBytes(v)
  2386  
  2387  		return nil
  2388  	case *[]byte:
  2389  		*vv = v
  2390  
  2391  		return nil
  2392  	default:
  2393  		return xerrors.WithStackTrace(fmt.Errorf(
  2394  			"%w '%+v' (type '%s') to '%T' destination",
  2395  			ErrCannotCast, v, v.Type().Yql(), vv,
  2396  		))
  2397  	}
  2398  }
  2399  
  2400  func (v bytesValue) Yql() string {
  2401  	return fmt.Sprintf("%q", string(v))
  2402  }
  2403  
  2404  func (bytesValue) Type() types.Type {
  2405  	return types.Bytes
  2406  }
  2407  
  2408  func (v bytesValue) toYDB(a *allocator.Allocator) *Ydb.Value {
  2409  	vv := a.Bytes()
  2410  
  2411  	vv.BytesValue = v
  2412  
  2413  	vvv := a.Value()
  2414  	vvv.Value = vv
  2415  
  2416  	return vvv
  2417  }
  2418  
  2419  func BytesValue(v []byte) bytesValue {
  2420  	return v
  2421  }