Coverage Report

Created: 2026-05-18 05:00

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