Coverage Report

Created: 2026-03-31 21:03

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
0
VExplodeMapTableFunction::VExplodeMapTableFunction() {
35
0
    _fn_name = "vexplode_map";
36
0
}
37
38
0
bool extract_column_map_info(const IColumn& src, ColumnMapExecutionData& data) {
39
0
    const IColumn* map_col = &src;
40
    // extract array nullable info
41
0
    if (src.is_nullable()) {
42
0
        const auto& null_col = reinterpret_cast<const ColumnNullable&>(src);
43
        // map column's nullmap
44
0
        data.map_nullmap_data = null_col.get_null_map_data().data();
45
0
        map_col = null_col.get_nested_column_ptr().get();
46
0
    }
47
48
0
    if (data.map_col = check_and_get_column<ColumnMap>(map_col); !data.map_col) {
49
0
        return false;
50
0
    }
51
52
0
    data.offsets_ptr = &data.map_col->get_offsets();
53
0
    return true;
54
0
}
55
56
0
Status VExplodeMapTableFunction::process_init(Block* block, RuntimeState* state) {
57
0
    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
0
    ColumnPtr value_column;
62
0
    RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute_column(
63
0
            _expr_context.get(), block, nullptr, block->rows(), value_column));
64
65
0
    _collection_column = value_column->convert_to_full_column_if_const();
66
67
0
    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
0
    return Status::OK();
73
0
}
74
75
0
void VExplodeMapTableFunction::process_row(size_t row_idx) {
76
0
    DCHECK(row_idx < _collection_column->size());
77
0
    TableFunction::process_row(row_idx);
78
79
0
    if (!_map_detail.map_nullmap_data || !_map_detail.map_nullmap_data[row_idx]) {
80
0
        _collection_offset = (*_map_detail.offsets_ptr)[row_idx - 1];
81
0
        _cur_size = (*_map_detail.offsets_ptr)[row_idx] - _collection_offset;
82
0
    }
83
0
}
84
85
0
void VExplodeMapTableFunction::process_close() {
86
0
    _collection_column = nullptr;
87
0
    _map_detail.reset();
88
0
    _collection_offset = 0;
89
0
}
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
0
int VExplodeMapTableFunction::get_value(MutableColumnPtr& column, int max_step) {
124
0
    max_step = std::min(max_step, (int)(_cur_size - _cur_offset));
125
0
    size_t pos = _collection_offset + _cur_offset;
126
0
    if (current_empty()) {
127
0
        column->insert_default();
128
0
        max_step = 1;
129
0
    } else {
130
0
        ColumnStruct* struct_column = nullptr;
131
0
        if (_is_nullable) {
132
0
            auto* nullable_column = assert_cast<ColumnNullable*>(column.get());
133
0
            struct_column =
134
0
                    assert_cast<ColumnStruct*>(nullable_column->get_nested_column_ptr().get());
135
0
            auto* nullmap_column =
136
0
                    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
0
            nullmap_column->insert_many_defaults(max_step);
141
0
        } else {
142
0
            struct_column = assert_cast<ColumnStruct*>(column.get());
143
0
        }
144
0
        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
0
        struct_column->get_column(0).insert_range_from(_map_detail.map_col->get_keys(), pos,
150
0
                                                       max_step);
151
0
        struct_column->get_column(1).insert_range_from(_map_detail.map_col->get_values(), pos,
152
0
                                                       max_step);
153
0
    }
154
0
    forward(max_step);
155
0
    return max_step;
156
0
}
157
158
#include "common/compile_check_end.h"
159
} // namespace doris