|
| 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 Interface for a data type for a field. |
| 36 | +class ICEBERG_EXPORT Type { |
| 37 | + public: |
| 38 | + virtual ~Type() = default; |
| 39 | + |
| 40 | + /// \brief Get the type ID. |
| 41 | + [[nodiscard]] virtual TypeId type_id() const = 0; |
| 42 | + |
| 43 | + /// \brief Get a user-readable string representation of the type. |
| 44 | + [[nodiscard]] virtual std::string ToString() const = 0; |
| 45 | + |
| 46 | + /// \brief Compare two types for equality. |
| 47 | + [[nodiscard]] virtual bool Equals(const Type& other) const = 0; |
| 48 | + |
| 49 | + friend bool operator==(const Type& lhs, const Type& rhs) { return lhs.Equals(rhs); } |
| 50 | + |
| 51 | + friend bool operator!=(const Type& lhs, const Type& rhs) { return !(lhs == rhs); } |
| 52 | +}; |
| 53 | + |
| 54 | +/// \brief A type combined with a name. |
| 55 | +class ICEBERG_EXPORT Field { |
| 56 | + public: |
| 57 | + Field(int32_t field_id, std::string name, std::shared_ptr<Type> type, bool optional); |
| 58 | + |
| 59 | + static Field MakeOptional(int32_t field_id, std::string name, |
| 60 | + std::shared_ptr<Type> type); |
| 61 | + static Field MakeRequired(int32_t field_id, std::string name, |
| 62 | + std::shared_ptr<Type> type); |
| 63 | + |
| 64 | + /// \brief Get the field ID. |
| 65 | + [[nodiscard]] int32_t field_id() const; |
| 66 | + |
| 67 | + /// \brief Get the field name. |
| 68 | + [[nodiscard]] std::string_view name() const; |
| 69 | + |
| 70 | + /// \brief Get the field type. |
| 71 | + [[nodiscard]] const std::shared_ptr<Type>& type() const; |
| 72 | + |
| 73 | + /// \brief Get whether the field is optional. |
| 74 | + [[nodiscard]] bool optional() const; |
| 75 | + |
| 76 | + /// \brief Get a user-readable string representation of the field. |
| 77 | + [[nodiscard]] std::string ToString() const; |
| 78 | + |
| 79 | + /// \brief Compare two fields for equality. |
| 80 | + [[nodiscard]] bool Equals(const Field& other) const; |
| 81 | + |
| 82 | + friend bool operator==(const Field& lhs, const Field& rhs) { return lhs.Equals(rhs); } |
| 83 | + |
| 84 | + friend bool operator!=(const Field& lhs, const Field& rhs) { return !(lhs == rhs); } |
| 85 | +}; |
| 86 | + |
| 87 | +/// \brief A data type representing a boolean. |
| 88 | +class ICEBERG_EXPORT BooleanType : public Type { |
| 89 | + public: |
| 90 | + BooleanType() = default; |
| 91 | + ~BooleanType() = default; |
| 92 | + |
| 93 | + TypeId type_id() const override; |
| 94 | + |
| 95 | + std::string ToString() const override; |
| 96 | + |
| 97 | + bool Equals(const Type& other) const override; |
| 98 | +}; |
| 99 | + |
| 100 | +/// \brief A data type representing a struct with nested fields. |
| 101 | +class ICEBERG_EXPORT StructType : public Type { |
| 102 | + public: |
| 103 | + explicit StructType(std::vector<Field> fields); |
| 104 | + |
| 105 | + StructType() = default; |
| 106 | + ~StructType() = default; |
| 107 | + |
| 108 | + [[nodiscard]] std::span<Field> fields() const; |
| 109 | + |
| 110 | + const Field& field(int i) const; |
| 111 | + const Field& field(std::string_view name) const; |
| 112 | + |
| 113 | + TypeId type_id() const override; |
| 114 | + |
| 115 | + std::string ToString() const override; |
| 116 | + |
| 117 | + bool Equals(const Type& other) const override; |
| 118 | +}; |
| 119 | + |
| 120 | +} // namespace iceberg |
0 commit comments