forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_type_array.cpp
More file actions
362 lines (323 loc) · 13.4 KB
/
Copy pathdata_type_array.cpp
File metadata and controls
362 lines (323 loc) · 13.4 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
// 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/DataTypeArray.h
// and modified by Doris
#include "vec/data_types/data_type_array.h"
#include <ctype.h>
#include <gen_cpp/data.pb.h>
#include <glog/logging.h>
#include <string.h>
#include <typeinfo>
#include <utility>
#include "agent/be_exec_version_manager.h"
#include "runtime/decimalv2_value.h"
#include "util/types.h"
#include "vec/columns/column.h"
#include "vec/columns/column_array.h"
#include "vec/columns/column_const.h"
#include "vec/columns/column_nullable.h"
#include "vec/common/assert_cast.h"
#include "vec/common/string_buffer.hpp"
#include "vec/common/string_ref.h"
#include "vec/common/typeid_cast.h"
#include "vec/data_types/data_type_nullable.h"
#include "vec/io/reader_buffer.h"
namespace doris::vectorized {
namespace ErrorCodes {
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
DataTypeArray::DataTypeArray(const DataTypePtr& nested_) : nested {nested_} {}
MutableColumnPtr DataTypeArray::create_column() const {
return ColumnArray::create(nested->create_column(), ColumnArray::ColumnOffsets::create());
}
Field DataTypeArray::get_default() const {
Array a;
a.push_back(nested->get_default());
return a;
}
bool DataTypeArray::equals(const IDataType& rhs) const {
return typeid(rhs) == typeid(*this) &&
nested->equals(*static_cast<const DataTypeArray&>(rhs).nested);
}
size_t DataTypeArray::get_number_of_dimensions() const {
const DataTypeArray* nested_array = typeid_cast<const DataTypeArray*>(nested.get());
if (!nested_array) return 1;
return 1 +
nested_array
->get_number_of_dimensions(); /// Every modern C++ compiler optimizes tail recursion.
}
// binary : const flag | row num | real saved num | offsets | data
// offsets: data_off1 | data_off2 | ...
// data : data1 | data2 | ...
int64_t DataTypeArray::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);
auto real_need_copy_num = is_const_column ? 1 : column.size();
const IColumn* array_column = &column;
if (is_const_column) {
const auto& const_column = assert_cast<const ColumnConst&>(column);
array_column = &(const_column.get_data_column());
}
const auto& data_column = assert_cast<const ColumnArray&>(*array_column);
size = size + sizeof(ColumnArray::Offset64) * real_need_copy_num;
return size + get_nested_type()->get_uncompressed_serialized_bytes(data_column.get_data(),
be_exec_version);
} else {
auto ptr = column.convert_to_full_column_if_const();
const auto& data_column = assert_cast<const ColumnArray&>(*ptr.get());
return sizeof(ColumnArray::Offset64) * (column.size() + 1) +
get_nested_type()->get_uncompressed_serialized_bytes(data_column.get_data(),
be_exec_version);
}
}
char* DataTypeArray::serialize(const IColumn& column, char* buf, int be_exec_version) const {
if (be_exec_version >= USE_CONST_SERDE) {
const auto* array_column = &column;
size_t real_need_copy_num = 0;
buf = serialize_const_flag_and_row_num(&array_column, buf, &real_need_copy_num);
const auto& data_column = assert_cast<const ColumnArray&>(*array_column);
// offsets
memcpy(buf, data_column.get_offsets().data(),
real_need_copy_num * sizeof(ColumnArray::Offset64));
buf += real_need_copy_num * sizeof(ColumnArray::Offset64);
// children
return get_nested_type()->serialize(data_column.get_data(), buf, be_exec_version);
} else {
auto ptr = column.convert_to_full_column_if_const();
const auto& data_column = assert_cast<const ColumnArray&>(*ptr.get());
// row num
*reinterpret_cast<ColumnArray::Offset64*>(buf) = column.size();
buf += sizeof(ColumnArray::Offset64);
// offsets
memcpy(buf, data_column.get_offsets().data(),
column.size() * sizeof(ColumnArray::Offset64));
buf += column.size() * sizeof(ColumnArray::Offset64);
// children
return get_nested_type()->serialize(data_column.get_data(), buf, be_exec_version);
}
}
const char* DataTypeArray::deserialize(const char* buf, MutableColumnPtr* column,
int be_exec_version) const {
if (be_exec_version >= USE_CONST_SERDE) {
auto* origin_column = column->get();
size_t real_have_saved_num = 0;
buf = deserialize_const_flag_and_row_num(buf, column, &real_have_saved_num);
auto* data_column = assert_cast<ColumnArray*>(origin_column);
auto& offsets = data_column->get_offsets();
// offsets
offsets.resize(real_have_saved_num);
memcpy(offsets.data(), buf, sizeof(ColumnArray::Offset64) * real_have_saved_num);
buf += sizeof(ColumnArray::Offset64) * real_have_saved_num;
// children
auto nested_column = data_column->get_data_ptr()->assume_mutable();
buf = get_nested_type()->deserialize(buf, &nested_column, be_exec_version);
return buf;
} else {
auto* data_column = assert_cast<ColumnArray*>(column->get());
auto& offsets = data_column->get_offsets();
// row num
ColumnArray::Offset64 row_num = *reinterpret_cast<const ColumnArray::Offset64*>(buf);
buf += sizeof(ColumnArray::Offset64);
// offsets
offsets.resize(row_num);
memcpy(offsets.data(), buf, sizeof(ColumnArray::Offset64) * row_num);
buf += sizeof(ColumnArray::Offset64) * row_num;
// children
auto nested_column = data_column->get_data_ptr()->assume_mutable();
return get_nested_type()->deserialize(buf, &nested_column, be_exec_version);
}
}
void DataTypeArray::to_pb_column_meta(PColumnMeta* col_meta) const {
IDataType::to_pb_column_meta(col_meta);
auto children = col_meta->add_children();
get_nested_type()->to_pb_column_meta(children);
}
void DataTypeArray::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& data_column = assert_cast<const ColumnArray&>(*ptr);
auto& offsets = data_column.get_offsets();
size_t offset = offsets[row_num - 1];
size_t next_offset = offsets[row_num];
const IColumn& nested_column = data_column.get_data();
ostr.write("[", 1);
for (size_t i = offset; i < next_offset; ++i) {
if (i != offset) {
ostr.write(", ", 2);
}
WhichDataType which(remove_nullable(nested));
if (which.is_string_or_fixed_string()) {
ostr.write("'", 1);
nested->to_string(nested_column, i, ostr);
ostr.write("'", 1);
} else {
nested->to_string(nested_column, i, ostr);
}
}
ostr.write("]", 1);
}
std::string DataTypeArray::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& data_column = assert_cast<const ColumnArray&>(*ptr);
auto& offsets = data_column.get_offsets();
size_t offset = offsets[row_num - 1];
size_t next_offset = offsets[row_num];
const IColumn& nested_column = data_column.get_data();
std::string str;
str += "[";
for (size_t i = offset; i < next_offset; ++i) {
if (i != offset) {
str += ", ";
}
WhichDataType which(remove_nullable(nested));
if (which.is_string_or_fixed_string()) {
str += "'";
str += nested->to_string(nested_column, i);
str += "'";
} else {
str += nested->to_string(nested_column, i);
}
}
str += "]";
return str;
}
bool next_element_from_string(ReadBuffer& rb, StringRef& output, bool& has_quota) {
StringRef element(rb.position(), 0);
has_quota = false;
if (rb.eof()) {
return false;
}
// ltrim
while (!rb.eof() && isspace(*rb.position())) {
++rb.position();
element.data = rb.position();
}
// parse string
if (*rb.position() == '"' || *rb.position() == '\'') {
const char str_sep = *rb.position();
size_t str_len = 1;
// search until next '"' or '\''
while (str_len < rb.count() && *(rb.position() + str_len) != str_sep) {
++str_len;
}
// invalid string
if (str_len >= rb.count()) {
rb.position() = rb.end();
return false;
}
has_quota = true;
rb.position() += str_len + 1;
element.size += str_len + 1;
}
// parse array element until array separator ',' or end ']'
while (!rb.eof() && (*rb.position() != ',') && (rb.count() != 1 || *rb.position() != ']')) {
// invalid elements such as ["123" 456,"789" 777]
// correct elements such as ["123" ,"789" ]
if (has_quota && !isspace(*rb.position())) {
return false;
}
++rb.position();
++element.size;
}
// invalid array element
if (rb.eof()) {
return false;
}
// adjust read buffer position to first char of next array element
++rb.position();
// rtrim
while (element.size > 0 && isspace(element.data[element.size - 1])) {
--element.size;
}
// trim '"' and '\'' for string
if (element.size >= 2 && (element.data[0] == '"' || element.data[0] == '\'') &&
element.data[0] == element.data[element.size - 1]) {
++element.data;
element.size -= 2;
}
output = element;
return true;
}
Status DataTypeArray::from_string(ReadBuffer& rb, IColumn* column) const {
DCHECK(!rb.eof());
// only support one level now
auto* array_column = assert_cast<ColumnArray*>(column);
auto& offsets = array_column->get_offsets();
IColumn& nested_column = array_column->get_data();
DCHECK(nested_column.is_nullable());
if (*rb.position() != '[') {
return Status::InvalidArgument("Array does not start with '[' character, found '{}'",
*rb.position());
}
if (*(rb.end() - 1) != ']') {
return Status::InvalidArgument("Array does not end with ']' character, found '{}'",
*(rb.end() - 1));
}
// empty array []
if (rb.count() == 2) {
offsets.push_back(offsets.back());
return Status::OK();
}
++rb.position();
size_t element_num = 0;
// parse array element until end of array
while (!rb.eof()) {
StringRef element(rb.position(), rb.count());
bool has_quota = false;
if (!next_element_from_string(rb, element, has_quota)) {
// we should do array element column revert if error
nested_column.pop_back(element_num);
return Status::InvalidArgument("Cannot read array element from text '{}'",
element.to_string());
}
// handle empty element
if (element.size == 0) {
auto& nested_null_col = reinterpret_cast<ColumnNullable&>(nested_column);
nested_null_col.get_nested_column().insert_default();
nested_null_col.get_null_map_data().push_back(0);
++element_num;
continue;
}
// handle null element, need to distinguish null and "null"
if (!has_quota && element.size == 4 && strncmp(element.data, "null", 4) == 0) {
// insert null
auto& nested_null_col = reinterpret_cast<ColumnNullable&>(nested_column);
nested_null_col.get_nested_column().insert_default();
nested_null_col.get_null_map_data().push_back(1);
++element_num;
continue;
}
// handle normal element
ReadBuffer read_buffer(const_cast<char*>(element.data), element.size);
auto st = nested->from_string(read_buffer, &nested_column);
if (!st.ok()) {
// we should do array element column revert if error
nested_column.pop_back(element_num);
return st;
}
++element_num;
}
offsets.push_back(offsets.back() + element_num);
return Status::OK();
}
} // namespace doris::vectorized