-
Notifications
You must be signed in to change notification settings - Fork 0
/
capnpparquet.h
2610 lines (2261 loc) · 89.1 KB
/
capnpparquet.h
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 2017 Rene Sugar
*
* 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.
*/
/*
* @file capnpparquet.h
* @author Rene Sugar <rene.sugar@gmail.com>
* @brief Convert a Cap'n Proto schema to a Parquet schema.
*/
#ifndef _CAPNPPARQUET_H_
#define _CAPNPPARQUET_H_
#include <parquet/types.h>
#include <parquet/util/macros.h>
#include <parquet/util/visibility.h>
#include <parquet/schema.h>
#include <algorithm>
#include <cstdint>
#include <cmath>
#include <memory>
#include <ostream>
#include <string>
#include <unordered_map>
#include <vector>
#include "capnpgeneric.h"
/*
Cap'n Proto Types Parquet Physical Type Parquet Logical Type Thrift Type Arrow Type
----------------- --------------------- -------------------- ----------- -----------------
VOID, FIXED_LEN_BYTE_ARRAY, NONE, VOID, FIXED_SIZE_BINARY
BOOL, BOOLEAN, NONE, BOOL, BOOL
INT8, INT32, INT_8, I8, INT8
INT16, INT32, INT_16, I16, INT16
INT32, INT32, INT_32, I32, INT32
INT64, INT64, INT_64, I64, INT64
UINT8, INT32, UINT_8, I32, UINT8
UINT16, INT32, UINT_16, I32, UINT16
UINT32, INT64, UINT_32, I64, UINT32
UINT64, INT64, UINT_64, I64, UINT64
FLOAT32, FLOAT, NONE, DOUBLE, FLOAT
FLOAT64, DOUBLE, NONE, DOUBLE, DOUBLE
TEXT, BYTE_ARRAY, UTF8, STRING, STRING
DATA, BYTE_ARRAY, NONE, BINARY, BINARY
LIST, BYTE_ARRAY, LIST, LIST, LIST
ENUM, BYTE_ARRAY, ENUM, ENUM, STRING
STRUCT, BYTE_ARRAY, NONE, STRUCT, STRUCT
INTERFACE, * * SERVICE, *
ANY_POINTER, * * VOID *
DATA, BYTE_ARRAY, BSON, BINARY, BINARY
TEXT, BYTE_ARRAY, JSON, STRING, STRING
SET, BYTE_ARRAY, LIST, SET, LIST
MAP, BYTE_ARRAY, MAP, MAP, DICTIONARY
FLOAT64, FIXED_LEN_BYTE_ARRAY, DECIMAL (see (1), (9)) DOUBLE, DECIMAL
INT32, INT32, DATE, INT32, DATE32
INT32, INT32, DATE, INT32, DATE64
INT32, INT32, TIME_MILLIS, INT32, TIME32
INT64, INT64, TIME_MICROS, INT64, TIME64
INT64, INT64, TIMESTAMP_MILLIS, INT64, TIMESTAMP(TimeUnit::MILLI)
INT64 INT64, TIMESTAMP_MICROS, INT64, TIMESTAMP(TimeUnit::MICRO)
*, *, *, *, TIMESTAMP(TimeUnit::NANO)
*, *, *, *, TIMESTAMP(TimeUnit::SECOND)
DATA, FIXED_LEN_BYTE_ARRAY(12) INTERVAL, BINARY, INTERVAL
STRUCT, BYTE_ARRAY, MAP_KEY_VALUE, STRUCT, STRUCT
UNION, *, *, *, UNION
*, *, *, *, NA
*, *, *, *, HALF_FLOAT
References:
(1) https://github.com/apache/parquet-format/blob/master/LogicalTypes.md
(2) https://diwakergupta.github.io/thrift-missing-guide/
(3) http://thrift.apache.org/docs/idl
(4) http://thrift.apache.org/tutorial/
(5) https://github.com/apache/thrift/blob/master/compiler/cpp/src/thrift/thriftl.ll
(6) https://github.com/apache/thrift/blob/master/compiler/cpp/src/thrift/thrifty.yy
(7) https://github.com/apache/parquet-cpp/blob/master/src/parquet/arrow/arrow-reader-writer-test.cc
(8) https://gerrit.cloudera.org/#/c/6550/9/fe/src/main/java/org/apache/impala/analysis/CreateTableLikeFileStmt.java
(9) https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetSchemaConverter.scala
(10) https://github.com/Parquet/parquet-mr/blob/master/parquet-avro/src/main/java/parquet/avro/AvroSchemaConverter.java
(11) https://avro.apache.org/docs/current/spec.html#Enums
(12) https://thrift.apache.org/docs/idl
(13) https://github.com/apache/thrift/blob/master/lib/json/schema.json
(14) https://github.com/apache/parquet-cpp/blob/master/src/parquet/schema.cc
(15) https://github.com/Parquet/parquet-mr/blob/master/parquet-avro/src/main/java/parquet/avro/package-info.java
(16) https://github.com/Parquet/parquet-mr/blob/master/parquet-avro/src/main/java/parquet/avro/AvroWriteSupport.java
(17) https://www.cloudera.com/documentation/enterprise/5-5-x/topics/impala_complex_types.html#complex_types_nesting
(18) https://github.com/apache/arrow/blob/master/cpp/src/arrow/type.h
Excerpt from (9) (covers decimal, timestamp, arrays and maps)
DECIMAL
-------
// ======================
// Decimals (legacy mode)
// ======================
// Spark 1.4.x and prior versions only support decimals with a maximum precision of 18 and
// always store decimals in fixed-length byte arrays. To keep compatibility with these older
// versions, here we convert decimals with all precisions to `FIXED_LEN_BYTE_ARRAY` annotated
// by `DECIMAL`.
// ========================
// Decimals (standard mode)
// ========================
// Uses INT32 for 1 <= precision <= 9 (if precision <= Decimal.MAX_INT_DIGITS)
// Uses INT64 for 1 <= precision <= 18 (if precision <= Decimal.MAX_LONG_DIGITS)
// Uses FIXED_LEN_BYTE_ARRAY for all other precisions
TIMESTAMP
---------
// NOTE: Spark SQL TimestampType is NOT a well defined type in Parquet format spec.
//
// As stated in PARQUET-323, Parquet `INT96` was originally introduced to represent nanosecond
// timestamp in Impala for some historical reasons. It's not recommended to be used for any
// other types and will probably be deprecated in some future version of parquet-format spec.
// That's the reason why parquet-format spec only defines `TIMESTAMP_MILLIS` and
// `TIMESTAMP_MICROS` which are both logical types annotating `INT64`.
//
// Originally, Spark SQL uses the same nanosecond timestamp type as Impala and Hive. Starting
// from Spark 1.5.0, we resort to a timestamp type with 100 ns precision so that we can store
// a timestamp into a `Long`. This design decision is subject to change though, for example,
// we may resort to microsecond precision in the future.
//
// For Parquet, we plan to write all `TimestampType` value as `TIMESTAMP_MICROS`, but it's
// currently not implemented yet because parquet-mr 1.8.1 (the version we're currently using)
// hasn't implemented `TIMESTAMP_MICROS` yet, however it supports TIMESTAMP_MILLIS. We will
// encode timestamp values as TIMESTAMP_MILLIS annotating INT64 if
// 'spark.sql.parquet.int64AsTimestampMillis' is set.
//
// TODO Converts `TIMESTAMP_MICROS` once parquet-mr implements that.
ARRAYS AND MAPS
---------------
// ===================================
// ArrayType and MapType (legacy mode)
// ===================================
//
// "bag", "array", "list", "element", "key_value", "map", "key", "value" below are the names
// used in the Parquet schema nodes.
//
// Spark 1.4.x and prior versions convert `ArrayType` with nullable elements into a 3-level
// `LIST` structure. This behavior is somewhat a hybrid of parquet-hive and parquet-avro
// (1.6.0rc3): the 3-level structure is similar to parquet-hive while the 3rd level element
// field name "array" is borrowed from parquet-avro.
case ArrayType(elementType, nullable @ true) if writeLegacyParquetFormat =>
// <list-repetition> group <name> (LIST) {
// optional group bag {
// repeated <element-type> array;
// }
// }
//
// This should not use `listOfElements` here because this new method checks if the
// element name is `element` in the `GroupType` and throws an exception if not.
// As mentioned above, Spark prior to 1.4.x writes `ArrayType` as `LIST` but with
// `array` as its element name as below. Therefore, we build manually
// the correct group type here via the builder. (See SPARK-16777)
//
// Spark 1.4.x and prior versions convert ArrayType with non-nullable elements into a 2-level
// LIST structure. This behavior mimics parquet-avro (1.6.0rc3). Note that this case is
// covered by the backwards-compatibility rules implemented in `isElementType()`.
//
// <list-repetition> group <name> (LIST) {
// repeated <element-type> array;
// }
//
// Here too, we should not use `listOfElements`. (See SPARK-16777)
//
// "array" is the name chosen by parquet-avro (1.7.0 and prior version)
//
// Spark 1.4.x and prior versions convert MapType into a 3-level group annotated by
// MAP_KEY_VALUE. This is covered by `convertGroupField(field: GroupType): DataType`.
//
// <map-repetition> group <name> (MAP) {
// repeated group map (MAP_KEY_VALUE) {
// required <key-type> key;
// <value-repetition> <value-type> value;
// }
// }
//
// =====================================
// ArrayType and MapType (standard mode)
// =====================================
//
// <list-repetition> group <name> (LIST) {
// repeated group list {
// <element-repetition> <element-type> element;
// }
// }
//
// <map-repetition> group <name> (MAP) {
// repeated group key_value {
// required <key-type> key;
// <value-repetition> <value-type> value;
// }
// }
Excerpt from (18):
// static inline int decimal_byte_width(int precision) {
// if (precision >= 0 && precision < 10) {
// return 4;
// } else if (precision >= 10 && precision < 19) {
// return 8;
// } else {
// // TODO(phillipc): validate that we can't construct > 128 bit types
// return 16;
// }
// }
// case TimeUnit::SECOND:
// case TimeUnit::MILLI:
// case TimeUnit::MICRO:
// case TimeUnit::NANO:
*/
/*
Number of bits required to represent a DECIMAL number for a given precision (number of digits)
bmax = ceil(d*(log(10)/log(2)))
bmax = ceil(d*log2(10))
Find the log base 2 of an integer with the MSB N set in O(N) operations (the obvious way)
unsigned int v; // 32-bit word to find the log base 2 of
unsigned int r = 0; // r will be lg(v)
while (v >>= 1) // unroll for more speed...
{
r++;
}
The log base 2 of an integer is the same as the position of the highest bit set (or most significant bit set, MSB)
The following log base 2 methods are faster than this one.
References:
(1) http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
(2) http://www.exploringbinary.com/number-of-bits-in-a-decimal-integer/
(3) https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetSchemaConverter.scala
*/
// TODO(renesugar): Add support for generic structures in capnpgeneric.h
//
// TODO(renesugar): Add support for Cap'n Proto groups in capnpgeneric.h
// https://capnproto.org/language.html#structs
//
namespace capnpparquet {
// Cog code generation tool: https://nedbatchelder.com/code/cog/
//
// cog.py-2.7 -r capnpparquet.h
//
/*[[[cog
def capitalize(name):
return ''.join(s[0].upper() + s[1:] for s in name.split('_'))
dict = {
'repetition_type': [
('is_required', '', 'bool', '', '', 'parquet::Repetition::REQUIRED'),
('is_optional', '', 'bool', '', '', 'parquet::Repetition::OPTIONAL'),
('is_repeated', '', 'bool', '', '', 'parquet::Repetition::REPEATED'),
],
'parquet_node_type': [
('is_parquet', 'primitive', 'bool', '', '', 'parquet::schema::Node::PRIMITIVE'),
('is_parquet', 'group', 'bool', '', '', 'parquet::schema::Node::GROUP'),
],
'value': [
('value', 'bool', 'bool', 'bool', 'value', 'b'),
('value', 'i8', 'int8_t', 'int8_t', 'value', 'i8'),
('value', 'i16', 'int16_t', 'int16_t', 'value', 'i16'),
('value', 'i32', 'int32_t', 'int32_t', 'value', 'i32'),
('value', 'i64', 'int64_t', 'int64_t', 'value', 'i64'),
('value', 'ui8', 'uint8_t', 'uint8_t', 'value', 'ui8'),
('value', 'ui16', 'uint16_t', 'uint16_t', 'value', 'ui16'),
('value', 'ui32', 'uint32_t', 'uint32_t', 'value', 'ui32'),
('value', 'ui64', 'uint64_t', 'uint64_t', 'value', 'ui64'),
('value', 'float', 'float', 'float', 'value', 'f'),
('value', 'double', 'double', 'double', 'value', 'd'),
('value', 'string', 'std::string', 'std::string', 'value', 'string'),
('value', 'binary', 'std::vector<uint8_t>&', 'std::vector<uint8_t>&', 'value', 'binary')
]
}
types = [
# property get type set type param name param value
# -------- -------- -------- ---------- -----------
('node_type', 'ASTNode::type', 'ASTNode::type', 'type', 'type'),
('name', 'std::string', 'std::string', 'name', 'name'),
('capnp_type', 'capnp::schema::Type::Which', 'capnp::schema::Type::Which', 'type', 'capnp::schema::Type::VOID'),
('type_length', 'int32_t', 'int32_t', 'length', '-1'),
('repetition_type', 'parquet::Repetition::type', '', '', 'parquet::Repetition::OPTIONAL'),
('physical_type', 'parquet::Type::type', 'parquet::Type::type', 'type', 'parquet::Type::BYTE_ARRAY'),
('logical_type', 'parquet::LogicalType::type', 'parquet::LogicalType::type','type', 'parquet::LogicalType::NONE'),
('parquet_node_type', 'parquet::schema::Node::type', '', '', 'parquet::schema::Node::PRIMITIVE'),
('scale', 'int32_t', 'int32_t', 'scale', '-1'),
('precision', 'int32_t', 'int32_t', 'precision', '-1'),
('node_id', 'uint64_t', 'uint64_t', 'node_id', '0'),
('scope_id', 'uint64_t', 'uint64_t', 'scope_id', '0'),
('ordinal', 'uint16_t', 'uint16_t', 'ordinal', '0'),
('offset', 'uint32_t', 'uint32_t', 'offset', '0'),
('index', 'uint', 'uint', 'index', '0'),
('default_value_offset', 'uint32_t', 'uint32_t', 'offset', '0'),
('had_default_value', '', '', '', ''),
('unconstrained', '', '', '', ''),
('type_id', 'uint64_t', 'uint64_t', 'type_id', '0'),
('type_name', 'std::string', 'std::string', 'name', ''),
('enumerant_name', 'std::string', 'std::string', 'name', ''),
('schema_name', 'std::string', 'std::string', 'name', ''),
('parent', 'ASTNode*', 'ASTNode*', 'parent', 'nullptr'),
('node', 'parquet::schema::NodePtr', 'parquet::schema::NodePtr', 'node', 'nullptr'),
('decl', '', '', '', ''),
('decimal', '', '', '', ''),
('date', '', '', '', ''),
('time_millis', '', '', '', ''),
('time_micros', '', '', '', ''),
('timestamp_millis', '', '', '', ''),
('timestamp_micros', '', '', '', ''),
('bson', '', '', '', ''),
('json', '', '', '', ''),
('interval', '', '', '', ''),
('fixed_len_byte_array','', '', '', ''),
('map', '', '', '', ''),
('map_key_value', '', '', '', ''),
('list', '', '', '', ''),
('value', '', '', '', ''),
]
cog.outl('typedef struct _ASTNode__isset {')
cog.outl(' _ASTNode__isset() : ')
delim = ''
for property, get_type, set_type, param_name, param_value in types:
cog.outl(' %s %s(false)' % (delim, property.lower()))
delim = ','
cog.outl(' {}')
for property, get_type, set_type, param_name, param_value in types:
cog.outl(' bool %s :1;' % (property.lower()))
cog.outl('} _ASTNode__isset;')
cog.outl('')
cog.outl('')
cog.outl('typedef struct _value {')
values = dict['value']
for name, suffix, get_type, set_type, param_name, param_value in values:
cog.outl(' %s %s;' % (get_type.replace('&', '').ljust(20, ' '), param_value.rjust(10, ' ')))
cog.outl('} _ASTNodeValue;')
]]]*/
typedef struct _ASTNode__isset {
_ASTNode__isset() :
node_type(false)
, name(false)
, capnp_type(false)
, type_length(false)
, repetition_type(false)
, physical_type(false)
, logical_type(false)
, parquet_node_type(false)
, scale(false)
, precision(false)
, node_id(false)
, scope_id(false)
, ordinal(false)
, offset(false)
, index(false)
, default_value_offset(false)
, had_default_value(false)
, unconstrained(false)
, type_id(false)
, type_name(false)
, enumerant_name(false)
, schema_name(false)
, parent(false)
, node(false)
, decl(false)
, decimal(false)
, date(false)
, time_millis(false)
, time_micros(false)
, timestamp_millis(false)
, timestamp_micros(false)
, bson(false)
, json(false)
, interval(false)
, fixed_len_byte_array(false)
, map(false)
, map_key_value(false)
, list(false)
, value(false)
{}
bool node_type :1;
bool name :1;
bool capnp_type :1;
bool type_length :1;
bool repetition_type :1;
bool physical_type :1;
bool logical_type :1;
bool parquet_node_type :1;
bool scale :1;
bool precision :1;
bool node_id :1;
bool scope_id :1;
bool ordinal :1;
bool offset :1;
bool index :1;
bool default_value_offset :1;
bool had_default_value :1;
bool unconstrained :1;
bool type_id :1;
bool type_name :1;
bool enumerant_name :1;
bool schema_name :1;
bool parent :1;
bool node :1;
bool decl :1;
bool decimal :1;
bool date :1;
bool time_millis :1;
bool time_micros :1;
bool timestamp_millis :1;
bool timestamp_micros :1;
bool bson :1;
bool json :1;
bool interval :1;
bool fixed_len_byte_array :1;
bool map :1;
bool map_key_value :1;
bool list :1;
bool value :1;
} _ASTNode__isset;
typedef struct _value {
bool b;
int8_t i8;
int16_t i16;
int32_t i32;
int64_t i64;
uint8_t ui8;
uint16_t ui16;
uint32_t ui32;
uint64_t ui64;
float f;
double d;
std::string string;
std::vector<uint8_t> binary;
} _ASTNodeValue;
//[[[end]]]
// Convert lowerCamelCase and UpperCamelCase strings to lower_with_underscore.
// https://gist.github.com/rodamber/2558e25d4d8f6b9f2ffdf7bd49471340
std::string convertCamelCase(std::string camelCase) {
std::string str(1, tolower(camelCase[0]));
//printf("convertCamelCase: input: %s\n", camelCase.c_str());
// First place underscores between contiguous lower and upper case letters.
// For example, `_LowerCamelCase` becomes `_Lower_Camel_Case`.
for (auto it = camelCase.begin() + 1; it != camelCase.end(); ++it) {
if (isdigit(*it) && *(it-1) != '_' && islower(*(it-1))) {
str += "_";
} else if (isupper(*it) && *(it-1) != '_' && (isdigit(*(it-1)) || islower(*(it-1)))) {
str += "_";
}
str += *it;
}
// Then convert it to lower case.
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
//printf("convertCamelCase: output: %s\n", str.c_str());
return str;
}
class ASTNode {
public:
enum type {
NONE,
STRUCT,
INTERFACE,
GROUP,
ENUM,
FILE,
FIELD,
UNION,
ANNOTATION,
CONST,
PARAM,
METHOD,
ENUMERANT,
TYPE,
VALUE
};
ASTNode(ASTNode::type type, const std::string& name)
:
/*[[[cog
delim = ''
for property, get_type, set_type, param_name, param_value in types:
if (len(get_type) > 0) and (len(param_value) >0):
cog.outl(' %s%s_(%s)' % (delim, property.lower(), param_value))
delim = ', '
cog.outl(' {}')
]]]*/
node_type_(type)
, name_(name)
, capnp_type_(capnp::schema::Type::VOID)
, type_length_(-1)
, repetition_type_(parquet::Repetition::OPTIONAL)
, physical_type_(parquet::Type::BYTE_ARRAY)
, logical_type_(parquet::LogicalType::NONE)
, parquet_node_type_(parquet::schema::Node::PRIMITIVE)
, scale_(-1)
, precision_(-1)
, node_id_(0)
, scope_id_(0)
, ordinal_(0)
, offset_(0)
, index_(0)
, default_value_offset_(0)
, type_id_(0)
, parent_(nullptr)
, node_(nullptr)
{}
//[[[end]]]
~ASTNode() {
}
/*[[[cog
cog.outl('// Methods to check if a property is set')
cog.outl('')
for property, get_type, set_type, param_name, param_value in types:
cog.outl('bool is_%s() const { return __isset.%s == true; }' % (property.lower(),property.lower()))
]]]*/
// Methods to check if a property is set
bool is_node_type() const { return __isset.node_type == true; }
bool is_name() const { return __isset.name == true; }
bool is_capnp_type() const { return __isset.capnp_type == true; }
bool is_type_length() const { return __isset.type_length == true; }
bool is_repetition_type() const { return __isset.repetition_type == true; }
bool is_physical_type() const { return __isset.physical_type == true; }
bool is_logical_type() const { return __isset.logical_type == true; }
bool is_parquet_node_type() const { return __isset.parquet_node_type == true; }
bool is_scale() const { return __isset.scale == true; }
bool is_precision() const { return __isset.precision == true; }
bool is_node_id() const { return __isset.node_id == true; }
bool is_scope_id() const { return __isset.scope_id == true; }
bool is_ordinal() const { return __isset.ordinal == true; }
bool is_offset() const { return __isset.offset == true; }
bool is_index() const { return __isset.index == true; }
bool is_default_value_offset() const { return __isset.default_value_offset == true; }
bool is_had_default_value() const { return __isset.had_default_value == true; }
bool is_unconstrained() const { return __isset.unconstrained == true; }
bool is_type_id() const { return __isset.type_id == true; }
bool is_type_name() const { return __isset.type_name == true; }
bool is_enumerant_name() const { return __isset.enumerant_name == true; }
bool is_schema_name() const { return __isset.schema_name == true; }
bool is_parent() const { return __isset.parent == true; }
bool is_node() const { return __isset.node == true; }
bool is_decl() const { return __isset.decl == true; }
bool is_decimal() const { return __isset.decimal == true; }
bool is_date() const { return __isset.date == true; }
bool is_time_millis() const { return __isset.time_millis == true; }
bool is_time_micros() const { return __isset.time_micros == true; }
bool is_timestamp_millis() const { return __isset.timestamp_millis == true; }
bool is_timestamp_micros() const { return __isset.timestamp_micros == true; }
bool is_bson() const { return __isset.bson == true; }
bool is_json() const { return __isset.json == true; }
bool is_interval() const { return __isset.interval == true; }
bool is_fixed_len_byte_array() const { return __isset.fixed_len_byte_array == true; }
bool is_map() const { return __isset.map == true; }
bool is_map_key_value() const { return __isset.map_key_value == true; }
bool is_list() const { return __isset.list == true; }
bool is_value() const { return __isset.value == true; }
//[[[end]]]
bool is_target(std::string& target) {
if (targets_.count(target) > 0) {
return true;
}
return false;
}
/*[[[cog
cog.outl('// Methods to get a property')
for property, get_type, set_type, param_name, param_value in types:
if len(get_type) > 0:
cog.outl('')
cog.outl('%s %s() { return %s_; }' % (get_type, property.lower(), property.lower()))
cog.outl('')
]]]*/
// Methods to get a property
ASTNode::type node_type() { return node_type_; }
std::string name() { return name_; }
capnp::schema::Type::Which capnp_type() { return capnp_type_; }
int32_t type_length() { return type_length_; }
parquet::Repetition::type repetition_type() { return repetition_type_; }
parquet::Type::type physical_type() { return physical_type_; }
parquet::LogicalType::type logical_type() { return logical_type_; }
parquet::schema::Node::type parquet_node_type() { return parquet_node_type_; }
int32_t scale() { return scale_; }
int32_t precision() { return precision_; }
uint64_t node_id() { return node_id_; }
uint64_t scope_id() { return scope_id_; }
uint16_t ordinal() { return ordinal_; }
uint32_t offset() { return offset_; }
uint index() { return index_; }
uint32_t default_value_offset() { return default_value_offset_; }
uint64_t type_id() { return type_id_; }
std::string type_name() { return type_name_; }
std::string enumerant_name() { return enumerant_name_; }
std::string schema_name() { return schema_name_; }
ASTNode* parent() { return parent_; }
parquet::schema::NodePtr node() { return node_; }
//[[[end]]]
/*[[[cog
cog.outl('// Methods to get repetition type')
values = dict['repetition_type']
for name, suffix, get_type, set_type, param_name, param_value in values:
cog.outl('')
cog.outl('%s %s() const { return repetition_type_ == %s; }' % (get_type, name, param_value))
]]]*/
// Methods to get repetition type
bool is_required() const { return repetition_type_ == parquet::Repetition::REQUIRED; }
bool is_optional() const { return repetition_type_ == parquet::Repetition::OPTIONAL; }
bool is_repeated() const { return repetition_type_ == parquet::Repetition::REPEATED; }
//[[[end]]]
/*[[[cog
cog.outl('// Methods to get Parquet node type')
values = dict['parquet_node_type']
for name, suffix, get_type, set_type, param_name, param_value in values:
cog.outl('')
cog.outl('%s %s_%s() const { return parquet_node_type_ == %s; }' % (get_type, name, suffix, param_value))
]]]*/
// Methods to get Parquet node type
bool is_parquet_primitive() const { return parquet_node_type_ == parquet::schema::Node::PRIMITIVE; }
bool is_parquet_group() const { return parquet_node_type_ == parquet::schema::Node::GROUP; }
//[[[end]]]
/*[[[cog
cog.outl('// Methods to get value')
values = dict['value']
for name, suffix, get_type, set_type, param_name, param_value in values:
cog.outl('')
cog.outl('%s get%s%s() {' % (get_type, capitalize(name), suffix.upper()))
cog.outl(' return value_.%s;' % (param_value))
cog.outl('}')
]]]*/
// Methods to get value
bool getValueBOOL() {
return value_.b;
}
int8_t getValueI8() {
return value_.i8;
}
int16_t getValueI16() {
return value_.i16;
}
int32_t getValueI32() {
return value_.i32;
}
int64_t getValueI64() {
return value_.i64;
}
uint8_t getValueUI8() {
return value_.ui8;
}
uint16_t getValueUI16() {
return value_.ui16;
}
uint32_t getValueUI32() {
return value_.ui32;
}
uint64_t getValueUI64() {
return value_.ui64;
}
float getValueFLOAT() {
return value_.f;
}
double getValueDOUBLE() {
return value_.d;
}
std::string getValueSTRING() {
return value_.string;
}
std::vector<uint8_t>& getValueBINARY() {
return value_.binary;
}
//[[[end]]]
/*[[[cog
cog.outl('// Methods to set a property')
for property, get_type, set_type, param_name, param_value in types:
cog.outl('')
if len(set_type) > 0:
cog.outl('void set%s(%s %s) {' % (capitalize(property), set_type, param_name))
cog.outl(' %s_ = %s;' % (property, param_name))
cog.outl(' __isset.%s = true;' % (property))
cog.outl('}')
elif property == "had_default_value":
cog.outl('void set%s() {' % (capitalize(property)))
cog.outl(' __isset.%s = true;' % (property))
cog.outl('}')
elif len(get_type) == 0:
cog.outl('void setIs%s() {' % (capitalize(property)))
cog.outl(' __isset.%s = true;' % (property))
cog.outl('}')
cog.outl('')
]]]*/
// Methods to set a property
void setNodeType(ASTNode::type type) {
node_type_ = type;
__isset.node_type = true;
}
void setName(std::string name) {
name_ = name;
__isset.name = true;
}
void setCapnpType(capnp::schema::Type::Which type) {
capnp_type_ = type;
__isset.capnp_type = true;
}
void setTypeLength(int32_t length) {
type_length_ = length;
__isset.type_length = true;
}
void setPhysicalType(parquet::Type::type type) {
physical_type_ = type;
__isset.physical_type = true;
}
void setLogicalType(parquet::LogicalType::type type) {
logical_type_ = type;
__isset.logical_type = true;
}
void setScale(int32_t scale) {
scale_ = scale;
__isset.scale = true;
}
void setPrecision(int32_t precision) {
precision_ = precision;
__isset.precision = true;
}
void setNodeId(uint64_t node_id) {
node_id_ = node_id;
__isset.node_id = true;
}
void setScopeId(uint64_t scope_id) {
scope_id_ = scope_id;
__isset.scope_id = true;
}
void setOrdinal(uint16_t ordinal) {
ordinal_ = ordinal;
__isset.ordinal = true;
}
void setOffset(uint32_t offset) {
offset_ = offset;
__isset.offset = true;
}
void setIndex(uint index) {
index_ = index;
__isset.index = true;
}
void setDefaultValueOffset(uint32_t offset) {
default_value_offset_ = offset;
__isset.default_value_offset = true;
}
void setHadDefaultValue() {
__isset.had_default_value = true;
}
void setIsUnconstrained() {
__isset.unconstrained = true;
}
void setTypeId(uint64_t type_id) {
type_id_ = type_id;
__isset.type_id = true;
}
void setTypeName(std::string name) {
type_name_ = name;
__isset.type_name = true;
}
void setEnumerantName(std::string name) {
enumerant_name_ = name;
__isset.enumerant_name = true;
}
void setSchemaName(std::string name) {
schema_name_ = name;
__isset.schema_name = true;
}
void setParent(ASTNode* parent) {
parent_ = parent;
__isset.parent = true;
}
void setNode(parquet::schema::NodePtr node) {
node_ = node;
__isset.node = true;
}
void setIsDecl() {
__isset.decl = true;
}
void setIsDecimal() {
__isset.decimal = true;
}
void setIsDate() {
__isset.date = true;
}
void setIsTimeMillis() {
__isset.time_millis = true;
}
void setIsTimeMicros() {
__isset.time_micros = true;
}
void setIsTimestampMillis() {
__isset.timestamp_millis = true;
}
void setIsTimestampMicros() {
__isset.timestamp_micros = true;
}
void setIsBson() {
__isset.bson = true;
}
void setIsJson() {
__isset.json = true;
}
void setIsInterval() {
__isset.interval = true;
}
void setIsFixedLenByteArray() {
__isset.fixed_len_byte_array = true;
}
void setIsMap() {
__isset.map = true;
}
void setIsMapKeyValue() {
__isset.map_key_value = true;
}
void setIsList() {
__isset.list = true;
}
void setIsValue() {
__isset.value = true;
}
//[[[end]]]
/*[[[cog
cog.outl('// Methods to set repetition type')
values = dict['repetition_type']
for name, suffix, get_type, set_type, param_name, param_value in values:
cog.outl('')
cog.outl('void set%s%s() {' % (capitalize(name), suffix.upper()))
cog.outl(' repetition_type_ = %s;' % param_value)
cog.outl(' __isset.repetition_type = true;')
cog.outl('}')
]]]*/
// Methods to set repetition type
void setIsRequired() {
repetition_type_ = parquet::Repetition::REQUIRED;
__isset.repetition_type = true;
}
void setIsOptional() {
repetition_type_ = parquet::Repetition::OPTIONAL;
__isset.repetition_type = true;
}
void setIsRepeated() {
repetition_type_ = parquet::Repetition::REPEATED;
__isset.repetition_type = true;
}
//[[[end]]]
/*[[[cog
cog.outl('// Methods to set Parquet node type')
values = dict['parquet_node_type']
for name, suffix, get_type, set_type, param_name, param_value in values:
cog.outl('void set%s%s() {' % (capitalize(name), suffix.upper()))
cog.outl(' parquet_node_type_ = %s;' % param_value)
cog.outl(' __isset.parquet_node_type = true;')
cog.outl('}')
]]]*/
// Methods to set Parquet node type
void setIsParquetPRIMITIVE() {
parquet_node_type_ = parquet::schema::Node::PRIMITIVE;
__isset.parquet_node_type = true;
}
void setIsParquetGROUP() {
parquet_node_type_ = parquet::schema::Node::GROUP;
__isset.parquet_node_type = true;
}
//[[[end]]]
/*[[[cog
cog.outl('// Methods to set value')
values = dict['value']
for name, suffix, get_type, set_type, param_name, param_value in values:
cog.outl('')