iceberg-cpp
Loading...
Searching...
No Matches
schema_field.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 <cstdint>
27#include <memory>
28#include <string>
29#include <string_view>
30
31#include "iceberg/iceberg_export.h"
32#include "iceberg/result.h"
33#include "iceberg/type_fwd.h"
35
36namespace iceberg {
37
39class ICEBERG_EXPORT SchemaField : public iceberg::util::Formattable {
40 public:
41 static constexpr int32_t kInvalidFieldId = -1;
42
53 SchemaField(int32_t field_id, std::string_view name, std::shared_ptr<Type> type,
54 bool optional, std::string_view doc = {},
55 std::shared_ptr<const Literal> initial_default = nullptr,
56 std::shared_ptr<const Literal> write_default = nullptr);
57
59 static SchemaField MakeOptional(int32_t field_id, std::string_view name,
60 std::shared_ptr<Type> type, std::string_view doc = {});
62 static SchemaField MakeRequired(int32_t field_id, std::string_view name,
63 std::shared_ptr<Type> type, std::string_view doc = {});
64
66 [[nodiscard]] int32_t field_id() const;
67
69 [[nodiscard]] std::string_view name() const;
70
72 [[nodiscard]] const std::shared_ptr<Type>& type() const;
73
75 [[nodiscard]] bool optional() const;
76
78 std::string_view doc() const;
79
82 const std::shared_ptr<const Literal>& initial_default() const;
83
86 const std::shared_ptr<const Literal>& write_default() const;
87
88 [[nodiscard]] std::string ToString() const override;
89
90 Status Validate() const;
91
92 friend bool operator==(const SchemaField& lhs, const SchemaField& rhs) {
93 return lhs.Equals(rhs);
94 }
95
96 SchemaField AsRequired() const {
97 auto copy = *this;
98 copy.optional_ = false;
99 return copy;
100 }
101
102 SchemaField AsOptional() const {
103 auto copy = *this;
104 copy.optional_ = true;
105 return copy;
106 }
107
108 private:
110 [[nodiscard]] bool Equals(const SchemaField& other) const;
111
112 int32_t field_id_;
113 std::string name_;
114 std::shared_ptr<Type> type_;
115 bool optional_;
116 std::string doc_;
117 // Immutable default values, shared (not deep-copied) across field copies, like `type_`.
118 std::shared_ptr<const Literal> initial_default_;
119 std::shared_ptr<const Literal> write_default_;
120};
121
122} // namespace iceberg
A type combined with a name.
Definition schema_field.h:39
Interface for objects that can be formatted via std::format.
Definition formattable.h:36