-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathjson_flattener.cpp
1473 lines (1321 loc) · 53.1 KB
/
json_flattener.cpp
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
#include "util/json_flattener.h"
#include <sys/types.h>
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <memory>
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "column/column_helper.h"
#include "column/json_column.h"
#include "column/nullable_column.h"
#include "column/type_traits.h"
#include "column/vectorized_fwd.h"
#include "common/compiler_util.h"
#include "common/config.h"
#include "common/status.h"
#include "common/statusor.h"
#include "exprs/cast_expr.h"
#include "exprs/column_ref.h"
#include "exprs/expr_context.h"
#include "gutil/casts.h"
#include "runtime/types.h"
#include "storage/rowset/column_reader.h"
#include "types/logical_type.h"
#include "util/bloom_filter.h"
#include "util/json.h"
#include "util/json_converter.h"
#include "util/runtime_profile.h"
namespace starrocks {
namespace flat_json {
template <LogicalType TYPE>
void extract_number(const vpack::Slice* json, NullableColumn* result) {
try {
if (LIKELY(json->isNumber() || json->isString())) {
auto st = get_number_from_vpjson<TYPE>(*json);
if (st.ok()) {
result->null_column()->append(0);
down_cast<RunTimeColumnType<TYPE>*>(result->data_column().get())->append(st.value());
} else {
result->append_nulls(1);
}
} else if (json->isNone() || json->isNull()) {
result->append_nulls(1);
} else if (json->isBool()) {
result->null_column()->append(0);
down_cast<RunTimeColumnType<TYPE>*>(result->data_column().get())->append(json->getBool());
} else {
result->append_nulls(1);
}
} catch (const vpack::Exception& e) {
result->append_nulls(1);
}
}
void extract_string(const vpack::Slice* json, NullableColumn* result) {
try {
if (json->isNone() || json->isNull()) {
result->append_nulls(1);
} else if (json->isString()) {
result->null_column()->append(0);
vpack::ValueLength len;
const char* str = json->getStringUnchecked(len);
down_cast<BinaryColumn*>(result->data_column().get())->append(Slice(str, len));
} else {
result->null_column()->append(0);
vpack::Options options = vpack::Options::Defaults;
options.singleLinePrettyPrint = true;
options.dumpAttributesInIndexOrder = false;
std::string str = json->toJson(&options);
down_cast<BinaryColumn*>(result->data_column().get())->append(Slice(str));
}
} catch (const vpack::Exception& e) {
result->append_nulls(1);
}
}
void extract_json(const vpack::Slice* json, NullableColumn* result) {
if (json->isNone()) {
result->append_nulls(1);
} else {
result->null_column()->append(0);
down_cast<JsonColumn*>(result->data_column().get())->append(JsonValue(*json));
}
}
template <LogicalType TYPE>
void merge_number(vpack::Builder* builder, const std::string_view& name, const Column* src, size_t idx) {
DCHECK(src->is_nullable());
auto* nullable_column = down_cast<const NullableColumn*>(src);
auto* col = down_cast<const RunTimeColumnType<TYPE>*>(nullable_column->data_column().get());
if constexpr (TYPE == LogicalType::TYPE_LARGEINT) {
// the value is from json, must be uint64_t
builder->addUnchecked(name.data(), name.size(), vpack::Value((uint64_t)col->get_data()[idx]));
} else {
builder->addUnchecked(name.data(), name.size(), vpack::Value(col->get_data()[idx]));
}
}
void merge_string(vpack::Builder* builder, const std::string_view& name, const Column* src, size_t idx) {
DCHECK(src->is_nullable());
auto* nullable_column = down_cast<const NullableColumn*>(src);
auto* col = down_cast<const BinaryColumn*>(nullable_column->data_column().get());
builder->addUnchecked(name.data(), name.size(), vpack::Value(col->get_slice(idx).to_string()));
}
void merge_json(vpack::Builder* builder, const std::string_view& name, const Column* src, size_t idx) {
DCHECK(src->is_nullable());
auto* nullable_column = down_cast<const NullableColumn*>(src);
auto* col = down_cast<const JsonColumn*>(nullable_column->data_column().get());
builder->addUnchecked(name.data(), name.size(), col->get_object(idx)->to_vslice());
}
// clang-format off
using JsonFlatExtractFunc = void (*)(const vpack::Slice* json, NullableColumn* result);
using JsonFlatMergeFunc = void (*)(vpack::Builder* builder, const std::string_view& name, const Column* src, size_t idx);
static const uint8_t JSON_BASE_TYPE_BITS = 0; // least flat to JSON type
static const uint8_t JSON_BIGINT_TYPE_BITS = 7; // bigint compatible type
// bool will flatting as string, because it's need save string-literal(true/false)
// int & string compatible type is json, because int cast to string will add double quote, it's different with json
static const FlatJsonHashMap<vpack::ValueType, uint8_t> JSON_TYPE_BITS {
{vpack::ValueType::None, 31}, // 00011111, 31
{vpack::ValueType::SmallInt, 15}, // 00001111, 15
{vpack::ValueType::Int, 7}, // 00000111, 7
{vpack::ValueType::UInt, 3}, // 00000011, 3
{vpack::ValueType::Double, 1}, // 00000001, 1
{vpack::ValueType::String, 16}, // 00010000, 16
};
// starrocks json fucntio only support read as bigint/string/bool/double, smallint will cast to bigint, so we save as bigint directly
static const FlatJsonHashMap<uint8_t, LogicalType> JSON_BITS_TO_LOGICAL_TYPE {
{JSON_TYPE_BITS.at(vpack::ValueType::None), LogicalType::TYPE_TINYINT},
{JSON_TYPE_BITS.at(vpack::ValueType::SmallInt), LogicalType::TYPE_BIGINT},
{JSON_TYPE_BITS.at(vpack::ValueType::Int), LogicalType::TYPE_BIGINT},
{JSON_TYPE_BITS.at(vpack::ValueType::UInt), LogicalType::TYPE_LARGEINT},
{JSON_TYPE_BITS.at(vpack::ValueType::Double), LogicalType::TYPE_DOUBLE},
{JSON_TYPE_BITS.at(vpack::ValueType::String), LogicalType::TYPE_VARCHAR},
{JSON_BASE_TYPE_BITS, LogicalType::TYPE_JSON},
};
static FlatJsonHashMap<LogicalType, uint8_t> LOGICAL_TYPE_TO_JSON_BITS {
{LogicalType::TYPE_TINYINT, JSON_TYPE_BITS.at(vpack::ValueType::None)},
{LogicalType::TYPE_BIGINT, JSON_TYPE_BITS.at(vpack::ValueType::Int)},
{LogicalType::TYPE_LARGEINT, JSON_TYPE_BITS.at(vpack::ValueType::UInt)},
{LogicalType::TYPE_DOUBLE, JSON_TYPE_BITS.at(vpack::ValueType::Double)},
{LogicalType::TYPE_VARCHAR, JSON_TYPE_BITS.at(vpack::ValueType::String)},
{LogicalType::TYPE_JSON, JSON_BASE_TYPE_BITS},
};
static const FlatJsonHashMap<LogicalType, JsonFlatExtractFunc> JSON_EXTRACT_FUNC {
{LogicalType::TYPE_TINYINT, &extract_number<LogicalType::TYPE_TINYINT>},
{LogicalType::TYPE_BIGINT, &extract_number<LogicalType::TYPE_BIGINT>},
{LogicalType::TYPE_LARGEINT, &extract_number<LogicalType::TYPE_LARGEINT>},
{LogicalType::TYPE_DOUBLE, &extract_number<LogicalType::TYPE_DOUBLE>},
{LogicalType::TYPE_VARCHAR, &extract_string},
{LogicalType::TYPE_CHAR, &extract_string},
{LogicalType::TYPE_JSON, &extract_json},
};
// should match with extract function
static const FlatJsonHashMap<LogicalType, JsonFlatMergeFunc> JSON_MERGE_FUNC {
{LogicalType::TYPE_TINYINT, &merge_number<LogicalType::TYPE_TINYINT>},
{LogicalType::TYPE_BIGINT, &merge_number<LogicalType::TYPE_BIGINT>},
{LogicalType::TYPE_LARGEINT, &merge_number<LogicalType::TYPE_LARGEINT>},
{LogicalType::TYPE_DOUBLE, &merge_number<LogicalType::TYPE_DOUBLE>},
{LogicalType::TYPE_VARCHAR, &merge_string},
{LogicalType::TYPE_JSON, &merge_json},
};
// clang-format on
inline uint8_t get_compatibility_type(vpack::ValueType type1, uint8_t type2) {
auto iter = JSON_TYPE_BITS.find(type1);
return iter != JSON_TYPE_BITS.end() ? type2 & iter->second : JSON_BASE_TYPE_BITS;
}
} // namespace flat_json
static const double FILTER_TEST_FPP[]{0.05, 0.1, 0.15, 0.2, 0.25, 0.3};
double estimate_filter_fpp(uint64_t element_nums) {
uint32_t bytes[6];
int min_idx = 7;
double min = config::json_flat_remain_filter_max_bytes + 1;
for (int i = 0; i < 6; i++) {
bytes[i] = BloomFilter::estimate_bytes(element_nums, FILTER_TEST_FPP[i]);
double d = bytes[i] + FILTER_TEST_FPP[i];
if (d < min) {
min = d;
min_idx = i;
}
}
return min_idx < 6 ? FILTER_TEST_FPP[min_idx] : -1;
}
std::pair<std::string_view, std::string_view> JsonFlatPath::split_path(const std::string_view& path) {
size_t pos = 0;
pos = path.find('.', pos);
std::string_view key;
std::string_view next;
if (pos == std::string::npos) {
key = path;
} else {
key = path.substr(0, pos);
next = path.substr(pos + 1);
}
return {key, next};
}
JsonFlatPath* JsonFlatPath::normalize_from_path(const std::string_view& path, JsonFlatPath* root) {
if (path.empty()) {
return root;
}
auto [key, next] = split_path(path);
auto iter = root->children.find(key);
JsonFlatPath* child_path = nullptr;
if (iter == root->children.end()) {
root->children.emplace(key, std::make_unique<JsonFlatPath>());
child_path = root->children[key].get();
} else {
child_path = iter->second.get();
}
return normalize_from_path(next, child_path);
}
/*
* to mark new root
* root(Ig)
* / | \
* a(Ex) b(Ig) c(Ex)
* / / \ \
* any b1(Ex) b2(N) any
* / \
* b3(IN) b4(IN)
*/
void JsonFlatPath::set_root(const std::string_view& new_root_path, JsonFlatPath* node) {
node->op = OP_IGNORE;
if (new_root_path.empty()) {
node->op = OP_ROOT;
return;
}
auto [key, next] = split_path(new_root_path);
auto iter = node->children.begin();
for (; iter != node->children.end(); iter++) {
iter->second->op = OP_EXCLUDE;
if (iter->first == key) {
set_root(next, iter->second.get());
}
}
}
StatusOr<size_t> check_null_factor(const std::vector<const Column*>& json_datas) {
size_t total_rows = 0;
size_t null_count = 0;
for (auto& column : json_datas) {
total_rows += column->size();
if (column->only_null() || column->empty()) {
null_count += column->size();
continue;
} else if (column->is_nullable()) {
auto* nullable_column = down_cast<const NullableColumn*>(column);
null_count += nullable_column->null_count();
}
}
// more than half of null
if (null_count > total_rows * config::json_flat_null_factor) {
VLOG(8) << "flat json, null_count[" << null_count << "], row[" << total_rows
<< "], null_factor: " << config::json_flat_null_factor;
return Status::InternalError("json flat null factor too high");
}
return total_rows - null_count;
}
JsonPathDeriver::JsonPathDeriver(const std::vector<std::string>& paths, const std::vector<LogicalType>& types,
bool has_remain)
: _has_remain(has_remain), _paths(std::move(paths)), _types(types) {
for (size_t i = 0; i < _paths.size(); i++) {
auto* leaf = JsonFlatPath::normalize_from_path(_paths[i], _path_root.get());
leaf->type = types[i];
leaf->index = i;
}
}
void JsonPathDeriver::derived(const std::vector<const Column*>& json_datas) {
DCHECK(_paths.empty());
DCHECK(_types.empty());
DCHECK(_derived_maps.empty());
DCHECK(_path_root == nullptr);
if (json_datas.empty()) {
return;
}
auto res = check_null_factor(json_datas);
if (!res.ok()) {
return;
}
_total_rows = res.value();
_path_root = std::make_shared<JsonFlatPath>();
// init path by flat json
_derived_on_flat_json(json_datas);
// extract common keys, type
size_t mark_row = 0;
for (size_t k = 0; k < json_datas.size(); k++) {
_derived(json_datas[k], mark_row);
mark_row += json_datas[k]->size();
}
_finalize();
}
JsonFlatPath* JsonPathDeriver::_normalize_exists_path(const std::string_view& path, JsonFlatPath* root, uint64_t hits) {
if (path.empty()) {
return root;
}
_derived_maps[root].hits += hits;
_derived_maps[root].type = flat_json::JSON_BASE_TYPE_BITS;
auto [key, next] = JsonFlatPath::split_path(path);
auto iter = root->children.find(key);
JsonFlatPath* child_path = nullptr;
if (iter == root->children.end()) {
root->children.emplace(key, std::make_unique<JsonFlatPath>());
child_path = root->children[key].get();
} else {
child_path = iter->second.get();
}
return _normalize_exists_path(next, child_path, hits);
}
void JsonPathDeriver::derived(const std::vector<const ColumnReader*>& json_readers) {
DCHECK(_paths.empty());
DCHECK(_types.empty());
DCHECK(_derived_maps.empty());
DCHECK(_path_root == nullptr);
if (json_readers.empty()) {
return;
}
_path_root = std::make_shared<JsonFlatPath>();
_total_rows = 0;
// extract flat paths
for (const auto& reader : json_readers) {
DCHECK_EQ(LogicalType::TYPE_JSON, reader->column_type());
DCHECK(!reader->sub_readers()->empty());
_total_rows += reader->num_rows();
int start = reader->is_nullable() ? 1 : 0;
int end = reader->has_remain_json() ? reader->sub_readers()->size() - 1 : reader->sub_readers()->size();
_has_remain |= reader->has_remain_json();
for (size_t i = start; i < end; i++) {
const auto& sub = (*reader->sub_readers())[i];
// compaction only extract common leaf, extract parent node need more compute on remain, it's bad performance
auto leaf = _normalize_exists_path(sub->name(), _path_root.get(), 0);
_derived_maps[leaf].type &= flat_json::LOGICAL_TYPE_TO_JSON_BITS.at(sub->column_type());
_derived_maps[leaf].hits += reader->num_rows();
}
}
_derived_maps.erase(_path_root.get());
_min_json_sparsity_factory = 1; // only extract common schema
_finalize();
}
void JsonPathDeriver::_derived_on_flat_json(const std::vector<const Column*>& json_datas) {
// extract flat paths
for (size_t k = 0; k < json_datas.size(); k++) {
auto col = json_datas[k];
const JsonColumn* json_col;
size_t hits = 0;
if (col->is_nullable()) {
auto nullable = down_cast<const NullableColumn*>(col);
hits = col->size() - nullable->null_count();
json_col = down_cast<const JsonColumn*>(nullable->data_column().get());
} else {
hits = col->size();
json_col = down_cast<const JsonColumn*>(col);
}
if (!json_col->is_flat_json()) {
continue;
}
const auto& paths = json_col->flat_column_paths();
const auto& types = json_col->flat_column_types();
for (size_t i = 0; i < paths.size(); i++) {
auto leaf = _normalize_exists_path(paths[i], _path_root.get(), hits);
_derived_maps[leaf].type &= flat_json::LOGICAL_TYPE_TO_JSON_BITS.at(types[i]);
_derived_maps[leaf].hits += hits;
}
}
_derived_maps.erase(_path_root.get());
}
void JsonPathDeriver::_derived(const Column* col, size_t mark_row) {
size_t row_count = col->size();
const JsonColumn* json_col;
if (col->is_nullable()) {
auto nullable = down_cast<const NullableColumn*>(col);
json_col = down_cast<const JsonColumn*>(nullable->data_column().get());
} else {
json_col = down_cast<const JsonColumn*>(col);
}
if (json_col->is_flat_json()) {
if (json_col->has_remain()) {
json_col = down_cast<const JsonColumn*>(json_col->get_remain().get());
} else {
return;
}
}
for (size_t i = 0; i < row_count; ++i) {
if (col->is_null(i)) {
continue;
}
JsonValue* json = json_col->get_object(i);
auto vslice = json->to_vslice();
if (vslice.isNull() || vslice.isNone()) {
continue;
}
if (vslice.isEmptyObject() || !vslice.isObject()) {
_has_remain = true;
continue;
}
_visit_json_paths(vslice, _path_root.get(), mark_row + i);
}
}
// TODO: incrementally find the top-k, rather than visit all nodes first
// recursively visit all JSON items to remember common paths
void JsonPathDeriver::_visit_json_paths(const vpack::Slice& value, JsonFlatPath* root, size_t mark_row) {
const size_t COMMON_PATH_STATE_FACTOR = 10;
size_t state_limit = config::json_flat_column_max > 0 ? COMMON_PATH_STATE_FACTOR * config::json_flat_column_max
: std::numeric_limits<size_t>::max();
// Optimize memory usage by limiting the number of paths to prevent excessive memory consumption
if (_derived_maps.size() >= state_limit) {
return;
}
vpack::ObjectIterator it(value, false);
for (; it.valid(); it.next()) {
auto current = (*it);
// sub-object?
auto v = current.value;
auto k = current.key.stringView();
if (!root->children.contains(k)) {
root->children.emplace(k, std::make_unique<JsonFlatPath>());
}
auto child = root->children[k].get();
auto desc = &_derived_maps[child];
desc->hits++;
desc->multi_times += (desc->last_row == mark_row);
desc->last_row = mark_row;
if (v.isObject()) {
child->remain = v.isEmptyObject();
desc->type = flat_json::JSON_BASE_TYPE_BITS;
_visit_json_paths(v, child, mark_row);
} else {
auto desc = &_derived_maps[child];
vpack::ValueType json_type = v.type();
desc->type = flat_json::get_compatibility_type(json_type, desc->type);
if (json_type == vpack::ValueType::UInt) {
desc->max = std::max(desc->max, v.getUIntUnchecked());
}
}
}
}
// why dfs? because need compute parent isn't extract base on bottom-up, stack is not suitable
uint32_t JsonPathDeriver::_dfs_finalize(JsonFlatPath* node, const std::string& absolute_path,
std::vector<std::pair<JsonFlatPath*, std::string>>* hit_leaf) {
uint32_t flat_count = 0;
for (auto& [key, child] : node->children) {
if (!key.empty() && key.find('.') == std::string::npos) {
// ignore empty key/quote key, it's can't handle in SQL
// why not support `.` in key?
// FE will add `"`. e.g: `a.b` -> `"a.b"`, in binary `\"a.b\"`
// but BE is hard to handle `"`, because vpackjson don't add escape for `"` and `\`
// input string `a\"b` -> in binary `a\\\"b` -> vpack json binary `a\"b`
// it's take us can't identify `"` and `\` corrently
std::string abs_path = fmt::format("{}.{}", absolute_path, key);
flat_count += _dfs_finalize(child.get(), abs_path, hit_leaf);
} else {
child->remain = true;
}
}
if (flat_count == 0 && !absolute_path.empty()) {
// leaf node or all children is remain
// check sparsity, same key may appear many times in json, so we need avoid duplicate compute hits
auto desc = _derived_maps[node];
if (desc.multi_times <= 0 && desc.hits >= _total_rows * _min_json_sparsity_factory) {
hit_leaf->emplace_back(node, absolute_path);
node->type = flat_json::JSON_BITS_TO_LOGICAL_TYPE.at(desc.type);
node->remain = false;
return 1;
} else {
node->remain = true;
return 0;
}
} else {
node->remain |= (flat_count != node->children.size());
return 1;
}
}
void dfs_add_remain_keys(JsonFlatPath* node, std::set<std::string_view>* remain_keys) {
auto iter = node->children.begin();
while (iter != node->children.end()) {
auto child = iter->second.get();
dfs_add_remain_keys(child, remain_keys);
if (child->remain) {
node->remain |= true;
remain_keys->insert(iter->first);
}
iter++;
}
}
void JsonPathDeriver::_finalize() {
// try downgrade json-uint to bigint
int128_t max = RunTimeTypeLimits<TYPE_BIGINT>::max_value();
for (auto& [name, desc] : _derived_maps) {
if (desc.type == flat_json::JSON_TYPE_BITS.at(vpack::ValueType::UInt) && desc.max <= max) {
desc.type = flat_json::JSON_BIGINT_TYPE_BITS;
}
}
std::vector<std::pair<JsonFlatPath*, std::string>> hit_leaf;
_dfs_finalize(_path_root.get(), "", &hit_leaf);
// sort by name, just for stable order
std::sort(hit_leaf.begin(), hit_leaf.end(), [&](const auto& a, const auto& b) {
auto desc_a = _derived_maps[a.first];
auto desc_b = _derived_maps[b.first];
return desc_a.hits > desc_b.hits;
});
size_t limit = config::json_flat_column_max > 0 ? config::json_flat_column_max : std::numeric_limits<size_t>::max();
for (size_t i = limit; i < hit_leaf.size(); i++) {
if (!hit_leaf[i].first->remain && _derived_maps[hit_leaf[i].first].hits >= _total_rows) {
limit++;
continue;
}
hit_leaf[i].first->remain = true;
}
if (hit_leaf.size() > limit) {
_has_remain |= true;
hit_leaf.resize(limit);
}
std::sort(hit_leaf.begin(), hit_leaf.end(), [](const auto& a, const auto& b) { return a.second < b.second; });
for (auto& [node, path] : hit_leaf) {
node->index = _paths.size();
_paths.emplace_back(path.substr(1));
_types.emplace_back(node->type);
}
std::set<std::string_view> remain_keys;
dfs_add_remain_keys(_path_root.get(), &remain_keys);
_has_remain |= _path_root->remain;
if (_has_remain && _generate_filter) {
double fpp = estimate_filter_fpp(remain_keys.size());
if (fpp < 0) {
return;
}
std::unique_ptr<BloomFilter> bf;
Status st = BloomFilter::create(BLOCK_BLOOM_FILTER, &bf);
DCHECK(st.ok());
st = bf->init(remain_keys.size(), fpp, HASH_MURMUR3_X64_64);
DCHECK(st.ok());
for (const auto& key : remain_keys) {
bf->add_bytes(key.data(), key.size());
}
_remain_filter = std::move(bf);
}
}
JsonFlattener::JsonFlattener(JsonPathDeriver& deriver) {
DCHECK(deriver.flat_path_root() != nullptr);
_dst_paths = deriver.flat_paths();
_has_remain = deriver.has_remain_json();
auto types = deriver.flat_types();
_dst_root = std::make_shared<JsonFlatPath>();
for (size_t i = 0; i < _dst_paths.size(); i++) {
auto* leaf = JsonFlatPath::normalize_from_path(_dst_paths[i], _dst_root.get());
leaf->type = types[i];
leaf->index = i;
_flat_columns.emplace_back(ColumnHelper::create_column(TypeDescriptor(types[i]), true));
}
if (_has_remain) {
_flat_columns.emplace_back(ColumnHelper::create_column(TypeDescriptor(LogicalType::TYPE_JSON), false));
_remain = down_cast<JsonColumn*>(_flat_columns.back().get());
}
}
JsonFlattener::JsonFlattener(const std::vector<std::string>& paths, const std::vector<LogicalType>& types,
bool has_remain)
: _has_remain(has_remain), _dst_paths(std::move(paths)) {
_dst_root = std::make_shared<JsonFlatPath>();
for (size_t i = 0; i < _dst_paths.size(); i++) {
auto* leaf = JsonFlatPath::normalize_from_path(_dst_paths[i], _dst_root.get());
leaf->type = types[i];
leaf->index = i;
_flat_columns.emplace_back(ColumnHelper::create_column(TypeDescriptor(types[i]), true));
}
if (_has_remain) {
_flat_columns.emplace_back(ColumnHelper::create_column(TypeDescriptor(LogicalType::TYPE_JSON), false));
_remain = down_cast<JsonColumn*>(_flat_columns.back().get());
}
}
void JsonFlattener::flatten(const Column* json_column) {
for (auto& col : _flat_columns) {
DCHECK_EQ(col->size(), 0);
col->reserve(json_column->size());
}
// input
const JsonColumn* json_data = nullptr;
if (json_column->is_nullable()) {
// append null column
auto* nullable_column = down_cast<const NullableColumn*>(json_column);
json_data = down_cast<const JsonColumn*>(nullable_column->data_column().get());
} else {
json_data = down_cast<const JsonColumn*>(json_column);
}
// output
if (_has_remain) {
_flatten<true>(json_column, json_data);
for (size_t i = 0; i < _flat_columns.size() - 1; i++) {
down_cast<NullableColumn*>(_flat_columns[i].get())->update_has_null();
}
} else {
_flatten<false>(json_column, json_data);
for (size_t i = 0; i < _flat_columns.size(); i++) {
down_cast<NullableColumn*>(_flat_columns[i].get())->update_has_null();
}
}
for (auto& col : _flat_columns) {
DCHECK_EQ(col->size(), json_column->size());
}
}
template <bool REMAIN>
bool JsonFlattener::_flatten_json(const vpack::Slice& value, const JsonFlatPath* root, vpack::Builder* builder,
uint32_t* hit_count) {
vpack::ObjectIterator it(value, false);
for (; it.valid(); it.next()) {
auto current = (*it);
// sub-object
auto v = current.value;
auto k = current.key.stringView();
auto child = root->children.find(k);
if constexpr (REMAIN) {
if (child == root->children.end()) {
builder->addUnchecked(k.data(), k.size(), v);
continue;
}
} else {
if (*hit_count == _dst_paths.size()) {
return false;
}
if (child == root->children.end()) {
continue;
}
}
if (child->second->children.empty()) {
// leaf node
auto index = child->second->index;
DCHECK(_flat_columns.size() > index);
DCHECK(_flat_columns[index]->is_nullable());
auto* c = down_cast<NullableColumn*>(_flat_columns[index].get());
auto func = flat_json::JSON_EXTRACT_FUNC.at(child->second->type);
func(&v, c);
*hit_count += 1;
// not leaf node, should goto deep
} else if (v.isObject()) {
if constexpr (REMAIN) {
builder->addUnchecked(k.data(), k.size(), vpack::Value(vpack::ValueType::Object));
_flatten_json<REMAIN>(v, child->second.get(), builder, hit_count);
builder->close();
} else {
if (!_flatten_json<REMAIN>(v, child->second.get(), builder, hit_count)) {
return false;
}
}
} else {
if constexpr (REMAIN) {
builder->addUnchecked(k.data(), k.size(), v);
}
}
}
return true;
}
template <bool HAS_REMAIN>
void JsonFlattener::_flatten(const Column* json_column, const JsonColumn* json_data) {
DCHECK(!_dst_paths.empty());
// output
DCHECK_LE(_dst_paths.size(), std::numeric_limits<int>::max());
for (size_t row = 0; row < json_column->size(); row++) {
if (json_column->is_null(row)) {
for (size_t k = 0; k < _flat_columns.size(); k++) { // all is null
_flat_columns[k]->append_default(1);
}
continue;
}
auto* obj = json_data->get_object(row);
auto vslice = obj->to_vslice();
if (vslice.isNone() || vslice.isNull()) {
for (size_t k = 0; k < _flat_columns.size(); k++) { // all is null
_flat_columns[k]->append_default(1);
}
continue;
}
if (vslice.isEmptyObject() || !vslice.isObject()) {
for (size_t k = 0; k < _dst_paths.size(); k++) { // remain push object
_flat_columns[k]->append_default(1);
}
if constexpr (HAS_REMAIN) {
_remain->append(obj);
}
continue;
}
// to count how many columns hit in json, for append default value
uint32_t hit_count = 0;
if constexpr (HAS_REMAIN) {
vpack::Builder builder;
builder.add(vpack::Value(vpack::ValueType::Object));
_flatten_json<HAS_REMAIN>(vslice, _dst_root.get(), &builder, &hit_count);
builder.close();
_remain->append(JsonValue(builder.slice()));
} else {
_flatten_json<HAS_REMAIN>(vslice, _dst_root.get(), nullptr, &hit_count);
}
if (UNLIKELY(hit_count < _dst_paths.size())) {
for (auto& col : _flat_columns) {
if (col->size() != row + 1) {
DCHECK_EQ(col->size(), row);
col->append_default(1);
}
}
}
for (auto& col : _flat_columns) {
DCHECK_EQ(col->size(), row + 1);
}
if constexpr (HAS_REMAIN) {
DCHECK_EQ(row + 1, _remain->size());
}
}
}
Columns JsonFlattener::mutable_result() {
Columns res;
for (size_t i = 0; i < _flat_columns.size(); i++) {
auto cloned = _flat_columns[i]->clone_empty();
res.emplace_back(std::move(_flat_columns[i]));
_flat_columns[i] = std::move(cloned);
}
if (_has_remain) {
_remain = down_cast<JsonColumn*>(_flat_columns.back().get());
}
return res;
}
JsonMerger::JsonMerger(const std::vector<std::string>& paths, const std::vector<LogicalType>& types, bool has_remain)
: _src_paths(std::move(paths)), _has_remain(has_remain) {
_src_root = std::make_shared<JsonFlatPath>();
for (size_t i = 0; i < _src_paths.size(); i++) {
auto* leaf = JsonFlatPath::normalize_from_path(_src_paths[i], _src_root.get());
leaf->type = types[i];
leaf->index = i;
}
}
void JsonMerger::set_exclude_paths(const std::vector<std::string>& exclude_paths) {
this->_exclude_paths = exclude_paths;
for (auto& path : _exclude_paths) {
auto* leaf = JsonFlatPath::normalize_from_path(path, _src_root.get());
leaf->op = JsonFlatPath::OP_EXCLUDE;
}
}
// add all level paths, e.g: a.b.c, level path: a.b, leaf path: c
void JsonMerger::_add_level_paths_impl(const std::string_view& path, JsonFlatPath* root) {
if (path.empty()) {
return;
}
auto [key, next] = JsonFlatPath::split_path(path);
if (next.empty()) {
// don't add leaf node
return;
}
auto iter = root->children.find(key);
JsonFlatPath* child_path = nullptr;
if (iter == root->children.end()) {
root->children.emplace(key, std::make_unique<JsonFlatPath>());
child_path = root->children[key].get();
child_path->op = JsonFlatPath::OP_NEW_LEVEL;
} else {
child_path = iter->second.get();
}
_add_level_paths_impl(next, child_path);
}
void JsonMerger::add_level_paths(const std::vector<std::string>& level_paths) {
this->_level_paths = level_paths;
for (auto& path : _level_paths) {
_add_level_paths_impl(path, _src_root.get());
}
}
void JsonMerger::set_root_path(const std::string& base_path) {
JsonFlatPath::set_root(base_path, _src_root.get());
}
ColumnPtr JsonMerger::merge(const Columns& columns) {
DCHECK_GE(columns.size(), 1);
DCHECK(_src_columns.empty());
_result = NullableColumn::create(JsonColumn::create(), NullColumn::create());
_json_result = down_cast<JsonColumn*>(down_cast<NullableColumn*>(_result.get())->data_column().get());
_null_result = down_cast<NullColumn*>(down_cast<NullableColumn*>(_result.get())->null_column().get());
size_t rows = columns[0]->size();
_result->reserve(rows);
for (auto& col : columns) {
_src_columns.emplace_back(col.get());
}
if (_src_root->op == JsonFlatPath::OP_INCLUDE) {
_merge_impl<true>(rows);
} else {
_merge_impl<false>(rows);
}
_src_columns.clear();
if (_output_nullable) {
down_cast<NullableColumn*>(_result.get())->update_has_null();
return _result;
} else {
return down_cast<NullableColumn*>(_result.get())->data_column();
}
}
template <bool IN_TREE>
void JsonMerger::_merge_impl(size_t rows) {
if (_has_remain) {
auto remain = down_cast<const JsonColumn*>(_src_columns.back());
for (size_t i = 0; i < rows; i++) {
auto obj = remain->get_object(i);
auto vs = obj->to_vslice();
if (obj->is_invalid()) {
vpack::Builder builder;
builder.add(vpack::Value(vpack::ValueType::Object));
_merge_json(_src_root.get(), &builder, i);
builder.close();
auto slice = builder.slice();
_json_result->append(JsonValue(slice));
_null_result->append(slice.isEmptyObject());
} else if (!vs.isObject()) {
for (int k = 0; k < _src_paths.size(); k++) {
// check child column should be null
DCHECK(_src_columns[k]->is_null(i));
}
_json_result->append(JsonValue(vs));
_null_result->append(vs.isEmptyObject());
} else {
vpack::Builder builder;
builder.add(vpack::Value(vpack::ValueType::Object));
_merge_json_with_remain<IN_TREE>(_src_root.get(), &vs, &builder, i);
builder.close();
auto slice = builder.slice();
_json_result->append(JsonValue(slice));
_null_result->append(slice.isEmptyObject());
}
}
} else {
for (size_t i = 0; i < rows; i++) {
vpack::Builder builder;
builder.add(vpack::Value(vpack::ValueType::Object));
_merge_json(_src_root.get(), &builder, i);
builder.close();
_json_result->append(JsonValue(builder.slice()));
}
int zero = 0;
_null_result->append_value_multiple_times(&zero, rows);
}
}
template <bool IN_TREE>
void JsonMerger::_merge_json_with_remain(const JsonFlatPath* root, const vpack::Slice* remain, vpack::Builder* builder,
size_t index) {
// #ifndef NDEBUG
// std::string json = remain->toJson();
// #endif
vpack::ObjectIterator it(*remain, false);
for (; it.valid(); it.next()) {
auto k = it.key().stringView();
auto v = it.value();
auto iter = root->children.find(k);
if (iter == root->children.end()) {
if constexpr (IN_TREE) {
// only remain contains
builder->addUnchecked(k.data(), k.size(), v);
}
continue;
}
auto* child = iter->second.get();
if (child->op == JsonFlatPath::OP_EXCLUDE) {
continue;
}
if (v.isObject()) {
if (child->op == JsonFlatPath::OP_IGNORE) {
_merge_json_with_remain<false>(child, &v, builder, index);
} else if (child->op == JsonFlatPath::OP_ROOT) {
_merge_json_with_remain<true>(child, &v, builder, index);
} else {
DCHECK(child->op == JsonFlatPath::OP_INCLUDE || child->op == JsonFlatPath::OP_NEW_LEVEL);
builder->addUnchecked(k.data(), k.size(), vpack::Value(vpack::ValueType::Object));
_merge_json_with_remain<true>(child, &v, builder, index);
builder->close();
}
continue;
}
// leaf node
DCHECK(child->op == JsonFlatPath::OP_INCLUDE || child->op == JsonFlatPath::OP_ROOT ||
child->op == JsonFlatPath::OP_NEW_LEVEL);
builder->addUnchecked(k.data(), k.size(), v);
}
for (auto& [child_name, child] : root->children) {
if (child->op == JsonFlatPath::OP_EXCLUDE) {
continue;
}