zhjwpku commented on code in PR #57:
URL: https://github.com/apache/iceberg-cpp/pull/57#discussion_r2022613224
##########
src/iceberg/schema_internal.cc:
##########
@@ -183,11 +186,171 @@ expected<void, Error> ToArrowSchema(const Schema&
schema, ArrowSchema* out) {
return {};
}
+namespace {
+
+int32_t GetFieldId(const ArrowSchema& schema) {
+ if (schema.metadata == nullptr) {
+ return kUnknownFieldId;
+ }
+
+ ArrowStringView field_id_key{.data = kFieldIdKey.data(),
+ .size_bytes = kFieldIdKey.size()};
+ ArrowStringView field_id_value;
+ if (ArrowMetadataGetValue(schema.metadata, field_id_key, &field_id_value) !=
+ NANOARROW_OK) {
+ return kUnknownFieldId;
+ }
+
+ return std::stoi(std::string(field_id_value.data,
field_id_value.size_bytes));
+}
+
+expected<std::shared_ptr<Type>, Error> FromArrowSchema(const ArrowSchema&
schema) {
+ auto to_schema_field =
+ [](const ArrowSchema& schema) -> expected<std::unique_ptr<SchemaField>,
Error> {
+ auto field_type_result = FromArrowSchema(schema);
+ if (!field_type_result) {
+ return unexpected<Error>(field_type_result.error());
+ }
+
+ auto field_id = GetFieldId(schema);
+ bool is_optional = (schema.flags & ARROW_FLAG_NULLABLE) != 0;
+ return std::make_unique<SchemaField>(
+ field_id, schema.name, std::move(field_type_result.value()),
is_optional);
+ };
+
+ ArrowError arrow_error;
+ ArrowErrorInit(&arrow_error);
+
+ ArrowSchemaView schema_view;
+ if (auto error_code = ArrowSchemaViewInit(&schema_view, &schema,
&arrow_error);
+ error_code != NANOARROW_OK) {
+ return unexpected<Error>{
+ {.kind = ErrorKind::kInvalidSchema,
+ .message = std::format("Failed to read Arrow schema, code: {},
message: {}",
+ error_code, arrow_error.message)}};
+ }
+
+ switch (schema_view.type) {
+ case NANOARROW_TYPE_STRUCT: {
+ std::vector<SchemaField> fields;
+ fields.reserve(schema.n_children);
+
+ for (int i = 0; i < schema.n_children; i++) {
+ auto field_result = to_schema_field(*schema.children[i]);
+ if (!field_result) {
+ return unexpected<Error>(field_result.error());
+ }
+ fields.emplace_back(std::move(*field_result.value()));
+ }
+
+ return std::make_shared<StructType>(std::move(fields));
+ } break;
Review Comment:
Do we need this `break` here?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]