Coverage Report

Created: 2026-03-31 13:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/table_function/vexplode_map.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_map.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 "exprs/vexpr.h"
29
#include "exprs/vexpr_context.h"
30
31
namespace doris {
32
#include "common/compile_check_begin.h"
33
34
334
VExplodeMapTableFunction::VExplodeMapTableFunction() {
35
334
    _fn_name = "vexplode_map";
36
334
}
37
38
43
bool extract_column_map_info(const IColumn& src, ColumnMapExecutionData& data) {
39
43
    const IColumn* map_col = &src;
40
    // extract array nullable info
41
43
    if (src.is_nullable()) {
42
25
        const auto& null_col = reinterpret_cast<const ColumnNullable&>(src);
43
        // map column's nullmap
44
25
        data.map_nullmap_data = null_col.get_null_map_data().data();
45
25
        map_col = null_col.get_nested_column_ptr().get();
46
25
    }
47
48
43
    if (data.map_col = check_and_get_column<ColumnMap>(map_col); !data.map_col) {
49
0
        return false;
50
0
    }
51
52
43
    data.offsets_ptr = &data.map_col->get_offsets();
53
43
    return true;
54
43
}
55
56
43
Status VExplodeMapTableFunction::process_init(Block* block, RuntimeState* state) {
57
43
    CHECK(_expr_context->root()->children().size() == 1)
58
0
            << "VExplodeMapTableFunction only support 1 child but has "
59
0
            << _expr_context->root()->children().size();
60
61
43
    ColumnPtr value_column;
62
43
    RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute_column(
63
43
            _expr_context.get(), block, nullptr, block->rows(), value_column));
64
65
43
    _collection_column = value_column->convert_to_full_column_if_const();
66
67
43
    if (!extract_column_map_info(*_collection_column, _map_detail)) {
68
0
        return Status::NotSupported("column type {} not supported now, only support array or map",
69
0
                                    _collection_column->get_name());
70
0
    }
71
72
43
    return Status::OK();
73
43
}
74
75
173
void VExplodeMapTableFunction::process_row(size_t row_idx) {
76
173
    DCHECK(row_idx < _collection_column->size());
77
173
    TableFunction::process_row(row_idx);
78
79
173
    if (!_map_detail.map_nullmap_data || !_map_detail.map_nullmap_data[row_idx]) {
80
161
        _collection_offset = (*_map_detail.offsets_ptr)[row_idx - 1];
81
161
        _cur_size = (*_map_detail.offsets_ptr)[row_idx] - _collection_offset;
82
161
    }
83
173
}
84
85
43
void VExplodeMapTableFunction::process_close() {
86
43
    _collection_column = nullptr;
87
43
    _map_detail.reset();
88
43
    _collection_offset = 0;
89
43
}
90
91
0
void VExplodeMapTableFunction::get_same_many_values(MutableColumnPtr& column, int length) {
92
    // now we only support map column explode to struct column
93
0
    size_t pos = _collection_offset + _cur_offset;
94
    // if current is empty map row, also append a default value
95
0
    if (current_empty()) {
96
0
        column->insert_many_defaults(length);
97
0
        return;
98
0
    }
99
0
    ColumnStruct* ret = nullptr;
100
    // this _is_nullable is whole output column's nullable
101
0
    if (_is_nullable) {
102
        // make map kv value into struct
103
0
        ret = assert_cast<ColumnStruct*>(
104
0
                assert_cast<ColumnNullable*>(column.get())->get_nested_column_ptr().get());
105
0
        assert_cast<ColumnUInt8*>(
106
0
                assert_cast<ColumnNullable*>(column.get())->get_null_map_column_ptr().get())
107
0
                ->insert_many_defaults(length);
108
0
    } else if (is_column<ColumnStruct>(column.get())) {
109
0
        ret = assert_cast<ColumnStruct*>(column.get());
110
0
    } else {
111
0
        throw Exception(ErrorCode::INTERNAL_ERROR,
112
0
                        "only support map column explode to struct column");
113
0
    }
114
0
    if (!ret || ret->tuple_size() != 2) {
115
0
        throw Exception(
116
0
                ErrorCode::INTERNAL_ERROR,
117
0
                "only support map column explode to two column, but given:  ", ret->tuple_size());
118
0
    }
119
0
    ret->get_column(0).insert_many_from(_map_detail.map_col->get_keys(), pos, length);
120
0
    ret->get_column(1).insert_many_from(_map_detail.map_col->get_values(), pos, length);
121
0
}
122
123
161
int VExplodeMapTableFunction::get_value(MutableColumnPtr& column, int max_step) {
124
161
    max_step = std::min(max_step, (int)(_cur_size - _cur_offset));
125
161
    size_t pos = _collection_offset + _cur_offset;
126
161
    if (current_empty()) {
127
5
        column->insert_default();
128
5
        max_step = 1;
129
156
    } else {
130
156
        ColumnStruct* struct_column = nullptr;
131
156
        if (_is_nullable) {
132
156
            auto* nullable_column = assert_cast<ColumnNullable*>(column.get());
133
156
            struct_column =
134
156
                    assert_cast<ColumnStruct*>(nullable_column->get_nested_column_ptr().get());
135
156
            auto* nullmap_column =
136
156
                    assert_cast<ColumnUInt8*>(nullable_column->get_null_map_column_ptr().get());
137
            // here nullmap_column insert max_step many defaults as if MAP[row_idx] is NULL
138
            // will be not update value, _cur_size = 0, means current_empty;
139
            // so here could insert directly
140
156
            nullmap_column->insert_many_defaults(max_step);
141
156
        } else {
142
0
            struct_column = assert_cast<ColumnStruct*>(column.get());
143
0
        }
144
156
        if (!struct_column || struct_column->tuple_size() != 2) {
145
0
            throw Exception(ErrorCode::INTERNAL_ERROR,
146
0
                            "only support map column explode to two column, but given:  ",
147
0
                            struct_column->tuple_size());
148
0
        }
149
156
        struct_column->get_column(0).insert_range_from(_map_detail.map_col->get_keys(), pos,
150
156
                                                       max_step);
151
156
        struct_column->get_column(1).insert_range_from(_map_detail.map_col->get_values(), pos,
152
156
                                                       max_step);
153
156
    }
154
161
    forward(max_step);
155
161
    return max_step;
156
161
}
157
158
#include "common/compile_check_end.h"
159
} // namespace doris