/root/doris/be/src/vec/utils/util.hpp
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 | | #pragma once |
19 | | |
20 | | #include <thrift/protocol/TJSONProtocol.h> |
21 | | |
22 | | #include <boost/shared_ptr.hpp> |
23 | | |
24 | | #include "runtime/descriptors.h" |
25 | | #include "util/simd/bits.h" |
26 | | #include "vec/columns/column.h" |
27 | | #include "vec/columns/column_nullable.h" |
28 | | #include "vec/core/block.h" |
29 | | #include "vec/exprs/vexpr.h" |
30 | | #include "vec/exprs/vexpr_context.h" |
31 | | |
32 | | namespace doris::vectorized { |
33 | | class VectorizedUtils { |
34 | | public: |
35 | 48.4k | static Block create_empty_columnswithtypename(const RowDescriptor& row_desc) { |
36 | | // Block block; |
37 | 48.4k | return create_columns_with_type_and_name(row_desc); |
38 | 48.4k | } |
39 | 32 | static MutableBlock build_mutable_mem_reuse_block(Block* block, const RowDescriptor& row_desc) { |
40 | 32 | if (!block->mem_reuse()) { |
41 | 32 | MutableBlock tmp(VectorizedUtils::create_columns_with_type_and_name(row_desc)); |
42 | 32 | block->swap(tmp.to_block()); |
43 | 32 | } |
44 | 32 | return MutableBlock::build_mutable_block(block); |
45 | 32 | } |
46 | 550 | static MutableBlock build_mutable_mem_reuse_block(Block* block, const Block& other) { |
47 | 550 | if (!block->mem_reuse()) { |
48 | 538 | MutableBlock tmp(other.clone_empty()); |
49 | 538 | block->swap(tmp.to_block()); |
50 | 538 | } |
51 | 550 | return MutableBlock::build_mutable_block(block); |
52 | 550 | } |
53 | | static MutableBlock build_mutable_mem_reuse_block(Block* block, |
54 | 11 | const std::vector<SlotDescriptor*>& slots) { |
55 | 11 | if (!block->mem_reuse()) { |
56 | 11 | size_t column_size = slots.size(); |
57 | 11 | MutableColumns columns(column_size); |
58 | 48 | for (size_t i = 0; i < column_size; i++) { |
59 | 37 | columns[i] = slots[i]->get_empty_mutable_column(); |
60 | 37 | } |
61 | 11 | int n_columns = 0; |
62 | 37 | for (const auto slot_desc : slots) { |
63 | 37 | block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]), |
64 | 37 | slot_desc->get_data_type_ptr(), |
65 | 37 | slot_desc->col_name())); |
66 | 37 | } |
67 | 11 | } |
68 | 11 | return MutableBlock(block); |
69 | 11 | } |
70 | | |
71 | 48.5k | static ColumnsWithTypeAndName create_columns_with_type_and_name(const RowDescriptor& row_desc) { |
72 | 48.5k | ColumnsWithTypeAndName columns_with_type_and_name; |
73 | 48.5k | for (const auto& tuple_desc : row_desc.tuple_descriptors()) { |
74 | 94.7k | for (const auto& slot_desc : tuple_desc->slots()) { |
75 | 94.7k | if (!slot_desc->is_materialized()) { |
76 | 0 | continue; |
77 | 0 | } |
78 | 94.7k | columns_with_type_and_name.emplace_back(nullptr, slot_desc->get_data_type_ptr(), |
79 | 94.7k | slot_desc->col_name()); |
80 | 94.7k | } |
81 | 48.5k | } |
82 | 48.5k | return columns_with_type_and_name; |
83 | 48.5k | } |
84 | | |
85 | 24.0k | static NameAndTypePairs create_name_and_data_types(const RowDescriptor& row_desc) { |
86 | 24.0k | NameAndTypePairs name_with_types; |
87 | 24.0k | for (const auto& tuple_desc : row_desc.tuple_descriptors()) { |
88 | 65.9k | for (const auto& slot_desc : tuple_desc->slots()) { |
89 | 65.9k | if (!slot_desc->is_materialized()) { |
90 | 0 | continue; |
91 | 0 | } |
92 | 65.9k | name_with_types.emplace_back(slot_desc->col_name(), slot_desc->get_data_type_ptr()); |
93 | 65.9k | } |
94 | 24.0k | } |
95 | 24.0k | return name_with_types; |
96 | 24.0k | } |
97 | | |
98 | | static ColumnsWithTypeAndName create_empty_block(const RowDescriptor& row_desc, |
99 | 869 | bool ignore_trivial_slot = true) { |
100 | 869 | ColumnsWithTypeAndName columns_with_type_and_name; |
101 | 869 | for (const auto& tuple_desc : row_desc.tuple_descriptors()) { |
102 | 879 | for (const auto& slot_desc : tuple_desc->slots()) { |
103 | 879 | if (ignore_trivial_slot && !slot_desc->is_materialized()) { |
104 | 0 | continue; |
105 | 0 | } |
106 | 879 | columns_with_type_and_name.emplace_back( |
107 | 879 | slot_desc->get_data_type_ptr()->create_column(), |
108 | 879 | slot_desc->get_data_type_ptr(), slot_desc->col_name()); |
109 | 879 | } |
110 | 869 | } |
111 | 869 | return columns_with_type_and_name; |
112 | 869 | } |
113 | | |
114 | | // Helper function to extract null map from column (including ColumnConst cases) |
115 | 67 | static const NullMap* get_null_map(const ColumnPtr& col) { |
116 | 67 | if (col->is_nullable()) { |
117 | 67 | return &static_cast<const ColumnNullable&>(*col).get_null_map_data(); |
118 | 67 | } |
119 | | // Handle Const(Nullable) case |
120 | 0 | if (const auto* const_col = check_and_get_column<ColumnConst>(col.get()); |
121 | 0 | const_col != nullptr && const_col->is_concrete_nullable()) { |
122 | 0 | return &static_cast<const ColumnNullable&>(const_col->get_data_column()) |
123 | 0 | .get_null_map_data(); |
124 | 0 | } |
125 | 0 | return nullptr; |
126 | 0 | }; |
127 | | |
128 | | // is_single: whether src is null map of a ColumnConst |
129 | 113k | static void update_null_map(NullMap& dst, const NullMap& src, bool is_single = false) { |
130 | 113k | size_t size = dst.size(); |
131 | 113k | auto* __restrict l = dst.data(); |
132 | 113k | auto* __restrict r = src.data(); |
133 | 113k | if (is_single) { |
134 | 0 | if (r[0]) { |
135 | 0 | for (size_t i = 0; i < size; ++i) { |
136 | 0 | l[i] = 1; |
137 | 0 | } |
138 | 0 | } |
139 | 113k | } else { |
140 | 456k | for (size_t i = 0; i < size; ++i) { |
141 | 342k | l[i] |= r[i]; |
142 | 342k | } |
143 | 113k | } |
144 | 113k | } |
145 | | |
146 | 48.0k | static DataTypes get_data_types(const RowDescriptor& row_desc) { |
147 | 48.0k | DataTypes data_types; |
148 | 48.0k | for (const auto& tuple_desc : row_desc.tuple_descriptors()) { |
149 | 94.2k | for (const auto& slot_desc : tuple_desc->slots()) { |
150 | 94.2k | data_types.push_back(slot_desc->get_data_type_ptr()); |
151 | 94.2k | } |
152 | 48.0k | } |
153 | 48.0k | return data_types; |
154 | 48.0k | } |
155 | | |
156 | 24.0k | static std::vector<std::string> get_column_names(const RowDescriptor& row_desc) { |
157 | 24.0k | std::vector<std::string> column_names; |
158 | 24.0k | for (const auto& tuple_desc : row_desc.tuple_descriptors()) { |
159 | 47.1k | for (const auto& slot_desc : tuple_desc->slots()) { |
160 | 47.1k | column_names.push_back(slot_desc->col_name()); |
161 | 47.1k | } |
162 | 24.0k | } |
163 | 24.0k | return column_names; |
164 | 24.0k | } |
165 | | |
166 | 105k | static bool all_arguments_are_constant(const Block& block, const ColumnNumbers& args) { |
167 | 118k | for (const auto& arg : args) { |
168 | 118k | if (!is_column_const(*block.get_by_position(arg).column)) { |
169 | 103k | return false; |
170 | 103k | } |
171 | 118k | } |
172 | 2.44k | return true; |
173 | 105k | } |
174 | | |
175 | | // SIMD helper: find the first not null in the null map |
176 | 0 | static size_t find_first_valid_simd(const NullMap& null_map, size_t start_pos, size_t end_pos) { |
177 | 0 | #ifdef __AVX2__ |
178 | | // search by simd |
179 | 0 | for (size_t pos = start_pos; pos + 31 < end_pos; pos += 32) { |
180 | 0 | __m256i null_vec = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&null_map[pos])); |
181 | 0 | __m256i zero_vec = _mm256_setzero_si256(); |
182 | 0 | __m256i cmp_result = _mm256_cmpeq_epi8(null_vec, zero_vec); |
183 | |
|
184 | 0 | int mask = _mm256_movemask_epi8(cmp_result); |
185 | 0 | if (mask != 0) { |
186 | | // find the first not null |
187 | 0 | return pos + __builtin_ctz(mask); |
188 | 0 | } |
189 | 0 | } |
190 | | |
191 | | // handle the rest elements |
192 | 0 | for (size_t pos = start_pos + ((end_pos - start_pos) / 32) * 32; pos < end_pos; ++pos) { |
193 | 0 | if (!null_map[pos]) { |
194 | 0 | return pos; |
195 | 0 | } |
196 | 0 | } |
197 | | #else |
198 | | // standard implementation |
199 | | for (size_t pos = start_pos; pos < end_pos; ++pos) { |
200 | | if (!null_map[pos]) { |
201 | | return pos; |
202 | | } |
203 | | } |
204 | | #endif |
205 | 0 | return end_pos; |
206 | 0 | } |
207 | | |
208 | | // SIMD helper: batch set non-null value with [start, end) to 1 |
209 | | static void range_set_nullmap_to_true_simd(NullMap& null_map, size_t start_pos, |
210 | 0 | size_t end_pos) { |
211 | 0 | #ifdef __AVX2__ |
212 | | // batch set null map to 1 using SIMD (32 bytes at a time) |
213 | 0 | for (size_t pos = start_pos; pos + 31 < end_pos; pos += 32) { |
214 | 0 | __m256i ones_vec = _mm256_set1_epi8(1); |
215 | 0 | _mm256_storeu_si256(reinterpret_cast<__m256i*>(&null_map[pos]), ones_vec); |
216 | 0 | } |
217 | | |
218 | | // handle the rest elements (less than 32 bytes) |
219 | 0 | for (size_t pos = start_pos + ((end_pos - start_pos) / 32) * 32; pos < end_pos; ++pos) { |
220 | 0 | null_map[pos] = 1; |
221 | 0 | } |
222 | | #else |
223 | | // standard implementation |
224 | | for (size_t pos = start_pos; pos < end_pos; ++pos) { |
225 | | null_map[pos] = 1; |
226 | | } |
227 | | #endif |
228 | 0 | } |
229 | | }; |
230 | | |
231 | 0 | inline bool match_suffix(const std::string& name, const std::string& suffix) { |
232 | 0 | if (name.length() < suffix.length()) { |
233 | 0 | return false; |
234 | 0 | } |
235 | 0 | return name.substr(name.length() - suffix.length()) == suffix; |
236 | 0 | } |
237 | | |
238 | 0 | inline std::string remove_suffix(const std::string& name, const std::string& suffix) { |
239 | 0 | CHECK(match_suffix(name, suffix)) |
240 | 0 | << ", suffix not match, name=" << name << ", suffix=" << suffix; |
241 | 0 | return name.substr(0, name.length() - suffix.length()); |
242 | 0 | }; |
243 | | |
244 | 0 | inline ColumnPtr create_always_true_column(size_t size, bool is_nullable) { |
245 | 0 | ColumnPtr res_data_column = ColumnUInt8::create(1, 1); |
246 | 0 | if (is_nullable) { |
247 | 0 | auto null_map = ColumnUInt8::create(1, 0); |
248 | 0 | res_data_column = ColumnNullable::create(res_data_column, std::move(null_map)); |
249 | 0 | } |
250 | 0 | return ColumnConst::create(std::move(res_data_column), size); |
251 | 0 | } |
252 | | |
253 | | // change null element to true element |
254 | 0 | inline void change_null_to_true(MutableColumnPtr column, ColumnPtr argument = nullptr) { |
255 | 0 | size_t rows = column->size(); |
256 | 0 | if (is_column_const(*column)) { |
257 | 0 | change_null_to_true( |
258 | 0 | assert_cast<ColumnConst*>(column.get())->get_data_column_ptr()->assume_mutable()); |
259 | 0 | } else if (column->has_null()) { |
260 | 0 | auto* nullable = assert_cast<ColumnNullable*>(column.get()); |
261 | 0 | auto* __restrict data = assert_cast<ColumnUInt8*>(nullable->get_nested_column_ptr().get()) |
262 | 0 | ->get_data() |
263 | 0 | .data(); |
264 | 0 | const NullMap& null_map = nullable->get_null_map_data(); |
265 | 0 | for (size_t i = 0; i < rows; ++i) { |
266 | 0 | data[i] |= null_map[i]; |
267 | 0 | } |
268 | 0 | nullable->fill_false_to_nullmap(rows); |
269 | 0 | } else if (argument && argument->has_null()) { |
270 | 0 | const auto* __restrict null_map = |
271 | 0 | assert_cast<const ColumnNullable*>(argument.get())->get_null_map_data().data(); |
272 | 0 | auto* __restrict data = assert_cast<ColumnUInt8*>(column.get())->get_data().data(); |
273 | 0 | for (size_t i = 0; i < rows; ++i) { |
274 | 0 | data[i] |= null_map[i]; |
275 | 0 | } |
276 | 0 | } |
277 | 0 | } |
278 | | |
279 | 0 | inline size_t calculate_false_number(ColumnPtr column) { |
280 | 0 | size_t rows = column->size(); |
281 | 0 | if (is_column_const(*column)) { |
282 | 0 | return calculate_false_number( |
283 | 0 | assert_cast<const ColumnConst*>(column.get())->get_data_column_ptr()) * |
284 | 0 | rows; |
285 | 0 | } else if (column->is_nullable()) { |
286 | 0 | const auto* nullable = assert_cast<const ColumnNullable*>(column.get()); |
287 | 0 | const auto* data = assert_cast<const ColumnUInt8*>(nullable->get_nested_column_ptr().get()) |
288 | 0 | ->get_data() |
289 | 0 | .data(); |
290 | 0 | const auto* __restrict null_map = nullable->get_null_map_data().data(); |
291 | 0 | return simd::count_zero_num(reinterpret_cast<const int8_t* __restrict>(data), null_map, |
292 | 0 | rows); |
293 | 0 | } else { |
294 | 0 | const auto* data = assert_cast<const ColumnUInt8*>(column.get())->get_data().data(); |
295 | 0 | return simd::count_zero_num(reinterpret_cast<const int8_t* __restrict>(data), rows); |
296 | 0 | } |
297 | 0 | } |
298 | | |
299 | | template <typename T> |
300 | 89 | T read_from_json(std::string& json_str) { |
301 | 89 | auto memBufferIn = std::make_shared<apache::thrift::transport::TMemoryBuffer>( |
302 | 89 | reinterpret_cast<uint8_t*>(json_str.data()), static_cast<uint32_t>(json_str.size())); |
303 | 89 | auto jsonProtocolIn = std::make_shared<apache::thrift::protocol::TJSONProtocol>(memBufferIn); |
304 | 89 | T params; |
305 | 89 | params.read(jsonProtocolIn.get()); |
306 | 89 | return params; |
307 | 89 | } _ZN5doris10vectorized14read_from_jsonINS_5TExprEEET_RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE Line | Count | Source | 300 | 83 | T read_from_json(std::string& json_str) { | 301 | 83 | auto memBufferIn = std::make_shared<apache::thrift::transport::TMemoryBuffer>( | 302 | 83 | reinterpret_cast<uint8_t*>(json_str.data()), static_cast<uint32_t>(json_str.size())); | 303 | 83 | auto jsonProtocolIn = std::make_shared<apache::thrift::protocol::TJSONProtocol>(memBufferIn); | 304 | 83 | T params; | 305 | 83 | params.read(jsonProtocolIn.get()); | 306 | 83 | return params; | 307 | 83 | } |
_ZN5doris10vectorized14read_from_jsonINS_16TDescriptorTableEEET_RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE Line | Count | Source | 300 | 6 | T read_from_json(std::string& json_str) { | 301 | 6 | auto memBufferIn = std::make_shared<apache::thrift::transport::TMemoryBuffer>( | 302 | 6 | reinterpret_cast<uint8_t*>(json_str.data()), static_cast<uint32_t>(json_str.size())); | 303 | 6 | auto jsonProtocolIn = std::make_shared<apache::thrift::protocol::TJSONProtocol>(memBufferIn); | 304 | 6 | T params; | 305 | 6 | params.read(jsonProtocolIn.get()); | 306 | 6 | return params; | 307 | 6 | } |
|
308 | | |
309 | | } // namespace doris::vectorized |
310 | | |
311 | | namespace apache::thrift { |
312 | | template <typename ThriftStruct> |
313 | 14 | ThriftStruct from_json_string(const std::string& json_val) { |
314 | 14 | using namespace apache::thrift::transport; |
315 | 14 | using namespace apache::thrift::protocol; |
316 | 14 | ThriftStruct ts; |
317 | 14 | std::shared_ptr<TTransport> trans = |
318 | 14 | std::make_shared<TMemoryBuffer>((uint8_t*)json_val.c_str(), (uint32_t)json_val.size()); |
319 | 14 | TJSONProtocol protocol(trans); |
320 | 14 | ts.read(&protocol); |
321 | 14 | return ts; |
322 | 14 | } |
323 | | |
324 | | } // namespace apache::thrift |