Coverage Report

Created: 2026-03-28 07:45

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
23
VJsonEachTableFunction<TEXT_MODE>::VJsonEachTableFunction() {
44
23
    _fn_name = TEXT_MODE ? "vjson_each_text" : "vjson_each";
45
23
}
_ZN5doris22VJsonEachTableFunctionILb0EEC2Ev
Line
Count
Source
43
17
VJsonEachTableFunction<TEXT_MODE>::VJsonEachTableFunction() {
44
17
    _fn_name = TEXT_MODE ? "vjson_each_text" : "vjson_each";
45
17
}
_ZN5doris22VJsonEachTableFunctionILb1EEC2Ev
Line
Count
Source
43
6
VJsonEachTableFunction<TEXT_MODE>::VJsonEachTableFunction() {
44
6
    _fn_name = TEXT_MODE ? "vjson_each_text" : "vjson_each";
45
6
}
46
47
template <bool TEXT_MODE>
48
39
Status VJsonEachTableFunction<TEXT_MODE>::process_init(Block* block, RuntimeState* /*state*/) {
49
39
    int value_column_idx = -1;
50
39
    RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute(_expr_context.get(), block,
51
39
                                                                  &value_column_idx));
52
39
    auto [col, is_const] = unpack_if_const(block->get_by_position(value_column_idx).column);
53
39
    _json_column = col;
54
39
    _is_const = is_const;
55
39
    return Status::OK();
56
39
}
_ZN5doris22VJsonEachTableFunctionILb0EE12process_initEPNS_5BlockEPNS_12RuntimeStateE
Line
Count
Source
48
26
Status VJsonEachTableFunction<TEXT_MODE>::process_init(Block* block, RuntimeState* /*state*/) {
49
26
    int value_column_idx = -1;
50
26
    RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute(_expr_context.get(), block,
51
26
                                                                  &value_column_idx));
52
26
    auto [col, is_const] = unpack_if_const(block->get_by_position(value_column_idx).column);
53
26
    _json_column = col;
54
26
    _is_const = is_const;
55
26
    return Status::OK();
56
26
}
_ZN5doris22VJsonEachTableFunctionILb1EE12process_initEPNS_5BlockEPNS_12RuntimeStateE
Line
Count
Source
48
13
Status VJsonEachTableFunction<TEXT_MODE>::process_init(Block* block, RuntimeState* /*state*/) {
49
13
    int value_column_idx = -1;
50
13
    RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute(_expr_context.get(), block,
51
13
                                                                  &value_column_idx));
52
13
    auto [col, is_const] = unpack_if_const(block->get_by_position(value_column_idx).column);
53
13
    _json_column = col;
54
13
    _is_const = is_const;
55
13
    return Status::OK();
56
13
}
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
12
static void insert_value_as_text(const JsonbValue* value, MutableColumnPtr& col) {
63
12
    if (value == nullptr || value->isNull()) {
64
1
        col->insert_default();
65
1
        return;
66
1
    }
67
11
    if (value->isString()) {
68
6
        const auto* str_val = value->unpack<JsonbStringVal>();
69
6
        col->insert_data(str_val->getBlob(), str_val->getBlobLen());
70
6
    } else {
71
5
        JsonbToJson converter;
72
5
        std::string text = converter.to_json_string(value);
73
5
        col->insert_data(text.data(), text.size());
74
5
    }
75
11
}
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
26
                                 JsonbWriter& writer) {
82
26
    if (value == nullptr || value->isNull()) {
83
2
        col->insert_default();
84
2
        return;
85
2
    }
86
24
    writer.reset();
87
24
    writer.writeValue(value);
88
24
    const auto* buf = writer.getOutput()->getBuffer();
89
24
    size_t len = writer.getOutput()->getSize();
90
24
    col->insert_data(buf, len);
91
24
}
92
93
template <bool TEXT_MODE>
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
1
        return;
98
1
    }
99
100
40
    StringRef text;
101
40
    const size_t idx = _is_const ? 0 : row_idx;
102
40
    if (const auto* nullable_col = check_and_get_column<ColumnNullable>(*_json_column)) {
103
40
        if (nullable_col->is_null_at(idx)) {
104
5
            return;
105
5
        }
106
35
        text = assert_cast<const ColumnString&>(nullable_col->get_nested_column()).get_data_at(idx);
107
35
    } else {
108
0
        text = assert_cast<const ColumnString&>(*_json_column).get_data_at(idx);
109
0
    }
110
111
35
    const JsonbDocument* doc = nullptr;
112
35
    auto st = JsonbDocument::checkAndCreateDocument(text.data, text.size, &doc);
113
35
    if (!st.ok() || !doc || !doc->getValue()) [[unlikely]] {
114
3
        return;
115
3
    }
116
117
32
    const JsonbValue* jv = doc->getValue();
118
32
    if (!jv->isObject()) {
119
4
        return;
120
4
    }
121
122
28
    const auto* obj = jv->unpack<ObjectVal>();
123
28
    _cur_size = obj->numElem();
124
28
    if (_cur_size == 0) {
125
7
        return;
126
7
    }
127
128
21
    _kv_pairs.first = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
129
21
    _kv_pairs.second = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
130
21
    _kv_pairs.first->reserve(_cur_size);
131
21
    _kv_pairs.second->reserve(_cur_size);
132
133
21
    if constexpr (TEXT_MODE) {
134
12
        for (const auto& kv : *obj) {
135
12
            _kv_pairs.first->insert_data(kv.getKeyStr(), kv.klen());
136
12
            insert_value_as_text(kv.value(), _kv_pairs.second);
137
12
        }
138
15
    } else {
139
15
        JsonbWriter writer;
140
26
        for (const auto& kv : *obj) {
141
26
            _kv_pairs.first->insert_data(kv.getKeyStr(), kv.klen());
142
26
            insert_value_as_json(kv.value(), _kv_pairs.second, writer);
143
26
        }
144
15
    }
145
21
}
_ZN5doris22VJsonEachTableFunctionILb0EE11process_rowEm
Line
Count
Source
94
28
void VJsonEachTableFunction<TEXT_MODE>::process_row(size_t row_idx) {
95
28
    TableFunction::process_row(row_idx);
96
28
    if (_is_const && _cur_size > 0) {
97
1
        return;
98
1
    }
99
100
27
    StringRef text;
101
27
    const size_t idx = _is_const ? 0 : row_idx;
102
27
    if (const auto* nullable_col = check_and_get_column<ColumnNullable>(*_json_column)) {
103
27
        if (nullable_col->is_null_at(idx)) {
104
3
            return;
105
3
        }
106
24
        text = assert_cast<const ColumnString&>(nullable_col->get_nested_column()).get_data_at(idx);
107
24
    } else {
108
0
        text = assert_cast<const ColumnString&>(*_json_column).get_data_at(idx);
109
0
    }
110
111
24
    const JsonbDocument* doc = nullptr;
112
24
    auto st = JsonbDocument::checkAndCreateDocument(text.data, text.size, &doc);
113
24
    if (!st.ok() || !doc || !doc->getValue()) [[unlikely]] {
114
1
        return;
115
1
    }
116
117
23
    const JsonbValue* jv = doc->getValue();
118
23
    if (!jv->isObject()) {
119
3
        return;
120
3
    }
121
122
20
    const auto* obj = jv->unpack<ObjectVal>();
123
20
    _cur_size = obj->numElem();
124
20
    if (_cur_size == 0) {
125
5
        return;
126
5
    }
127
128
15
    _kv_pairs.first = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
129
15
    _kv_pairs.second = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
130
15
    _kv_pairs.first->reserve(_cur_size);
131
15
    _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
15
    } else {
139
15
        JsonbWriter writer;
140
26
        for (const auto& kv : *obj) {
141
26
            _kv_pairs.first->insert_data(kv.getKeyStr(), kv.klen());
142
26
            insert_value_as_json(kv.value(), _kv_pairs.second, writer);
143
26
        }
144
15
    }
145
15
}
_ZN5doris22VJsonEachTableFunctionILb1EE11process_rowEm
Line
Count
Source
94
13
void VJsonEachTableFunction<TEXT_MODE>::process_row(size_t row_idx) {
95
13
    TableFunction::process_row(row_idx);
96
13
    if (_is_const && _cur_size > 0) {
97
0
        return;
98
0
    }
99
100
13
    StringRef text;
101
13
    const size_t idx = _is_const ? 0 : row_idx;
102
13
    if (const auto* nullable_col = check_and_get_column<ColumnNullable>(*_json_column)) {
103
13
        if (nullable_col->is_null_at(idx)) {
104
2
            return;
105
2
        }
106
11
        text = assert_cast<const ColumnString&>(nullable_col->get_nested_column()).get_data_at(idx);
107
11
    } else {
108
0
        text = assert_cast<const ColumnString&>(*_json_column).get_data_at(idx);
109
0
    }
110
111
11
    const JsonbDocument* doc = nullptr;
112
11
    auto st = JsonbDocument::checkAndCreateDocument(text.data, text.size, &doc);
113
11
    if (!st.ok() || !doc || !doc->getValue()) [[unlikely]] {
114
2
        return;
115
2
    }
116
117
9
    const JsonbValue* jv = doc->getValue();
118
9
    if (!jv->isObject()) {
119
1
        return;
120
1
    }
121
122
8
    const auto* obj = jv->unpack<ObjectVal>();
123
8
    _cur_size = obj->numElem();
124
8
    if (_cur_size == 0) {
125
2
        return;
126
2
    }
127
128
6
    _kv_pairs.first = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
129
6
    _kv_pairs.second = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
130
6
    _kv_pairs.first->reserve(_cur_size);
131
6
    _kv_pairs.second->reserve(_cur_size);
132
133
6
    if constexpr (TEXT_MODE) {
134
12
        for (const auto& kv : *obj) {
135
12
            _kv_pairs.first->insert_data(kv.getKeyStr(), kv.klen());
136
12
            insert_value_as_text(kv.value(), _kv_pairs.second);
137
12
        }
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
6
}
146
147
template <bool TEXT_MODE>
148
39
void VJsonEachTableFunction<TEXT_MODE>::process_close() {
149
39
    _json_column = nullptr;
150
39
    _kv_pairs.first = nullptr;
151
39
    _kv_pairs.second = nullptr;
152
39
    _cur_size = 0;
153
39
}
_ZN5doris22VJsonEachTableFunctionILb0EE13process_closeEv
Line
Count
Source
148
26
void VJsonEachTableFunction<TEXT_MODE>::process_close() {
149
26
    _json_column = nullptr;
150
26
    _kv_pairs.first = nullptr;
151
26
    _kv_pairs.second = nullptr;
152
26
    _cur_size = 0;
153
26
}
_ZN5doris22VJsonEachTableFunctionILb1EE13process_closeEv
Line
Count
Source
148
13
void VJsonEachTableFunction<TEXT_MODE>::process_close() {
149
13
    _json_column = nullptr;
150
13
    _kv_pairs.first = nullptr;
151
13
    _kv_pairs.second = nullptr;
152
13
    _cur_size = 0;
153
13
}
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
28
int VJsonEachTableFunction<TEXT_MODE>::get_value(MutableColumnPtr& column, int max_step) {
178
28
    max_step = std::min(max_step, (int)(_cur_size - _cur_offset));
179
180
28
    if (current_empty()) {
181
9
        column->insert_default();
182
9
        max_step = 1;
183
19
    } else {
184
19
        ColumnStruct* struct_col = nullptr;
185
19
        if (_is_nullable) {
186
18
            auto* nullable_col = assert_cast<ColumnNullable*>(column.get());
187
18
            struct_col = assert_cast<ColumnStruct*>(nullable_col->get_nested_column_ptr().get());
188
18
            assert_cast<ColumnUInt8*>(nullable_col->get_null_map_column_ptr().get())
189
18
                    ->insert_many_defaults(max_step);
190
18
        } else {
191
1
            struct_col = assert_cast<ColumnStruct*>(column.get());
192
1
        }
193
194
19
        struct_col->get_column(0).insert_range_from(*_kv_pairs.first, _cur_offset, max_step);
195
19
        struct_col->get_column(1).insert_range_from(*_kv_pairs.second, _cur_offset, max_step);
196
19
    }
197
198
28
    forward(max_step);
199
28
    return max_step;
200
28
}
_ZN5doris22VJsonEachTableFunctionILb0EE9get_valueERNS_3COWINS_7IColumnEE11mutable_ptrIS3_EEi
Line
Count
Source
177
16
int VJsonEachTableFunction<TEXT_MODE>::get_value(MutableColumnPtr& column, int max_step) {
178
16
    max_step = std::min(max_step, (int)(_cur_size - _cur_offset));
179
180
16
    if (current_empty()) {
181
5
        column->insert_default();
182
5
        max_step = 1;
183
11
    } else {
184
11
        ColumnStruct* struct_col = nullptr;
185
11
        if (_is_nullable) {
186
10
            auto* nullable_col = assert_cast<ColumnNullable*>(column.get());
187
10
            struct_col = assert_cast<ColumnStruct*>(nullable_col->get_nested_column_ptr().get());
188
10
            assert_cast<ColumnUInt8*>(nullable_col->get_null_map_column_ptr().get())
189
10
                    ->insert_many_defaults(max_step);
190
10
        } else {
191
1
            struct_col = assert_cast<ColumnStruct*>(column.get());
192
1
        }
193
194
11
        struct_col->get_column(0).insert_range_from(*_kv_pairs.first, _cur_offset, max_step);
195
11
        struct_col->get_column(1).insert_range_from(*_kv_pairs.second, _cur_offset, max_step);
196
11
    }
197
198
16
    forward(max_step);
199
16
    return max_step;
200
16
}
_ZN5doris22VJsonEachTableFunctionILb1EE9get_valueERNS_3COWINS_7IColumnEE11mutable_ptrIS3_EEi
Line
Count
Source
177
12
int VJsonEachTableFunction<TEXT_MODE>::get_value(MutableColumnPtr& column, int max_step) {
178
12
    max_step = std::min(max_step, (int)(_cur_size - _cur_offset));
179
180
12
    if (current_empty()) {
181
4
        column->insert_default();
182
4
        max_step = 1;
183
8
    } else {
184
8
        ColumnStruct* struct_col = nullptr;
185
8
        if (_is_nullable) {
186
8
            auto* nullable_col = assert_cast<ColumnNullable*>(column.get());
187
8
            struct_col = assert_cast<ColumnStruct*>(nullable_col->get_nested_column_ptr().get());
188
8
            assert_cast<ColumnUInt8*>(nullable_col->get_null_map_column_ptr().get())
189
8
                    ->insert_many_defaults(max_step);
190
8
        } else {
191
0
            struct_col = assert_cast<ColumnStruct*>(column.get());
192
0
        }
193
194
8
        struct_col->get_column(0).insert_range_from(*_kv_pairs.first, _cur_offset, max_step);
195
8
        struct_col->get_column(1).insert_range_from(*_kv_pairs.second, _cur_offset, max_step);
196
8
    }
197
198
12
    forward(max_step);
199
12
    return max_step;
200
12
}
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