You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/10/06 19:53:51 UTC

[GitHub] [arrow] bkietz commented on a change in pull request #8366: ARROW-9943: [C++] Recursively apply Arrow metadata when reading from Parquet

bkietz commented on a change in pull request #8366:
URL: https://github.com/apache/arrow/pull/8366#discussion_r500552098



##########
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 {};
+}

Review comment:
       IMHO it's more natural to provide a factory for data types:
   ```suggestion
   std::function<std::shared_ptr<DataType>(FieldVector)> GetNestedFactory(
       const ArrowType& origin_type, const ArrowType& inferred_type) {
     switch (inferred_type.id()) {
       case ::arrow::Type::STRUCT:
         if (origin_type.id() == ::arrow::Type::STRUCT) {
           return ::arrow::struct_;
         }
         break;
       case ::arrow::Type::LIST:
         // TODO also allow LARGE_LIST and FIXED_SIZE_LIST
         if (origin_type.id() == ::arrow::Type::LIST) {
           return [](FieldVector fields) { return ::arrow::list(std::move(fields[0])); };
         }
         break;
       default:
         break;
     }
     return {};
   }
   ```

##########
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:
       ```suggestion
       const auto nested_factory = GetNestedFactory(*origin_type, *inferred_type);
       if (nested_factory) {
         // 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;
           }
           inferred.field = inferred.field->WithType(nested_factory(std::move(modified_children)));
         }
       }
   ```




----------------------------------------------------------------
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:
users@infra.apache.org