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