32#include <unordered_map>
35#include "iceberg/iceberg_export.h"
36#include "iceberg/result.h"
46 ~Type()
override =
default;
58 bool is_struct()
const {
return type_id() == TypeId::kStruct; }
61 bool is_list()
const {
return type_id() == TypeId::kList; }
64 bool is_map()
const {
return type_id() == TypeId::kMap; }
67 bool is_variant()
const {
return type_id() == TypeId::kVariant; }
91 virtual std::span<const SchemaField>
fields()
const = 0;
92 using SchemaFieldConstRef = std::reference_wrapper<const SchemaField>;
97 int32_t field_id)
const = 0;
102 int32_t index)
const = 0;
109 std::string_view name,
bool case_sensitive)
const = 0;
111 Result<std::optional<SchemaFieldConstRef>> GetFieldByName(std::string_view name)
const;
121 constexpr static TypeId kTypeId = TypeId::kStruct;
122 explicit StructType(std::vector<SchemaField> fields);
125 TypeId type_id()
const override;
126 std::string ToString()
const override;
128 std::span<const SchemaField> fields()
const override;
129 Result<std::optional<SchemaFieldConstRef>> GetFieldById(
130 int32_t field_id)
const override;
131 Result<std::optional<SchemaFieldConstRef>> GetFieldByIndex(
132 int32_t index)
const override;
133 Result<std::optional<SchemaFieldConstRef>> GetFieldByName(
134 std::string_view name,
bool case_sensitive)
const override;
135 using NestedType::GetFieldByName;
137 std::unique_ptr<Schema> ToSchema()
const;
140 bool Equals(
const Type& other)
const override;
142 static Result<std::unordered_map<int32_t, SchemaFieldConstRef>> InitFieldById(
144 static Result<std::unordered_map<std::string_view, SchemaFieldConstRef>>
146 static Result<std::unordered_map<std::string, SchemaFieldConstRef>>
149 std::vector<SchemaField> fields_;
150 Lazy<InitFieldById> field_by_id_;
151 Lazy<InitFieldByName> field_by_name_;
152 Lazy<InitFieldByLowerCaseName> field_by_lowercase_name_;
158 constexpr static const TypeId kTypeId = TypeId::kList;
159 constexpr static const std::string_view kElementName =
"element";
161 static Result<std::unique_ptr<ListType>> Make(
SchemaField element);
168 ListType(int32_t field_id, std::shared_ptr<Type> type,
bool optional);
171 TypeId type_id()
const override;
173 std::string ToString()
const override;
175 std::span<const SchemaField> fields()
const override;
176 Result<std::optional<SchemaFieldConstRef>> GetFieldById(
177 int32_t field_id)
const override;
178 Result<std::optional<SchemaFieldConstRef>> GetFieldByIndex(
179 int32_t index)
const override;
180 Result<std::optional<SchemaFieldConstRef>> GetFieldByName(
181 std::string_view name,
bool case_sensitive)
const override;
182 using NestedType::GetFieldByName;
185 bool Equals(
const Type& other)
const override;
193 constexpr static const TypeId kTypeId = TypeId::kMap;
194 constexpr static const std::string_view kKeyName =
"key";
195 constexpr static const std::string_view kValueName =
"value";
208 TypeId type_id()
const override;
209 std::string ToString()
const override;
211 std::span<const SchemaField> fields()
const override;
212 Result<std::optional<SchemaFieldConstRef>> GetFieldById(
213 int32_t field_id)
const override;
214 Result<std::optional<SchemaFieldConstRef>> GetFieldByIndex(
215 int32_t index)
const override;
216 Result<std::optional<SchemaFieldConstRef>> GetFieldByName(
217 std::string_view name,
bool case_sensitive)
const override;
218 using NestedType::GetFieldByName;
221 bool Equals(
const Type& other)
const override;
223 std::array<SchemaField, 2> fields_;
235 constexpr static const TypeId kTypeId = TypeId::kVariant;
243 TypeId type_id()
const override;
244 std::string ToString()
const override;
247 bool Equals(
const Type& other)
const override;
259 constexpr static const TypeId kTypeId = TypeId::kBoolean;
264 TypeId type_id()
const override;
265 std::string ToString()
const override;
268 bool Equals(
const Type& other)
const override;
274 constexpr static const TypeId kTypeId = TypeId::kInt;
279 TypeId type_id()
const override;
280 std::string ToString()
const override;
283 bool Equals(
const Type& other)
const override;
289 constexpr static const TypeId kTypeId = TypeId::kLong;
294 TypeId type_id()
const override;
295 std::string ToString()
const override;
298 bool Equals(
const Type& other)
const override;
305 constexpr static const TypeId kTypeId = TypeId::kFloat;
310 TypeId type_id()
const override;
311 std::string ToString()
const override;
314 bool Equals(
const Type& other)
const override;
321 constexpr static const TypeId kTypeId = TypeId::kDouble;
326 TypeId type_id()
const override;
327 std::string ToString()
const override;
330 bool Equals(
const Type& other)
const override;
336 constexpr static const TypeId kTypeId = TypeId::kDecimal;
337 constexpr static const int32_t kMaxPrecision = 38;
345 int32_t precision()
const;
348 int32_t scale()
const;
350 TypeId type_id()
const override;
351 std::string ToString()
const override;
354 bool Equals(
const Type& other)
const override;
365 constexpr static const TypeId kTypeId = TypeId::kDate;
370 TypeId type_id()
const override;
371 std::string ToString()
const override;
374 bool Equals(
const Type& other)
const override;
381 constexpr static const TypeId kTypeId = TypeId::kTime;
386 TypeId type_id()
const override;
387 std::string ToString()
const override;
390 bool Equals(
const Type& other)
const override;
407 constexpr static const TypeId kTypeId = TypeId::kTimestamp;
412 bool is_zoned()
const override;
413 TimeUnit time_unit()
const override;
415 TypeId type_id()
const override;
416 std::string ToString()
const override;
419 bool Equals(
const Type& other)
const override;
426 constexpr static const TypeId kTypeId = TypeId::kTimestampTz;
431 bool is_zoned()
const override;
432 TimeUnit time_unit()
const override;
434 TypeId type_id()
const override;
435 std::string ToString()
const override;
438 bool Equals(
const Type& other)
const override;
445 constexpr static const TypeId kTypeId = TypeId::kTimestampNs;
450 bool is_zoned()
const override;
451 TimeUnit time_unit()
const override;
453 TypeId type_id()
const override;
454 std::string ToString()
const override;
457 bool Equals(
const Type& other)
const override;
464 constexpr static const TypeId kTypeId = TypeId::kTimestampTzNs;
469 bool is_zoned()
const override;
470 TimeUnit time_unit()
const override;
472 TypeId type_id()
const override;
473 std::string ToString()
const override;
476 bool Equals(
const Type& other)
const override;
482 constexpr static const TypeId kTypeId = TypeId::kBinary;
487 TypeId type_id()
const override;
488 std::string ToString()
const override;
491 bool Equals(
const Type& other)
const override;
498 constexpr static const TypeId kTypeId = TypeId::kString;
503 TypeId type_id()
const override;
504 std::string ToString()
const override;
507 bool Equals(
const Type& other)
const override;
513 constexpr static const TypeId kTypeId = TypeId::kFixed;
521 int32_t length()
const;
523 TypeId type_id()
const override;
524 std::string ToString()
const override;
527 bool Equals(
const Type& other)
const override;
537 constexpr static const TypeId kTypeId = TypeId::kUuid;
542 TypeId type_id()
const override;
543 std::string ToString()
const override;
546 bool Equals(
const Type& other)
const override;
552 constexpr static const TypeId kTypeId = TypeId::kUnknown;
557 TypeId type_id()
const override;
558 std::string ToString()
const override;
561 bool Equals(
const Type& other)
const override;
567 constexpr static const TypeId kTypeId = TypeId::kGeometry;
568 constexpr static std::string_view kDefaultCrs =
"OGC:CRS84";
570 static Result<std::unique_ptr<GeometryType>> Make();
571 static Result<std::unique_ptr<GeometryType>> Make(std::string crs);
574 std::string_view crs()
const;
576 TypeId type_id()
const override;
577 std::string ToString()
const override;
580 bool Equals(
const Type& other)
const override;
592 constexpr static const TypeId kTypeId = TypeId::kGeography;
593 constexpr static std::string_view kDefaultCrs =
"OGC:CRS84";
594 constexpr static EdgeAlgorithm kDefaultAlgorithm = EdgeAlgorithm::kSpherical;
596 static Result<std::unique_ptr<GeographyType>> Make();
597 static Result<std::unique_ptr<GeographyType>> Make(std::string crs);
598 static Result<std::unique_ptr<GeographyType>> Make(std::string crs,
602 std::string_view crs()
const;
605 TypeId type_id()
const override;
606 std::string ToString()
const override;
609 bool Equals(
const Type& other)
const override;
617 std::optional<EdgeAlgorithm> algorithm_;
628ICEBERG_EXPORT
const std::shared_ptr<BooleanType>&
boolean();
630ICEBERG_EXPORT
const std::shared_ptr<IntType>&
int32();
632ICEBERG_EXPORT
const std::shared_ptr<LongType>&
int64();
634ICEBERG_EXPORT
const std::shared_ptr<FloatType>&
float32();
636ICEBERG_EXPORT
const std::shared_ptr<DoubleType>&
float64();
638ICEBERG_EXPORT
const std::shared_ptr<DateType>&
date();
640ICEBERG_EXPORT
const std::shared_ptr<TimeType>&
time();
642ICEBERG_EXPORT
const std::shared_ptr<TimestampType>&
timestamp();
650ICEBERG_EXPORT
const std::shared_ptr<BinaryType>&
binary();
652ICEBERG_EXPORT
const std::shared_ptr<StringType>&
string();
654ICEBERG_EXPORT
const std::shared_ptr<UuidType>&
uuid();
656ICEBERG_EXPORT
const std::shared_ptr<UnknownType>&
unknown();
658ICEBERG_EXPORT
const std::shared_ptr<VariantType>&
variant();
660ICEBERG_EXPORT
const std::shared_ptr<GeometryType>&
geometry();
662ICEBERG_EXPORT
const std::shared_ptr<GeographyType>&
geography();
669ICEBERG_EXPORT std::shared_ptr<DecimalType>
decimal(int32_t precision, int32_t scale);
675ICEBERG_EXPORT std::shared_ptr<FixedType>
fixed(int32_t length);
679ICEBERG_EXPORT std::shared_ptr<GeometryType>
geometry(std::string crs);
683ICEBERG_EXPORT std::shared_ptr<GeographyType>
geography(std::string crs);
687ICEBERG_EXPORT std::shared_ptr<GeographyType>
geography(std::string crs,
693ICEBERG_EXPORT std::shared_ptr<StructType>
struct_(std::vector<SchemaField> fields);
717ICEBERG_EXPORT std::string_view ToString(
TypeId id);
720ICEBERG_EXPORT std::string_view ToString(
EdgeAlgorithm algorithm);
723ICEBERG_EXPORT Result<EdgeAlgorithm> EdgeAlgorithmFromString(std::string_view name);
A data type representing an arbitrary-length byte sequence.
Definition type.h:480
A data type representing a boolean (true or false).
Definition type.h:257
A data type representing a calendar date without reference to a timezone or time.
Definition type.h:363
A data type representing a fixed-precision decimal.
Definition type.h:334
A data type representing a 64-bit (double precision) IEEE-754 float.
Definition type.h:319
A data type representing a fixed-length bytestring.
Definition type.h:511
A data type representing a 32-bit (single precision) IEEE-754 float.
Definition type.h:303
A data type representing OGC geography in WKB format.
Definition type.h:590
A data type representing OGC geometry in WKB format.
Definition type.h:565
A data type representing a 32-bit signed integer.
Definition type.h:272
A data type representing a list of values.
Definition type.h:156
A data type representing a 64-bit signed integer.
Definition type.h:287
A data type representing a dictionary of values.
Definition type.h:191
A data type that has child fields.
Definition type.h:85
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:87
bool is_nested() const override
Is this a nested type (may have child fields)?
Definition type.h:88
A data type that does not have child fields.
Definition type.h:78
bool is_primitive() const override
Is this a primitive type (may not have child fields)?
Definition type.h:80
bool is_nested() const override
Is this a nested type (may have child fields)?
Definition type.h:81
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:496
A data type representing a struct with nested fields.
Definition type.h:119
A data type representing a wall clock time in microseconds without reference to a timezone or date.
Definition type.h:379
A base class for any timestamp time (irrespective of unit or timezone).
Definition type.h:395
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 nanoseconds without reference to a timezone.
Definition type.h:443
A data type representing a timestamp in microseconds without reference to a timezone.
Definition type.h:405
A data type representing a timestamp as nanoseconds since the epoch in UTC. A time zone or offset is ...
Definition type.h:462
A data type representing a timestamp as microseconds since the epoch in UTC. A time zone or offset is...
Definition type.h:424
Interface for a data type for a field.
Definition type.h:44
bool is_struct() const
Is this a struct type?
Definition type.h:58
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)?
bool is_variant() const
Is this a variant type?
Definition type.h:67
friend bool operator==(const Type &lhs, const Type &rhs)
Compare two types for equality.
Definition type.h:70
virtual bool Equals(const Type &other) const =0
Compare two types for equality.
bool is_list() const
Is this a list type?
Definition type.h:61
bool is_map() const
Is this a map type?
Definition type.h:64
A null-only placeholder type used when a more specific type is not known.
Definition type.h:550
A data type representing a UUID. While defined as a distinct type, it is effectively a fixed(16).
Definition type.h:535
A semi-structured type whose structure may vary across rows.
Definition type.h:233
bool is_nested() const override
Is this a nested type (may have child fields)?
Definition type.h:241
bool is_primitive() const override
Is this a primitive type (may not have child fields)?
Definition type.h:240
const std::shared_ptr< GeographyType > & geography()
Return the default GeographyType instance.
Definition type.cc:518
ICEBERG_EXPORT const std::shared_ptr< TimestampNsType > & timestamp_ns()
Return a TimestampNsType instance.
ICEBERG_EXPORT const std::shared_ptr< VariantType > & variant()
Return a VariantType instance.
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< ListType > list(SchemaField element)
Create a ListType with the given element field.
Definition type.cc:567
ICEBERG_EXPORT const std::shared_ptr< TimestampTzType > & timestamp_tz()
Return a TimestampTzType instance.
ICEBERG_EXPORT const std::shared_ptr< UnknownType > & unknown()
Return an UnknownType 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.
ICEBERG_EXPORT const std::shared_ptr< TimestampTzNsType > & timestamptz_ns()
Return a TimestampTzNsType instance.
std::shared_ptr< FixedType > fixed(int32_t length)
Create a FixedType with the given length.
Definition type.cc:531
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:575
const std::shared_ptr< GeometryType > & geometry()
Return the default GeometryType instance.
Definition type.cc:509
std::shared_ptr< DecimalType > decimal(int32_t precision, int32_t scale)
Create a DecimalType with the given precision and scale.
Definition type.cc:527
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:62
EdgeAlgorithm
The algorithm used to interpolate geography edges.
Definition type_fwd.h:68
TypeId
A data type.
Definition type_fwd.h:35