|
| 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/field.h |
| 23 | +/// A (schema) field is a name and a type and is part of a schema or nested |
| 24 | +/// type (e.g. a struct). |
| 25 | + |
| 26 | +#include <cstdint> |
| 27 | +#include <memory> |
| 28 | +#include <string> |
| 29 | +#include <string_view> |
| 30 | + |
| 31 | +#include "iceberg/iceberg_export.h" |
| 32 | +#include "iceberg/type_fwd.h" |
| 33 | +#include "iceberg/util/formattable.h" |
| 34 | + |
| 35 | +namespace iceberg { |
| 36 | + |
| 37 | +/// \brief A type combined with a name. |
| 38 | +class ICEBERG_EXPORT SchemaField : public iceberg::util::Formattable { |
| 39 | + public: |
| 40 | + /// \brief Construct a field. |
| 41 | + /// \param[in] field_id The field ID. |
| 42 | + /// \param[in] name The field name. |
| 43 | + /// \param[in] type The field type. |
| 44 | + /// \param[in] optional Whether values of this field are required or nullable. |
| 45 | + SchemaField(int32_t field_id, std::string name, std::shared_ptr<Type> type, |
| 46 | + bool optional); |
| 47 | + |
| 48 | + /// \brief Construct an optional (nullable) field. |
| 49 | + static SchemaField MakeOptional(int32_t field_id, std::string name, |
| 50 | + std::shared_ptr<Type> type); |
| 51 | + /// \brief Construct a required (non-null) field. |
| 52 | + static SchemaField MakeRequired(int32_t field_id, std::string name, |
| 53 | + std::shared_ptr<Type> type); |
| 54 | + |
| 55 | + /// \brief Get the field ID. |
| 56 | + [[nodiscard]] int32_t field_id() const; |
| 57 | + |
| 58 | + /// \brief Get the field name. |
| 59 | + [[nodiscard]] std::string_view name() const; |
| 60 | + |
| 61 | + /// \brief Get the field type. |
| 62 | + [[nodiscard]] const std::shared_ptr<Type>& type() const; |
| 63 | + |
| 64 | + /// \brief Get whether the field is optional. |
| 65 | + [[nodiscard]] bool optional() const; |
| 66 | + |
| 67 | + /// \brief Get a user-readable string representation of the field. |
| 68 | + [[nodiscard]] std::string ToString() const; |
| 69 | + |
| 70 | + friend bool operator==(const SchemaField& lhs, const SchemaField& rhs) { |
| 71 | + return lhs.Equals(rhs); |
| 72 | + } |
| 73 | + |
| 74 | + friend bool operator!=(const SchemaField& lhs, const SchemaField& rhs) { |
| 75 | + return !(lhs == rhs); |
| 76 | + } |
| 77 | + |
| 78 | + private: |
| 79 | + /// \brief Compare two fields for equality. |
| 80 | + [[nodiscard]] bool Equals(const SchemaField& other) const; |
| 81 | + |
| 82 | + int32_t field_id_; |
| 83 | + std::string name_; |
| 84 | + std::shared_ptr<Type> type_; |
| 85 | + bool optional_; |
| 86 | +}; |
| 87 | + |
| 88 | +} // namespace iceberg |
0 commit comments