be/src/exprs/table_function/vexplode_v2.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 "exprs/table_function/vexplode_v2.h" |
19 | | |
20 | | #include <glog/logging.h> |
21 | | |
22 | | #include <algorithm> |
23 | | #include <cstdint> |
24 | | #include <ostream> |
25 | | |
26 | | #include "common/status.h" |
27 | | #include "core/assert_cast.h" |
28 | | #include "core/block/block.h" |
29 | | #include "core/block/column_with_type_and_name.h" |
30 | | #include "core/column/column.h" |
31 | | #include "core/column/column_array.h" |
32 | | #include "core/column/column_nothing.h" |
33 | | #include "core/column/column_struct.h" |
34 | | #include "core/column/column_variant.h" |
35 | | #include "core/data_type/data_type.h" |
36 | | #include "core/data_type/data_type_array.h" |
37 | | #include "core/data_type/data_type_nothing.h" |
38 | | #include "core/data_type/primitive_type.h" |
39 | | #include "exprs/function/function_helpers.h" |
40 | | #include "exprs/vexpr.h" |
41 | | #include "exprs/vexpr_context.h" |
42 | | |
43 | | namespace doris { |
44 | | |
45 | 2.77k | VExplodeV2TableFunction::VExplodeV2TableFunction() { |
46 | 2.77k | _fn_name = "vexplode"; |
47 | 2.77k | } |
48 | | |
49 | | Status VExplodeV2TableFunction::_process_init_variant(Block* block, int value_column_idx, |
50 | 13 | int children_column_idx) { |
51 | | // explode variant array |
52 | 13 | auto column_without_nullable = remove_nullable(block->get_by_position(value_column_idx).column); |
53 | 13 | auto column = column_without_nullable->convert_to_full_column_if_const(); |
54 | 13 | auto& variant_column = assert_cast<ColumnVariant&>(*(column->assume_mutable())); |
55 | 13 | variant_column.finalize(); |
56 | 13 | _multi_detail[children_column_idx].output_as_variant = true; |
57 | 13 | _multi_detail[children_column_idx].variant_enable_doc_mode = variant_column.enable_doc_mode(); |
58 | 13 | if (!variant_column.is_null_root()) { |
59 | 13 | _array_columns[children_column_idx] = variant_column.get_root(); |
60 | | // We need to wrap the output nested column within a variant column. |
61 | | // Otherwise the type is missmatched |
62 | 13 | const auto* array_type = check_and_get_data_type<DataTypeArray>( |
63 | 13 | remove_nullable(variant_column.get_root_type()).get()); |
64 | 13 | if (array_type == nullptr) { |
65 | 0 | return Status::NotSupported("explode not support none array type {}", |
66 | 0 | variant_column.get_root_type()->get_name()); |
67 | 0 | } |
68 | 13 | _multi_detail[children_column_idx].nested_type = array_type->get_nested_type(); |
69 | 13 | } else { |
70 | | // null root, use nothing type |
71 | 0 | _array_columns[children_column_idx] = ColumnNullable::create( |
72 | 0 | ColumnArray::create(ColumnNothing::create(0)), ColumnUInt8::create(0)); |
73 | 0 | _array_columns[children_column_idx]->assume_mutable()->insert_many_defaults( |
74 | 0 | variant_column.size()); |
75 | 0 | _multi_detail[children_column_idx].nested_type = std::make_shared<DataTypeNothing>(); |
76 | 0 | } |
77 | 13 | return Status::OK(); |
78 | 13 | } |
79 | | |
80 | 930 | Status VExplodeV2TableFunction::process_init(Block* block, RuntimeState* state) { |
81 | 930 | auto expr_size = _expr_context->root()->children().size(); |
82 | 930 | CHECK(expr_size >= 1) << "VExplodeV2TableFunction support one or more child but has " |
83 | 0 | << expr_size; |
84 | | |
85 | 930 | int value_column_idx = -1; |
86 | 930 | _multi_detail.resize(expr_size); |
87 | 930 | _array_offsets.resize(expr_size); |
88 | 930 | _array_columns.resize(expr_size); |
89 | | |
90 | 1.98k | for (int i = 0; i < expr_size; i++) { |
91 | 1.05k | RETURN_IF_ERROR(_expr_context->root()->children()[i]->execute(_expr_context.get(), block, |
92 | 1.05k | &value_column_idx)); |
93 | 1.05k | if (block->get_by_position(value_column_idx).type->get_primitive_type() == TYPE_VARIANT) { |
94 | 13 | RETURN_IF_ERROR(_process_init_variant(block, value_column_idx, i)); |
95 | 1.04k | } else { |
96 | 1.04k | _array_columns[i] = block->get_by_position(value_column_idx) |
97 | 1.04k | .column->convert_to_full_column_if_const(); |
98 | 1.04k | } |
99 | 1.05k | if (!extract_column_array_info(*_array_columns[i], _multi_detail[i])) { |
100 | 0 | return Status::NotSupported( |
101 | 0 | "column type {} not supported now", |
102 | 0 | block->get_by_position(value_column_idx).column->get_name()); |
103 | 0 | } |
104 | 1.05k | } |
105 | | |
106 | 930 | return Status::OK(); |
107 | 930 | } |
108 | | |
109 | 1.75k | bool VExplodeV2TableFunction::support_block_fast_path() const { |
110 | 1.75k | return _multi_detail.size() == 1; |
111 | 1.75k | } |
112 | | |
113 | | Status VExplodeV2TableFunction::prepare_block_fast_path(Block* /*block*/, RuntimeState* /*state*/, |
114 | 728 | BlockFastPathContext* ctx) { |
115 | 728 | DCHECK(support_block_fast_path()); |
116 | 728 | const auto& detail = _multi_detail[0]; |
117 | 728 | if (detail.offsets_ptr == nullptr || detail.nested_col.get() == nullptr) { |
118 | 0 | return Status::InternalError("vexplode block fast path not initialized"); |
119 | 0 | } |
120 | 728 | ctx->array_nullmap_data = detail.array_nullmap_data; |
121 | 728 | ctx->offsets_ptr = detail.offsets_ptr; |
122 | 728 | ctx->nested_col = detail.nested_col; |
123 | 728 | ctx->nested_nullmap_data = detail.nested_nullmap_data; |
124 | 728 | ctx->generate_row_index = _generate_row_index; |
125 | 728 | return Status::OK(); |
126 | 728 | } |
127 | | |
128 | 1.07k | void VExplodeV2TableFunction::process_row(size_t row_idx) { |
129 | 1.07k | TableFunction::process_row(row_idx); |
130 | | |
131 | 2.41k | for (int i = 0; i < _multi_detail.size(); i++) { |
132 | 1.34k | auto& detail = _multi_detail[i]; |
133 | 1.34k | if (!detail.array_nullmap_data || !detail.array_nullmap_data[row_idx]) { |
134 | 1.23k | _array_offsets[i] = (*detail.offsets_ptr)[row_idx - 1]; |
135 | | // find max size in array |
136 | 1.23k | auto cur_size = (*detail.offsets_ptr)[row_idx] - _array_offsets[i]; |
137 | 1.23k | _cur_size = std::max<unsigned long>(_cur_size, cur_size); |
138 | 1.23k | } |
139 | 1.34k | } |
140 | 1.07k | _row_idx = row_idx; |
141 | 1.07k | } |
142 | | |
143 | 919 | void VExplodeV2TableFunction::process_close() { |
144 | 919 | _multi_detail.clear(); |
145 | 919 | _array_offsets.clear(); |
146 | 919 | _array_columns.clear(); |
147 | 919 | _row_idx = 0; |
148 | 919 | } |
149 | | |
150 | 31 | void VExplodeV2TableFunction::get_same_many_values(MutableColumnPtr& column, int length) { |
151 | 31 | if (current_empty()) { |
152 | 6 | column->insert_many_defaults(length); |
153 | 6 | return; |
154 | 6 | } |
155 | 25 | ColumnStruct* struct_column = nullptr; |
156 | 25 | std::vector<IColumn*> columns; |
157 | | |
158 | 25 | const bool multi_sub_columns = _multi_detail.size() > 1 || _generate_row_index; |
159 | | |
160 | 25 | if (multi_sub_columns) { |
161 | 9 | if (_is_nullable) { |
162 | 9 | auto* nullable_column = assert_cast<ColumnNullable*>(column.get()); |
163 | 9 | struct_column = |
164 | 9 | assert_cast<ColumnStruct*>(nullable_column->get_nested_column_ptr().get()); |
165 | 9 | auto* nullmap_column = |
166 | 9 | assert_cast<ColumnUInt8*>(nullable_column->get_null_map_column_ptr().get()); |
167 | 9 | nullmap_column->insert_many_defaults(length); |
168 | | |
169 | 9 | } else { |
170 | 0 | struct_column = assert_cast<ColumnStruct*>(column.get()); |
171 | 0 | } |
172 | | |
173 | 27 | for (size_t i = 0; i != _multi_detail.size(); ++i) { |
174 | 18 | columns.emplace_back(&struct_column->get_column(i + (_generate_row_index ? 1 : 0))); |
175 | 18 | } |
176 | 16 | } else { |
177 | 16 | columns.push_back(column.get()); |
178 | 16 | } |
179 | | |
180 | 25 | if (_generate_row_index) { |
181 | 0 | auto& pos_column = assert_cast<ColumnInt32&>(struct_column->get_column(0)); |
182 | 0 | pos_column.insert_many_vals(static_cast<int32_t>(_cur_offset), length); |
183 | 0 | } |
184 | | |
185 | 59 | for (int i = 0; i < _multi_detail.size(); i++) { |
186 | 34 | auto& detail = _multi_detail[i]; |
187 | 34 | size_t pos = _array_offsets[i] + _cur_offset; |
188 | 34 | size_t element_size = _multi_detail[i].array_col->size_at(_row_idx); |
189 | 34 | auto& struct_field = *columns.at(i); |
190 | 34 | if ((detail.array_nullmap_data && detail.array_nullmap_data[_row_idx])) { |
191 | 3 | struct_field.insert_many_defaults(length); |
192 | 31 | } else { |
193 | 31 | auto* nullable_column = assert_cast<ColumnNullable*>(struct_field.get_ptr().get()); |
194 | 31 | auto* nullmap_column = |
195 | 31 | assert_cast<ColumnUInt8*>(nullable_column->get_null_map_column_ptr().get()); |
196 | | // only need to check if the value at position pos is null |
197 | 31 | if (element_size < _cur_offset || |
198 | 31 | (detail.nested_nullmap_data && detail.nested_nullmap_data[pos])) { |
199 | 3 | nullable_column->insert_many_defaults(length); |
200 | 28 | } else { |
201 | 28 | nullable_column->get_nested_column_ptr()->insert_many_from(*detail.nested_col, pos, |
202 | 28 | length); |
203 | 28 | nullmap_column->insert_many_defaults(length); |
204 | 28 | } |
205 | 31 | } |
206 | 34 | } |
207 | 25 | } |
208 | | |
209 | 414 | int VExplodeV2TableFunction::get_value(MutableColumnPtr& column, int max_step) { |
210 | 414 | max_step = std::min(max_step, (int)(_cur_size - _cur_offset)); |
211 | 414 | const bool multi_sub_columns = _multi_detail.size() > 1 || _generate_row_index; |
212 | | |
213 | 414 | ColumnStruct* struct_column = nullptr; |
214 | 414 | std::vector<IColumn*> columns; |
215 | | |
216 | 414 | if (current_empty()) { |
217 | 19 | column->insert_default(); |
218 | 19 | max_step = 1; |
219 | 395 | } else { |
220 | 395 | if (multi_sub_columns) { |
221 | 261 | if (_is_nullable) { |
222 | 197 | auto* nullable_column = assert_cast<ColumnNullable*>(column.get()); |
223 | 197 | struct_column = |
224 | 197 | assert_cast<ColumnStruct*>(nullable_column->get_nested_column_ptr().get()); |
225 | 197 | auto* nullmap_column = |
226 | 197 | assert_cast<ColumnUInt8*>(nullable_column->get_null_map_column_ptr().get()); |
227 | 197 | nullmap_column->insert_many_defaults(max_step); |
228 | | |
229 | 197 | } else { |
230 | 64 | struct_column = assert_cast<ColumnStruct*>(column.get()); |
231 | 64 | } |
232 | | |
233 | 864 | for (size_t i = 0; i != _multi_detail.size(); ++i) { |
234 | 603 | columns.emplace_back(&struct_column->get_column(i + (_generate_row_index ? 1 : 0))); |
235 | 603 | } |
236 | 261 | } else { |
237 | 134 | columns.emplace_back(column.get()); |
238 | 134 | } |
239 | | |
240 | 395 | if (_generate_row_index) { |
241 | 151 | auto& pos_column = assert_cast<ColumnInt32&>(struct_column->get_column(0)); |
242 | 151 | pos_column.insert_range_of_integer(static_cast<int32_t>(_cur_offset), |
243 | 151 | static_cast<int32_t>(_cur_offset + max_step)); |
244 | 151 | } |
245 | | |
246 | 1.13k | for (int i = 0; i < _multi_detail.size(); i++) { |
247 | 737 | auto& detail = _multi_detail[i]; |
248 | 737 | size_t pos = _array_offsets[i] + _cur_offset; |
249 | 737 | size_t element_size = _multi_detail[i].array_col->size_at(_row_idx); |
250 | 737 | auto& struct_field = *columns.at(i); |
251 | 737 | if (detail.array_nullmap_data && detail.array_nullmap_data[_row_idx]) { |
252 | 42 | struct_field.insert_many_defaults(max_step); |
253 | 695 | } else { |
254 | 695 | auto* nullable_column = assert_cast<ColumnNullable*>(struct_field.get_ptr().get()); |
255 | 695 | auto* nullmap_column = |
256 | 695 | assert_cast<ColumnUInt8*>(nullable_column->get_null_map_column_ptr().get()); |
257 | 695 | if (element_size >= _cur_offset + max_step) { |
258 | 520 | nullable_column->get_nested_column_ptr()->insert_range_from(*detail.nested_col, |
259 | 520 | pos, max_step); |
260 | 520 | if (detail.nested_nullmap_data) { |
261 | 520 | size_t old_size = nullmap_column->size(); |
262 | 520 | nullmap_column->resize(old_size + max_step); |
263 | 520 | memcpy(nullmap_column->get_data().data() + old_size, |
264 | 520 | detail.nested_nullmap_data + pos, max_step * sizeof(UInt8)); |
265 | 520 | } else { |
266 | 0 | nullmap_column->insert_many_defaults(max_step); |
267 | 0 | } |
268 | 520 | } else if (element_size > _cur_offset) { |
269 | 127 | auto current_insert_num = element_size - _cur_offset; |
270 | 127 | nullable_column->get_nested_column_ptr()->insert_range_from( |
271 | 127 | *detail.nested_col, pos, current_insert_num); |
272 | 127 | if (detail.nested_nullmap_data) { |
273 | 127 | size_t old_size = nullmap_column->size(); |
274 | 127 | nullmap_column->resize(old_size + current_insert_num); |
275 | 127 | memcpy(nullmap_column->get_data().data() + old_size, |
276 | 127 | detail.nested_nullmap_data + pos, |
277 | 127 | current_insert_num * sizeof(UInt8)); |
278 | 127 | } else { |
279 | 0 | nullmap_column->insert_many_defaults(current_insert_num); |
280 | 0 | } |
281 | 127 | nullable_column->insert_many_defaults(max_step - current_insert_num); |
282 | 127 | } else { |
283 | 48 | nullable_column->insert_many_defaults(max_step); |
284 | 48 | } |
285 | 695 | } |
286 | 737 | } |
287 | 395 | } |
288 | | |
289 | 414 | forward(max_step); |
290 | 414 | return max_step; |
291 | 414 | } |
292 | | |
293 | | } // namespace doris |