Coverage Report

Created: 2026-04-15 19:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/table_function/vexplode_json_object.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_json_object.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_struct.h"
29
#include "core/string_ref.h"
30
#include "exprs/vexpr.h"
31
#include "exprs/vexpr_context.h"
32
#include "util/jsonb_document.h"
33
#include "util/jsonb_writer.h"
34
35
namespace doris {
36
37
0
VExplodeJsonObjectTableFunction::VExplodeJsonObjectTableFunction() {
38
0
    _fn_name = "vexplode_json_object";
39
0
}
40
41
0
Status VExplodeJsonObjectTableFunction::process_init(Block* block, RuntimeState* state) {
42
0
    CHECK(_expr_context->root()->children().size() == 1)
43
0
            << "VExplodeJsonObjectTableFunction only support 1 child but has "
44
0
            << _expr_context->root()->children().size();
45
46
0
    RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute_column(
47
0
            _expr_context.get(), block, nullptr, block->rows(), _json_object_column));
48
49
0
    return Status::OK();
50
0
}
51
52
0
void VExplodeJsonObjectTableFunction::process_row(size_t row_idx) {
53
0
    TableFunction::process_row(row_idx);
54
55
0
    StringRef text = _json_object_column->get_data_at(row_idx);
56
0
    if (text.data != nullptr) {
57
0
        const JsonbDocument* doc = nullptr;
58
0
        auto st = JsonbDocument::checkAndCreateDocument(text.data, text.size, &doc);
59
0
        if (!st.ok() || !doc || !doc->getValue()) [[unlikely]] {
60
            // error jsonb, put null into output, cur_size = 0 , we will insert_default
61
0
            return;
62
0
        }
63
        // value is NOT necessary to be deleted since JsonbValue will not allocate memory
64
0
        const JsonbValue* value = doc->getValue();
65
0
        auto writer = std::make_unique<JsonbWriter>();
66
0
        if (value->isObject()) {
67
0
            _cur_size = value->numElements();
68
0
            const auto* obj = value->unpack<ObjectVal>();
69
0
            _object_pairs.first =
70
0
                    ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
71
0
            _object_pairs.second =
72
0
                    ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
73
0
            _object_pairs.first->reserve(_cur_size);
74
0
            _object_pairs.second->reserve(_cur_size);
75
0
            for (const auto& it : *obj) {
76
0
                _object_pairs.first->insert_data(it.getKeyStr(), it.klen());
77
0
                writer->reset();
78
0
                writer->writeValue(it.value());
79
0
                if (it.value()->isNull()) {
80
0
                    _object_pairs.second->insert_default();
81
0
                } else {
82
0
                    const std::string_view& jsonb_value = std::string_view(
83
0
                            writer->getOutput()->getBuffer(), writer->getOutput()->getSize());
84
0
                    _object_pairs.second->insert_data(jsonb_value.data(), jsonb_value.size());
85
0
                }
86
0
            }
87
0
        }
88
        // we do not support other json type except object
89
0
    }
90
0
}
91
92
0
void VExplodeJsonObjectTableFunction::process_close() {
93
0
    _json_object_column = nullptr;
94
0
    _object_pairs.first = nullptr;
95
0
    _object_pairs.second = nullptr;
96
0
}
97
98
0
void VExplodeJsonObjectTableFunction::get_same_many_values(MutableColumnPtr& column, int length) {
99
    // if current is empty map row, also append a default value
100
0
    if (current_empty()) {
101
0
        column->insert_many_defaults(length);
102
0
        return;
103
0
    }
104
0
    ColumnStruct* ret = nullptr;
105
    // this _is_nullable is whole output column's nullable
106
0
    if (_is_nullable) {
107
        // make map kv value into struct
108
0
        ret = assert_cast<ColumnStruct*>(
109
0
                assert_cast<ColumnNullable*>(column.get())->get_nested_column_ptr().get());
110
0
        assert_cast<ColumnUInt8*>(
111
0
                assert_cast<ColumnNullable*>(column.get())->get_null_map_column_ptr().get())
112
0
                ->insert_many_defaults(length);
113
0
    } else if (is_column<ColumnStruct>(column.get())) {
114
0
        ret = assert_cast<ColumnStruct*>(column.get());
115
0
    } else {
116
0
        throw Exception(ErrorCode::INTERNAL_ERROR,
117
0
                        "only support expand json object int to struct(kv pair), but given: ",
118
0
                        column->get_name());
119
0
    }
120
0
    if (!ret || ret->tuple_size() != 2) {
121
0
        throw Exception(ErrorCode::INTERNAL_ERROR,
122
0
                        "only support expand json object int to kv pair column, but given: ",
123
0
                        ret->tuple_size());
124
0
    }
125
0
    ret->get_column(0).insert_many_from(*_object_pairs.first, _cur_offset, length);
126
0
    ret->get_column(1).insert_many_from(*_object_pairs.second, _cur_offset, length);
127
0
}
128
129
0
int VExplodeJsonObjectTableFunction::get_value(MutableColumnPtr& column, int max_step) {
130
0
    max_step = std::min(max_step, (int)(_cur_size - _cur_offset));
131
0
    if (current_empty()) {
132
0
        column->insert_default();
133
0
        max_step = 1;
134
0
    } else {
135
0
        ColumnStruct* struct_column = nullptr;
136
0
        if (_is_nullable) {
137
0
            auto* nullable_column = assert_cast<ColumnNullable*>(column.get());
138
0
            struct_column =
139
0
                    assert_cast<ColumnStruct*>(nullable_column->get_nested_column_ptr().get());
140
0
            auto* nullmap_column =
141
0
                    assert_cast<ColumnUInt8*>(nullable_column->get_null_map_column_ptr().get());
142
            // here nullmap_column insert max_step many defaults as if MAP[row_idx] is NULL
143
            // will be not update value, _cur_size = 0, means current_empty;
144
            // so here could insert directly
145
0
            nullmap_column->insert_many_defaults(max_step);
146
0
        } else {
147
0
            struct_column = assert_cast<ColumnStruct*>(column.get());
148
0
        }
149
0
        if (!struct_column || struct_column->tuple_size() != 2) {
150
0
            throw Exception(ErrorCode::INTERNAL_ERROR,
151
0
                            "only support expand json object int to kv pair column, but given:  ",
152
0
                            struct_column->tuple_size());
153
0
        }
154
0
        struct_column->get_column(0).insert_range_from(*_object_pairs.first, _cur_offset, max_step);
155
0
        struct_column->get_column(1).insert_range_from(*_object_pairs.second, _cur_offset,
156
0
                                                       max_step);
157
0
    }
158
0
    forward(max_step);
159
0
    return max_step;
160
0
}
161
162
} // namespace doris