2020#include " iceberg/schema_internal.h"
2121
2222#include < cstring>
23- #include < format>
2423#include < optional>
2524#include < string>
2625
2726#include " iceberg/schema.h"
2827#include " iceberg/type.h"
28+ #include " iceberg/util/macros.h"
2929
3030namespace iceberg {
3131
@@ -170,18 +170,14 @@ ArrowErrorCode ToArrowSchema(const Type& type, bool optional, std::string_view n
170170
171171Status ToArrowSchema (const Schema& schema, ArrowSchema* out) {
172172 if (out == nullptr ) [[unlikely]] {
173- return unexpected<Error>{{.kind = ErrorKind::kInvalidArgument ,
174- .message = " Output Arrow schema cannot be null" }};
173+ return InvalidArgument (" Output Arrow schema cannot be null" );
175174 }
176175
177176 if (ArrowErrorCode errorCode = ToArrowSchema (schema, /* optional=*/ false , /* name=*/ " " ,
178177 /* field_id=*/ std::nullopt , out);
179178 errorCode != NANOARROW_OK) {
180- return unexpected<Error>{
181- {.kind = ErrorKind::kInvalidSchema ,
182- .message = std::format (
183- " Failed to convert Iceberg schema to Arrow schema, error code: {}" ,
184- errorCode)}};
179+ return InvalidSchema (
180+ " Failed to convert Iceberg schema to Arrow schema, error code: {}" , errorCode);
185181 }
186182
187183 return {};
@@ -208,15 +204,12 @@ int32_t GetFieldId(const ArrowSchema& schema) {
208204Result<std::shared_ptr<Type>> FromArrowSchema (const ArrowSchema& schema) {
209205 auto to_schema_field =
210206 [](const ArrowSchema& schema) -> Result<std::unique_ptr<SchemaField>> {
211- auto field_type_result = FromArrowSchema (schema);
212- if (!field_type_result) {
213- return unexpected<Error>(field_type_result.error ());
214- }
207+ ICEBERG_ASSIGN_OR_RAISE (auto field_type, FromArrowSchema (schema));
215208
216209 auto field_id = GetFieldId (schema);
217210 bool is_optional = (schema.flags & ARROW_FLAG_NULLABLE) != 0 ;
218- return std::make_unique<SchemaField>(
219- field_id, schema. name , std::move (field_type_result. value ()), is_optional);
211+ return std::make_unique<SchemaField>(field_id, schema. name , std::move (field_type),
212+ is_optional);
220213 };
221214
222215 ArrowError arrow_error;
@@ -225,10 +218,8 @@ Result<std::shared_ptr<Type>> FromArrowSchema(const ArrowSchema& schema) {
225218 ArrowSchemaView schema_view;
226219 if (auto error_code = ArrowSchemaViewInit (&schema_view, &schema, &arrow_error);
227220 error_code != NANOARROW_OK) {
228- return unexpected<Error>{
229- {.kind = ErrorKind::kInvalidSchema ,
230- .message = std::format (" Failed to read Arrow schema, code: {}, message: {}" ,
231- error_code, arrow_error.message )}};
221+ return InvalidSchema (" Failed to read Arrow schema, code: {}, message: {}" , error_code,
222+ arrow_error.message );
232223 }
233224
234225 switch (schema_view.type ) {
@@ -237,35 +228,24 @@ Result<std::shared_ptr<Type>> FromArrowSchema(const ArrowSchema& schema) {
237228 fields.reserve (schema.n_children );
238229
239230 for (int i = 0 ; i < schema.n_children ; i++) {
240- auto field_result = to_schema_field (*schema.children [i]);
241- if (!field_result) {
242- return unexpected<Error>(field_result.error ());
243- }
244- fields.emplace_back (std::move (*field_result.value ()));
231+ ICEBERG_ASSIGN_OR_RAISE (auto field, to_schema_field (*schema.children [i]));
232+ fields.emplace_back (std::move (*field));
245233 }
246234
247235 return std::make_shared<StructType>(std::move (fields));
248236 }
249237 case NANOARROW_TYPE_LIST: {
250- auto element_field_result = to_schema_field (*schema.children [0 ]);
251- if (!element_field_result) {
252- return unexpected<Error>(element_field_result.error ());
253- }
254- return std::make_shared<ListType>(std::move (*element_field_result.value ()));
238+ ICEBERG_ASSIGN_OR_RAISE (auto element_field_result,
239+ to_schema_field (*schema.children [0 ]));
240+ return std::make_shared<ListType>(std::move (*element_field_result));
255241 }
256242 case NANOARROW_TYPE_MAP: {
257- auto key_field_result = to_schema_field (*schema. children [ 0 ]-> children [ 0 ]);
258- if (!key_field_result) {
259- return unexpected<Error>(key_field_result. error ());
260- }
243+ ICEBERG_ASSIGN_OR_RAISE ( auto key_field,
244+ to_schema_field (*schema. children [ 0 ]-> children [ 0 ]));
245+ ICEBERG_ASSIGN_OR_RAISE ( auto value_field,
246+ to_schema_field (*schema. children [ 0 ]-> children [ 1 ]));
261247
262- auto value_field_result = to_schema_field (*schema.children [0 ]->children [1 ]);
263- if (!value_field_result) {
264- return unexpected<Error>(value_field_result.error ());
265- }
266-
267- return std::make_shared<MapType>(std::move (*key_field_result.value ()),
268- std::move (*value_field_result.value ()));
248+ return std::make_shared<MapType>(std::move (*key_field), std::move (*value_field));
269249 }
270250 case NANOARROW_TYPE_BOOL:
271251 return std::make_shared<BooleanType>();
@@ -284,20 +264,16 @@ Result<std::shared_ptr<Type>> FromArrowSchema(const ArrowSchema& schema) {
284264 return std::make_shared<DateType>();
285265 case NANOARROW_TYPE_TIME64:
286266 if (schema_view.time_unit != NANOARROW_TIME_UNIT_MICRO) {
287- return unexpected<Error>{
288- {.kind = ErrorKind::kInvalidSchema ,
289- .message = std::format (" Unsupported time unit for Arrow time type: {}" ,
290- static_cast <int >(schema_view.time_unit ))}};
267+ return InvalidSchema (" Unsupported time unit for Arrow time type: {}" ,
268+ static_cast <int >(schema_view.time_unit ));
291269 }
292270 return std::make_shared<TimeType>();
293271 case NANOARROW_TYPE_TIMESTAMP: {
294272 bool with_timezone =
295273 schema_view.timezone != nullptr && std::strlen (schema_view.timezone ) > 0 ;
296274 if (schema_view.time_unit != NANOARROW_TIME_UNIT_MICRO) {
297- return unexpected<Error>{
298- {.kind = ErrorKind::kInvalidSchema ,
299- .message = std::format (" Unsupported time unit for Arrow timestamp type: {}" ,
300- static_cast <int >(schema_view.time_unit ))}};
275+ return InvalidSchema (" Unsupported time unit for Arrow timestamp type: {}" ,
276+ static_cast <int >(schema_view.time_unit ));
301277 }
302278 if (with_timezone) {
303279 return std::make_shared<TimestampTzType>();
@@ -314,18 +290,15 @@ Result<std::shared_ptr<Type>> FromArrowSchema(const ArrowSchema& schema) {
314290 schema_view.extension_name .size_bytes );
315291 extension_name == kArrowUuidExtensionName ) {
316292 if (schema_view.fixed_size != 16 ) {
317- return unexpected<Error>{{.kind = ErrorKind::kInvalidSchema ,
318- .message = " UUID type must have a fixed size of 16" }};
293+ return InvalidSchema (" UUID type must have a fixed size of 16" );
319294 }
320295 return std::make_shared<UuidType>();
321296 }
322297 return std::make_shared<FixedType>(schema_view.fixed_size );
323298 }
324299 default :
325- return unexpected<Error>{
326- {.kind = ErrorKind::kInvalidSchema ,
327- .message = std::format (" Unsupported Arrow type: {}" ,
328- ArrowTypeString (schema_view.type ))}};
300+ return InvalidSchema (" Unsupported Arrow type: {}" ,
301+ ArrowTypeString (schema_view.type ));
329302 }
330303}
331304
@@ -343,16 +316,10 @@ std::unique_ptr<Schema> FromStructType(StructType&& struct_type,
343316
344317Result<std::unique_ptr<Schema>> FromArrowSchema (const ArrowSchema& schema,
345318 std::optional<int32_t > schema_id) {
346- auto type_result = FromArrowSchema (schema);
347- if (!type_result) {
348- return unexpected<Error>(type_result.error ());
349- }
319+ ICEBERG_ASSIGN_OR_RAISE (auto type, FromArrowSchema (schema));
350320
351- auto & type = type_result.value ();
352321 if (type->type_id () != TypeId::kStruct ) {
353- return unexpected<Error>{
354- {.kind = ErrorKind::kInvalidSchema ,
355- .message = " Arrow schema must be a struct type for Iceberg schema" }};
322+ return InvalidSchema (" Arrow schema must be a struct type for Iceberg schema" );
356323 }
357324
358325 auto & struct_type = static_cast <StructType&>(*type);
0 commit comments