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