-
Notifications
You must be signed in to change notification settings - Fork 294
/
common.go
1249 lines (1119 loc) · 34.6 KB
/
common.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
package common
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/apache/arrow/go/v12/arrow"
"github.com/apache/arrow/go/v12/arrow/array"
"github.com/xitongsys/parquet-go/parquet"
)
// `parquet:"name=Name, type=FIXED_LEN_BYTE_ARRAY, length=12"`
type Tag struct {
InName string
ExName string
Type string
KeyType string
ValueType string
ConvertedType string
KeyConvertedType string
ValueConvertedType string
Length int32
KeyLength int32
ValueLength int32
Scale int32
KeyScale int32
ValueScale int32
Precision int32
KeyPrecision int32
ValuePrecision int32
IsAdjustedToUTC bool
KeyIsAdjustedToUTC bool
ValueIsAdjustedToUTC bool
FieldID int32
KeyFieldID int32
ValueFieldID int32
Encoding parquet.Encoding
KeyEncoding parquet.Encoding
ValueEncoding parquet.Encoding
OmitStats bool
KeyOmitStats bool
ValueOmitStats bool
RepetitionType parquet.FieldRepetitionType
KeyRepetitionType parquet.FieldRepetitionType
ValueRepetitionType parquet.FieldRepetitionType
LogicalTypeFields map[string]string
KeyLogicalTypeFields map[string]string
ValueLogicalTypeFields map[string]string
}
func NewTag() *Tag {
return &Tag{
LogicalTypeFields: make(map[string]string),
KeyLogicalTypeFields: make(map[string]string),
ValueLogicalTypeFields: make(map[string]string),
}
}
func StringToTag(tag string) (*Tag, error) {
mp := NewTag()
tagStr := strings.Replace(tag, "\t", "", -1)
tags := strings.Split(tagStr, ",")
for _, tag := range tags {
tag = strings.TrimSpace(tag)
kv := strings.SplitN(tag, "=", 2)
if len(kv) != 2 {
return nil, fmt.Errorf("expect 'key=value' but got '%s'", tag)
}
key := kv[0]
key = strings.ToLower(key)
key = strings.TrimSpace(key)
val := kv[1]
val = strings.TrimSpace(val)
var err error
switch key {
case "type":
mp.Type = val
case "keytype":
mp.KeyType = val
case "valuetype":
mp.ValueType = val
case "convertedtype":
mp.ConvertedType = val
case "keyconvertedtype":
mp.KeyConvertedType = val
case "valueconvertedtype":
mp.ValueConvertedType = val
case "length":
if mp.Length, err = Str2Int32(val); err != nil {
return nil, fmt.Errorf("failed to parse length: %s", err.Error())
}
case "keylength":
if mp.KeyLength, err = Str2Int32(val); err != nil {
return nil, fmt.Errorf("failed to parse keylength: %s", err.Error())
}
case "valuelength":
if mp.ValueLength, err = Str2Int32(val); err != nil {
return nil, fmt.Errorf("failed to parse valuelength: %s", err.Error())
}
case "scale":
if mp.Scale, err = Str2Int32(val); err != nil {
return nil, fmt.Errorf("failed to parse scale: %s", err.Error())
}
case "keyscale":
if mp.KeyScale, err = Str2Int32(val); err != nil {
return nil, fmt.Errorf("failed to parse keyscale: %s", err.Error())
}
case "valuescale":
if mp.ValueScale, err = Str2Int32(val); err != nil {
return nil, fmt.Errorf("failed to parse valuescale: %s", err.Error())
}
case "precision":
if mp.Precision, err = Str2Int32(val); err != nil {
return nil, fmt.Errorf("failed to parse precision: %s", err.Error())
}
case "keyprecision":
if mp.KeyPrecision, err = Str2Int32(val); err != nil {
return nil, fmt.Errorf("failed to parse keyprecision: %s", err.Error())
}
case "valueprecision":
if mp.ValuePrecision, err = Str2Int32(val); err != nil {
return nil, fmt.Errorf("failed to parse valueprecision: %s", err.Error())
}
case "fieldid":
if mp.FieldID, err = Str2Int32(val); err != nil {
return nil, fmt.Errorf("failed to parse fieldid: %s", err.Error())
}
case "keyfieldid":
if mp.KeyFieldID, err = Str2Int32(val); err != nil {
return nil, fmt.Errorf("failed to parse keyfieldid: %s", err.Error())
}
case "valuefieldid":
if mp.ValueFieldID, err = Str2Int32(val); err != nil {
return nil, fmt.Errorf("failed to parse valuefieldid: %s", err.Error())
}
case "isadjustedtoutc":
if mp.IsAdjustedToUTC, err = Str2Bool(val); err != nil {
return nil, fmt.Errorf("failed to parse isadjustedtoutc: %s", err.Error())
}
case "keyisadjustedtoutc":
if mp.KeyIsAdjustedToUTC, err = Str2Bool(val); err != nil {
return nil, fmt.Errorf("failed to parse keyisadjustedtoutc: %s", err.Error())
}
case "valueisadjustedtoutc":
if mp.ValueIsAdjustedToUTC, err = Str2Bool(val); err != nil {
return nil, fmt.Errorf("failed to parse valueisadjustedtoutc: %s", err.Error())
}
case "name":
if mp.InName == "" {
mp.InName = StringToVariableName(val)
}
mp.ExName = val
case "inname":
mp.InName = val
case "omitstats":
if mp.OmitStats, err = Str2Bool(val); err != nil {
return nil, fmt.Errorf("failed to parse omitstats: %s", err.Error())
}
case "keyomitstats":
if mp.KeyOmitStats, err = Str2Bool(val); err != nil {
return nil, fmt.Errorf("failed to parse keyomitstats: %s", err.Error())
}
case "valueomitstats":
if mp.ValueOmitStats, err = Str2Bool(val); err != nil {
return nil, fmt.Errorf("failed to parse valueomitstats: %s", err.Error())
}
case "repetitiontype":
switch strings.ToLower(val) {
case "repeated":
mp.RepetitionType = parquet.FieldRepetitionType_REPEATED
case "required":
mp.RepetitionType = parquet.FieldRepetitionType_REQUIRED
case "optional":
mp.RepetitionType = parquet.FieldRepetitionType_OPTIONAL
default:
return nil, fmt.Errorf("unknown repetitiontype: '%v'", val)
}
case "keyrepetitiontype":
switch strings.ToLower(val) {
case "repeated":
mp.KeyRepetitionType = parquet.FieldRepetitionType_REPEATED
case "required":
mp.KeyRepetitionType = parquet.FieldRepetitionType_REQUIRED
case "optional":
mp.KeyRepetitionType = parquet.FieldRepetitionType_OPTIONAL
default:
return nil, fmt.Errorf("unknown keyrepetitiontype: '%v'", val)
}
case "valuerepetitiontype":
switch strings.ToLower(val) {
case "repeated":
mp.ValueRepetitionType = parquet.FieldRepetitionType_REPEATED
case "required":
mp.ValueRepetitionType = parquet.FieldRepetitionType_REQUIRED
case "optional":
mp.ValueRepetitionType = parquet.FieldRepetitionType_OPTIONAL
default:
return nil, fmt.Errorf("unknown valuerepetitiontype: '%v'", val)
}
case "encoding":
switch strings.ToLower(val) {
case "plain":
mp.Encoding = parquet.Encoding_PLAIN
case "rle":
mp.Encoding = parquet.Encoding_RLE
case "delta_binary_packed":
mp.Encoding = parquet.Encoding_DELTA_BINARY_PACKED
case "delta_length_byte_array":
mp.Encoding = parquet.Encoding_DELTA_LENGTH_BYTE_ARRAY
case "delta_byte_array":
mp.Encoding = parquet.Encoding_DELTA_BYTE_ARRAY
case "plain_dictionary":
mp.Encoding = parquet.Encoding_PLAIN_DICTIONARY
case "rle_dictionary":
mp.Encoding = parquet.Encoding_RLE_DICTIONARY
case "byte_stream_split":
mp.Encoding = parquet.Encoding_BYTE_STREAM_SPLIT
default:
return nil, fmt.Errorf("unknown encoding type: '%v'", val)
}
case "keyencoding":
switch strings.ToLower(val) {
case "rle":
mp.KeyEncoding = parquet.Encoding_RLE
case "delta_binary_packed":
mp.KeyEncoding = parquet.Encoding_DELTA_BINARY_PACKED
case "delta_length_byte_array":
mp.KeyEncoding = parquet.Encoding_DELTA_LENGTH_BYTE_ARRAY
case "delta_byte_array":
mp.KeyEncoding = parquet.Encoding_DELTA_BYTE_ARRAY
case "plain_dictionary":
mp.KeyEncoding = parquet.Encoding_PLAIN_DICTIONARY
case "byte_stream_split":
mp.KeyEncoding = parquet.Encoding_BYTE_STREAM_SPLIT
default:
return nil, fmt.Errorf("unknown keyencoding type: '%v'", val)
}
case "valueencoding":
switch strings.ToLower(val) {
case "rle":
mp.ValueEncoding = parquet.Encoding_RLE
case "delta_binary_packed":
mp.ValueEncoding = parquet.Encoding_DELTA_BINARY_PACKED
case "delta_length_byte_array":
mp.ValueEncoding = parquet.Encoding_DELTA_LENGTH_BYTE_ARRAY
case "delta_byte_array":
mp.ValueEncoding = parquet.Encoding_DELTA_BYTE_ARRAY
case "plain_dictionary":
mp.ValueEncoding = parquet.Encoding_PLAIN_DICTIONARY
case "byte_stream_split":
mp.ValueEncoding = parquet.Encoding_BYTE_STREAM_SPLIT
default:
return nil, fmt.Errorf("unknown valueencoding type: '%v'", val)
}
default:
if strings.HasPrefix(key, "logicaltype") {
mp.LogicalTypeFields[key] = val
} else if strings.HasPrefix(key, "keylogicaltype") {
newKey := key[3:]
mp.KeyLogicalTypeFields[newKey] = val
} else if strings.HasPrefix(key, "valuelogicaltype") {
newKey := key[5:]
mp.ValueLogicalTypeFields[newKey] = val
} else {
return nil, fmt.Errorf("unrecognized tag '%v'", key)
}
}
}
return mp, nil
}
func NewSchemaElementFromTagMap(info *Tag) (*parquet.SchemaElement, error) {
schema := parquet.NewSchemaElement()
schema.Name = info.InName
schema.TypeLength = &info.Length
schema.Scale = &info.Scale
schema.Precision = &info.Precision
schema.FieldID = &info.FieldID
schema.RepetitionType = &info.RepetitionType
schema.NumChildren = nil
if t, err := parquet.TypeFromString(info.Type); err == nil {
schema.Type = &t
} else {
return nil, fmt.Errorf("type " + info.Type + ": " + err.Error())
}
if ct, err := parquet.ConvertedTypeFromString(info.ConvertedType); err == nil {
schema.ConvertedType = &ct
}
var logicalType *parquet.LogicalType
var err error
if len(info.LogicalTypeFields) > 0 {
logicalType, err = NewLogicalTypeFromFieldsMap(info.LogicalTypeFields)
if err != nil {
return nil, fmt.Errorf("failed to create logicaltype from field map: %s", err.Error())
}
} else {
logicalType = NewLogicalTypeFromConvertedType(schema, info)
}
schema.LogicalType = logicalType
return schema, nil
}
func NewLogicalTypeFromFieldsMap(mp map[string]string) (*parquet.LogicalType, error) {
if val, ok := mp["logicaltype"]; !ok {
return nil, errors.New("does not have logicaltype")
} else {
var err error
logicalType := parquet.NewLogicalType()
switch val {
case "STRING":
logicalType.STRING = parquet.NewStringType()
case "MAP":
logicalType.MAP = parquet.NewMapType()
case "LIST":
logicalType.LIST = parquet.NewListType()
case "ENUM":
logicalType.ENUM = parquet.NewEnumType()
case "DECIMAL":
logicalType.DECIMAL = parquet.NewDecimalType()
logicalType.DECIMAL.Precision, err = Str2Int32(mp["logicaltype.precision"])
if err != nil {
return nil, fmt.Errorf("cannot parse logicaltype.precision as int32: %s", err.Error())
}
logicalType.DECIMAL.Scale, err = Str2Int32(mp["logicaltype.scale"])
if err != nil {
return nil, fmt.Errorf("cannot parse logicaltype.scale as int32: %s", err.Error())
}
case "DATE":
logicalType.DATE = parquet.NewDateType()
case "TIME":
logicalType.TIME = parquet.NewTimeType()
logicalType.TIME.IsAdjustedToUTC, err = Str2Bool(mp["logicaltype.isadjustedtoutc"])
if err != nil {
return nil, fmt.Errorf("cannot parse logicaltype.isadjustedtoutc as boolean: %s", err.Error())
}
switch mp["logicaltype.unit"] {
case "MILLIS":
logicalType.TIME.Unit = parquet.NewTimeUnit()
logicalType.TIME.Unit.MILLIS = parquet.NewMilliSeconds()
case "MICROS":
logicalType.TIME.Unit = parquet.NewTimeUnit()
logicalType.TIME.Unit.MICROS = parquet.NewMicroSeconds()
case "NANOS":
logicalType.TIME.Unit = parquet.NewTimeUnit()
logicalType.TIME.Unit.NANOS = parquet.NewNanoSeconds()
default:
return nil, fmt.Errorf("logicaltype time error, unknown unit: %s", mp["logicaltype.unit"])
}
case "TIMESTAMP":
logicalType.TIMESTAMP = parquet.NewTimestampType()
logicalType.TIMESTAMP.IsAdjustedToUTC, err = Str2Bool(mp["logicaltype.isadjustedtoutc"])
if err != nil {
return nil, fmt.Errorf("cannot parse logicaltype.isadjustedtoutc as boolean: %s", err.Error())
}
switch mp["logicaltype.unit"] {
case "MILLIS":
logicalType.TIMESTAMP.Unit = parquet.NewTimeUnit()
logicalType.TIMESTAMP.Unit.MILLIS = parquet.NewMilliSeconds()
case "MICROS":
logicalType.TIMESTAMP.Unit = parquet.NewTimeUnit()
logicalType.TIMESTAMP.Unit.MICROS = parquet.NewMicroSeconds()
case "NANOS":
logicalType.TIMESTAMP.Unit = parquet.NewTimeUnit()
logicalType.TIMESTAMP.Unit.NANOS = parquet.NewNanoSeconds()
default:
return nil, fmt.Errorf("logicaltype time error, unknown unit: %s", mp["logicaltype.unit"])
}
case "INTEGER":
logicalType.INTEGER = parquet.NewIntType()
bitWidth, err := Str2Int32(mp["logicaltype.bitwidth"])
if err != nil {
return nil, fmt.Errorf("cannot parse logicaltype.bitwidth as int32: %s", err.Error())
}
logicalType.INTEGER.BitWidth = int8(bitWidth)
logicalType.INTEGER.IsSigned, err = Str2Bool(mp["logicaltype.issigned"])
if err != nil {
return nil, fmt.Errorf("cannot parse logicaltype.issigned as boolean: %s", err.Error())
}
case "JSON":
logicalType.JSON = parquet.NewJsonType()
case "BSON":
logicalType.BSON = parquet.NewBsonType()
case "UUID":
logicalType.UUID = parquet.NewUUIDType()
default:
return nil, fmt.Errorf("unknow logicaltype: " + val)
}
return logicalType, nil
}
}
func NewLogicalTypeFromConvertedType(schemaElement *parquet.SchemaElement, info *Tag) *parquet.LogicalType {
_, ct := schemaElement.Type, schemaElement.ConvertedType
if ct == nil {
return nil
}
logicalType := parquet.NewLogicalType()
switch *ct {
case parquet.ConvertedType_INT_8:
logicalType.INTEGER = parquet.NewIntType()
logicalType.INTEGER.BitWidth = 8
logicalType.INTEGER.IsSigned = true
case parquet.ConvertedType_INT_16:
logicalType.INTEGER = parquet.NewIntType()
logicalType.INTEGER.BitWidth = 16
logicalType.INTEGER.IsSigned = true
case parquet.ConvertedType_INT_32:
logicalType.INTEGER = parquet.NewIntType()
logicalType.INTEGER.BitWidth = 32
logicalType.INTEGER.IsSigned = true
case parquet.ConvertedType_INT_64:
logicalType.INTEGER = parquet.NewIntType()
logicalType.INTEGER.BitWidth = 64
logicalType.INTEGER.IsSigned = true
case parquet.ConvertedType_UINT_8:
logicalType.INTEGER = parquet.NewIntType()
logicalType.INTEGER.BitWidth = 8
logicalType.INTEGER.IsSigned = false
case parquet.ConvertedType_UINT_16:
logicalType.INTEGER = parquet.NewIntType()
logicalType.INTEGER.BitWidth = 16
logicalType.INTEGER.IsSigned = false
case parquet.ConvertedType_UINT_32:
logicalType.INTEGER = parquet.NewIntType()
logicalType.INTEGER.BitWidth = 32
logicalType.INTEGER.IsSigned = false
case parquet.ConvertedType_UINT_64:
logicalType.INTEGER = parquet.NewIntType()
logicalType.INTEGER.BitWidth = 64
logicalType.INTEGER.IsSigned = false
case parquet.ConvertedType_DECIMAL:
logicalType.DECIMAL = parquet.NewDecimalType()
logicalType.DECIMAL.Precision = info.Precision
logicalType.DECIMAL.Scale = info.Scale
case parquet.ConvertedType_DATE:
logicalType.DATE = parquet.NewDateType()
case parquet.ConvertedType_TIME_MICROS:
logicalType.TIME = parquet.NewTimeType()
logicalType.TIME.IsAdjustedToUTC = info.IsAdjustedToUTC
logicalType.TIME.Unit = parquet.NewTimeUnit()
logicalType.TIME.Unit.MICROS = parquet.NewMicroSeconds()
case parquet.ConvertedType_TIME_MILLIS:
logicalType.TIME = parquet.NewTimeType()
logicalType.TIME.IsAdjustedToUTC = info.IsAdjustedToUTC
logicalType.TIME.Unit = parquet.NewTimeUnit()
logicalType.TIME.Unit.MILLIS = parquet.NewMilliSeconds()
case parquet.ConvertedType_TIMESTAMP_MICROS:
logicalType.TIMESTAMP = parquet.NewTimestampType()
logicalType.TIMESTAMP.IsAdjustedToUTC = info.IsAdjustedToUTC
logicalType.TIMESTAMP.Unit = parquet.NewTimeUnit()
logicalType.TIMESTAMP.Unit.MICROS = parquet.NewMicroSeconds()
case parquet.ConvertedType_TIMESTAMP_MILLIS:
logicalType.TIMESTAMP = parquet.NewTimestampType()
logicalType.TIMESTAMP.IsAdjustedToUTC = info.IsAdjustedToUTC
logicalType.TIMESTAMP.Unit = parquet.NewTimeUnit()
logicalType.TIMESTAMP.Unit.MILLIS = parquet.NewMilliSeconds()
case parquet.ConvertedType_BSON:
logicalType.BSON = parquet.NewBsonType()
case parquet.ConvertedType_ENUM:
logicalType.ENUM = parquet.NewEnumType()
case parquet.ConvertedType_JSON:
logicalType.JSON = parquet.NewJsonType()
case parquet.ConvertedType_LIST:
logicalType.LIST = parquet.NewListType()
case parquet.ConvertedType_MAP:
logicalType.MAP = parquet.NewMapType()
case parquet.ConvertedType_UTF8:
logicalType.STRING = parquet.NewStringType()
default:
return nil
}
return logicalType
}
func DeepCopy(src, dst interface{}) {
var buf bytes.Buffer
gob.NewEncoder(&buf).Encode(src)
gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst)
return
}
// Get key tag map for map
func GetKeyTagMap(src *Tag) *Tag {
res := NewTag()
res.InName = "Key"
res.ExName = "key"
res.Type = src.KeyType
res.ConvertedType = src.KeyConvertedType
res.IsAdjustedToUTC = src.KeyIsAdjustedToUTC
res.Length = src.KeyLength
res.Scale = src.KeyScale
res.Precision = src.KeyPrecision
res.FieldID = src.KeyFieldID
res.Encoding = src.KeyEncoding
res.OmitStats = src.KeyOmitStats
res.RepetitionType = parquet.FieldRepetitionType_REQUIRED
return res
}
// Get value tag map for map
func GetValueTagMap(src *Tag) *Tag {
res := NewTag()
res.InName = "Value"
res.ExName = "value"
res.Type = src.ValueType
res.ConvertedType = src.ValueConvertedType
res.IsAdjustedToUTC = src.ValueIsAdjustedToUTC
res.Length = src.ValueLength
res.Scale = src.ValueScale
res.Precision = src.ValuePrecision
res.FieldID = src.ValueFieldID
res.Encoding = src.ValueEncoding
res.OmitStats = src.ValueOmitStats
res.RepetitionType = src.ValueRepetitionType
return res
}
// Convert string to a golang variable name
func StringToVariableName(str string) string {
ln := len(str)
if ln <= 0 {
return str
}
name := ""
for i := 0; i < ln; i++ {
c := str[i]
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' {
name += string(c)
} else {
name += strconv.Itoa(int(c))
}
}
name = HeadToUpper(name)
return name
}
// Convert the first letter of a string to uppercase
func HeadToUpper(str string) string {
ln := len(str)
if ln <= 0 {
return str
}
c := str[0]
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') {
return strings.ToUpper(str[0:1]) + str[1:]
}
//handle non-alpha prefix such as "_"
return "PARGO_PREFIX_" + str
}
func CmpIntBinary(as string, bs string, order string, signed bool) bool {
abs, bbs := []byte(as), []byte(bs)
la, lb := len(abs), len(bbs)
if order == "LittleEndian" {
for i, j := 0, len(abs)-1; i < j; i, j = i+1, j-1 {
abs[i], abs[j] = abs[j], abs[i]
}
for i, j := 0, len(bbs)-1; i < j; i, j = i+1, j-1 {
bbs[i], bbs[j] = bbs[j], bbs[i]
}
}
if !signed {
if la < lb {
abs = append(make([]byte, lb-la), abs...)
} else if lb < la {
bbs = append(make([]byte, la-lb), bbs...)
}
} else {
if la < lb {
sb := (abs[0] >> 7) & 1
pre := make([]byte, lb-la)
if sb == 1 {
for i := 0; i < lb-la; i++ {
pre[i] = byte(0xFF)
}
}
abs = append(pre, abs...)
} else if la > lb {
sb := (bbs[0] >> 7) & 1
pre := make([]byte, la-lb)
if sb == 1 {
for i := 0; i < la-lb; i++ {
pre[i] = byte(0xFF)
}
}
bbs = append(pre, bbs...)
}
asb, bsb := (abs[0]>>7)&1, (bbs[0]>>7)&1
if asb < bsb {
return false
} else if asb > bsb {
return true
}
}
for i := 0; i < len(abs); i++ {
if abs[i] < bbs[i] {
return true
} else if abs[i] > bbs[i] {
return false
}
}
return false
}
func FindFuncTable(pT *parquet.Type, cT *parquet.ConvertedType, logT *parquet.LogicalType) FuncTable {
if cT == nil && logT == nil {
if *pT == parquet.Type_BOOLEAN {
return boolFuncTable{}
} else if *pT == parquet.Type_INT32 {
return int32FuncTable{}
} else if *pT == parquet.Type_INT64 {
return int64FuncTable{}
} else if *pT == parquet.Type_INT96 {
return int96FuncTable{}
} else if *pT == parquet.Type_FLOAT {
return float32FuncTable{}
} else if *pT == parquet.Type_DOUBLE {
return float64FuncTable{}
} else if *pT == parquet.Type_BYTE_ARRAY {
return stringFuncTable{}
} else if *pT == parquet.Type_FIXED_LEN_BYTE_ARRAY {
return stringFuncTable{}
}
}
if cT != nil {
if *cT == parquet.ConvertedType_UTF8 || *cT == parquet.ConvertedType_BSON || *cT == parquet.ConvertedType_JSON || *cT == parquet.ConvertedType_ENUM {
return stringFuncTable{}
} else if *cT == parquet.ConvertedType_INT_8 || *cT == parquet.ConvertedType_INT_16 || *cT == parquet.ConvertedType_INT_32 ||
*cT == parquet.ConvertedType_DATE || *cT == parquet.ConvertedType_TIME_MILLIS {
return int32FuncTable{}
} else if *cT == parquet.ConvertedType_UINT_8 || *cT == parquet.ConvertedType_UINT_16 || *cT == parquet.ConvertedType_UINT_32 {
return uint32FuncTable{}
} else if *cT == parquet.ConvertedType_INT_64 || *cT == parquet.ConvertedType_TIME_MICROS ||
*cT == parquet.ConvertedType_TIMESTAMP_MILLIS || *cT == parquet.ConvertedType_TIMESTAMP_MICROS {
return int64FuncTable{}
} else if *cT == parquet.ConvertedType_UINT_64 {
return uint64FuncTable{}
} else if *cT == parquet.ConvertedType_INTERVAL {
return intervalFuncTable{}
} else if *cT == parquet.ConvertedType_DECIMAL {
if *pT == parquet.Type_BYTE_ARRAY || *pT == parquet.Type_FIXED_LEN_BYTE_ARRAY {
return decimalStringFuncTable{}
} else if *pT == parquet.Type_INT32 {
return int32FuncTable{}
} else if *pT == parquet.Type_INT64 {
return int64FuncTable{}
}
}
}
if logT != nil {
if logT.TIME != nil || logT.TIMESTAMP != nil {
return FindFuncTable(pT, nil, nil)
} else if logT.DATE != nil {
return int32FuncTable{}
} else if logT.INTEGER != nil {
if logT.INTEGER.IsSigned {
return FindFuncTable(pT, nil, nil)
} else {
if *pT == parquet.Type_INT32 {
return uint32FuncTable{}
} else if *pT == parquet.Type_INT64 {
return uint64FuncTable{}
}
}
} else if logT.DECIMAL != nil {
if *pT == parquet.Type_BYTE_ARRAY || *pT == parquet.Type_FIXED_LEN_BYTE_ARRAY {
return decimalStringFuncTable{}
} else if *pT == parquet.Type_INT32 {
return int32FuncTable{}
} else if *pT == parquet.Type_INT64 {
return int64FuncTable{}
}
} else if logT.BSON != nil || logT.JSON != nil || logT.STRING != nil || logT.UUID != nil {
return stringFuncTable{}
}
}
panic("No known func table in FindFuncTable")
}
func Str2Int32(val string) (int32, error) {
valInt, err := strconv.Atoi(val)
if err != nil {
return 0, err
}
return int32(valInt), nil
}
func Str2Bool(val string) (bool, error) {
valBoolean, err := strconv.ParseBool(val)
if err != nil {
return false, err
}
return valBoolean, nil
}
type FuncTable interface {
LessThan(a interface{}, b interface{}) bool
MinMaxSize(minVal interface{}, maxVal interface{}, val interface{}) (interface{}, interface{}, int32)
}
func Min(table FuncTable, a interface{}, b interface{}) interface{} {
if a == nil {
return b
}
if b == nil {
return a
}
if table.LessThan(a, b) {
return a
} else {
return b
}
}
func Max(table FuncTable, a interface{}, b interface{}) interface{} {
if a == nil {
return b
}
if b == nil {
return a
}
if table.LessThan(a, b) {
return b
} else {
return a
}
}
type boolFuncTable struct{}
func (_ boolFuncTable) LessThan(a interface{}, b interface{}) bool {
return !a.(bool) && b.(bool)
}
func (table boolFuncTable) MinMaxSize(minVal interface{}, maxVal interface{}, val interface{}) (interface{}, interface{}, int32) {
return Min(table, minVal, val), Max(table, maxVal, val), 1
}
type int32FuncTable struct{}
func (_ int32FuncTable) LessThan(a interface{}, b interface{}) bool {
return a.(int32) < b.(int32)
}
func (table int32FuncTable) MinMaxSize(minVal interface{}, maxVal interface{}, val interface{}) (interface{}, interface{}, int32) {
return Min(table, minVal, val), Max(table, maxVal, val), 4
}
type uint32FuncTable struct{}
func (_ uint32FuncTable) LessThan(a interface{}, b interface{}) bool {
return uint32(a.(int32)) < uint32(b.(int32))
}
func (table uint32FuncTable) MinMaxSize(minVal interface{}, maxVal interface{}, val interface{}) (interface{}, interface{}, int32) {
return Min(table, minVal, val), Max(table, maxVal, val), 4
}
type int64FuncTable struct{}
func (_ int64FuncTable) LessThan(a interface{}, b interface{}) bool {
return a.(int64) < b.(int64)
}
func (table int64FuncTable) MinMaxSize(minVal interface{}, maxVal interface{}, val interface{}) (interface{}, interface{}, int32) {
return Min(table, minVal, val), Max(table, maxVal, val), 8
}
type uint64FuncTable struct{}
func (_ uint64FuncTable) LessThan(a interface{}, b interface{}) bool {
return uint64(a.(int64)) < uint64(b.(int64))
}
func (table uint64FuncTable) MinMaxSize(minVal interface{}, maxVal interface{}, val interface{}) (interface{}, interface{}, int32) {
return Min(table, minVal, val), Max(table, maxVal, val), 8
}
type int96FuncTable struct{}
func (_ int96FuncTable) LessThan(ai interface{}, bi interface{}) bool {
a, b := []byte(ai.(string)), []byte(bi.(string))
fa, fb := a[11]>>7, b[11]>>7
if fa > fb {
return true
} else if fa < fb {
return false
}
for i := 11; i >= 0; i-- {
if a[i] < b[i] {
return true
} else if a[i] > b[i] {
return false
}
}
return false
}
func (table int96FuncTable) MinMaxSize(minVal interface{}, maxVal interface{}, val interface{}) (interface{}, interface{}, int32) {
return Min(table, minVal, val), Max(table, maxVal, val), int32(len(val.(string)))
}
type float32FuncTable struct{}
func (_ float32FuncTable) LessThan(a interface{}, b interface{}) bool {
return a.(float32) < b.(float32)
}
func (table float32FuncTable) MinMaxSize(minVal interface{}, maxVal interface{}, val interface{}) (interface{}, interface{}, int32) {
return Min(table, minVal, val), Max(table, maxVal, val), 4
}
type float64FuncTable struct{}
func (_ float64FuncTable) LessThan(a interface{}, b interface{}) bool {
return a.(float64) < b.(float64)
}
func (table float64FuncTable) MinMaxSize(minVal interface{}, maxVal interface{}, val interface{}) (interface{}, interface{}, int32) {
return Min(table, minVal, val), Max(table, maxVal, val), 8
}
type stringFuncTable struct{}
func (_ stringFuncTable) LessThan(a interface{}, b interface{}) bool {
return a.(string) < b.(string)
}
func (table stringFuncTable) MinMaxSize(minVal interface{}, maxVal interface{}, val interface{}) (interface{}, interface{}, int32) {
return Min(table, minVal, val), Max(table, maxVal, val), int32(len(val.(string)))
}
type intervalFuncTable struct{}
func (_ intervalFuncTable) LessThan(ai interface{}, bi interface{}) bool {
a, b := []byte(ai.(string)), []byte(bi.(string))
for i := 11; i >= 0; i-- {
if a[i] > b[i] {
return false
} else if a[i] < b[i] {
return true
}
}
return false
}
func (table intervalFuncTable) MinMaxSize(minVal interface{}, maxVal interface{}, val interface{}) (interface{}, interface{}, int32) {
return Min(table, minVal, val), Max(table, maxVal, val), int32(len(val.(string)))
}
type decimalStringFuncTable struct{}
func (_ decimalStringFuncTable) LessThan(a interface{}, b interface{}) bool {
return CmpIntBinary(a.(string), b.(string), "BigEndian", true)
}
func (table decimalStringFuncTable) MinMaxSize(minVal interface{}, maxVal interface{}, val interface{}) (interface{}, interface{}, int32) {
return Min(table, minVal, val), Max(table, maxVal, val), int32(len(val.(string)))
}
// Get the size of a parquet value
func SizeOf(val reflect.Value) int64 {
var size int64
switch val.Type().Kind() {
case reflect.Ptr:
if val.IsNil() {
return 0
}
return SizeOf(val.Elem())
case reflect.Slice:
for i := 0; i < val.Len(); i++ {
size += SizeOf(val.Index(i))
}
return size
case reflect.Struct:
for i := 0; i < val.Type().NumField(); i++ {
size += SizeOf(val.Field(i))
}
return size
case reflect.Map:
keys := val.MapKeys()
for i := 0; i < len(keys); i++ {
size += SizeOf(keys[i])
size += SizeOf(val.MapIndex(keys[i]))
}
return size
case reflect.Bool:
return 1
case reflect.Int32:
return 4
case reflect.Int64:
return 8
case reflect.String:
return int64(val.Len())
case reflect.Float32:
return 4
case reflect.Float64:
return 8
}
return 4
}
const PAR_GO_PATH_DELIMITER = "\x01"
// . -> \x01
func ReformPathStr(pathStr string) string {
return strings.ReplaceAll(pathStr, ".", "\x01")
}
// Convert path slice to string
func PathToStr(path []string) string {
return strings.Join(path, PAR_GO_PATH_DELIMITER)
}
// Convert string to path slice
func StrToPath(str string) []string {
return strings.Split(str, PAR_GO_PATH_DELIMITER)
}
// Get the pathStr index in a path
func PathStrIndex(str string) int {
return len(strings.Split(str, PAR_GO_PATH_DELIMITER))
}
func IsChildPath(parent, child string) bool {
ln := len(parent)
return strings.HasPrefix(child, parent) && (len(child) == ln || child[ln] == PAR_GO_PATH_DELIMITER[0])
}