-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathvjson_each.cpp
More file actions
206 lines (180 loc) · 7.34 KB
/
vjson_each.cpp
File metadata and controls
206 lines (180 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "vec/exprs/table_function/vjson_each.h"
#include <glog/logging.h>
#include <ostream>
#include <string>
#include "common/status.h"
#include "util/jsonb_document.h"
#include "util/jsonb_utils.h"
#include "util/jsonb_writer.h"
#include "vec/columns/column.h"
#include "vec/columns/column_const.h"
#include "vec/columns/column_struct.h"
#include "vec/common/assert_cast.h"
#include "vec/common/string_ref.h"
#include "vec/core/block.h"
#include "vec/core/column_with_type_and_name.h"
#include "vec/exprs/vexpr.h"
#include "vec/exprs/vexpr_context.h"
namespace doris::vectorized {
#include "common/compile_check_begin.h"
template <bool TEXT_MODE>
VJsonEachTableFunction<TEXT_MODE>::VJsonEachTableFunction() {
_fn_name = TEXT_MODE ? "vjson_each_text" : "vjson_each";
}
template <bool TEXT_MODE>
Status VJsonEachTableFunction<TEXT_MODE>::process_init(Block* block, RuntimeState* /*state*/) {
int value_column_idx = -1;
RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute(_expr_context.get(), block,
&value_column_idx));
auto [col, is_const] = unpack_if_const(block->get_by_position(value_column_idx).column);
_json_column = col;
_is_const = is_const;
return Status::OK();
}
// Helper: insert one JsonbValue as plain text into a ColumnNullable<ColumnString>.
// For strings: raw blob content (quotes stripped, matching json_each_text PG semantics).
// For null JSON values: SQL NULL (insert_default).
// For all others (numbers, bools, objects, arrays): JSON text representation.
static void insert_value_as_text(const JsonbValue* value, MutableColumnPtr& col) {
if (value == nullptr || value->isNull()) {
col->insert_default();
return;
}
if (value->isString()) {
const auto* str_val = value->unpack<JsonbStringVal>();
col->insert_data(str_val->getBlob(), str_val->getBlobLen());
} else {
JsonbToJson converter;
std::string text = converter.to_json_string(value);
col->insert_data(text.data(), text.size());
}
}
// Helper: insert one JsonbValue in JSONB binary form into a ColumnNullable<ColumnString>.
// For null JSON values: SQL NULL (insert_default).
// For all others: write JSONB binary via JsonbWriter.
static void insert_value_as_json(const JsonbValue* value, MutableColumnPtr& col,
JsonbWriter& writer) {
if (value == nullptr || value->isNull()) {
col->insert_default();
return;
}
writer.reset();
writer.writeValue(value);
const auto* buf = writer.getOutput()->getBuffer();
size_t len = writer.getOutput()->getSize();
col->insert_data(buf, len);
}
template <bool TEXT_MODE>
void VJsonEachTableFunction<TEXT_MODE>::process_row(size_t row_idx) {
TableFunction::process_row(row_idx);
if (_is_const && _cur_size > 0) {
return;
}
StringRef text;
const size_t idx = _is_const ? 0 : row_idx;
if (const auto* nullable_col = check_and_get_column<ColumnNullable>(*_json_column)) {
if (nullable_col->is_null_at(idx)) {
return;
}
text = assert_cast<const ColumnString&>(nullable_col->get_nested_column()).get_data_at(idx);
} else {
text = assert_cast<const ColumnString&>(*_json_column).get_data_at(idx);
}
const JsonbDocument* doc = nullptr;
auto st = JsonbDocument::checkAndCreateDocument(text.data, text.size, &doc);
if (!st.ok() || !doc || !doc->getValue()) [[unlikely]] {
return;
}
const JsonbValue* jv = doc->getValue();
if (!jv->isObject()) {
return;
}
const auto* obj = jv->unpack<ObjectVal>();
_cur_size = obj->numElem();
if (_cur_size == 0) {
return;
}
_kv_pairs.first = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
_kv_pairs.second = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
_kv_pairs.first->reserve(_cur_size);
_kv_pairs.second->reserve(_cur_size);
if constexpr (TEXT_MODE) {
for (const auto& kv : *obj) {
_kv_pairs.first->insert_data(kv.getKeyStr(), kv.klen());
insert_value_as_text(kv.value(), _kv_pairs.second);
}
} else {
JsonbWriter writer;
for (const auto& kv : *obj) {
_kv_pairs.first->insert_data(kv.getKeyStr(), kv.klen());
insert_value_as_json(kv.value(), _kv_pairs.second, writer);
}
}
}
template <bool TEXT_MODE>
void VJsonEachTableFunction<TEXT_MODE>::process_close() {
_json_column = nullptr;
_kv_pairs.first = nullptr;
_kv_pairs.second = nullptr;
}
template <bool TEXT_MODE>
void VJsonEachTableFunction<TEXT_MODE>::get_same_many_values(MutableColumnPtr& column, int length) {
if (current_empty()) {
column->insert_many_defaults(length);
return;
}
ColumnStruct* ret;
if (_is_nullable) {
auto* nullable = assert_cast<ColumnNullable*>(column.get());
ret = assert_cast<ColumnStruct*>(nullable->get_nested_column_ptr().get());
assert_cast<ColumnUInt8*>(nullable->get_null_map_column_ptr().get())
->insert_many_defaults(length);
} else {
ret = assert_cast<ColumnStruct*>(column.get());
}
ret->get_column(0).insert_many_from(*_kv_pairs.first, _cur_offset, length);
ret->get_column(1).insert_many_from(*_kv_pairs.second, _cur_offset, length);
}
template <bool TEXT_MODE>
int VJsonEachTableFunction<TEXT_MODE>::get_value(MutableColumnPtr& column, int max_step) {
max_step = std::min(max_step, (int)(_cur_size - _cur_offset));
if (current_empty()) {
column->insert_default();
max_step = 1;
} else {
ColumnStruct* struct_col = nullptr;
if (_is_nullable) {
auto* nullable_col = assert_cast<ColumnNullable*>(column.get());
struct_col = assert_cast<ColumnStruct*>(nullable_col->get_nested_column_ptr().get());
assert_cast<ColumnUInt8*>(nullable_col->get_null_map_column_ptr().get())
->insert_many_defaults(max_step);
} else {
struct_col = assert_cast<ColumnStruct*>(column.get());
}
struct_col->get_column(0).insert_range_from(*_kv_pairs.first, _cur_offset, max_step);
struct_col->get_column(1).insert_range_from(*_kv_pairs.second, _cur_offset, max_step);
}
forward(max_step);
return max_step;
}
// // Explicit template instantiations
template class VJsonEachTableFunction<false>; // json_each
template class VJsonEachTableFunction<true>; // json_each_text
#include "common/compile_check_end.h"
} // namespace doris::vectorized