-
Notifications
You must be signed in to change notification settings - Fork 70
feat: convert iceberg schema to arrow schema #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #include "iceberg/schema_internal.h" | ||
|
|
||
| #include <format> | ||
| #include <optional> | ||
| #include <string> | ||
|
|
||
| #include "iceberg/expected.h" | ||
| #include "iceberg/schema.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| namespace { | ||
|
|
||
| constexpr const char* kArrowExtensionName = "ARROW:extension:name"; | ||
| constexpr const char* kArrowExtensionMetadata = "ARROW:extension:metadata"; | ||
|
|
||
| // Convert an Iceberg type to Arrow schema. Return value is Nanoarrow error code. | ||
| ArrowErrorCode ConvertToArrowSchema(const Type& type, ArrowSchema* schema, bool optional, | ||
| std::string_view name = "", | ||
| std::optional<int32_t> field_id = std::nullopt) { | ||
| ArrowBuffer metadata_buffer; | ||
| NANOARROW_RETURN_NOT_OK(ArrowMetadataBuilderInit(&metadata_buffer, nullptr)); | ||
| if (field_id.has_value()) { | ||
| NANOARROW_RETURN_NOT_OK(ArrowMetadataBuilderAppend( | ||
| &metadata_buffer, ArrowCharView(std::string(kFieldIdKey).c_str()), | ||
| ArrowCharView(std::to_string(field_id.value()).c_str()))); | ||
| } | ||
|
|
||
| switch (type.type_id()) { | ||
| case TypeId::kStruct: { | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaInitFromType(schema, NANOARROW_TYPE_STRUCT)); | ||
|
|
||
| const auto& struct_type = static_cast<const StructType&>(type); | ||
| const auto& fields = struct_type.fields(); | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaAllocateChildren(schema, fields.size())); | ||
|
|
||
| for (size_t i = 0; i < fields.size(); i++) { | ||
| const auto& field = fields[i]; | ||
| NANOARROW_RETURN_NOT_OK(ConvertToArrowSchema(*field.type(), schema->children[i], | ||
| field.optional(), field.name(), | ||
| field.field_id())); | ||
| } | ||
| } break; | ||
| case TypeId::kList: { | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaInitFromType(schema, NANOARROW_TYPE_LIST)); | ||
|
|
||
| const auto& list_type = static_cast<const ListType&>(type); | ||
| const auto& elem_field = list_type.fields()[0]; | ||
| NANOARROW_RETURN_NOT_OK(ConvertToArrowSchema( | ||
| *elem_field.type(), schema->children[0], elem_field.optional(), | ||
| elem_field.name(), elem_field.field_id())); | ||
| } break; | ||
| case TypeId::kMap: { | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaInitFromType(schema, NANOARROW_TYPE_MAP)); | ||
|
|
||
| const auto& map_type = static_cast<const MapType&>(type); | ||
| const auto& key_field = map_type.key(); | ||
| const auto& value_field = map_type.value(); | ||
| NANOARROW_RETURN_NOT_OK(ConvertToArrowSchema( | ||
| *key_field.type(), schema->children[0]->children[0], key_field.optional(), | ||
| key_field.name(), key_field.field_id())); | ||
| NANOARROW_RETURN_NOT_OK(ConvertToArrowSchema( | ||
| *value_field.type(), schema->children[0]->children[1], value_field.optional(), | ||
| value_field.name(), value_field.field_id())); | ||
| } break; | ||
| case TypeId::kBoolean: | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaInitFromType(schema, NANOARROW_TYPE_BOOL)); | ||
| break; | ||
| case TypeId::kInt: | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaInitFromType(schema, NANOARROW_TYPE_INT32)); | ||
| break; | ||
| case TypeId::kLong: | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaInitFromType(schema, NANOARROW_TYPE_INT64)); | ||
| break; | ||
| case TypeId::kFloat: | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaInitFromType(schema, NANOARROW_TYPE_FLOAT)); | ||
| break; | ||
| case TypeId::kDouble: | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaInitFromType(schema, NANOARROW_TYPE_DOUBLE)); | ||
| break; | ||
| case TypeId::kDecimal: { | ||
| ArrowSchemaInit(schema); | ||
| const auto& decimal_type = static_cast<const DecimalType&>(type); | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaSetTypeDecimal(schema, NANOARROW_TYPE_DECIMAL128, | ||
| decimal_type.precision(), | ||
| decimal_type.scale())); | ||
| } break; | ||
| case TypeId::kDate: | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaInitFromType(schema, NANOARROW_TYPE_DATE32)); | ||
| break; | ||
| case TypeId::kTime: { | ||
| ArrowSchemaInit(schema); | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaSetTypeDateTime(schema, NANOARROW_TYPE_TIME64, | ||
| NANOARROW_TIME_UNIT_MICRO, | ||
| /*timezone=*/nullptr)); | ||
| } break; | ||
| case TypeId::kTimestamp: { | ||
| ArrowSchemaInit(schema); | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaSetTypeDateTime(schema, NANOARROW_TYPE_TIMESTAMP, | ||
| NANOARROW_TIME_UNIT_MICRO, | ||
| /*timezone=*/nullptr)); | ||
| } break; | ||
| case TypeId::kTimestampTz: { | ||
| ArrowSchemaInit(schema); | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaSetTypeDateTime( | ||
| schema, NANOARROW_TYPE_TIMESTAMP, NANOARROW_TIME_UNIT_MICRO, "UTC")); | ||
| } break; | ||
| case TypeId::kString: | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaInitFromType(schema, NANOARROW_TYPE_STRING)); | ||
| break; | ||
| case TypeId::kBinary: | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaInitFromType(schema, NANOARROW_TYPE_BINARY)); | ||
| break; | ||
| case TypeId::kFixed: { | ||
| ArrowSchemaInit(schema); | ||
| const auto& fixed_type = static_cast<const FixedType&>(type); | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaSetTypeFixedSize( | ||
| schema, NANOARROW_TYPE_FIXED_SIZE_BINARY, fixed_type.length())); | ||
| } break; | ||
| case TypeId::kUuid: { | ||
| ArrowSchemaInit(schema); | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaSetTypeFixedSize( | ||
| schema, NANOARROW_TYPE_FIXED_SIZE_BINARY, /*fixed_size=*/16)); | ||
| NANOARROW_RETURN_NOT_OK( | ||
| ArrowMetadataBuilderAppend(&metadata_buffer, ArrowCharView(kArrowExtensionName), | ||
| ArrowCharView("arrow.uuid"))); | ||
| } break; | ||
| } | ||
|
|
||
| if (!name.empty()) { | ||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaSetName(schema, std::string(name).c_str())); | ||
| } | ||
|
|
||
| NANOARROW_RETURN_NOT_OK(ArrowSchemaSetMetadata( | ||
| schema, reinterpret_cast<const char*>(metadata_buffer.data))); | ||
| ArrowBufferReset(&metadata_buffer); | ||
|
|
||
| if (optional) { | ||
| schema->flags |= ARROW_FLAG_NULLABLE; | ||
| } else { | ||
| schema->flags &= ~ARROW_FLAG_NULLABLE; | ||
| } | ||
|
|
||
| return NANOARROW_OK; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| expected<void, Error> ToArrowSchema(const Schema& schema, ArrowSchema* out) { | ||
| if (out == nullptr) [[unlikely]] { | ||
| return unexpected<Error>{{.kind = ErrorKind::kInvalidArgument, | ||
| .message = "Output Arrow schema cannot be null"}}; | ||
| } | ||
|
|
||
| if (ArrowErrorCode errorCode = ConvertToArrowSchema(schema, out, ""); | ||
| errorCode != NANOARROW_OK) { | ||
| return unexpected<Error>{ | ||
| {.kind = ErrorKind::kInvalidSchema, | ||
| .message = std::format( | ||
| "Failed to convert Iceberg schema to Arrow schema, error code: {}", | ||
| errorCode)}}; | ||
| } | ||
|
|
||
| return {}; | ||
| } | ||
|
|
||
| expected<std::unique_ptr<Schema>, Error> FromArrowSchema(const ArrowSchema& schema, | ||
| int32_t schema_id) { | ||
| // TODO(wgtmac): Implement this | ||
| return unexpected<Error>{ | ||
| {.kind = ErrorKind::kInvalidSchema, .message = "Not implemented yet"}}; | ||
| } | ||
|
|
||
| } // namespace iceberg | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include <nanoarrow/nanoarrow.h> | ||
|
|
||
| #include "iceberg/error.h" | ||
| #include "iceberg/expected.h" | ||
| #include "iceberg/type_fwd.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| // Apache Arrow C++ uses "PARQUET:field_id" to store field IDs for Parquet. | ||
| // Here we follow a similar convention for Iceberg but we might also add | ||
| // "PARQUET:field_id" in the future once we implement a Parquet writer. | ||
| constexpr std::string_view kFieldIdKey = "ICEBERG:field_id"; | ||
|
|
||
| /// \brief Convert an Iceberg schema to an Arrow schema. | ||
| /// | ||
| /// \param[in] schema The Iceberg schema to convert. | ||
| /// \param[out] out The Arrow schema to convert to. | ||
| /// \return An error if the conversion fails. | ||
| expected<void, Error> ToArrowSchema(const Schema& schema, ArrowSchema* out); | ||
|
|
||
| /// \brief Convert an Arrow schema to an Iceberg schema. | ||
| /// | ||
| /// \param[in] schema The Arrow schema to convert. | ||
| /// \param[in] schema_id The schema ID of the Iceberg schema. | ||
| /// \return The Iceberg schema or an error if the conversion fails. | ||
| expected<std::unique_ptr<Schema>, Error> FromArrowSchema(const ArrowSchema& schema, | ||
| int32_t schema_id); | ||
|
|
||
| } // namespace iceberg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ConvertToArrowSchemaandToArrowSchemaare a bit confusing to me. Do we need to make the API naming more clear or split them in different namespaces?By the way, do we have a policy for our API args' order?
input_field_a, out, input_field_b, input_field_cseems not good to me and hard to follow.Is it widely adopted in cpp community?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. Actually they are in different namespaces (the former one is in the anonymous namespace). I have renamed them to
ToArrowSchemato be consistent.W.r.t. the order of arguments, those with default values must be placed at the end. Since they are all internal functions, I have just removed any default argument and put the out param to the end.