forked from apache/iceberg-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype.h
More file actions
534 lines (423 loc) · 17 KB
/
type.h
File metadata and controls
534 lines (423 loc) · 17 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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
* 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.
*/
#pragma once
/// \file iceberg/type.h
/// Data types for Iceberg. This header defines the data types, but see
/// iceberg/type_fwd.h for the enum defining the list of types.
#include <array>
#include <cstdint>
#include <memory>
#include <mutex>
#include <optional>
#include <span>
#include <string>
#include <unordered_map>
#include <vector>
#include "iceberg/iceberg_export.h"
#include "iceberg/result.h"
#include "iceberg/schema_field.h"
#include "iceberg/util/formattable.h"
namespace iceberg {
template <typename Func>
Status LazyInitWithCallOnce(std::once_flag& flag, Func&& func) {
Status status;
std::call_once(flag, [&status, &func]() { status = func(); });
return status;
}
/// \brief Interface for a data type for a field.
class ICEBERG_EXPORT Type : public iceberg::util::Formattable {
public:
~Type() override = default;
/// \brief Get the type ID.
[[nodiscard]] virtual TypeId type_id() const = 0;
/// \brief Is this a primitive type (may not have child fields)?
[[nodiscard]] virtual bool is_primitive() const = 0;
/// \brief Is this a nested type (may have child fields)?
[[nodiscard]] virtual bool is_nested() const = 0;
/// \brief Compare two types for equality.
friend bool operator==(const Type& lhs, const Type& rhs) { return lhs.Equals(rhs); }
protected:
/// \brief Compare two types for equality.
[[nodiscard]] virtual bool Equals(const Type& other) const = 0;
};
/// \brief A data type that does not have child fields.
class ICEBERG_EXPORT PrimitiveType : public Type {
public:
bool is_primitive() const override { return true; }
bool is_nested() const override { return false; }
};
/// \brief A data type that has child fields.
class ICEBERG_EXPORT NestedType : public Type {
public:
bool is_primitive() const override { return false; }
bool is_nested() const override { return true; }
/// \brief Get a view of the child fields.
[[nodiscard]] virtual std::span<const SchemaField> fields() const = 0;
using SchemaFieldConstRef = std::reference_wrapper<const SchemaField>;
/// \brief Get a field by field ID.
///
/// \note This is O(1) complexity.
[[nodiscard]] virtual Result<std::optional<SchemaFieldConstRef>> GetFieldById(
int32_t field_id) const = 0;
/// \brief Get a field by index.
///
/// \note This is O(1) complexity.
[[nodiscard]] virtual Result<std::optional<SchemaFieldConstRef>> GetFieldByIndex(
int32_t index) const = 0;
/// \brief Get a field by name. Return an error Status if
/// the field name is not unique; prefer GetFieldById or GetFieldByIndex
/// when possible.
///
/// \note This is O(1) complexity.
[[nodiscard]] virtual Result<std::optional<SchemaFieldConstRef>> GetFieldByName(
std::string_view name, bool case_sensitive) const = 0;
/// \brief Get a field by name (case-sensitive).
[[nodiscard]] Result<std::optional<SchemaFieldConstRef>> GetFieldByName(
std::string_view name) const;
};
/// \defgroup type-nested Nested Types
/// Nested types have child fields.
/// @{
/// \brief A data type representing a struct with nested fields.
class ICEBERG_EXPORT StructType : public NestedType {
public:
constexpr static TypeId kTypeId = TypeId::kStruct;
explicit StructType(std::vector<SchemaField> fields);
~StructType() override = default;
TypeId type_id() const override;
std::string ToString() const override;
std::span<const SchemaField> fields() const override;
Result<std::optional<SchemaFieldConstRef>> GetFieldById(
int32_t field_id) const override;
Result<std::optional<SchemaFieldConstRef>> GetFieldByIndex(
int32_t index) const override;
Result<std::optional<SchemaFieldConstRef>> GetFieldByName(
std::string_view name, bool case_sensitive) const override;
using NestedType::GetFieldByName;
protected:
bool Equals(const Type& other) const override;
Status InitFieldById() const;
Status InitFieldByName() const;
Status InitFieldByLowerCaseName() const;
std::vector<SchemaField> fields_;
mutable std::unordered_map<int32_t, SchemaFieldConstRef> field_by_id_;
mutable std::unordered_map<std::string_view, SchemaFieldConstRef> field_by_name_;
mutable std::unordered_map<std::string, SchemaFieldConstRef> field_by_lowercase_name_;
mutable std::once_flag field_by_id_flag_;
mutable std::once_flag field_by_name_flag_;
mutable std::once_flag field_by_lowercase_name_flag_;
};
/// \brief A data type representing a list of values.
class ICEBERG_EXPORT ListType : public NestedType {
public:
constexpr static const TypeId kTypeId = TypeId::kList;
constexpr static const std::string_view kElementName = "element";
/// \brief Construct a list of the given element. The name of the child
/// field should be "element".
explicit ListType(SchemaField element);
/// \brief Construct a list of the given element type.
ListType(int32_t field_id, std::shared_ptr<Type> type, bool optional);
~ListType() override = default;
TypeId type_id() const override;
std::string ToString() const override;
std::span<const SchemaField> fields() const override;
Result<std::optional<SchemaFieldConstRef>> GetFieldById(
int32_t field_id) const override;
Result<std::optional<SchemaFieldConstRef>> GetFieldByIndex(
int32_t index) const override;
Result<std::optional<SchemaFieldConstRef>> GetFieldByName(
std::string_view name, bool case_sensitive) const override;
using NestedType::GetFieldByName;
protected:
bool Equals(const Type& other) const override;
SchemaField element_;
};
/// \brief A data type representing a dictionary of values.
class ICEBERG_EXPORT MapType : public NestedType {
public:
constexpr static const TypeId kTypeId = TypeId::kMap;
constexpr static const std::string_view kKeyName = "key";
constexpr static const std::string_view kValueName = "value";
/// \brief Construct a map of the given key/value fields. The field names
/// should be "key" and "value", respectively.
explicit MapType(SchemaField key, SchemaField value);
~MapType() override = default;
const SchemaField& key() const;
const SchemaField& value() const;
TypeId type_id() const override;
std::string ToString() const override;
std::span<const SchemaField> fields() const override;
Result<std::optional<SchemaFieldConstRef>> GetFieldById(
int32_t field_id) const override;
Result<std::optional<SchemaFieldConstRef>> GetFieldByIndex(
int32_t index) const override;
Result<std::optional<SchemaFieldConstRef>> GetFieldByName(
std::string_view name, bool case_sensitive) const override;
using NestedType::GetFieldByName;
protected:
bool Equals(const Type& other) const override;
std::array<SchemaField, 2> fields_;
};
/// @}
/// \defgroup type-primitive Primitive Types
/// Primitive types do not have nested fields.
/// @{
/// \brief A data type representing a boolean (true or false).
class ICEBERG_EXPORT BooleanType : public PrimitiveType {
public:
constexpr static const TypeId kTypeId = TypeId::kBoolean;
BooleanType() = default;
~BooleanType() override = default;
TypeId type_id() const override;
std::string ToString() const override;
protected:
bool Equals(const Type& other) const override;
};
/// \brief A data type representing a 32-bit signed integer.
class ICEBERG_EXPORT IntType : public PrimitiveType {
public:
constexpr static const TypeId kTypeId = TypeId::kInt;
IntType() = default;
~IntType() override = default;
TypeId type_id() const override;
std::string ToString() const override;
protected:
bool Equals(const Type& other) const override;
};
/// \brief A data type representing a 64-bit signed integer.
class ICEBERG_EXPORT LongType : public PrimitiveType {
public:
constexpr static const TypeId kTypeId = TypeId::kLong;
LongType() = default;
~LongType() override = default;
TypeId type_id() const override;
std::string ToString() const override;
protected:
bool Equals(const Type& other) const override;
};
/// \brief A data type representing a 32-bit (single precision) IEEE-754
/// float.
class ICEBERG_EXPORT FloatType : public PrimitiveType {
public:
constexpr static const TypeId kTypeId = TypeId::kFloat;
FloatType() = default;
~FloatType() override = default;
TypeId type_id() const override;
std::string ToString() const override;
protected:
bool Equals(const Type& other) const override;
};
/// \brief A data type representing a 64-bit (double precision) IEEE-754
/// float.
class ICEBERG_EXPORT DoubleType : public PrimitiveType {
public:
constexpr static const TypeId kTypeId = TypeId::kDouble;
DoubleType() = default;
~DoubleType() override = default;
TypeId type_id() const override;
std::string ToString() const override;
protected:
bool Equals(const Type& other) const override;
};
/// \brief A data type representing a fixed-precision decimal.
class ICEBERG_EXPORT DecimalType : public PrimitiveType {
public:
constexpr static const TypeId kTypeId = TypeId::kDecimal;
constexpr static const int32_t kMaxPrecision = 38;
/// \brief Construct a decimal type with the given precision and scale.
DecimalType(int32_t precision, int32_t scale);
~DecimalType() override = default;
/// \brief Get the precision (the number of decimal digits).
[[nodiscard]] int32_t precision() const;
/// \brief Get the scale (essentially, the number of decimal digits after
/// the decimal point; precisely, the value is scaled by $$10^{-s}$$.).
[[nodiscard]] int32_t scale() const;
TypeId type_id() const override;
std::string ToString() const override;
protected:
bool Equals(const Type& other) const override;
private:
int32_t precision_;
int32_t scale_;
};
/// \brief A data type representing a calendar date without reference to a
/// timezone or time.
class ICEBERG_EXPORT DateType : public PrimitiveType {
public:
constexpr static const TypeId kTypeId = TypeId::kDate;
DateType() = default;
~DateType() override = default;
TypeId type_id() const override;
std::string ToString() const override;
protected:
bool Equals(const Type& other) const override;
};
/// \brief A data type representing a wall clock time in microseconds without
/// reference to a timezone or date.
class ICEBERG_EXPORT TimeType : public PrimitiveType {
public:
constexpr static const TypeId kTypeId = TypeId::kTime;
TimeType() = default;
~TimeType() override = default;
TypeId type_id() const override;
std::string ToString() const override;
protected:
bool Equals(const Type& other) const override;
};
/// \brief A base class for any timestamp time (irrespective of unit or
/// timezone).
class ICEBERG_EXPORT TimestampBase : public PrimitiveType {
public:
/// \brief Is this type zoned or naive?
[[nodiscard]] virtual bool is_zoned() const = 0;
/// \brief The time resolution.
[[nodiscard]] virtual TimeUnit time_unit() const = 0;
};
/// \brief A data type representing a timestamp in microseconds without
/// reference to a timezone.
class ICEBERG_EXPORT TimestampType : public TimestampBase {
public:
constexpr static const TypeId kTypeId = TypeId::kTimestamp;
TimestampType() = default;
~TimestampType() override = default;
bool is_zoned() const override;
TimeUnit time_unit() const override;
TypeId type_id() const override;
std::string ToString() const override;
protected:
bool Equals(const Type& other) const override;
};
/// \brief A data type representing a timestamp as microseconds since the
/// epoch in UTC. A time zone or offset is not stored.
class ICEBERG_EXPORT TimestampTzType : public TimestampBase {
public:
constexpr static const TypeId kTypeId = TypeId::kTimestampTz;
TimestampTzType() = default;
~TimestampTzType() override = default;
bool is_zoned() const override;
TimeUnit time_unit() const override;
TypeId type_id() const override;
std::string ToString() const override;
protected:
bool Equals(const Type& other) const override;
};
/// \brief A data type representing an arbitrary-length byte sequence.
class ICEBERG_EXPORT BinaryType : public PrimitiveType {
public:
constexpr static const TypeId kTypeId = TypeId::kBinary;
BinaryType() = default;
~BinaryType() override = default;
TypeId type_id() const override;
std::string ToString() const override;
protected:
bool Equals(const Type& other) const override;
};
/// \brief A data type representing an arbitrary-length character sequence
/// (encoded in UTF-8).
class ICEBERG_EXPORT StringType : public PrimitiveType {
public:
constexpr static const TypeId kTypeId = TypeId::kString;
StringType() = default;
~StringType() override = default;
TypeId type_id() const override;
std::string ToString() const override;
protected:
bool Equals(const Type& other) const override;
};
/// \brief A data type representing a fixed-length bytestring.
class ICEBERG_EXPORT FixedType : public PrimitiveType {
public:
constexpr static const TypeId kTypeId = TypeId::kFixed;
/// \brief Construct a fixed type with the given length.
explicit FixedType(int32_t length);
~FixedType() override = default;
/// \brief The length (the number of bytes to store).
[[nodiscard]] int32_t length() const;
TypeId type_id() const override;
std::string ToString() const override;
protected:
bool Equals(const Type& other) const override;
private:
int32_t length_;
};
/// \brief A data type representing a UUID. While defined as a distinct type,
/// it is effectively a fixed(16).
class ICEBERG_EXPORT UuidType : public PrimitiveType {
public:
constexpr static const TypeId kTypeId = TypeId::kUuid;
UuidType() = default;
~UuidType() override = default;
TypeId type_id() const override;
std::string ToString() const override;
protected:
bool Equals(const Type& other) const override;
};
/// @}
/// \defgroup type-factories Factory functions for creating primitive data types
///
/// Factory functions for creating primitive data types
/// @{
/// \brief Return a BooleanType instance.
ICEBERG_EXPORT const std::shared_ptr<BooleanType>& boolean();
/// \brief Return an IntType instance.
ICEBERG_EXPORT const std::shared_ptr<IntType>& int32();
/// \brief Return a LongType instance.
ICEBERG_EXPORT const std::shared_ptr<LongType>& int64();
/// \brief Return a FloatType instance.
ICEBERG_EXPORT const std::shared_ptr<FloatType>& float32();
/// \brief Return a DoubleType instance.
ICEBERG_EXPORT const std::shared_ptr<DoubleType>& float64();
/// \brief Return a DateType instance.
ICEBERG_EXPORT const std::shared_ptr<DateType>& date();
/// \brief Return a TimeType instance.
ICEBERG_EXPORT const std::shared_ptr<TimeType>& time();
/// \brief Return a TimestampType instance.
ICEBERG_EXPORT const std::shared_ptr<TimestampType>& timestamp();
/// \brief Return a TimestampTzType instance.
ICEBERG_EXPORT const std::shared_ptr<TimestampTzType>& timestamp_tz();
/// \brief Return a BinaryType instance.
ICEBERG_EXPORT const std::shared_ptr<BinaryType>& binary();
/// \brief Return a StringType instance.
ICEBERG_EXPORT const std::shared_ptr<StringType>& string();
/// \brief Return a UuidType instance.
ICEBERG_EXPORT const std::shared_ptr<UuidType>& uuid();
/// \brief Create a DecimalType with the given precision and scale.
/// \param precision The number of decimal digits (max 38).
/// \param scale The number of decimal digits after the decimal point.
/// \return A shared pointer to the DecimalType instance.
ICEBERG_EXPORT std::shared_ptr<DecimalType> decimal(int32_t precision, int32_t scale);
/// \brief Create a FixedType with the given length.
/// \param length The number of bytes to store (must be >= 0).
/// \return A shared pointer to the FixedType instance.
ICEBERG_EXPORT std::shared_ptr<FixedType> fixed(int32_t length);
/// \brief Create a StructType with the given fields.
/// \param fields The fields of the struct.
/// \return A shared pointer to the StructType instance.
ICEBERG_EXPORT std::shared_ptr<StructType> struct_(std::vector<SchemaField> fields);
/// \brief Create a ListType with the given element field.
/// \param element The element field of the list.
/// \return A shared pointer to the ListType instance.
ICEBERG_EXPORT std::shared_ptr<ListType> list(SchemaField element);
/// \brief Create a MapType with the given key and value fields.
/// \param key The key field of the map.
/// \param value The value field of the map.
/// \return A shared pointer to the MapType instance.
ICEBERG_EXPORT std::shared_ptr<MapType> map(SchemaField key, SchemaField value);
/// @}
} // namespace iceberg