forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype.cc
More file actions
506 lines (404 loc) · 15.3 KB
/
Copy pathtype.cc
File metadata and controls
506 lines (404 loc) · 15.3 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// 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 "arrow/type.h"
#include <climits>
#include <sstream>
#include <string>
#include "arrow/array.h"
#include "arrow/compare.h"
#include "arrow/status.h"
#include "arrow/util/key_value_metadata.h"
#include "arrow/util/logging.h"
#include "arrow/util/stl.h"
#include "arrow/visitor.h"
namespace arrow {
std::shared_ptr<Field> Field::AddMetadata(
const std::shared_ptr<const KeyValueMetadata>& metadata) const {
return std::make_shared<Field>(name_, type_, nullable_, metadata);
}
#ifndef ARROW_NO_DEPRECATED_API
Status Field::AddMetadata(const std::shared_ptr<const KeyValueMetadata>& metadata,
std::shared_ptr<Field>* out) const {
*out = AddMetadata(metadata);
return Status::OK();
}
#endif
std::shared_ptr<Field> Field::RemoveMetadata() const {
return std::make_shared<Field>(name_, type_, nullable_);
}
bool Field::Equals(const Field& other) const {
if (this == &other) {
return true;
}
if (this->name_ == other.name_ && this->nullable_ == other.nullable_ &&
this->type_->Equals(*other.type_.get())) {
if (metadata_ == nullptr && other.metadata_ == nullptr) {
return true;
} else if ((metadata_ == nullptr) ^ (other.metadata_ == nullptr)) {
return false;
} else {
return metadata_->Equals(*other.metadata_);
}
}
return false;
}
bool Field::Equals(const std::shared_ptr<Field>& other) const {
return Equals(*other.get());
}
std::string Field::ToString() const {
std::stringstream ss;
ss << this->name_ << ": " << this->type_->ToString();
if (!this->nullable_) {
ss << " not null";
}
return ss.str();
}
DataType::~DataType() {}
bool DataType::Equals(const DataType& other) const { return TypeEquals(*this, other); }
bool DataType::Equals(const std::shared_ptr<DataType>& other) const {
if (!other) {
return false;
}
return Equals(*other.get());
}
std::string BooleanType::ToString() const { return name(); }
FloatingPoint::Precision HalfFloatType::precision() const { return FloatingPoint::HALF; }
FloatingPoint::Precision FloatType::precision() const { return FloatingPoint::SINGLE; }
FloatingPoint::Precision DoubleType::precision() const { return FloatingPoint::DOUBLE; }
std::string StringType::ToString() const { return std::string("string"); }
std::string ListType::ToString() const {
std::stringstream s;
s << "list<" << value_field()->ToString() << ">";
return s.str();
}
std::string BinaryType::ToString() const { return std::string("binary"); }
int FixedSizeBinaryType::bit_width() const { return CHAR_BIT * byte_width(); }
std::string FixedSizeBinaryType::ToString() const {
std::stringstream ss;
ss << "fixed_size_binary[" << byte_width_ << "]";
return ss.str();
}
std::string StructType::ToString() const {
std::stringstream s;
s << "struct<";
for (int i = 0; i < this->num_children(); ++i) {
if (i > 0) {
s << ", ";
}
std::shared_ptr<Field> field = this->child(i);
s << field->name() << ": " << field->type()->ToString();
}
s << ">";
return s.str();
}
// ----------------------------------------------------------------------
// Date types
DateType::DateType(Type::type type_id, DateUnit unit)
: FixedWidthType(type_id), unit_(unit) {}
Date32Type::Date32Type() : DateType(Type::DATE32, DateUnit::DAY) {}
Date64Type::Date64Type() : DateType(Type::DATE64, DateUnit::MILLI) {}
std::string Date64Type::ToString() const { return std::string("date64[ms]"); }
std::string Date32Type::ToString() const { return std::string("date32[day]"); }
// ----------------------------------------------------------------------
// Time types
TimeType::TimeType(Type::type type_id, TimeUnit::type unit)
: FixedWidthType(type_id), unit_(unit) {}
Time32Type::Time32Type(TimeUnit::type unit) : TimeType(Type::TIME32, unit) {
DCHECK(unit == TimeUnit::SECOND || unit == TimeUnit::MILLI)
<< "Must be seconds or milliseconds";
}
std::string Time32Type::ToString() const {
std::stringstream ss;
ss << "time32[" << this->unit_ << "]";
return ss.str();
}
Time64Type::Time64Type(TimeUnit::type unit) : TimeType(Type::TIME64, unit) {
DCHECK(unit == TimeUnit::MICRO || unit == TimeUnit::NANO)
<< "Must be microseconds or nanoseconds";
}
std::string Time64Type::ToString() const {
std::stringstream ss;
ss << "time64[" << this->unit_ << "]";
return ss.str();
}
// ----------------------------------------------------------------------
// Timestamp types
std::string TimestampType::ToString() const {
std::stringstream ss;
ss << "timestamp[" << this->unit_;
if (this->timezone_.size() > 0) {
ss << ", tz=" << this->timezone_;
}
ss << "]";
return ss.str();
}
// ----------------------------------------------------------------------
// Union type
UnionType::UnionType(const std::vector<std::shared_ptr<Field>>& fields,
const std::vector<uint8_t>& type_codes, UnionMode mode)
: NestedType(Type::UNION), mode_(mode), type_codes_(type_codes) {
children_ = fields;
}
std::string UnionType::ToString() const {
std::stringstream s;
if (mode_ == UnionMode::SPARSE) {
s << "union[sparse]<";
} else {
s << "union[dense]<";
}
for (size_t i = 0; i < children_.size(); ++i) {
if (i) {
s << ", ";
}
s << children_[i]->ToString() << "=" << static_cast<int>(type_codes_[i]);
}
s << ">";
return s.str();
}
// ----------------------------------------------------------------------
// DictionaryType
DictionaryType::DictionaryType(const std::shared_ptr<DataType>& index_type,
const std::shared_ptr<Array>& dictionary, bool ordered)
: FixedWidthType(Type::DICTIONARY),
index_type_(index_type),
dictionary_(dictionary),
ordered_(ordered) {}
int DictionaryType::bit_width() const {
return static_cast<const FixedWidthType*>(index_type_.get())->bit_width();
}
std::shared_ptr<Array> DictionaryType::dictionary() const { return dictionary_; }
std::string DictionaryType::ToString() const {
std::stringstream ss;
ss << "dictionary<values=" << dictionary_->type()->ToString()
<< ", indices=" << index_type_->ToString() << ", ordered=" << ordered_ << ">";
return ss.str();
}
// ----------------------------------------------------------------------
// Null type
std::string NullType::ToString() const { return name(); }
// ----------------------------------------------------------------------
// Schema implementation
Schema::Schema(const std::vector<std::shared_ptr<Field>>& fields,
const std::shared_ptr<const KeyValueMetadata>& metadata)
: fields_(fields), metadata_(metadata) {}
Schema::Schema(std::vector<std::shared_ptr<Field>>&& fields,
const std::shared_ptr<const KeyValueMetadata>& metadata)
: fields_(std::move(fields)), metadata_(metadata) {}
bool Schema::Equals(const Schema& other) const {
if (this == &other) {
return true;
}
if (num_fields() != other.num_fields()) {
return false;
}
for (int i = 0; i < num_fields(); ++i) {
if (!field(i)->Equals(*other.field(i).get())) {
return false;
}
}
return true;
}
std::shared_ptr<Field> Schema::GetFieldByName(const std::string& name) const {
int64_t i = GetFieldIndex(name);
return i == -1 ? nullptr : fields_[i];
}
int64_t Schema::GetFieldIndex(const std::string& name) const {
if (fields_.size() > 0 && name_to_index_.size() == 0) {
for (size_t i = 0; i < fields_.size(); ++i) {
name_to_index_[fields_[i]->name()] = static_cast<int>(i);
}
}
auto it = name_to_index_.find(name);
if (it == name_to_index_.end()) {
return -1;
} else {
return it->second;
}
}
Status Schema::AddField(int i, const std::shared_ptr<Field>& field,
std::shared_ptr<Schema>* out) const {
DCHECK_GE(i, 0);
DCHECK_LE(i, this->num_fields());
*out =
std::make_shared<Schema>(internal::AddVectorElement(fields_, i, field), metadata_);
return Status::OK();
}
std::shared_ptr<Schema> Schema::AddMetadata(
const std::shared_ptr<const KeyValueMetadata>& metadata) const {
return std::make_shared<Schema>(fields_, metadata);
}
#ifndef ARROW_NO_DEPRECATED_API
Status Schema::AddMetadata(const std::shared_ptr<const KeyValueMetadata>& metadata,
std::shared_ptr<Schema>* out) const {
*out = AddMetadata(metadata);
return Status::OK();
}
#endif
std::shared_ptr<const KeyValueMetadata> Schema::metadata() const { return metadata_; }
std::shared_ptr<Schema> Schema::RemoveMetadata() const {
return std::make_shared<Schema>(fields_);
}
Status Schema::RemoveField(int i, std::shared_ptr<Schema>* out) const {
DCHECK_GE(i, 0);
DCHECK_LT(i, this->num_fields());
*out = std::make_shared<Schema>(internal::DeleteVectorElement(fields_, i), metadata_);
return Status::OK();
}
std::string Schema::ToString() const {
std::stringstream buffer;
int i = 0;
for (auto field : fields_) {
if (i > 0) {
buffer << std::endl;
}
buffer << field->ToString();
++i;
}
if (metadata_) {
buffer << "\n-- metadata --";
for (int64_t i = 0; i < metadata_->size(); ++i) {
buffer << "\n" << metadata_->key(i) << ": " << metadata_->value(i);
}
}
return buffer.str();
}
std::shared_ptr<Schema> schema(const std::vector<std::shared_ptr<Field>>& fields,
const std::shared_ptr<const KeyValueMetadata>& metadata) {
return std::make_shared<Schema>(fields, metadata);
}
std::shared_ptr<Schema> schema(std::vector<std::shared_ptr<Field>>&& fields,
const std::shared_ptr<const KeyValueMetadata>& metadata) {
return std::make_shared<Schema>(std::move(fields), metadata);
}
// ----------------------------------------------------------------------
// Visitors and factory functions
#define ACCEPT_VISITOR(TYPE) \
Status TYPE::Accept(TypeVisitor* visitor) const { return visitor->Visit(*this); }
ACCEPT_VISITOR(NullType);
ACCEPT_VISITOR(BooleanType);
ACCEPT_VISITOR(BinaryType);
ACCEPT_VISITOR(FixedSizeBinaryType);
ACCEPT_VISITOR(StringType);
ACCEPT_VISITOR(ListType);
ACCEPT_VISITOR(StructType);
ACCEPT_VISITOR(DecimalType);
ACCEPT_VISITOR(UnionType);
ACCEPT_VISITOR(Date32Type);
ACCEPT_VISITOR(Date64Type);
ACCEPT_VISITOR(Time32Type);
ACCEPT_VISITOR(Time64Type);
ACCEPT_VISITOR(TimestampType);
ACCEPT_VISITOR(IntervalType);
ACCEPT_VISITOR(DictionaryType);
#define TYPE_FACTORY(NAME, KLASS) \
std::shared_ptr<DataType> NAME() { \
static std::shared_ptr<DataType> result = std::make_shared<KLASS>(); \
return result; \
}
TYPE_FACTORY(null, NullType);
TYPE_FACTORY(boolean, BooleanType);
TYPE_FACTORY(int8, Int8Type);
TYPE_FACTORY(uint8, UInt8Type);
TYPE_FACTORY(int16, Int16Type);
TYPE_FACTORY(uint16, UInt16Type);
TYPE_FACTORY(int32, Int32Type);
TYPE_FACTORY(uint32, UInt32Type);
TYPE_FACTORY(int64, Int64Type);
TYPE_FACTORY(uint64, UInt64Type);
TYPE_FACTORY(float16, HalfFloatType);
TYPE_FACTORY(float32, FloatType);
TYPE_FACTORY(float64, DoubleType);
TYPE_FACTORY(utf8, StringType);
TYPE_FACTORY(binary, BinaryType);
TYPE_FACTORY(date64, Date64Type);
TYPE_FACTORY(date32, Date32Type);
std::shared_ptr<DataType> fixed_size_binary(int32_t byte_width) {
return std::make_shared<FixedSizeBinaryType>(byte_width);
}
std::shared_ptr<DataType> timestamp(TimeUnit::type unit) {
return std::make_shared<TimestampType>(unit);
}
std::shared_ptr<DataType> timestamp(TimeUnit::type unit, const std::string& timezone) {
return std::make_shared<TimestampType>(unit, timezone);
}
std::shared_ptr<DataType> time32(TimeUnit::type unit) {
return std::make_shared<Time32Type>(unit);
}
std::shared_ptr<DataType> time64(TimeUnit::type unit) {
return std::make_shared<Time64Type>(unit);
}
std::shared_ptr<DataType> list(const std::shared_ptr<DataType>& value_type) {
return std::make_shared<ListType>(value_type);
}
std::shared_ptr<DataType> list(const std::shared_ptr<Field>& value_field) {
return std::make_shared<ListType>(value_field);
}
std::shared_ptr<DataType> struct_(const std::vector<std::shared_ptr<Field>>& fields) {
return std::make_shared<StructType>(fields);
}
std::shared_ptr<DataType> union_(const std::vector<std::shared_ptr<Field>>& child_fields,
const std::vector<uint8_t>& type_codes, UnionMode mode) {
return std::make_shared<UnionType>(child_fields, type_codes, mode);
}
std::shared_ptr<DataType> dictionary(const std::shared_ptr<DataType>& index_type,
const std::shared_ptr<Array>& dict_values,
bool ordered) {
return std::make_shared<DictionaryType>(index_type, dict_values, ordered);
}
std::shared_ptr<Field> field(const std::string& name,
const std::shared_ptr<DataType>& type, bool nullable,
const std::shared_ptr<const KeyValueMetadata>& metadata) {
return std::make_shared<Field>(name, type, nullable, metadata);
}
std::shared_ptr<DataType> decimal(int32_t precision, int32_t scale) {
return std::make_shared<DecimalType>(precision, scale);
}
static const BufferDescr kValidityBuffer(BufferType::VALIDITY, 1);
static const BufferDescr kOffsetBuffer(BufferType::OFFSET, 32);
static const BufferDescr kTypeBuffer(BufferType::TYPE, 32);
static const BufferDescr kBooleanBuffer(BufferType::DATA, 1);
static const BufferDescr kValues64(BufferType::DATA, 64);
static const BufferDescr kValues32(BufferType::DATA, 32);
static const BufferDescr kValues16(BufferType::DATA, 16);
static const BufferDescr kValues8(BufferType::DATA, 8);
std::vector<BufferDescr> FixedWidthType::GetBufferLayout() const {
return {kValidityBuffer, BufferDescr(BufferType::DATA, bit_width())};
}
std::vector<BufferDescr> NullType::GetBufferLayout() const { return {}; }
std::vector<BufferDescr> BinaryType::GetBufferLayout() const {
return {kValidityBuffer, kOffsetBuffer, kValues8};
}
std::vector<BufferDescr> FixedSizeBinaryType::GetBufferLayout() const {
return {kValidityBuffer, BufferDescr(BufferType::DATA, bit_width())};
}
std::vector<BufferDescr> ListType::GetBufferLayout() const {
return {kValidityBuffer, kOffsetBuffer};
}
std::vector<BufferDescr> StructType::GetBufferLayout() const { return {kValidityBuffer}; }
std::vector<BufferDescr> UnionType::GetBufferLayout() const {
if (mode_ == UnionMode::SPARSE) {
return {kValidityBuffer, kTypeBuffer};
} else {
return {kValidityBuffer, kTypeBuffer, kOffsetBuffer};
}
}
std::string DecimalType::ToString() const {
std::stringstream s;
s << "decimal(" << precision_ << ", " << scale_ << ")";
return s.str();
}
} // namespace arrow