Coverage Report

Created: 2026-07-25 08:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/table_function/vjson_each.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/vjson_each.h"
19
20
#include <glog/logging.h>
21
22
#include <ostream>
23
#include <string>
24
25
#include "common/status.h"
26
#include "core/assert_cast.h"
27
#include "core/block/block.h"
28
#include "core/block/column_with_type_and_name.h"
29
#include "core/column/column.h"
30
#include "core/column/column_const.h"
31
#include "core/column/column_struct.h"
32
#include "core/string_ref.h"
33
#include "exprs/vexpr.h"
34
#include "exprs/vexpr_context.h"
35
#include "util/jsonb_document.h"
36
#include "util/jsonb_utils.h"
37
#include "util/jsonb_writer.h"
38
39
namespace doris {
40
41
template <bool TEXT_MODE>
42
323
VJsonEachTableFunction<TEXT_MODE>::VJsonEachTableFunction() {
43
323
    _fn_name = TEXT_MODE ? "vjson_each_text" : "vjson_each";
44
323
}
_ZN5doris22VJsonEachTableFunctionILb0EEC2Ev
Line
Count
Source
42
198
VJsonEachTableFunction<TEXT_MODE>::VJsonEachTableFunction() {
43
198
    _fn_name = TEXT_MODE ? "vjson_each_text" : "vjson_each";
44
198
}
_ZN5doris22VJsonEachTableFunctionILb1EEC2Ev
Line
Count
Source
42
125
VJsonEachTableFunction<TEXT_MODE>::VJsonEachTableFunction() {
43
125
    _fn_name = TEXT_MODE ? "vjson_each_text" : "vjson_each";
44
125
}
45
46
template <bool TEXT_MODE>
47
124
Status VJsonEachTableFunction<TEXT_MODE>::process_init(Block* block, RuntimeState* /*state*/) {
48
124
    ColumnPtr value_column;
49
124
    RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute_column(
50
124
            _expr_context.get(), block, nullptr, block->rows(), value_column));
51
124
    auto [col, is_const] = unpack_if_const(value_column);
52
124
    _json_column = col;
53
124
    _is_const = is_const;
54
124
    return Status::OK();
55
124
}
_ZN5doris22VJsonEachTableFunctionILb0EE12process_initEPNS_5BlockEPNS_12RuntimeStateE
Line
Count
Source
47
79
Status VJsonEachTableFunction<TEXT_MODE>::process_init(Block* block, RuntimeState* /*state*/) {
48
79
    ColumnPtr value_column;
49
79
    RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute_column(
50
79
            _expr_context.get(), block, nullptr, block->rows(), value_column));
51
79
    auto [col, is_const] = unpack_if_const(value_column);
52
79
    _json_column = col;
53
79
    _is_const = is_const;
54
79
    return Status::OK();
55
79
}
_ZN5doris22VJsonEachTableFunctionILb1EE12process_initEPNS_5BlockEPNS_12RuntimeStateE
Line
Count
Source
47
45
Status VJsonEachTableFunction<TEXT_MODE>::process_init(Block* block, RuntimeState* /*state*/) {
48
45
    ColumnPtr value_column;
49
45
    RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute_column(
50
45
            _expr_context.get(), block, nullptr, block->rows(), value_column));
51
45
    auto [col, is_const] = unpack_if_const(value_column);
52
45
    _json_column = col;
53
45
    _is_const = is_const;
54
45
    return Status::OK();
55
45
}
56
57
// Helper: insert one JsonbValue as plain text into a ColumnNullable<ColumnString>.
58
// For strings: raw blob content (quotes stripped, matching json_each_text PG semantics).
59
// For null JSON values: SQL NULL (insert_default).
60
// For all others (numbers, bools, objects, arrays): JSON text representation.
61
54
static void insert_value_as_text(const JsonbValue* value, MutableColumnPtr& col) {
62
54
    if (value == nullptr || value->isNull()) {
63
5
        col->insert_default();
64
5
        return;
65
5
    }
66
49
    if (value->isString()) {
67
28
        const auto* str_val = value->unpack<JsonbStringVal>();
68
28
        col->insert_data(str_val->getBlob(), str_val->getBlobLen());
69
28
    } else {
70
21
        JsonbToJson converter;
71
21
        std::string text = converter.to_json_string(value);
72
21
        col->insert_data(text.data(), text.size());
73
21
    }
74
49
}
75
76
// Helper: insert one JsonbValue in JSONB binary form into a ColumnNullable<ColumnString>.
77
// For null JSON values: SQL NULL (insert_default).
78
// For all others: write JSONB binary via JsonbWriter.
79
static void insert_value_as_json(const JsonbValue* value, MutableColumnPtr& col,
80
107
                                 JsonbWriter& writer) {
81
107
    if (value == nullptr || value->isNull()) {
82
7
        col->insert_default();
83
7
        return;
84
7
    }
85
100
    writer.reset();
86
100
    writer.writeValue(value);
87
100
    const auto* buf = writer.getOutput()->getBuffer();
88
100
    size_t len = writer.getOutput()->getSize();
89
100
    col->insert_data(buf, len);
90
100
}
91
92
template <bool TEXT_MODE>
93
150
void VJsonEachTableFunction<TEXT_MODE>::process_row(size_t row_idx) {
94
150
    TableFunction::process_row(row_idx);
95
150
    if (_is_const && _cur_size > 0) {
96
9
        return;
97
9
    }
98
99
141
    StringRef text;
100
141
    const size_t idx = _is_const ? 0 : row_idx;
101
141
    if (const auto* nullable_col = check_and_get_column<ColumnNullable>(*_json_column)) {
102
141
        if (nullable_col->is_null_at(idx)) {
103
18
            return;
104
18
        }
105
123
        text = assert_cast<const ColumnString&>(nullable_col->get_nested_column()).get_data_at(idx);
106
123
    } else {
107
0
        text = assert_cast<const ColumnString&>(*_json_column).get_data_at(idx);
108
0
    }
109
110
123
    const JsonbDocument* doc = nullptr;
111
123
    auto st = JsonbDocument::checkAndCreateDocument(text.data, text.size, &doc);
112
123
    if (!st.ok() || !doc || !doc->getValue()) [[unlikely]] {
113
6
        return;
114
6
    }
115
116
117
    const JsonbValue* jv = doc->getValue();
117
117
    if (!jv->isObject()) {
118
16
        return;
119
16
    }
120
121
101
    const auto* obj = jv->unpack<ObjectVal>();
122
101
    _cur_size = obj->numElem();
123
101
    if (_cur_size == 0) {
124
22
        return;
125
22
    }
126
127
79
    _kv_pairs.first = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
128
79
    _kv_pairs.second = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
129
79
    _kv_pairs.first->reserve(_cur_size);
130
79
    _kv_pairs.second->reserve(_cur_size);
131
132
79
    if constexpr (TEXT_MODE) {
133
54
        for (const auto& kv : *obj) {
134
54
            _kv_pairs.first->insert_data(kv.getKeyStr(), kv.klen());
135
54
            insert_value_as_text(kv.value(), _kv_pairs.second);
136
54
        }
137
53
    } else {
138
53
        JsonbWriter writer;
139
107
        for (const auto& kv : *obj) {
140
107
            _kv_pairs.first->insert_data(kv.getKeyStr(), kv.klen());
141
107
            insert_value_as_json(kv.value(), _kv_pairs.second, writer);
142
107
        }
143
53
    }
144
79
}
_ZN5doris22VJsonEachTableFunctionILb0EE11process_rowEm
Line
Count
Source
93
96
void VJsonEachTableFunction<TEXT_MODE>::process_row(size_t row_idx) {
94
96
    TableFunction::process_row(row_idx);
95
96
    if (_is_const && _cur_size > 0) {
96
7
        return;
97
7
    }
98
99
89
    StringRef text;
100
89
    const size_t idx = _is_const ? 0 : row_idx;
101
89
    if (const auto* nullable_col = check_and_get_column<ColumnNullable>(*_json_column)) {
102
89
        if (nullable_col->is_null_at(idx)) {
103
10
            return;
104
10
        }
105
79
        text = assert_cast<const ColumnString&>(nullable_col->get_nested_column()).get_data_at(idx);
106
79
    } else {
107
0
        text = assert_cast<const ColumnString&>(*_json_column).get_data_at(idx);
108
0
    }
109
110
79
    const JsonbDocument* doc = nullptr;
111
79
    auto st = JsonbDocument::checkAndCreateDocument(text.data, text.size, &doc);
112
79
    if (!st.ok() || !doc || !doc->getValue()) [[unlikely]] {
113
2
        return;
114
2
    }
115
116
77
    const JsonbValue* jv = doc->getValue();
117
77
    if (!jv->isObject()) {
118
10
        return;
119
10
    }
120
121
67
    const auto* obj = jv->unpack<ObjectVal>();
122
67
    _cur_size = obj->numElem();
123
67
    if (_cur_size == 0) {
124
14
        return;
125
14
    }
126
127
53
    _kv_pairs.first = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
128
53
    _kv_pairs.second = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
129
53
    _kv_pairs.first->reserve(_cur_size);
130
53
    _kv_pairs.second->reserve(_cur_size);
131
132
    if constexpr (TEXT_MODE) {
133
        for (const auto& kv : *obj) {
134
            _kv_pairs.first->insert_data(kv.getKeyStr(), kv.klen());
135
            insert_value_as_text(kv.value(), _kv_pairs.second);
136
        }
137
53
    } else {
138
53
        JsonbWriter writer;
139
107
        for (const auto& kv : *obj) {
140
107
            _kv_pairs.first->insert_data(kv.getKeyStr(), kv.klen());
141
107
            insert_value_as_json(kv.value(), _kv_pairs.second, writer);
142
107
        }
143
53
    }
144
53
}
_ZN5doris22VJsonEachTableFunctionILb1EE11process_rowEm
Line
Count
Source
93
54
void VJsonEachTableFunction<TEXT_MODE>::process_row(size_t row_idx) {
94
54
    TableFunction::process_row(row_idx);
95
54
    if (_is_const && _cur_size > 0) {
96
2
        return;
97
2
    }
98
99
52
    StringRef text;
100
52
    const size_t idx = _is_const ? 0 : row_idx;
101
52
    if (const auto* nullable_col = check_and_get_column<ColumnNullable>(*_json_column)) {
102
52
        if (nullable_col->is_null_at(idx)) {
103
8
            return;
104
8
        }
105
44
        text = assert_cast<const ColumnString&>(nullable_col->get_nested_column()).get_data_at(idx);
106
44
    } else {
107
0
        text = assert_cast<const ColumnString&>(*_json_column).get_data_at(idx);
108
0
    }
109
110
44
    const JsonbDocument* doc = nullptr;
111
44
    auto st = JsonbDocument::checkAndCreateDocument(text.data, text.size, &doc);
112
44
    if (!st.ok() || !doc || !doc->getValue()) [[unlikely]] {
113
4
        return;
114
4
    }
115
116
40
    const JsonbValue* jv = doc->getValue();
117
40
    if (!jv->isObject()) {
118
6
        return;
119
6
    }
120
121
34
    const auto* obj = jv->unpack<ObjectVal>();
122
34
    _cur_size = obj->numElem();
123
34
    if (_cur_size == 0) {
124
8
        return;
125
8
    }
126
127
26
    _kv_pairs.first = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
128
26
    _kv_pairs.second = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
129
26
    _kv_pairs.first->reserve(_cur_size);
130
26
    _kv_pairs.second->reserve(_cur_size);
131
132
26
    if constexpr (TEXT_MODE) {
133
54
        for (const auto& kv : *obj) {
134
54
            _kv_pairs.first->insert_data(kv.getKeyStr(), kv.klen());
135
54
            insert_value_as_text(kv.value(), _kv_pairs.second);
136
54
        }
137
    } else {
138
        JsonbWriter writer;
139
        for (const auto& kv : *obj) {
140
            _kv_pairs.first->insert_data(kv.getKeyStr(), kv.klen());
141
            insert_value_as_json(kv.value(), _kv_pairs.second, writer);
142
        }
143
    }
144
26
}
145
146
template <bool TEXT_MODE>
147
124
void VJsonEachTableFunction<TEXT_MODE>::process_close() {
148
124
    _json_column = nullptr;
149
124
    _kv_pairs.first = nullptr;
150
124
    _kv_pairs.second = nullptr;
151
124
    _cur_size = 0;
152
124
}
_ZN5doris22VJsonEachTableFunctionILb0EE13process_closeEv
Line
Count
Source
147
79
void VJsonEachTableFunction<TEXT_MODE>::process_close() {
148
79
    _json_column = nullptr;
149
79
    _kv_pairs.first = nullptr;
150
79
    _kv_pairs.second = nullptr;
151
79
    _cur_size = 0;
152
79
}
_ZN5doris22VJsonEachTableFunctionILb1EE13process_closeEv
Line
Count
Source
147
45
void VJsonEachTableFunction<TEXT_MODE>::process_close() {
148
45
    _json_column = nullptr;
149
45
    _kv_pairs.first = nullptr;
150
45
    _kv_pairs.second = nullptr;
151
45
    _cur_size = 0;
152
45
}
153
154
template <bool TEXT_MODE>
155
10
void VJsonEachTableFunction<TEXT_MODE>::get_same_many_values(MutableColumnPtr& column, int length) {
156
10
    if (current_empty()) {
157
2
        column->insert_many_defaults(length);
158
2
        return;
159
2
    }
160
161
8
    ColumnStruct* ret;
162
8
    if (_is_nullable) {
163
6
        auto* nullable = assert_cast<ColumnNullable*>(column.get());
164
6
        ret = assert_cast<ColumnStruct*>(nullable->get_nested_column_ptr().get());
165
6
        nullable->get_null_map_column_ptr()->insert_many_defaults(length);
166
6
    } else {
167
2
        ret = assert_cast<ColumnStruct*>(column.get());
168
2
    }
169
170
8
    ret->get_column(0).insert_many_from(*_kv_pairs.first, _cur_offset, length);
171
8
    ret->get_column(1).insert_many_from(*_kv_pairs.second, _cur_offset, length);
172
8
}
_ZN5doris22VJsonEachTableFunctionILb0EE20get_same_many_valuesERNS_3COWINS_7IColumnEE11mutable_ptrIS3_EEi
Line
Count
Source
155
8
void VJsonEachTableFunction<TEXT_MODE>::get_same_many_values(MutableColumnPtr& column, int length) {
156
8
    if (current_empty()) {
157
2
        column->insert_many_defaults(length);
158
2
        return;
159
2
    }
160
161
6
    ColumnStruct* ret;
162
6
    if (_is_nullable) {
163
4
        auto* nullable = assert_cast<ColumnNullable*>(column.get());
164
4
        ret = assert_cast<ColumnStruct*>(nullable->get_nested_column_ptr().get());
165
4
        nullable->get_null_map_column_ptr()->insert_many_defaults(length);
166
4
    } else {
167
2
        ret = assert_cast<ColumnStruct*>(column.get());
168
2
    }
169
170
6
    ret->get_column(0).insert_many_from(*_kv_pairs.first, _cur_offset, length);
171
6
    ret->get_column(1).insert_many_from(*_kv_pairs.second, _cur_offset, length);
172
6
}
_ZN5doris22VJsonEachTableFunctionILb1EE20get_same_many_valuesERNS_3COWINS_7IColumnEE11mutable_ptrIS3_EEi
Line
Count
Source
155
2
void VJsonEachTableFunction<TEXT_MODE>::get_same_many_values(MutableColumnPtr& column, int length) {
156
2
    if (current_empty()) {
157
0
        column->insert_many_defaults(length);
158
0
        return;
159
0
    }
160
161
2
    ColumnStruct* ret;
162
2
    if (_is_nullable) {
163
2
        auto* nullable = assert_cast<ColumnNullable*>(column.get());
164
2
        ret = assert_cast<ColumnStruct*>(nullable->get_nested_column_ptr().get());
165
2
        nullable->get_null_map_column_ptr()->insert_many_defaults(length);
166
2
    } else {
167
0
        ret = assert_cast<ColumnStruct*>(column.get());
168
0
    }
169
170
2
    ret->get_column(0).insert_many_from(*_kv_pairs.first, _cur_offset, length);
171
2
    ret->get_column(1).insert_many_from(*_kv_pairs.second, _cur_offset, length);
172
2
}
173
174
template <bool TEXT_MODE>
175
112
int VJsonEachTableFunction<TEXT_MODE>::get_value(MutableColumnPtr& column, int max_step) {
176
112
    max_step = std::min(max_step, (int)(_cur_size - _cur_offset));
177
178
112
    if (current_empty()) {
179
30
        column->insert_default();
180
30
        max_step = 1;
181
82
    } else {
182
82
        ColumnStruct* struct_col = nullptr;
183
82
        if (_is_nullable) {
184
80
            auto* nullable_col = assert_cast<ColumnNullable*>(column.get());
185
80
            struct_col = assert_cast<ColumnStruct*>(nullable_col->get_nested_column_ptr().get());
186
80
            nullable_col->get_null_map_column_ptr()->insert_many_defaults(max_step);
187
80
        } else {
188
2
            struct_col = assert_cast<ColumnStruct*>(column.get());
189
2
        }
190
191
82
        struct_col->get_column(0).insert_range_from(*_kv_pairs.first, _cur_offset, max_step);
192
82
        struct_col->get_column(1).insert_range_from(*_kv_pairs.second, _cur_offset, max_step);
193
82
    }
194
195
112
    forward(max_step);
196
112
    return max_step;
197
112
}
_ZN5doris22VJsonEachTableFunctionILb0EE9get_valueERNS_3COWINS_7IColumnEE11mutable_ptrIS3_EEi
Line
Count
Source
175
66
int VJsonEachTableFunction<TEXT_MODE>::get_value(MutableColumnPtr& column, int max_step) {
176
66
    max_step = std::min(max_step, (int)(_cur_size - _cur_offset));
177
178
66
    if (current_empty()) {
179
16
        column->insert_default();
180
16
        max_step = 1;
181
50
    } else {
182
50
        ColumnStruct* struct_col = nullptr;
183
50
        if (_is_nullable) {
184
48
            auto* nullable_col = assert_cast<ColumnNullable*>(column.get());
185
48
            struct_col = assert_cast<ColumnStruct*>(nullable_col->get_nested_column_ptr().get());
186
48
            nullable_col->get_null_map_column_ptr()->insert_many_defaults(max_step);
187
48
        } else {
188
2
            struct_col = assert_cast<ColumnStruct*>(column.get());
189
2
        }
190
191
50
        struct_col->get_column(0).insert_range_from(*_kv_pairs.first, _cur_offset, max_step);
192
50
        struct_col->get_column(1).insert_range_from(*_kv_pairs.second, _cur_offset, max_step);
193
50
    }
194
195
66
    forward(max_step);
196
66
    return max_step;
197
66
}
_ZN5doris22VJsonEachTableFunctionILb1EE9get_valueERNS_3COWINS_7IColumnEE11mutable_ptrIS3_EEi
Line
Count
Source
175
46
int VJsonEachTableFunction<TEXT_MODE>::get_value(MutableColumnPtr& column, int max_step) {
176
46
    max_step = std::min(max_step, (int)(_cur_size - _cur_offset));
177
178
46
    if (current_empty()) {
179
14
        column->insert_default();
180
14
        max_step = 1;
181
32
    } else {
182
32
        ColumnStruct* struct_col = nullptr;
183
32
        if (_is_nullable) {
184
32
            auto* nullable_col = assert_cast<ColumnNullable*>(column.get());
185
32
            struct_col = assert_cast<ColumnStruct*>(nullable_col->get_nested_column_ptr().get());
186
32
            nullable_col->get_null_map_column_ptr()->insert_many_defaults(max_step);
187
32
        } else {
188
0
            struct_col = assert_cast<ColumnStruct*>(column.get());
189
0
        }
190
191
32
        struct_col->get_column(0).insert_range_from(*_kv_pairs.first, _cur_offset, max_step);
192
32
        struct_col->get_column(1).insert_range_from(*_kv_pairs.second, _cur_offset, max_step);
193
32
    }
194
195
46
    forward(max_step);
196
46
    return max_step;
197
46
}
198
199
// // Explicit template instantiations
200
template class VJsonEachTableFunction<false>; // json_each
201
template class VJsonEachTableFunction<true>;  // json_each_text
202
203
} // namespace doris