|
| 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 | + |
| 22 | +/// \file iceberg/type.h |
| 23 | +/// Data types for Iceberg. |
| 24 | + |
| 25 | +#include <memory> |
| 26 | +#include <span> |
| 27 | +#include <string> |
| 28 | +#include <vector> |
| 29 | + |
| 30 | +#include "iceberg/iceberg_export.h" |
| 31 | +#include "iceberg/type_fwd.h" |
| 32 | + |
| 33 | +namespace iceberg { |
| 34 | + |
| 35 | +/// \brief A type combined with a name. |
| 36 | +class ICEBERG_EXPORT Field { |
| 37 | + public: |
| 38 | + /// \brief Construct a field. |
| 39 | + /// \param[in] field_id The field ID. |
| 40 | + /// \param[in] name The field name. |
| 41 | + /// \param[in] type The field type. |
| 42 | + /// \param[in] optional Whether values of this field are required or nullable. |
| 43 | + Field(int32_t field_id, std::string name, std::shared_ptr<Type> type, bool optional); |
| 44 | + |
| 45 | + /// \brief Construct an optional (nullable) field. |
| 46 | + static Field MakeOptional(int32_t field_id, std::string name, |
| 47 | + std::shared_ptr<Type> type); |
| 48 | + /// \brief Construct a required (non-null) field. |
| 49 | + static Field MakeRequired(int32_t field_id, std::string name, |
| 50 | + std::shared_ptr<Type> type); |
| 51 | + |
| 52 | + /// \brief Get the field ID. |
| 53 | + [[nodiscard]] int32_t field_id() const; |
| 54 | + |
| 55 | + /// \brief Get the field name. |
| 56 | + [[nodiscard]] std::string_view name() const; |
| 57 | + |
| 58 | + /// \brief Get the field type. |
| 59 | + [[nodiscard]] const std::shared_ptr<Type>& type() const; |
| 60 | + |
| 61 | + /// \brief Get whether the field is optional. |
| 62 | + [[nodiscard]] bool optional() const; |
| 63 | + |
| 64 | + /// \brief Get a user-readable string representation of the field. |
| 65 | + [[nodiscard]] std::string ToString() const; |
| 66 | + |
| 67 | + /// \brief Compare two fields for equality. |
| 68 | + [[nodiscard]] bool Equals(const Field& other) const; |
| 69 | + |
| 70 | + friend bool operator==(const Field& lhs, const Field& rhs) { return lhs.Equals(rhs); } |
| 71 | + |
| 72 | + friend bool operator!=(const Field& lhs, const Field& rhs) { return !(lhs == rhs); } |
| 73 | +}; |
| 74 | + |
| 75 | +/// \brief Interface for a data type for a field. |
| 76 | +class ICEBERG_EXPORT Type { |
| 77 | + public: |
| 78 | + virtual ~Type() = default; |
| 79 | + |
| 80 | + /// \brief Get the type ID. |
| 81 | + [[nodiscard]] virtual TypeId type_id() const = 0; |
| 82 | + |
| 83 | + /// \brief Get a user-readable string representation of the type. |
| 84 | + [[nodiscard]] virtual std::string ToString() const = 0; |
| 85 | + |
| 86 | + /// \brief Compare two types for equality. |
| 87 | + [[nodiscard]] virtual bool Equals(const Type& other) const = 0; |
| 88 | + |
| 89 | + /// \brief Is this a primitive type (may not have child fields)? |
| 90 | + [[nodiscard]] virtual bool is_primitive() const = 0; |
| 91 | + |
| 92 | + /// \brief Is this a nested type (may have child fields)? |
| 93 | + [[nodiscard]] virtual bool is_nested() const = 0; |
| 94 | + |
| 95 | + friend bool operator==(const Type& lhs, const Type& rhs) { return lhs.Equals(rhs); } |
| 96 | + |
| 97 | + friend bool operator!=(const Type& lhs, const Type& rhs) { return !(lhs == rhs); } |
| 98 | +}; |
| 99 | + |
| 100 | +/// \brief A data type that may not have child fields. |
| 101 | +class ICEBERG_EXPORT PrimitiveType : public Type { |
| 102 | + public: |
| 103 | + bool is_primitive() const override { return true; } |
| 104 | + bool is_nested() const override { return false; } |
| 105 | +}; |
| 106 | + |
| 107 | +/// \brief A data type that may have child fields. |
| 108 | +class ICEBERG_EXPORT NestedType : public Type { |
| 109 | + public: |
| 110 | + bool is_primitive() const override { return false; } |
| 111 | + bool is_nested() const override { return true; } |
| 112 | +}; |
| 113 | + |
| 114 | +/// \brief A data type representing a boolean. |
| 115 | +class ICEBERG_EXPORT BooleanType : public PrimitiveType { |
| 116 | + public: |
| 117 | + BooleanType() = default; |
| 118 | + ~BooleanType() = default; |
| 119 | + |
| 120 | + TypeId type_id() const override; |
| 121 | + |
| 122 | + std::string ToString() const override; |
| 123 | + |
| 124 | + bool Equals(const Type& other) const override; |
| 125 | +}; |
| 126 | + |
| 127 | +/// \brief A data type representing a struct with nested fields. |
| 128 | +class ICEBERG_EXPORT StructType : public NestedType { |
| 129 | + public: |
| 130 | + explicit StructType(std::vector<Field> fields); |
| 131 | + |
| 132 | + StructType() = default; |
| 133 | + ~StructType() = default; |
| 134 | + |
| 135 | + /// \brief Get a view of the child fields. |
| 136 | + [[nodiscard]] std::span<Field> fields() const; |
| 137 | + /// \brief Get a field by index. |
| 138 | + const Field& GetFieldByIndex(int i) const; |
| 139 | + /// \brief Get a field by name. |
| 140 | + const Field& GetFieldByName(std::string_view name) const; |
| 141 | + |
| 142 | + TypeId type_id() const override; |
| 143 | + |
| 144 | + std::string ToString() const override; |
| 145 | + |
| 146 | + bool Equals(const Type& other) const override; |
| 147 | +}; |
| 148 | + |
| 149 | +} // namespace iceberg |
0 commit comments