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