forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
column_type_encoding.go
1143 lines (1104 loc) · 34.9 KB
/
column_type_encoding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package sqlbase
import (
"time"
"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/bitarray"
"github.com/cockroachdb/cockroach/pkg/util/duration"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/ipaddr"
"github.com/cockroachdb/cockroach/pkg/util/json"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/lib/pq/oid"
"github.com/pkg/errors"
)
// This file contains facilities to encode values of specific SQL
// types to either index keys or to store in the value part of column
// families.
// EncodeTableKey encodes `val` into `b` and returns the new buffer.
// This is suitable to generate index/lookup keys in KV.
//
// The encoded value is guaranteed to be lexicographically sortable,
// but not guaranteed to be round-trippable during decoding: some
// values like decimals or collated strings have composite encoding
// where part of their value lies in the value part of the key/value
// pair.
//
// See also: docs/tech-notes/encoding.md, EncodeTableValue().
func EncodeTableKey(b []byte, val tree.Datum, dir encoding.Direction) ([]byte, error) {
if (dir != encoding.Ascending) && (dir != encoding.Descending) {
return nil, errors.Errorf("invalid direction: %d", dir)
}
if val == tree.DNull {
if dir == encoding.Ascending {
return encoding.EncodeNullAscending(b), nil
}
return encoding.EncodeNullDescending(b), nil
}
switch t := tree.UnwrapDatum(nil, val).(type) {
case *tree.DBool:
var x int64
if *t {
x = 1
} else {
x = 0
}
if dir == encoding.Ascending {
return encoding.EncodeVarintAscending(b, x), nil
}
return encoding.EncodeVarintDescending(b, x), nil
case *tree.DInt:
if dir == encoding.Ascending {
return encoding.EncodeVarintAscending(b, int64(*t)), nil
}
return encoding.EncodeVarintDescending(b, int64(*t)), nil
case *tree.DFloat:
if dir == encoding.Ascending {
return encoding.EncodeFloatAscending(b, float64(*t)), nil
}
return encoding.EncodeFloatDescending(b, float64(*t)), nil
case *tree.DDecimal:
if dir == encoding.Ascending {
return encoding.EncodeDecimalAscending(b, &t.Decimal), nil
}
return encoding.EncodeDecimalDescending(b, &t.Decimal), nil
case *tree.DString:
if dir == encoding.Ascending {
return encoding.EncodeStringAscending(b, string(*t)), nil
}
return encoding.EncodeStringDescending(b, string(*t)), nil
case *tree.DBytes:
if dir == encoding.Ascending {
return encoding.EncodeStringAscending(b, string(*t)), nil
}
return encoding.EncodeStringDescending(b, string(*t)), nil
case *tree.DDate:
if dir == encoding.Ascending {
return encoding.EncodeVarintAscending(b, int64(*t)), nil
}
return encoding.EncodeVarintDescending(b, int64(*t)), nil
case *tree.DTime:
if dir == encoding.Ascending {
return encoding.EncodeVarintAscending(b, int64(*t)), nil
}
return encoding.EncodeVarintDescending(b, int64(*t)), nil
case *tree.DTimestamp:
if dir == encoding.Ascending {
return encoding.EncodeTimeAscending(b, t.Time), nil
}
return encoding.EncodeTimeDescending(b, t.Time), nil
case *tree.DTimestampTZ:
if dir == encoding.Ascending {
return encoding.EncodeTimeAscending(b, t.Time), nil
}
return encoding.EncodeTimeDescending(b, t.Time), nil
case *tree.DInterval:
if dir == encoding.Ascending {
return encoding.EncodeDurationAscending(b, t.Duration)
}
return encoding.EncodeDurationDescending(b, t.Duration)
case *tree.DUuid:
if dir == encoding.Ascending {
return encoding.EncodeBytesAscending(b, t.GetBytes()), nil
}
return encoding.EncodeBytesDescending(b, t.GetBytes()), nil
case *tree.DIPAddr:
data := t.ToBuffer(nil)
if dir == encoding.Ascending {
return encoding.EncodeBytesAscending(b, data), nil
}
return encoding.EncodeBytesDescending(b, data), nil
case *tree.DTuple:
for _, datum := range t.D {
var err error
b, err = EncodeTableKey(b, datum, dir)
if err != nil {
return nil, err
}
}
return b, nil
case *tree.DCollatedString:
if dir == encoding.Ascending {
return encoding.EncodeBytesAscending(b, t.Key), nil
}
return encoding.EncodeBytesDescending(b, t.Key), nil
case *tree.DBitArray:
if dir == encoding.Ascending {
return encoding.EncodeBitArrayAscending(b, t.BitArray), nil
}
return encoding.EncodeBitArrayDescending(b, t.BitArray), nil
case *tree.DArray:
for _, datum := range t.Array {
var err error
b, err = EncodeTableKey(b, datum, dir)
if err != nil {
return nil, err
}
}
return b, nil
case *tree.DOid:
if dir == encoding.Ascending {
return encoding.EncodeVarintAscending(b, int64(t.DInt)), nil
}
return encoding.EncodeVarintDescending(b, int64(t.DInt)), nil
}
return nil, errors.Errorf("unable to encode table key: %T", val)
}
// DecodeTableKey decodes a value encoded by EncodeTableKey.
func DecodeTableKey(
a *DatumAlloc, valType *types.T, key []byte, dir encoding.Direction,
) (tree.Datum, []byte, error) {
if (dir != encoding.Ascending) && (dir != encoding.Descending) {
return nil, nil, errors.Errorf("invalid direction: %d", dir)
}
var isNull bool
if key, isNull = encoding.DecodeIfNull(key); isNull {
return tree.DNull, key, nil
}
var rkey []byte
var err error
switch valType.Family() {
case types.BitFamily:
var r bitarray.BitArray
if dir == encoding.Ascending {
rkey, r, err = encoding.DecodeBitArrayAscending(key)
} else {
rkey, r, err = encoding.DecodeBitArrayDescending(key)
}
return a.NewDBitArray(tree.DBitArray{BitArray: r}), rkey, err
case types.BoolFamily:
var i int64
if dir == encoding.Ascending {
rkey, i, err = encoding.DecodeVarintAscending(key)
} else {
rkey, i, err = encoding.DecodeVarintDescending(key)
}
// No need to chunk allocate DBool as MakeDBool returns either
// tree.DBoolTrue or tree.DBoolFalse.
return tree.MakeDBool(tree.DBool(i != 0)), rkey, err
case types.IntFamily:
var i int64
if dir == encoding.Ascending {
rkey, i, err = encoding.DecodeVarintAscending(key)
} else {
rkey, i, err = encoding.DecodeVarintDescending(key)
}
return a.NewDInt(tree.DInt(i)), rkey, err
case types.FloatFamily:
var f float64
if dir == encoding.Ascending {
rkey, f, err = encoding.DecodeFloatAscending(key)
} else {
rkey, f, err = encoding.DecodeFloatDescending(key)
}
return a.NewDFloat(tree.DFloat(f)), rkey, err
case types.DecimalFamily:
var d apd.Decimal
if dir == encoding.Ascending {
rkey, d, err = encoding.DecodeDecimalAscending(key, nil)
} else {
rkey, d, err = encoding.DecodeDecimalDescending(key, nil)
}
dd := a.NewDDecimal(tree.DDecimal{Decimal: d})
return dd, rkey, err
case types.StringFamily:
var r string
if dir == encoding.Ascending {
rkey, r, err = encoding.DecodeUnsafeStringAscending(key, nil)
} else {
rkey, r, err = encoding.DecodeUnsafeStringDescending(key, nil)
}
if valType.Oid() == oid.T_name {
return a.NewDName(tree.DString(r)), rkey, err
}
return a.NewDString(tree.DString(r)), rkey, err
case types.CollatedStringFamily:
var r string
rkey, r, err = encoding.DecodeUnsafeStringAscending(key, nil)
if err != nil {
return nil, nil, err
}
return tree.NewDCollatedString(r, valType.Locale(), &a.env), rkey, err
case types.JsonFamily:
return tree.DNull, []byte{}, nil
case types.BytesFamily:
var r []byte
if dir == encoding.Ascending {
rkey, r, err = encoding.DecodeBytesAscending(key, nil)
} else {
rkey, r, err = encoding.DecodeBytesDescending(key, nil)
}
return a.NewDBytes(tree.DBytes(r)), rkey, err
case types.DateFamily:
var t int64
if dir == encoding.Ascending {
rkey, t, err = encoding.DecodeVarintAscending(key)
} else {
rkey, t, err = encoding.DecodeVarintDescending(key)
}
return a.NewDDate(tree.DDate(t)), rkey, err
case types.TimeFamily:
var t int64
if dir == encoding.Ascending {
rkey, t, err = encoding.DecodeVarintAscending(key)
} else {
rkey, t, err = encoding.DecodeVarintDescending(key)
}
return a.NewDTime(tree.DTime(t)), rkey, err
case types.TimestampFamily:
var t time.Time
if dir == encoding.Ascending {
rkey, t, err = encoding.DecodeTimeAscending(key)
} else {
rkey, t, err = encoding.DecodeTimeDescending(key)
}
return a.NewDTimestamp(tree.DTimestamp{Time: t}), rkey, err
case types.TimestampTZFamily:
var t time.Time
if dir == encoding.Ascending {
rkey, t, err = encoding.DecodeTimeAscending(key)
} else {
rkey, t, err = encoding.DecodeTimeDescending(key)
}
return a.NewDTimestampTZ(tree.DTimestampTZ{Time: t}), rkey, err
case types.IntervalFamily:
var d duration.Duration
if dir == encoding.Ascending {
rkey, d, err = encoding.DecodeDurationAscending(key)
} else {
rkey, d, err = encoding.DecodeDurationDescending(key)
}
return a.NewDInterval(tree.DInterval{Duration: d}), rkey, err
case types.UuidFamily:
var r []byte
if dir == encoding.Ascending {
rkey, r, err = encoding.DecodeBytesAscending(key, nil)
} else {
rkey, r, err = encoding.DecodeBytesDescending(key, nil)
}
if err != nil {
return nil, nil, err
}
u, err := uuid.FromBytes(r)
return a.NewDUuid(tree.DUuid{UUID: u}), rkey, err
case types.INetFamily:
var r []byte
if dir == encoding.Ascending {
rkey, r, err = encoding.DecodeBytesAscending(key, nil)
} else {
rkey, r, err = encoding.DecodeBytesDescending(key, nil)
}
if err != nil {
return nil, nil, err
}
var ipAddr ipaddr.IPAddr
_, err := ipAddr.FromBuffer(r)
return a.NewDIPAddr(tree.DIPAddr{IPAddr: ipAddr}), rkey, err
case types.OidFamily:
var i int64
if dir == encoding.Ascending {
rkey, i, err = encoding.DecodeVarintAscending(key)
} else {
rkey, i, err = encoding.DecodeVarintDescending(key)
}
return a.NewDOid(tree.MakeDOid(tree.DInt(i))), rkey, err
default:
return nil, nil, errors.Errorf("unable to decode table key: %s", valType)
}
}
// EncodeTableValue encodes `val` into `appendTo` using DatumEncoding_VALUE
// and returns the new buffer.
//
// This is suitable for generating the value part of individual columns
// in a column family.
//
// The encoded value is guaranteed to round
// trip and decode exactly to its input, but is not guaranteed to be
// lexicographically sortable.
//
// See also: docs/tech-notes/encoding.md, EncodeTableKey().
func EncodeTableValue(
appendTo []byte, colID ColumnID, val tree.Datum, scratch []byte,
) ([]byte, error) {
if val == tree.DNull {
return encoding.EncodeNullValue(appendTo, uint32(colID)), nil
}
switch t := tree.UnwrapDatum(nil, val).(type) {
case *tree.DBitArray:
return encoding.EncodeBitArrayValue(appendTo, uint32(colID), t.BitArray), nil
case *tree.DBool:
return encoding.EncodeBoolValue(appendTo, uint32(colID), bool(*t)), nil
case *tree.DInt:
return encoding.EncodeIntValue(appendTo, uint32(colID), int64(*t)), nil
case *tree.DFloat:
return encoding.EncodeFloatValue(appendTo, uint32(colID), float64(*t)), nil
case *tree.DDecimal:
return encoding.EncodeDecimalValue(appendTo, uint32(colID), &t.Decimal), nil
case *tree.DString:
return encoding.EncodeBytesValue(appendTo, uint32(colID), []byte(*t)), nil
case *tree.DBytes:
return encoding.EncodeBytesValue(appendTo, uint32(colID), []byte(*t)), nil
case *tree.DDate:
return encoding.EncodeIntValue(appendTo, uint32(colID), int64(*t)), nil
case *tree.DTime:
return encoding.EncodeIntValue(appendTo, uint32(colID), int64(*t)), nil
case *tree.DTimestamp:
return encoding.EncodeTimeValue(appendTo, uint32(colID), t.Time), nil
case *tree.DTimestampTZ:
return encoding.EncodeTimeValue(appendTo, uint32(colID), t.Time), nil
case *tree.DInterval:
return encoding.EncodeDurationValue(appendTo, uint32(colID), t.Duration), nil
case *tree.DUuid:
return encoding.EncodeUUIDValue(appendTo, uint32(colID), t.UUID), nil
case *tree.DIPAddr:
return encoding.EncodeIPAddrValue(appendTo, uint32(colID), t.IPAddr), nil
case *tree.DJSON:
encoded, err := json.EncodeJSON(scratch, t.JSON)
if err != nil {
return nil, err
}
return encoding.EncodeJSONValue(appendTo, uint32(colID), encoded), nil
case *tree.DArray:
a, err := encodeArray(t, scratch)
if err != nil {
return nil, err
}
return encoding.EncodeArrayValue(appendTo, uint32(colID), a), nil
case *tree.DTuple:
return encodeTuple(t, appendTo, uint32(colID), scratch)
case *tree.DCollatedString:
return encoding.EncodeBytesValue(appendTo, uint32(colID), []byte(t.Contents)), nil
case *tree.DOid:
return encoding.EncodeIntValue(appendTo, uint32(colID), int64(t.DInt)), nil
default:
return nil, errors.Errorf("unable to encode table value: %T", t)
}
}
// DecodeTableValue decodes a value encoded by EncodeTableValue.
func DecodeTableValue(a *DatumAlloc, valType *types.T, b []byte) (tree.Datum, []byte, error) {
_, dataOffset, _, typ, err := encoding.DecodeValueTag(b)
if err != nil {
return nil, b, err
}
// NULL is special because it is a valid value for any type.
if typ == encoding.Null {
return tree.DNull, b[dataOffset:], nil
}
// Bool is special because the value is stored in the value tag.
if valType.Family() != types.BoolFamily {
b = b[dataOffset:]
}
return decodeUntaggedDatum(a, valType, b)
}
// decodeUntaggedDatum is used to decode a Datum whose type is known,
// and which doesn't have a value tag (either due to it having been
// consumed already or not having one in the first place).
//
// This is used to decode datums encoded using value encoding.
//
// If t is types.Bool, the value tag must be present, as its value is encoded in
// the tag directly.
func decodeUntaggedDatum(a *DatumAlloc, t *types.T, buf []byte) (tree.Datum, []byte, error) {
switch t.Family() {
case types.IntFamily:
b, i, err := encoding.DecodeUntaggedIntValue(buf)
if err != nil {
return nil, b, err
}
return a.NewDInt(tree.DInt(i)), b, nil
case types.StringFamily:
b, data, err := encoding.DecodeUntaggedBytesValue(buf)
if err != nil {
return nil, b, err
}
return a.NewDString(tree.DString(data)), b, nil
case types.CollatedStringFamily:
b, data, err := encoding.DecodeUntaggedBytesValue(buf)
return tree.NewDCollatedString(string(data), t.Locale(), &a.env), b, err
case types.BitFamily:
b, data, err := encoding.DecodeUntaggedBitArrayValue(buf)
return a.NewDBitArray(tree.DBitArray{BitArray: data}), b, err
case types.BoolFamily:
// A boolean's value is encoded in its tag directly, so we don't have an
// "Untagged" version of this function.
b, data, err := encoding.DecodeBoolValue(buf)
if err != nil {
return nil, b, err
}
return tree.MakeDBool(tree.DBool(data)), b, nil
case types.FloatFamily:
b, data, err := encoding.DecodeUntaggedFloatValue(buf)
if err != nil {
return nil, b, err
}
return a.NewDFloat(tree.DFloat(data)), b, nil
case types.DecimalFamily:
b, data, err := encoding.DecodeUntaggedDecimalValue(buf)
if err != nil {
return nil, b, err
}
return a.NewDDecimal(tree.DDecimal{Decimal: data}), b, nil
case types.BytesFamily:
b, data, err := encoding.DecodeUntaggedBytesValue(buf)
if err != nil {
return nil, b, err
}
return a.NewDBytes(tree.DBytes(data)), b, nil
case types.DateFamily:
b, data, err := encoding.DecodeUntaggedIntValue(buf)
if err != nil {
return nil, b, err
}
return a.NewDDate(tree.DDate(data)), b, nil
case types.TimeFamily:
b, data, err := encoding.DecodeUntaggedIntValue(buf)
if err != nil {
return nil, b, err
}
return a.NewDTime(tree.DTime(data)), b, nil
case types.TimestampFamily:
b, data, err := encoding.DecodeUntaggedTimeValue(buf)
if err != nil {
return nil, b, err
}
return a.NewDTimestamp(tree.DTimestamp{Time: data}), b, nil
case types.TimestampTZFamily:
b, data, err := encoding.DecodeUntaggedTimeValue(buf)
if err != nil {
return nil, b, err
}
return a.NewDTimestampTZ(tree.DTimestampTZ{Time: data}), b, nil
case types.IntervalFamily:
b, data, err := encoding.DecodeUntaggedDurationValue(buf)
return a.NewDInterval(tree.DInterval{Duration: data}), b, err
case types.UuidFamily:
b, data, err := encoding.DecodeUntaggedUUIDValue(buf)
return a.NewDUuid(tree.DUuid{UUID: data}), b, err
case types.INetFamily:
b, data, err := encoding.DecodeUntaggedIPAddrValue(buf)
return a.NewDIPAddr(tree.DIPAddr{IPAddr: data}), b, err
case types.JsonFamily:
b, data, err := encoding.DecodeUntaggedBytesValue(buf)
if err != nil {
return nil, b, err
}
j, err := json.FromEncoding(data)
if err != nil {
return nil, b, err
}
return a.NewDJSON(tree.DJSON{JSON: j}), b, nil
case types.OidFamily:
b, data, err := encoding.DecodeUntaggedIntValue(buf)
return a.NewDOid(tree.MakeDOid(tree.DInt(data))), b, err
case types.ArrayFamily:
return decodeArray(a, t.ArrayContents(), buf)
case types.TupleFamily:
return decodeTuple(a, t, buf)
default:
return nil, buf, errors.Errorf("couldn't decode type %s", t)
}
}
// EncodeDatumKeyAscending encodes a datum using an order-preserving
// encoding.
// The encoding is lossy: some datums need composite encoding where
// the key part only contains part of the datum's information.
func EncodeDatumKeyAscending(b []byte, d tree.Datum) ([]byte, error) {
if values, ok := d.(*tree.DTuple); ok {
return EncodeDatumsKeyAscending(b, values.D)
}
return EncodeTableKey(b, d, encoding.Ascending)
}
// EncodeDatumsKeyAscending encodes a Datums (tuple) using an
// order-preserving encoding.
// The encoding is lossy: some datums need composite encoding where
// the key part only contains part of the datum's information.
func EncodeDatumsKeyAscending(b []byte, d tree.Datums) ([]byte, error) {
for _, val := range d {
var err error
b, err = EncodeDatumKeyAscending(b, val)
if err != nil {
return nil, err
}
}
return b, nil
}
// MarshalColumnValue produces the value encoding of the given datum,
// constrained by the given column type, into a roachpb.Value.
//
// This is used when when the table format does not use column
// families, such as pre-2.0 tables and some system tables.
//
// If val's type is incompatible with col, or if col's type is not yet
// implemented by this function, an error is returned.
func MarshalColumnValue(col ColumnDescriptor, val tree.Datum) (roachpb.Value, error) {
var r roachpb.Value
if val == tree.DNull {
return r, nil
}
switch col.Type.Family() {
case types.BitFamily:
if v, ok := val.(*tree.DBitArray); ok {
r.SetBitArray(v.BitArray)
return r, nil
}
case types.BoolFamily:
if v, ok := val.(*tree.DBool); ok {
r.SetBool(bool(*v))
return r, nil
}
case types.IntFamily:
if v, ok := tree.AsDInt(val); ok {
r.SetInt(int64(v))
return r, nil
}
case types.FloatFamily:
if v, ok := val.(*tree.DFloat); ok {
r.SetFloat(float64(*v))
return r, nil
}
case types.DecimalFamily:
if v, ok := val.(*tree.DDecimal); ok {
err := r.SetDecimal(&v.Decimal)
return r, err
}
case types.StringFamily:
if v, ok := tree.AsDString(val); ok {
r.SetString(string(v))
return r, nil
}
case types.BytesFamily:
if v, ok := val.(*tree.DBytes); ok {
r.SetString(string(*v))
return r, nil
}
case types.DateFamily:
if v, ok := val.(*tree.DDate); ok {
r.SetInt(int64(*v))
return r, nil
}
case types.TimeFamily:
if v, ok := val.(*tree.DTime); ok {
r.SetInt(int64(*v))
return r, nil
}
case types.TimestampFamily:
if v, ok := val.(*tree.DTimestamp); ok {
r.SetTime(v.Time)
return r, nil
}
case types.TimestampTZFamily:
if v, ok := val.(*tree.DTimestampTZ); ok {
r.SetTime(v.Time)
return r, nil
}
case types.IntervalFamily:
if v, ok := val.(*tree.DInterval); ok {
err := r.SetDuration(v.Duration)
return r, err
}
case types.UuidFamily:
if v, ok := val.(*tree.DUuid); ok {
r.SetBytes(v.GetBytes())
return r, nil
}
case types.INetFamily:
if v, ok := val.(*tree.DIPAddr); ok {
data := v.ToBuffer(nil)
r.SetBytes(data)
return r, nil
}
case types.JsonFamily:
if v, ok := val.(*tree.DJSON); ok {
data, err := json.EncodeJSON(nil, v.JSON)
if err != nil {
return r, err
}
r.SetBytes(data)
return r, nil
}
case types.ArrayFamily:
if v, ok := val.(*tree.DArray); ok {
if err := checkElementType(v.ParamTyp, col.Type.ArrayContents()); err != nil {
return r, err
}
b, err := encodeArray(v, nil)
if err != nil {
return r, err
}
r.SetBytes(b)
return r, nil
}
case types.CollatedStringFamily:
if v, ok := val.(*tree.DCollatedString); ok {
if v.Locale == col.Type.Locale() {
r.SetString(v.Contents)
return r, nil
}
// We can't fail here with a locale mismatch, this is a sign
// that the proper validation has not been performed upstream in
// the mutation planning code.
return r, pgerror.AssertionFailedf(
"locale mismatch %q vs %q for column %q",
v.Locale, col.Type.Locale(), tree.ErrNameString(col.Name))
}
case types.OidFamily:
if v, ok := val.(*tree.DOid); ok {
r.SetInt(int64(v.DInt))
return r, nil
}
default:
return r, pgerror.AssertionFailedf("unsupported column type: %s", col.Type.Family())
}
return r, pgerror.AssertionFailedf("mismatched type %q vs %q for column %q",
val.ResolvedType(), col.Type.Family(), tree.ErrNameString(col.Name))
}
// UnmarshalColumnValue is the counterpart to MarshalColumnValues.
//
// It decodes the value from a roachpb.Value using the type expected
// by the column. An error is returned if the value's type does not
// match the column's type.
func UnmarshalColumnValue(a *DatumAlloc, typ *types.T, value roachpb.Value) (tree.Datum, error) {
if value.RawBytes == nil {
return tree.DNull, nil
}
switch typ.Family() {
case types.BitFamily:
d, err := value.GetBitArray()
if err != nil {
return nil, err
}
return a.NewDBitArray(tree.DBitArray{BitArray: d}), nil
case types.BoolFamily:
v, err := value.GetBool()
if err != nil {
return nil, err
}
return tree.MakeDBool(tree.DBool(v)), nil
case types.IntFamily:
v, err := value.GetInt()
if err != nil {
return nil, err
}
return a.NewDInt(tree.DInt(v)), nil
case types.FloatFamily:
v, err := value.GetFloat()
if err != nil {
return nil, err
}
return a.NewDFloat(tree.DFloat(v)), nil
case types.DecimalFamily:
v, err := value.GetDecimal()
if err != nil {
return nil, err
}
dd := a.NewDDecimal(tree.DDecimal{Decimal: v})
return dd, nil
case types.StringFamily:
v, err := value.GetBytes()
if err != nil {
return nil, err
}
if typ.Oid() == oid.T_name {
return a.NewDName(tree.DString(v)), nil
}
return a.NewDString(tree.DString(v)), nil
case types.BytesFamily:
v, err := value.GetBytes()
if err != nil {
return nil, err
}
return a.NewDBytes(tree.DBytes(v)), nil
case types.DateFamily:
v, err := value.GetInt()
if err != nil {
return nil, err
}
return a.NewDDate(tree.DDate(v)), nil
case types.TimeFamily:
v, err := value.GetInt()
if err != nil {
return nil, err
}
return a.NewDTime(tree.DTime(v)), nil
case types.TimestampFamily:
v, err := value.GetTime()
if err != nil {
return nil, err
}
return a.NewDTimestamp(tree.DTimestamp{Time: v}), nil
case types.TimestampTZFamily:
v, err := value.GetTime()
if err != nil {
return nil, err
}
return a.NewDTimestampTZ(tree.DTimestampTZ{Time: v}), nil
case types.IntervalFamily:
d, err := value.GetDuration()
if err != nil {
return nil, err
}
return a.NewDInterval(tree.DInterval{Duration: d}), nil
case types.CollatedStringFamily:
v, err := value.GetBytes()
if err != nil {
return nil, err
}
return tree.NewDCollatedString(string(v), typ.Locale(), &a.env), nil
case types.UuidFamily:
v, err := value.GetBytes()
if err != nil {
return nil, err
}
u, err := uuid.FromBytes(v)
if err != nil {
return nil, err
}
return a.NewDUuid(tree.DUuid{UUID: u}), nil
case types.INetFamily:
v, err := value.GetBytes()
if err != nil {
return nil, err
}
var ipAddr ipaddr.IPAddr
_, err = ipAddr.FromBuffer(v)
if err != nil {
return nil, err
}
return a.NewDIPAddr(tree.DIPAddr{IPAddr: ipAddr}), nil
case types.OidFamily:
v, err := value.GetInt()
if err != nil {
return nil, err
}
return a.NewDOid(tree.MakeDOid(tree.DInt(v))), nil
case types.ArrayFamily:
v, err := value.GetBytes()
if err != nil {
return nil, err
}
datum, _, err := decodeArrayNoMarshalColumnValue(a, typ.ArrayContents(), v)
return datum, err
case types.JsonFamily:
v, err := value.GetBytes()
if err != nil {
return nil, err
}
_, jsonDatum, err := json.DecodeJSON(v)
if err != nil {
return nil, err
}
return tree.NewDJSON(jsonDatum), nil
default:
return nil, errors.Errorf("unsupported column type: %s", typ.Family())
}
}
// encodeTuple produces the value encoding for a tuple.
func encodeTuple(t *tree.DTuple, appendTo []byte, colID uint32, scratch []byte) ([]byte, error) {
appendTo = encoding.EncodeValueTag(appendTo, colID, encoding.Tuple)
appendTo = encoding.EncodeNonsortingUvarint(appendTo, uint64(len(t.D)))
var err error
for _, dd := range t.D {
appendTo, err = EncodeTableValue(appendTo, ColumnID(encoding.NoColumnID), dd, scratch)
if err != nil {
return nil, err
}
}
return appendTo, nil
}
// decodeTuple decodes a tuple from its value encoding. It is the
// counterpart of encodeTuple().
func decodeTuple(a *DatumAlloc, tupTyp *types.T, b []byte) (tree.Datum, []byte, error) {
b, _, _, err := encoding.DecodeNonsortingUvarint(b)
if err != nil {
return nil, nil, err
}
result := tree.DTuple{
D: a.NewDatums(len(tupTyp.TupleContents())),
}
var datum tree.Datum
for i := range tupTyp.TupleContents() {
datum, b, err = DecodeTableValue(a, &tupTyp.TupleContents()[i], b)
if err != nil {
return nil, b, err
}
result.D[i] = datum
}
return a.NewDTuple(result), b, nil
}
// encodeArray produces the value encoding for an array.
func encodeArray(d *tree.DArray, scratch []byte) ([]byte, error) {
if err := d.Validate(); err != nil {
return scratch, err
}
scratch = scratch[0:0]
elementType, err := datumTypeToArrayElementEncodingType(d.ParamTyp)
if err != nil {
return nil, err
}
header := arrayHeader{
hasNulls: d.HasNulls,
// TODO(justin): support multiple dimensions.
numDimensions: 1,
elementType: elementType,
length: uint64(d.Len()),
// We don't encode the NULL bitmap in this function because we do it in lockstep with the
// main data.
}
scratch, err = encodeArrayHeader(header, scratch)
if err != nil {
return nil, err
}
nullBitmapStart := len(scratch)
if d.HasNulls {
for i := 0; i < numBytesInBitArray(d.Len()); i++ {
scratch = append(scratch, 0)
}
}
for i, e := range d.Array {
var err error
if d.HasNulls && e == tree.DNull {
setBit(scratch[nullBitmapStart:], i)
} else {
scratch, err = encodeArrayElement(scratch, e)
if err != nil {
return nil, err
}
}
}
return scratch, nil
}
// decodeArray decodes the value encoding for an array.
func decodeArray(a *DatumAlloc, elementType *types.T, b []byte) (tree.Datum, []byte, error) {
b, _, _, err := encoding.DecodeNonsortingUvarint(b)
if err != nil {
return nil, b, err
}
return decodeArrayNoMarshalColumnValue(a, elementType, b)
}
// decodeArrayNoMarshalColumnValue skips the step where the MarshalColumnValue
// is stripped from the bytes. This is required for single-column family arrays.
func decodeArrayNoMarshalColumnValue(
a *DatumAlloc, elementType *types.T, b []byte,
) (tree.Datum, []byte, error) {
header, b, err := decodeArrayHeader(b)
if err != nil {
return nil, b, err
}
result := tree.DArray{
Array: make(tree.Datums, header.length),
ParamTyp: elementType,
}
var val tree.Datum
for i := uint64(0); i < header.length; i++ {
if header.isNull(i) {
result.Array[i] = tree.DNull
result.HasNulls = true
} else {
val, b, err = decodeUntaggedDatum(a, elementType, b)
if err != nil {
return nil, b, err
}
result.Array[i] = val
}
}
return &result, b, nil
}
// arrayHeader is a parameter passing struct between
// encodeArray/decodeArray and encodeArrayHeader/decodeArrayHeader.
//
// It describes the important properties of an array that are useful
// for an efficient value encoding.
type arrayHeader struct {
// hasNulls is set if the array contains any NULL values.
hasNulls bool
// numDimensions is the number of dimensions in the array.
numDimensions int
// elementType is the encoding type of the array elements.
elementType encoding.Type
// length is the total number of elements encoded.
length uint64
// nullBitmap is a compact representation of which array indexes
// have NULL values.
nullBitmap []byte
}
// isNull returns true iff the array element at the given index is
// NULL.
func (h arrayHeader) isNull(i uint64) bool {
return h.hasNulls && ((h.nullBitmap[i/8]>>(i%8))&1) == 1
}
// setBit sets the bit in the given bitmap at index idx to 1. It's used to
// construct the NULL bitmap within arrays.
func setBit(bitmap []byte, idx int) {
bitmap[idx/8] = bitmap[idx/8] | (1 << uint(idx%8))
}
// numBytesInBitArray returns the minimum number of bytes necessary to
// store the given number of bits.
func numBytesInBitArray(numBits int) int {
return (numBits + 7) / 8
}
// makeBitVec carves a bitmap (byte array intended to store bits) for
// the given number of bits out of its first argument. It returns the
// remainder of the first argument after the bitmap has been reserved
// into it.
func makeBitVec(src []byte, length int) (b, bitVec []byte) {
nullBitmapNumBytes := numBytesInBitArray(length)
return src[nullBitmapNumBytes:], src[:nullBitmapNumBytes]
}
const hasNullFlag = 1 << 4
// encodeArrayHeader is used by encodeArray to encode the header
// at the beginning of the value encoding.