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
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
51 SchemaField(int32_t field_id, std::string_view name, std::shared_ptr<Type> type,
52 bool optional, std::string_view doc = {},
53 std::shared_ptr<const Literal> initial_default = nullptr,
54 std::shared_ptr<const Literal> write_default = nullptr);
55
57 static SchemaField MakeOptional(int32_t field_id, std::string_view name,
58 std::shared_ptr<Type> type, std::string_view doc = {});
60 static SchemaField MakeRequired(int32_t field_id, std::string_view name,
61 std::shared_ptr<Type> type, std::string_view doc = {});
62
64 SchemaField WithName(std::string_view name) const;
65
67 SchemaField WithType(std::shared_ptr<Type> type) const;
68
70 SchemaField WithDoc(std::string_view doc) const;
71
73 SchemaField WithInitialDefault(std::shared_ptr<const Literal> initial_default) const;
74
76 SchemaField WithWriteDefault(std::shared_ptr<const Literal> write_default) const;
77
80 const Literal& value, const std::shared_ptr<PrimitiveType>& target_type);
81
83 int32_t field_id() const;
84
86 std::string_view name() const;
87
89 const std::shared_ptr<Type>& type() const;
90
92 [[nodiscard]] bool optional() const;
93
95 std::string_view doc() const;
96
98 const std::shared_ptr<const Literal>& initial_default() const;
99
101 const std::shared_ptr<const Literal>& write_default() const;
102
103 std::string ToString() const override;
104
105 Status Validate() const;
106
107 friend bool operator==(const SchemaField& lhs, const SchemaField& rhs) {
108 return lhs.Equals(rhs);
109 }
110
111 SchemaField AsRequired() const {
112 auto copy = *this;
113 copy.optional_ = false;
114 return copy;
115 }
116
117 SchemaField AsOptional() const {
118 auto copy = *this;
119 copy.optional_ = true;
120 return copy;
121 }
122
123 private:
125 bool Equals(const SchemaField& other) const;
126
127 int32_t field_id_;
128 std::string name_;
129 std::shared_ptr<Type> type_;
130 bool optional_;
131 std::string doc_;
132 std::shared_ptr<const Literal> initial_default_;
133 std::shared_ptr<const Literal> write_default_;
134};
135
136} // namespace iceberg
Literal is a literal value that is associated with a primitive type.
Definition literal.h:42
A type combined with a name.
Definition schema_field.h:39
SchemaField WithInitialDefault(std::shared_ptr< const Literal > initial_default) const
Return a copy with a new initial-default.
SchemaField WithName(std::string_view name) const
Return a copy with a new name.
const std::shared_ptr< const Literal > & write_default() const
Get the v3 write-default, or null if absent.
std::string ToString() const override
Get a user-readable string representation.
int32_t field_id() const
Get the field ID.
SchemaField WithDoc(std::string_view doc) const
Return a copy with new documentation.
SchemaField WithWriteDefault(std::shared_ptr< const Literal > write_default) const
Return a copy with a new write-default.
SchemaField WithType(std::shared_ptr< Type > type) const
Return a copy with a new type.
std::string_view name() const
Get the field name.
SchemaField(int32_t field_id, std::string_view name, std::shared_ptr< Type > type, bool optional, std::string_view doc={}, std::shared_ptr< const Literal > initial_default=nullptr, std::shared_ptr< const Literal > write_default=nullptr)
Construct a field.
static SchemaField MakeRequired(int32_t field_id, std::string_view name, std::shared_ptr< Type > type, std::string_view doc={})
Construct a required (non-null) field.
const std::shared_ptr< Type > & type() const
Get the field type.
bool optional() const
Get whether the field is optional.
static Result< Literal > CastDefaultValue(const Literal &value, const std::shared_ptr< PrimitiveType > &target_type)
Cast a default literal to a field type.
static SchemaField MakeOptional(int32_t field_id, std::string_view name, std::shared_ptr< Type > type, std::string_view doc={})
Construct an optional (nullable) field.
std::string_view doc() const
Get the field documentation.
const std::shared_ptr< const Literal > & initial_default() const
Get the v3 initial-default, or null if absent.
Interface for objects that can be formatted via std::format.
Definition formattable.h:36
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
std::expected< T, E > Result
Result alias.
Definition result.h:88
Define Result, Status, and error helpers.