Coverage Report

Created: 2026-05-13 15:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/common/variant_util.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "exec/common/variant_util.h"
19
20
#include <assert.h>
21
#include <fmt/format.h>
22
#include <gen_cpp/FrontendService.h>
23
#include <gen_cpp/FrontendService_types.h>
24
#include <gen_cpp/HeartbeatService_types.h>
25
#include <gen_cpp/MasterService_types.h>
26
#include <gen_cpp/Status_types.h>
27
#include <gen_cpp/Types_types.h>
28
#include <glog/logging.h>
29
#include <rapidjson/document.h>
30
#include <rapidjson/stringbuffer.h>
31
#include <rapidjson/writer.h>
32
#include <simdjson/simdjson.h> // IWYU pragma: keep
33
#include <unicode/uchar.h>
34
35
#include <algorithm>
36
#include <cassert>
37
#include <cstddef>
38
#include <cstdint>
39
#include <cstring>
40
#include <list>
41
#include <memory>
42
#include <mutex>
43
#include <optional>
44
#include <ostream>
45
#include <ranges>
46
#include <set>
47
#include <stack>
48
#include <string>
49
#include <string_view>
50
#include <unordered_map>
51
#include <utility>
52
#include <vector>
53
54
#include "common/config.h"
55
#include "common/status.h"
56
#include "core/assert_cast.h"
57
#include "core/block/block.h"
58
#include "core/block/column_numbers.h"
59
#include "core/block/column_with_type_and_name.h"
60
#include "core/column/column.h"
61
#include "core/column/column_array.h"
62
#include "core/column/column_map.h"
63
#include "core/column/column_nullable.h"
64
#include "core/column/column_string.h"
65
#include "core/column/column_variant.h"
66
#include "core/data_type/data_type.h"
67
#include "core/data_type/data_type_array.h"
68
#include "core/data_type/data_type_factory.hpp"
69
#include "core/data_type/data_type_jsonb.h"
70
#include "core/data_type/data_type_nullable.h"
71
#include "core/data_type/data_type_string.h"
72
#include "core/data_type/data_type_variant.h"
73
#include "core/data_type/define_primitive_type.h"
74
#include "core/data_type/get_least_supertype.h"
75
#include "core/data_type/primitive_type.h"
76
#include "core/field.h"
77
#include "core/typeid_cast.h"
78
#include "core/types.h"
79
#include "exec/common/field_visitors.h"
80
#include "exec/common/sip_hash.h"
81
#include "exprs/function/function.h"
82
#include "exprs/function/simple_function_factory.h"
83
#include "exprs/function_context.h"
84
#include "exprs/json_functions.h"
85
#include "re2/re2.h"
86
#include "runtime/exec_env.h"
87
#include "runtime/runtime_state.h"
88
#include "storage/olap_common.h"
89
#include "storage/rowset/beta_rowset.h"
90
#include "storage/rowset/rowset.h"
91
#include "storage/rowset/rowset_fwd.h"
92
#include "storage/segment/segment_loader.h"
93
#include "storage/segment/variant/nested_group_path.h"
94
#include "storage/segment/variant/variant_column_reader.h"
95
#include "storage/segment/variant/variant_column_writer_impl.h"
96
#include "storage/tablet/tablet.h"
97
#include "storage/tablet/tablet_fwd.h"
98
#include "storage/tablet/tablet_schema.h"
99
#include "util/client_cache.h"
100
#include "util/defer_op.h"
101
#include "util/json/json_parser.h"
102
#include "util/json/path_in_data.h"
103
#include "util/json/simd_json_parser.h"
104
105
namespace doris::variant_util {
106
107
2.76k
inline void append_escaped_regex_char(std::string* regex_output, char ch) {
108
2.76k
    switch (ch) {
109
22
    case '.':
110
24
    case '^':
111
26
    case '$':
112
28
    case '+':
113
34
    case '*':
114
36
    case '?':
115
38
    case '(':
116
40
    case ')':
117
42
    case '|':
118
44
    case '{':
119
46
    case '}':
120
48
    case '[':
121
48
    case ']':
122
52
    case '\\':
123
52
        regex_output->push_back('\\');
124
52
        regex_output->push_back(ch);
125
52
        break;
126
2.71k
    default:
127
2.71k
        regex_output->push_back(ch);
128
2.71k
        break;
129
2.76k
    }
130
2.76k
}
131
132
// Small LRU to cap compiled glob patterns
133
constexpr size_t kGlobRegexCacheCapacity = 256;
134
135
struct GlobRegexCacheEntry {
136
    std::shared_ptr<RE2> re2;
137
    std::list<std::string>::iterator lru_it;
138
};
139
140
static std::mutex g_glob_regex_cache_mutex;
141
static std::list<std::string> g_glob_regex_cache_lru;
142
static std::unordered_map<std::string, GlobRegexCacheEntry> g_glob_regex_cache;
143
144
149k
std::shared_ptr<RE2> get_or_build_re2(const std::string& glob_pattern) {
145
149k
    {
146
149k
        std::lock_guard<std::mutex> lock(g_glob_regex_cache_mutex);
147
149k
        auto it = g_glob_regex_cache.find(glob_pattern);
148
149k
        if (it != g_glob_regex_cache.end()) {
149
149k
            g_glob_regex_cache_lru.splice(g_glob_regex_cache_lru.begin(), g_glob_regex_cache_lru,
150
149k
                                          it->second.lru_it);
151
149k
            return it->second.re2;
152
149k
        }
153
149k
    }
154
136
    std::string regex_pattern;
155
136
    Status st = glob_to_regex(glob_pattern, &regex_pattern);
156
136
    if (!st.ok()) {
157
2
        return nullptr;
158
2
    }
159
134
    auto compiled = std::make_shared<RE2>(regex_pattern);
160
134
    if (!compiled->ok()) {
161
3
        return nullptr;
162
3
    }
163
131
    {
164
131
        std::lock_guard<std::mutex> lock(g_glob_regex_cache_mutex);
165
131
        auto it = g_glob_regex_cache.find(glob_pattern);
166
131
        if (it != g_glob_regex_cache.end()) {
167
0
            g_glob_regex_cache_lru.splice(g_glob_regex_cache_lru.begin(), g_glob_regex_cache_lru,
168
0
                                          it->second.lru_it);
169
0
            return it->second.re2;
170
0
        }
171
131
        g_glob_regex_cache_lru.push_front(glob_pattern);
172
131
        g_glob_regex_cache.emplace(glob_pattern,
173
131
                                   GlobRegexCacheEntry {compiled, g_glob_regex_cache_lru.begin()});
174
131
        if (g_glob_regex_cache.size() > kGlobRegexCacheCapacity) {
175
0
            const std::string& evict_key = g_glob_regex_cache_lru.back();
176
0
            g_glob_regex_cache.erase(evict_key);
177
0
            g_glob_regex_cache_lru.pop_back();
178
0
        }
179
131
    }
180
0
    return compiled;
181
131
}
182
183
// Convert a restricted glob pattern into a regex.
184
// Supported: '*', '?', '[...]', '\\' escape. Others are treated as literals.
185
289
Status glob_to_regex(const std::string& glob_pattern, std::string* regex_pattern) {
186
289
    regex_pattern->clear();
187
289
    regex_pattern->append("^");
188
289
    bool is_escaped = false;
189
289
    size_t pattern_length = glob_pattern.size();
190
3.17k
    for (size_t index = 0; index < pattern_length; ++index) {
191
2.89k
        char current_char = glob_pattern[index];
192
2.89k
        if (is_escaped) {
193
10
            append_escaped_regex_char(regex_pattern, current_char);
194
10
            is_escaped = false;
195
10
            continue;
196
10
        }
197
2.88k
        if (current_char == '\\') {
198
14
            is_escaped = true;
199
14
            continue;
200
14
        }
201
2.86k
        if (current_char == '*') {
202
68
            regex_pattern->append(".*");
203
68
            continue;
204
68
        }
205
2.79k
        if (current_char == '?') {
206
15
            regex_pattern->append(".");
207
15
            continue;
208
15
        }
209
2.78k
        if (current_char == '[') {
210
33
            size_t class_index = index + 1;
211
33
            bool class_closed = false;
212
33
            bool is_class_escaped = false;
213
33
            std::string class_buffer;
214
33
            if (class_index < pattern_length &&
215
33
                (glob_pattern[class_index] == '!' || glob_pattern[class_index] == '^')) {
216
9
                class_buffer.push_back('^');
217
9
                ++class_index;
218
9
            }
219
99
            for (; class_index < pattern_length; ++class_index) {
220
95
                char class_char = glob_pattern[class_index];
221
95
                if (is_class_escaped) {
222
10
                    class_buffer.push_back(class_char);
223
10
                    is_class_escaped = false;
224
10
                    continue;
225
10
                }
226
85
                if (class_char == '\\') {
227
10
                    is_class_escaped = true;
228
10
                    continue;
229
10
                }
230
75
                if (class_char == ']') {
231
29
                    class_closed = true;
232
29
                    break;
233
29
                }
234
46
                class_buffer.push_back(class_char);
235
46
            }
236
33
            if (!class_closed) {
237
4
                return Status::InvalidArgument("Unclosed character class in glob pattern: {}",
238
4
                                               glob_pattern);
239
4
            }
240
29
            regex_pattern->append("[");
241
29
            regex_pattern->append(class_buffer);
242
29
            regex_pattern->append("]");
243
29
            index = class_index;
244
29
            continue;
245
33
        }
246
2.75k
        append_escaped_regex_char(regex_pattern, current_char);
247
2.75k
    }
248
285
    if (is_escaped) {
249
4
        append_escaped_regex_char(regex_pattern, '\\');
250
4
    }
251
285
    regex_pattern->append("$");
252
285
    return Status::OK();
253
289
}
254
255
149k
bool glob_match_re2(const std::string& glob_pattern, const std::string& candidate_path) {
256
149k
    auto compiled = get_or_build_re2(glob_pattern);
257
149k
    if (compiled == nullptr) {
258
5
        return false;
259
5
    }
260
149k
    return RE2::FullMatch(candidate_path, *compiled);
261
149k
}
262
263
// NestedGroup's physical children and offsets are produced by NestedGroupWriteProvider, not by
264
// appending TabletSchema extracted columns here. This predicate keeps only ordinary Variant paths
265
// that are outside the NG tree, for example `v.owner` beside `v.items[*]`.
266
0
bool is_regular_path_outside_nested_group(const PathInData& path) {
267
0
    const std::string& relative_path = path.get_path();
268
0
    return !relative_path.empty() && !path.get_is_typed() && !path.has_nested_part() &&
269
0
           !segment_v2::contains_nested_group_marker(relative_path) &&
270
0
           !segment_v2::is_root_nested_group_path(relative_path) &&
271
0
           relative_path != SPARSE_COLUMN_PATH &&
272
0
           relative_path.find(DOC_VALUE_COLUMN_PATH) == std::string::npos;
273
0
}
274
275
bool should_materialize_nested_group_regular_subcolumns(
276
        const TabletColumnPtr& column,
277
632
        const std::unordered_map<int32_t, VariantExtendedInfo>& uid_to_variant_extended_info) {
278
632
    const auto info_it = uid_to_variant_extended_info.find(column->unique_id());
279
632
    return column->variant_enable_nested_group() ||
280
632
           (info_it != uid_to_variant_extended_info.end() && info_it->second.has_nested_group);
281
632
}
282
283
std::unordered_set<int32_t> collect_nested_group_compaction_root_uids(
284
        const TabletSchemaSPtr& target,
285
7.65k
        const std::unordered_map<int32_t, VariantExtendedInfo>& uid_to_variant_extended_info) {
286
7.65k
    std::unordered_set<int32_t> root_uids;
287
87.6k
    for (const TabletColumnPtr& column : target->columns()) {
288
87.6k
        if (column->is_variant_type() && should_materialize_nested_group_regular_subcolumns(
289
632
                                                 column, uid_to_variant_extended_info)) {
290
1
            root_uids.insert(column->unique_id());
291
1
        }
292
87.6k
    }
293
7.65k
    return root_uids;
294
7.65k
}
295
296
PathToDataTypes collect_regular_types_outside_nested_group(
297
1
        const VariantExtendedInfo& extended_info) {
298
1
    PathToDataTypes regular_path_to_data_types;
299
1
    for (const auto& [path, data_types] : extended_info.path_to_data_types) {
300
0
        if (!is_regular_path_outside_nested_group(path)) {
301
0
            continue;
302
0
        }
303
0
        regular_path_to_data_types.emplace(path, data_types);
304
0
    }
305
1
    return regular_path_to_data_types;
306
1
}
307
308
966
size_t get_number_of_dimensions(const IDataType& type) {
309
966
    if (const auto* type_array = typeid_cast<const DataTypeArray*>(&type)) {
310
4
        return type_array->get_number_of_dimensions();
311
4
    }
312
962
    return 0;
313
966
}
314
3
size_t get_number_of_dimensions(const IColumn& column) {
315
3
    if (const auto* column_array = check_and_get_column<ColumnArray>(column)) {
316
2
        return column_array->get_number_of_dimensions();
317
2
    }
318
1
    return 0;
319
3
}
320
321
76.7k
DataTypePtr get_base_type_of_array(const DataTypePtr& type) {
322
    /// Get raw pointers to avoid extra copying of type pointers.
323
76.7k
    const DataTypeArray* last_array = nullptr;
324
76.7k
    const auto* current_type = type.get();
325
76.7k
    if (const auto* nullable = typeid_cast<const DataTypeNullable*>(current_type)) {
326
76.7k
        current_type = nullable->get_nested_type().get();
327
76.7k
    }
328
78.0k
    while (const auto* type_array = typeid_cast<const DataTypeArray*>(current_type)) {
329
1.29k
        current_type = type_array->get_nested_type().get();
330
1.29k
        last_array = type_array;
331
1.29k
        if (const auto* nullable = typeid_cast<const DataTypeNullable*>(current_type)) {
332
1.29k
            current_type = nullable->get_nested_type().get();
333
1.29k
        }
334
1.29k
    }
335
76.7k
    return last_array ? last_array->get_nested_type() : type;
336
76.7k
}
337
338
76.1k
Status cast_column(const ColumnWithTypeAndName& arg, const DataTypePtr& type, ColumnPtr* result) {
339
76.1k
    ColumnsWithTypeAndName arguments {arg, {nullptr, type, type->get_name()}};
340
341
    // To prevent from null info lost, we should not call function since the function framework will wrap
342
    // nullable to Variant instead of the root of Variant
343
    // correct output: Nullable(Array(int)) -> Nullable(Variant(Nullable(Array(int))))
344
    // incorrect output: Nullable(Array(int)) -> Nullable(Variant(Array(int)))
345
76.1k
    if (type->get_primitive_type() == TYPE_VARIANT) {
346
        // If source column is variant, so the nullable info is different from dst column
347
17.7k
        if (arg.type->get_primitive_type() == TYPE_VARIANT) {
348
555
            *result = type->is_nullable() ? make_nullable(arg.column) : remove_nullable(arg.column);
349
555
            return Status::OK();
350
555
        }
351
        // set variant root column/type to from column/type
352
17.7k
        CHECK(arg.column->is_nullable());
353
17.1k
        auto to_type = remove_nullable(type);
354
17.1k
        const auto& data_type_object = assert_cast<const DataTypeVariant&>(*to_type);
355
17.1k
        auto variant = ColumnVariant::create(data_type_object.variant_max_subcolumns_count(),
356
17.1k
                                             data_type_object.enable_doc_mode());
357
358
17.1k
        variant->create_root(arg.type, arg.column->assume_mutable());
359
17.1k
        ColumnPtr nullable = ColumnNullable::create(
360
17.1k
                variant->get_ptr(),
361
17.1k
                check_and_get_column<ColumnNullable>(arg.column.get())->get_null_map_column_ptr());
362
17.1k
        *result = type->is_nullable() ? nullable : variant->get_ptr();
363
17.1k
        return Status::OK();
364
17.7k
    }
365
366
58.4k
    auto function = SimpleFunctionFactory::instance().get_function("CAST", arguments, type);
367
58.4k
    if (!function) {
368
0
        return Status::InternalError("Not found cast function {} to {}", arg.type->get_name(),
369
0
                                     type->get_name());
370
0
    }
371
58.4k
    Block tmp_block {arguments};
372
58.4k
    uint32_t result_column = cast_set<uint32_t>(tmp_block.columns());
373
58.4k
    RuntimeState state;
374
58.4k
    auto ctx = FunctionContext::create_context(&state, {}, {});
375
376
58.4k
    if (arg.type->get_primitive_type() == INVALID_TYPE) {
377
        // cast from nothing to any type should result in nulls
378
5.85k
        *result = type->create_column_const_with_default_value(arg.column->size())
379
5.85k
                          ->convert_to_full_column_if_const();
380
5.85k
        return Status::OK();
381
5.85k
    }
382
383
    // We convert column string to jsonb type just add a string jsonb field to dst column instead of parse
384
    // each line in original string column.
385
52.6k
    ctx->set_string_as_jsonb_string(true);
386
52.6k
    ctx->set_jsonb_string_as_string(true);
387
52.6k
    tmp_block.insert({nullptr, type, arg.name});
388
    // TODO(lihangyu): we should handle this error in strict mode
389
52.6k
    if (!function->execute(ctx.get(), tmp_block, {0}, result_column, arg.column->size())) {
390
1
        LOG_EVERY_N(WARNING, 100) << fmt::format("cast from {} to {}", arg.type->get_name(),
391
1
                                                 type->get_name());
392
1
        *result = type->create_column_const_with_default_value(arg.column->size())
393
1
                          ->convert_to_full_column_if_const();
394
1
        return Status::OK();
395
1
    }
396
52.6k
    *result = tmp_block.get_by_position(result_column).column->convert_to_full_column_if_const();
397
52.6k
    VLOG_DEBUG << fmt::format("{} before convert {}, after convert {}", arg.name,
398
4
                              arg.column->get_name(), (*result)->get_name());
399
52.6k
    return Status::OK();
400
52.6k
}
401
402
void get_column_by_type(const DataTypePtr& data_type, const std::string& name, TabletColumn& column,
403
155k
                        const ExtraInfo& ext_info) {
404
155k
    column.set_name(name);
405
155k
    column.set_type(data_type->get_storage_field_type());
406
155k
    if (ext_info.unique_id >= 0) {
407
4
        column.set_unique_id(ext_info.unique_id);
408
4
    }
409
155k
    if (ext_info.parent_unique_id >= 0) {
410
76.3k
        column.set_parent_unique_id(ext_info.parent_unique_id);
411
76.3k
    }
412
155k
    if (!ext_info.path_info.empty()) {
413
76.3k
        column.set_path_info(ext_info.path_info);
414
76.3k
    }
415
155k
    if (data_type->is_nullable()) {
416
77.5k
        const auto& real_type = static_cast<const DataTypeNullable&>(*data_type);
417
77.5k
        column.set_is_nullable(true);
418
77.5k
        get_column_by_type(real_type.get_nested_type(), name, column, {});
419
77.5k
        return;
420
77.5k
    }
421
77.5k
    if (data_type->get_primitive_type() == PrimitiveType::TYPE_ARRAY) {
422
1.25k
        TabletColumn child;
423
1.25k
        get_column_by_type(assert_cast<const DataTypeArray*>(data_type.get())->get_nested_type(),
424
1.25k
                           "", child, {});
425
1.25k
        column.set_length(TabletColumn::get_field_length_by_type(TPrimitiveType::ARRAY, 0));
426
1.25k
        column.add_sub_column(child);
427
1.25k
        return;
428
1.25k
    }
429
76.2k
    if (data_type->get_primitive_type() == PrimitiveType::TYPE_VARIANT) {
430
0
        const auto* dt_variant = assert_cast<const DataTypeVariant*>(data_type.get());
431
0
        column.set_variant_max_subcolumns_count(dt_variant->variant_max_subcolumns_count());
432
0
        column.set_variant_enable_doc_mode(dt_variant->enable_doc_mode());
433
0
        return;
434
0
    }
435
    // size is not fixed when type is string or json
436
76.2k
    if (is_string_type(data_type->get_primitive_type()) ||
437
76.2k
        data_type->get_primitive_type() == TYPE_JSONB) {
438
37.9k
        column.set_length(INT_MAX);
439
37.9k
        return;
440
37.9k
    }
441
442
38.3k
    PrimitiveType type = data_type->get_primitive_type();
443
38.3k
    if (is_int_or_bool(type) || is_string_type(type) || is_float_or_double(type) || is_ip(type) ||
444
38.3k
        is_date_or_datetime(type) || type == PrimitiveType::TYPE_DATEV2) {
445
38.2k
        column.set_length(cast_set<int32_t>(data_type->get_size_of_value_in_memory()));
446
38.2k
        return;
447
38.2k
    }
448
105
    if (is_decimal(type)) {
449
105
        column.set_precision(data_type->get_precision());
450
105
        column.set_frac(data_type->get_scale());
451
105
        return;
452
105
    }
453
    // datetimev2 needs scale
454
18.4E
    if (type == PrimitiveType::TYPE_DATETIMEV2 || type == PrimitiveType::TYPE_TIMESTAMPTZ) {
455
17
        column.set_precision(-1);
456
17
        column.set_frac(data_type->get_scale());
457
17
        return;
458
17
    }
459
460
18.4E
    throw doris::Exception(doris::ErrorCode::INTERNAL_ERROR,
461
18.4E
                           "unexcepted data column type: {}, column name is: {}",
462
18.4E
                           data_type->get_name(), name);
463
18.4E
}
464
465
TabletColumn get_column_by_type(const DataTypePtr& data_type, const std::string& name,
466
75.4k
                                const ExtraInfo& ext_info) {
467
75.4k
    TabletColumn result;
468
75.4k
    get_column_by_type(data_type, name, result, ext_info);
469
75.4k
    return result;
470
75.4k
}
471
472
// check if two paths which same prefix have different structure
473
static bool has_different_structure_in_same_path(const PathInData::Parts& lhs,
474
9.04k
                                                 const PathInData::Parts& rhs) {
475
9.04k
    if (lhs.size() != rhs.size()) {
476
1
        return false; // different size means different structure
477
1
    }
478
    // Since we group by path string, lhs and rhs must have the same size and keys
479
    // We only need to check if they have different nested structure
480
36.1k
    for (size_t i = 0; i < lhs.size(); ++i) {
481
27.0k
        if (lhs[i] != rhs[i]) {
482
5
            VLOG_DEBUG << fmt::format(
483
0
                    "Check different structure: {} vs {}, lhs[i].is_nested: {}, rhs[i].is_nested: "
484
0
                    "{}",
485
0
                    lhs[i].key, rhs[i].key, lhs[i].is_nested, rhs[i].is_nested);
486
5
            return true;
487
5
        }
488
27.0k
    }
489
9.03k
    return false;
490
9.04k
}
491
492
4.75k
Status check_variant_has_no_ambiguous_paths(const PathsInData& tuple_paths) {
493
    // Group paths by their string representation to reduce comparisons
494
4.75k
    std::unordered_map<std::string, std::vector<size_t>> path_groups;
495
496
26.8k
    for (size_t i = 0; i < tuple_paths.size(); ++i) {
497
        // same path should have same structure, so we group them by path
498
22.0k
        path_groups[tuple_paths[i].get_path()].push_back(i);
499
        // print part of tuple_paths[i]
500
22.0k
        VLOG_DEBUG << "tuple_paths[i]: " << tuple_paths[i].get_path();
501
22.0k
    }
502
503
    // Only compare paths within the same group
504
13.0k
    for (const auto& [path_str, indices] : path_groups) {
505
13.0k
        if (indices.size() <= 1) {
506
3.99k
            continue; // No conflicts possible
507
3.99k
        }
508
509
        // Compare all pairs within this group
510
27.0k
        for (size_t i = 0; i < indices.size(); ++i) {
511
27.0k
            for (size_t j = 0; j < i; ++j) {
512
9.04k
                if (has_different_structure_in_same_path(tuple_paths[indices[i]].get_parts(),
513
9.04k
                                                         tuple_paths[indices[j]].get_parts())) {
514
5
                    return Status::DataQualityError(
515
5
                            "Ambiguous paths: {} vs {} with different nested part {} vs {}",
516
5
                            tuple_paths[indices[i]].get_path(), tuple_paths[indices[j]].get_path(),
517
5
                            tuple_paths[indices[i]].has_nested_part(),
518
5
                            tuple_paths[indices[j]].has_nested_part());
519
5
                }
520
9.04k
            }
521
18.0k
        }
522
9.01k
    }
523
4.75k
    return Status::OK();
524
4.75k
}
525
526
Status update_least_schema_internal(const std::map<PathInData, DataTypes>& subcolumns_types,
527
                                    TabletSchemaSPtr& common_schema, int32_t variant_col_unique_id,
528
                                    const std::map<std::string, TabletColumnPtr>& typed_columns,
529
1.70k
                                    std::set<PathInData>* path_set) {
530
1.70k
    PathsInData tuple_paths;
531
1.70k
    DataTypes tuple_types;
532
1.70k
    CHECK(common_schema.use_count() == 1);
533
    // Get the least common type for all paths.
534
1.70k
    for (const auto& [key, subtypes] : subcolumns_types) {
535
925
        assert(!subtypes.empty());
536
925
        if (key.get_path() == ColumnVariant::COLUMN_NAME_DUMMY) {
537
0
            continue;
538
0
        }
539
925
        size_t first_dim = get_number_of_dimensions(*subtypes[0]);
540
925
        tuple_paths.emplace_back(key);
541
952
        for (size_t i = 1; i < subtypes.size(); ++i) {
542
27
            if (first_dim != get_number_of_dimensions(*subtypes[i])) {
543
0
                tuple_types.emplace_back(make_nullable(std::make_shared<DataTypeJsonb>()));
544
0
                LOG(INFO) << fmt::format(
545
0
                        "Uncompatible types of subcolumn '{}': {} and {}, cast to JSONB",
546
0
                        key.get_path(), subtypes[0]->get_name(), subtypes[i]->get_name());
547
0
                break;
548
0
            }
549
27
        }
550
925
        if (tuple_paths.size() == tuple_types.size()) {
551
0
            continue;
552
0
        }
553
925
        DataTypePtr common_type;
554
925
        get_least_supertype_jsonb(subtypes, &common_type);
555
925
        if (!common_type->is_nullable()) {
556
0
            common_type = make_nullable(common_type);
557
0
        }
558
925
        tuple_types.emplace_back(common_type);
559
925
    }
560
1.70k
    CHECK_EQ(tuple_paths.size(), tuple_types.size());
561
562
    // Append all common type columns of this variant
563
2.63k
    for (int i = 0; i < tuple_paths.size(); ++i) {
564
925
        TabletColumn common_column;
565
        // typed path not contains root part
566
925
        auto path_without_root = tuple_paths[i].copy_pop_front().get_path();
567
925
        if (typed_columns.contains(path_without_root) && !tuple_paths[i].has_nested_part()) {
568
0
            common_column = *typed_columns.at(path_without_root);
569
            // parent unique id and path may not be init in write path
570
0
            common_column.set_parent_unique_id(variant_col_unique_id);
571
0
            common_column.set_path_info(tuple_paths[i]);
572
0
            common_column.set_name(tuple_paths[i].get_path());
573
925
        } else {
574
            // const std::string& column_name = variant_col_name + "." + tuple_paths[i].get_path();
575
925
            get_column_by_type(tuple_types[i], tuple_paths[i].get_path(), common_column,
576
925
                               ExtraInfo {.unique_id = -1,
577
925
                                          .parent_unique_id = variant_col_unique_id,
578
925
                                          .path_info = tuple_paths[i]});
579
925
        }
580
925
        common_schema->append_column(common_column);
581
925
        if (path_set != nullptr) {
582
925
            path_set->insert(tuple_paths[i]);
583
925
        }
584
925
    }
585
1.70k
    return Status::OK();
586
1.70k
}
587
588
Status update_least_common_schema(const std::vector<TabletSchemaSPtr>& schemas,
589
                                  TabletSchemaSPtr& common_schema, int32_t variant_col_unique_id,
590
1.71k
                                  std::set<PathInData>* path_set) {
591
1.71k
    std::map<std::string, TabletColumnPtr> typed_columns;
592
1.71k
    for (const TabletColumnPtr& col :
593
7.82k
         common_schema->column_by_uid(variant_col_unique_id).get_sub_columns()) {
594
7.82k
        typed_columns[col->name()] = col;
595
7.82k
    }
596
    // Types of subcolumns by path from all tuples.
597
1.71k
    std::map<PathInData, DataTypes> subcolumns_types;
598
599
    // Collect all paths first to enable batch checking
600
1.71k
    std::vector<PathInData> all_paths;
601
602
1.84k
    for (const TabletSchemaSPtr& schema : schemas) {
603
5.47k
        for (const TabletColumnPtr& col : schema->columns()) {
604
            // Get subcolumns of this variant
605
5.47k
            if (col->has_path_info() && col->parent_unique_id() >= 0 &&
606
5.47k
                col->parent_unique_id() == variant_col_unique_id) {
607
958
                subcolumns_types[*col->path_info_ptr()].emplace_back(
608
958
                        DataTypeFactory::instance().create_data_type(*col, col->is_nullable()));
609
958
                all_paths.push_back(*col->path_info_ptr());
610
958
            }
611
5.47k
        }
612
1.84k
    }
613
614
    // Batch check for conflicts
615
1.71k
    RETURN_IF_ERROR(check_variant_has_no_ambiguous_paths(all_paths));
616
617
1.71k
    return update_least_schema_internal(subcolumns_types, common_schema, variant_col_unique_id,
618
1.71k
                                        typed_columns, path_set);
619
1.71k
}
620
621
// Keep variant subcolumn BF support aligned with FE DDL checks.
622
86.1k
bool is_bf_supported_by_fe_for_variant_subcolumn(FieldType type) {
623
86.1k
    switch (type) {
624
91
    case FieldType::OLAP_FIELD_TYPE_SMALLINT:
625
441
    case FieldType::OLAP_FIELD_TYPE_INT:
626
33.0k
    case FieldType::OLAP_FIELD_TYPE_BIGINT:
627
33.1k
    case FieldType::OLAP_FIELD_TYPE_LARGEINT:
628
33.1k
    case FieldType::OLAP_FIELD_TYPE_CHAR:
629
33.1k
    case FieldType::OLAP_FIELD_TYPE_VARCHAR:
630
70.9k
    case FieldType::OLAP_FIELD_TYPE_STRING:
631
70.9k
    case FieldType::OLAP_FIELD_TYPE_DATE:
632
70.9k
    case FieldType::OLAP_FIELD_TYPE_DATETIME:
633
71.1k
    case FieldType::OLAP_FIELD_TYPE_DATEV2:
634
71.4k
    case FieldType::OLAP_FIELD_TYPE_DATETIMEV2:
635
71.4k
    case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ:
636
71.4k
    case FieldType::OLAP_FIELD_TYPE_DECIMAL:
637
71.5k
    case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
638
71.6k
    case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
639
71.8k
    case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
640
71.9k
    case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
641
72.2k
    case FieldType::OLAP_FIELD_TYPE_IPV4:
642
72.3k
    case FieldType::OLAP_FIELD_TYPE_IPV6:
643
72.3k
        return true;
644
13.7k
    default:
645
13.7k
        return false;
646
86.1k
    }
647
86.1k
}
648
649
void inherit_column_attributes(const TabletColumn& source, TabletColumn& target,
650
86.1k
                               TabletSchemaSPtr* target_schema) {
651
86.1k
    if (!target.is_extracted_column()) {
652
0
        return;
653
0
    }
654
86.1k
    target.set_aggregation_method(source.aggregation());
655
656
    // 1. bloom filter
657
86.1k
    if (is_bf_supported_by_fe_for_variant_subcolumn(target.type())) {
658
72.3k
        target.set_is_bf_column(source.is_bf_column());
659
72.3k
    }
660
661
86.1k
    if (!target_schema) {
662
78.8k
        return;
663
78.8k
    }
664
665
    // 2. inverted index
666
7.25k
    TabletIndexes indexes_to_add;
667
7.25k
    auto source_indexes = (*target_schema)->inverted_indexs(source.unique_id());
668
    // if target is variant type, we need to inherit all indexes
669
    // because this schema is a read schema from fe
670
7.25k
    if (target.is_variant_type()) {
671
6.34k
        for (auto& index : source_indexes) {
672
302
            auto index_info = std::make_shared<TabletIndex>(*index);
673
302
            index_info->set_escaped_escaped_index_suffix_path(target.path_info_ptr()->get_path());
674
302
            indexes_to_add.emplace_back(std::move(index_info));
675
302
        }
676
6.34k
    } else {
677
909
        inherit_index(source_indexes, indexes_to_add, target);
678
909
    }
679
7.25k
    auto target_indexes = (*target_schema)
680
7.25k
                                  ->inverted_indexs(target.parent_unique_id(),
681
7.25k
                                                    target.path_info_ptr()->get_path());
682
7.27k
    if (target_indexes.empty()) {
683
7.27k
        for (auto& index_info : indexes_to_add) {
684
300
            (*target_schema)->append_index(std::move(*index_info));
685
300
        }
686
7.27k
    }
687
688
    // 3. TODO: gnragm bf index
689
7.25k
}
690
691
7.92k
void inherit_column_attributes(TabletSchemaSPtr& schema) {
692
    // Add index meta if extracted column is missing index meta
693
91.4k
    for (size_t i = 0; i < schema->num_columns(); ++i) {
694
83.4k
        TabletColumn& col = schema->mutable_column(i);
695
83.4k
        if (!col.is_extracted_column()) {
696
76.2k
            continue;
697
76.2k
        }
698
7.27k
        if (schema->field_index(col.parent_unique_id()) == -1) {
699
            // parent column is missing, maybe dropped
700
0
            continue;
701
0
        }
702
7.27k
        inherit_column_attributes(schema->column_by_uid(col.parent_unique_id()), col, &schema);
703
7.27k
    }
704
7.92k
}
705
706
Status get_least_common_schema(const std::vector<TabletSchemaSPtr>& schemas,
707
                               const TabletSchemaSPtr& base_schema, TabletSchemaSPtr& output_schema,
708
1.67k
                               bool check_schema_size) {
709
1.67k
    std::vector<int32_t> variant_column_unique_id;
710
    // Construct a schema excluding the extracted columns and gather unique identifiers for variants.
711
    // Ensure that the output schema also excludes these extracted columns. This approach prevents
712
    // duplicated paths following the update_least_common_schema process.
713
1.67k
    auto build_schema_without_extracted_columns = [&](const TabletSchemaSPtr& base_schema) {
714
1.67k
        output_schema = std::make_shared<TabletSchema>();
715
        // not copy columns but only shadow copy other attributes
716
1.67k
        output_schema->shawdow_copy_without_columns(*base_schema);
717
        // Get all columns without extracted columns and collect variant col unique id
718
4.20k
        for (const TabletColumnPtr& col : base_schema->columns()) {
719
4.20k
            if (col->is_variant_type()) {
720
1.71k
                variant_column_unique_id.push_back(col->unique_id());
721
1.71k
            }
722
4.20k
            if (!col->is_extracted_column()) {
723
3.66k
                output_schema->append_column(*col);
724
3.66k
            }
725
4.20k
        }
726
1.67k
    };
727
1.67k
    if (base_schema == nullptr) {
728
        // Pick tablet schema with max schema version
729
264
        auto max_version_schema =
730
264
                *std::max_element(schemas.cbegin(), schemas.cend(),
731
1.35k
                                  [](const TabletSchemaSPtr a, const TabletSchemaSPtr b) {
732
1.35k
                                      return a->schema_version() < b->schema_version();
733
1.35k
                                  });
734
264
        CHECK(max_version_schema);
735
264
        build_schema_without_extracted_columns(max_version_schema);
736
1.41k
    } else {
737
        // use input base_schema schema as base schema
738
1.41k
        build_schema_without_extracted_columns(base_schema);
739
1.41k
    }
740
741
1.71k
    for (int32_t unique_id : variant_column_unique_id) {
742
1.71k
        std::set<PathInData> path_set;
743
1.71k
        RETURN_IF_ERROR(update_least_common_schema(schemas, output_schema, unique_id, &path_set));
744
1.71k
    }
745
746
1.67k
    inherit_column_attributes(output_schema);
747
1.67k
    if (check_schema_size &&
748
1.67k
        output_schema->columns().size() > config::variant_max_merged_tablet_schema_size) {
749
0
        return Status::DataQualityError("Reached max column size limit {}",
750
0
                                        config::variant_max_merged_tablet_schema_size);
751
0
    }
752
753
1.67k
    return Status::OK();
754
1.67k
}
755
756
// sort by paths in lexicographical order
757
8.94k
ColumnVariant::Subcolumns get_sorted_subcolumns(const ColumnVariant::Subcolumns& subcolumns) {
758
    // sort by paths in lexicographical order
759
8.94k
    ColumnVariant::Subcolumns sorted = subcolumns;
760
1.79M
    std::sort(sorted.begin(), sorted.end(), [](const auto& lhsItem, const auto& rhsItem) {
761
1.79M
        return lhsItem->path < rhsItem->path;
762
1.79M
    });
763
8.94k
    return sorted;
764
8.94k
}
765
766
bool has_schema_index_diff(const TabletSchema* new_schema, const TabletSchema* old_schema,
767
21.7k
                           int32_t new_col_idx, int32_t old_col_idx) {
768
21.7k
    const auto& column_new = new_schema->column(new_col_idx);
769
21.7k
    const auto& column_old = old_schema->column(old_col_idx);
770
771
21.7k
    if (column_new.is_bf_column() != column_old.is_bf_column()) {
772
94
        return true;
773
94
    }
774
775
21.6k
    auto new_schema_inverted_indexs = new_schema->inverted_indexs(column_new);
776
21.6k
    auto old_schema_inverted_indexs = old_schema->inverted_indexs(column_old);
777
778
21.6k
    if (new_schema_inverted_indexs.size() != old_schema_inverted_indexs.size()) {
779
696
        return true;
780
696
    }
781
782
21.3k
    for (size_t i = 0; i < new_schema_inverted_indexs.size(); ++i) {
783
388
        if (!new_schema_inverted_indexs[i]->is_same_except_id(old_schema_inverted_indexs[i])) {
784
19
            return true;
785
19
        }
786
388
    }
787
788
20.9k
    return false;
789
20.9k
}
790
791
2.54k
TabletColumn create_sparse_column(const TabletColumn& variant) {
792
2.54k
    TabletColumn res;
793
2.54k
    res.set_name(variant.name_lower_case() + "." + SPARSE_COLUMN_PATH);
794
2.54k
    res.set_type(FieldType::OLAP_FIELD_TYPE_MAP);
795
2.54k
    res.set_aggregation_method(variant.aggregation());
796
2.54k
    res.set_path_info(PathInData {variant.name_lower_case() + "." + SPARSE_COLUMN_PATH});
797
2.54k
    res.set_parent_unique_id(variant.unique_id());
798
    // set default value to "NULL" DefaultColumnIterator will call insert_many_defaults
799
2.54k
    res.set_default_value("NULL");
800
2.54k
    TabletColumn child_tcolumn;
801
2.54k
    child_tcolumn.set_type(FieldType::OLAP_FIELD_TYPE_STRING);
802
2.54k
    res.add_sub_column(child_tcolumn);
803
2.54k
    res.add_sub_column(child_tcolumn);
804
2.54k
    return res;
805
2.54k
}
806
807
11.4k
TabletColumn create_sparse_shard_column(const TabletColumn& variant, int bucket_index) {
808
11.4k
    TabletColumn res;
809
11.4k
    std::string name = variant.name_lower_case() + "." + SPARSE_COLUMN_PATH + ".b" +
810
11.4k
                       std::to_string(bucket_index);
811
11.4k
    res.set_name(name);
812
11.4k
    res.set_type(FieldType::OLAP_FIELD_TYPE_MAP);
813
11.4k
    res.set_aggregation_method(variant.aggregation());
814
11.4k
    res.set_parent_unique_id(variant.unique_id());
815
11.4k
    res.set_default_value("NULL");
816
11.4k
    PathInData path(name);
817
11.4k
    res.set_path_info(path);
818
11.4k
    TabletColumn child_tcolumn;
819
11.4k
    child_tcolumn.set_type(FieldType::OLAP_FIELD_TYPE_STRING);
820
11.4k
    res.add_sub_column(child_tcolumn);
821
11.4k
    res.add_sub_column(child_tcolumn);
822
11.4k
    return res;
823
11.4k
}
824
825
11.3k
TabletColumn create_doc_value_column(const TabletColumn& variant, int bucket_index) {
826
11.3k
    TabletColumn res;
827
11.3k
    std::string name = variant.name_lower_case() + "." + DOC_VALUE_COLUMN_PATH + ".b" +
828
11.3k
                       std::to_string(bucket_index);
829
11.3k
    res.set_name(name);
830
11.3k
    res.set_type(FieldType::OLAP_FIELD_TYPE_MAP);
831
11.3k
    res.set_aggregation_method(variant.aggregation());
832
11.3k
    res.set_parent_unique_id(variant.unique_id());
833
11.3k
    res.set_default_value("NULL");
834
11.3k
    res.set_path_info(PathInData {name});
835
836
11.3k
    TabletColumn child_tcolumn;
837
11.3k
    child_tcolumn.set_type(FieldType::OLAP_FIELD_TYPE_STRING);
838
11.3k
    res.add_sub_column(child_tcolumn);
839
11.3k
    res.add_sub_column(child_tcolumn);
840
11.3k
    return res;
841
11.3k
}
842
843
88.5k
uint32_t variant_binary_shard_of(const StringRef& path, uint32_t bucket_num) {
844
88.5k
    if (bucket_num <= 1) return 0;
845
51.1k
    SipHash hash;
846
51.1k
    hash.update(path.data, path.size);
847
51.1k
    uint64_t h = hash.get64();
848
51.1k
    return static_cast<uint32_t>(h % bucket_num);
849
88.5k
}
850
851
Status VariantCompactionUtil::aggregate_path_to_stats(
852
        const RowsetSharedPtr& rs,
853
3.21k
        std::unordered_map<int32_t, PathToNoneNullValues>* uid_to_path_stats) {
854
3.21k
    SegmentCacheHandle segment_cache;
855
3.21k
    RETURN_IF_ERROR(SegmentLoader::instance()->load_segments(
856
3.21k
            std::static_pointer_cast<BetaRowset>(rs), &segment_cache));
857
858
8.98k
    for (const auto& column : rs->tablet_schema()->columns()) {
859
8.98k
        if (!column->is_variant_type() || column->unique_id() < 0) {
860
4.76k
            continue;
861
4.76k
        }
862
4.22k
        if (!should_check_variant_path_stats(*column)) {
863
0
            continue;
864
0
        }
865
4.22k
        for (const auto& segment : segment_cache.get_segments()) {
866
2.01k
            std::shared_ptr<ColumnReader> column_reader;
867
2.01k
            OlapReaderStatistics stats;
868
2.01k
            RETURN_IF_ERROR(
869
2.01k
                    segment->get_column_reader(column->unique_id(), &column_reader, &stats));
870
2.01k
            if (!column_reader) {
871
0
                continue;
872
0
            }
873
874
2.01k
            CHECK(column_reader->get_meta_type() == FieldType::OLAP_FIELD_TYPE_VARIANT);
875
2.01k
            auto* variant_column_reader =
876
2.01k
                    assert_cast<segment_v2::VariantColumnReader*>(column_reader.get());
877
            // load external meta before getting stats
878
2.01k
            RETURN_IF_ERROR(variant_column_reader->load_external_meta_once());
879
2.01k
            const auto* source_stats = variant_column_reader->get_stats();
880
2.01k
            CHECK(source_stats);
881
882
            // agg path -> stats
883
4.93k
            for (const auto& [path, size] : source_stats->sparse_column_non_null_size) {
884
4.93k
                (*uid_to_path_stats)[column->unique_id()][path] += size;
885
4.93k
            }
886
887
6.87k
            for (const auto& [path, size] : source_stats->subcolumns_non_null_size) {
888
6.87k
                (*uid_to_path_stats)[column->unique_id()][path] += size;
889
6.87k
            }
890
2.01k
        }
891
4.22k
    }
892
3.21k
    return Status::OK();
893
3.21k
}
894
895
Status VariantCompactionUtil::aggregate_variant_extended_info(
896
        const RowsetSharedPtr& rs,
897
5.15k
        std::unordered_map<int32_t, VariantExtendedInfo>* uid_to_variant_extended_info) {
898
5.15k
    SegmentCacheHandle segment_cache;
899
5.15k
    RETURN_IF_ERROR(SegmentLoader::instance()->load_segments(
900
5.15k
            std::static_pointer_cast<BetaRowset>(rs), &segment_cache));
901
902
19.3k
    for (const auto& column : rs->tablet_schema()->columns()) {
903
19.3k
        if (!column->is_variant_type()) {
904
13.0k
            continue;
905
13.0k
        }
906
6.28k
        auto& extended_info = (*uid_to_variant_extended_info)[column->unique_id()];
907
6.28k
        if (column->variant_enable_nested_group()) {
908
0
            extended_info.has_nested_group = true;
909
0
        }
910
6.28k
        for (const auto& segment : segment_cache.get_segments()) {
911
3.44k
            std::shared_ptr<ColumnReader> column_reader;
912
3.44k
            OlapReaderStatistics stats;
913
3.44k
            RETURN_IF_ERROR(
914
3.44k
                    segment->get_column_reader(column->unique_id(), &column_reader, &stats));
915
3.44k
            if (!column_reader) {
916
0
                continue;
917
0
            }
918
919
3.44k
            CHECK(column_reader->get_meta_type() == FieldType::OLAP_FIELD_TYPE_VARIANT);
920
3.44k
            auto* variant_column_reader =
921
3.44k
                    assert_cast<segment_v2::VariantColumnReader*>(column_reader.get());
922
            // load external meta before getting stats
923
3.44k
            RETURN_IF_ERROR(variant_column_reader->load_external_meta_once());
924
3.44k
            const auto* source_stats = variant_column_reader->get_stats();
925
3.44k
            CHECK(source_stats);
926
927
3.44k
            if (!column->variant_enable_nested_group()) {
928
                // NG roots still need type metadata for regular subpaths such as `v.owner`,
929
                // but their compaction schema should not be driven by flat path stats.
930
3.44k
                for (const auto& [path, size] : source_stats->sparse_column_non_null_size) {
931
2.68k
                    extended_info.path_to_none_null_values[path] += size;
932
2.68k
                    extended_info.sparse_paths.emplace(path);
933
2.68k
                }
934
935
6.03k
                for (const auto& [path, size] : source_stats->subcolumns_non_null_size) {
936
6.03k
                    extended_info.path_to_none_null_values[path] += size;
937
6.03k
                }
938
3.44k
            }
939
940
            //2. agg path -> schema
941
3.44k
            variant_column_reader->get_subcolumns_types(&extended_info.path_to_data_types);
942
943
            // 3. extract typed paths
944
3.44k
            variant_column_reader->get_typed_paths(&extended_info.typed_paths);
945
946
            // 4. extract nested paths
947
3.44k
            if (!column->variant_enable_nested_group()) {
948
3.44k
                variant_column_reader->get_nested_paths(&extended_info.nested_paths);
949
3.44k
            }
950
3.44k
        }
951
6.28k
    }
952
5.15k
    return Status::OK();
953
5.15k
}
954
955
// get the subpaths and sparse paths for the variant column
956
void VariantCompactionUtil::get_subpaths(int32_t max_subcolumns_count,
957
                                         const PathToNoneNullValues& stats,
958
336
                                         TabletSchema::PathsSetInfo& paths_set_info) {
959
    // max_subcolumns_count is 0 means no limit
960
336
    if (max_subcolumns_count > 0 && stats.size() > max_subcolumns_count) {
961
84
        std::vector<std::pair<size_t, std::string_view>> paths_with_sizes;
962
84
        paths_with_sizes.reserve(stats.size());
963
1.73k
        for (const auto& [path, size] : stats) {
964
1.73k
            paths_with_sizes.emplace_back(size, path);
965
1.73k
        }
966
84
        std::sort(paths_with_sizes.begin(), paths_with_sizes.end(), std::greater());
967
968
        // Select top N paths as subcolumns, remaining paths as sparse columns
969
1.73k
        for (const auto& [size, path] : paths_with_sizes) {
970
1.73k
            if (paths_set_info.sub_path_set.size() < max_subcolumns_count) {
971
204
                paths_set_info.sub_path_set.emplace(path);
972
1.53k
            } else {
973
1.53k
                paths_set_info.sparse_path_set.emplace(path);
974
1.53k
            }
975
1.73k
        }
976
84
        LOG(INFO) << "subpaths " << paths_set_info.sub_path_set.size() << " sparse paths "
977
84
                  << paths_set_info.sparse_path_set.size() << " variant max subcolumns count "
978
84
                  << max_subcolumns_count << " stats size " << paths_with_sizes.size();
979
252
    } else {
980
        // Apply all paths as subcolumns
981
1.48k
        for (const auto& [path, _] : stats) {
982
1.48k
            paths_set_info.sub_path_set.emplace(path);
983
1.48k
        }
984
252
    }
985
336
}
986
987
Status VariantCompactionUtil::check_path_stats(const std::vector<RowsetSharedPtr>& intputs,
988
8.17k
                                               RowsetSharedPtr output, BaseTabletSPtr tablet) {
989
8.17k
    if (output->tablet_schema()->num_variant_columns() == 0) {
990
7.62k
        return Status::OK();
991
7.62k
    }
992
5.14k
    for (const auto& rowset : intputs) {
993
19.2k
        for (const auto& column : rowset->tablet_schema()->columns()) {
994
19.2k
            if (column->is_variant_type() && !should_check_variant_path_stats(*column)) {
995
0
                return Status::OK();
996
0
            }
997
19.2k
        }
998
5.14k
    }
999
    // check no extended schema in input rowsets
1000
5.15k
    for (const auto& rowset : intputs) {
1001
19.2k
        for (const auto& column : rowset->tablet_schema()->columns()) {
1002
19.2k
            if (column->is_extracted_column()) {
1003
0
                return Status::OK();
1004
0
            }
1005
19.2k
        }
1006
5.15k
    }
1007
551
#ifndef BE_TEST
1008
    // check no extended schema in output rowset
1009
1.83k
    for (const auto& column : output->tablet_schema()->columns()) {
1010
1.83k
        if (column->is_extracted_column()) {
1011
0
            const auto& name = column->name();
1012
0
            if (name.find("." + DOC_VALUE_COLUMN_PATH + ".") != std::string::npos ||
1013
0
                name.find("." + SPARSE_COLUMN_PATH + ".") != std::string::npos ||
1014
0
                name.ends_with("." + SPARSE_COLUMN_PATH)) {
1015
0
                continue;
1016
0
            }
1017
0
            return Status::InternalError("Unexpected extracted column {} in output rowset",
1018
0
                                         column->name());
1019
0
        }
1020
1.83k
    }
1021
551
#endif
1022
    // only check path stats for dup_keys since the rows may be merged in other models
1023
551
    if (tablet->keys_type() != KeysType::DUP_KEYS) {
1024
161
        return Status::OK();
1025
161
    }
1026
    // if there is a delete predicate in the input rowsets, we skip the path stats check
1027
2.88k
    for (auto& rowset : intputs) {
1028
2.88k
        if (rowset->rowset_meta()->has_delete_predicate()) {
1029
4
            return Status::OK();
1030
4
        }
1031
2.88k
    }
1032
1.03k
    for (const auto& column : output->tablet_schema()->columns()) {
1033
1.03k
        if (column->is_variant_type() && !should_check_variant_path_stats(*column)) {
1034
0
            return Status::OK();
1035
0
        }
1036
1.03k
    }
1037
386
    std::unordered_map<int32_t, PathToNoneNullValues> original_uid_to_path_stats;
1038
2.83k
    for (const auto& rs : intputs) {
1039
2.83k
        RETURN_IF_ERROR(aggregate_path_to_stats(rs, &original_uid_to_path_stats));
1040
2.83k
    }
1041
386
    std::unordered_map<int32_t, PathToNoneNullValues> output_uid_to_path_stats;
1042
386
    RETURN_IF_ERROR(aggregate_path_to_stats(output, &output_uid_to_path_stats));
1043
386
    for (const auto& [uid, stats] : output_uid_to_path_stats) {
1044
243
        if (output->tablet_schema()->column_by_uid(uid).is_variant_type() &&
1045
243
            output->tablet_schema()->column_by_uid(uid).variant_enable_doc_mode()) {
1046
67
            continue;
1047
67
        }
1048
176
        if (original_uid_to_path_stats.find(uid) == original_uid_to_path_stats.end()) {
1049
0
            return Status::InternalError("Path stats not found for uid {}, tablet_id {}", uid,
1050
0
                                         tablet->tablet_id());
1051
0
        }
1052
1053
        // In input rowsets, some rowsets may have statistics values exceeding the maximum limit,
1054
        // which leads to inaccurate statistics
1055
176
        if (stats.size() > output->tablet_schema()
1056
176
                                   ->column_by_uid(uid)
1057
176
                                   .variant_max_sparse_column_statistics_size()) {
1058
            // When there is only one segment, we can ensure that the size of each path in output stats is accurate
1059
1
            if (output->num_segments() == 1) {
1060
13
                for (const auto& [path, size] : stats) {
1061
13
                    if (original_uid_to_path_stats.at(uid).find(path) ==
1062
13
                        original_uid_to_path_stats.at(uid).end()) {
1063
0
                        continue;
1064
0
                    }
1065
13
                    if (original_uid_to_path_stats.at(uid).at(path) > size) {
1066
0
                        return Status::InternalError(
1067
0
                                "Path stats not smaller for uid {} with path `{}`, input size {}, "
1068
0
                                "output "
1069
0
                                "size {}, "
1070
0
                                "tablet_id {}",
1071
0
                                uid, path, original_uid_to_path_stats.at(uid).at(path), size,
1072
0
                                tablet->tablet_id());
1073
0
                    }
1074
13
                }
1075
1
            }
1076
1
        }
1077
        // in this case, input stats is accurate, so we check the stats size and stats value
1078
175
        else {
1079
3.05k
            for (const auto& [path, size] : stats) {
1080
3.05k
                if (original_uid_to_path_stats.at(uid).find(path) ==
1081
3.05k
                    original_uid_to_path_stats.at(uid).end()) {
1082
0
                    return Status::InternalError(
1083
0
                            "Path stats not found for uid {}, path {}, tablet_id {}", uid, path,
1084
0
                            tablet->tablet_id());
1085
0
                }
1086
3.05k
                if (original_uid_to_path_stats.at(uid).at(path) != size) {
1087
0
                    return Status::InternalError(
1088
0
                            "Path stats not match for uid {} with path `{}`, input size {}, output "
1089
0
                            "size {}, "
1090
0
                            "tablet_id {}",
1091
0
                            uid, path, original_uid_to_path_stats.at(uid).at(path), size,
1092
0
                            tablet->tablet_id());
1093
0
                }
1094
3.05k
            }
1095
175
        }
1096
176
    }
1097
1098
386
    return Status::OK();
1099
386
}
1100
1101
Status VariantCompactionUtil::get_compaction_typed_columns(
1102
        const TabletSchemaSPtr& target, const std::unordered_set<std::string>& typed_paths,
1103
        const TabletColumnPtr parent_column, TabletSchemaSPtr& output_schema,
1104
333
        TabletSchema::PathsSetInfo& paths_set_info) {
1105
333
    if (parent_column->variant_enable_typed_paths_to_sparse()) {
1106
40
        return Status::OK();
1107
40
    }
1108
466
    for (const auto& path : typed_paths) {
1109
466
        TabletSchema::SubColumnInfo sub_column_info;
1110
466
        if (generate_sub_column_info(*target, parent_column->unique_id(), path, &sub_column_info)) {
1111
465
            inherit_column_attributes(*parent_column, sub_column_info.column);
1112
465
            output_schema->append_column(sub_column_info.column);
1113
465
            paths_set_info.typed_path_set.insert({path, std::move(sub_column_info)});
1114
465
            VLOG_DEBUG << "append typed column " << path;
1115
465
        } else {
1116
1
            return Status::InternalError("Failed to generate sub column info for path {}", path);
1117
1
        }
1118
466
    }
1119
292
    return Status::OK();
1120
293
}
1121
1122
Status VariantCompactionUtil::get_compaction_nested_columns(
1123
        const std::unordered_set<PathInData, PathInData::Hash>& nested_paths,
1124
        const PathToDataTypes& path_to_data_types, const TabletColumnPtr parent_column,
1125
332
        TabletSchemaSPtr& output_schema, TabletSchema::PathsSetInfo& paths_set_info) {
1126
332
    const auto& parent_indexes = output_schema->inverted_indexs(parent_column->unique_id());
1127
332
    for (const auto& path : nested_paths) {
1128
3
        const auto& find_data_types = path_to_data_types.find(path);
1129
3
        if (find_data_types == path_to_data_types.end() || find_data_types->second.empty()) {
1130
1
            return Status::InternalError("Nested path {} has no data type", path.get_path());
1131
1
        }
1132
2
        DataTypePtr data_type;
1133
2
        get_least_supertype_jsonb(find_data_types->second, &data_type);
1134
1135
2
        const std::string& column_name = parent_column->name_lower_case() + "." + path.get_path();
1136
2
        PathInDataBuilder full_path_builder;
1137
2
        auto full_path = full_path_builder.append(parent_column->name_lower_case(), false)
1138
2
                                 .append(path.get_parts(), false)
1139
2
                                 .build();
1140
2
        TabletColumn nested_column =
1141
2
                get_column_by_type(data_type, column_name,
1142
2
                                   ExtraInfo {.unique_id = -1,
1143
2
                                              .parent_unique_id = parent_column->unique_id(),
1144
2
                                              .path_info = full_path});
1145
2
        inherit_column_attributes(*parent_column, nested_column);
1146
2
        TabletIndexes sub_column_indexes;
1147
2
        inherit_index(parent_indexes, sub_column_indexes, nested_column);
1148
2
        paths_set_info.subcolumn_indexes.emplace(path.get_path(), std::move(sub_column_indexes));
1149
2
        output_schema->append_column(nested_column);
1150
2
        VLOG_DEBUG << "append nested column " << path.get_path();
1151
2
    }
1152
331
    return Status::OK();
1153
332
}
1154
1155
void VariantCompactionUtil::get_compaction_subcolumns_from_subpaths(
1156
        TabletSchema::PathsSetInfo& paths_set_info, const TabletColumnPtr parent_column,
1157
        const TabletSchemaSPtr& target, const PathToDataTypes& path_to_data_types,
1158
173
        const std::unordered_set<std::string>& sparse_paths, TabletSchemaSPtr& output_schema) {
1159
173
    auto& path_set = paths_set_info.sub_path_set;
1160
173
    std::vector<StringRef> sorted_subpaths(path_set.begin(), path_set.end());
1161
173
    std::sort(sorted_subpaths.begin(), sorted_subpaths.end());
1162
173
    const auto& parent_indexes = target->inverted_indexs(parent_column->unique_id());
1163
    // append subcolumns
1164
414
    for (const auto& subpath : sorted_subpaths) {
1165
414
        auto column_name = parent_column->name_lower_case() + "." + subpath.to_string();
1166
414
        auto column_path = PathInData(column_name);
1167
1168
414
        const auto& find_data_types = path_to_data_types.find(PathInData(subpath));
1169
1170
        // some cases: the subcolumn type is variant
1171
        // 1. this path has no data type in segments
1172
        // 2. this path is in sparse paths
1173
        // 3. the sparse paths are too much
1174
414
        TabletSchema::SubColumnInfo sub_column_info;
1175
414
        if (parent_column->variant_enable_typed_paths_to_sparse() &&
1176
414
            generate_sub_column_info(*target, parent_column->unique_id(), std::string(subpath),
1177
71
                                     &sub_column_info)) {
1178
61
            inherit_column_attributes(*parent_column, sub_column_info.column);
1179
61
            output_schema->append_column(sub_column_info.column);
1180
61
            paths_set_info.subcolumn_indexes.emplace(subpath, std::move(sub_column_info.indexes));
1181
61
            VLOG_DEBUG << "append typed column " << subpath;
1182
353
        } else if (find_data_types == path_to_data_types.end() || find_data_types->second.empty() ||
1183
353
                   sparse_paths.find(std::string(subpath)) != sparse_paths.end() ||
1184
353
                   sparse_paths.size() >=
1185
336
                           parent_column->variant_max_sparse_column_statistics_size()) {
1186
21
            TabletColumn subcolumn;
1187
21
            subcolumn.set_name(column_name);
1188
21
            subcolumn.set_type(FieldType::OLAP_FIELD_TYPE_VARIANT);
1189
21
            subcolumn.set_parent_unique_id(parent_column->unique_id());
1190
21
            subcolumn.set_path_info(column_path);
1191
21
            subcolumn.set_aggregation_method(parent_column->aggregation());
1192
21
            subcolumn.set_variant_max_subcolumns_count(
1193
21
                    parent_column->variant_max_subcolumns_count());
1194
21
            subcolumn.set_variant_enable_doc_mode(parent_column->variant_enable_doc_mode());
1195
21
            subcolumn.set_is_nullable(true);
1196
21
            output_schema->append_column(subcolumn);
1197
21
            VLOG_DEBUG << "append sub column " << subpath << " data type "
1198
0
                       << "VARIANT";
1199
21
        }
1200
        // normal case: the subcolumn type can be calculated from the data types in segments
1201
332
        else {
1202
332
            DataTypePtr data_type;
1203
332
            get_least_supertype_jsonb(find_data_types->second, &data_type);
1204
332
            TabletColumn sub_column =
1205
332
                    get_column_by_type(data_type, column_name,
1206
332
                                       ExtraInfo {.unique_id = -1,
1207
332
                                                  .parent_unique_id = parent_column->unique_id(),
1208
332
                                                  .path_info = column_path});
1209
332
            inherit_column_attributes(*parent_column, sub_column);
1210
332
            TabletIndexes sub_column_indexes;
1211
332
            inherit_index(parent_indexes, sub_column_indexes, sub_column);
1212
332
            paths_set_info.subcolumn_indexes.emplace(subpath, std::move(sub_column_indexes));
1213
332
            output_schema->append_column(sub_column);
1214
332
            VLOG_DEBUG << "append sub column " << subpath << " data type " << data_type->get_name();
1215
332
        }
1216
414
    }
1217
173
}
1218
1219
void VariantCompactionUtil::get_compaction_subcolumns_from_data_types(
1220
        TabletSchema::PathsSetInfo& paths_set_info, const TabletColumnPtr parent_column,
1221
        const TabletSchemaSPtr& target, const PathToDataTypes& path_to_data_types,
1222
167
        TabletSchemaSPtr& output_schema) {
1223
167
    const auto& parent_indexes = target->inverted_indexs(parent_column->unique_id());
1224
1.36k
    for (const auto& [path, data_types] : path_to_data_types) {
1225
        // Typed paths are materialized by get_compaction_typed_columns(); this helper only
1226
        // materializes regular subcolumns inferred from rowset data types.
1227
1.36k
        if (data_types.empty() || path.empty() || path.get_is_typed() || path.has_nested_part()) {
1228
75
            continue;
1229
75
        }
1230
1.28k
        DataTypePtr data_type;
1231
1.28k
        get_least_supertype_jsonb(data_types, &data_type);
1232
1.28k
        auto column_name = parent_column->name_lower_case() + "." + path.get_path();
1233
1.28k
        auto column_path = PathInData(column_name);
1234
1.28k
        TabletColumn sub_column =
1235
1.28k
                get_column_by_type(data_type, column_name,
1236
1.28k
                                   ExtraInfo {.unique_id = -1,
1237
1.28k
                                              .parent_unique_id = parent_column->unique_id(),
1238
1.28k
                                              .path_info = column_path});
1239
1.28k
        inherit_column_attributes(*parent_column, sub_column);
1240
1.28k
        TabletIndexes sub_column_indexes;
1241
1.28k
        inherit_index(parent_indexes, sub_column_indexes, sub_column);
1242
1.28k
        paths_set_info.sub_path_set.emplace(path.get_path());
1243
1.28k
        paths_set_info.subcolumn_indexes.emplace(path.get_path(), std::move(sub_column_indexes));
1244
1.28k
        output_schema->append_column(sub_column);
1245
1.28k
        VLOG_DEBUG << "append sub column " << path.get_path() << " data type "
1246
0
                   << data_type->get_name();
1247
1.28k
    }
1248
167
}
1249
1250
// Build the temporary schema for compaction.
1251
// NestedGroup roots are special: the root VARIANT column owns the NG tree and the streaming NG
1252
// writer handles NG children, while regular non-NG paths beside the arrays are materialized as
1253
// ordinary extracted subcolumns. NG typed paths still use get_compaction_typed_columns(), keeping
1254
// typed-column rules out of the NG-specific regular-path filtering.
1255
Status VariantCompactionUtil::get_extended_compaction_schema(
1256
7.64k
        const std::vector<RowsetSharedPtr>& rowsets, TabletSchemaSPtr& target) {
1257
7.64k
    std::unordered_map<int32_t, VariantExtendedInfo> uid_to_variant_extended_info;
1258
7.64k
    const bool needs_variant_extended_info =
1259
82.6k
            std::ranges::any_of(target->columns(), [](const TabletColumnPtr& column) {
1260
82.6k
                return column->is_variant_type() && (should_check_variant_path_stats(*column) ||
1261
529
                                                     column->variant_enable_nested_group());
1262
82.6k
            });
1263
7.64k
    if (needs_variant_extended_info) {
1264
        // collect path stats from all rowsets and segments
1265
5.15k
        for (const auto& rs : rowsets) {
1266
5.15k
            RETURN_IF_ERROR(aggregate_variant_extended_info(rs, &uid_to_variant_extended_info));
1267
5.15k
        }
1268
539
    }
1269
1270
    // build the output schema
1271
7.64k
    TabletSchemaSPtr output_schema = std::make_shared<TabletSchema>();
1272
7.64k
    output_schema->shawdow_copy_without_columns(*target);
1273
7.64k
    std::unordered_map<int32_t, TabletSchema::PathsSetInfo> uid_to_paths_set_info;
1274
7.64k
    const auto ng_root_uids =
1275
7.64k
            collect_nested_group_compaction_root_uids(target, uid_to_variant_extended_info);
1276
88.1k
    for (const TabletColumnPtr& column : target->columns()) {
1277
88.1k
        if (!column->is_extracted_column()) {
1278
88.0k
            output_schema->append_column(*column);
1279
88.0k
        }
1280
88.1k
        if (!column->is_variant_type()) {
1281
87.6k
            continue;
1282
87.6k
        }
1283
18.4E
        VLOG_DEBUG << "column " << column->name() << " unique id " << column->unique_id();
1284
1285
496
        const auto info_it = uid_to_variant_extended_info.find(column->unique_id());
1286
496
        const VariantExtendedInfo empty_extended_info;
1287
496
        const VariantExtendedInfo& extended_info = info_it == uid_to_variant_extended_info.end()
1288
496
                                                           ? empty_extended_info
1289
496
                                                           : info_it->second;
1290
496
        auto& paths_set_info = uid_to_paths_set_info[column->unique_id()];
1291
496
        const bool use_nested_group_compaction_schema = ng_root_uids.contains(column->unique_id());
1292
1293
496
        if (use_nested_group_compaction_schema) {
1294
            // 1. append typed columns. Keep this shared with the non-NG typed helper; only the
1295
            // regular-path selection below is NG-specific.
1296
1
            RETURN_IF_ERROR(get_compaction_typed_columns(target, extended_info.typed_paths, column,
1297
1
                                                         output_schema, paths_set_info));
1298
1299
            // NG roots do not record path-count stats for ordinary Variant paths, so their regular
1300
            // non-NG subcolumns use the same data-types materialization helper as the
1301
            // all-materialized non-NG branch below.
1302
1
            auto regular_path_to_data_types =
1303
1
                    collect_regular_types_outside_nested_group(extended_info);
1304
1
            get_compaction_subcolumns_from_data_types(paths_set_info, column, target,
1305
1
                                                      regular_path_to_data_types, output_schema);
1306
1
            LOG(INFO) << "Variant column uid=" << column->unique_id()
1307
1
                      << " keeps nested-group root and materializes regular non-NG subcolumns in "
1308
1
                         "compaction schema";
1309
1
            continue;
1310
1
        }
1311
1312
495
        if (column->variant_enable_doc_mode()) {
1313
300
            const int bucket_num = std::max(1, column->variant_doc_hash_shard_count());
1314
844
            for (int b = 0; b < bucket_num; ++b) {
1315
544
                TabletColumn doc_value_bucket_column = create_doc_value_column(*column, b);
1316
544
                doc_value_bucket_column.set_type(FieldType::OLAP_FIELD_TYPE_VARIANT);
1317
544
                doc_value_bucket_column.set_is_nullable(false);
1318
544
                doc_value_bucket_column.set_variant_enable_doc_mode(true);
1319
544
                output_schema->append_column(doc_value_bucket_column);
1320
544
            }
1321
300
            continue;
1322
300
        }
1323
1324
        // 1. append typed columns
1325
195
        RETURN_IF_ERROR(get_compaction_typed_columns(target, extended_info.typed_paths, column,
1326
195
                                                     output_schema, paths_set_info));
1327
1328
        // 2. append nested columns
1329
195
        RETURN_IF_ERROR(get_compaction_nested_columns(extended_info.nested_paths,
1330
195
                                                      extended_info.path_to_data_types, column,
1331
195
                                                      output_schema, paths_set_info));
1332
1333
        // 3. get the subpaths
1334
195
        get_subpaths(column->variant_max_subcolumns_count(), extended_info.path_to_none_null_values,
1335
195
                     paths_set_info);
1336
1337
        // 4. append subcolumns
1338
195
        if (column->variant_max_subcolumns_count() > 0 || !column->get_sub_columns().empty()) {
1339
165
            get_compaction_subcolumns_from_subpaths(paths_set_info, column, target,
1340
165
                                                    extended_info.path_to_data_types,
1341
165
                                                    extended_info.sparse_paths, output_schema);
1342
165
        }
1343
        // variant_max_subcolumns_count == 0 and no typed paths materialized
1344
        // it means that all subcolumns are materialized, may be from old data
1345
30
        else {
1346
30
            get_compaction_subcolumns_from_data_types(paths_set_info, column, target,
1347
30
                                                      extended_info.path_to_data_types,
1348
30
                                                      output_schema);
1349
30
        }
1350
1351
        // append sparse column(s)
1352
        // If variant uses bucketized sparse columns, append one sparse bucket column per bucket.
1353
        // Otherwise, append the single sparse column.
1354
195
        int bucket_num = std::max(1, column->variant_sparse_hash_shard_count());
1355
289
        if (bucket_num > 1) {
1356
1.00k
            for (int b = 0; b < bucket_num; ++b) {
1357
715
                TabletColumn sparse_bucket_column = create_sparse_shard_column(*column, b);
1358
715
                output_schema->append_column(sparse_bucket_column);
1359
715
            }
1360
18.4E
        } else {
1361
18.4E
            TabletColumn sparse_column = create_sparse_column(*column);
1362
18.4E
            output_schema->append_column(sparse_column);
1363
18.4E
        }
1364
195
    }
1365
1366
7.64k
    target = output_schema;
1367
    // used to merge & filter path to sparse column during reading in compaction
1368
7.64k
    target->set_path_set_info(std::move(uid_to_paths_set_info));
1369
18.4E
    VLOG_DEBUG << "dump schema " << target->dump_full_schema();
1370
7.64k
    return Status::OK();
1371
7.64k
}
1372
1373
// Calculate statistics about variant data paths from the encoded sparse column
1374
void VariantCompactionUtil::calculate_variant_stats(const IColumn& encoded_sparse_column,
1375
                                                    segment_v2::VariantStatisticsPB* stats,
1376
                                                    size_t max_sparse_column_statistics_size,
1377
720
                                                    size_t row_pos, size_t num_rows) {
1378
    // Cast input column to ColumnMap type since sparse column is stored as a map
1379
720
    const auto& map_column = assert_cast<const ColumnMap&>(encoded_sparse_column);
1380
1381
    // Get the keys column which contains the paths as strings
1382
720
    const auto& sparse_data_paths =
1383
720
            assert_cast<const ColumnString*>(map_column.get_keys_ptr().get());
1384
720
    const auto& serialized_sparse_column_offsets = map_column.get_offsets();
1385
720
    auto& count_map = *stats->mutable_sparse_column_non_null_size();
1386
    // Iterate through all paths in the sparse column
1387
242k
    for (size_t i = row_pos; i != row_pos + num_rows; ++i) {
1388
242k
        size_t offset = serialized_sparse_column_offsets[i - 1];
1389
242k
        size_t end = serialized_sparse_column_offsets[i];
1390
1.62M
        for (size_t j = offset; j != end; ++j) {
1391
1.38M
            auto path = sparse_data_paths->get_data_at(j);
1392
1393
1.38M
            const auto& sparse_path = path.to_string();
1394
            // If path already exists in statistics, increment its count
1395
1.38M
            if (auto it = count_map.find(sparse_path); it != count_map.end()) {
1396
1.37M
                ++it->second;
1397
1.37M
            }
1398
            // If path doesn't exist and we haven't hit the max statistics size limit,
1399
            // add it with count 1
1400
1.48k
            else if (count_map.size() < max_sparse_column_statistics_size) {
1401
1.48k
                count_map.emplace(sparse_path, 1);
1402
1.48k
            }
1403
1.38M
        }
1404
242k
    }
1405
1406
720
    if (stats->sparse_column_non_null_size().size() > max_sparse_column_statistics_size) {
1407
0
        throw doris::Exception(
1408
0
                ErrorCode::INTERNAL_ERROR,
1409
0
                "Sparse column non null size: {} is greater than max statistics size: {}",
1410
0
                stats->sparse_column_non_null_size().size(), max_sparse_column_statistics_size);
1411
0
    }
1412
720
}
1413
1414
/// Calculates number of dimensions in array field.
1415
/// Returns 0 for scalar fields.
1416
class FieldVisitorToNumberOfDimensions : public StaticVisitor<size_t> {
1417
public:
1418
    FieldVisitorToNumberOfDimensions() = default;
1419
    template <PrimitiveType T>
1420
23.5M
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
23.5M
        if constexpr (T == TYPE_ARRAY) {
1422
2.22M
            const size_t size = x.size();
1423
2.22M
            size_t dimensions = 0;
1424
5.36M
            for (size_t i = 0; i < size; ++i) {
1425
3.14M
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
3.14M
                dimensions = std::max(dimensions, element_dimensions);
1427
3.14M
            }
1428
2.22M
            return 1 + dimensions;
1429
21.2M
        } else {
1430
21.2M
            return 0;
1431
21.2M
        }
1432
23.5M
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE1EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
121k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
121k
        } else {
1430
121k
            return 0;
1431
121k
        }
1432
121k
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE26EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
511
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
511
        } else {
1430
511
            return 0;
1431
511
        }
1432
511
    }
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE42EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE7EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
41.9k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
41.9k
        } else {
1430
41.9k
            return 0;
1431
41.9k
        }
1432
41.9k
    }
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE12EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE11EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE25EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
428
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
428
        } else {
1430
428
            return 0;
1431
428
        }
1432
428
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE2EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
332k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
332k
        } else {
1430
332k
            return 0;
1431
332k
        }
1432
332k
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE3EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
1.05k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
1.05k
        } else {
1430
1.05k
            return 0;
1431
1.05k
        }
1432
1.05k
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE4EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
1.04k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
1.04k
        } else {
1430
1.04k
            return 0;
1431
1.04k
        }
1432
1.04k
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE5EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
1.97k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
1.97k
        } else {
1430
1.97k
            return 0;
1431
1.97k
        }
1432
1.97k
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE6EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
6.32M
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
6.32M
        } else {
1430
6.32M
            return 0;
1431
6.32M
        }
1432
6.32M
    }
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE38EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE39EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE8EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
884
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
884
        } else {
1430
884
            return 0;
1431
884
        }
1432
884
    }
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE27EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE9EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
2.96M
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
2.96M
        } else {
1430
2.96M
            return 0;
1431
2.96M
        }
1432
2.96M
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE36EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
339
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
339
        } else {
1430
339
            return 0;
1431
339
        }
1432
339
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE37EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
338
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
338
        } else {
1430
338
            return 0;
1431
338
        }
1432
338
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE23EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
11.3M
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
11.3M
        } else {
1430
11.3M
            return 0;
1431
11.3M
        }
1432
11.3M
    }
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE15EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE10EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE41EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE17EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
2.22M
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
2.22M
        if constexpr (T == TYPE_ARRAY) {
1422
2.22M
            const size_t size = x.size();
1423
2.22M
            size_t dimensions = 0;
1424
5.36M
            for (size_t i = 0; i < size; ++i) {
1425
3.14M
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
3.14M
                dimensions = std::max(dimensions, element_dimensions);
1427
3.14M
            }
1428
2.22M
            return 1 + dimensions;
1429
        } else {
1430
            return 0;
1431
        }
1432
2.22M
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE16EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
1
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
1
        } else {
1430
1
            return 0;
1431
1
        }
1432
1
    }
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE18EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE32EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
1
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
1
        } else {
1430
1
            return 0;
1431
1
        }
1432
1
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE28EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
783
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
783
        } else {
1430
783
            return 0;
1431
783
        }
1432
783
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE29EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
724
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
724
        } else {
1430
724
            return 0;
1431
724
        }
1432
724
    }
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE20EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE30EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
70.8k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
70.8k
        } else {
1430
70.8k
            return 0;
1431
70.8k
        }
1432
70.8k
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE35EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
588
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
588
        } else {
1430
588
            return 0;
1431
588
        }
1432
588
    }
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE22EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE19EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE24EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE31EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
46.8k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
        if constexpr (T == TYPE_ARRAY) {
1422
            const size_t size = x.size();
1423
            size_t dimensions = 0;
1424
            for (size_t i = 0; i < size; ++i) {
1425
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
                dimensions = std::max(dimensions, element_dimensions);
1427
            }
1428
            return 1 + dimensions;
1429
46.8k
        } else {
1430
46.8k
            return 0;
1431
46.8k
        }
1432
46.8k
    }
1433
};
1434
1435
// Visitor that allows to get type of scalar field
1436
// but exclude fields contain complex field.This is a faster version
1437
// for FieldVisitorToScalarType which does not support complex field.
1438
class SimpleFieldVisitorToScalarType : public StaticVisitor<size_t> {
1439
public:
1440
    template <PrimitiveType T>
1441
18.6M
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1442
18.6M
        if constexpr (T == TYPE_ARRAY) {
1443
0
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Array type is not supported");
1444
109k
        } else if constexpr (T == TYPE_NULL) {
1445
109k
            have_nulls = true;
1446
109k
            return 1;
1447
18.5M
        } else {
1448
18.5M
            type = T;
1449
18.5M
            return 1;
1450
18.5M
        }
1451
18.6M
    }
_ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE1EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1441
109k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1442
        if constexpr (T == TYPE_ARRAY) {
1443
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Array type is not supported");
1444
109k
        } else if constexpr (T == TYPE_NULL) {
1445
109k
            have_nulls = true;
1446
109k
            return 1;
1447
        } else {
1448
            type = T;
1449
            return 1;
1450
        }
1451
109k
    }
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE26EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE42EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE7EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1441
12.3k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1442
        if constexpr (T == TYPE_ARRAY) {
1443
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Array type is not supported");
1444
        } else if constexpr (T == TYPE_NULL) {
1445
            have_nulls = true;
1446
            return 1;
1447
12.3k
        } else {
1448
12.3k
            type = T;
1449
12.3k
            return 1;
1450
12.3k
        }
1451
12.3k
    }
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE12EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE11EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE25EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE2EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1441
273k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1442
        if constexpr (T == TYPE_ARRAY) {
1443
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Array type is not supported");
1444
        } else if constexpr (T == TYPE_NULL) {
1445
            have_nulls = true;
1446
            return 1;
1447
273k
        } else {
1448
273k
            type = T;
1449
273k
            return 1;
1450
273k
        }
1451
273k
    }
_ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE3EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1441
2
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1442
        if constexpr (T == TYPE_ARRAY) {
1443
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Array type is not supported");
1444
        } else if constexpr (T == TYPE_NULL) {
1445
            have_nulls = true;
1446
            return 1;
1447
2
        } else {
1448
2
            type = T;
1449
2
            return 1;
1450
2
        }
1451
2
    }
_ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE4EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1441
7
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1442
        if constexpr (T == TYPE_ARRAY) {
1443
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Array type is not supported");
1444
        } else if constexpr (T == TYPE_NULL) {
1445
            have_nulls = true;
1446
            return 1;
1447
7
        } else {
1448
7
            type = T;
1449
7
            return 1;
1450
7
        }
1451
7
    }
_ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE5EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1441
570
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1442
        if constexpr (T == TYPE_ARRAY) {
1443
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Array type is not supported");
1444
        } else if constexpr (T == TYPE_NULL) {
1445
            have_nulls = true;
1446
            return 1;
1447
570
        } else {
1448
570
            type = T;
1449
570
            return 1;
1450
570
        }
1451
570
    }
_ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE6EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1441
4.90M
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1442
        if constexpr (T == TYPE_ARRAY) {
1443
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Array type is not supported");
1444
        } else if constexpr (T == TYPE_NULL) {
1445
            have_nulls = true;
1446
            return 1;
1447
4.90M
        } else {
1448
4.90M
            type = T;
1449
4.90M
            return 1;
1450
4.90M
        }
1451
4.90M
    }
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE38EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE39EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE8EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1441
1
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1442
        if constexpr (T == TYPE_ARRAY) {
1443
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Array type is not supported");
1444
        } else if constexpr (T == TYPE_NULL) {
1445
            have_nulls = true;
1446
            return 1;
1447
1
        } else {
1448
1
            type = T;
1449
1
            return 1;
1450
1
        }
1451
1
    }
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE27EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE9EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1441
2.76M
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1442
        if constexpr (T == TYPE_ARRAY) {
1443
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Array type is not supported");
1444
        } else if constexpr (T == TYPE_NULL) {
1445
            have_nulls = true;
1446
            return 1;
1447
2.76M
        } else {
1448
2.76M
            type = T;
1449
2.76M
            return 1;
1450
2.76M
        }
1451
2.76M
    }
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE36EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE37EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE23EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1441
10.4M
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1442
        if constexpr (T == TYPE_ARRAY) {
1443
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Array type is not supported");
1444
        } else if constexpr (T == TYPE_NULL) {
1445
            have_nulls = true;
1446
            return 1;
1447
10.4M
        } else {
1448
10.4M
            type = T;
1449
10.4M
            return 1;
1450
10.4M
        }
1451
10.4M
    }
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE15EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE10EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE41EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE17EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE16EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE18EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE32EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE28EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE29EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE20EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE30EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE35EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE22EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE19EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE24EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE31EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1441
46.8k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1442
        if constexpr (T == TYPE_ARRAY) {
1443
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Array type is not supported");
1444
        } else if constexpr (T == TYPE_NULL) {
1445
            have_nulls = true;
1446
            return 1;
1447
46.8k
        } else {
1448
46.8k
            type = T;
1449
46.8k
            return 1;
1450
46.8k
        }
1451
46.8k
    }
1452
18.1M
    void get_scalar_type(PrimitiveType* data_type) const { *data_type = type; }
1453
18.1M
    bool contain_nulls() const { return have_nulls; }
1454
1455
18.9M
    bool need_convert_field() const { return false; }
1456
1457
private:
1458
    PrimitiveType type = PrimitiveType::INVALID_TYPE;
1459
    bool have_nulls = false;
1460
};
1461
1462
/// Visitor that allows to get type of scalar field
1463
/// or least common type of scalars in array.
1464
/// More optimized version of FieldToDataType.
1465
class FieldVisitorToScalarType : public StaticVisitor<size_t> {
1466
public:
1467
    template <PrimitiveType T>
1468
4.72M
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
4.72M
        if constexpr (T == TYPE_ARRAY) {
1470
2.21M
            size_t size = x.size();
1471
5.36M
            for (size_t i = 0; i < size; ++i) {
1472
3.14M
                apply_visitor(*this, x[i]);
1473
3.14M
            }
1474
2.21M
            return 0;
1475
2.21M
        } else if constexpr (T == TYPE_NULL) {
1476
12.2k
            have_nulls = true;
1477
12.2k
            return 0;
1478
2.49M
        } else {
1479
2.49M
            field_types.insert(T);
1480
2.49M
            type_indexes.insert(T);
1481
2.49M
            return 0;
1482
2.49M
        }
1483
4.72M
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE1EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
12.2k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
12.2k
        } else if constexpr (T == TYPE_NULL) {
1476
12.2k
            have_nulls = true;
1477
12.2k
            return 0;
1478
        } else {
1479
            field_types.insert(T);
1480
            type_indexes.insert(T);
1481
            return 0;
1482
        }
1483
12.2k
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE26EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
511
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
511
        } else {
1479
511
            field_types.insert(T);
1480
511
            type_indexes.insert(T);
1481
511
            return 0;
1482
511
        }
1483
511
    }
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE42EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE7EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
29.6k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
29.6k
        } else {
1479
29.6k
            field_types.insert(T);
1480
29.6k
            type_indexes.insert(T);
1481
29.6k
            return 0;
1482
29.6k
        }
1483
29.6k
    }
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE12EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE11EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE25EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
428
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
428
        } else {
1479
428
            field_types.insert(T);
1480
428
            type_indexes.insert(T);
1481
428
            return 0;
1482
428
        }
1483
428
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE2EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
58.3k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
58.3k
        } else {
1479
58.3k
            field_types.insert(T);
1480
58.3k
            type_indexes.insert(T);
1481
58.3k
            return 0;
1482
58.3k
        }
1483
58.3k
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE3EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
1.04k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
1.04k
        } else {
1479
1.04k
            field_types.insert(T);
1480
1.04k
            type_indexes.insert(T);
1481
1.04k
            return 0;
1482
1.04k
        }
1483
1.04k
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE4EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
1.03k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
1.03k
        } else {
1479
1.03k
            field_types.insert(T);
1480
1.03k
            type_indexes.insert(T);
1481
1.03k
            return 0;
1482
1.03k
        }
1483
1.03k
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE5EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
1.40k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
1.40k
        } else {
1479
1.40k
            field_types.insert(T);
1480
1.40k
            type_indexes.insert(T);
1481
1.40k
            return 0;
1482
1.40k
        }
1483
1.40k
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE6EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
1.38M
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
1.38M
        } else {
1479
1.38M
            field_types.insert(T);
1480
1.38M
            type_indexes.insert(T);
1481
1.38M
            return 0;
1482
1.38M
        }
1483
1.38M
    }
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE38EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE39EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE8EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
883
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
883
        } else {
1479
883
            field_types.insert(T);
1480
883
            type_indexes.insert(T);
1481
883
            return 0;
1482
883
        }
1483
883
    }
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE27EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE9EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
183k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
183k
        } else {
1479
183k
            field_types.insert(T);
1480
183k
            type_indexes.insert(T);
1481
183k
            return 0;
1482
183k
        }
1483
183k
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE36EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
339
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
339
        } else {
1479
339
            field_types.insert(T);
1480
339
            type_indexes.insert(T);
1481
339
            return 0;
1482
339
        }
1483
339
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE37EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
338
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
338
        } else {
1479
338
            field_types.insert(T);
1480
338
            type_indexes.insert(T);
1481
338
            return 0;
1482
338
        }
1483
338
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE23EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
763k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
763k
        } else {
1479
763k
            field_types.insert(T);
1480
763k
            type_indexes.insert(T);
1481
763k
            return 0;
1482
763k
        }
1483
763k
    }
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE15EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE10EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE41EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE17EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
2.21M
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
2.21M
        if constexpr (T == TYPE_ARRAY) {
1470
2.21M
            size_t size = x.size();
1471
5.36M
            for (size_t i = 0; i < size; ++i) {
1472
3.14M
                apply_visitor(*this, x[i]);
1473
3.14M
            }
1474
2.21M
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
        } else {
1479
            field_types.insert(T);
1480
            type_indexes.insert(T);
1481
            return 0;
1482
        }
1483
2.21M
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE16EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
1
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
1
        } else {
1479
1
            field_types.insert(T);
1480
1
            type_indexes.insert(T);
1481
1
            return 0;
1482
1
        }
1483
1
    }
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE18EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE32EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
1
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
1
        } else {
1479
1
            field_types.insert(T);
1480
1
            type_indexes.insert(T);
1481
1
            return 0;
1482
1
        }
1483
1
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE28EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
783
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
783
        } else {
1479
783
            field_types.insert(T);
1480
783
            type_indexes.insert(T);
1481
783
            return 0;
1482
783
        }
1483
783
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE29EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
724
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
724
        } else {
1479
724
            field_types.insert(T);
1480
724
            type_indexes.insert(T);
1481
724
            return 0;
1482
724
        }
1483
724
    }
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE20EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE30EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
70.8k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
70.8k
        } else {
1479
70.8k
            field_types.insert(T);
1480
70.8k
            type_indexes.insert(T);
1481
70.8k
            return 0;
1482
70.8k
        }
1483
70.8k
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE35EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
588
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
588
        } else {
1479
588
            field_types.insert(T);
1480
588
            type_indexes.insert(T);
1481
588
            return 0;
1482
588
        }
1483
588
    }
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE22EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE19EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE24EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE31EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
26
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
        if constexpr (T == TYPE_ARRAY) {
1470
            size_t size = x.size();
1471
            for (size_t i = 0; i < size; ++i) {
1472
                apply_visitor(*this, x[i]);
1473
            }
1474
            return 0;
1475
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
26
        } else {
1479
26
            field_types.insert(T);
1480
26
            type_indexes.insert(T);
1481
26
            return 0;
1482
26
        }
1483
26
    }
1484
1.59M
    void get_scalar_type(PrimitiveType* type) const {
1485
1.59M
        if (type_indexes.size() == 1) {
1486
            // Most cases will have only one type
1487
1.51M
            *type = *type_indexes.begin();
1488
1.51M
            return;
1489
1.51M
        }
1490
73.9k
        DataTypePtr data_type;
1491
73.9k
        get_least_supertype_jsonb(type_indexes, &data_type);
1492
73.9k
        *type = data_type->get_primitive_type();
1493
73.9k
    }
1494
1.59M
    bool contain_nulls() const { return have_nulls; }
1495
1.59M
    bool need_convert_field() const { return field_types.size() > 1; }
1496
1497
private:
1498
    phmap::flat_hash_set<PrimitiveType> type_indexes;
1499
    phmap::flat_hash_set<PrimitiveType> field_types;
1500
    bool have_nulls = false;
1501
};
1502
1503
template <typename Visitor>
1504
19.9M
void get_field_info_impl(const Field& field, FieldInfo* info) {
1505
19.9M
    Visitor to_scalar_type_visitor;
1506
19.9M
    apply_visitor(to_scalar_type_visitor, field);
1507
19.9M
    PrimitiveType type_id;
1508
19.9M
    to_scalar_type_visitor.get_scalar_type(&type_id);
1509
    // array item's dimension may missmatch, eg. [1, 2, [1, 2, 3]]
1510
19.9M
    *info = {type_id, to_scalar_type_visitor.contain_nulls(),
1511
19.9M
             to_scalar_type_visitor.need_convert_field(),
1512
19.9M
             apply_visitor(FieldVisitorToNumberOfDimensions(), field)};
1513
19.9M
}
_ZN5doris12variant_util19get_field_info_implINS0_24FieldVisitorToScalarTypeEEEvRKNS_5FieldEPNS_9FieldInfoE
Line
Count
Source
1504
1.58M
void get_field_info_impl(const Field& field, FieldInfo* info) {
1505
1.58M
    Visitor to_scalar_type_visitor;
1506
1.58M
    apply_visitor(to_scalar_type_visitor, field);
1507
1.58M
    PrimitiveType type_id;
1508
1.58M
    to_scalar_type_visitor.get_scalar_type(&type_id);
1509
    // array item's dimension may missmatch, eg. [1, 2, [1, 2, 3]]
1510
1.58M
    *info = {type_id, to_scalar_type_visitor.contain_nulls(),
1511
1.58M
             to_scalar_type_visitor.need_convert_field(),
1512
1.58M
             apply_visitor(FieldVisitorToNumberOfDimensions(), field)};
1513
1.58M
}
_ZN5doris12variant_util19get_field_info_implINS0_30SimpleFieldVisitorToScalarTypeEEEvRKNS_5FieldEPNS_9FieldInfoE
Line
Count
Source
1504
18.3M
void get_field_info_impl(const Field& field, FieldInfo* info) {
1505
18.3M
    Visitor to_scalar_type_visitor;
1506
18.3M
    apply_visitor(to_scalar_type_visitor, field);
1507
18.3M
    PrimitiveType type_id;
1508
18.3M
    to_scalar_type_visitor.get_scalar_type(&type_id);
1509
    // array item's dimension may missmatch, eg. [1, 2, [1, 2, 3]]
1510
18.3M
    *info = {type_id, to_scalar_type_visitor.contain_nulls(),
1511
18.3M
             to_scalar_type_visitor.need_convert_field(),
1512
18.3M
             apply_visitor(FieldVisitorToNumberOfDimensions(), field)};
1513
18.3M
}
1514
1515
19.9M
void get_field_info(const Field& field, FieldInfo* info) {
1516
19.9M
    if (field.is_complex_field()) {
1517
1.58M
        get_field_info_impl<FieldVisitorToScalarType>(field, info);
1518
18.3M
    } else {
1519
18.3M
        get_field_info_impl<SimpleFieldVisitorToScalarType>(field, info);
1520
18.3M
    }
1521
19.9M
}
1522
1523
bool generate_sub_column_info(const TabletSchema& schema, int32_t col_unique_id,
1524
                              const std::string& path,
1525
220k
                              TabletSchema::SubColumnInfo* sub_column_info) {
1526
220k
    const auto& parent_column = schema.column_by_uid(col_unique_id);
1527
220k
    std::function<void(const TabletColumn&, TabletColumn*)> generate_result_column =
1528
220k
            [&](const TabletColumn& from_column, TabletColumn* to_column) {
1529
12.6k
                to_column->set_name(parent_column.name_lower_case() + "." + path);
1530
12.6k
                to_column->set_type(from_column.type());
1531
12.6k
                to_column->set_parent_unique_id(parent_column.unique_id());
1532
12.6k
                bool is_typed = !parent_column.variant_enable_typed_paths_to_sparse();
1533
12.6k
                to_column->set_path_info(
1534
12.6k
                        PathInData(parent_column.name_lower_case() + "." + path, is_typed));
1535
12.6k
                to_column->set_aggregation_method(parent_column.aggregation());
1536
12.6k
                to_column->set_is_nullable(true);
1537
12.6k
                to_column->set_parent_unique_id(parent_column.unique_id());
1538
12.6k
                if (from_column.is_decimal()) {
1539
12.6k
                    to_column->set_precision(from_column.precision());
1540
12.6k
                }
1541
12.6k
                to_column->set_frac(from_column.frac());
1542
1543
12.6k
                if (from_column.is_array_type()) {
1544
2.74k
                    TabletColumn nested_column;
1545
2.74k
                    generate_result_column(*from_column.get_sub_columns()[0], &nested_column);
1546
2.74k
                    to_column->add_sub_column(nested_column);
1547
2.74k
                }
1548
12.6k
            };
1549
1550
220k
    auto generate_index = [&](const std::string& pattern) {
1551
        // 1. find subcolumn's index
1552
9.90k
        if (const auto& indexes = schema.inverted_index_by_field_pattern(col_unique_id, pattern);
1553
9.90k
            !indexes.empty()) {
1554
3.76k
            for (const auto& index : indexes) {
1555
3.76k
                auto index_ptr = std::make_shared<TabletIndex>(*index);
1556
3.76k
                index_ptr->set_escaped_escaped_index_suffix_path(
1557
3.76k
                        sub_column_info->column.path_info_ptr()->get_path());
1558
3.76k
                sub_column_info->indexes.emplace_back(std::move(index_ptr));
1559
3.76k
            }
1560
3.70k
        }
1561
        // 2. find parent column's index
1562
6.20k
        else if (const auto parent_index = schema.inverted_indexs(col_unique_id);
1563
6.20k
                 !parent_index.empty()) {
1564
400
            inherit_index(parent_index, sub_column_info->indexes, sub_column_info->column);
1565
5.80k
        } else {
1566
5.80k
            sub_column_info->indexes.clear();
1567
5.80k
        }
1568
9.90k
    };
1569
1570
220k
    const auto& sub_columns = parent_column.get_sub_columns();
1571
220k
    for (const auto& sub_column : sub_columns) {
1572
154k
        const char* pattern = sub_column->name().c_str();
1573
154k
        switch (sub_column->pattern_type()) {
1574
4.69k
        case PatternTypePB::MATCH_NAME: {
1575
4.69k
            if (strcmp(pattern, path.c_str()) == 0) {
1576
1.26k
                generate_result_column(*sub_column, &sub_column_info->column);
1577
1.26k
                generate_index(sub_column->name());
1578
1.26k
                return true;
1579
1.26k
            }
1580
3.42k
            break;
1581
4.69k
        }
1582
149k
        case PatternTypePB::MATCH_NAME_GLOB: {
1583
149k
            if (glob_match_re2(pattern, path)) {
1584
8.64k
                generate_result_column(*sub_column, &sub_column_info->column);
1585
8.64k
                generate_index(sub_column->name());
1586
8.64k
                return true;
1587
8.64k
            }
1588
140k
            break;
1589
149k
        }
1590
140k
        default:
1591
0
            break;
1592
154k
        }
1593
154k
    }
1594
210k
    return false;
1595
220k
}
1596
1597
TabletSchemaSPtr VariantCompactionUtil::calculate_variant_extended_schema(
1598
1.41k
        const std::vector<RowsetSharedPtr>& rowsets, const TabletSchemaSPtr& base_schema) {
1599
1.41k
    if (rowsets.empty()) {
1600
0
        return nullptr;
1601
0
    }
1602
1603
1.41k
    std::vector<TabletSchemaSPtr> schemas;
1604
3.31k
    for (const auto& rs : rowsets) {
1605
3.31k
        if (rs->num_segments() == 0) {
1606
3.13k
            continue;
1607
3.13k
        }
1608
179
        const auto& tablet_schema = rs->tablet_schema();
1609
179
        SegmentCacheHandle segment_cache;
1610
179
        auto st = SegmentLoader::instance()->load_segments(std::static_pointer_cast<BetaRowset>(rs),
1611
179
                                                           &segment_cache);
1612
179
        if (!st.ok()) {
1613
0
            return base_schema;
1614
0
        }
1615
179
        for (const auto& segment : segment_cache.get_segments()) {
1616
179
            TabletSchemaSPtr schema = tablet_schema->copy_without_variant_extracted_columns();
1617
364
            for (const auto& column : tablet_schema->columns()) {
1618
364
                if (!column->is_variant_type()) {
1619
179
                    continue;
1620
179
                }
1621
185
                std::shared_ptr<ColumnReader> column_reader;
1622
185
                OlapReaderStatistics stats;
1623
185
                st = segment->get_column_reader(column->unique_id(), &column_reader, &stats);
1624
185
                if (!st.ok()) {
1625
0
                    LOG(WARNING) << "Failed to get column reader for column: " << column->name()
1626
0
                                 << " error: " << st.to_string();
1627
0
                    continue;
1628
0
                }
1629
185
                if (!column_reader) {
1630
0
                    continue;
1631
0
                }
1632
1633
185
                CHECK(column_reader->get_meta_type() == FieldType::OLAP_FIELD_TYPE_VARIANT);
1634
185
                auto* variant_column_reader =
1635
185
                        assert_cast<segment_v2::VariantColumnReader*>(column_reader.get());
1636
                // load external meta before getting subcolumn meta info
1637
185
                st = variant_column_reader->load_external_meta_once();
1638
185
                if (!st.ok()) {
1639
0
                    LOG(WARNING) << "Failed to load external meta for column: " << column->name()
1640
0
                                 << " error: " << st.to_string();
1641
0
                    continue;
1642
0
                }
1643
185
                const auto* subcolumn_meta_info = variant_column_reader->get_subcolumns_meta_info();
1644
519
                for (const auto& entry : *subcolumn_meta_info) {
1645
519
                    if (entry->path.empty()) {
1646
185
                        continue;
1647
185
                    }
1648
334
                    const std::string& column_name =
1649
334
                            column->name_lower_case() + "." + entry->path.get_path();
1650
334
                    const DataTypePtr& data_type = entry->data.file_column_type;
1651
334
                    PathInDataBuilder full_path_builder;
1652
334
                    auto full_path = full_path_builder.append(column->name_lower_case(), false)
1653
334
                                             .append(entry->path.get_parts(), false)
1654
334
                                             .build();
1655
334
                    TabletColumn subcolumn =
1656
334
                            get_column_by_type(data_type, column_name,
1657
334
                                               ExtraInfo {.unique_id = -1,
1658
334
                                                          .parent_unique_id = column->unique_id(),
1659
334
                                                          .path_info = full_path});
1660
334
                    schema->append_column(subcolumn);
1661
334
                }
1662
185
            }
1663
179
            schemas.emplace_back(schema);
1664
179
        }
1665
179
    }
1666
1.41k
    TabletSchemaSPtr least_common_schema;
1667
1.41k
    auto st = get_least_common_schema(schemas, base_schema, least_common_schema, false);
1668
1.41k
    if (!st.ok()) {
1669
0
        return base_schema;
1670
0
    }
1671
1.41k
    return least_common_schema;
1672
1.41k
}
1673
1674
bool inherit_index(const std::vector<const TabletIndex*>& parent_indexes,
1675
                   TabletIndexes& subcolumns_indexes, FieldType column_type,
1676
76.3k
                   const std::string& suffix_path, bool is_array_nested_type) {
1677
76.3k
    if (parent_indexes.empty()) {
1678
70.9k
        return false;
1679
70.9k
    }
1680
5.42k
    subcolumns_indexes.clear();
1681
    // bkd index or array index only need to inherit one index
1682
5.42k
    if (field_is_numeric_type(column_type) ||
1683
5.42k
        (is_array_nested_type &&
1684
3.63k
         (field_is_numeric_type(column_type) || field_is_slice_type(column_type)))) {
1685
1.80k
        auto index_ptr = std::make_shared<TabletIndex>(*parent_indexes[0]);
1686
1.80k
        index_ptr->set_escaped_escaped_index_suffix_path(suffix_path);
1687
        // no need parse for bkd index or array index
1688
1.80k
        index_ptr->remove_parser_and_analyzer();
1689
1.80k
        subcolumns_indexes.emplace_back(std::move(index_ptr));
1690
1.80k
        return true;
1691
1.80k
    }
1692
    // string type need to inherit all indexes
1693
3.62k
    else if (field_is_slice_type(column_type) && !is_array_nested_type) {
1694
3.62k
        for (const auto& index : parent_indexes) {
1695
3.62k
            auto index_ptr = std::make_shared<TabletIndex>(*index);
1696
3.62k
            index_ptr->set_escaped_escaped_index_suffix_path(suffix_path);
1697
3.62k
            subcolumns_indexes.emplace_back(std::move(index_ptr));
1698
3.62k
        }
1699
3.59k
        return true;
1700
3.59k
    }
1701
33
    return false;
1702
5.42k
}
1703
1704
bool inherit_index(const std::vector<const TabletIndex*>& parent_indexes,
1705
76.3k
                   TabletIndexes& subcolumns_indexes, const TabletColumn& column) {
1706
76.3k
    if (!column.is_extracted_column()) {
1707
3
        return false;
1708
3
    }
1709
76.3k
    if (column.is_array_type()) {
1710
1.16k
        if (column.get_sub_columns().empty()) {
1711
0
            return false;
1712
0
        }
1713
1.16k
        const TabletColumn* nested = column.get_sub_columns()[0].get();
1714
1.16k
        while (nested != nullptr && nested->is_array_type()) {
1715
0
            if (nested->get_sub_columns().empty()) {
1716
0
                return false;
1717
0
            }
1718
0
            nested = nested->get_sub_columns()[0].get();
1719
0
        }
1720
1.16k
        if (nested == nullptr) {
1721
0
            return false;
1722
0
        }
1723
1.16k
        return inherit_index(parent_indexes, subcolumns_indexes, nested->type(),
1724
1.16k
                             column.path_info_ptr()->get_path(), true);
1725
1.16k
    }
1726
75.1k
    return inherit_index(parent_indexes, subcolumns_indexes, column.type(),
1727
75.1k
                         column.path_info_ptr()->get_path());
1728
76.3k
}
1729
1730
bool inherit_index(const std::vector<const TabletIndex*>& parent_indexes,
1731
0
                   TabletIndexes& subcolumns_indexes, const ColumnMetaPB& column_pb) {
1732
0
    if (!column_pb.has_column_path_info()) {
1733
0
        return false;
1734
0
    }
1735
0
    if (column_pb.type() == (int)FieldType::OLAP_FIELD_TYPE_ARRAY) {
1736
0
        if (column_pb.children_columns_size() == 0) {
1737
0
            return false;
1738
0
        }
1739
0
        const ColumnMetaPB* nested = &column_pb.children_columns(0);
1740
0
        while (nested != nullptr && nested->type() == (int)FieldType::OLAP_FIELD_TYPE_ARRAY) {
1741
0
            if (nested->children_columns_size() == 0) {
1742
0
                return false;
1743
0
            }
1744
0
            nested = &nested->children_columns(0);
1745
0
        }
1746
0
        if (nested == nullptr) {
1747
0
            return false;
1748
0
        }
1749
0
        return inherit_index(parent_indexes, subcolumns_indexes, (FieldType)nested->type(),
1750
0
                             column_pb.column_path_info().path(), true);
1751
0
    }
1752
0
    return inherit_index(parent_indexes, subcolumns_indexes, (FieldType)column_pb.type(),
1753
0
                         column_pb.column_path_info().path());
1754
0
}
1755
1756
// ============ Implementation from parse2column.cpp ============
1757
1758
/** Pool for objects that cannot be used from different threads simultaneously.
1759
  * Allows to create an object for each thread.
1760
  * Pool has unbounded size and objects are not destroyed before destruction of pool.
1761
  *
1762
  * Use it in cases when thread local storage is not appropriate
1763
  *  (when maximum number of simultaneously used objects is less
1764
  *   than number of running/sleeping threads, that has ever used object,
1765
  *   and creation/destruction of objects is expensive).
1766
  */
1767
template <typename T>
1768
class SimpleObjectPool {
1769
protected:
1770
    /// Hold all available objects in stack.
1771
    std::mutex mutex;
1772
    std::stack<std::unique_ptr<T>> stack;
1773
    /// Specialized deleter for std::unique_ptr.
1774
    /// Returns underlying pointer back to stack thus reclaiming its ownership.
1775
    struct Deleter {
1776
        SimpleObjectPool<T>* parent;
1777
16.9k
        Deleter(SimpleObjectPool<T>* parent_ = nullptr) : parent {parent_} {} /// NOLINT
1778
16.9k
        void operator()(T* owning_ptr) const {
1779
16.9k
            std::lock_guard lock {parent->mutex};
1780
16.9k
            parent->stack.emplace(owning_ptr);
1781
16.9k
        }
1782
    };
1783
1784
public:
1785
    using Pointer = std::unique_ptr<T, Deleter>;
1786
    /// Extracts and returns a pointer from the stack if it's not empty,
1787
    ///  creates a new one by calling provided f() otherwise.
1788
    template <typename Factory>
1789
16.7k
    Pointer get(Factory&& f) {
1790
16.7k
        std::unique_lock lock(mutex);
1791
16.7k
        if (stack.empty()) {
1792
23
            return {f(), this};
1793
23
        }
1794
16.7k
        auto object = stack.top().release();
1795
16.7k
        stack.pop();
1796
16.7k
        return std::unique_ptr<T, Deleter>(object, Deleter(this));
1797
16.7k
    }
variant_util.cpp:_ZN5doris12variant_util16SimpleObjectPoolINS_14JSONDataParserINS_14SimdJSONParserEEEE3getIZNS0_21parse_json_to_variantERNS_7IColumnERKNS_9StringRefEPS4_RKNS_11ParseConfigEE3$_0EESt10unique_ptrIS4_NS5_7DeleterEEOT_
Line
Count
Source
1789
12.4k
    Pointer get(Factory&& f) {
1790
12.4k
        std::unique_lock lock(mutex);
1791
12.4k
        if (stack.empty()) {
1792
1
            return {f(), this};
1793
1
        }
1794
12.4k
        auto object = stack.top().release();
1795
12.4k
        stack.pop();
1796
12.4k
        return std::unique_ptr<T, Deleter>(object, Deleter(this));
1797
12.4k
    }
variant_util.cpp:_ZN5doris12variant_util16SimpleObjectPoolINS_14JSONDataParserINS_14SimdJSONParserEEEE3getIZNS0_21parse_json_to_variantERNS_7IColumnERKNS_9ColumnStrIjEERKNS_11ParseConfigEE3$_0EESt10unique_ptrIS4_NS5_7DeleterEEOT_
Line
Count
Source
1789
4.36k
    Pointer get(Factory&& f) {
1790
4.36k
        std::unique_lock lock(mutex);
1791
4.36k
        if (stack.empty()) {
1792
22
            return {f(), this};
1793
22
        }
1794
4.34k
        auto object = stack.top().release();
1795
4.34k
        stack.pop();
1796
4.34k
        return std::unique_ptr<T, Deleter>(object, Deleter(this));
1797
4.36k
    }
1798
    /// Like get(), but creates object using default constructor.
1799
    Pointer getDefault() {
1800
        return get([] { return new T; });
1801
    }
1802
};
1803
1804
SimpleObjectPool<JsonParser> parsers_pool;
1805
1806
using Node = typename ColumnVariant::Subcolumns::Node;
1807
1808
23.6M
static inline void append_binary_bytes(ColumnString::Chars& chars, const void* data, size_t size) {
1809
23.6M
    const auto old_size = chars.size();
1810
23.6M
    chars.resize(old_size + size);
1811
23.6M
    memcpy(chars.data() + old_size, reinterpret_cast<const char*>(data), size);
1812
23.6M
}
1813
1814
11.1M
static inline void append_binary_type(ColumnString::Chars& chars, FieldType type) {
1815
11.1M
    const uint8_t t = static_cast<uint8_t>(type);
1816
11.1M
    append_binary_bytes(chars, &t, sizeof(uint8_t));
1817
11.1M
}
1818
1819
6.24M
static inline void append_binary_sizet(ColumnString::Chars& chars, size_t v) {
1820
6.24M
    append_binary_bytes(chars, &v, sizeof(size_t));
1821
6.24M
}
1822
1823
10.2M
static void append_field_to_binary_chars(const Field& field, ColumnString::Chars& chars) {
1824
10.2M
    switch (field.get_type()) {
1825
0
    case PrimitiveType::TYPE_NULL: {
1826
0
        append_binary_type(chars, FieldType::OLAP_FIELD_TYPE_NONE);
1827
0
        return;
1828
0
    }
1829
109k
    case PrimitiveType::TYPE_BOOLEAN: {
1830
109k
        append_binary_type(chars,
1831
109k
                           TabletColumn::get_field_type_by_type(PrimitiveType::TYPE_BOOLEAN));
1832
109k
        const auto v = static_cast<UInt8>(field.get<PrimitiveType::TYPE_BOOLEAN>());
1833
109k
        append_binary_bytes(chars, &v, sizeof(UInt8));
1834
109k
        return;
1835
0
    }
1836
3.31M
    case PrimitiveType::TYPE_BIGINT: {
1837
3.31M
        append_binary_type(chars, TabletColumn::get_field_type_by_type(PrimitiveType::TYPE_BIGINT));
1838
3.31M
        const auto v = field.get<PrimitiveType::TYPE_BIGINT>();
1839
3.31M
        append_binary_bytes(chars, &v, sizeof(Int64));
1840
3.31M
        return;
1841
0
    }
1842
1
    case PrimitiveType::TYPE_LARGEINT: {
1843
1
        append_binary_type(chars,
1844
1
                           TabletColumn::get_field_type_by_type(PrimitiveType::TYPE_LARGEINT));
1845
1
        const auto v = field.get<PrimitiveType::TYPE_LARGEINT>();
1846
1
        append_binary_bytes(chars, &v, sizeof(int128_t));
1847
1
        return;
1848
0
    }
1849
2.48M
    case PrimitiveType::TYPE_DOUBLE: {
1850
2.48M
        append_binary_type(chars, TabletColumn::get_field_type_by_type(PrimitiveType::TYPE_DOUBLE));
1851
2.48M
        const auto v = field.get<PrimitiveType::TYPE_DOUBLE>();
1852
2.48M
        append_binary_bytes(chars, &v, sizeof(Float64));
1853
2.48M
        return;
1854
0
    }
1855
6.09M
    case PrimitiveType::TYPE_STRING: {
1856
6.09M
        append_binary_type(chars, FieldType::OLAP_FIELD_TYPE_STRING);
1857
6.09M
        const auto& v = field.get<PrimitiveType::TYPE_STRING>();
1858
6.09M
        append_binary_sizet(chars, v.size());
1859
6.09M
        append_binary_bytes(chars, v.data(), v.size());
1860
6.09M
        return;
1861
0
    }
1862
17.4k
    case PrimitiveType::TYPE_JSONB: {
1863
17.4k
        append_binary_type(chars, FieldType::OLAP_FIELD_TYPE_JSONB);
1864
17.4k
        const auto& v = field.get<PrimitiveType::TYPE_JSONB>();
1865
17.4k
        append_binary_sizet(chars, v.get_size());
1866
17.4k
        append_binary_bytes(chars, v.get_value(), v.get_size());
1867
17.4k
        return;
1868
0
    }
1869
150k
    case PrimitiveType::TYPE_ARRAY: {
1870
150k
        append_binary_type(chars, FieldType::OLAP_FIELD_TYPE_ARRAY);
1871
150k
        const auto& a = field.get<PrimitiveType::TYPE_ARRAY>();
1872
150k
        append_binary_sizet(chars, a.size());
1873
162k
        for (const auto& elem : a) {
1874
162k
            append_field_to_binary_chars(elem, chars);
1875
162k
        }
1876
150k
        return;
1877
0
    }
1878
0
    default:
1879
0
        throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Unsupported field type {}",
1880
0
                               field.get_type());
1881
10.2M
    }
1882
10.2M
}
1883
template <typename ParserImpl>
1884
void parse_json_to_variant_impl(IColumn& column, const char* src, size_t length,
1885
1.25M
                                JSONDataParser<ParserImpl>* parser, const ParseConfig& config) {
1886
1.25M
    auto& column_variant = assert_cast<ColumnVariant&>(column);
1887
1.25M
    std::optional<ParseResult> result;
1888
    /// Treat empty string as an empty object
1889
    /// for better CAST from String to Object.
1890
1.25M
    if (length > 0) {
1891
1.25M
        result = parser->parse(src, length, config);
1892
18.4E
    } else {
1893
18.4E
        result = ParseResult {};
1894
18.4E
    }
1895
1.25M
    if (!result) {
1896
653
        VLOG_DEBUG << "failed to parse " << std::string_view(src, length) << ", length= " << length;
1897
653
        if (config::variant_throw_exeception_on_invalid_json) {
1898
0
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Failed to parse object {}",
1899
0
                                   std::string_view(src, length));
1900
0
        }
1901
        // Treat as string
1902
653
        PathInData root_path;
1903
653
        Field field = Field::create_field<TYPE_STRING>(String(src, length));
1904
653
        result = ParseResult {{root_path}, {field}};
1905
653
    }
1906
1.25M
    auto& [paths, values] = *result;
1907
1.25M
    assert(paths.size() == values.size());
1908
1.25M
    size_t old_num_rows = column_variant.rows();
1909
1.25M
    if (config.deprecated_enable_flatten_nested) {
1910
        // here we should check the paths in variant and paths in result,
1911
        // if two paths which same prefix have different structure, we should throw an exception
1912
26
        std::vector<PathInData> check_paths;
1913
26
        for (const auto& entry : column_variant.get_subcolumns()) {
1914
26
            check_paths.push_back(entry->path);
1915
26
        }
1916
26
        check_paths.insert(check_paths.end(), paths.begin(), paths.end());
1917
26
        THROW_IF_ERROR(check_variant_has_no_ambiguous_paths(check_paths));
1918
26
    }
1919
1.25M
    auto [doc_value_data_paths, doc_value_data_values] =
1920
1.25M
            column_variant.get_doc_value_data_paths_and_values();
1921
1.25M
    auto& doc_value_data_offsets = column_variant.serialized_doc_value_column_offsets();
1922
1923
5.62M
    auto flush_defaults = [](ColumnVariant::Subcolumn* subcolumn) {
1924
5.62M
        const auto num_defaults = subcolumn->cur_num_of_defaults();
1925
5.62M
        if (num_defaults > 0) {
1926
2.21M
            subcolumn->insert_many_defaults(num_defaults);
1927
2.21M
            subcolumn->reset_current_num_of_defaults();
1928
2.21M
        }
1929
5.62M
    };
1930
1931
1.25M
    auto is_plain_path = [](const PathInData& path) {
1932
8.74k
        for (const auto& part : path.get_parts()) {
1933
8.74k
            if (part.is_nested || part.anonymous_array_level != 0) {
1934
3
                return false;
1935
3
            }
1936
8.74k
        }
1937
8.66k
        return true;
1938
8.67k
    };
1939
1940
1.25M
    auto get_or_create_subcolumn = [&](const PathInData& path, size_t index_hint,
1941
5.62M
                                       const FieldInfo& field_info) -> ColumnVariant::Subcolumn* {
1942
5.62M
        auto* subcolumn = column_variant.get_subcolumn(path, index_hint);
1943
5.62M
        if (subcolumn == nullptr) {
1944
98.2k
            if (path.has_nested_part()) {
1945
17
                column_variant.add_nested_subcolumn(path, field_info, old_num_rows);
1946
98.2k
            } else {
1947
98.2k
                column_variant.add_sub_column(path, old_num_rows);
1948
98.2k
            }
1949
98.2k
            subcolumn = column_variant.get_subcolumn(path, index_hint);
1950
98.2k
        }
1951
5.62M
        if (!subcolumn) {
1952
0
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Failed to find sub column {}",
1953
0
                                   path.get_path());
1954
0
        }
1955
5.62M
        return subcolumn;
1956
5.62M
    };
1957
1958
5.61M
    auto normalize_plain_path = [&](const PathInData& path) {
1959
5.61M
        if (!config.check_duplicate_json_path || path.empty() || !is_plain_path(path)) {
1960
5.61M
            return path;
1961
5.61M
        }
1962
8.19k
        return PathInData(path.get_path());
1963
5.61M
    };
1964
1965
1.25M
    auto insert_into_subcolumn = [&](size_t i,
1966
5.67M
                                     bool check_size_mismatch) -> ColumnVariant::Subcolumn* {
1967
5.67M
        FieldInfo field_info;
1968
5.67M
        get_field_info(values[i], &field_info);
1969
5.67M
        if (field_info.scalar_type_id == PrimitiveType::INVALID_TYPE) {
1970
52.6k
            return nullptr;
1971
52.6k
        }
1972
5.62M
        auto path = normalize_plain_path(paths[i]);
1973
5.62M
        auto* subcolumn = get_or_create_subcolumn(path, i, field_info);
1974
5.62M
        flush_defaults(subcolumn);
1975
5.62M
        if (check_size_mismatch && subcolumn->size() != old_num_rows) {
1976
2
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
1977
2
                                   "subcolumn {} size missmatched, may contains duplicated entry",
1978
2
                                   path.get_path());
1979
2
        }
1980
5.62M
        subcolumn->insert(std::move(values[i]), std::move(field_info));
1981
5.62M
        return subcolumn;
1982
5.62M
    };
1983
1984
1.25M
    switch (config.parse_to) {
1985
245k
    case ParseConfig::ParseTo::OnlySubcolumns:
1986
4.56M
        for (size_t i = 0; i < paths.size(); ++i) {
1987
4.32M
            insert_into_subcolumn(i, true);
1988
4.32M
        }
1989
245k
        break;
1990
1.00M
    case ParseConfig::ParseTo::OnlyDocValueColumn: {
1991
18.4E
        CHECK(column_variant.enable_doc_mode()) << "OnlyDocValueColumn requires doc mode enabled";
1992
1.00M
        std::vector<size_t> doc_item_indexes;
1993
1.00M
        doc_item_indexes.reserve(paths.size());
1994
1.00M
        phmap::flat_hash_set<StringRef, StringRefHash> seen_paths;
1995
1.00M
        seen_paths.reserve(paths.size());
1996
1997
14.1M
        for (size_t i = 0; i < paths.size(); ++i) {
1998
13.1M
            FieldInfo field_info;
1999
13.1M
            get_field_info(values[i], &field_info);
2000
13.1M
            if (paths[i].empty()) {
2001
703
                auto* subcolumn = column_variant.get_subcolumn(paths[i]);
2002
703
                DCHECK(subcolumn != nullptr);
2003
703
                flush_defaults(subcolumn);
2004
703
                subcolumn->insert(std::move(values[i]), std::move(field_info));
2005
703
                continue;
2006
703
            }
2007
13.1M
            if (field_info.scalar_type_id == PrimitiveType::INVALID_TYPE ||
2008
13.1M
                values[i].get_type() == PrimitiveType::TYPE_NULL) {
2009
64.1k
                continue;
2010
64.1k
            }
2011
13.0M
            const auto& path_str = paths[i].get_path();
2012
13.0M
            StringRef path_ref {path_str.data(), path_str.size()};
2013
13.0M
            if (UNLIKELY(!seen_paths.emplace(path_ref).second)) {
2014
0
                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
2015
0
                                       "may contains duplicated entry : {}",
2016
0
                                       std::string_view(path_str));
2017
0
            }
2018
13.0M
            doc_item_indexes.push_back(i);
2019
13.0M
        }
2020
2021
1.00M
        std::sort(doc_item_indexes.begin(), doc_item_indexes.end(),
2022
42.9M
                  [&](size_t l, size_t r) { return paths[l].get_path() < paths[r].get_path(); });
2023
9.34M
        for (const auto idx : doc_item_indexes) {
2024
9.34M
            const auto& path_str = paths[idx].get_path();
2025
9.34M
            doc_value_data_paths->insert_data(path_str.data(), path_str.size());
2026
9.34M
            auto& chars = doc_value_data_values->get_chars();
2027
9.34M
            append_field_to_binary_chars(values[idx], chars);
2028
9.34M
            doc_value_data_values->get_offsets().push_back(chars.size());
2029
9.34M
        }
2030
1.00M
    } break;
2031
1.25M
    }
2032
1.26M
    doc_value_data_offsets.push_back(doc_value_data_paths->size());
2033
    // /// Insert default values to missed subcolumns.
2034
1.26M
    const auto& subcolumns = column_variant.get_subcolumns();
2035
35.3M
    for (const auto& entry : subcolumns) {
2036
35.3M
        if (entry->data.size() == old_num_rows) {
2037
            // Handle nested paths differently from simple paths
2038
31.1M
            if (entry->path.has_nested_part()) {
2039
                // Try to insert default from nested, if failed, insert regular default
2040
0
                bool success = UNLIKELY(column_variant.try_insert_default_from_nested(entry));
2041
0
                if (!success) {
2042
0
                    entry->data.insert_default();
2043
0
                }
2044
31.1M
            } else {
2045
                // For non-nested paths, increment default counter
2046
31.1M
                entry->data.increment_default_counter();
2047
31.1M
            }
2048
31.1M
        }
2049
35.3M
    }
2050
1.26M
    column_variant.incr_num_rows();
2051
1.26M
    auto sparse_column = column_variant.get_sparse_column();
2052
1.26M
    if (sparse_column->size() == old_num_rows) {
2053
1.26M
        sparse_column->assume_mutable()->insert_default();
2054
1.26M
    }
2055
#ifndef NDEBUG
2056
    column_variant.check_consistency();
2057
#endif
2058
1.26M
}
2059
2060
// exposed interfaces
2061
void parse_json_to_variant(IColumn& column, const StringRef& json, JsonParser* parser,
2062
12.4k
                           const ParseConfig& config) {
2063
12.4k
    if (parser) {
2064
0
        return parse_json_to_variant_impl(column, json.data, json.size, parser, config);
2065
12.4k
    } else {
2066
12.4k
        auto pool_parser = parsers_pool.get([] { return new JsonParser(); });
2067
12.4k
        return parse_json_to_variant_impl(column, json.data, json.size, pool_parser.get(), config);
2068
12.4k
    }
2069
12.4k
}
2070
2071
void parse_json_to_variant(IColumn& column, const ColumnString& raw_json_column,
2072
4.35k
                           const ParseConfig& config) {
2073
4.35k
    auto parser = parsers_pool.get([] { return new JsonParser(); });
2074
1.33M
    for (size_t i = 0; i < raw_json_column.size(); ++i) {
2075
1.33M
        StringRef raw_json = raw_json_column.get_data_at(i);
2076
1.33M
        parse_json_to_variant_impl(column, raw_json.data, raw_json.size, parser.get(), config);
2077
1.33M
    }
2078
4.35k
    column.finalize();
2079
4.35k
}
2080
2081
// parse the doc snapshot column to subcolumns
2082
0
void materialize_docs_to_subcolumns(ColumnVariant& column_variant) {
2083
0
    auto subcolumns = materialize_docs_to_subcolumns_map(column_variant);
2084
2085
0
    for (auto& entry : subcolumns) {
2086
0
        entry.second.finalize();
2087
0
        if (!column_variant.add_sub_column(PathInData(entry.first),
2088
0
                                           IColumn::mutate(entry.second.get_finalized_column_ptr()),
2089
0
                                           entry.second.get_least_common_type())) {
2090
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
2091
0
                                   "Failed to add subcolumn {}, which is from doc snapshot column",
2092
0
                                   entry.first);
2093
0
        }
2094
0
    }
2095
2096
0
    column_variant.finalize();
2097
0
}
2098
2099
// ============ Implementation from variant_util.cpp ============
2100
2101
phmap::flat_hash_map<std::string_view, ColumnVariant::Subcolumn> materialize_docs_to_subcolumns_map(
2102
0
        const ColumnVariant& variant, size_t expected_unique_paths) {
2103
0
    constexpr size_t kInitialPathReserve = 8192;
2104
0
    phmap::flat_hash_map<std::string_view, ColumnVariant::Subcolumn> subcolumns;
2105
2106
0
    const auto [column_key, column_value] = variant.get_doc_value_data_paths_and_values();
2107
0
    const auto& column_offsets = variant.serialized_doc_value_column_offsets();
2108
0
    const size_t num_rows = column_offsets.size();
2109
2110
0
    DCHECK_EQ(num_rows, variant.size()) << "doc snapshot offsets size mismatch with variant rows";
2111
2112
0
    subcolumns.reserve(expected_unique_paths != 0
2113
0
                               ? expected_unique_paths
2114
0
                               : std::min<size_t>(column_key->size(), kInitialPathReserve));
2115
2116
0
    for (size_t row = 0; row < num_rows; ++row) {
2117
0
        const size_t start = column_offsets[row - 1];
2118
0
        const size_t end = column_offsets[row];
2119
0
        for (size_t i = start; i < end; ++i) {
2120
0
            const auto& key = column_key->get_data_at(i);
2121
0
            const std::string_view path_sv(key.data, key.size);
2122
2123
0
            auto [it, inserted] =
2124
0
                    subcolumns.try_emplace(path_sv, ColumnVariant::Subcolumn {0, true, false});
2125
0
            auto& subcolumn = it->second;
2126
0
            if (inserted) {
2127
0
                subcolumn.insert_many_defaults(row);
2128
0
            } else if (subcolumn.size() != row) {
2129
0
                subcolumn.insert_many_defaults(row - subcolumn.size());
2130
0
            }
2131
0
            subcolumn.deserialize_from_binary_column(column_value, i);
2132
0
        }
2133
0
    }
2134
2135
0
    for (auto& [path, subcolumn] : subcolumns) {
2136
0
        if (subcolumn.size() != num_rows) {
2137
0
            subcolumn.insert_many_defaults(num_rows - subcolumn.size());
2138
0
        }
2139
0
    }
2140
2141
0
    return subcolumns;
2142
0
}
2143
2144
Status _parse_and_materialize_variant_columns(Block& block,
2145
                                              const std::vector<uint32_t>& variant_pos,
2146
4.45k
                                              const std::vector<ParseConfig>& configs) {
2147
9.75k
    for (size_t i = 0; i < variant_pos.size(); ++i) {
2148
5.30k
        auto column_ref = block.get_by_position(variant_pos[i]).column;
2149
5.30k
        bool is_nullable = column_ref->is_nullable();
2150
5.30k
        MutableColumnPtr var_column = column_ref->assume_mutable();
2151
5.30k
        if (is_nullable) {
2152
4.94k
            const auto& nullable = assert_cast<const ColumnNullable&>(*column_ref);
2153
4.94k
            var_column = nullable.get_nested_column_ptr()->assume_mutable();
2154
4.94k
        }
2155
5.30k
        auto& var = assert_cast<ColumnVariant&>(*var_column);
2156
5.30k
        var_column->finalize();
2157
2158
5.30k
        MutableColumnPtr variant_column;
2159
5.30k
        if (!var.is_scalar_variant()) {
2160
            // already parsed
2161
1.15k
            continue;
2162
1.15k
        }
2163
2164
18.4E
        VLOG_DEBUG << "parse scalar variant column: " << var.get_root_type()->get_name();
2165
4.15k
        ColumnPtr scalar_root_column;
2166
4.15k
        if (var.get_root_type()->get_primitive_type() == TYPE_JSONB) {
2167
            // TODO more efficient way to parse jsonb type, currently we just convert jsonb to
2168
            // json str and parse them into variant
2169
30
            RETURN_IF_ERROR(cast_column({var.get_root(), var.get_root_type(), ""},
2170
30
                                        var.get_root()->is_nullable()
2171
30
                                                ? make_nullable(std::make_shared<DataTypeString>())
2172
30
                                                : std::make_shared<DataTypeString>(),
2173
30
                                        &scalar_root_column));
2174
30
            if (scalar_root_column->is_nullable()) {
2175
30
                scalar_root_column = assert_cast<const ColumnNullable*>(scalar_root_column.get())
2176
30
                                             ->get_nested_column_ptr();
2177
30
            }
2178
4.12k
        } else {
2179
4.12k
            const auto& root = *var.get_root();
2180
4.12k
            scalar_root_column =
2181
4.12k
                    root.is_nullable()
2182
4.12k
                            ? assert_cast<const ColumnNullable&>(root).get_nested_column_ptr()
2183
4.12k
                            : var.get_root();
2184
4.12k
        }
2185
2186
4.23k
        if (scalar_root_column->is_column_string()) {
2187
4.23k
            variant_column = ColumnVariant::create(0, var.enable_doc_mode());
2188
4.23k
            parse_json_to_variant(*variant_column.get(),
2189
4.23k
                                  assert_cast<const ColumnString&>(*scalar_root_column),
2190
4.23k
                                  configs[i]);
2191
18.4E
        } else {
2192
            // Root maybe other types rather than string like ColumnVariant(Int32).
2193
            // In this case, we should finlize the root and cast to JSON type
2194
18.4E
            auto expected_root_type =
2195
18.4E
                    make_nullable(std::make_shared<ColumnVariant::MostCommonType>());
2196
18.4E
            var.ensure_root_node_type(expected_root_type);
2197
18.4E
            variant_column = var.assume_mutable();
2198
18.4E
        }
2199
2200
        // Wrap variant with nullmap if it is nullable
2201
4.15k
        ColumnPtr result = variant_column->get_ptr();
2202
4.15k
        if (is_nullable) {
2203
4.07k
            const auto& null_map =
2204
4.07k
                    assert_cast<const ColumnNullable&>(*column_ref).get_null_map_column_ptr();
2205
4.07k
            result = ColumnNullable::create(result, null_map);
2206
4.07k
        }
2207
4.15k
        block.get_by_position(variant_pos[i]).column = result;
2208
4.15k
    }
2209
4.45k
    return Status::OK();
2210
4.45k
}
2211
2212
Status parse_and_materialize_variant_columns(Block& block, const std::vector<uint32_t>& variant_pos,
2213
4.45k
                                             const std::vector<ParseConfig>& configs) {
2214
4.45k
    RETURN_IF_CATCH_EXCEPTION(
2215
4.45k
            { return _parse_and_materialize_variant_columns(block, variant_pos, configs); });
2216
4.45k
}
2217
2218
Status parse_and_materialize_variant_columns(Block& block, const TabletSchema& tablet_schema,
2219
4.60k
                                             const std::vector<uint32_t>& column_pos) {
2220
4.60k
    std::vector<uint32_t> variant_column_pos;
2221
4.60k
    std::vector<uint32_t> variant_schema_pos;
2222
4.60k
    variant_column_pos.reserve(column_pos.size());
2223
4.60k
    variant_schema_pos.reserve(column_pos.size());
2224
27.6k
    for (size_t block_pos = 0; block_pos < column_pos.size(); ++block_pos) {
2225
23.0k
        const uint32_t schema_pos = column_pos[block_pos];
2226
23.0k
        const auto& column = tablet_schema.column(schema_pos);
2227
23.0k
        if (column.is_variant_type()) {
2228
5.19k
            variant_column_pos.push_back(schema_pos);
2229
5.19k
            variant_schema_pos.push_back(schema_pos);
2230
5.19k
        }
2231
23.0k
    }
2232
2233
4.60k
    if (variant_column_pos.empty()) {
2234
46
        return Status::OK();
2235
46
    }
2236
2237
4.55k
    std::vector<ParseConfig> configs(variant_column_pos.size());
2238
9.85k
    for (size_t i = 0; i < variant_column_pos.size(); ++i) {
2239
        // Deprecated legacy flatten-nested switch. Distinct from variant_enable_nested_group.
2240
5.29k
        configs[i].deprecated_enable_flatten_nested =
2241
5.29k
                tablet_schema.deprecated_variant_flatten_nested();
2242
5.29k
        configs[i].check_duplicate_json_path = config::variant_enable_duplicate_json_path_check;
2243
5.29k
        const auto& column = tablet_schema.column(variant_schema_pos[i]);
2244
5.29k
        if (!column.is_variant_type()) {
2245
0
            return Status::InternalError("column is not variant type, column name: {}",
2246
0
                                         column.name());
2247
0
        }
2248
        // if doc mode is not enabled, no need to parse to doc value column
2249
5.29k
        if (!column.variant_enable_doc_mode()) {
2250
2.94k
            configs[i].parse_to = ParseConfig::ParseTo::OnlySubcolumns;
2251
2.94k
            continue;
2252
2.94k
        }
2253
2254
2.35k
        configs[i].parse_to = ParseConfig::ParseTo::OnlyDocValueColumn;
2255
2.35k
    }
2256
2257
4.55k
    RETURN_IF_ERROR(parse_and_materialize_variant_columns(block, variant_column_pos, configs));
2258
4.55k
    return Status::OK();
2259
4.55k
}
2260
2261
} // namespace doris::variant_util