forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvexplode_json_object.cpp
More file actions
161 lines (146 loc) · 6.79 KB
/
Copy pathvexplode_json_object.cpp
File metadata and controls
161 lines (146 loc) · 6.79 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
// 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 "exprs/table_function/vexplode_json_object.h"
#include <glog/logging.h>
#include <ostream>
#include "common/status.h"
#include "core/block/block.h"
#include "core/block/column_with_type_and_name.h"
#include "core/column/column.h"
#include "core/column/column_struct.h"
#include "core/string_ref.h"
#include "exprs/vexpr.h"
#include "exprs/vexpr_context.h"
#include "util/jsonb_document.h"
#include "util/jsonb_writer.h"
namespace doris {
VExplodeJsonObjectTableFunction::VExplodeJsonObjectTableFunction() {
_fn_name = "vexplode_json_object";
}
Status VExplodeJsonObjectTableFunction::process_init(Block* block, RuntimeState* state) {
CHECK(_expr_context->root()->children().size() == 1)
<< "VExplodeJsonObjectTableFunction only support 1 child but has "
<< _expr_context->root()->children().size();
RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute_column(
_expr_context.get(), block, nullptr, block->rows(), _json_object_column));
return Status::OK();
}
void VExplodeJsonObjectTableFunction::process_row(size_t row_idx) {
TableFunction::process_row(row_idx);
StringRef text = _json_object_column->get_data_at(row_idx);
if (text.data != nullptr) {
const JsonbDocument* doc = nullptr;
auto st = JsonbDocument::checkAndCreateDocument(text.data, text.size, &doc);
if (!st.ok() || !doc || !doc->getValue()) [[unlikely]] {
// error jsonb, put null into output, cur_size = 0 , we will insert_default
return;
}
// value is NOT necessary to be deleted since JsonbValue will not allocate memory
const JsonbValue* value = doc->getValue();
auto writer = std::make_unique<JsonbWriter>();
if (value->isObject()) {
_cur_size = value->numElements();
const auto* obj = value->unpack<ObjectVal>();
_object_pairs.first =
ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
_object_pairs.second =
ColumnNullable::create(ColumnString::create(), ColumnUInt8::create());
_object_pairs.first->reserve(_cur_size);
_object_pairs.second->reserve(_cur_size);
for (const auto& it : *obj) {
_object_pairs.first->insert_data(it.getKeyStr(), it.klen());
writer->reset();
writer->writeValue(it.value());
if (it.value()->isNull()) {
_object_pairs.second->insert_default();
} else {
const std::string_view& jsonb_value = std::string_view(
writer->getOutput()->getBuffer(), writer->getOutput()->getSize());
_object_pairs.second->insert_data(jsonb_value.data(), jsonb_value.size());
}
}
}
// we do not support other json type except object
}
}
void VExplodeJsonObjectTableFunction::process_close() {
_json_object_column = nullptr;
_object_pairs.first = nullptr;
_object_pairs.second = nullptr;
}
void VExplodeJsonObjectTableFunction::get_same_many_values(MutableColumnPtr& column, int length) {
// if current is empty map row, also append a default value
if (current_empty()) {
column->insert_many_defaults(length);
return;
}
ColumnStruct* ret = nullptr;
// this _is_nullable is whole output column's nullable
if (_is_nullable) {
// make map kv value into struct
ret = assert_cast<ColumnStruct*>(
assert_cast<ColumnNullable*>(column.get())->get_nested_column_ptr().get());
assert_cast<ColumnNullable*>(column.get())
->get_null_map_column_ptr()
->insert_many_defaults(length);
} else if (is_column<ColumnStruct>(column.get())) {
ret = assert_cast<ColumnStruct*>(column.get());
} else {
throw Exception(ErrorCode::INTERNAL_ERROR,
"only support expand json object int to struct(kv pair), but given: ",
column->get_name());
}
if (!ret || ret->tuple_size() != 2) {
throw Exception(ErrorCode::INTERNAL_ERROR,
"only support expand json object int to kv pair column, but given: ",
ret->tuple_size());
}
ret->get_column(0).insert_many_from(*_object_pairs.first, _cur_offset, length);
ret->get_column(1).insert_many_from(*_object_pairs.second, _cur_offset, length);
}
int VExplodeJsonObjectTableFunction::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_column = nullptr;
if (_is_nullable) {
auto* nullable_column = assert_cast<ColumnNullable*>(column.get());
struct_column =
assert_cast<ColumnStruct*>(nullable_column->get_nested_column_ptr().get());
auto* nullmap_column = nullable_column->get_null_map_column_ptr().get();
// here nullmap_column insert max_step many defaults as if MAP[row_idx] is NULL
// will be not update value, _cur_size = 0, means current_empty;
// so here could insert directly
nullmap_column->insert_many_defaults(max_step);
} else {
struct_column = assert_cast<ColumnStruct*>(column.get());
}
if (!struct_column || struct_column->tuple_size() != 2) {
throw Exception(ErrorCode::INTERNAL_ERROR,
"only support expand json object int to kv pair column, but given: ",
struct_column->tuple_size());
}
struct_column->get_column(0).insert_range_from(*_object_pairs.first, _cur_offset, max_step);
struct_column->get_column(1).insert_range_from(*_object_pairs.second, _cur_offset,
max_step);
}
forward(max_step);
return max_step;
}
} // namespace doris