-
Notifications
You must be signed in to change notification settings - Fork 9
/
cls_tabular_utils.cc
3879 lines (3530 loc) · 158 KB
/
cls_tabular_utils.cc
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 (C) 2018 The Regents of the University of California
* All Rights Reserved
*
* This library can redistribute it and/or modify under the terms
* of the GNU Lesser General Public License Version 2.1 as published
* by the Free Software Foundation.
*
*/
#include "cls_tabular_utils.h"
#include "cls_tabular_processing.h"
namespace Tables {
// simple converstion from schema to its str representation.
std::string schemaToString(schema_vec schema) {
std::string s;
for (auto it = schema.begin(); it != schema.end(); ++it)
s.append(it->toString() + "\n");
return s;
}
schema_vec schemaFromColNames(schema_vec ¤t_schema,
std::string col_names) {
schema_vec schema;
boost::trim(col_names);
if (col_names == PROJECT_DEFAULT) {
for (auto it=current_schema.begin(); it!=current_schema.end(); ++it) {
schema.push_back(*it);
}
}
else if (col_names == RID_INDEX) {
col_info ci(RID_COL_INDEX, SDT_UINT64, true, false, RID_INDEX);
schema.push_back(ci);
}
else {
vector<std::string> cols;
boost::split(cols, col_names, boost::is_any_of(","),
boost::token_compress_on);
// build return schema elems in order of colnames provided.
for (auto it=cols.begin(); it!=cols.end(); ++it) {
for (auto it2=current_schema.begin();
it2!=current_schema.end(); ++it2) {
if (it2->compareName(*it))
schema.push_back(*it2);
}
}
}
return schema;
}
// schema string expects the format in cls_tabular_utils.h
// see lineitem_test_schema
schema_vec schemaFromString(std::string schema_string) {
schema_vec schema;
vector<std::string> elems;
// schema col info may be delimited by either; or newline, currently
if (schema_string.find(';') != std::string::npos) {
boost::split(elems, schema_string, boost::is_any_of(";"),
boost::token_compress_on);
}
else if (schema_string.find('\n') != std::string::npos) {
boost::split(elems, schema_string, boost::is_any_of("\n"),
boost::token_compress_on);
}
else {
// assume only 1 column so no col separators
elems.push_back(schema_string);
}
// assume schema string contains at least one col's info
if (elems.size() < 1)
assert (TablesErrCodes::EmptySchema==0);
for (auto it = elems.begin(); it != elems.end(); ++it) {
vector<std::string> col_data; // each string describes one col info
std::string col_info_string = *it;
boost::trim(col_info_string);
// expected num of metadata items in our Tables::col_info struct
uint32_t col_metadata_items = NUM_COL_INFO_FIELDS;
// ignore empty strings after trimming, due to above boost split.
// expected len of at least n items with n-1 spaces
uint32_t col_info_string_min_len = (2 * col_metadata_items) - 1;
if (col_info_string.length() < col_info_string_min_len)
continue;
boost::split(col_data, col_info_string, boost::is_any_of(" "),
boost::token_compress_on);
if (col_data.size() != col_metadata_items)
assert (TablesErrCodes::BadColInfoFormat==0);
std::string name = col_data[4];
boost::trim(name);
const struct col_info ci(col_data[0], col_data[1], col_data[2],
col_data[3], name);
schema.push_back(ci);
}
return schema;
}
predicate_vec predsFromString(schema_vec &schema, std::string preds_string) {
// format: ;colname,opname,value;colname,opname,value;...
// e.g.,;orderkey,eq,5;comment,like,hello world;..
predicate_vec preds;
boost::trim(preds_string); // whitespace
boost::trim_if(preds_string, boost::is_any_of(PRED_DELIM_OUTER));
if (preds_string.empty() || preds_string== SELECT_DEFAULT) return preds;
vector<std::string> pred_items;
boost::split(pred_items, preds_string, boost::is_any_of(PRED_DELIM_OUTER),
boost::token_compress_on);
vector<std::string> colnames;
vector<std::string> select_descr;
Tables::predicate_vec agg_preds;
for (auto it=pred_items.begin(); it!=pred_items.end(); ++it) {
boost::split(select_descr, *it, boost::is_any_of(PRED_DELIM_INNER),
boost::token_compress_on);
assert(select_descr.size()==3); // currently a triple per pred.
std::string colname = select_descr.at(0);
std::string opname = select_descr.at(1);
std::string val = select_descr.at(2);
boost::to_upper(colname);
// this only has 1 col and only used to verify input
schema_vec sv = schemaFromColNames(schema, colname);
if (sv.empty()) {
cerr << "Error: colname=" << colname << " not present in schema."
<< std::endl;
assert (TablesErrCodes::RequestedColNotPresent == 0);
}
col_info ci = sv.at(0);
int op_type = skyOpTypeFromString(opname);
switch (ci.type) {
case SDT_BOOL: {
TypedPredicate<bool>* p = \
new TypedPredicate<bool> \
(ci.idx, ci.type, op_type, std::stol(val));
if (p->isGlobalAgg()) agg_preds.push_back(p);
else preds.push_back(p);
break;
}
case SDT_INT8: {
TypedPredicate<int8_t>* p = \
new TypedPredicate<int8_t> \
(ci.idx, ci.type, op_type, \
static_cast<int8_t>(std::stol(val)));
if (p->isGlobalAgg()) agg_preds.push_back(p);
else preds.push_back(p);
break;
}
case SDT_INT16: {
TypedPredicate<int16_t>* p = \
new TypedPredicate<int16_t> \
(ci.idx, ci.type, op_type, \
static_cast<int16_t>(std::stol(val)));
if (p->isGlobalAgg()) agg_preds.push_back(p);
else preds.push_back(p);
break;
}
case SDT_INT32: {
TypedPredicate<int32_t>* p = \
new TypedPredicate<int32_t> \
(ci.idx, ci.type, op_type, \
static_cast<int32_t>(std::stol(val)));
if (p->isGlobalAgg()) agg_preds.push_back(p);
else preds.push_back(p);
break;
}
case SDT_INT64: {
TypedPredicate<int64_t>* p = \
new TypedPredicate<int64_t> \
(ci.idx, ci.type, op_type, \
static_cast<int64_t>(std::stoll(val)));
if (p->isGlobalAgg()) agg_preds.push_back(p);
else preds.push_back(p);
break;
}
case SDT_UINT8: {
TypedPredicate<uint8_t>* p = \
new TypedPredicate<uint8_t> \
(ci.idx, ci.type, op_type, \
static_cast<uint8_t>(std::stoul(val)));
if (p->isGlobalAgg()) agg_preds.push_back(p);
else preds.push_back(p);
break;
}
case SDT_UINT16: {
TypedPredicate<uint16_t>* p = \
new TypedPredicate<uint16_t> \
(ci.idx, ci.type, op_type,
static_cast<uint16_t>(std::stoul(val)));
if (p->isGlobalAgg()) agg_preds.push_back(p);
else preds.push_back(p);
break;
}
case SDT_UINT32: {
TypedPredicate<uint32_t>* p = \
new TypedPredicate<uint32_t> \
(ci.idx, ci.type, op_type,
static_cast<uint32_t>(std::stoul(val)));
if (p->isGlobalAgg()) agg_preds.push_back(p);
else preds.push_back(p);
break;
}
case SDT_UINT64: {
TypedPredicate<uint64_t>* p = \
new TypedPredicate<uint64_t> \
(ci.idx, ci.type, op_type, \
static_cast<uint64_t>(std::stoull(val)));
if (p->isGlobalAgg()) agg_preds.push_back(p);
else preds.push_back(p);
break;
}
case SDT_FLOAT: {
TypedPredicate<float>* p = \
new TypedPredicate<float> \
(ci.idx, ci.type, op_type, std::stof(val));
if (p->isGlobalAgg()) agg_preds.push_back(p);
else preds.push_back(p);
break;
}
case SDT_DOUBLE: {
TypedPredicate<double>* p = \
new TypedPredicate<double> \
(ci.idx, ci.type, op_type, std::stod(val));
if (p->isGlobalAgg()) agg_preds.push_back(p);
else preds.push_back(p);
break;
}
case SDT_CHAR: {
assert (val.length() > 0);
TypedPredicate<char>* p = \
new TypedPredicate<char> \
(ci.idx, ci.type, op_type, val[0]);
if (p->isGlobalAgg()) agg_preds.push_back(p);
else preds.push_back(p);
break;
}
case SDT_UCHAR: {
assert (val.length() > 0);
TypedPredicate<unsigned char>* p = \
new TypedPredicate<unsigned char> \
(ci.idx, ci.type, op_type, val[0]);
if (p->isGlobalAgg()) agg_preds.push_back(p);
else preds.push_back(p);
break;
}
case SDT_STRING: {
TypedPredicate<std::string>* p = \
new TypedPredicate<std::string> \
(ci.idx, ci.type, op_type, val);
preds.push_back(p);
break;
}
case SDT_DATE: {
TypedPredicate<std::string>* p = \
new TypedPredicate<std::string> \
(ci.idx, ci.type, op_type, val);
preds.push_back(p);
break;
}
default: assert (TablesErrCodes::UnknownSkyDataType==0);
}
}
// add agg preds to end so they are only updated if all other preds pass.
// currently in apply_predicates they are applied in order.
if (!agg_preds.empty()) {
preds.reserve(preds.size() + agg_preds.size());
std::move(agg_preds.begin(), agg_preds.end(),
std::inserter(preds, preds.end()));
agg_preds.clear();
agg_preds.shrink_to_fit();
}
return preds;
}
std::vector<std::string> colnamesFromPreds(predicate_vec &preds,
schema_vec &schema) {
std::vector<std::string> colnames;
for (auto it_prd=preds.begin(); it_prd!=preds.end(); ++it_prd) {
for (auto it_scm=schema.begin(); it_scm!=schema.end(); ++it_scm) {
if ((*it_prd)->colIdx() == it_scm->idx) {
colnames.push_back(it_scm->name);
}
}
}
return colnames;
}
std::vector<std::string> colnamesFromSchema(schema_vec &schema) {
std::vector<std::string> colnames;
for (auto it = schema.begin(); it != schema.end(); ++it) {
colnames.push_back(it->name);
}
return colnames;
}
std::string predsToString(predicate_vec &preds, schema_vec &schema) {
// output format: "|orderkey,lt,5|comment,like,he|extendedprice,gt,2.01|"
// where '|' and ',' are denoted as PRED_DELIM_OUTER and PRED_DELIM_INNER
std::string preds_str;
// for each pred specified, we iterate over the schema to find its
// correpsonding column index so we can build the col value string
// based on col type.
for (auto it_prd = preds.begin(); it_prd != preds.end(); ++it_prd) {
for (auto it_sch = schema.begin(); it_sch != schema.end(); ++it_sch) {
col_info ci = *it_sch;
// if col indexes match then build the value string.
if (((*it_prd)->colIdx() == ci.idx) or
((*it_prd)->colIdx() == RID_COL_INDEX)) {
preds_str.append(PRED_DELIM_OUTER);
std::string colname;
// set the column name string
if ((*it_prd)->colIdx() == RID_COL_INDEX)
colname = RID_INDEX; // special col index for RID 'col'
else
colname = ci.name;
preds_str.append(colname);
preds_str.append(PRED_DELIM_INNER);
preds_str.append(skyOpTypeToString((*it_prd)->opType()));
preds_str.append(PRED_DELIM_INNER);
// set the col's value as string based on data type
std::string val;
switch ((*it_prd)->colType()) {
case SDT_BOOL: {
TypedPredicate<bool>* p = \
dynamic_cast<TypedPredicate<bool>*>(*it_prd);
val = std::string(1, p->Val());
break;
}
case SDT_INT8: {
TypedPredicate<int8_t>* p = \
dynamic_cast<TypedPredicate<int8_t>*>(*it_prd);
val = std::to_string(p->Val());
break;
}
case SDT_INT16: {
TypedPredicate<int16_t>* p = \
dynamic_cast<TypedPredicate<int16_t>*>(*it_prd);
val = std::to_string(p->Val());
break;
}
case SDT_INT32: {
TypedPredicate<int32_t>* p = \
dynamic_cast<TypedPredicate<int32_t>*>(*it_prd);
val = std::to_string(p->Val());
break;
}
case SDT_INT64: {
TypedPredicate<int64_t>* p = \
dynamic_cast<TypedPredicate<int64_t>*>(*it_prd);
val = std::to_string(p->Val());
break;
}
case SDT_UINT8: {
TypedPredicate<uint8_t>* p = \
dynamic_cast<TypedPredicate<uint8_t>*>(*it_prd);
val = std::to_string(p->Val());
break;
}
case SDT_UINT16: {
TypedPredicate<uint16_t>* p = \
dynamic_cast<TypedPredicate<uint16_t>*>(*it_prd);
val = std::to_string(p->Val());
break;
}
case SDT_UINT32: {
TypedPredicate<uint32_t>* p = \
dynamic_cast<TypedPredicate<uint32_t>*>(*it_prd);
val = std::to_string(p->Val());
break;
}
case SDT_UINT64: {
TypedPredicate<uint64_t>* p = \
dynamic_cast<TypedPredicate<uint64_t>*>(*it_prd);
val = std::to_string(p->Val());
break;
}
case SDT_CHAR: {
TypedPredicate<char>* p = \
dynamic_cast<TypedPredicate<char>*>(*it_prd);
val = std::string(1, p->Val());
break;
}
case SDT_UCHAR: {
TypedPredicate<unsigned char>* p = \
dynamic_cast<TypedPredicate<unsigned char>*>(*it_prd);
val = std::string(1, p->Val());
break;
}
case SDT_FLOAT: {
TypedPredicate<float>* p = \
dynamic_cast<TypedPredicate<float>*>(*it_prd);
val = std::to_string(p->Val());
break;
}
case SDT_DOUBLE: {
TypedPredicate<double>* p = \
dynamic_cast<TypedPredicate<double>*>(*it_prd);
val = std::to_string(p->Val());
break;
}
case SDT_STRING:
case SDT_DATE: {
TypedPredicate<std::string>* p = \
dynamic_cast<TypedPredicate<std::string>*>(*it_prd);
val = p->Val();
break;
}
default: assert (!val.empty());
}
preds_str.append(val);
}
if ((*it_prd)->colIdx() == RID_COL_INDEX)
break; // only 1 RID col in the schema
}
}
preds_str.append(PRED_DELIM_OUTER);
return preds_str;
}
int skyOpTypeFromString(std::string op) {
int op_type = 0;
if (op=="lt") op_type = SOT_lt;
else if (op=="gt") op_type = SOT_gt;
else if (op=="eq") op_type = SOT_eq;
else if (op=="ne") op_type = SOT_ne;
else if (op=="leq") op_type = SOT_leq;
else if (op=="geq") op_type = SOT_geq;
else if (op=="add") op_type = SOT_add;
else if (op=="sub") op_type = SOT_sub;
else if (op=="mul") op_type = SOT_mul;
else if (op=="div") op_type = SOT_div;
else if (op=="min") op_type = SOT_min;
else if (op=="max") op_type = SOT_max;
else if (op=="sum") op_type = SOT_sum;
else if (op=="cnt") op_type = SOT_cnt;
else if (op=="like") op_type = SOT_like;
else if (op=="in") op_type = SOT_in;
else if (op=="not_in") op_type = SOT_not_in;
else if (op=="before") op_type = SOT_before;
else if (op=="between") op_type = SOT_between;
else if (op=="after") op_type = SOT_after;
else if (op=="logical_or") op_type = SOT_logical_or;
else if (op=="logical_and") op_type = SOT_logical_and;
else if (op=="logical_not") op_type = SOT_logical_not;
else if (op=="logical_nor") op_type = SOT_logical_nor;
else if (op=="logical_xor") op_type = SOT_logical_xor;
else if (op=="logical_nand") op_type = SOT_logical_nand;
else if (op=="bitwise_and") op_type = SOT_bitwise_and;
else if (op=="bitwise_or") op_type = SOT_bitwise_or;
else assert (TablesErrCodes::OpNotRecognized==0);
return op_type;
}
std::string skyOpTypeToString(int op) {
std::string op_str;
if (op==SOT_lt) op_str = "lt";
else if (op==SOT_gt) op_str = "gt";
else if (op==SOT_eq) op_str = "eq";
else if (op==SOT_ne) op_str = "ne";
else if (op==SOT_leq) op_str = "leq";
else if (op==SOT_geq) op_str = "geq";
else if (op==SOT_add) op_str = "add";
else if (op==SOT_sub) op_str = "sub";
else if (op==SOT_mul) op_str = "mul";
else if (op==SOT_div) op_str = "div";
else if (op==SOT_min) op_str = "min";
else if (op==SOT_max) op_str = "max";
else if (op==SOT_sum) op_str = "sum";
else if (op==SOT_cnt) op_str = "cnt";
else if (op==SOT_like) op_str = "like";
else if (op==SOT_in) op_str = "in";
else if (op==SOT_not_in) op_str = "not_in";
else if (op==SOT_before) op_str = "before";
else if (op==SOT_between) op_str = "between";
else if (op==SOT_after) op_str = "after";
else if (op==SOT_logical_or) op_str = "logical_or";
else if (op==SOT_logical_and) op_str = "logical_and";
else if (op==SOT_logical_not) op_str = "logical_not";
else if (op==SOT_logical_nor) op_str = "logical_nor";
else if (op==SOT_logical_xor) op_str = "logical_xor";
else if (op==SOT_logical_nand) op_str = "logical_nand";
else if (op==SOT_bitwise_and) op_str = "bitwise_and";
else if (op==SOT_bitwise_or) op_str = "bitwise_or";
else assert (!op_str.empty());
return op_str;
}
void printSkyRootHeader(sky_root &r) {
std::cout << "\n\nSKYHOOK_ROOT HEADER"<< std::endl;
std::cout << "skyhook_version: "<< r.skyhook_version << std::endl;
std::cout << "data_format_type: "<< r.data_format_type << std::endl;
std::cout << "data_structure_version: "<< r.data_structure_version << std::endl;
std::cout << "data_schema_version: "<< r.data_schema_version << std::endl;
std::cout << "db_schema_name: "<< r.db_schema_name << std::endl;
std::cout << "table name: "<< r.table_name << std::endl;
std::cout << "data_schema: \n"<< r.data_schema << std::endl;
std::cout << "delete vector: [";
for (int i=0; i< (int)r.delete_vec.size(); i++) {
std::cout << (int)r.delete_vec[i];
if (i != (int)r.delete_vec.size()-1)
std::cout <<", ";
}
std::cout << "]" << std::endl;
std::cout << "nrows: " << r.nrows << std::endl;
std::cout << std::endl;
}
void printSkyRecHeader(sky_rec &r) {
std::cout << "\nSKYHOOK_REC HEADER" << std::endl;
std::cout << "RID: "<< r.RID << std::endl;
std::string bitstring = "";
int64_t val = 0;
uint64_t bit = 0;
for(int j = 0; j < (int)r.nullbits.size(); j++) {
val = r.nullbits.at(j);
for (uint64_t k=0; k < 8 * sizeof(r.nullbits.at(j)); k++) {
uint64_t mask = 1 << k;
((val&mask)>0) ? bit=1 : bit=0;
bitstring.append(std::to_string(bit));
}
std::cout << "nullbits ["<< j << "]: val=" << val << ": bits="
<< bitstring;
std::cout << std::endl;
bitstring.clear();
}
}
long long int printFlatbufFlexRowAsCsv(
const char* dataptr,
const size_t datasz,
bool print_header,
bool print_verbose,
long long int max_to_print) {
// get root table ptr as sky struct
sky_root root = getSkyRoot(dataptr, datasz, SFT_FLATBUF_FLEX_ROW);
schema_vec sc = schemaFromString(root.data_schema);
assert(!sc.empty());
if (print_verbose)
printSkyRootHeader(root);
// print header row showing schema
if (print_header) {
bool first = true;
for (schema_vec::iterator it = sc.begin(); it != sc.end(); ++it) {
if (!first) std::cout << CSV_DELIM;
first = false;
std::cout << it->name;
if (it->is_key) std::cout << "(key)";
if (!it->nullable) std::cout << "(NOT NULL)";
}
std::cout << std::endl; // newline to start first row.
}
long long int counter = 0;
for (uint32_t i = 0; i < root.nrows; i++, counter++) {
if (counter >= max_to_print)
break;
if (root.delete_vec.at(i) == 1) continue; // skip dead rows.
// get the record struct
sky_rec skyrec = \
getSkyRec(static_cast<row_offs>(root.data_vec)->Get(i));
// now get the flexbuf row's data as a flexbuf vec
auto row = skyrec.data.AsVector();
if (print_verbose)
printSkyRecHeader(skyrec);
// for each col in the row, print a NULL or the col's value/
bool first = true;
for (uint32_t j = 0; j < sc.size(); j++) {
if (!first) std::cout << CSV_DELIM;
first = false;
col_info col = sc.at(j);
if (col.nullable) { // check nullbit
bool is_null = false;
int pos = col.idx / (8*sizeof(skyrec.nullbits.at(0)));
int col_bitmask = 1 << col.idx;
if ((col_bitmask & skyrec.nullbits.at(pos)) != 0) {
is_null =true;
}
if (is_null) {
std::cout << "NULL";
continue;
}
}
switch (col.type) {
case SDT_BOOL: std::cout << row[j].AsBool(); break;
case SDT_INT8: std::cout << row[j].AsInt8(); break;
case SDT_INT16: std::cout << row[j].AsInt16(); break;
case SDT_INT32: std::cout << row[j].AsInt32(); break;
case SDT_INT64: std::cout << row[j].AsInt64(); break;
case SDT_UINT8: std::cout << row[j].AsUInt8(); break;
case SDT_UINT16: std::cout << row[j].AsUInt16(); break;
case SDT_UINT32: std::cout << row[j].AsUInt32(); break;
case SDT_UINT64: std::cout << row[j].AsUInt64(); break;
case SDT_FLOAT: std::cout << row[j].AsFloat(); break;
case SDT_DOUBLE: std::cout << row[j].AsDouble(); break;
case SDT_CHAR: std::cout <<
std::string(1, row[j].AsInt8()); break;
case SDT_UCHAR: std::cout <<
std::string(1, row[j].AsUInt8()); break;
case SDT_DATE: std::cout <<
row[j].AsString().str(); break;
case SDT_STRING: std::cout <<
row[j].AsString().str(); break;
default: assert (TablesErrCodes::UnknownSkyDataType);
}
}
std::cout << std::endl; // newline to start next row.
}
return counter;
}
long long int printFlatbufFlexRowAsPGBinary(
const char* dataptr,
const size_t datasz,
bool print_header,
bool print_verbose,
long long int max_to_print) {
// get root table ptr as sky struct
sky_root root = getSkyRoot(dataptr, datasz, SFT_FLATBUF_FLEX_ROW);
schema_vec sc = schemaFromString(root.data_schema);
assert(!sc.empty());
// postgres fstreams expect big endianness
bool big_endian = is_big_endian();
stringstream ss(std::stringstream::in |
std::stringstream::out|
std::stringstream::binary);
// print binary stream header sequence first time only
if (print_header) {
// 11 byte signature sequence
const char* header_signature = "PGCOPY\n\377\r\n\0";
ss.write(header_signature, 11);
// 32 bit flags field, set bit#16=1 only if OIDs included in data
int flags_field = 0;
ss.write(reinterpret_cast<const char*>(&flags_field),
sizeof(flags_field));
// 32 bit extra header len
int header_extension_len = 0;
ss.write(reinterpret_cast<const char*>(&header_extension_len),
sizeof(header_extension_len));
}
// 16 bit int, assumes same num cols for all rows below.
int16_t ncols = static_cast<int16_t>(sc.size());
if (!big_endian)
ncols = __builtin_bswap16(ncols);
// row printing counter, used with --limit flag
long long int counter = 0;
for (uint32_t i = 0; i < root.nrows; i++, counter++) {
if (counter >= max_to_print) break;
if (root.delete_vec.at(i) == 1) continue;
// get the record struct
sky_rec skyrec = \
getSkyRec(static_cast<row_offs>(root.data_vec)->Get(i));
// 16 bit int num cols in this row (all rows same ncols currently)
ss.write(reinterpret_cast<const char*>(&ncols), sizeof(ncols));
// get the flexbuf row's data as a flexbuf vec
auto row = skyrec.data.AsVector();
// for each col in the row, print a NULL or the col's value/
for (unsigned j = 0; j < sc.size(); j++ ) {
col_info col = sc.at(j);
if (col.nullable) { // check nullbit
bool is_null = false;
int pos = col.idx / (8*sizeof(skyrec.nullbits.at(0)));
int col_bitmask = 1 << col.idx;
if ((col_bitmask & skyrec.nullbits.at(pos)) != 0) {
is_null = true;
}
if (is_null) {
// for null we only write the int representation of null,
// followed by no data
ss.write(reinterpret_cast<const char*>(&PGNULLBINARY),
sizeof(PGNULLBINARY));
continue;
}
}
// for each col, write 32 bit data len followed by data as char*
switch (col.type) {
case SDT_BOOL: {
uint8_t val = row[j].AsBool();
int32_t len = sizeof(val);
if (!big_endian) {
// val is single byte, has no endianness
len = __builtin_bswap32(len);
}
ss.write(reinterpret_cast<const char*>(&len), sizeof(len));
ss.write(reinterpret_cast<const char*>(&val), sizeof(val));
break;
}
case SDT_INT8: {
int8_t val = row[j].AsInt8();
int32_t len = sizeof(val);
if (!big_endian) {
// val is single byte, has no endianness
len = __builtin_bswap32(len);
}
ss.write(reinterpret_cast<const char*>(&len), sizeof(len));
ss.write(reinterpret_cast<const char*>(&val), sizeof(val));
break;
}
case SDT_INT16: {
int16_t val = row[j].AsInt16();
int32_t len = sizeof(val);
if (!big_endian) {
val = __builtin_bswap16(val);
len = __builtin_bswap32(len);
}
ss.write(reinterpret_cast<const char*>(&len), sizeof(len));
ss.write(reinterpret_cast<const char*>(&val), sizeof(val));
break;
}
case SDT_INT32: {
int32_t val = row[j].AsInt32();
int32_t len = sizeof(val);
if (!big_endian) {
val = __builtin_bswap32(val);
len = __builtin_bswap32(len);
}
ss.write(reinterpret_cast<const char*>(&len), sizeof(len));
ss.write(reinterpret_cast<const char*>(&val), sizeof(val));
break;
}
case SDT_INT64: {
int64_t val = row[j].AsInt64();
int32_t len = sizeof(val);
if (!big_endian) {
val = __builtin_bswap64(val);
len = __builtin_bswap32(len);
}
ss.write(reinterpret_cast<const char*>(&len), sizeof(len));
ss.write(reinterpret_cast<const char*>(&val), sizeof(val));
break;
}
case SDT_UINT8: {
uint8_t val = row[j].AsUInt8();
int32_t len = sizeof(val);
if (!big_endian) {
// val is single byte, has no endianness
len = __builtin_bswap32(len);
}
ss.write(reinterpret_cast<const char*>(&len), sizeof(len));
ss.write(reinterpret_cast<const char*>(&val), sizeof(val));
break;
}
case SDT_UINT16: {
uint16_t val = row[j].AsUInt16();
int32_t len = sizeof(val);
if (!big_endian) {
val = __builtin_bswap16(val);
len = __builtin_bswap32(len);
}
ss.write(reinterpret_cast<const char*>(&len), sizeof(len));
ss.write(reinterpret_cast<const char*>(&val), sizeof(val));
break;
}
case SDT_UINT32: {
uint32_t val = row[j].AsUInt32();
int32_t len = sizeof(val);
if (!big_endian) {
val = __builtin_bswap32(val);
len = __builtin_bswap32(len);
}
ss.write(reinterpret_cast<const char*>(&len), sizeof(len));
ss.write(reinterpret_cast<const char*>(&val), sizeof(val));
break;
}
case SDT_UINT64: {
uint64_t val = row[j].AsUInt64();
int32_t len = sizeof(val);
if (!big_endian) {
val = __builtin_bswap64(val);
len = __builtin_bswap32(len);
}
ss.write(reinterpret_cast<const char*>(&len), sizeof(len));
ss.write(reinterpret_cast<const char*>(&val), sizeof(val));
break;
}
case SDT_FLOAT: // postgres float is alias for double
case SDT_DOUBLE: {
double val = 0.0;
if (col.type == SDT_FLOAT)
val = row[j].AsFloat(); // flexbuf api requires type
else
val = row[j].AsDouble();
int32_t len = sizeof(val);
if (!big_endian) {
char val_bigend[len];
char* vptr = (char*)&val;
val_bigend[0]=vptr[7];
val_bigend[1]=vptr[6];
val_bigend[2]=vptr[5];
val_bigend[3]=vptr[4];
val_bigend[4]=vptr[3];
val_bigend[5]=vptr[2];
val_bigend[6]=vptr[1];
val_bigend[7]=vptr[0];
len = __builtin_bswap32(len);
ss.write(reinterpret_cast<const char*>(&len), sizeof(len));
ss.write(val_bigend, sizeof(val));
}
else {
ss.write(reinterpret_cast<const char*>(&len), sizeof(len));
ss.write(reinterpret_cast<const char*>(&val), sizeof(val));
}
break;
}
case SDT_CHAR: {
int8_t val = row[j].AsInt8();
int32_t len = sizeof(val);
if (!big_endian) {
// val is single byte, has no endianness
len = __builtin_bswap32(len);
}
ss.write(reinterpret_cast<const char*>(&len), sizeof(len));
ss.write(reinterpret_cast<const char*>(&val), sizeof(val));
break;
}
case SDT_UCHAR: {
uint8_t val = row[j].AsUInt8();
int32_t len = sizeof(val);
if (!big_endian) {
// val is single byte, has no endianness
len = __builtin_bswap32(len);
}
ss.write(reinterpret_cast<const char*>(&len), sizeof(len));
ss.write(reinterpret_cast<const char*>(&val), sizeof(val));
break;
}
case SDT_DATE: {
// postgres uses 4 byte int date vals, offset by pg epoch
std::string strdate = row[j].AsString().str();
int32_t len = 4; // fixed, postgres date specification
boost::gregorian::date d = \
boost::gregorian::from_string(strdate);
int32_t val = d.julian_day() - Tables::POSTGRES_EPOCH_JDATE;
if (!big_endian) {
val = __builtin_bswap32(val);
len = __builtin_bswap32(len);
}
ss.write(reinterpret_cast<const char*>(&len), sizeof(len));
ss.write(reinterpret_cast<const char*>(&val), sizeof(val));
break;
}
case SDT_STRING: {
std::string val = row[j].AsString().str();
int32_t len = val.length();
if (!big_endian) {
// val is byte array, has no endianness
len = __builtin_bswap32(len);
}
ss.write(reinterpret_cast<const char*>(&len), sizeof(len));
ss.write(val.c_str(), static_cast<int>(val.length()));
break;
}
default: assert (TablesErrCodes::UnknownSkyDataType);
}
}
}
// rewind and output all row data for this fb
ss.seekg (0, ios::beg);
std::cout << ss.rdbuf();
ss.flush();
return counter;
}
long long int printJSONAsCsv(
const char* dataptr,
const size_t datasz,
bool print_header,
bool print_verbose,
long long int max_to_print) {
// get root table ptr as sky struct
sky_root root = getSkyRoot(dataptr, datasz, SFT_JSON);
schema_vec sc = schemaFromString(root.data_schema);
assert(!sc.empty());
if (print_verbose)
printSkyRootHeader(root);
// print header row showing schema
if (print_header) {
bool first = true;
for (schema_vec::iterator it = sc.begin(); it != sc.end(); ++it) {
if (!first) std::cout << CSV_DELIM;
first = false;
std::cout << it->name;
if (it->is_key) std::cout << "(key)";
if (!it->nullable) std::cout << "(NOT NULL)";
}
std::cout << std::endl; // newline to start first row.
}
// iterate over each row data (Record_FBX)
// NOTE: JSON stores all rows (json objs) in single record currently.
long long int counter = 0;
for (uint32_t i = 0; i < root.nrows; i++, counter++) {
if (counter >= max_to_print)
break;
if (root.delete_vec.at(i) == 1) continue; // skip dead rows.
// get the root pointer from skyhookv2_csv.fbs, used for json and
// csv text data
const Table_FBX* rootfbx = GetTable_FBX(dataptr);
// ptr to records offsets
const flatbuffers::Vector<flatbuffers::Offset<Record_FBX>>* \
offs = rootfbx->rows_vec();
// get the next record
const Record_FBX* rec = offs->Get(i);
// NOTE:
// rec->RID() and rec->nullbits() are set but not yet used for JSON
// NOTE: this a vector of strings, but for now JSON data will be stored
// as single string in elem[0], so data vector size here is only 1.
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>* \
data = rec->data();
// iterate over json data, extracting from flatbuffers vec as a string.
std::string json_str("");
for (unsigned j = 0; j < data->size(); j++) {
// TODO: Assuming a flatflex object...
// for each row, extract as json string and print cols
// from each row according to schema_vec sc.
json_str = data->Get(j)->str();
std::cout << "row[" << i << "]=" << json_str << std::endl;
rapidjson::Document doc;
doc.Parse(json_str.c_str());
// TODO: right now this crashes due to a malformed JSON test obj.
//assert(doc.IsObject());
// static example of using rapidjson lib in ceph
rapidjson::Document d;
d.Parse(JSON_SAMPLE.c_str());
assert(d.IsObject());
assert(d.HasMember("V"));
assert(d["V"].IsString());