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
250 lines (218 loc) · 8.44 KB
/
Copy pathdata_type_struct.cpp
File metadata and controls
250 lines (218 loc) · 8.44 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
// 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 "core/data_type/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 "common/status.h"
#include "core/assert_cast.h"
#include "core/column/column.h"
#include "core/column/column_const.h"
#include "core/column/column_nullable.h"
#include "core/column/column_struct.h"
#include "core/string_buffer.hpp"
#include "core/string_ref.h"
#include "util/string_util.h"
namespace doris {
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 struct elements cannot be empty");
}
if (!names_set.insert(name).second) {
return Status::InvalidArgument("Names of struct 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();
}
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));
}
Status DataTypeStruct::check_column(const IColumn& column) const {
const auto* column_struct = DORIS_TRY(check_column_nested_type<ColumnStruct>(column));
if (elems.size() != column_struct->tuple_size()) {
return Status::InvalidArgument(
"ColumnStruct has {} elements, but DataTypeStruct has {} elements",
column_struct->tuple_size(), elems.size());
}
for (size_t i = 0; i < elems.size(); ++i) {
const auto& elem = elems[i];
const auto& child_column = column_struct->get_column(i);
RETURN_IF_ERROR(elem->check_column(child_column));
}
return Status::OK();
}
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 (iequal(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 {
// Struct field names are canonically lower-cased by the FE, but a query may reference a field
// with different casing (e.g. migrated Iceberg tables store sName as sname). Match the field
// name case-insensitively so projection resolves the field instead of failing on the BE.
size_t size = elems.size();
for (size_t i = 0; i < size; ++i) {
if (iequal(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 {
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;
}
char* DataTypeStruct::serialize(const IColumn& column, char* buf, int be_exec_version) const {
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;
}
const char* DataTypeStruct::deserialize(const char* buf, MutableColumnPtr* column,
int be_exec_version) const {
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 = std::move(*struct_column->get_column_ptr(i)).mutate();
buf = elems[i]->deserialize(buf, &child_column, be_exec_version);
struct_column->get_column_ptr(i) = std::move(child_column);
}
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::have_maximum_size_of_value() const {
return std::all_of(elems.begin(), elems.end(),
[](auto&& elem) { return elem->have_maximum_size_of_value(); });
}
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