Coverage Report

Created: 2026-03-28 19:32

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