iceberg-cpp
Loading...
Searching...
No Matches
type.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
25
26#include <array>
27#include <cstdint>
28#include <memory>
29#include <optional>
30#include <span>
31#include <string>
32#include <unordered_map>
33#include <vector>
34
35#include "iceberg/iceberg_export.h"
36#include "iceberg/result.h"
39#include "iceberg/util/lazy.h"
40
41namespace iceberg {
42
44class ICEBERG_EXPORT Type : public iceberg::util::Formattable {
45 public:
46 ~Type() override = default;
47
49 [[nodiscard]] virtual TypeId type_id() const = 0;
50
52 [[nodiscard]] virtual bool is_primitive() const = 0;
53
55 [[nodiscard]] virtual bool is_nested() const = 0;
56
58 friend bool operator==(const Type& lhs, const Type& rhs) { return lhs.Equals(rhs); }
59
60 protected:
62 [[nodiscard]] virtual bool Equals(const Type& other) const = 0;
63};
64
66class ICEBERG_EXPORT PrimitiveType : public Type {
67 public:
68 bool is_primitive() const override { return true; }
69 bool is_nested() const override { return false; }
70};
71
73class ICEBERG_EXPORT NestedType : public Type {
74 public:
75 bool is_primitive() const override { return false; }
76 bool is_nested() const override { return true; }
77
79 [[nodiscard]] virtual std::span<const SchemaField> fields() const = 0;
80 using SchemaFieldConstRef = std::reference_wrapper<const SchemaField>;
84 [[nodiscard]] virtual Result<std::optional<SchemaFieldConstRef>> GetFieldById(
85 int32_t field_id) const = 0;
89 [[nodiscard]] virtual Result<std::optional<SchemaFieldConstRef>> GetFieldByIndex(
90 int32_t index) const = 0;
96 [[nodiscard]] virtual Result<std::optional<SchemaFieldConstRef>> GetFieldByName(
97 std::string_view name, bool case_sensitive) const = 0;
99 [[nodiscard]] Result<std::optional<SchemaFieldConstRef>> GetFieldByName(
100 std::string_view name) const;
101};
102
106
108class ICEBERG_EXPORT StructType : public NestedType {
109 public:
110 constexpr static TypeId kTypeId = TypeId::kStruct;
111 explicit StructType(std::vector<SchemaField> fields);
112 ~StructType() override = default;
113
114 TypeId type_id() const override;
115 std::string ToString() const override;
116
117 std::span<const SchemaField> fields() const override;
118 Result<std::optional<SchemaFieldConstRef>> GetFieldById(
119 int32_t field_id) const override;
120 Result<std::optional<SchemaFieldConstRef>> GetFieldByIndex(
121 int32_t index) const override;
122 Result<std::optional<SchemaFieldConstRef>> GetFieldByName(
123 std::string_view name, bool case_sensitive) const override;
124 using NestedType::GetFieldByName;
125
126 std::unique_ptr<Schema> ToSchema() const;
127
128 protected:
129 bool Equals(const Type& other) const override;
130
131 static Result<std::unordered_map<int32_t, SchemaFieldConstRef>> InitFieldById(
132 const StructType&);
133 static Result<std::unordered_map<std::string_view, SchemaFieldConstRef>>
134 InitFieldByName(const StructType&);
135 static Result<std::unordered_map<std::string, SchemaFieldConstRef>>
136 InitFieldByLowerCaseName(const StructType&);
137
138 std::vector<SchemaField> fields_;
139 Lazy<InitFieldById> field_by_id_;
140 Lazy<InitFieldByName> field_by_name_;
141 Lazy<InitFieldByLowerCaseName> field_by_lowercase_name_;
142};
143
145class ICEBERG_EXPORT ListType : public NestedType {
146 public:
147 constexpr static const TypeId kTypeId = TypeId::kList;
148 constexpr static const std::string_view kElementName = "element";
149
152 explicit ListType(SchemaField element);
154 ListType(int32_t field_id, std::shared_ptr<Type> type, bool optional);
155 ~ListType() override = default;
156
157 TypeId type_id() const override;
158 const SchemaField& element() const;
159 std::string ToString() const override;
160
161 std::span<const SchemaField> fields() const override;
162 Result<std::optional<SchemaFieldConstRef>> GetFieldById(
163 int32_t field_id) const override;
164 Result<std::optional<SchemaFieldConstRef>> GetFieldByIndex(
165 int32_t index) const override;
166 Result<std::optional<SchemaFieldConstRef>> GetFieldByName(
167 std::string_view name, bool case_sensitive) const override;
168 using NestedType::GetFieldByName;
169
170 protected:
171 bool Equals(const Type& other) const override;
172
173 SchemaField element_;
174};
175
177class ICEBERG_EXPORT MapType : public NestedType {
178 public:
179 constexpr static const TypeId kTypeId = TypeId::kMap;
180 constexpr static const std::string_view kKeyName = "key";
181 constexpr static const std::string_view kValueName = "value";
182
185 explicit MapType(SchemaField key, SchemaField value);
186 ~MapType() override = default;
187
188 const SchemaField& key() const;
189 const SchemaField& value() const;
190
191 TypeId type_id() const override;
192 std::string ToString() const override;
193
194 std::span<const SchemaField> fields() const override;
195 Result<std::optional<SchemaFieldConstRef>> GetFieldById(
196 int32_t field_id) const override;
197 Result<std::optional<SchemaFieldConstRef>> GetFieldByIndex(
198 int32_t index) const override;
199 Result<std::optional<SchemaFieldConstRef>> GetFieldByName(
200 std::string_view name, bool case_sensitive) const override;
201 using NestedType::GetFieldByName;
202
203 protected:
204 bool Equals(const Type& other) const override;
205
206 std::array<SchemaField, 2> fields_;
207};
208
210
214
216class ICEBERG_EXPORT BooleanType : public PrimitiveType {
217 public:
218 constexpr static const TypeId kTypeId = TypeId::kBoolean;
219
220 BooleanType() = default;
221 ~BooleanType() override = default;
222
223 TypeId type_id() const override;
224 std::string ToString() const override;
225
226 protected:
227 bool Equals(const Type& other) const override;
228};
229
231class ICEBERG_EXPORT IntType : public PrimitiveType {
232 public:
233 constexpr static const TypeId kTypeId = TypeId::kInt;
234
235 IntType() = default;
236 ~IntType() override = default;
237
238 TypeId type_id() const override;
239 std::string ToString() const override;
240
241 protected:
242 bool Equals(const Type& other) const override;
243};
244
246class ICEBERG_EXPORT LongType : public PrimitiveType {
247 public:
248 constexpr static const TypeId kTypeId = TypeId::kLong;
249
250 LongType() = default;
251 ~LongType() override = default;
252
253 TypeId type_id() const override;
254 std::string ToString() const override;
255
256 protected:
257 bool Equals(const Type& other) const override;
258};
259
262class ICEBERG_EXPORT FloatType : public PrimitiveType {
263 public:
264 constexpr static const TypeId kTypeId = TypeId::kFloat;
265
266 FloatType() = default;
267 ~FloatType() override = default;
268
269 TypeId type_id() const override;
270 std::string ToString() const override;
271
272 protected:
273 bool Equals(const Type& other) const override;
274};
275
278class ICEBERG_EXPORT DoubleType : public PrimitiveType {
279 public:
280 constexpr static const TypeId kTypeId = TypeId::kDouble;
281
282 DoubleType() = default;
283 ~DoubleType() override = default;
284
285 TypeId type_id() const override;
286 std::string ToString() const override;
287
288 protected:
289 bool Equals(const Type& other) const override;
290};
291
293class ICEBERG_EXPORT DecimalType : public PrimitiveType {
294 public:
295 constexpr static const TypeId kTypeId = TypeId::kDecimal;
296 constexpr static const int32_t kMaxPrecision = 38;
297
299 DecimalType(int32_t precision, int32_t scale);
300 ~DecimalType() override = default;
301
303 [[nodiscard]] int32_t precision() const;
306 [[nodiscard]] int32_t scale() const;
307
308 TypeId type_id() const override;
309 std::string ToString() const override;
310
311 protected:
312 bool Equals(const Type& other) const override;
313
314 private:
315 int32_t precision_;
316 int32_t scale_;
317};
318
321class ICEBERG_EXPORT DateType : public PrimitiveType {
322 public:
323 constexpr static const TypeId kTypeId = TypeId::kDate;
324
325 DateType() = default;
326 ~DateType() override = default;
327
328 TypeId type_id() const override;
329 std::string ToString() const override;
330
331 protected:
332 bool Equals(const Type& other) const override;
333};
334
337class ICEBERG_EXPORT TimeType : public PrimitiveType {
338 public:
339 constexpr static const TypeId kTypeId = TypeId::kTime;
340
341 TimeType() = default;
342 ~TimeType() override = default;
343
344 TypeId type_id() const override;
345 std::string ToString() const override;
346
347 protected:
348 bool Equals(const Type& other) const override;
349};
350
353class ICEBERG_EXPORT TimestampBase : public PrimitiveType {
354 public:
356 [[nodiscard]] virtual bool is_zoned() const = 0;
358 [[nodiscard]] virtual TimeUnit time_unit() const = 0;
359};
360
363class ICEBERG_EXPORT TimestampType : public TimestampBase {
364 public:
365 constexpr static const TypeId kTypeId = TypeId::kTimestamp;
366
367 TimestampType() = default;
368 ~TimestampType() override = default;
369
370 bool is_zoned() const override;
371 TimeUnit time_unit() const override;
372
373 TypeId type_id() const override;
374 std::string ToString() const override;
375
376 protected:
377 bool Equals(const Type& other) const override;
378};
379
382class ICEBERG_EXPORT TimestampTzType : public TimestampBase {
383 public:
384 constexpr static const TypeId kTypeId = TypeId::kTimestampTz;
385
386 TimestampTzType() = default;
387 ~TimestampTzType() override = default;
388
389 bool is_zoned() const override;
390 TimeUnit time_unit() const override;
391
392 TypeId type_id() const override;
393 std::string ToString() const override;
394
395 protected:
396 bool Equals(const Type& other) const override;
397};
398
400class ICEBERG_EXPORT BinaryType : public PrimitiveType {
401 public:
402 constexpr static const TypeId kTypeId = TypeId::kBinary;
403
404 BinaryType() = default;
405 ~BinaryType() override = default;
406
407 TypeId type_id() const override;
408 std::string ToString() const override;
409
410 protected:
411 bool Equals(const Type& other) const override;
412};
413
416class ICEBERG_EXPORT StringType : public PrimitiveType {
417 public:
418 constexpr static const TypeId kTypeId = TypeId::kString;
419
420 StringType() = default;
421 ~StringType() override = default;
422
423 TypeId type_id() const override;
424 std::string ToString() const override;
425
426 protected:
427 bool Equals(const Type& other) const override;
428};
429
431class ICEBERG_EXPORT FixedType : public PrimitiveType {
432 public:
433 constexpr static const TypeId kTypeId = TypeId::kFixed;
434
436 explicit FixedType(int32_t length);
437 ~FixedType() override = default;
438
440 [[nodiscard]] int32_t length() const;
441
442 TypeId type_id() const override;
443 std::string ToString() const override;
444
445 protected:
446 bool Equals(const Type& other) const override;
447
448 private:
449 int32_t length_;
450};
451
454class ICEBERG_EXPORT UuidType : public PrimitiveType {
455 public:
456 constexpr static const TypeId kTypeId = TypeId::kUuid;
457
458 UuidType() = default;
459 ~UuidType() override = default;
460
461 TypeId type_id() const override;
462 std::string ToString() const override;
463
464 protected:
465 bool Equals(const Type& other) const override;
466};
467
469
474
476ICEBERG_EXPORT const std::shared_ptr<BooleanType>& boolean();
478ICEBERG_EXPORT const std::shared_ptr<IntType>& int32();
480ICEBERG_EXPORT const std::shared_ptr<LongType>& int64();
482ICEBERG_EXPORT const std::shared_ptr<FloatType>& float32();
484ICEBERG_EXPORT const std::shared_ptr<DoubleType>& float64();
486ICEBERG_EXPORT const std::shared_ptr<DateType>& date();
488ICEBERG_EXPORT const std::shared_ptr<TimeType>& time();
490ICEBERG_EXPORT const std::shared_ptr<TimestampType>& timestamp();
492ICEBERG_EXPORT const std::shared_ptr<TimestampTzType>& timestamp_tz();
494ICEBERG_EXPORT const std::shared_ptr<BinaryType>& binary();
496ICEBERG_EXPORT const std::shared_ptr<StringType>& string();
498ICEBERG_EXPORT const std::shared_ptr<UuidType>& uuid();
499
504ICEBERG_EXPORT std::shared_ptr<DecimalType> decimal(int32_t precision, int32_t scale);
505
509ICEBERG_EXPORT std::shared_ptr<FixedType> fixed(int32_t length);
510
514ICEBERG_EXPORT std::shared_ptr<StructType> struct_(std::vector<SchemaField> fields);
515
519ICEBERG_EXPORT std::shared_ptr<ListType> list(SchemaField element);
520
525ICEBERG_EXPORT std::shared_ptr<MapType> map(SchemaField key, SchemaField value);
526
528
536ICEBERG_EXPORT std::string_view ToString(TypeId id);
537
538} // namespace iceberg
A data type representing an arbitrary-length byte sequence.
Definition type.h:400
A data type representing a boolean (true or false).
Definition type.h:216
A data type representing a calendar date without reference to a timezone or time.
Definition type.h:321
A data type representing a fixed-precision decimal.
Definition type.h:293
A data type representing a 64-bit (double precision) IEEE-754 float.
Definition type.h:278
A data type representing a fixed-length bytestring.
Definition type.h:431
A data type representing a 32-bit (single precision) IEEE-754 float.
Definition type.h:262
A data type representing a 32-bit signed integer.
Definition type.h:231
A data type representing a list of values.
Definition type.h:145
A data type representing a 64-bit signed integer.
Definition type.h:246
A data type representing a dictionary of values.
Definition type.h:177
A data type that has child fields.
Definition type.h:73
virtual std::span< const SchemaField > fields() const =0
Get a view of the child fields.
virtual Result< std::optional< SchemaFieldConstRef > > GetFieldByName(std::string_view name, bool case_sensitive) const =0
Get a field by name. Return an error Status if the field name is not unique; prefer GetFieldById or G...
virtual Result< std::optional< SchemaFieldConstRef > > GetFieldById(int32_t field_id) const =0
Get a field by field ID.
virtual Result< std::optional< SchemaFieldConstRef > > GetFieldByIndex(int32_t index) const =0
Get a field by index.
bool is_primitive() const override
Is this a primitive type (may not have child fields)?
Definition type.h:75
bool is_nested() const override
Is this a nested type (may have child fields)?
Definition type.h:76
A data type that does not have child fields.
Definition type.h:66
bool is_primitive() const override
Is this a primitive type (may not have child fields)?
Definition type.h:68
bool is_nested() const override
Is this a nested type (may have child fields)?
Definition type.h:69
A type combined with a name.
Definition schema_field.h:39
A data type representing an arbitrary-length character sequence (encoded in UTF-8).
Definition type.h:416
A data type representing a struct with nested fields.
Definition type.h:108
A data type representing a wall clock time in microseconds without reference to a timezone or date.
Definition type.h:337
A base class for any timestamp time (irrespective of unit or timezone).
Definition type.h:353
virtual bool is_zoned() const =0
Is this type zoned or naive?
virtual TimeUnit time_unit() const =0
The time resolution.
A data type representing a timestamp in microseconds without reference to a timezone.
Definition type.h:363
A data type representing a timestamp as microseconds since the epoch in UTC. A time zone or offset is...
Definition type.h:382
Interface for a data type for a field.
Definition type.h:44
virtual TypeId type_id() const =0
Get the type ID.
virtual bool is_primitive() const =0
Is this a primitive type (may not have child fields)?
virtual bool is_nested() const =0
Is this a nested type (may have child fields)?
friend bool operator==(const Type &lhs, const Type &rhs)
Compare two types for equality.
Definition type.h:58
virtual bool Equals(const Type &other) const =0
Compare two types for equality.
A data type representing a UUID. While defined as a distinct type, it is effectively a fixed(16).
Definition type.h:454
Interface for objects that can be formatted via std::format.
Definition formattable.h:36
ICEBERG_EXPORT const std::shared_ptr< BinaryType > & binary()
Return a BinaryType instance.
ICEBERG_EXPORT const std::shared_ptr< DateType > & date()
Return a DateType instance.
std::shared_ptr< MapType > map(SchemaField key, SchemaField value)
Create a MapType with the given key and value fields.
Definition type.cc:388
std::shared_ptr< ListType > list(SchemaField element)
Create a ListType with the given element field.
Definition type.cc:392
ICEBERG_EXPORT const std::shared_ptr< TimestampTzType > & timestamp_tz()
Return a TimestampTzType instance.
ICEBERG_EXPORT const std::shared_ptr< TimeType > & time()
Return a TimeType instance.
ICEBERG_EXPORT const std::shared_ptr< DoubleType > & float64()
Return a DoubleType instance.
ICEBERG_EXPORT const std::shared_ptr< StringType > & string()
Return a StringType instance.
ICEBERG_EXPORT const std::shared_ptr< IntType > & int32()
Return an IntType instance.
ICEBERG_EXPORT const std::shared_ptr< TimestampType > & timestamp()
Return a TimestampType instance.
std::shared_ptr< FixedType > fixed(int32_t length)
Create a FixedType with the given length.
Definition type.cc:384
ICEBERG_EXPORT const std::shared_ptr< FloatType > & float32()
Return a FloatType instance.
std::shared_ptr< StructType > struct_(std::vector< SchemaField > fields)
Create a StructType with the given fields.
Definition type.cc:396
std::shared_ptr< DecimalType > decimal(int32_t precision, int32_t scale)
Create a DecimalType with the given precision and scale.
Definition type.cc:380
ICEBERG_EXPORT const std::shared_ptr< BooleanType > & boolean()
Return a BooleanType instance.
ICEBERG_EXPORT const std::shared_ptr< LongType > & int64()
Return a LongType instance.
ICEBERG_EXPORT const std::shared_ptr< UuidType > & uuid()
Return a UuidType instance.
TimeUnit
The time unit. In Iceberg V3 nanoseconds are also supported.
Definition type_fwd.h:56
TypeId
A data type.
Definition type_fwd.h:35