Coverage Report

Created: 2026-03-31 21:11

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