forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_type_struct.cpp
More file actions
438 lines (385 loc) · 15.2 KB
/
Copy pathdata_type_struct.cpp
File metadata and controls
438 lines (385 loc) · 15.2 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// 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/DataTypeTuple.cpp
// and modified by Doris
#include "vec/data_types/data_type_struct.h"
#include <ctype.h>
#include <fmt/format.h>
#include <gen_cpp/data.pb.h>
#include <glog/logging.h>
#include <string.h>
#include <algorithm>
#include <cstddef>
#include <memory>
#include <ostream>
#include <typeinfo>
#include <unordered_set>
#include <utility>
#include <vector>
#include "agent/be_exec_version_manager.h"
#include "vec/columns/column.h"
#include "vec/columns/column_const.h"
#include "vec/columns/column_nullable.h"
#include "vec/columns/column_struct.h"
#include "vec/common/assert_cast.h"
#include "vec/common/string_buffer.hpp"
#include "vec/common/string_ref.h"
#include "vec/io/reader_buffer.h"
namespace doris::vectorized {
DataTypeStruct::DataTypeStruct(const DataTypes& elems_)
: elems(elems_), have_explicit_names(false) {
/// Automatically assigned names in form of '1', '2', ...
size_t size = elems.size();
names.resize(size);
for (size_t i = 0; i < size; ++i) {
names[i] = std::to_string(i + 1);
}
}
static Status check_tuple_names(const Strings& names) {
std::unordered_set<String> names_set;
for (const auto& name : names) {
if (name.empty()) {
return Status::InvalidArgument("Names of tuple elements cannot be empty");
}
if (!names_set.insert(name).second) {
return Status::InvalidArgument("Names of tuple elements must be unique");
}
}
return {};
}
DataTypeStruct::DataTypeStruct(const DataTypes& elems_, const Strings& names_)
: elems(elems_), names(names_), have_explicit_names(true) {
size_t size = elems.size();
if (names.size() != size) {
throw doris::Exception(ErrorCode::INTERNAL_ERROR,
"Wrong number of names passed to constructor of DataTypeStruct");
__builtin_unreachable();
}
Status st = check_tuple_names(names);
}
std::string DataTypeStruct::do_get_name() const {
size_t size = elems.size();
std::stringstream s;
s << "Struct(";
for (size_t i = 0; i < size; ++i) {
if (i != 0) {
s << ", ";
}
s << names[i] << ":";
s << elems[i]->get_name();
}
s << ")";
return s.str();
}
Status DataTypeStruct::from_string(ReadBuffer& rb, IColumn* column) const {
DCHECK(!rb.eof());
auto* struct_column = assert_cast<ColumnStruct*>(column);
if (*rb.position() != '{') {
return Status::InvalidArgument("Struct does not start with '{}' character, found '{}'", "{",
*rb.position());
}
if (*(rb.end() - 1) != '}') {
return Status::InvalidArgument("Struct does not end with '{}' character, found '{}'", "}",
*(rb.end() - 1));
}
// here need handle the empty struct '{}'
if (rb.count() == 2) {
for (size_t i = 0; i < struct_column->tuple_size(); ++i) {
struct_column->get_column(i).insert_default();
}
return Status::OK();
}
++rb.position();
bool is_explicit_names = false;
std::vector<std::string> field_names;
std::vector<ReadBuffer> field_rbs;
std::vector<size_t> field_pos;
while (!rb.eof()) {
StringRef slot(rb.position(), rb.count());
bool has_quota = false;
bool is_name = false;
if (!DataTypeStructSerDe::next_slot_from_string(rb, slot, is_name, has_quota)) {
return Status::InvalidArgument("Cannot read struct field from text '{}'",
slot.to_string());
}
if (is_name) {
std::string name = slot.to_string();
if (!DataTypeStructSerDe::next_slot_from_string(rb, slot, is_name, has_quota)) {
return Status::InvalidArgument("Cannot read struct field from text '{}'",
slot.to_string());
}
ReadBuffer field_rb(const_cast<char*>(slot.data), slot.size);
field_names.push_back(name);
field_rbs.push_back(field_rb);
if (!is_explicit_names) {
is_explicit_names = true;
}
} else {
ReadBuffer field_rb(const_cast<char*>(slot.data), slot.size);
field_rbs.push_back(field_rb);
}
}
// TODO: should we support insert default field value when actual field number is less than
// schema field number?
if (field_rbs.size() != elems.size()) {
std::string cmp_str = field_rbs.size() > elems.size() ? "more" : "less";
return Status::InvalidArgument(
"Actual struct field number {} is {} than schema field number {}.",
field_rbs.size(), cmp_str, elems.size());
}
if (is_explicit_names) {
if (field_names.size() != field_rbs.size()) {
return Status::InvalidArgument(
"Struct field name number {} is not equal to field number {}.",
field_names.size(), field_rbs.size());
}
std::unordered_set<std::string> name_set;
for (size_t i = 0; i < field_names.size(); i++) {
// check duplicate fields
auto ret = name_set.insert(field_names[i]);
if (!ret.second) {
return Status::InvalidArgument("Struct field name {} is duplicate with others.",
field_names[i]);
}
// check name valid
auto idx = try_get_position_by_name(field_names[i]);
if (idx == std::nullopt) {
return Status::InvalidArgument("Cannot find struct field name {} in schema.",
field_names[i]);
}
field_pos.push_back(idx.value());
}
} else {
for (size_t i = 0; i < field_rbs.size(); i++) {
field_pos.push_back(i);
}
}
for (size_t idx = 0; idx < elems.size(); idx++) {
auto field_rb = field_rbs[field_pos[idx]];
// handle empty element
if (field_rb.count() == 0) {
struct_column->get_column(idx).insert_default();
continue;
}
// handle null element
if (field_rb.count() == 4 && strncmp(field_rb.position(), "null", 4) == 0) {
auto& nested_null_col =
reinterpret_cast<ColumnNullable&>(struct_column->get_column(idx));
nested_null_col.insert_null_elements(1);
continue;
}
auto st = elems[idx]->from_string(field_rb, &struct_column->get_column(idx));
if (!st.ok()) {
// we should do column revert if error
for (size_t j = 0; j < idx; j++) {
struct_column->get_column(j).pop_back(1);
}
return st;
}
}
return Status::OK();
}
std::string DataTypeStruct::to_string(const IColumn& column, size_t row_num) const {
auto result = check_column_const_set_readability(column, row_num);
ColumnPtr ptr = result.first;
row_num = result.second;
auto& struct_column = assert_cast<const ColumnStruct&>(*ptr);
std::string str;
str += "{";
for (size_t idx = 0; idx < elems.size(); idx++) {
if (idx != 0) {
str += ", ";
}
str += elems[idx]->to_string(struct_column.get_column(idx), row_num);
}
str += "}";
return str;
}
void DataTypeStruct::to_string(const IColumn& column, size_t row_num, BufferWritable& ostr) const {
auto result = check_column_const_set_readability(column, row_num);
ColumnPtr ptr = result.first;
row_num = result.second;
auto& struct_column = assert_cast<const ColumnStruct&>(*ptr);
ostr.write("{", 1);
for (size_t idx = 0; idx < elems.size(); idx++) {
if (idx != 0) {
ostr.write(", ", 2);
}
elems[idx]->to_string(struct_column.get_column(idx), row_num, ostr);
}
ostr.write("}", 1);
}
MutableColumnPtr DataTypeStruct::create_column() const {
size_t size = elems.size();
MutableColumns tuple_columns(size);
for (size_t i = 0; i < size; ++i) {
tuple_columns[i] = elems[i]->create_column();
}
return ColumnStruct::create(std::move(tuple_columns));
}
Field DataTypeStruct::get_default() const {
size_t size = elems.size();
Tuple t;
for (size_t i = 0; i < size; ++i) {
t.push_back(elems[i]->get_default());
}
return t;
}
bool DataTypeStruct::equals(const IDataType& rhs) const {
if (typeid(rhs) != typeid(*this)) {
return false;
}
const DataTypeStruct& rhs_tuple = static_cast<const DataTypeStruct&>(rhs);
size_t size = elems.size();
if (size != rhs_tuple.elems.size()) {
return false;
}
for (size_t i = 0; i < size; ++i) {
if (!elems[i]->equals(*rhs_tuple.elems[i])) {
return false;
}
}
return true;
}
size_t DataTypeStruct::get_position_by_name(const String& name) const {
size_t size = elems.size();
for (size_t i = 0; i < size; ++i) {
if (names[i] == name) {
return i;
}
}
throw doris::Exception(ErrorCode::INTERNAL_ERROR,
"Struct doesn't have element with name " + name);
__builtin_unreachable();
}
std::optional<size_t> DataTypeStruct::try_get_position_by_name(const String& name) const {
size_t size = elems.size();
for (size_t i = 0; i < size; ++i) {
if (names[i] == name) {
return std::optional<size_t>(i);
}
}
return std::nullopt;
}
String DataTypeStruct::get_name_by_position(size_t i) const {
return names[i];
}
// binary : const flag| row num | read saved num| childs
// childs : child1 | child2 ...
int64_t DataTypeStruct::get_uncompressed_serialized_bytes(const IColumn& column,
int be_exec_version) const {
if (be_exec_version >= USE_CONST_SERDE) {
auto size = sizeof(bool) + sizeof(size_t) + sizeof(size_t);
bool is_const_column = is_column_const(column);
const IColumn* data_column = &column;
if (is_const_column) {
const auto& const_column = assert_cast<const ColumnConst&>(column);
data_column = &(const_column.get_data_column());
}
const auto& struct_column = assert_cast<const ColumnStruct&>(*data_column);
DCHECK(elems.size() == struct_column.tuple_size());
int64_t bytes = 0;
for (size_t i = 0; i < elems.size(); ++i) {
bytes += elems[i]->get_uncompressed_serialized_bytes(struct_column.get_column(i),
be_exec_version);
}
return size + bytes;
} else {
auto ptr = column.convert_to_full_column_if_const();
const auto& struct_column = assert_cast<const ColumnStruct&>(*ptr.get());
DCHECK(elems.size() == struct_column.tuple_size());
int64_t bytes = 0;
for (size_t i = 0; i < elems.size(); ++i) {
bytes += elems[i]->get_uncompressed_serialized_bytes(struct_column.get_column(i),
be_exec_version);
}
return bytes;
}
}
char* DataTypeStruct::serialize(const IColumn& column, char* buf, int be_exec_version) const {
if (be_exec_version >= USE_CONST_SERDE) {
const auto* data_column = &column;
[[maybe_unused]] size_t real_need_copy_num = 0;
buf = serialize_const_flag_and_row_num(&data_column, buf, &real_need_copy_num);
const auto& struct_column = assert_cast<const ColumnStruct&>(*data_column);
DCHECK(elems.size() == struct_column.tuple_size());
for (size_t i = 0; i < elems.size(); ++i) {
buf = elems[i]->serialize(struct_column.get_column(i), buf, be_exec_version);
}
return buf;
} else {
auto ptr = column.convert_to_full_column_if_const();
const auto& struct_column = assert_cast<const ColumnStruct&>(*ptr.get());
DCHECK(elems.size() == struct_column.tuple_size());
for (size_t i = 0; i < elems.size(); ++i) {
buf = elems[i]->serialize(struct_column.get_column(i), buf, be_exec_version);
}
return buf;
}
}
const char* DataTypeStruct::deserialize(const char* buf, MutableColumnPtr* column,
int be_exec_version) const {
if (be_exec_version >= USE_CONST_SERDE) {
auto* origin_column = column->get();
[[maybe_unused]] size_t real_have_saved_num = 0;
buf = deserialize_const_flag_and_row_num(buf, column, &real_have_saved_num);
auto* struct_column = assert_cast<ColumnStruct*>(origin_column);
DCHECK(elems.size() == struct_column->tuple_size());
for (size_t i = 0; i < elems.size(); ++i) {
auto child_column = struct_column->get_column_ptr(i)->assume_mutable();
buf = elems[i]->deserialize(buf, &child_column, be_exec_version);
}
return buf;
} else {
auto* struct_column = assert_cast<ColumnStruct*>(column->get());
DCHECK(elems.size() == struct_column->tuple_size());
for (size_t i = 0; i < elems.size(); ++i) {
auto child_column = struct_column->get_column_ptr(i)->assume_mutable();
buf = elems[i]->deserialize(buf, &child_column, be_exec_version);
}
return buf;
}
}
void DataTypeStruct::to_pb_column_meta(PColumnMeta* col_meta) const {
IDataType::to_pb_column_meta(col_meta);
for (size_t i = 0; i < elems.size(); ++i) {
auto child = col_meta->add_children();
child->set_name(names[i]);
elems[i]->to_pb_column_meta(child);
}
}
bool DataTypeStruct::text_can_contain_only_valid_utf8() const {
return std::all_of(elems.begin(), elems.end(),
[](auto&& elem) { return elem->text_can_contain_only_valid_utf8(); });
}
bool DataTypeStruct::have_maximum_size_of_value() const {
return std::all_of(elems.begin(), elems.end(),
[](auto&& elem) { return elem->have_maximum_size_of_value(); });
}
bool DataTypeStruct::is_comparable() const {
return std::all_of(elems.begin(), elems.end(),
[](auto&& elem) { return elem->is_comparable(); });
}
size_t DataTypeStruct::get_size_of_value_in_memory() const {
size_t res = 0;
for (const auto& elem : elems) {
res += elem->get_size_of_value_in_memory();
}
return res;
}
} // namespace doris::vectorized