forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_type_object.cpp
More file actions
198 lines (172 loc) · 7.1 KB
/
Copy pathdata_type_object.cpp
File metadata and controls
198 lines (172 loc) · 7.1 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
// 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.
// This file is copied from
// https://github.com/ClickHouse/ClickHouse/blob/master/src/DataTypes/DataTypeObject.cpp
// and modified by Doris
#include "vec/data_types/data_type_object.h"
#include <gen_cpp/data.pb.h>
#include <string.h>
#include <util/string_util.h>
#include <cassert>
#include <memory>
#include <utility>
#include <vector>
#include "agent/be_exec_version_manager.h"
#include "vec/columns/column_object.h"
#include "vec/common/assert_cast.h"
#include "vec/common/typeid_cast.h"
#include "vec/core/types.h"
#include "vec/data_types/data_type.h"
#include "vec/data_types/data_type_factory.hpp"
#include "vec/json/path_in_data.h"
namespace doris {
namespace vectorized {
class IColumn;
} // namespace vectorized
} // namespace doris
namespace doris::vectorized {
#include "common/compile_check_begin.h"
DataTypeObject::DataTypeObject(const String& schema_format_, bool is_nullable_)
: schema_format(to_lower(schema_format_)), is_nullable(is_nullable_) {}
bool DataTypeObject::equals(const IDataType& rhs) const {
return typeid_cast<const DataTypeObject*>(&rhs) != nullptr;
}
int64_t DataTypeObject::get_uncompressed_serialized_bytes(const IColumn& column,
int be_exec_version) const {
const auto& column_object = assert_cast<const ColumnObject&>(column);
if (!column_object.is_finalized()) {
const_cast<ColumnObject&>(column_object).finalize();
}
const auto& subcolumns = column_object.get_subcolumns();
size_t size = 0;
size += sizeof(uint32_t);
for (const auto& entry : subcolumns) {
auto type = entry->data.get_least_common_type();
if (is_nothing(type)) {
continue;
}
PColumnMeta column_meta_pb;
column_meta_pb.set_name(entry->path.get_path());
type->to_pb_column_meta(&column_meta_pb);
std::string meta_binary;
column_meta_pb.SerializeToString(&meta_binary);
size += sizeof(uint32_t);
size += meta_binary.size();
size += type->get_uncompressed_serialized_bytes(entry->data.get_finalized_column(),
be_exec_version);
}
// serialize num of rows, only take effect when subcolumns empty
if (be_exec_version >= VARIANT_SERDE) {
size += sizeof(uint32_t);
}
return size;
}
char* DataTypeObject::serialize(const IColumn& column, char* buf, int be_exec_version) const {
const auto& column_object = assert_cast<const ColumnObject&>(column);
if (!column_object.is_finalized()) {
const_cast<ColumnObject&>(column_object).finalize();
}
#ifndef NDEBUG
// DCHECK size
column_object.check_consistency();
#endif
const auto& subcolumns = column_object.get_subcolumns();
char* size_pos = buf;
buf += sizeof(uint32_t);
size_t num_of_columns = 0;
// 2. serialize each subcolumn in a loop
for (const auto& entry : subcolumns) {
// 2.1 serialize subcolumn column meta pb (path and type)
auto type = entry->data.get_least_common_type();
if (is_nothing(type)) {
continue;
}
++num_of_columns;
PColumnMeta column_meta_pb;
column_meta_pb.set_name(entry->path.get_path());
type->to_pb_column_meta(&column_meta_pb);
std::string meta_binary;
column_meta_pb.SerializeToString(&meta_binary);
// Safe cast
*reinterpret_cast<uint32_t*>(buf) = static_cast<UInt32>(meta_binary.size());
buf += sizeof(uint32_t);
memcpy(buf, meta_binary.data(), meta_binary.size());
buf += meta_binary.size();
// 2.2 serialize subcolumn
buf = type->serialize(entry->data.get_finalized_column(), buf, be_exec_version);
}
// serialize num of subcolumns
// Safe case
*reinterpret_cast<uint32_t*>(size_pos) = static_cast<UInt32>(num_of_columns);
// serialize num of rows, only take effect when subcolumns empty
if (be_exec_version >= VARIANT_SERDE) {
*reinterpret_cast<uint32_t*>(buf) = static_cast<UInt32>(column_object.rows());
buf += sizeof(uint32_t);
}
return buf;
}
const char* DataTypeObject::deserialize(const char* buf, MutableColumnPtr* column,
int be_exec_version) const {
auto column_object = assert_cast<ColumnObject*>(column->get());
// 1. deserialize num of subcolumns
uint32_t num_subcolumns = *reinterpret_cast<const uint32_t*>(buf);
buf += sizeof(uint32_t);
// 2. deserialize each subcolumn in a loop
for (uint32_t i = 0; i < num_subcolumns; i++) {
// 2.1 deserialize subcolumn column path (str size + str data)
uint32_t size = *reinterpret_cast<const uint32_t*>(buf);
buf += sizeof(uint32_t);
std::string meta_binary {buf, size};
buf += size;
PColumnMeta column_meta_pb;
column_meta_pb.ParseFromString(meta_binary);
// 2.2 deserialize subcolumn
auto type = DataTypeFactory::instance().create_data_type(column_meta_pb);
MutableColumnPtr sub_column = type->create_column();
buf = type->deserialize(buf, &sub_column, be_exec_version);
// add subcolumn to column_object
PathInData key;
if (!column_meta_pb.name().empty()) {
key = PathInData {column_meta_pb.name()};
}
column_object->add_sub_column(key, std::move(sub_column), type);
}
size_t num_rows = 0;
// serialize num of rows, only take effect when subcolumns empty
if (be_exec_version >= VARIANT_SERDE) {
num_rows = *reinterpret_cast<const uint32_t*>(buf);
column_object->set_num_rows(num_rows);
buf += sizeof(uint32_t);
}
column_object->finalize();
#ifndef NDEBUG
// DCHECK size
column_object->check_consistency();
#endif
return buf;
}
std::string DataTypeObject::to_string(const IColumn& column, size_t row_num) const {
const auto& variant = assert_cast<const ColumnObject&>(column);
std::string res;
static_cast<void>(variant.serialize_one_row_to_string(cast_set<Int32>(row_num), &res));
return res;
}
void DataTypeObject::to_string(const IColumn& column, size_t row_num, BufferWritable& ostr) const {
const auto& variant = assert_cast<const ColumnObject&>(column);
static_cast<void>(variant.serialize_one_row_to_string(cast_set<Int32>(row_num), ostr));
}
} // namespace doris::vectorized