Coverage Report

Created: 2026-07-29 18:07

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
#include <span>
26
27
#include "common/status.h"
28
#include "core/assert_cast.h"
29
#include "core/block/block.h"
30
#include "core/block/column_with_type_and_name.h"
31
#include "core/column/column.h"
32
#include "core/column/column_array.h"
33
#include "core/column/column_nothing.h"
34
#include "core/column/column_struct.h"
35
#include "core/column/column_variant.h"
36
#include "core/column/variant_v2/column_variant_v2.h"
37
#include "core/data_type/data_type.h"
38
#include "core/data_type/data_type_array.h"
39
#include "core/data_type/data_type_nothing.h"
40
#include "core/data_type/data_type_nullable.h"
41
#include "core/data_type/data_type_variant_v2.h"
42
#include "core/data_type/primitive_type.h"
43
#include "exprs/function/cast/variant_v2/cast_variant_v2_internal.h"
44
#include "exprs/function/function_helpers.h"
45
#include "exprs/vexpr.h"
46
#include "exprs/vexpr_context.h"
47
48
namespace doris {
49
50
1.92k
VExplodeV2TableFunction::VExplodeV2TableFunction() {
51
1.92k
    _fn_name = "vexplode";
52
1.92k
}
53
54
Status VExplodeV2TableFunction::_process_init_variant(Block* block, int value_column_idx,
55
13
                                                      int children_column_idx) {
56
    // explode variant array
57
13
    auto materialized =
58
13
            block->get_by_position(value_column_idx).column->convert_to_full_column_if_const();
59
13
    std::span<const uint8_t> outer_nulls;
60
13
    const IColumn* nested = materialized.get();
61
13
    if (const auto* nullable = check_and_get_column<ColumnNullable>(nested)) {
62
13
        outer_nulls = nullable->get_null_map_data();
63
13
        nested = &nullable->get_nested_column();
64
13
    }
65
13
    if (const auto* variant_v2 = check_and_get_column<ColumnVariantV2>(nested)) {
66
0
        auto variant_type = std::make_shared<DataTypeVariantV2>();
67
0
        auto target_type = std::make_shared<DataTypeArray>(variant_type);
68
0
        ColumnPtr array_column;
69
0
        RETURN_IF_ERROR(CastWrapper::variant_v2_internal::cast_variant_to_array(
70
0
                nullptr, *variant_v2, target_type, variant_v2->size(), outer_nulls, &array_column));
71
0
        _array_columns[children_column_idx] = std::move(array_column);
72
0
        auto& detail = _multi_detail[children_column_idx];
73
0
        detail.output_as_variant = true;
74
0
        detail.nested_type = make_nullable(std::move(variant_type));
75
0
        _variant_v2_outputs[children_column_idx] = true;
76
0
        _has_variant_v2_output = true;
77
0
        return Status::OK();
78
0
    }
79
80
13
    auto column = remove_nullable(materialized);
81
13
    auto variant_column_ptr = IColumn::mutate(std::move(column));
82
13
    auto& variant_column = assert_cast<ColumnVariant&>(*variant_column_ptr);
83
13
    variant_column.finalize();
84
13
    _multi_detail[children_column_idx].output_as_variant = true;
85
13
    _multi_detail[children_column_idx].variant_enable_doc_mode = variant_column.enable_doc_mode();
86
13
    if (!variant_column.is_null_root()) {
87
13
        _array_columns[children_column_idx] = variant_column.get_root();
88
        // We need to wrap the output nested column within a variant column.
89
        // Otherwise the type is missmatched
90
13
        const auto* array_type = check_and_get_data_type<DataTypeArray>(
91
13
                remove_nullable(variant_column.get_root_type()).get());
92
13
        if (array_type == nullptr) {
93
0
            return Status::NotSupported("explode not support none array type {}",
94
0
                                        variant_column.get_root_type()->get_name());
95
0
        }
96
13
        _multi_detail[children_column_idx].nested_type = array_type->get_nested_type();
97
13
    } else {
98
        // null root, use nothing type
99
0
        auto array_column = ColumnNullable::create(ColumnArray::create(ColumnNothing::create(0)),
100
0
                                                   ColumnUInt8::create(0));
101
0
        array_column->insert_many_defaults(variant_column.size());
102
0
        _array_columns[children_column_idx] = std::move(array_column);
103
0
        _multi_detail[children_column_idx].nested_type = std::make_shared<DataTypeNothing>();
104
0
    }
105
13
    return Status::OK();
106
13
}
107
108
956
Status VExplodeV2TableFunction::process_init(Block* block, RuntimeState* state) {
109
956
    auto expr_size = _expr_context->root()->children().size();
110
18.4E
    CHECK(expr_size >= 1) << "VExplodeV2TableFunction support one or more child but has "
111
18.4E
                          << expr_size;
112
113
956
    int value_column_idx = -1;
114
956
    _multi_detail.resize(expr_size);
115
956
    _array_offsets.resize(expr_size);
116
956
    _array_columns.resize(expr_size);
117
956
    _variant_v2_outputs.assign(expr_size, false);
118
119
2.04k
    for (int i = 0; i < expr_size; i++) {
120
1.08k
        RETURN_IF_ERROR(_expr_context->root()->children()[i]->execute(_expr_context.get(), block,
121
1.08k
                                                                      &value_column_idx));
122
1.08k
        if (block->get_by_position(value_column_idx).type->get_primitive_type() == TYPE_VARIANT) {
123
13
            RETURN_IF_ERROR(_process_init_variant(block, value_column_idx, i));
124
1.07k
        } else {
125
1.07k
            _array_columns[i] = block->get_by_position(value_column_idx)
126
1.07k
                                        .column->convert_to_full_column_if_const();
127
1.07k
        }
128
1.08k
        if (!extract_column_array_info(*_array_columns[i], _multi_detail[i])) {
129
0
            return Status::NotSupported(
130
0
                    "column type {} not supported now",
131
0
                    block->get_by_position(value_column_idx).column->get_name());
132
0
        }
133
1.08k
        if (check_and_get_column<ColumnVariantV2>(_multi_detail[i].nested_col.get()) != nullptr) {
134
0
            _variant_v2_outputs[i] = true;
135
0
            _has_variant_v2_output = true;
136
0
        }
137
1.08k
    }
138
139
956
    return Status::OK();
140
956
}
141
142
1.81k
bool VExplodeV2TableFunction::support_block_fast_path() const {
143
1.81k
    return _multi_detail.size() == 1 && !_has_variant_v2_output;
144
1.81k
}
145
146
Status VExplodeV2TableFunction::prepare_block_fast_path(Block* /*block*/, RuntimeState* /*state*/,
147
754
                                                        BlockFastPathContext* ctx) {
148
754
    DCHECK(support_block_fast_path());
149
754
    const auto& detail = _multi_detail[0];
150
754
    if (detail.offsets_ptr == nullptr || detail.nested_col.get() == nullptr) {
151
0
        return Status::InternalError("vexplode block fast path not initialized");
152
0
    }
153
754
    ctx->array_nullmap_data = detail.array_nullmap_data;
154
754
    ctx->offsets_ptr = detail.offsets_ptr;
155
754
    ctx->nested_col = detail.nested_col;
156
754
    ctx->nested_nullmap_data = detail.nested_nullmap_data;
157
754
    ctx->generate_row_index = _generate_row_index;
158
754
    return Status::OK();
159
754
}
160
161
1.10k
void VExplodeV2TableFunction::process_row(size_t row_idx) {
162
1.10k
    TableFunction::process_row(row_idx);
163
164
2.47k
    for (int i = 0; i < _multi_detail.size(); i++) {
165
1.37k
        auto& detail = _multi_detail[i];
166
1.37k
        if (!detail.array_nullmap_data || !detail.array_nullmap_data[row_idx]) {
167
1.26k
            _array_offsets[i] = (*detail.offsets_ptr)[row_idx - 1];
168
            // find max size in array
169
1.26k
            auto cur_size = (*detail.offsets_ptr)[row_idx] - _array_offsets[i];
170
1.26k
            _cur_size = std::max<unsigned long>(_cur_size, cur_size);
171
1.26k
        }
172
1.37k
    }
173
1.10k
    _row_idx = row_idx;
174
1.10k
}
175
176
946
void VExplodeV2TableFunction::process_close() {
177
946
    _multi_detail.clear();
178
946
    _array_offsets.clear();
179
946
    _array_columns.clear();
180
946
    _variant_v2_outputs.clear();
181
946
    _row_idx = 0;
182
946
    _has_variant_v2_output = false;
183
946
}
184
185
450
void VExplodeV2TableFunction::_ensure_variant_v2_output(MutableColumnPtr& column) const {
186
450
    if (!_has_variant_v2_output) {
187
450
        return;
188
450
    }
189
0
    const bool struct_output = _multi_detail.size() > 1 || _generate_row_index;
190
0
    if (!struct_output) {
191
0
        auto* nullable = check_and_get_column<ColumnNullable>(column.get());
192
0
        DORIS_CHECK(nullable != nullptr);
193
0
        if (check_and_get_column<ColumnVariantV2>(&nullable->get_nested_column()) != nullptr) {
194
0
            return;
195
0
        }
196
0
        DORIS_CHECK_EQ(column->size(), 0);
197
0
        column = ColumnNullable::create(ColumnVariantV2::create(), ColumnUInt8::create());
198
0
        return;
199
0
    }
200
201
0
    IColumn* nested_output = column.get();
202
0
    if (auto* nullable = check_and_get_column<ColumnNullable>(nested_output)) {
203
0
        nested_output = nullable->get_nested_column_ptr().get();
204
0
    }
205
0
    auto* struct_column = check_and_get_column<ColumnStruct>(nested_output);
206
0
    DORIS_CHECK(struct_column != nullptr);
207
0
    for (size_t i = 0; i < _variant_v2_outputs.size(); ++i) {
208
0
        if (!_variant_v2_outputs[i]) {
209
0
            continue;
210
0
        }
211
0
        const size_t field_index = i + (_generate_row_index ? 1 : 0);
212
0
        ColumnPtr& field = struct_column->get_column_ptr(field_index);
213
0
        const auto* nullable = check_and_get_column<ColumnNullable>(field.get());
214
0
        DORIS_CHECK(nullable != nullptr);
215
0
        if (check_and_get_column<ColumnVariantV2>(&nullable->get_nested_column()) != nullptr) {
216
0
            continue;
217
0
        }
218
0
        DORIS_CHECK(field->empty());
219
0
        field = ColumnNullable::create(ColumnVariantV2::create(), ColumnUInt8::create());
220
0
    }
221
0
}
222
223
31
void VExplodeV2TableFunction::get_same_many_values(MutableColumnPtr& column, int length) {
224
31
    _ensure_variant_v2_output(column);
225
31
    if (current_empty()) {
226
6
        column->insert_many_defaults(length);
227
6
        return;
228
6
    }
229
25
    ColumnStruct* struct_column = nullptr;
230
25
    std::vector<IColumn*> columns;
231
232
25
    const bool multi_sub_columns = _multi_detail.size() > 1 || _generate_row_index;
233
234
25
    if (multi_sub_columns) {
235
9
        if (_is_nullable) {
236
9
            auto* nullable_column = assert_cast<ColumnNullable*>(column.get());
237
9
            struct_column =
238
9
                    assert_cast<ColumnStruct*>(nullable_column->get_nested_column_ptr().get());
239
9
            auto* nullmap_column = nullable_column->get_null_map_column_ptr().get();
240
9
            nullmap_column->insert_many_defaults(length);
241
242
9
        } else {
243
0
            struct_column = assert_cast<ColumnStruct*>(column.get());
244
0
        }
245
246
27
        for (size_t i = 0; i != _multi_detail.size(); ++i) {
247
18
            columns.emplace_back(&struct_column->get_column(i + (_generate_row_index ? 1 : 0)));
248
18
        }
249
16
    } else {
250
16
        columns.push_back(column.get());
251
16
    }
252
253
25
    if (_generate_row_index) {
254
0
        auto& pos_column = assert_cast<ColumnInt32&>(struct_column->get_column(0));
255
0
        pos_column.insert_many_vals(static_cast<int32_t>(_cur_offset), length);
256
0
    }
257
258
59
    for (int i = 0; i < _multi_detail.size(); i++) {
259
34
        auto& detail = _multi_detail[i];
260
34
        size_t pos = _array_offsets[i] + _cur_offset;
261
34
        size_t element_size = _multi_detail[i].array_col->size_at(_row_idx);
262
34
        auto& struct_field = *columns.at(i);
263
34
        if ((detail.array_nullmap_data && detail.array_nullmap_data[_row_idx])) {
264
3
            struct_field.insert_many_defaults(length);
265
31
        } else {
266
31
            auto* nullable_column = assert_cast<ColumnNullable*>(struct_field.get_ptr().get());
267
31
            auto* nullmap_column = nullable_column->get_null_map_column_ptr().get();
268
            // only need to check if the value at position pos is null
269
31
            if (element_size < _cur_offset ||
270
31
                (detail.nested_nullmap_data && detail.nested_nullmap_data[pos])) {
271
3
                nullable_column->insert_many_defaults(length);
272
28
            } else {
273
28
                nullable_column->get_nested_column_ptr()->insert_many_from(*detail.nested_col, pos,
274
28
                                                                           length);
275
28
                nullmap_column->insert_many_defaults(length);
276
28
            }
277
31
        }
278
34
    }
279
25
}
280
281
419
int VExplodeV2TableFunction::get_value(MutableColumnPtr& column, int max_step) {
282
419
    _ensure_variant_v2_output(column);
283
419
    max_step = std::min(max_step, (int)(_cur_size - _cur_offset));
284
419
    const bool multi_sub_columns = _multi_detail.size() > 1 || _generate_row_index;
285
286
419
    ColumnStruct* struct_column = nullptr;
287
419
    std::vector<IColumn*> columns;
288
289
419
    if (current_empty()) {
290
19
        column->insert_default();
291
19
        max_step = 1;
292
400
    } else {
293
400
        if (multi_sub_columns) {
294
261
            if (_is_nullable) {
295
197
                auto* nullable_column = assert_cast<ColumnNullable*>(column.get());
296
197
                struct_column =
297
197
                        assert_cast<ColumnStruct*>(nullable_column->get_nested_column_ptr().get());
298
197
                auto* nullmap_column = nullable_column->get_null_map_column_ptr().get();
299
197
                nullmap_column->insert_many_defaults(max_step);
300
301
197
            } else {
302
64
                struct_column = assert_cast<ColumnStruct*>(column.get());
303
64
            }
304
305
864
            for (size_t i = 0; i != _multi_detail.size(); ++i) {
306
603
                columns.emplace_back(&struct_column->get_column(i + (_generate_row_index ? 1 : 0)));
307
603
            }
308
261
        } else {
309
139
            columns.emplace_back(column.get());
310
139
        }
311
312
400
        if (_generate_row_index) {
313
151
            auto& pos_column = assert_cast<ColumnInt32&>(struct_column->get_column(0));
314
151
            pos_column.insert_range_of_integer(static_cast<int32_t>(_cur_offset),
315
151
                                               static_cast<int32_t>(_cur_offset + max_step));
316
151
        }
317
318
1.14k
        for (int i = 0; i < _multi_detail.size(); i++) {
319
742
            auto& detail = _multi_detail[i];
320
742
            size_t pos = _array_offsets[i] + _cur_offset;
321
742
            size_t element_size = _multi_detail[i].array_col->size_at(_row_idx);
322
742
            auto& struct_field = *columns.at(i);
323
742
            if (detail.array_nullmap_data && detail.array_nullmap_data[_row_idx]) {
324
42
                struct_field.insert_many_defaults(max_step);
325
700
            } else {
326
700
                auto* nullable_column = assert_cast<ColumnNullable*>(struct_field.get_ptr().get());
327
700
                auto* nullmap_column = nullable_column->get_null_map_column_ptr().get();
328
700
                if (element_size >= _cur_offset + max_step) {
329
525
                    nullable_column->get_nested_column_ptr()->insert_range_from(*detail.nested_col,
330
525
                                                                                pos, max_step);
331
525
                    if (detail.nested_nullmap_data) {
332
525
                        size_t old_size = nullmap_column->size();
333
525
                        nullmap_column->resize(old_size + max_step);
334
525
                        memcpy(nullmap_column->get_data().data() + old_size,
335
525
                               detail.nested_nullmap_data + pos, max_step * sizeof(UInt8));
336
525
                    } else {
337
0
                        nullmap_column->insert_many_defaults(max_step);
338
0
                    }
339
525
                } else if (element_size > _cur_offset) {
340
127
                    auto current_insert_num = element_size - _cur_offset;
341
127
                    nullable_column->get_nested_column_ptr()->insert_range_from(
342
127
                            *detail.nested_col, pos, current_insert_num);
343
127
                    if (detail.nested_nullmap_data) {
344
127
                        size_t old_size = nullmap_column->size();
345
127
                        nullmap_column->resize(old_size + current_insert_num);
346
127
                        memcpy(nullmap_column->get_data().data() + old_size,
347
127
                               detail.nested_nullmap_data + pos,
348
127
                               current_insert_num * sizeof(UInt8));
349
127
                    } else {
350
0
                        nullmap_column->insert_many_defaults(current_insert_num);
351
0
                    }
352
127
                    nullable_column->insert_many_defaults(max_step - current_insert_num);
353
127
                } else {
354
48
                    nullable_column->insert_many_defaults(max_step);
355
48
                }
356
700
            }
357
742
        }
358
400
    }
359
360
419
    forward(max_step);
361
419
    return max_step;
362
419
}
363
364
} // namespace doris