Coverage Report

Created: 2026-03-15 08:11

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