Coverage Report

Created: 2026-04-10 06:20

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