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