Coverage Report

Created: 2026-04-16 16:35

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