wgtmac commented on code in PR #50252:
URL: https://github.com/apache/arrow/pull/50252#discussion_r4409877285


##########
cpp/src/parquet/arrow/schema.cc:
##########
@@ -131,21 +369,40 @@ Status MapToNode(const std::shared_ptr<::arrow::MapType>& 
type, const std::strin
   return Status::OK();
 }
 
-Status VariantToNode(
-    const std::shared_ptr<::arrow::extension::VariantExtensionType>& type,
-    const std::string& name, bool nullable, int field_id,
-    const WriterProperties& properties, const ArrowWriterProperties& 
arrow_properties,
-    NodePtr* out) {
-  NodePtr metadata_node;
-  RETURN_NOT_OK(FieldToNode("metadata", type->metadata(), properties, 
arrow_properties,
-                            &metadata_node));
+Status VariantToNode(const std::shared_ptr<VariantExtensionType>& type,
+                     const std::string& name, bool nullable, int field_id,
+                     const WriterProperties& properties,
+                     const ArrowWriterProperties& arrow_properties, NodePtr* 
out) {
+  // `VariantExtensionType` already validates the general storage shape. 
Writing is
+  // stricter than reading: we keep accepting typed-only Variant schemas on 
read,
+  // but must not write them.
+  if (type->value() == nullptr) {
+    return Status::Invalid("Invalid Variant write schema: missing top-level 
value field");
+  }
 
-  NodePtr value_node;
-  RETURN_NOT_OK(
-      FieldToNode("value", type->value(), properties, arrow_properties, 
&value_node));
+  std::vector<NodePtr> children;
+  children.reserve(type->storage_type()->num_fields());
+
+  auto AddChild = [&](const std::shared_ptr<::arrow::Field>& field) {
+    if (field == nullptr) {
+      return Status::OK();
+    }
+    NodePtr child;
+    if (field->name() == "typed_value") {
+      RETURN_NOT_OK(VariantTypedValueToNode(field, properties, 
arrow_properties, &child));
+    } else {
+      RETURN_NOT_OK(
+          FieldToNode(field->name(), field, properties, arrow_properties, 
&child));
+    }
+    children.emplace_back(std::move(child));
+    return Status::OK();
+  };
+
+  RETURN_NOT_OK(AddChild(type->metadata()));
+  RETURN_NOT_OK(AddChild(type->value()));
+  RETURN_NOT_OK(AddChild(type->typed_value()));

Review Comment:
   These calls reorder storage fields, but the writer traverses the Arrow array 
in its original order. A supported typed_value-first layout can therefore write 
to the wrong Parquet leaf.



-- 
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]

Reply via email to