Coverage Report

Created: 2026-06-12 03:18

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.93k
inline void append_escaped_regex_char(std::string* regex_output, char ch) {
108
2.93k
    switch (ch) {
109
16
    case '.':
110
18
    case '^':
111
20
    case '$':
112
22
    case '+':
113
27
    case '*':
114
29
    case '?':
115
31
    case '(':
116
33
    case ')':
117
35
    case '|':
118
37
    case '{':
119
39
    case '}':
120
41
    case '[':
121
41
    case ']':
122
45
    case '\\':
123
45
        regex_output->push_back('\\');
124
45
        regex_output->push_back(ch);
125
45
        break;
126
2.88k
    default:
127
2.88k
        regex_output->push_back(ch);
128
2.88k
        break;
129
2.93k
    }
130
2.93k
}
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
167k
std::shared_ptr<RE2> get_or_build_re2(const std::string& glob_pattern) {
145
167k
    {
146
167k
        std::lock_guard<std::mutex> lock(g_glob_regex_cache_mutex);
147
167k
        auto it = g_glob_regex_cache.find(glob_pattern);
148
167k
        if (it != g_glob_regex_cache.end()) {
149
167k
            g_glob_regex_cache_lru.splice(g_glob_regex_cache_lru.begin(), g_glob_regex_cache_lru,
150
167k
                                          it->second.lru_it);
151
167k
            return it->second.re2;
152
167k
        }
153
167k
    }
154
70
    std::string regex_pattern;
155
70
    Status st = glob_to_regex(glob_pattern, &regex_pattern);
156
70
    if (!st.ok()) {
157
2
        return nullptr;
158
2
    }
159
68
    auto compiled = std::make_shared<RE2>(regex_pattern);
160
68
    if (!compiled->ok()) {
161
3
        return nullptr;
162
3
    }
163
65
    {
164
65
        std::lock_guard<std::mutex> lock(g_glob_regex_cache_mutex);
165
65
        auto it = g_glob_regex_cache.find(glob_pattern);
166
65
        if (it != g_glob_regex_cache.end()) {
167
25
            g_glob_regex_cache_lru.splice(g_glob_regex_cache_lru.begin(), g_glob_regex_cache_lru,
168
25
                                          it->second.lru_it);
169
25
            return it->second.re2;
170
25
        }
171
40
        g_glob_regex_cache_lru.push_front(glob_pattern);
172
40
        g_glob_regex_cache.emplace(glob_pattern,
173
40
                                   GlobRegexCacheEntry {compiled, g_glob_regex_cache_lru.begin()});
174
40
        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
40
    }
180
0
    return compiled;
181
65
}
182
183
// Convert a restricted glob pattern into a regex.
184
// Supported: '*', '?', '[...]', '\\' escape. Others are treated as literals.
185
233
Status glob_to_regex(const std::string& glob_pattern, std::string* regex_pattern) {
186
233
    regex_pattern->clear();
187
233
    regex_pattern->append("^");
188
233
    bool is_escaped = false;
189
233
    size_t pattern_length = glob_pattern.size();
190
3.23k
    for (size_t index = 0; index < pattern_length; ++index) {
191
3.00k
        char current_char = glob_pattern[index];
192
3.00k
        if (is_escaped) {
193
9
            append_escaped_regex_char(regex_pattern, current_char);
194
9
            is_escaped = false;
195
9
            continue;
196
9
        }
197
2.99k
        if (current_char == '\\') {
198
13
            is_escaped = true;
199
13
            continue;
200
13
        }
201
2.98k
        if (current_char == '*') {
202
18
            regex_pattern->append(".*");
203
18
            continue;
204
18
        }
205
2.96k
        if (current_char == '?') {
206
13
            regex_pattern->append(".");
207
13
            continue;
208
13
        }
209
2.95k
        if (current_char == '[') {
210
32
            size_t class_index = index + 1;
211
32
            bool class_closed = false;
212
32
            bool is_class_escaped = false;
213
32
            std::string class_buffer;
214
32
            if (class_index < pattern_length &&
215
32
                (glob_pattern[class_index] == '!' || glob_pattern[class_index] == '^')) {
216
9
                class_buffer.push_back('^');
217
9
                ++class_index;
218
9
            }
219
95
            for (; class_index < pattern_length; ++class_index) {
220
91
                char class_char = glob_pattern[class_index];
221
91
                if (is_class_escaped) {
222
10
                    class_buffer.push_back(class_char);
223
10
                    is_class_escaped = false;
224
10
                    continue;
225
10
                }
226
81
                if (class_char == '\\') {
227
10
                    is_class_escaped = true;
228
10
                    continue;
229
10
                }
230
71
                if (class_char == ']') {
231
28
                    class_closed = true;
232
28
                    break;
233
28
                }
234
43
                class_buffer.push_back(class_char);
235
43
            }
236
32
            if (!class_closed) {
237
4
                return Status::InvalidArgument("Unclosed character class in glob pattern: {}",
238
4
                                               glob_pattern);
239
4
            }
240
28
            regex_pattern->append("[");
241
28
            regex_pattern->append(class_buffer);
242
28
            regex_pattern->append("]");
243
28
            index = class_index;
244
28
            continue;
245
32
        }
246
2.92k
        append_escaped_regex_char(regex_pattern, current_char);
247
2.92k
    }
248
229
    if (is_escaped) {
249
4
        append_escaped_regex_char(regex_pattern, '\\');
250
4
    }
251
229
    regex_pattern->append("$");
252
229
    return Status::OK();
253
233
}
254
255
167k
bool glob_match_re2(const std::string& glob_pattern, const std::string& candidate_path) {
256
167k
    auto compiled = get_or_build_re2(glob_pattern);
257
167k
    if (compiled == nullptr) {
258
5
        return false;
259
5
    }
260
167k
    return RE2::FullMatch(candidate_path, *compiled);
261
167k
}
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
248
        const std::unordered_map<int32_t, VariantExtendedInfo>& uid_to_variant_extended_info) {
278
248
    const auto info_it = uid_to_variant_extended_info.find(column->unique_id());
279
248
    return column->variant_enable_nested_group() ||
280
249
           (info_it != uid_to_variant_extended_info.end() && info_it->second.has_nested_group);
281
248
}
282
283
std::unordered_set<int32_t> collect_nested_group_compaction_root_uids(
284
        const TabletSchemaSPtr& target,
285
2.79k
        const std::unordered_map<int32_t, VariantExtendedInfo>& uid_to_variant_extended_info) {
286
2.79k
    std::unordered_set<int32_t> root_uids;
287
30.0k
    for (const TabletColumnPtr& column : target->columns()) {
288
30.0k
        if (column->is_variant_type() && should_materialize_nested_group_regular_subcolumns(
289
248
                                                 column, uid_to_variant_extended_info)) {
290
1
            root_uids.insert(column->unique_id());
291
1
        }
292
30.0k
    }
293
2.79k
    return root_uids;
294
2.79k
}
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
14
size_t get_number_of_dimensions(const IDataType& type) {
309
14
    if (const auto* type_array = typeid_cast<const DataTypeArray*>(&type)) {
310
4
        return type_array->get_number_of_dimensions();
311
4
    }
312
10
    return 0;
313
14
}
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
93.7k
DataTypePtr get_base_type_of_array(const DataTypePtr& type) {
322
    /// Get raw pointers to avoid extra copying of type pointers.
323
93.7k
    const DataTypeArray* last_array = nullptr;
324
93.7k
    const auto* current_type = type.get();
325
93.7k
    if (const auto* nullable = typeid_cast<const DataTypeNullable*>(current_type)) {
326
93.7k
        current_type = nullable->get_nested_type().get();
327
93.7k
    }
328
96.4k
    while (const auto* type_array = typeid_cast<const DataTypeArray*>(current_type)) {
329
2.73k
        current_type = type_array->get_nested_type().get();
330
2.73k
        last_array = type_array;
331
2.73k
        if (const auto* nullable = typeid_cast<const DataTypeNullable*>(current_type)) {
332
2.73k
            current_type = nullable->get_nested_type().get();
333
2.73k
        }
334
2.73k
    }
335
93.7k
    return last_array ? last_array->get_nested_type() : type;
336
93.7k
}
337
338
59.5k
Status cast_column(const ColumnWithTypeAndName& arg, const DataTypePtr& type, ColumnPtr* result) {
339
59.5k
    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
59.5k
    if (type->get_primitive_type() == TYPE_VARIANT) {
346
        // If source column is variant, so the nullable info is different from dst column
347
6.81k
        if (arg.type->get_primitive_type() == TYPE_VARIANT) {
348
49
            *result = type->is_nullable() ? make_nullable(arg.column) : remove_nullable(arg.column);
349
49
            return Status::OK();
350
49
        }
351
        // set variant root column/type to from column/type
352
6.81k
        CHECK(arg.column->is_nullable());
353
6.76k
        auto to_type = remove_nullable(type);
354
6.76k
        const auto& data_type_object = assert_cast<const DataTypeVariant&>(*to_type);
355
6.76k
        auto variant = ColumnVariant::create(data_type_object.variant_max_subcolumns_count(),
356
6.76k
                                             data_type_object.enable_doc_mode());
357
358
6.76k
        variant->create_root(arg.type, std::move(*arg.column).mutate());
359
6.76k
        ColumnPtr nullable = ColumnNullable::create(
360
6.76k
                variant->get_ptr(),
361
6.76k
                assert_cast<const ColumnNullable*>(arg.column.get())->get_null_map_column_ptr());
362
6.76k
        *result = type->is_nullable() ? nullable : variant->get_ptr();
363
6.76k
        return Status::OK();
364
6.81k
    }
365
366
52.7k
    auto function = SimpleFunctionFactory::instance().get_function("CAST", arguments, type);
367
52.7k
    if (!function) {
368
0
        return Status::InternalError("Not found cast function {} to {}", arg.type->get_name(),
369
0
                                     type->get_name());
370
0
    }
371
52.7k
    Block tmp_block {arguments};
372
52.7k
    uint32_t result_column = cast_set<uint32_t>(tmp_block.columns());
373
52.7k
    RuntimeState state;
374
52.7k
    auto ctx = FunctionContext::create_context(&state, {}, {});
375
376
52.7k
    if (arg.type->get_primitive_type() == INVALID_TYPE) {
377
        // cast from nothing to any type should result in nulls
378
2.06k
        *result = type->create_column_const_with_default_value(arg.column->size())
379
2.06k
                          ->convert_to_full_column_if_const();
380
2.06k
        return Status::OK();
381
2.06k
    }
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
50.7k
    ctx->set_string_as_jsonb_string(true);
386
50.7k
    ctx->set_jsonb_string_as_string(true);
387
50.7k
    tmp_block.insert({nullptr, type, arg.name});
388
    // TODO(lihangyu): we should handle this error in strict mode
389
50.7k
    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
50.7k
    *result = tmp_block.get_by_position(result_column).column->convert_to_full_column_if_const();
397
50.7k
    VLOG_DEBUG << fmt::format("{} before convert {}, after convert {}", arg.name,
398
1
                              arg.column->get_name(), (*result)->get_name());
399
50.7k
    return Status::OK();
400
50.7k
}
401
402
void get_column_by_type(const DataTypePtr& data_type, const std::string& name, TabletColumn& column,
403
187k
                        const ExtraInfo& ext_info) {
404
187k
    column.set_name(name);
405
187k
    column.set_type(data_type->get_storage_field_type());
406
187k
    if (ext_info.unique_id >= 0) {
407
4
        column.set_unique_id(ext_info.unique_id);
408
4
    }
409
187k
    if (ext_info.parent_unique_id >= 0) {
410
91.5k
        column.set_parent_unique_id(ext_info.parent_unique_id);
411
91.5k
    }
412
187k
    if (!ext_info.path_info.empty()) {
413
91.5k
        column.set_path_info(ext_info.path_info);
414
91.5k
    }
415
187k
    if (data_type->is_nullable()) {
416
94.0k
        const auto& real_type = static_cast<const DataTypeNullable&>(*data_type);
417
94.0k
        column.set_is_nullable(true);
418
94.0k
        get_column_by_type(real_type.get_nested_type(), name, column, {});
419
94.0k
        return;
420
94.0k
    }
421
93.9k
    if (data_type->get_primitive_type() == PrimitiveType::TYPE_ARRAY) {
422
2.43k
        TabletColumn child;
423
2.43k
        get_column_by_type(assert_cast<const DataTypeArray*>(data_type.get())->get_nested_type(),
424
2.43k
                           "", child, {});
425
2.43k
        column.set_length(TabletColumn::get_field_length_by_type(TPrimitiveType::ARRAY, 0));
426
2.43k
        column.add_sub_column(child);
427
2.43k
        return;
428
2.43k
    }
429
91.5k
    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
91.5k
    if (is_string_type(data_type->get_primitive_type()) ||
437
91.5k
        data_type->get_primitive_type() == TYPE_JSONB) {
438
29.2k
        column.set_length(INT_MAX);
439
29.2k
        return;
440
29.2k
    }
441
442
62.2k
    PrimitiveType type = data_type->get_primitive_type();
443
62.2k
    if (is_int_or_bool(type) || is_string_type(type) || is_float_or_double(type) || is_ip(type) ||
444
62.2k
        is_date_or_datetime(type) || type == PrimitiveType::TYPE_DATEV2) {
445
62.2k
        column.set_length(cast_set<int32_t>(data_type->get_size_of_value_in_memory()));
446
62.2k
        return;
447
62.2k
    }
448
18.4E
    if (is_decimal(type)) {
449
1
        column.set_precision(data_type->get_precision());
450
1
        column.set_frac(data_type->get_scale());
451
1
        return;
452
1
    }
453
    // datetimev2 needs scale
454
18.4E
    if (type == PrimitiveType::TYPE_DATETIMEV2 || type == PrimitiveType::TYPE_TIMESTAMPTZ) {
455
1
        column.set_precision(-1);
456
1
        column.set_frac(data_type->get_scale());
457
1
        return;
458
1
    }
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
91.5k
                                const ExtraInfo& ext_info) {
467
91.5k
    TabletColumn result;
468
91.5k
    get_column_by_type(data_type, name, result, ext_info);
469
91.5k
    return result;
470
91.5k
}
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.00k
                                                 const PathInData::Parts& rhs) {
475
9.00k
    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
35.9k
    for (size_t i = 0; i < lhs.size(); ++i) {
481
26.9k
        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
26.9k
    }
489
8.99k
    return false;
490
8.99k
}
491
492
3.05k
Status check_variant_has_no_ambiguous_paths(const PathsInData& tuple_paths) {
493
    // Group paths by their string representation to reduce comparisons
494
3.05k
    std::unordered_map<std::string, std::vector<size_t>> path_groups;
495
496
24.1k
    for (size_t i = 0; i < tuple_paths.size(); ++i) {
497
        // same path should have same structure, so we group them by path
498
21.0k
        path_groups[tuple_paths[i].get_path()].push_back(i);
499
        // print part of tuple_paths[i]
500
21.0k
        VLOG_DEBUG << "tuple_paths[i]: " << tuple_paths[i].get_path();
501
21.0k
    }
502
503
    // Only compare paths within the same group
504
12.0k
    for (const auto& [path_str, indices] : path_groups) {
505
12.0k
        if (indices.size() <= 1) {
506
3.05k
            continue; // No conflicts possible
507
3.05k
        }
508
509
        // Compare all pairs within this group
510
26.9k
        for (size_t i = 0; i < indices.size(); ++i) {
511
26.9k
            for (size_t j = 0; j < i; ++j) {
512
9.00k
                if (has_different_structure_in_same_path(tuple_paths[indices[i]].get_parts(),
513
9.00k
                                                         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.00k
            }
521
18.0k
        }
522
9.00k
    }
523
3.04k
    return Status::OK();
524
3.05k
}
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
23
                                    std::set<PathInData>* path_set) {
530
23
    PathsInData tuple_paths;
531
23
    DataTypes tuple_types;
532
23
    CHECK(common_schema.use_count() == 1);
533
    // Get the least common type for all paths.
534
23
    for (const auto& [key, subtypes] : subcolumns_types) {
535
7
        assert(!subtypes.empty());
536
7
        if (key.get_path() == ColumnVariant::COLUMN_NAME_DUMMY) {
537
0
            continue;
538
0
        }
539
7
        size_t first_dim = get_number_of_dimensions(*subtypes[0]);
540
7
        tuple_paths.emplace_back(key);
541
10
        for (size_t i = 1; i < subtypes.size(); ++i) {
542
4
            if (first_dim != get_number_of_dimensions(*subtypes[i])) {
543
1
                tuple_types.emplace_back(make_nullable(std::make_shared<DataTypeJsonb>()));
544
1
                LOG(INFO) << fmt::format(
545
1
                        "Uncompatible types of subcolumn '{}': {} and {}, cast to JSONB",
546
1
                        key.get_path(), subtypes[0]->get_name(), subtypes[i]->get_name());
547
1
                break;
548
1
            }
549
4
        }
550
7
        if (tuple_paths.size() == tuple_types.size()) {
551
1
            continue;
552
1
        }
553
6
        DataTypePtr common_type;
554
6
        get_least_supertype_jsonb(subtypes, &common_type);
555
6
        if (!common_type->is_nullable()) {
556
3
            common_type = make_nullable(common_type);
557
3
        }
558
6
        tuple_types.emplace_back(common_type);
559
6
    }
560
23
    CHECK_EQ(tuple_paths.size(), tuple_types.size());
561
562
    // Append all common type columns of this variant
563
30
    for (int i = 0; i < tuple_paths.size(); ++i) {
564
7
        TabletColumn common_column;
565
        // typed path not contains root part
566
7
        auto path_without_root = tuple_paths[i].copy_pop_front().get_path();
567
7
        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
7
        } else {
574
            // const std::string& column_name = variant_col_name + "." + tuple_paths[i].get_path();
575
7
            get_column_by_type(tuple_types[i], tuple_paths[i].get_path(), common_column,
576
7
                               ExtraInfo {.unique_id = -1,
577
7
                                          .parent_unique_id = variant_col_unique_id,
578
7
                                          .path_info = tuple_paths[i]});
579
7
        }
580
7
        common_schema->append_column(common_column);
581
7
        if (path_set != nullptr) {
582
4
            path_set->insert(tuple_paths[i]);
583
4
        }
584
7
    }
585
23
    return Status::OK();
586
23
}
587
588
Status update_least_common_schema(const std::vector<TabletSchemaSPtr>& schemas,
589
                                  TabletSchemaSPtr& common_schema, int32_t variant_col_unique_id,
590
22
                                  std::set<PathInData>* path_set) {
591
22
    std::map<std::string, TabletColumnPtr> typed_columns;
592
22
    for (const TabletColumnPtr& col :
593
22
         common_schema->column_by_uid(variant_col_unique_id).get_sub_columns()) {
594
2
        typed_columns[col->name()] = col;
595
2
    }
596
    // Types of subcolumns by path from all tuples.
597
22
    std::map<PathInData, DataTypes> subcolumns_types;
598
599
    // Collect all paths first to enable batch checking
600
22
    std::vector<PathInData> all_paths;
601
602
42
    for (const TabletSchemaSPtr& schema : schemas) {
603
356
        for (const TabletColumnPtr& col : schema->columns()) {
604
            // Get subcolumns of this variant
605
356
            if (col->has_path_info() && col->parent_unique_id() >= 0 &&
606
356
                col->parent_unique_id() == variant_col_unique_id) {
607
6
                subcolumns_types[*col->path_info_ptr()].emplace_back(
608
6
                        DataTypeFactory::instance().create_data_type(*col, col->is_nullable()));
609
6
                all_paths.push_back(*col->path_info_ptr());
610
6
            }
611
356
        }
612
42
    }
613
614
    // Batch check for conflicts
615
22
    RETURN_IF_ERROR(check_variant_has_no_ambiguous_paths(all_paths));
616
617
22
    return update_least_schema_internal(subcolumns_types, common_schema, variant_col_unique_id,
618
22
                                        typed_columns, path_set);
619
22
}
620
621
// Keep variant subcolumn BF support aligned with FE DDL checks.
622
97.7k
bool is_bf_supported_by_fe_for_variant_subcolumn(FieldType type) {
623
97.7k
    switch (type) {
624
80
    case FieldType::OLAP_FIELD_TYPE_SMALLINT:
625
193
    case FieldType::OLAP_FIELD_TYPE_INT:
626
55.0k
    case FieldType::OLAP_FIELD_TYPE_BIGINT:
627
55.1k
    case FieldType::OLAP_FIELD_TYPE_LARGEINT:
628
55.1k
    case FieldType::OLAP_FIELD_TYPE_CHAR:
629
55.1k
    case FieldType::OLAP_FIELD_TYPE_VARCHAR:
630
83.6k
    case FieldType::OLAP_FIELD_TYPE_STRING:
631
83.6k
    case FieldType::OLAP_FIELD_TYPE_DATE:
632
83.6k
    case FieldType::OLAP_FIELD_TYPE_DATETIME:
633
83.7k
    case FieldType::OLAP_FIELD_TYPE_DATEV2:
634
83.9k
    case FieldType::OLAP_FIELD_TYPE_DATETIMEV2:
635
83.9k
    case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ:
636
83.9k
    case FieldType::OLAP_FIELD_TYPE_DECIMAL:
637
84.0k
    case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
638
84.1k
    case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
639
84.3k
    case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
640
84.4k
    case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
641
84.6k
    case FieldType::OLAP_FIELD_TYPE_IPV4:
642
84.7k
    case FieldType::OLAP_FIELD_TYPE_IPV6:
643
84.7k
        return true;
644
13.0k
    default:
645
13.0k
        return false;
646
97.7k
    }
647
97.7k
}
648
649
void inherit_column_attributes(const TabletColumn& source, TabletColumn& target,
650
97.7k
                               TabletSchemaSPtr* target_schema) {
651
97.7k
    if (!target.is_extracted_column()) {
652
0
        return;
653
0
    }
654
97.7k
    target.set_aggregation_method(source.aggregation());
655
656
    // 1. bloom filter
657
97.7k
    if (is_bf_supported_by_fe_for_variant_subcolumn(target.type())) {
658
84.6k
        target.set_is_bf_column(source.is_bf_column());
659
84.6k
    }
660
661
97.7k
    if (!target_schema) {
662
94.1k
        return;
663
94.1k
    }
664
665
    // 2. inverted index
666
3.55k
    TabletIndexes indexes_to_add;
667
3.55k
    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
3.60k
    if (target.is_variant_type()) {
671
3.60k
        for (auto& index : source_indexes) {
672
164
            auto index_info = std::make_shared<TabletIndex>(*index);
673
164
            index_info->set_escaped_escaped_index_suffix_path(target.path_info_ptr()->get_path());
674
164
            indexes_to_add.emplace_back(std::move(index_info));
675
164
        }
676
18.4E
    } else {
677
18.4E
        inherit_index(source_indexes, indexes_to_add, target);
678
18.4E
    }
679
3.55k
    auto target_indexes = (*target_schema)
680
3.55k
                                  ->inverted_indexs(target.parent_unique_id(),
681
3.55k
                                                    target.path_info_ptr()->get_path());
682
3.61k
    if (target_indexes.empty()) {
683
3.61k
        for (auto& index_info : indexes_to_add) {
684
172
            (*target_schema)->append_index(std::move(*index_info));
685
172
        }
686
3.61k
    }
687
688
    // 3. TODO: gnragm bf index
689
3.55k
}
690
691
2.54k
void inherit_column_attributes(TabletSchemaSPtr& schema) {
692
    // Add index meta if extracted column is missing index meta
693
57.7k
    for (size_t i = 0; i < schema->num_columns(); ++i) {
694
55.2k
        TabletColumn& col = schema->mutable_column(i);
695
55.2k
        if (!col.is_extracted_column()) {
696
51.6k
            continue;
697
51.6k
        }
698
3.60k
        if (schema->field_index(col.parent_unique_id()) == -1) {
699
            // parent column is missing, maybe dropped
700
0
            continue;
701
0
        }
702
3.60k
        inherit_column_attributes(schema->column_by_uid(col.parent_unique_id()), col, &schema);
703
3.60k
    }
704
2.54k
}
705
706
Status get_least_common_schema(const std::vector<TabletSchemaSPtr>& schemas,
707
                               const TabletSchemaSPtr& base_schema, TabletSchemaSPtr& output_schema,
708
19
                               bool check_schema_size) {
709
19
    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
19
    auto build_schema_without_extracted_columns = [&](const TabletSchemaSPtr& base_schema) {
714
19
        output_schema = std::make_shared<TabletSchema>();
715
        // not copy columns but only shadow copy other attributes
716
19
        output_schema->shawdow_copy_without_columns(*base_schema);
717
        // Get all columns without extracted columns and collect variant col unique id
718
177
        for (const TabletColumnPtr& col : base_schema->columns()) {
719
177
            if (col->is_variant_type()) {
720
19
                variant_column_unique_id.push_back(col->unique_id());
721
19
            }
722
177
            if (!col->is_extracted_column()) {
723
174
                output_schema->append_column(*col);
724
174
            }
725
177
        }
726
19
    };
727
19
    if (base_schema == nullptr) {
728
        // Pick tablet schema with max schema version
729
19
        auto max_version_schema =
730
19
                *std::max_element(schemas.cbegin(), schemas.cend(),
731
19
                                  [](const TabletSchemaSPtr a, const TabletSchemaSPtr b) {
732
17
                                      return a->schema_version() < b->schema_version();
733
17
                                  });
734
19
        CHECK(max_version_schema);
735
19
        build_schema_without_extracted_columns(max_version_schema);
736
19
    } else {
737
        // use input base_schema schema as base schema
738
0
        build_schema_without_extracted_columns(base_schema);
739
0
    }
740
741
19
    for (int32_t unique_id : variant_column_unique_id) {
742
19
        std::set<PathInData> path_set;
743
19
        RETURN_IF_ERROR(update_least_common_schema(schemas, output_schema, unique_id, &path_set));
744
19
    }
745
746
19
    inherit_column_attributes(output_schema);
747
19
    if (check_schema_size &&
748
19
        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
19
    return Status::OK();
754
19
}
755
756
// sort by paths in lexicographical order
757
773
ColumnVariant::Subcolumns get_sorted_subcolumns(const ColumnVariant::Subcolumns& subcolumns) {
758
    // sort by paths in lexicographical order
759
773
    ColumnVariant::Subcolumns sorted = subcolumns;
760
83.5k
    std::sort(sorted.begin(), sorted.end(), [](const auto& lhsItem, const auto& rhsItem) {
761
83.5k
        return lhsItem->path < rhsItem->path;
762
83.5k
    });
763
773
    return sorted;
764
773
}
765
766
bool has_schema_index_diff(const TabletSchema* new_schema, const TabletSchema* old_schema,
767
3.38k
                           int32_t new_col_idx, int32_t old_col_idx) {
768
3.38k
    const auto& column_new = new_schema->column(new_col_idx);
769
3.38k
    const auto& column_old = old_schema->column(old_col_idx);
770
771
3.38k
    if (column_new.is_bf_column() != column_old.is_bf_column()) {
772
52
        return true;
773
52
    }
774
775
3.33k
    auto new_schema_inverted_indexs = new_schema->inverted_indexs(column_new);
776
3.33k
    auto old_schema_inverted_indexs = old_schema->inverted_indexs(column_old);
777
778
3.33k
    if (new_schema_inverted_indexs.size() != old_schema_inverted_indexs.size()) {
779
61
        return true;
780
61
    }
781
782
3.29k
    for (size_t i = 0; i < new_schema_inverted_indexs.size(); ++i) {
783
22
        if (!new_schema_inverted_indexs[i]->is_same_except_id(old_schema_inverted_indexs[i])) {
784
0
            return true;
785
0
        }
786
22
    }
787
788
3.27k
    return false;
789
3.27k
}
790
791
1.22k
TabletColumn create_sparse_column(const TabletColumn& variant) {
792
1.22k
    TabletColumn res;
793
1.22k
    res.set_name(variant.name_lower_case() + "." + SPARSE_COLUMN_PATH);
794
1.22k
    res.set_type(FieldType::OLAP_FIELD_TYPE_MAP);
795
1.22k
    res.set_aggregation_method(variant.aggregation());
796
1.22k
    res.set_path_info(PathInData {variant.name_lower_case() + "." + SPARSE_COLUMN_PATH});
797
1.22k
    res.set_parent_unique_id(variant.unique_id());
798
    // set default value to "NULL" DefaultColumnIterator will call insert_many_defaults
799
1.22k
    res.set_default_value("NULL");
800
1.22k
    TabletColumn child_tcolumn;
801
1.22k
    child_tcolumn.set_type(FieldType::OLAP_FIELD_TYPE_STRING);
802
1.22k
    res.add_sub_column(child_tcolumn);
803
1.22k
    res.add_sub_column(child_tcolumn);
804
1.22k
    return res;
805
1.22k
}
806
807
4.28k
TabletColumn create_sparse_shard_column(const TabletColumn& variant, int bucket_index) {
808
4.28k
    TabletColumn res;
809
4.28k
    std::string name = variant.name_lower_case() + "." + SPARSE_COLUMN_PATH + ".b" +
810
4.28k
                       std::to_string(bucket_index);
811
4.28k
    res.set_name(name);
812
4.28k
    res.set_type(FieldType::OLAP_FIELD_TYPE_MAP);
813
4.28k
    res.set_aggregation_method(variant.aggregation());
814
4.28k
    res.set_parent_unique_id(variant.unique_id());
815
4.28k
    res.set_default_value("NULL");
816
4.28k
    PathInData path(name);
817
4.28k
    res.set_path_info(path);
818
4.28k
    TabletColumn child_tcolumn;
819
4.28k
    child_tcolumn.set_type(FieldType::OLAP_FIELD_TYPE_STRING);
820
4.28k
    res.add_sub_column(child_tcolumn);
821
4.28k
    res.add_sub_column(child_tcolumn);
822
4.28k
    return res;
823
4.28k
}
824
825
3.69k
TabletColumn create_doc_value_column(const TabletColumn& variant, int bucket_index) {
826
3.69k
    TabletColumn res;
827
3.69k
    std::string name = variant.name_lower_case() + "." + DOC_VALUE_COLUMN_PATH + ".b" +
828
3.69k
                       std::to_string(bucket_index);
829
3.69k
    res.set_name(name);
830
3.69k
    res.set_type(FieldType::OLAP_FIELD_TYPE_MAP);
831
3.69k
    res.set_aggregation_method(variant.aggregation());
832
3.69k
    res.set_parent_unique_id(variant.unique_id());
833
3.69k
    res.set_default_value("NULL");
834
3.69k
    res.set_path_info(PathInData {name});
835
836
3.69k
    TabletColumn child_tcolumn;
837
3.69k
    child_tcolumn.set_type(FieldType::OLAP_FIELD_TYPE_STRING);
838
3.69k
    res.add_sub_column(child_tcolumn);
839
3.69k
    res.add_sub_column(child_tcolumn);
840
3.69k
    return res;
841
3.69k
}
842
843
167k
uint32_t variant_binary_shard_of(const StringRef& path, uint32_t bucket_num) {
844
167k
    if (bucket_num <= 1) return 0;
845
164k
    SipHash hash;
846
164k
    hash.update(path.data, path.size);
847
164k
    uint64_t h = hash.get64();
848
164k
    return static_cast<uint32_t>(h % bucket_num);
849
167k
}
850
851
Status VariantCompactionUtil::aggregate_path_to_stats(
852
        const RowsetSharedPtr& rs,
853
1.65k
        std::unordered_map<int32_t, PathToNoneNullValues>* uid_to_path_stats) {
854
1.65k
    SegmentCacheHandle segment_cache;
855
1.65k
    RETURN_IF_ERROR(SegmentLoader::instance()->load_segments(
856
1.65k
            std::static_pointer_cast<BetaRowset>(rs), &segment_cache));
857
858
4.51k
    for (const auto& column : rs->tablet_schema()->columns()) {
859
4.51k
        if (!column->is_variant_type() || column->unique_id() < 0) {
860
2.84k
            continue;
861
2.84k
        }
862
1.67k
        if (!should_check_variant_path_stats(*column)) {
863
0
            continue;
864
0
        }
865
1.67k
        for (const auto& segment : segment_cache.get_segments()) {
866
775
            std::shared_ptr<ColumnReader> column_reader;
867
775
            OlapReaderStatistics stats;
868
775
            RETURN_IF_ERROR(
869
775
                    segment->get_column_reader(column->unique_id(), &column_reader, &stats));
870
775
            if (!column_reader) {
871
0
                continue;
872
0
            }
873
874
775
            CHECK(column_reader->get_meta_type() == FieldType::OLAP_FIELD_TYPE_VARIANT);
875
775
            auto* variant_column_reader =
876
775
                    assert_cast<segment_v2::VariantColumnReader*>(column_reader.get());
877
            // load external meta before getting stats
878
775
            RETURN_IF_ERROR(variant_column_reader->load_external_meta_once());
879
775
            const auto* source_stats = variant_column_reader->get_stats();
880
775
            CHECK(source_stats);
881
882
            // agg path -> stats
883
2.12k
            for (const auto& [path, size] : source_stats->sparse_column_non_null_size) {
884
2.12k
                (*uid_to_path_stats)[column->unique_id()][path] += size;
885
2.12k
            }
886
887
2.04k
            for (const auto& [path, size] : source_stats->subcolumns_non_null_size) {
888
2.04k
                (*uid_to_path_stats)[column->unique_id()][path] += size;
889
2.04k
            }
890
775
        }
891
1.67k
    }
892
1.65k
    return Status::OK();
893
1.65k
}
894
895
Status VariantCompactionUtil::aggregate_variant_extended_info(
896
        const RowsetSharedPtr& rs,
897
1.75k
        std::unordered_map<int32_t, VariantExtendedInfo>* uid_to_variant_extended_info) {
898
1.75k
    SegmentCacheHandle segment_cache;
899
1.75k
    RETURN_IF_ERROR(SegmentLoader::instance()->load_segments(
900
1.75k
            std::static_pointer_cast<BetaRowset>(rs), &segment_cache));
901
902
5.34k
    for (const auto& column : rs->tablet_schema()->columns()) {
903
5.34k
        if (!column->is_variant_type()) {
904
3.59k
            continue;
905
3.59k
        }
906
1.75k
        auto& extended_info = (*uid_to_variant_extended_info)[column->unique_id()];
907
1.75k
        if (column->variant_enable_nested_group()) {
908
0
            extended_info.has_nested_group = true;
909
0
        }
910
1.75k
        for (const auto& segment : segment_cache.get_segments()) {
911
661
            std::shared_ptr<ColumnReader> column_reader;
912
661
            OlapReaderStatistics stats;
913
661
            RETURN_IF_ERROR(
914
661
                    segment->get_column_reader(column->unique_id(), &column_reader, &stats));
915
661
            if (!column_reader) {
916
0
                continue;
917
0
            }
918
919
661
            CHECK(column_reader->get_meta_type() == FieldType::OLAP_FIELD_TYPE_VARIANT);
920
661
            auto* variant_column_reader =
921
661
                    assert_cast<segment_v2::VariantColumnReader*>(column_reader.get());
922
            // load external meta before getting stats
923
661
            RETURN_IF_ERROR(variant_column_reader->load_external_meta_once());
924
661
            const auto* source_stats = variant_column_reader->get_stats();
925
661
            CHECK(source_stats);
926
927
661
            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
870
                for (const auto& [path, size] : source_stats->sparse_column_non_null_size) {
931
870
                    extended_info.path_to_none_null_values[path] += size;
932
870
                    extended_info.sparse_paths.emplace(path);
933
870
                }
934
935
1.42k
                for (const auto& [path, size] : source_stats->subcolumns_non_null_size) {
936
1.42k
                    extended_info.path_to_none_null_values[path] += size;
937
1.42k
                }
938
661
            }
939
940
            //2. agg path -> schema
941
661
            variant_column_reader->get_subcolumns_types(&extended_info.path_to_data_types);
942
943
            // 3. extract typed paths
944
661
            variant_column_reader->get_typed_paths(&extended_info.typed_paths);
945
946
            // 4. extract nested paths
947
661
            if (!column->variant_enable_nested_group()) {
948
661
                variant_column_reader->get_nested_paths(&extended_info.nested_paths);
949
661
            }
950
661
        }
951
1.75k
    }
952
1.75k
    return Status::OK();
953
1.75k
}
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
167
                                         TabletSchema::PathsSetInfo& paths_set_info) {
959
    // max_subcolumns_count is 0 means no limit
960
167
    if (max_subcolumns_count > 0 && stats.size() > max_subcolumns_count) {
961
42
        std::vector<std::pair<size_t, std::string_view>> paths_with_sizes;
962
42
        paths_with_sizes.reserve(stats.size());
963
653
        for (const auto& [path, size] : stats) {
964
653
            paths_with_sizes.emplace_back(size, path);
965
653
        }
966
42
        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
653
        for (const auto& [size, path] : paths_with_sizes) {
970
653
            if (paths_set_info.sub_path_set.size() < max_subcolumns_count) {
971
184
                paths_set_info.sub_path_set.emplace(path);
972
469
            } else {
973
469
                paths_set_info.sparse_path_set.emplace(path);
974
469
            }
975
653
        }
976
42
        LOG(INFO) << "subpaths " << paths_set_info.sub_path_set.size() << " sparse paths "
977
42
                  << paths_set_info.sparse_path_set.size() << " variant max subcolumns count "
978
42
                  << max_subcolumns_count << " stats size " << paths_with_sizes.size();
979
125
    } else {
980
        // Apply all paths as subcolumns
981
134
        for (const auto& [path, _] : stats) {
982
134
            paths_set_info.sub_path_set.emplace(path);
983
134
        }
984
125
    }
985
167
}
986
987
Status VariantCompactionUtil::check_path_stats(const std::vector<RowsetSharedPtr>& intputs,
988
2.77k
                                               RowsetSharedPtr output, BaseTabletSPtr tablet) {
989
2.77k
    if (output->tablet_schema()->num_variant_columns() == 0) {
990
2.52k
        return Status::OK();
991
2.52k
    }
992
1.76k
    for (const auto& rowset : intputs) {
993
5.40k
        for (const auto& column : rowset->tablet_schema()->columns()) {
994
5.40k
            if (column->is_variant_type() && !should_check_variant_path_stats(*column)) {
995
0
                return Status::OK();
996
0
            }
997
5.40k
        }
998
1.76k
    }
999
    // check no extended schema in input rowsets
1000
1.76k
    for (const auto& rowset : intputs) {
1001
5.39k
        for (const auto& column : rowset->tablet_schema()->columns()) {
1002
5.39k
            if (column->is_extracted_column()) {
1003
0
                return Status::OK();
1004
0
            }
1005
5.39k
        }
1006
1.76k
    }
1007
245
#ifndef BE_TEST
1008
    // check no extended schema in output rowset
1009
784
    for (const auto& column : output->tablet_schema()->columns()) {
1010
784
        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
784
    }
1021
245
#endif
1022
    // only check path stats for dup_keys since the rows may be merged in other models
1023
245
    if (tablet->keys_type() != KeysType::DUP_KEYS) {
1024
45
        return Status::OK();
1025
45
    }
1026
    // if there is a delete predicate in the input rowsets, we skip the path stats check
1027
1.44k
    for (auto& rowset : intputs) {
1028
1.44k
        if (rowset->rowset_meta()->has_delete_predicate()) {
1029
0
            return Status::OK();
1030
0
        }
1031
1.44k
    }
1032
562
    for (const auto& column : output->tablet_schema()->columns()) {
1033
562
        if (column->is_variant_type() && !should_check_variant_path_stats(*column)) {
1034
0
            return Status::OK();
1035
0
        }
1036
562
    }
1037
200
    std::unordered_map<int32_t, PathToNoneNullValues> original_uid_to_path_stats;
1038
1.43k
    for (const auto& rs : intputs) {
1039
1.43k
        RETURN_IF_ERROR(aggregate_path_to_stats(rs, &original_uid_to_path_stats));
1040
1.43k
    }
1041
200
    std::unordered_map<int32_t, PathToNoneNullValues> output_uid_to_path_stats;
1042
200
    RETURN_IF_ERROR(aggregate_path_to_stats(output, &output_uid_to_path_stats));
1043
200
    for (const auto& [uid, stats] : output_uid_to_path_stats) {
1044
79
        if (output->tablet_schema()->column_by_uid(uid).is_variant_type() &&
1045
79
            output->tablet_schema()->column_by_uid(uid).variant_enable_doc_mode()) {
1046
21
            continue;
1047
21
        }
1048
58
        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
58
        if (stats.size() > output->tablet_schema()
1056
58
                                   ->column_by_uid(uid)
1057
58
                                   .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
0
            if (output->num_segments() == 1) {
1060
0
                for (const auto& [path, size] : stats) {
1061
0
                    if (original_uid_to_path_stats.at(uid).find(path) ==
1062
0
                        original_uid_to_path_stats.at(uid).end()) {
1063
0
                        continue;
1064
0
                    }
1065
0
                    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
0
                }
1075
0
            }
1076
0
        }
1077
        // in this case, input stats is accurate, so we check the stats size and stats value
1078
58
        else {
1079
693
            for (const auto& [path, size] : stats) {
1080
693
                if (original_uid_to_path_stats.at(uid).find(path) ==
1081
693
                    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
693
                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
693
            }
1095
58
        }
1096
58
    }
1097
1098
200
    return Status::OK();
1099
200
}
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
164
        TabletSchema::PathsSetInfo& paths_set_info) {
1105
164
    if (parent_column->variant_enable_typed_paths_to_sparse()) {
1106
0
        return Status::OK();
1107
0
    }
1108
270
    for (const auto& path : typed_paths) {
1109
270
        TabletSchema::SubColumnInfo sub_column_info;
1110
270
        if (generate_sub_column_info(*target, parent_column->unique_id(), path, &sub_column_info)) {
1111
269
            inherit_column_attributes(*parent_column, sub_column_info.column);
1112
269
            output_schema->append_column(sub_column_info.column);
1113
269
            paths_set_info.typed_path_set.insert({path, std::move(sub_column_info)});
1114
269
            VLOG_DEBUG << "append typed column " << path;
1115
269
        } else {
1116
1
            return Status::InternalError("Failed to generate sub column info for path {}", path);
1117
1
        }
1118
270
    }
1119
163
    return Status::OK();
1120
164
}
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
162
        TabletSchemaSPtr& output_schema, TabletSchema::PathsSetInfo& paths_set_info) {
1126
162
    const auto& parent_indexes = output_schema->inverted_indexs(parent_column->unique_id());
1127
162
    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
161
    return Status::OK();
1153
162
}
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
118
        const std::unordered_set<std::string>& sparse_paths, TabletSchemaSPtr& output_schema) {
1159
118
    auto& path_set = paths_set_info.sub_path_set;
1160
118
    std::vector<StringRef> sorted_subpaths(path_set.begin(), path_set.end());
1161
118
    std::sort(sorted_subpaths.begin(), sorted_subpaths.end());
1162
118
    const auto& parent_indexes = target->inverted_indexs(parent_column->unique_id());
1163
    // append subcolumns
1164
284
    for (const auto& subpath : sorted_subpaths) {
1165
284
        auto column_name = parent_column->name_lower_case() + "." + subpath.to_string();
1166
284
        auto column_path = PathInData(column_name);
1167
1168
284
        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
284
        TabletSchema::SubColumnInfo sub_column_info;
1175
284
        if (parent_column->variant_enable_typed_paths_to_sparse() &&
1176
284
            generate_sub_column_info(*target, parent_column->unique_id(), std::string(subpath),
1177
16
                                     &sub_column_info)) {
1178
8
            inherit_column_attributes(*parent_column, sub_column_info.column);
1179
8
            output_schema->append_column(sub_column_info.column);
1180
8
            paths_set_info.subcolumn_indexes.emplace(subpath, std::move(sub_column_info.indexes));
1181
8
            VLOG_DEBUG << "append typed column " << subpath;
1182
276
        } else if (find_data_types == path_to_data_types.end() || find_data_types->second.empty() ||
1183
276
                   sparse_paths.find(std::string(subpath)) != sparse_paths.end() ||
1184
276
                   sparse_paths.size() >=
1185
165
                           parent_column->variant_max_sparse_column_statistics_size()) {
1186
115
            TabletColumn subcolumn;
1187
115
            subcolumn.set_name(column_name);
1188
115
            subcolumn.set_type(FieldType::OLAP_FIELD_TYPE_VARIANT);
1189
115
            subcolumn.set_parent_unique_id(parent_column->unique_id());
1190
115
            subcolumn.set_path_info(column_path);
1191
115
            subcolumn.set_aggregation_method(parent_column->aggregation());
1192
115
            subcolumn.set_variant_max_subcolumns_count(
1193
115
                    parent_column->variant_max_subcolumns_count());
1194
115
            subcolumn.set_variant_enable_doc_mode(parent_column->variant_enable_doc_mode());
1195
115
            subcolumn.set_is_nullable(true);
1196
115
            output_schema->append_column(subcolumn);
1197
115
            VLOG_DEBUG << "append sub column " << subpath << " data type "
1198
0
                       << "VARIANT";
1199
115
        }
1200
        // normal case: the subcolumn type can be calculated from the data types in segments
1201
161
        else {
1202
161
            DataTypePtr data_type;
1203
161
            get_least_supertype_jsonb(find_data_types->second, &data_type);
1204
161
            TabletColumn sub_column =
1205
161
                    get_column_by_type(data_type, column_name,
1206
161
                                       ExtraInfo {.unique_id = -1,
1207
161
                                                  .parent_unique_id = parent_column->unique_id(),
1208
161
                                                  .path_info = column_path});
1209
161
            inherit_column_attributes(*parent_column, sub_column);
1210
161
            TabletIndexes sub_column_indexes;
1211
161
            inherit_index(parent_indexes, sub_column_indexes, sub_column);
1212
161
            paths_set_info.subcolumn_indexes.emplace(subpath, std::move(sub_column_indexes));
1213
161
            output_schema->append_column(sub_column);
1214
161
            VLOG_DEBUG << "append sub column " << subpath << " data type " << data_type->get_name();
1215
161
        }
1216
284
    }
1217
118
}
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
53
        TabletSchemaSPtr& output_schema) {
1223
53
    const auto& parent_indexes = target->inverted_indexs(parent_column->unique_id());
1224
65
    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
65
        if (data_types.empty() || path.empty() || path.get_is_typed() || path.has_nested_part()) {
1228
21
            continue;
1229
21
        }
1230
44
        DataTypePtr data_type;
1231
44
        get_least_supertype_jsonb(data_types, &data_type);
1232
44
        auto column_name = parent_column->name_lower_case() + "." + path.get_path();
1233
44
        auto column_path = PathInData(column_name);
1234
44
        TabletColumn sub_column =
1235
44
                get_column_by_type(data_type, column_name,
1236
44
                                   ExtraInfo {.unique_id = -1,
1237
44
                                              .parent_unique_id = parent_column->unique_id(),
1238
44
                                              .path_info = column_path});
1239
44
        inherit_column_attributes(*parent_column, sub_column);
1240
44
        TabletIndexes sub_column_indexes;
1241
44
        inherit_index(parent_indexes, sub_column_indexes, sub_column);
1242
44
        paths_set_info.sub_path_set.emplace(path.get_path());
1243
44
        paths_set_info.subcolumn_indexes.emplace(path.get_path(), std::move(sub_column_indexes));
1244
44
        output_schema->append_column(sub_column);
1245
44
        VLOG_DEBUG << "append sub column " << path.get_path() << " data type "
1246
0
                   << data_type->get_name();
1247
44
    }
1248
53
}
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
2.79k
        const std::vector<RowsetSharedPtr>& rowsets, TabletSchemaSPtr& target) {
1257
2.79k
    std::unordered_map<int32_t, VariantExtendedInfo> uid_to_variant_extended_info;
1258
2.79k
    const bool needs_variant_extended_info =
1259
29.6k
            std::ranges::any_of(target->columns(), [](const TabletColumnPtr& column) {
1260
29.6k
                return column->is_variant_type() && (should_check_variant_path_stats(*column) ||
1261
242
                                                     column->variant_enable_nested_group());
1262
29.6k
            });
1263
2.79k
    if (needs_variant_extended_info) {
1264
        // collect path stats from all rowsets and segments
1265
1.75k
        for (const auto& rs : rowsets) {
1266
1.75k
            RETURN_IF_ERROR(aggregate_variant_extended_info(rs, &uid_to_variant_extended_info));
1267
1.75k
        }
1268
241
    }
1269
1270
    // build the output schema
1271
2.79k
    TabletSchemaSPtr output_schema = std::make_shared<TabletSchema>();
1272
2.79k
    output_schema->shawdow_copy_without_columns(*target);
1273
2.79k
    std::unordered_map<int32_t, TabletSchema::PathsSetInfo> uid_to_paths_set_info;
1274
2.79k
    const auto ng_root_uids =
1275
2.79k
            collect_nested_group_compaction_root_uids(target, uid_to_variant_extended_info);
1276
30.6k
    for (const TabletColumnPtr& column : target->columns()) {
1277
30.6k
        if (!column->is_extracted_column()) {
1278
30.5k
            output_schema->append_column(*column);
1279
30.5k
        }
1280
30.6k
        if (!column->is_variant_type()) {
1281
30.3k
            continue;
1282
30.3k
        }
1283
18.4E
        VLOG_DEBUG << "column " << column->name() << " unique id " << column->unique_id();
1284
1285
247
        const auto info_it = uid_to_variant_extended_info.find(column->unique_id());
1286
247
        const VariantExtendedInfo empty_extended_info;
1287
247
        const VariantExtendedInfo& extended_info = info_it == uid_to_variant_extended_info.end()
1288
247
                                                           ? empty_extended_info
1289
247
                                                           : info_it->second;
1290
247
        auto& paths_set_info = uid_to_paths_set_info[column->unique_id()];
1291
247
        const bool use_nested_group_compaction_schema = ng_root_uids.contains(column->unique_id());
1292
1293
247
        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
246
        if (column->variant_enable_doc_mode()) {
1313
88
            const int bucket_num = std::max(1, column->variant_doc_hash_shard_count());
1314
270
            for (int b = 0; b < bucket_num; ++b) {
1315
182
                TabletColumn doc_value_bucket_column = create_doc_value_column(*column, b);
1316
182
                doc_value_bucket_column.set_type(FieldType::OLAP_FIELD_TYPE_VARIANT);
1317
182
                doc_value_bucket_column.set_is_nullable(false);
1318
182
                doc_value_bucket_column.set_variant_enable_doc_mode(true);
1319
182
                output_schema->append_column(doc_value_bucket_column);
1320
182
            }
1321
88
            continue;
1322
88
        }
1323
1324
        // 1. append typed columns
1325
158
        RETURN_IF_ERROR(get_compaction_typed_columns(target, extended_info.typed_paths, column,
1326
158
                                                     output_schema, paths_set_info));
1327
1328
        // 2. append nested columns
1329
158
        RETURN_IF_ERROR(get_compaction_nested_columns(extended_info.nested_paths,
1330
158
                                                      extended_info.path_to_data_types, column,
1331
158
                                                      output_schema, paths_set_info));
1332
1333
        // 3. get the subpaths
1334
158
        get_subpaths(column->variant_max_subcolumns_count(), extended_info.path_to_none_null_values,
1335
158
                     paths_set_info);
1336
1337
        // 4. append subcolumns
1338
158
        if (column->variant_max_subcolumns_count() > 0 || !column->get_sub_columns().empty()) {
1339
110
            get_compaction_subcolumns_from_subpaths(paths_set_info, column, target,
1340
110
                                                    extended_info.path_to_data_types,
1341
110
                                                    extended_info.sparse_paths, output_schema);
1342
110
        }
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
48
        else {
1346
48
            get_compaction_subcolumns_from_data_types(paths_set_info, column, target,
1347
48
                                                      extended_info.path_to_data_types,
1348
48
                                                      output_schema);
1349
48
        }
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
158
        int bucket_num = std::max(1, column->variant_sparse_hash_shard_count());
1355
158
        if (bucket_num > 1) {
1356
288
            for (int b = 0; b < bucket_num; ++b) {
1357
230
                TabletColumn sparse_bucket_column = create_sparse_shard_column(*column, b);
1358
230
                output_schema->append_column(sparse_bucket_column);
1359
230
            }
1360
100
        } else {
1361
100
            TabletColumn sparse_column = create_sparse_column(*column);
1362
100
            output_schema->append_column(sparse_column);
1363
100
        }
1364
158
    }
1365
1366
2.79k
    target = output_schema;
1367
    // used to merge & filter path to sparse column during reading in compaction
1368
2.79k
    target->set_path_set_info(std::move(uid_to_paths_set_info));
1369
18.4E
    VLOG_DEBUG << "dump schema " << target->dump_full_schema();
1370
2.79k
    return Status::OK();
1371
2.79k
}
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
250
                                                    size_t row_pos, size_t num_rows) {
1378
    // Cast input column to ColumnMap type since sparse column is stored as a map
1379
250
    const auto& map_column = assert_cast<const ColumnMap&>(encoded_sparse_column);
1380
1381
    // Get the keys column which contains the paths as strings
1382
250
    const auto& sparse_data_paths =
1383
250
            assert_cast<const ColumnString*>(map_column.get_keys_ptr().get());
1384
250
    const auto& serialized_sparse_column_offsets = map_column.get_offsets();
1385
250
    auto& count_map = *stats->mutable_sparse_column_non_null_size();
1386
    // Iterate through all paths in the sparse column
1387
5.81k
    for (size_t i = row_pos; i != row_pos + num_rows; ++i) {
1388
5.56k
        size_t offset = serialized_sparse_column_offsets[i - 1];
1389
5.56k
        size_t end = serialized_sparse_column_offsets[i];
1390
8.29k
        for (size_t j = offset; j != end; ++j) {
1391
2.73k
            auto path = sparse_data_paths->get_data_at(j);
1392
1393
2.73k
            const auto& sparse_path = path.to_string();
1394
            // If path already exists in statistics, increment its count
1395
2.73k
            if (auto it = count_map.find(sparse_path); it != count_map.end()) {
1396
2.29k
                ++it->second;
1397
2.29k
            }
1398
            // If path doesn't exist and we haven't hit the max statistics size limit,
1399
            // add it with count 1
1400
434
            else if (count_map.size() < max_sparse_column_statistics_size) {
1401
434
                count_map.emplace(sparse_path, 1);
1402
434
            }
1403
2.73k
        }
1404
5.56k
    }
1405
1406
250
    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
250
}
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
16.7M
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
16.7M
        if constexpr (T == TYPE_ARRAY) {
1422
777k
            const size_t size = x.size();
1423
777k
            size_t dimensions = 0;
1424
2.31M
            for (size_t i = 0; i < size; ++i) {
1425
1.54M
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
1.54M
                dimensions = std::max(dimensions, element_dimensions);
1427
1.54M
            }
1428
777k
            return 1 + dimensions;
1429
15.9M
        } else {
1430
15.9M
            return 0;
1431
15.9M
        }
1432
16.7M
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE1EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
57.0k
    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
57.0k
        } else {
1430
57.0k
            return 0;
1431
57.0k
        }
1432
57.0k
    }
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE26EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE42EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE7EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
40.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
40.9k
        } else {
1430
40.9k
            return 0;
1431
40.9k
        }
1432
40.9k
    }
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE12EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE11EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE25EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE2EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
149k
    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
149k
        } else {
1430
149k
            return 0;
1431
149k
        }
1432
149k
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE3EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
6
    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
        } else {
1430
6
            return 0;
1431
6
        }
1432
6
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE4EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
7
    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
7
        } else {
1430
7
            return 0;
1431
7
        }
1432
7
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE5EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
1.20k
    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.20k
        } else {
1430
1.20k
            return 0;
1431
1.20k
        }
1432
1.20k
    }
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE6EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
4.95M
    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
4.95M
        } else {
1430
4.95M
            return 0;
1431
4.95M
        }
1432
4.95M
    }
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE38EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE39EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE8EEEmRKNS_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_13PrimitiveTypeE27EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE9EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
2.93M
    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.93M
        } else {
1430
2.93M
            return 0;
1431
2.93M
        }
1432
2.93M
    }
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE36EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE37EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE23EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1420
7.80M
    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
7.80M
        } else {
1430
7.80M
            return 0;
1431
7.80M
        }
1432
7.80M
    }
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
777k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1421
777k
        if constexpr (T == TYPE_ARRAY) {
1422
777k
            const size_t size = x.size();
1423
777k
            size_t dimensions = 0;
1424
2.31M
            for (size_t i = 0; i < size; ++i) {
1425
1.54M
                size_t element_dimensions = apply_visitor(*this, x[i]);
1426
1.54M
                dimensions = std::max(dimensions, element_dimensions);
1427
1.54M
            }
1428
777k
            return 1 + dimensions;
1429
        } else {
1430
            return 0;
1431
        }
1432
777k
    }
_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
    }
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE28EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE29EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE20EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE30EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util32FieldVisitorToNumberOfDimensions5applyILNS_13PrimitiveTypeE35EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
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
15.7k
    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
15.7k
        } else {
1430
15.7k
            return 0;
1431
15.7k
        }
1432
15.7k
    }
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
14.7M
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1442
14.7M
        if constexpr (T == TYPE_ARRAY) {
1443
0
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Array type is not supported");
1444
45.3k
        } else if constexpr (T == TYPE_NULL) {
1445
45.3k
            have_nulls = true;
1446
45.3k
            return 1;
1447
14.6M
        } else {
1448
14.6M
            type = T;
1449
14.6M
            return 1;
1450
14.6M
        }
1451
14.7M
    }
_ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE1EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1441
45.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
45.3k
        } else if constexpr (T == TYPE_NULL) {
1445
45.3k
            have_nulls = true;
1446
45.3k
            return 1;
1447
        } else {
1448
            type = T;
1449
            return 1;
1450
        }
1451
45.3k
    }
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
92.6k
    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
92.6k
        } else {
1448
92.6k
            type = T;
1449
92.6k
            return 1;
1450
92.6k
        }
1451
92.6k
    }
_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
676
    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
676
        } else {
1448
676
            type = T;
1449
676
            return 1;
1450
676
        }
1451
676
    }
_ZN5doris12variant_util30SimpleFieldVisitorToScalarType5applyILNS_13PrimitiveTypeE6EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1441
4.33M
    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.33M
        } else {
1448
4.33M
            type = T;
1449
4.33M
            return 1;
1450
4.33M
        }
1451
4.33M
    }
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
7.44M
    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.44M
        } else {
1448
7.44M
            type = T;
1449
7.44M
            return 1;
1450
7.44M
        }
1451
7.44M
    }
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
15.6k
    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
15.6k
        } else {
1448
15.6k
            type = T;
1449
15.6k
            return 1;
1450
15.6k
        }
1451
15.6k
    }
1452
14.4M
    void get_scalar_type(PrimitiveType* data_type) const { *data_type = type; }
1453
14.4M
    bool contain_nulls() const { return have_nulls; }
1454
1455
14.4M
    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
2.07M
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
2.07M
        if constexpr (T == TYPE_ARRAY) {
1470
777k
            size_t size = x.size();
1471
2.31M
            for (size_t i = 0; i < size; ++i) {
1472
1.54M
                apply_visitor(*this, x[i]);
1473
1.54M
            }
1474
777k
            return 0;
1475
777k
        } else if constexpr (T == TYPE_NULL) {
1476
11.6k
            have_nulls = true;
1477
11.6k
            return 0;
1478
1.28M
        } else {
1479
1.28M
            field_types.insert(T);
1480
1.28M
            type_indexes.insert(T);
1481
1.28M
            return 0;
1482
1.28M
        }
1483
2.07M
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE1EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
11.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
11.6k
        } else if constexpr (T == TYPE_NULL) {
1476
11.6k
            have_nulls = true;
1477
11.6k
            return 0;
1478
        } else {
1479
            field_types.insert(T);
1480
            type_indexes.insert(T);
1481
            return 0;
1482
        }
1483
11.6k
    }
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE26EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE42EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE7EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
28.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
28.6k
        } else {
1479
28.6k
            field_types.insert(T);
1480
28.6k
            type_indexes.insert(T);
1481
28.6k
            return 0;
1482
28.6k
        }
1483
28.6k
    }
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE12EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE11EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE25EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE2EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
57.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
        } else if constexpr (T == TYPE_NULL) {
1476
            have_nulls = true;
1477
            return 0;
1478
57.2k
        } else {
1479
57.2k
            field_types.insert(T);
1480
57.2k
            type_indexes.insert(T);
1481
57.2k
            return 0;
1482
57.2k
        }
1483
57.2k
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE3EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
4
    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
4
        } else {
1479
4
            field_types.insert(T);
1480
4
            type_indexes.insert(T);
1481
4
            return 0;
1482
4
        }
1483
4
    }
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE4EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE5EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
531
    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
531
        } else {
1479
531
            field_types.insert(T);
1480
531
            type_indexes.insert(T);
1481
531
            return 0;
1482
531
        }
1483
531
    }
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE6EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
633k
    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
633k
        } else {
1479
633k
            field_types.insert(T);
1480
633k
            type_indexes.insert(T);
1481
633k
            return 0;
1482
633k
        }
1483
633k
    }
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE38EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE39EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE8EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE27EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE9EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
173k
    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
173k
        } else {
1479
173k
            field_types.insert(T);
1480
173k
            type_indexes.insert(T);
1481
173k
            return 0;
1482
173k
        }
1483
173k
    }
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE36EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE37EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
_ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE23EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Line
Count
Source
1468
389k
    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
389k
        } else {
1479
389k
            field_types.insert(T);
1480
389k
            type_indexes.insert(T);
1481
389k
            return 0;
1482
389k
        }
1483
389k
    }
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
777k
    size_t apply(const typename PrimitiveTypeTraits<T>::CppType& x) {
1469
777k
        if constexpr (T == TYPE_ARRAY) {
1470
777k
            size_t size = x.size();
1471
2.31M
            for (size_t i = 0; i < size; ++i) {
1472
1.54M
                apply_visitor(*this, x[i]);
1473
1.54M
            }
1474
777k
            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
777k
    }
_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
    }
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE28EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE29EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE20EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE30EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
Unexecuted instantiation: _ZN5doris12variant_util24FieldVisitorToScalarType5applyILNS_13PrimitiveTypeE35EEEmRKNS_19PrimitiveTypeTraitsIXT_EE7CppTypeE
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
529k
    void get_scalar_type(PrimitiveType* type) const {
1485
529k
        if (type_indexes.size() == 1) {
1486
            // Most cases will have only one type
1487
469k
            *type = *type_indexes.begin();
1488
469k
            return;
1489
469k
        }
1490
60.4k
        DataTypePtr data_type;
1491
60.4k
        get_least_supertype_jsonb(type_indexes, &data_type);
1492
60.4k
        *type = data_type->get_primitive_type();
1493
60.4k
    }
1494
529k
    bool contain_nulls() const { return have_nulls; }
1495
529k
    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
15.1M
void get_field_info_impl(const Field& field, FieldInfo* info) {
1505
15.1M
    Visitor to_scalar_type_visitor;
1506
15.1M
    apply_visitor(to_scalar_type_visitor, field);
1507
15.1M
    PrimitiveType type_id;
1508
15.1M
    to_scalar_type_visitor.get_scalar_type(&type_id);
1509
    // array item's dimension may missmatch, eg. [1, 2, [1, 2, 3]]
1510
15.1M
    *info = {type_id, to_scalar_type_visitor.contain_nulls(),
1511
15.1M
             to_scalar_type_visitor.need_convert_field(),
1512
15.1M
             apply_visitor(FieldVisitorToNumberOfDimensions(), field)};
1513
15.1M
}
_ZN5doris12variant_util19get_field_info_implINS0_24FieldVisitorToScalarTypeEEEvRKNS_5FieldEPNS_9FieldInfoE
Line
Count
Source
1504
528k
void get_field_info_impl(const Field& field, FieldInfo* info) {
1505
528k
    Visitor to_scalar_type_visitor;
1506
528k
    apply_visitor(to_scalar_type_visitor, field);
1507
528k
    PrimitiveType type_id;
1508
528k
    to_scalar_type_visitor.get_scalar_type(&type_id);
1509
    // array item's dimension may missmatch, eg. [1, 2, [1, 2, 3]]
1510
528k
    *info = {type_id, to_scalar_type_visitor.contain_nulls(),
1511
528k
             to_scalar_type_visitor.need_convert_field(),
1512
528k
             apply_visitor(FieldVisitorToNumberOfDimensions(), field)};
1513
528k
}
_ZN5doris12variant_util19get_field_info_implINS0_30SimpleFieldVisitorToScalarTypeEEEvRKNS_5FieldEPNS_9FieldInfoE
Line
Count
Source
1504
14.5M
void get_field_info_impl(const Field& field, FieldInfo* info) {
1505
14.5M
    Visitor to_scalar_type_visitor;
1506
14.5M
    apply_visitor(to_scalar_type_visitor, field);
1507
14.5M
    PrimitiveType type_id;
1508
14.5M
    to_scalar_type_visitor.get_scalar_type(&type_id);
1509
    // array item's dimension may missmatch, eg. [1, 2, [1, 2, 3]]
1510
14.5M
    *info = {type_id, to_scalar_type_visitor.contain_nulls(),
1511
14.5M
             to_scalar_type_visitor.need_convert_field(),
1512
14.5M
             apply_visitor(FieldVisitorToNumberOfDimensions(), field)};
1513
14.5M
}
1514
1515
15.1M
void get_field_info(const Field& field, FieldInfo* info) {
1516
15.1M
    if (field.is_complex_field()) {
1517
530k
        get_field_info_impl<FieldVisitorToScalarType>(field, info);
1518
14.6M
    } else {
1519
14.6M
        get_field_info_impl<SimpleFieldVisitorToScalarType>(field, info);
1520
14.6M
    }
1521
15.1M
}
1522
1523
bool generate_sub_column_info(const TabletSchema& schema, int32_t col_unique_id,
1524
                              const std::string& path,
1525
123k
                              TabletSchema::SubColumnInfo* sub_column_info) {
1526
123k
    const auto& parent_column = schema.column_by_uid(col_unique_id);
1527
123k
    std::function<void(const TabletColumn&, TabletColumn*)> generate_result_column =
1528
123k
            [&](const TabletColumn& from_column, TabletColumn* to_column) {
1529
9.73k
                to_column->set_name(parent_column.name_lower_case() + "." + path);
1530
9.73k
                to_column->set_type(from_column.type());
1531
9.73k
                to_column->set_parent_unique_id(parent_column.unique_id());
1532
9.73k
                bool is_typed = !parent_column.variant_enable_typed_paths_to_sparse();
1533
9.73k
                to_column->set_path_info(
1534
9.73k
                        PathInData(parent_column.name_lower_case() + "." + path, is_typed));
1535
9.73k
                to_column->set_aggregation_method(parent_column.aggregation());
1536
9.73k
                to_column->set_is_nullable(true);
1537
9.73k
                to_column->set_parent_unique_id(parent_column.unique_id());
1538
9.73k
                if (from_column.is_decimal()) {
1539
9.69k
                    to_column->set_precision(from_column.precision());
1540
9.69k
                }
1541
9.73k
                to_column->set_frac(from_column.frac());
1542
1543
9.73k
                if (from_column.is_array_type()) {
1544
1.89k
                    TabletColumn nested_column;
1545
1.89k
                    generate_result_column(*from_column.get_sub_columns()[0], &nested_column);
1546
1.89k
                    to_column->add_sub_column(nested_column);
1547
1.89k
                }
1548
9.73k
            };
1549
1550
123k
    auto generate_index = [&](const std::string& pattern) {
1551
        // 1. find subcolumn's index
1552
7.84k
        if (const auto& indexes = schema.inverted_index_by_field_pattern(col_unique_id, pattern);
1553
7.84k
            !indexes.empty()) {
1554
2.54k
            for (const auto& index : indexes) {
1555
2.54k
                auto index_ptr = std::make_shared<TabletIndex>(*index);
1556
2.54k
                index_ptr->set_escaped_escaped_index_suffix_path(
1557
2.54k
                        sub_column_info->column.path_info_ptr()->get_path());
1558
2.54k
                sub_column_info->indexes.emplace_back(std::move(index_ptr));
1559
2.54k
            }
1560
2.51k
        }
1561
        // 2. find parent column's index
1562
5.32k
        else if (const auto parent_index = schema.inverted_indexs(col_unique_id);
1563
5.32k
                 !parent_index.empty()) {
1564
219
            inherit_index(parent_index, sub_column_info->indexes, sub_column_info->column);
1565
5.10k
        } else {
1566
5.10k
            sub_column_info->indexes.clear();
1567
5.10k
        }
1568
7.84k
    };
1569
1570
123k
    const auto& sub_columns = parent_column.get_sub_columns();
1571
167k
    for (const auto& sub_column : sub_columns) {
1572
167k
        const char* pattern = sub_column->name().c_str();
1573
167k
        switch (sub_column->pattern_type()) {
1574
88
        case PatternTypePB::MATCH_NAME: {
1575
88
            if (strcmp(pattern, path.c_str()) == 0) {
1576
31
                generate_result_column(*sub_column, &sub_column_info->column);
1577
31
                generate_index(sub_column->name());
1578
31
                return true;
1579
31
            }
1580
57
            break;
1581
88
        }
1582
167k
        case PatternTypePB::MATCH_NAME_GLOB: {
1583
167k
            if (glob_match_re2(pattern, path)) {
1584
7.81k
                generate_result_column(*sub_column, &sub_column_info->column);
1585
7.81k
                generate_index(sub_column->name());
1586
7.81k
                return true;
1587
7.81k
            }
1588
159k
            break;
1589
167k
        }
1590
159k
        default:
1591
0
            break;
1592
167k
        }
1593
167k
    }
1594
115k
    return false;
1595
123k
}
1596
1597
TabletSchemaSPtr VariantCompactionUtil::calculate_variant_extended_schema(
1598
0
        const std::vector<RowsetSharedPtr>& rowsets, const TabletSchemaSPtr& base_schema) {
1599
0
    if (rowsets.empty()) {
1600
0
        return nullptr;
1601
0
    }
1602
1603
0
    std::vector<TabletSchemaSPtr> schemas;
1604
0
    for (const auto& rs : rowsets) {
1605
0
        if (rs->num_segments() == 0) {
1606
0
            continue;
1607
0
        }
1608
0
        const auto& tablet_schema = rs->tablet_schema();
1609
0
        SegmentCacheHandle segment_cache;
1610
0
        auto st = SegmentLoader::instance()->load_segments(std::static_pointer_cast<BetaRowset>(rs),
1611
0
                                                           &segment_cache);
1612
0
        if (!st.ok()) {
1613
0
            return base_schema;
1614
0
        }
1615
0
        for (const auto& segment : segment_cache.get_segments()) {
1616
0
            TabletSchemaSPtr schema = tablet_schema->copy_without_variant_extracted_columns();
1617
0
            for (const auto& column : tablet_schema->columns()) {
1618
0
                if (!column->is_variant_type()) {
1619
0
                    continue;
1620
0
                }
1621
0
                std::shared_ptr<ColumnReader> column_reader;
1622
0
                OlapReaderStatistics stats;
1623
0
                st = segment->get_column_reader(column->unique_id(), &column_reader, &stats);
1624
0
                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
0
                if (!column_reader) {
1630
0
                    continue;
1631
0
                }
1632
1633
0
                CHECK(column_reader->get_meta_type() == FieldType::OLAP_FIELD_TYPE_VARIANT);
1634
0
                auto* variant_column_reader =
1635
0
                        assert_cast<segment_v2::VariantColumnReader*>(column_reader.get());
1636
                // load external meta before getting subcolumn meta info
1637
0
                st = variant_column_reader->load_external_meta_once();
1638
0
                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
0
                const auto* subcolumn_meta_info = variant_column_reader->get_subcolumns_meta_info();
1644
0
                for (const auto& entry : *subcolumn_meta_info) {
1645
0
                    if (entry->path.empty()) {
1646
0
                        continue;
1647
0
                    }
1648
0
                    const std::string& column_name =
1649
0
                            column->name_lower_case() + "." + entry->path.get_path();
1650
0
                    const DataTypePtr& data_type = entry->data.file_column_type;
1651
0
                    PathInDataBuilder full_path_builder;
1652
0
                    auto full_path = full_path_builder.append(column->name_lower_case(), false)
1653
0
                                             .append(entry->path.get_parts(), false)
1654
0
                                             .build();
1655
0
                    TabletColumn subcolumn =
1656
0
                            get_column_by_type(data_type, column_name,
1657
0
                                               ExtraInfo {.unique_id = -1,
1658
0
                                                          .parent_unique_id = column->unique_id(),
1659
0
                                                          .path_info = full_path});
1660
0
                    schema->append_column(subcolumn);
1661
0
                }
1662
0
            }
1663
0
            schemas.emplace_back(schema);
1664
0
        }
1665
0
    }
1666
0
    TabletSchemaSPtr least_common_schema;
1667
0
    auto st = get_least_common_schema(schemas, base_schema, least_common_schema, false);
1668
0
    if (!st.ok()) {
1669
0
        return base_schema;
1670
0
    }
1671
0
    return least_common_schema;
1672
0
}
1673
1674
bool inherit_index(const std::vector<const TabletIndex*>& parent_indexes,
1675
                   TabletIndexes& subcolumns_indexes, FieldType column_type,
1676
91.6k
                   const std::string& suffix_path, bool is_array_nested_type) {
1677
91.6k
    if (parent_indexes.empty()) {
1678
91.0k
        return false;
1679
91.0k
    }
1680
539
    subcolumns_indexes.clear();
1681
    // bkd index or array index only need to inherit one index
1682
539
    if (field_is_numeric_type(column_type) ||
1683
539
        (is_array_nested_type &&
1684
302
         (field_is_numeric_type(column_type) || field_is_slice_type(column_type)))) {
1685
302
        auto index_ptr = std::make_shared<TabletIndex>(*parent_indexes[0]);
1686
302
        index_ptr->set_escaped_escaped_index_suffix_path(suffix_path);
1687
        // no need parse for bkd index or array index
1688
302
        index_ptr->remove_parser_and_analyzer();
1689
302
        subcolumns_indexes.emplace_back(std::move(index_ptr));
1690
302
        return true;
1691
302
    }
1692
    // string type need to inherit all indexes
1693
238
    else if (field_is_slice_type(column_type) && !is_array_nested_type) {
1694
239
        for (const auto& index : parent_indexes) {
1695
239
            auto index_ptr = std::make_shared<TabletIndex>(*index);
1696
239
            index_ptr->set_escaped_escaped_index_suffix_path(suffix_path);
1697
239
            subcolumns_indexes.emplace_back(std::move(index_ptr));
1698
239
        }
1699
238
        return true;
1700
238
    }
1701
18.4E
    return false;
1702
539
}
1703
1704
bool inherit_index(const std::vector<const TabletIndex*>& parent_indexes,
1705
91.7k
                   TabletIndexes& subcolumns_indexes, const TabletColumn& column) {
1706
91.7k
    if (!column.is_extracted_column()) {
1707
3
        return false;
1708
3
    }
1709
91.7k
    if (column.is_array_type()) {
1710
2.43k
        if (column.get_sub_columns().empty()) {
1711
0
            return false;
1712
0
        }
1713
2.43k
        const TabletColumn* nested = column.get_sub_columns()[0].get();
1714
2.43k
        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
2.43k
        if (nested == nullptr) {
1721
0
            return false;
1722
0
        }
1723
2.43k
        return inherit_index(parent_indexes, subcolumns_indexes, nested->type(),
1724
2.43k
                             column.path_info_ptr()->get_path(), true);
1725
2.43k
    }
1726
89.2k
    return inherit_index(parent_indexes, subcolumns_indexes, column.type(),
1727
89.2k
                         column.path_info_ptr()->get_path());
1728
91.7k
}
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
14.0k
        Deleter(SimpleObjectPool<T>* parent_ = nullptr) : parent {parent_} {} /// NOLINT
1778
14.0k
        void operator()(T* owning_ptr) const {
1779
14.0k
            std::lock_guard lock {parent->mutex};
1780
14.0k
            parent->stack.emplace(owning_ptr);
1781
14.0k
        }
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
14.0k
    Pointer get(Factory&& f) {
1790
14.0k
        std::unique_lock lock(mutex);
1791
14.0k
        if (stack.empty()) {
1792
26
            return {f(), this};
1793
26
        }
1794
14.0k
        auto object = stack.top().release();
1795
14.0k
        stack.pop();
1796
14.0k
        return std::unique_ptr<T, Deleter>(object, Deleter(this));
1797
14.0k
    }
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
1.61k
    Pointer get(Factory&& f) {
1790
1.61k
        std::unique_lock lock(mutex);
1791
1.61k
        if (stack.empty()) {
1792
25
            return {f(), this};
1793
25
        }
1794
1.59k
        auto object = stack.top().release();
1795
1.59k
        stack.pop();
1796
1.59k
        return std::unique_ptr<T, Deleter>(object, Deleter(this));
1797
1.61k
    }
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
29.5M
static inline void append_binary_bytes(ColumnString::Chars& chars, const void* data, size_t size) {
1809
29.5M
    const auto old_size = chars.size();
1810
29.5M
    chars.resize(old_size + size);
1811
29.5M
    memcpy(chars.data() + old_size, reinterpret_cast<const char*>(data), size);
1812
29.5M
}
1813
1814
12.9M
static inline void append_binary_type(ColumnString::Chars& chars, FieldType type) {
1815
12.9M
    const uint8_t t = static_cast<uint8_t>(type);
1816
12.9M
    append_binary_bytes(chars, &t, sizeof(uint8_t));
1817
12.9M
}
1818
1819
7.00M
static inline void append_binary_sizet(ColumnString::Chars& chars, size_t v) {
1820
7.00M
    append_binary_bytes(chars, &v, sizeof(size_t));
1821
7.00M
}
1822
1823
13.0M
static void append_field_to_binary_chars(const Field& field, ColumnString::Chars& chars) {
1824
13.0M
    switch (field.get_type()) {
1825
5
    case PrimitiveType::TYPE_NULL: {
1826
5
        append_binary_type(chars, FieldType::OLAP_FIELD_TYPE_NONE);
1827
5
        return;
1828
0
    }
1829
80.0k
    case PrimitiveType::TYPE_BOOLEAN: {
1830
80.0k
        append_binary_type(chars,
1831
80.0k
                           TabletColumn::get_field_type_by_type(PrimitiveType::TYPE_BOOLEAN));
1832
80.0k
        const auto v = static_cast<UInt8>(field.get<PrimitiveType::TYPE_BOOLEAN>());
1833
80.0k
        append_binary_bytes(chars, &v, sizeof(UInt8));
1834
80.0k
        return;
1835
0
    }
1836
3.77M
    case PrimitiveType::TYPE_BIGINT: {
1837
3.77M
        append_binary_type(chars, TabletColumn::get_field_type_by_type(PrimitiveType::TYPE_BIGINT));
1838
3.77M
        const auto v = field.get<PrimitiveType::TYPE_BIGINT>();
1839
3.77M
        append_binary_bytes(chars, &v, sizeof(Int64));
1840
3.77M
        return;
1841
0
    }
1842
3
    case PrimitiveType::TYPE_LARGEINT: {
1843
3
        append_binary_type(chars,
1844
3
                           TabletColumn::get_field_type_by_type(PrimitiveType::TYPE_LARGEINT));
1845
3
        const auto v = field.get<PrimitiveType::TYPE_LARGEINT>();
1846
3
        append_binary_bytes(chars, &v, sizeof(int128_t));
1847
3
        return;
1848
0
    }
1849
2.73M
    case PrimitiveType::TYPE_DOUBLE: {
1850
2.73M
        append_binary_type(chars, TabletColumn::get_field_type_by_type(PrimitiveType::TYPE_DOUBLE));
1851
2.73M
        const auto v = field.get<PrimitiveType::TYPE_DOUBLE>();
1852
2.73M
        append_binary_bytes(chars, &v, sizeof(Float64));
1853
2.73M
        return;
1854
0
    }
1855
6.67M
    case PrimitiveType::TYPE_STRING: {
1856
6.67M
        append_binary_type(chars, FieldType::OLAP_FIELD_TYPE_STRING);
1857
6.67M
        const auto& v = field.get<PrimitiveType::TYPE_STRING>();
1858
6.67M
        append_binary_sizet(chars, v.size());
1859
6.67M
        append_binary_bytes(chars, v.data(), v.size());
1860
6.67M
        return;
1861
0
    }
1862
15.6k
    case PrimitiveType::TYPE_JSONB: {
1863
15.6k
        append_binary_type(chars, FieldType::OLAP_FIELD_TYPE_JSONB);
1864
15.6k
        const auto& v = field.get<PrimitiveType::TYPE_JSONB>();
1865
15.6k
        append_binary_sizet(chars, v.get_size());
1866
15.6k
        append_binary_bytes(chars, v.get_value(), v.get_size());
1867
15.6k
        return;
1868
0
    }
1869
310k
    case PrimitiveType::TYPE_ARRAY: {
1870
310k
        append_binary_type(chars, FieldType::OLAP_FIELD_TYPE_ARRAY);
1871
310k
        const auto& a = field.get<PrimitiveType::TYPE_ARRAY>();
1872
310k
        append_binary_sizet(chars, a.size());
1873
377k
        for (const auto& elem : a) {
1874
377k
            append_field_to_binary_chars(elem, chars);
1875
377k
        }
1876
310k
        return;
1877
0
    }
1878
0
    default:
1879
0
        throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Unsupported field type {}",
1880
0
                               field.get_type());
1881
13.0M
    }
1882
13.0M
}
1883
template <typename ParserImpl>
1884
void parse_json_to_variant_impl(IColumn& column, const char* src, size_t length,
1885
1.10M
                                JSONDataParser<ParserImpl>* parser, const ParseConfig& config) {
1886
1.10M
    auto& column_variant = assert_cast<ColumnVariant&>(column);
1887
1.10M
    std::optional<ParseResult> result;
1888
    /// Treat empty string as an empty object
1889
    /// for better CAST from String to Object.
1890
1.10M
    if (length > 0) {
1891
1.10M
        result = parser->parse(src, length, config);
1892
1.10M
    } else {
1893
3.10k
        result = ParseResult {};
1894
3.10k
    }
1895
1.10M
    if (!result) {
1896
12
        VLOG_DEBUG << "failed to parse " << std::string_view(src, length) << ", length= " << length;
1897
12
        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
12
        PathInData root_path;
1903
12
        Field field = Field::create_field<TYPE_STRING>(String(src, length));
1904
12
        result = ParseResult {{root_path}, {field}};
1905
12
    }
1906
1.10M
    auto& [paths, values] = *result;
1907
1.10M
    assert(paths.size() == values.size());
1908
1.10M
    size_t old_num_rows = column_variant.rows();
1909
1.10M
    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
3.01k
        std::vector<PathInData> check_paths;
1913
12.0k
        for (const auto& entry : column_variant.get_subcolumns()) {
1914
12.0k
            check_paths.push_back(entry->path);
1915
12.0k
        }
1916
3.01k
        check_paths.insert(check_paths.end(), paths.begin(), paths.end());
1917
3.01k
        THROW_IF_ERROR(check_variant_has_no_ambiguous_paths(check_paths));
1918
3.01k
    }
1919
1.10M
    auto [doc_value_data_paths, doc_value_data_values] =
1920
1.10M
            column_variant.get_doc_value_data_paths_and_values();
1921
1.10M
    auto& doc_value_data_offsets = column_variant.serialized_doc_value_column_offsets();
1922
1923
1.41M
    auto flush_defaults = [](ColumnVariant::Subcolumn* subcolumn) {
1924
1.41M
        const auto num_defaults = subcolumn->cur_num_of_defaults();
1925
1.41M
        if (num_defaults > 0) {
1926
165k
            subcolumn->insert_many_defaults(num_defaults);
1927
165k
            subcolumn->reset_current_num_of_defaults();
1928
165k
        }
1929
1.41M
    };
1930
1931
1.10M
    auto is_plain_path = [](const PathInData& path) {
1932
13
        for (const auto& part : path.get_parts()) {
1933
13
            if (part.is_nested || part.anonymous_array_level != 0) {
1934
0
                return false;
1935
0
            }
1936
13
        }
1937
9
        return true;
1938
9
    };
1939
1940
1.10M
    auto get_or_create_subcolumn = [&](const PathInData& path, size_t index_hint,
1941
1.41M
                                       const FieldInfo& field_info) -> ColumnVariant::Subcolumn* {
1942
1.41M
        auto* subcolumn = column_variant.get_subcolumn(path, index_hint);
1943
1.41M
        if (subcolumn == nullptr) {
1944
3.65k
            if (path.has_nested_part()) {
1945
8
                column_variant.add_nested_subcolumn(path, field_info, old_num_rows);
1946
3.64k
            } else {
1947
3.64k
                column_variant.add_sub_column(path, old_num_rows);
1948
3.64k
            }
1949
3.65k
            subcolumn = column_variant.get_subcolumn(path, index_hint);
1950
3.65k
        }
1951
1.41M
        if (!subcolumn) {
1952
0
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "Failed to find sub column {}",
1953
0
                                   path.get_path());
1954
0
        }
1955
1.41M
        return subcolumn;
1956
1.41M
    };
1957
1958
1.41M
    auto normalize_plain_path = [&](const PathInData& path) {
1959
1.41M
        if (!config.check_duplicate_json_path || path.empty() || !is_plain_path(path)) {
1960
1.41M
            return path;
1961
1.41M
        }
1962
9
        return PathInData(path.get_path());
1963
1.41M
    };
1964
1965
1.10M
    auto insert_into_subcolumn = [&](size_t i,
1966
1.41M
                                     bool check_size_mismatch) -> ColumnVariant::Subcolumn* {
1967
1.41M
        FieldInfo field_info;
1968
1.41M
        get_field_info(values[i], &field_info);
1969
1.41M
        if (field_info.scalar_type_id == PrimitiveType::INVALID_TYPE) {
1970
104
            return nullptr;
1971
104
        }
1972
1.41M
        auto path = normalize_plain_path(paths[i]);
1973
1.41M
        auto* subcolumn = get_or_create_subcolumn(path, i, field_info);
1974
1.41M
        flush_defaults(subcolumn);
1975
1.41M
        if (check_size_mismatch && subcolumn->size() != old_num_rows) {
1976
1
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
1977
1
                                   "subcolumn {} size missmatched, may contains duplicated entry",
1978
1
                                   path.get_path());
1979
1
        }
1980
1.41M
        subcolumn->insert(std::move(values[i]), std::move(field_info));
1981
1.41M
        return subcolumn;
1982
1.41M
    };
1983
1984
1.10M
    switch (config.parse_to) {
1985
82.1k
    case ParseConfig::ParseTo::OnlySubcolumns:
1986
1.50M
        for (size_t i = 0; i < paths.size(); ++i) {
1987
1.41M
            insert_into_subcolumn(i, true);
1988
1.41M
        }
1989
82.1k
        break;
1990
1.02M
    case ParseConfig::ParseTo::OnlyDocValueColumn: {
1991
1.02M
        std::vector<size_t> doc_item_indexes;
1992
1.02M
        doc_item_indexes.reserve(paths.size());
1993
1.02M
        phmap::flat_hash_set<StringRef, StringRefHash> seen_paths;
1994
1.02M
        seen_paths.reserve(paths.size());
1995
1996
14.4M
        for (size_t i = 0; i < paths.size(); ++i) {
1997
13.4M
            FieldInfo field_info;
1998
13.4M
            get_field_info(values[i], &field_info);
1999
13.4M
            if (paths[i].empty()) {
2000
                // Plain non-doc VARIANT can use doc-value KV as writer-side staging. An
2001
                // invalid root entry from JSON object/array is neither a scalar root value nor
2002
                // a doc KV path, so leave this row's doc offset empty. Doc-mode and valid scalar
2003
                // roots still populate the root subcolumn below.
2004
80
                if (!column_variant.enable_doc_mode() &&
2005
80
                    field_info.scalar_type_id == PrimitiveType::INVALID_TYPE) {
2006
1
                    continue;
2007
1
                }
2008
79
                auto* subcolumn = column_variant.get_subcolumn(paths[i]);
2009
79
                DCHECK(subcolumn != nullptr);
2010
79
                flush_defaults(subcolumn);
2011
79
                subcolumn->insert(std::move(values[i]), std::move(field_info));
2012
79
                continue;
2013
80
            }
2014
13.4M
            if (field_info.scalar_type_id == PrimitiveType::INVALID_TYPE ||
2015
13.4M
                values[i].get_type() == PrimitiveType::TYPE_NULL) {
2016
38.9k
                continue;
2017
38.9k
            }
2018
13.3M
            const auto& path_str = paths[i].get_path();
2019
13.3M
            StringRef path_ref {path_str.data(), path_str.size()};
2020
13.3M
            if (UNLIKELY(!seen_paths.emplace(path_ref).second)) {
2021
0
                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
2022
0
                                       "may contains duplicated entry : {}",
2023
0
                                       std::string_view(path_str));
2024
0
            }
2025
13.3M
            doc_item_indexes.push_back(i);
2026
13.3M
        }
2027
2028
1.02M
        std::sort(doc_item_indexes.begin(), doc_item_indexes.end(),
2029
42.0M
                  [&](size_t l, size_t r) { return paths[l].get_path() < paths[r].get_path(); });
2030
11.5M
        for (const auto idx : doc_item_indexes) {
2031
11.5M
            const auto& path_str = paths[idx].get_path();
2032
11.5M
            doc_value_data_paths->insert_data(path_str.data(), path_str.size());
2033
11.5M
            auto& chars = doc_value_data_values->get_chars();
2034
11.5M
            append_field_to_binary_chars(values[idx], chars);
2035
11.5M
            doc_value_data_values->get_offsets().push_back(chars.size());
2036
11.5M
        }
2037
1.02M
    } break;
2038
1.10M
    }
2039
1.10M
    doc_value_data_offsets.push_back(doc_value_data_paths->size());
2040
    // /// Insert default values to missed subcolumns.
2041
1.10M
    const auto& subcolumns = column_variant.get_subcolumns();
2042
5.29M
    for (const auto& entry : subcolumns) {
2043
5.29M
        if (entry->data.size() == old_num_rows) {
2044
            // Handle nested paths differently from simple paths
2045
3.87M
            if (entry->path.has_nested_part()) {
2046
                // Try to insert default from nested, if failed, insert regular default
2047
0
                bool success = UNLIKELY(column_variant.try_insert_default_from_nested(entry));
2048
0
                if (!success) {
2049
0
                    entry->data.insert_default();
2050
0
                }
2051
3.87M
            } else {
2052
                // For non-nested paths, increment default counter
2053
3.87M
                entry->data.increment_default_counter();
2054
3.87M
            }
2055
3.87M
        }
2056
5.29M
    }
2057
1.10M
    column_variant.incr_num_rows();
2058
1.10M
    if (column_variant.get_sparse_column()->size() == old_num_rows) {
2059
1.10M
        column_variant.get_sparse_column_mutable().insert_default();
2060
1.10M
    }
2061
1.10M
#ifndef NDEBUG
2062
1.10M
    column_variant.check_consistency();
2063
1.10M
#endif
2064
1.10M
}
2065
2066
// exposed interfaces
2067
void parse_json_to_variant(IColumn& column, const StringRef& json, JsonParser* parser,
2068
12.4k
                           const ParseConfig& config) {
2069
12.4k
    if (parser) {
2070
0
        return parse_json_to_variant_impl(column, json.data, json.size, parser, config);
2071
12.4k
    } else {
2072
12.4k
        auto pool_parser = parsers_pool.get([] { return new JsonParser(); });
2073
12.4k
        return parse_json_to_variant_impl(column, json.data, json.size, pool_parser.get(), config);
2074
12.4k
    }
2075
12.4k
}
2076
2077
void parse_json_to_variant(IColumn& column, const ColumnString& raw_json_column,
2078
1.61k
                           const ParseConfig& config) {
2079
1.61k
    auto parser = parsers_pool.get([] { return new JsonParser(); });
2080
1.09M
    for (size_t i = 0; i < raw_json_column.size(); ++i) {
2081
1.09M
        StringRef raw_json = raw_json_column.get_data_at(i);
2082
1.09M
        parse_json_to_variant_impl(column, raw_json.data, raw_json.size, parser.get(), config);
2083
1.09M
    }
2084
1.61k
    column.finalize();
2085
1.61k
}
2086
2087
// parse the doc snapshot column to subcolumns
2088
0
void materialize_docs_to_subcolumns(ColumnVariant& column_variant) {
2089
0
    auto subcolumns = materialize_docs_to_subcolumns_map(column_variant);
2090
2091
0
    for (auto& entry : subcolumns) {
2092
0
        entry.second.finalize();
2093
0
        if (!column_variant.add_sub_column(PathInData(entry.first),
2094
0
                                           IColumn::mutate(entry.second.get_finalized_column_ptr()),
2095
0
                                           entry.second.get_least_common_type())) {
2096
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
2097
0
                                   "Failed to add subcolumn {}, which is from doc snapshot column",
2098
0
                                   entry.first);
2099
0
        }
2100
0
    }
2101
2102
0
    column_variant.finalize();
2103
0
}
2104
2105
// ============ Implementation from variant_util.cpp ============
2106
2107
phmap::flat_hash_map<std::string_view, ColumnVariant::Subcolumn> materialize_docs_to_subcolumns_map(
2108
11
        const ColumnVariant& variant, size_t expected_unique_paths) {
2109
11
    constexpr size_t kInitialPathReserve = 8192;
2110
11
    phmap::flat_hash_map<std::string_view, ColumnVariant::Subcolumn> subcolumns;
2111
2112
11
    const auto [column_key, column_value] = variant.get_doc_value_data_paths_and_values();
2113
11
    const auto& column_offsets = variant.serialized_doc_value_column_offsets();
2114
11
    const size_t num_rows = column_offsets.size();
2115
2116
11
    DCHECK_EQ(num_rows, variant.size()) << "doc snapshot offsets size mismatch with variant rows";
2117
2118
11
    subcolumns.reserve(expected_unique_paths != 0
2119
11
                               ? expected_unique_paths
2120
11
                               : std::min<size_t>(column_key->size(), kInitialPathReserve));
2121
2122
36
    for (size_t row = 0; row < num_rows; ++row) {
2123
25
        const size_t start = column_offsets[row - 1];
2124
25
        const size_t end = column_offsets[row];
2125
71
        for (size_t i = start; i < end; ++i) {
2126
46
            const auto& key = column_key->get_data_at(i);
2127
46
            const std::string_view path_sv(key.data, key.size);
2128
2129
46
            auto [it, inserted] =
2130
46
                    subcolumns.try_emplace(path_sv, ColumnVariant::Subcolumn {0, true, false});
2131
46
            auto& subcolumn = it->second;
2132
46
            if (inserted) {
2133
27
                subcolumn.insert_many_defaults(row);
2134
27
            } else if (subcolumn.size() != row) {
2135
4
                subcolumn.insert_many_defaults(row - subcolumn.size());
2136
4
            }
2137
46
            subcolumn.deserialize_from_binary_column(column_value, i);
2138
46
        }
2139
25
    }
2140
2141
27
    for (auto& [path, subcolumn] : subcolumns) {
2142
27
        if (subcolumn.size() != num_rows) {
2143
7
            subcolumn.insert_many_defaults(num_rows - subcolumn.size());
2144
7
        }
2145
27
    }
2146
2147
11
    return subcolumns;
2148
11
}
2149
2150
Status _parse_and_materialize_variant_columns(Block& block,
2151
                                              const std::vector<uint32_t>& variant_pos,
2152
1.44k
                                              const std::vector<ParseConfig>& configs) {
2153
3.15k
    for (size_t i = 0; i < variant_pos.size(); ++i) {
2154
1.70k
        auto column_ref = block.get_by_position(variant_pos[i]).column;
2155
1.70k
        bool is_nullable = column_ref->is_nullable();
2156
1.70k
        MutableColumnPtr owner_column = std::move(*column_ref).mutate();
2157
1.70k
        ColumnPtr nullable_null_map;
2158
1.70k
        MutableColumnPtr var_column;
2159
1.70k
        if (is_nullable) {
2160
1.33k
            const auto& nullable = assert_cast<const ColumnNullable&>(*owner_column);
2161
1.33k
            nullable_null_map = nullable.get_null_map_column_ptr();
2162
1.33k
            var_column = std::move(*nullable.get_nested_column_ptr()).mutate();
2163
1.33k
        } else {
2164
372
            var_column = std::move(owner_column);
2165
372
        }
2166
1.70k
        auto& var = assert_cast<ColumnVariant&>(*var_column);
2167
1.70k
        var_column->finalize();
2168
2169
1.70k
        MutableColumnPtr variant_column;
2170
1.70k
        if (!var.is_scalar_variant()) {
2171
            // already parsed
2172
318
            continue;
2173
318
        }
2174
2175
18.4E
        VLOG_DEBUG << "parse scalar variant column: " << var.get_root_type()->get_name();
2176
1.39k
        ColumnPtr scalar_root_column;
2177
1.39k
        if (var.get_root_type()->get_primitive_type() == TYPE_JSONB) {
2178
            // TODO more efficient way to parse jsonb type, currently we just convert jsonb to
2179
            // json str and parse them into variant
2180
3
            RETURN_IF_ERROR(cast_column({var.get_root(), var.get_root_type(), ""},
2181
3
                                        var.get_root()->is_nullable()
2182
3
                                                ? make_nullable(std::make_shared<DataTypeString>())
2183
3
                                                : std::make_shared<DataTypeString>(),
2184
3
                                        &scalar_root_column));
2185
3
            if (scalar_root_column->is_nullable()) {
2186
3
                scalar_root_column = assert_cast<const ColumnNullable*>(scalar_root_column.get())
2187
3
                                             ->get_nested_column_ptr();
2188
3
            }
2189
1.38k
        } else {
2190
1.38k
            const auto& root = *var.get_root();
2191
1.38k
            scalar_root_column =
2192
1.38k
                    root.is_nullable()
2193
1.38k
                            ? assert_cast<const ColumnNullable&>(root).get_nested_column_ptr()
2194
1.38k
                            : var.get_root();
2195
1.38k
        }
2196
2197
1.39k
        if (scalar_root_column->is_column_string()) {
2198
1.39k
            variant_column = ColumnVariant::create(0, var.enable_doc_mode());
2199
1.39k
            parse_json_to_variant(*variant_column.get(),
2200
1.39k
                                  assert_cast<const ColumnString&>(*scalar_root_column),
2201
1.39k
                                  configs[i]);
2202
18.4E
        } else {
2203
            // Root maybe other types rather than string like ColumnVariant(Int32).
2204
            // In this case, we should finlize the root and cast to JSON type
2205
18.4E
            auto expected_root_type =
2206
18.4E
                    make_nullable(std::make_shared<ColumnVariant::MostCommonType>());
2207
18.4E
            var.ensure_root_node_type(expected_root_type);
2208
18.4E
            variant_column = std::move(var_column);
2209
18.4E
        }
2210
2211
        // Wrap variant with nullmap if it is nullable
2212
1.39k
        ColumnPtr result = variant_column->get_ptr();
2213
1.39k
        if (is_nullable) {
2214
1.29k
            result = ColumnNullable::create(result, nullable_null_map);
2215
1.29k
        }
2216
1.39k
        block.get_by_position(variant_pos[i]).column = result;
2217
1.39k
    }
2218
1.44k
    return Status::OK();
2219
1.44k
}
2220
2221
Status parse_and_materialize_variant_columns(Block& block, const std::vector<uint32_t>& variant_pos,
2222
1.44k
                                             const std::vector<ParseConfig>& configs) {
2223
1.44k
    RETURN_IF_CATCH_EXCEPTION(
2224
1.44k
            { return _parse_and_materialize_variant_columns(block, variant_pos, configs); });
2225
1.44k
}
2226
2227
namespace {
2228
2229
ParseConfig::ParseTo select_storage_variant_parse_target(const TabletColumn& column,
2230
1.69k
                                                         const ParseConfig& config) {
2231
    // NestedGroup consumes the parse-time subcolumn tree to build nested storage structures, so it
2232
    // must not go through doc-value staging.
2233
1.69k
    if (column.variant_enable_nested_group()) {
2234
4
        return ParseConfig::ParseTo::OnlySubcolumns;
2235
4
    }
2236
2237
    // Persistent doc mode owns doc-value bucket columns in VariantDocWriter. Keep it separate from
2238
    // the plain non-doc staging optimization, even when typed paths or parent indexes exist.
2239
1.68k
    if (column.variant_enable_doc_mode()) {
2240
658
        return ParseConfig::ParseTo::OnlyDocValueColumn;
2241
658
    }
2242
2243
    // Deprecated flatten-nested still consumes parse-time subcolumns. Predefined typed paths and
2244
    // parent inverted indexes are handled later by regular doc-value staging: typed paths are
2245
    // forced into the materialized set unless typed-to-sparse is enabled, and materialized dynamic
2246
    // subcolumns inherit parent indexes while sparse payloads stay unindexed.
2247
1.03k
    if (config.deprecated_enable_flatten_nested) {
2248
16
        return ParseConfig::ParseTo::OnlySubcolumns;
2249
16
    }
2250
2251
    // Plain dynamic non-doc VARIANT can avoid eagerly creating thousands of parse-time subcolumns.
2252
    // The segment writer will pick the materialized/sparse split from this doc-value KV staging.
2253
    // Keep a BE switch so tests and rollouts can compare the old parse-time path with staging under
2254
    // the same writer and schema.
2255
1.01k
    switch (config::variant_storage_parse_mode) {
2256
1.01k
    case 0:
2257
1.01k
    case 2:
2258
1.01k
        return ParseConfig::ParseTo::OnlyDocValueColumn;
2259
2
    case 1:
2260
2
        return ParseConfig::ParseTo::OnlySubcolumns;
2261
0
    default:
2262
0
        CHECK(false) << "invalid variant_storage_parse_mode: "
2263
0
                     << config::variant_storage_parse_mode;
2264
0
        return ParseConfig::ParseTo::OnlyDocValueColumn;
2265
1.01k
    }
2266
1.01k
}
2267
2268
} // namespace
2269
2270
Status parse_and_materialize_variant_columns(Block& block, const TabletSchema& tablet_schema,
2271
1.44k
                                             const std::vector<uint32_t>& column_pos) {
2272
1.44k
    std::vector<uint32_t> variant_column_pos;
2273
1.44k
    std::vector<uint32_t> variant_schema_pos;
2274
1.44k
    variant_column_pos.reserve(column_pos.size());
2275
1.44k
    variant_schema_pos.reserve(column_pos.size());
2276
11.8k
    for (size_t block_pos = 0; block_pos < column_pos.size(); ++block_pos) {
2277
10.3k
        const uint32_t schema_pos = column_pos[block_pos];
2278
10.3k
        const auto& column = tablet_schema.column(schema_pos);
2279
10.3k
        if (column.is_variant_type()) {
2280
1.69k
            variant_column_pos.push_back(schema_pos);
2281
1.69k
            variant_schema_pos.push_back(schema_pos);
2282
1.69k
        }
2283
10.3k
    }
2284
2285
1.44k
    if (variant_column_pos.empty()) {
2286
14
        return Status::OK();
2287
14
    }
2288
2289
1.43k
    std::vector<ParseConfig> configs(variant_column_pos.size());
2290
3.13k
    for (size_t i = 0; i < variant_column_pos.size(); ++i) {
2291
        // Deprecated legacy flatten-nested switch. Distinct from variant_enable_nested_group.
2292
1.69k
        configs[i].deprecated_enable_flatten_nested =
2293
1.69k
                tablet_schema.deprecated_variant_flatten_nested();
2294
1.69k
        configs[i].check_duplicate_json_path = config::variant_enable_duplicate_json_path_check;
2295
1.69k
        const auto& column = tablet_schema.column(variant_schema_pos[i]);
2296
1.69k
        if (!column.is_variant_type()) {
2297
0
            return Status::InternalError("column is not variant type, column name: {}",
2298
0
                                         column.name());
2299
0
        }
2300
1.69k
        configs[i].parse_to = select_storage_variant_parse_target(column, configs[i]);
2301
1.69k
    }
2302
2303
1.43k
    RETURN_IF_ERROR(parse_and_materialize_variant_columns(block, variant_column_pos, configs));
2304
1.43k
    return Status::OK();
2305
1.43k
}
2306
2307
} // namespace doris::variant_util