forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialize.cpp
More file actions
146 lines (137 loc) · 5.84 KB
/
Copy pathserialize.cpp
File metadata and controls
146 lines (137 loc) · 5.84 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
// 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/jsonb/serialize.h"
#include <assert.h>
#include <algorithm>
#include <memory>
#include <unordered_set>
#include <vector>
#include "olap/tablet_schema.h"
#include "runtime/descriptors.h"
#include "runtime/jsonb_value.h"
#include "runtime/primitive_type.h"
#include "runtime/types.h"
#include "util/jsonb_document.h"
#include "util/jsonb_stream.h"
#include "util/jsonb_writer.h"
#include "vec/columns/column.h"
#include "vec/columns/column_string.h"
#include "vec/common/arena.h"
#include "vec/common/string_ref.h"
#include "vec/core/column_with_type_and_name.h"
#include "vec/core/columns_with_type_and_name.h"
#include "vec/data_types/data_type.h"
#include "vec/data_types/serde/data_type_serde.h"
#include "vec/io/reader_buffer.h"
namespace doris::vectorized {
void JsonbSerializeUtil::block_to_jsonb(const TabletSchema& schema, const Block& block,
ColumnString& dst, int num_cols,
const DataTypeSerDeSPtrs& serdes,
const std::unordered_set<int32_t>& row_store_cids) {
auto num_rows = block.rows();
Arena pool;
assert(num_cols <= block.columns());
for (int i = 0; i < num_rows; ++i) {
JsonbWriterT<JsonbOutStream> jsonb_writer;
jsonb_writer.writeStartObject();
for (int j = 0; j < num_cols; ++j) {
const auto& column = block.get_by_position(j).column;
const auto& tablet_column = *schema.columns()[j];
// ignore row store columns
if (tablet_column.is_row_store_column()) {
continue;
}
// TODO improve performance for checking column in group
if (row_store_cids.empty() || row_store_cids.contains(tablet_column.unique_id())) {
serdes[j]->write_one_cell_to_jsonb(*column, jsonb_writer, &pool,
tablet_column.unique_id(), i);
}
}
jsonb_writer.writeEndObject();
dst.insert_data(jsonb_writer.getOutput()->getBuffer(), jsonb_writer.getOutput()->getSize());
}
}
// batch rows
Status JsonbSerializeUtil::jsonb_to_block(
const DataTypeSerDeSPtrs& serdes, const ColumnString& jsonb_column,
const std::unordered_map<uint32_t, uint32_t>& col_id_to_idx, Block& dst,
const std::vector<std::string>& default_values,
const std::unordered_set<int>& include_cids) {
for (int i = 0; i < jsonb_column.size(); ++i) {
StringRef jsonb_data = jsonb_column.get_data_at(i);
RETURN_IF_ERROR(jsonb_to_block(serdes, jsonb_data.data, jsonb_data.size, col_id_to_idx, dst,
default_values, include_cids));
}
return Status::OK();
}
// single row
Status JsonbSerializeUtil::jsonb_to_block(
const DataTypeSerDeSPtrs& serdes, const char* data, size_t size,
const std::unordered_map<uint32_t, uint32_t>& col_id_to_idx, Block& dst,
const std::vector<std::string>& default_values,
const std::unordered_set<int>& include_cids) {
auto pdoc = JsonbDocument::checkAndCreateDocument(data, size);
JsonbDocument& doc = *pdoc;
size_t num_rows = dst.rows();
size_t filled_columns = 0;
for (auto it = doc->begin(); it != doc->end(); ++it) {
auto col_it = col_id_to_idx.find(it->getKeyId());
if (col_it != col_id_to_idx.end() &&
(include_cids.empty() || include_cids.contains(it->getKeyId()))) {
MutableColumnPtr dst_column =
dst.get_by_position(col_it->second).column->assume_mutable();
serdes[col_it->second]->read_one_cell_from_jsonb(*dst_column, it->value());
++filled_columns;
}
}
if (filled_columns >= dst.columns()) {
return Status::OK();
}
auto fill_column = [&](Block& dst, int pos, size_t old_num_rows) {
MutableColumnPtr dst_column = dst.get_by_position(pos).column->assume_mutable();
if (dst_column->size() < old_num_rows + 1) {
DCHECK(dst_column->size() == old_num_rows);
if (default_values[pos].empty()) {
dst_column->insert_default();
} else {
Slice value(default_values[pos].data(), default_values[pos].size());
DataTypeSerDe::FormatOptions opt;
opt.converted_from_string = true;
RETURN_IF_ERROR(
serdes[pos]->deserialize_one_cell_from_json(*dst_column, value, opt));
}
}
DCHECK(dst_column->size() == num_rows + 1);
return Status::OK();
};
// fill missing column
if (!include_cids.empty()) {
for (auto cid : include_cids) {
auto col_it = col_id_to_idx.find(cid);
if (col_it == col_id_to_idx.end()) {
continue;
}
RETURN_IF_ERROR(fill_column(dst, col_it->second, num_rows));
}
} else {
for (int i = 0; i < dst.columns(); ++i) {
RETURN_IF_ERROR(fill_column(dst, i, num_rows));
}
}
return Status::OK();
}
} // namespace doris::vectorized