Coverage Report

Created: 2026-04-01 14:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/table_function/vexplode.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.h"
19
20
#include <glog/logging.h>
21
22
#include <ostream>
23
24
#include "common/status.h"
25
#include "core/block/block.h"
26
#include "core/block/column_with_type_and_name.h"
27
#include "core/column/column.h"
28
#include "core/column/column_array.h"
29
#include "core/column/column_nothing.h"
30
#include "core/column/column_variant.h"
31
#include "core/data_type/data_type.h"
32
#include "core/data_type/data_type_array.h"
33
#include "core/data_type/data_type_nothing.h"
34
#include "exprs/function/function_helpers.h"
35
#include "exprs/vexpr.h"
36
#include "exprs/vexpr_context.h"
37
38
namespace doris {
39
#include "common/compile_check_begin.h"
40
41
2
VExplodeTableFunction::VExplodeTableFunction() {
42
2
    _fn_name = "vexplode";
43
2
}
44
45
0
Status VExplodeTableFunction::_process_init_variant(Block* block, int value_column_idx) {
46
    // explode variant array
47
0
    auto column_without_nullable = remove_nullable(block->get_by_position(value_column_idx).column);
48
0
    auto column = column_without_nullable->convert_to_full_column_if_const();
49
0
    auto& variant_column = assert_cast<ColumnVariant&>(*(column->assume_mutable()));
50
0
    variant_column.finalize();
51
0
    _detail.output_as_variant = true;
52
0
    _detail.variant_enable_doc_mode = variant_column.enable_doc_mode();
53
0
    if (!variant_column.is_null_root()) {
54
0
        _array_column = variant_column.get_root();
55
        // We need to wrap the output nested column within a variant column.
56
        // Otherwise the type is missmatched
57
0
        const auto* array_type = check_and_get_data_type<DataTypeArray>(
58
0
                remove_nullable(variant_column.get_root_type()).get());
59
0
        if (array_type == nullptr) {
60
0
            return Status::NotSupported("explode not support none array type {}",
61
0
                                        variant_column.get_root_type()->get_name());
62
0
        }
63
0
        _detail.nested_type = array_type->get_nested_type();
64
0
    } else {
65
        // null root, use nothing type
66
0
        _array_column = ColumnNullable::create(ColumnArray::create(ColumnNothing::create(0)),
67
0
                                               ColumnUInt8::create(0));
68
0
        _array_column->assume_mutable()->insert_many_defaults(variant_column.size());
69
0
        _detail.nested_type = std::make_shared<DataTypeNothing>();
70
0
    }
71
0
    return Status::OK();
72
0
}
73
74
5
Status VExplodeTableFunction::process_init(Block* block, RuntimeState* state) {
75
5
    CHECK(_expr_context->root()->children().size() == 1)
76
0
            << "VExplodeTableFunction only support 1 child but has "
77
0
            << _expr_context->root()->children().size();
78
79
5
    int value_column_idx = -1;
80
5
    RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute(_expr_context.get(), block,
81
5
                                                                  &value_column_idx));
82
5
    if (block->get_by_position(value_column_idx).type->get_primitive_type() == TYPE_VARIANT) {
83
0
        RETURN_IF_ERROR(_process_init_variant(block, value_column_idx));
84
5
    } else {
85
5
        _array_column =
86
5
                block->get_by_position(value_column_idx).column->convert_to_full_column_if_const();
87
5
    }
88
5
    if (!extract_column_array_info(*_array_column, _detail)) {
89
0
        return Status::NotSupported("column type {} not supported now",
90
0
                                    block->get_by_position(value_column_idx).column->get_name());
91
0
    }
92
93
5
    return Status::OK();
94
5
}
95
96
0
bool VExplodeTableFunction::support_block_fast_path() const {
97
0
    return !_is_outer;
98
0
}
99
100
Status VExplodeTableFunction::prepare_block_fast_path(Block* /*block*/, RuntimeState* /*state*/,
101
0
                                                      BlockFastPathContext* ctx) {
102
0
    if (!support_block_fast_path()) {
103
0
        return Status::NotSupported("vexplode doesn't support block fast path in current mode");
104
0
    }
105
0
    if (_detail.offsets_ptr == nullptr || _detail.nested_col.get() == nullptr) {
106
0
        return Status::InternalError("vexplode block fast path not initialized");
107
0
    }
108
0
    ctx->array_nullmap_data = _detail.array_nullmap_data;
109
0
    ctx->offsets_ptr = _detail.offsets_ptr;
110
0
    ctx->nested_col = _detail.nested_col;
111
0
    ctx->nested_nullmap_data = _detail.nested_nullmap_data;
112
0
    return Status::OK();
113
0
}
114
115
15
void VExplodeTableFunction::process_row(size_t row_idx) {
116
15
    DCHECK(row_idx < _array_column->size());
117
15
    TableFunction::process_row(row_idx);
118
119
15
    if (!_detail.array_nullmap_data || !_detail.array_nullmap_data[row_idx]) {
120
10
        _array_offset = (*_detail.offsets_ptr)[row_idx - 1];
121
10
        _cur_size = (*_detail.offsets_ptr)[row_idx] - _array_offset;
122
10
    }
123
15
}
124
125
0
void VExplodeTableFunction::process_close() {
126
0
    _array_column = nullptr;
127
0
    _detail.reset();
128
0
    _array_offset = 0;
129
0
}
130
131
22
void VExplodeTableFunction::get_same_many_values(MutableColumnPtr& column, int length) {
132
22
    size_t pos = _array_offset + _cur_offset;
133
22
    if (current_empty() || (_detail.nested_nullmap_data && _detail.nested_nullmap_data[pos])) {
134
8
        column->insert_many_defaults(length);
135
14
    } else {
136
14
        if (_is_nullable) {
137
14
            assert_cast<ColumnNullable*>(column.get())
138
14
                    ->get_nested_column_ptr()
139
14
                    ->insert_many_from(*_detail.nested_col, pos, length);
140
14
            assert_cast<ColumnUInt8*>(
141
14
                    assert_cast<ColumnNullable*>(column.get())->get_null_map_column_ptr().get())
142
14
                    ->insert_many_defaults(length);
143
14
        } else {
144
0
            column->insert_many_from(*_detail.nested_col, pos, length);
145
0
        }
146
14
    }
147
22
}
148
149
0
int VExplodeTableFunction::get_value(MutableColumnPtr& column, int max_step) {
150
0
    max_step = std::min(max_step, (int)(_cur_size - _cur_offset));
151
0
    size_t pos = _array_offset + _cur_offset;
152
0
    if (current_empty()) {
153
0
        column->insert_default();
154
0
        max_step = 1;
155
0
    } else {
156
0
        if (_is_nullable) {
157
0
            auto* nullable_column = assert_cast<ColumnNullable*>(column.get());
158
0
            auto nested_column = nullable_column->get_nested_column_ptr();
159
0
            auto* nullmap_column =
160
0
                    assert_cast<ColumnUInt8*>(nullable_column->get_null_map_column_ptr().get());
161
0
            nested_column->insert_range_from(*_detail.nested_col, pos, max_step);
162
0
            size_t old_size = nullmap_column->size();
163
0
            nullmap_column->resize(old_size + max_step);
164
0
            memcpy(nullmap_column->get_data().data() + old_size,
165
0
                   _detail.nested_nullmap_data + pos * sizeof(UInt8), max_step * sizeof(UInt8));
166
0
        } else {
167
0
            column->insert_range_from(*_detail.nested_col, pos, max_step);
168
0
        }
169
0
    }
170
0
    forward(max_step);
171
0
    return max_step;
172
0
}
173
174
#include "common/compile_check_end.h"
175
} // namespace doris