pitrou commented on a change in pull request #8366:
URL: https://github.com/apache/arrow/pull/8366#discussion_r500568316
##########
File path: cpp/src/parquet/arrow/schema.cc
##########
@@ -689,10 +686,73 @@ Status GetOriginSchema(const std::shared_ptr<const
KeyValueMetadata>& metadata,
// but that is not necessarily present in the field reconstitued from Parquet
data
// (for example, Parquet timestamp types doesn't carry timezone information).
-Status ApplyOriginalStorageMetadata(const Field& origin_field, SchemaField*
inferred) {
+Result<bool> ApplyOriginalMetadata(const Field& origin_field, SchemaField*
inferred);
+
+using NestedFieldRewrapper = std::function<Status(FieldVector, SchemaField*)>;
+
+Status RewrapStructField(FieldVector new_children, SchemaField* inferred) {
+ inferred->field =
inferred->field->WithType(::arrow::struct_(std::move(new_children)));
+ return Status::OK();
+}
+
+Status RewrapListField(FieldVector new_children, SchemaField* inferred) {
+ DCHECK_EQ(new_children.size(), 1);
+ inferred->field = inferred->field->WithType(::arrow::list(new_children[0]));
+ return Status::OK();
+}
+
+// XXX Perhaps add a DataType::WithFields method?
+NestedFieldRewrapper GetNestedFieldRewrapper(const ArrowType& origin_type,
+ const ArrowType& inferred_type) {
+ switch (inferred_type.id()) {
+ case ::arrow::Type::STRUCT:
+ if (origin_type.id() == ::arrow::Type::STRUCT) {
+ return RewrapStructField;
+ }
+ break;
+ case ::arrow::Type::LIST:
+ // TODO also allow LARGE_LIST and FIXED_SIZE_LIST
+ if (origin_type.id() == ::arrow::Type::LIST) {
+ return RewrapListField;
+ }
+ break;
+ default:
+ break;
+ }
+ return {};
+}
+
+Result<bool> ApplyOriginalStorageMetadata(const Field& origin_field,
+ SchemaField* inferred) {
+ bool modified = false;
+
auto origin_type = origin_field.type();
auto inferred_type = inferred->field->type();
+ const int num_children = inferred_type->num_fields();
+
+ if (num_children > 0 && origin_type->num_fields() == num_children) {
+ DCHECK_EQ(static_cast<int>(inferred->children.size()), num_children);
+ const auto nested_rewrapper = GetNestedFieldRewrapper(*origin_type,
*inferred_type);
+ if (nested_rewrapper) {
+ // Apply original metadata recursively to children
+ for (int i = 0; i < inferred_type->num_fields(); ++i) {
+ ARROW_ASSIGN_OR_RAISE(
+ const bool child_modified,
+ ApplyOriginalMetadata(*origin_type->field(i),
&inferred->children[i]));
+ modified |= child_modified;
+ }
+ if (modified) {
+ // Recreate this field using the modified child fields
+ ::arrow::FieldVector modified_children(inferred_type->num_fields());
+ for (int i = 0; i < inferred_type->num_fields(); ++i) {
+ modified_children[i] = inferred->children[i].field;
+ }
+ RETURN_NOT_OK(nested_rewrapper(std::move(modified_children),
inferred));
+ }
+ }
Review comment:
Do you want to try and make this change?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]