2525#include < utility>
2626
2727#include " iceberg/exception.h"
28+ #include " iceberg/schema.h"
2829#include " iceberg/util/formatter.h" // IWYU pragma: keep
2930#include " iceberg/util/macros.h"
3031#include " iceberg/util/string_util.h"
@@ -48,6 +49,7 @@ std::string StructType::ToString() const {
4849 repr += " >" ;
4950 return repr;
5051}
52+
5153std::span<const SchemaField> StructType::fields () const { return fields_; }
5254Result<std::optional<NestedType::SchemaFieldConstRef>> StructType::GetFieldById (
5355 int32_t field_id) const {
@@ -56,13 +58,15 @@ Result<std::optional<NestedType::SchemaFieldConstRef>> StructType::GetFieldById(
5658 if (it == field_by_id.get ().end ()) return std::nullopt ;
5759 return it->second ;
5860}
61+
5962Result<std::optional<NestedType::SchemaFieldConstRef>> StructType::GetFieldByIndex (
6063 int32_t index) const {
6164 if (index < 0 || static_cast <size_t >(index) >= fields_.size ()) {
6265 return InvalidArgument (" Invalid index {} to get field from struct" , index);
6366 }
6467 return fields_[index];
6568}
69+
6670Result<std::optional<NestedType::SchemaFieldConstRef>> StructType::GetFieldByName (
6771 std::string_view name, bool case_sensitive) const {
6872 if (case_sensitive) {
@@ -81,13 +85,19 @@ Result<std::optional<NestedType::SchemaFieldConstRef>> StructType::GetFieldByNam
8185 }
8286 return std::nullopt ;
8387}
88+
89+ std::unique_ptr<Schema> StructType::ToSchema () const {
90+ return std::make_unique<Schema>(fields_);
91+ }
92+
8493bool StructType::Equals (const Type& other) const {
8594 if (other.type_id () != TypeId::kStruct ) {
8695 return false ;
8796 }
8897 const auto & struct_ = static_cast <const StructType&>(other);
8998 return fields_ == struct_.fields_ ;
9099}
100+
91101Result<std::unordered_map<int32_t , StructType::SchemaFieldConstRef>>
92102StructType::InitFieldById (const StructType& self) {
93103 std::unordered_map<int32_t , SchemaFieldConstRef> field_by_id;
@@ -100,6 +110,7 @@ StructType::InitFieldById(const StructType& self) {
100110 }
101111 return field_by_id;
102112}
113+
103114Result<std::unordered_map<std::string_view, StructType::SchemaFieldConstRef>>
104115StructType::InitFieldByName (const StructType& self) {
105116 std::unordered_map<std::string_view, StructType::SchemaFieldConstRef> field_by_name;
@@ -113,6 +124,7 @@ StructType::InitFieldByName(const StructType& self) {
113124 }
114125 return field_by_name;
115126}
127+
116128Result<std::unordered_map<std::string, StructType::SchemaFieldConstRef>>
117129StructType::InitFieldByLowerCaseName (const StructType& self) {
118130 std::unordered_map<std::string, SchemaFieldConstRef> field_by_lowercase_name;
@@ -146,6 +158,7 @@ std::string ListType::ToString() const {
146158 repr += " >" ;
147159 return repr;
148160}
161+
149162std::span<const SchemaField> ListType::fields () const { return {&element_, 1 }; }
150163Result<std::optional<NestedType::SchemaFieldConstRef>> ListType::GetFieldById (
151164 int32_t field_id) const {
@@ -154,13 +167,15 @@ Result<std::optional<NestedType::SchemaFieldConstRef>> ListType::GetFieldById(
154167 }
155168 return std::nullopt ;
156169}
170+
157171Result<std::optional<NestedType::SchemaFieldConstRef>> ListType::GetFieldByIndex (
158172 int index) const {
159173 if (index == 0 ) {
160174 return std::cref (element_);
161175 }
162176 return InvalidArgument (" Invalid index {} to get field from list" , index);
163177}
178+
164179Result<std::optional<NestedType::SchemaFieldConstRef>> ListType::GetFieldByName (
165180 std::string_view name, bool case_sensitive) const {
166181 if (case_sensitive) {
@@ -174,6 +189,7 @@ Result<std::optional<NestedType::SchemaFieldConstRef>> ListType::GetFieldByName(
174189 }
175190 return std::nullopt ;
176191}
192+
177193bool ListType::Equals (const Type& other) const {
178194 if (other.type_id () != TypeId::kList ) {
179195 return false ;
@@ -195,6 +211,7 @@ MapType::MapType(SchemaField key, SchemaField value)
195211const SchemaField& MapType::key () const { return fields_[0 ]; }
196212const SchemaField& MapType::value () const { return fields_[1 ]; }
197213TypeId MapType::type_id () const { return kTypeId ; }
214+
198215std::string MapType::ToString () const {
199216 // XXX: work around Clang/libc++: "<{}>" in a format string appears to get
200217 // parsed as {<>} or something; split up the format string to avoid that
@@ -204,6 +221,7 @@ std::string MapType::ToString() const {
204221 repr += " >" ;
205222 return repr;
206223}
224+
207225std::span<const SchemaField> MapType::fields () const { return fields_; }
208226Result<std::optional<NestedType::SchemaFieldConstRef>> MapType::GetFieldById (
209227 int32_t field_id) const {
@@ -214,6 +232,7 @@ Result<std::optional<NestedType::SchemaFieldConstRef>> MapType::GetFieldById(
214232 }
215233 return std::nullopt ;
216234}
235+
217236Result<std::optional<NestedType::SchemaFieldConstRef>> MapType::GetFieldByIndex (
218237 int32_t index) const {
219238 if (index == 0 ) {
@@ -223,6 +242,7 @@ Result<std::optional<NestedType::SchemaFieldConstRef>> MapType::GetFieldByIndex(
223242 }
224243 return InvalidArgument (" Invalid index {} to get field from map" , index);
225244}
245+
226246Result<std::optional<NestedType::SchemaFieldConstRef>> MapType::GetFieldByName (
227247 std::string_view name, bool case_sensitive) const {
228248 if (case_sensitive) {
@@ -241,6 +261,7 @@ Result<std::optional<NestedType::SchemaFieldConstRef>> MapType::GetFieldByName(
241261 }
242262 return std::nullopt ;
243263}
264+
244265bool MapType::Equals (const Type& other) const {
245266 if (other.type_id () != TypeId::kMap ) {
246267 return false ;
0 commit comments