Coverage Report

Created: 2026-03-20 09:16

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