Coverage Report

Created: 2026-03-23 06:05

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