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


##########
cpp/src/parquet/arrow/arrow_schema_test.cc:
##########
@@ -660,6 +660,42 @@ TEST_F(TestConvertParquetSchema, ParquetLists) {
   ASSERT_NO_FATAL_FAILURE(CheckFlatSchema(arrow_schema));
 }
 
+TEST_F(TestConvertParquetSchema, ParquetVariant) {

Review Comment:
   I just noticed that these two new test cases actually have nothing to do 
with variant type. They just happen to have the same physical structure with 
variant type but without the variant logical type. If that's the case, we can 
remove these duplicate cases.



##########
cpp/src/parquet/arrow/variant.cc:
##########
@@ -0,0 +1,94 @@
+// 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 "parquet/arrow/variant.h"
+
+#include <string>
+
+#include "arrow/extension_type.h"
+#include "arrow/result.h"
+#include "arrow/status.h"
+#include "arrow/type_fwd.h"
+#include "arrow/util/logging.h"
+
+namespace parquet::arrow {
+
+using ::arrow::Array;
+using ::arrow::ArrayData;
+using ::arrow::DataType;
+using ::arrow::ExtensionType;
+using ::arrow::Result;
+using ::arrow::Type;
+
+bool VariantExtensionType::ExtensionEquals(const ExtensionType& other) const {
+  return other.extension_name() == this->extension_name() &&
+         other.storage_type()->Equals(this->storage_type_);

Review Comment:
   ```suggestion
            other.storage_type()->Equals(this->storage_type());
   ```
   
   Just to be consistent :)



##########
cpp/src/parquet/arrow/schema.cc:
##########
@@ -114,6 +115,24 @@ Status MapToNode(const std::shared_ptr<::arrow::MapType>& 
type, const std::strin
   return Status::OK();
 }
 
+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) {
+  NodePtr metadata_node;
+  RETURN_NOT_OK(FieldToNode("metadata", type->metadata_field(), properties,
+                            arrow_properties, &metadata_node));
+
+  NodePtr value_node;
+  RETURN_NOT_OK(FieldToNode("value", type->value_field(), properties, 
arrow_properties,
+                            &value_node));
+
+  *out =
+      GroupNode::Make("variant", RepetitionFromNullable(nullable), 
{std::move(metadata_node), std::move(value_node)});

Review Comment:
   The input `field_id` is not used. We should set it to the key-value metadata 
of `out` node.



##########
cpp/src/parquet/arrow/variant.cc:
##########
@@ -0,0 +1,94 @@
+// 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 "parquet/arrow/variant.h"
+
+#include <string>
+
+#include "arrow/extension_type.h"
+#include "arrow/result.h"
+#include "arrow/status.h"
+#include "arrow/type_fwd.h"
+#include "arrow/util/logging.h"
+
+namespace parquet::arrow {
+
+using ::arrow::Array;
+using ::arrow::ArrayData;
+using ::arrow::DataType;
+using ::arrow::ExtensionType;
+using ::arrow::Result;
+using ::arrow::Type;
+
+bool VariantExtensionType::ExtensionEquals(const ExtensionType& other) const {
+  return other.extension_name() == this->extension_name() &&
+         other.storage_type()->Equals(this->storage_type_);
+}
+
+Result<std::shared_ptr<DataType>> VariantExtensionType::Deserialize(
+    std::shared_ptr<DataType> storage_type, const std::string& serialized) 
const {
+  return VariantExtensionType::Make(std::move(storage_type));
+}
+
+std::string VariantExtensionType::Serialize() const { return ""; }
+
+std::shared_ptr<Array> VariantExtensionType::MakeArray(
+    std::shared_ptr<ArrayData> data) const {
+  DCHECK_EQ(data->type->id(), Type::EXTENSION);
+  DCHECK_EQ("parquet.variant",
+            ::arrow::internal::checked_cast<const ExtensionType&>(*data->type)
+                .extension_name());
+  return std::make_shared<::arrow::ExtensionArray>(data);
+}
+
+bool VariantExtensionType::IsSupportedStorageType(
+    const std::shared_ptr<DataType>& storage_type) {
+  // For now we only supported unshredded variants. Unshredded variant storage
+  // type should be a struct with a binary metadata and binary value.
+  //
+  // GH-45948: In shredded variants, the binary value field can be replaced
+  // with one or more of the following: object, array, typed_value, and
+  // variant_value.
+  if (storage_type->id() == Type::STRUCT) {
+    if (storage_type->num_fields() == 2) {
+      const auto& metadata_field = storage_type->field(0);
+      const auto& value_field = storage_type->field(1);
+
+      // Metadata and value both must be non-nullable binary types
+      return metadata_field->type()->storage_id() == Type::BINARY &&
+             value_field->type()->storage_id() == Type::BINARY &&
+             !metadata_field->nullable() && !value_field->nullable();
+    }
+  }
+
+  return false;
+}
+
+Result<std::shared_ptr<DataType>> VariantExtensionType::Make(
+    std::shared_ptr<DataType> storage_type) {
+  if (!IsSupportedStorageType(storage_type)) {
+    return ::arrow::Status::Invalid("Invalid storage type for 
VariantExtensionType: ",
+                                    storage_type->ToString());
+  }
+  return std::make_shared<VariantExtensionType>(std::move(storage_type));
+}
+
+std::shared_ptr<DataType> variant(std::shared_ptr<DataType> storage_type) {
+  return VariantExtensionType::Make(std::move(storage_type)).ValueOrDie();

Review Comment:
   If it can throw, what about removing this function? Or define a 
`std::shared_ptr<DataType> unshredded_variant()`?



##########
cpp/src/parquet/arrow/variant.h:
##########
@@ -0,0 +1,70 @@
+// 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 <stdexcept>
+#include <string>
+
+#include "arrow/extension_type.h"
+#include "parquet/platform.h"
+
+namespace parquet::arrow {
+
+/// EXPERIMENTAL: Variant is not yet fully supported.
+///
+/// Variant supports semi-structured objects that can be composed of
+/// primitives, arrays, and objects, which can be queried by path.
+/// An unshredded variant contains two binaries, one for the metadata and
+/// one for the value.
+///
+/// To read more about variant encoding, see the variant encoding spec at
+/// https://github.com/apache/parquet-format/blob/master/VariantEncoding.md
+class PARQUET_EXPORT VariantExtensionType : public ::arrow::ExtensionType {
+ public:
+  explicit VariantExtensionType(const std::shared_ptr<::arrow::DataType>& 
storage_type)
+      : ::arrow::ExtensionType(std::move(storage_type)) {}
+
+  std::string extension_name() const override { return "parquet.variant"; }
+
+  bool ExtensionEquals(const ::arrow::ExtensionType& other) const override;
+
+  ::arrow::Result<std::shared_ptr<::arrow::DataType>> Deserialize(
+      std::shared_ptr<::arrow::DataType> storage_type,
+      const std::string& serialized_data) const override;
+
+  std::string Serialize() const override;
+
+  std::shared_ptr<::arrow::Array> MakeArray(
+      std::shared_ptr<::arrow::ArrayData> data) const override;
+
+  static ::arrow::Result<std::shared_ptr<::arrow::DataType>> Make(
+      std::shared_ptr<::arrow::DataType> storage_type);
+
+  static bool IsSupportedStorageType(
+      const std::shared_ptr<::arrow::DataType>& storage_type);
+
+  std::shared_ptr<::arrow::Field> metadata_field() const { return 
children_.at(0); }

Review Comment:
   Where is `children_` defined?



##########
cpp/src/parquet/arrow/variant.cc:
##########
@@ -0,0 +1,89 @@
+// 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 "parquet/arrow/variant.h"
+
+#include <string>
+
+#include "arrow/extension_type.h"
+#include "arrow/result.h"
+#include "arrow/status.h"
+#include "arrow/type_fwd.h"
+#include "arrow/util/logging.h"
+
+namespace parquet::arrow {
+
+using ::arrow::Array;
+using ::arrow::ArrayData;
+using ::arrow::DataType;
+using ::arrow::ExtensionType;
+using ::arrow::Result;
+using ::arrow::Type;
+
+bool VariantExtensionType::ExtensionEquals(const ExtensionType& other) const {
+  return other.extension_name() == this->extension_name() &&
+         other.storage_type()->Equals(this->storage_type_);
+}
+
+Result<std::shared_ptr<DataType>> VariantExtensionType::Deserialize(
+    std::shared_ptr<DataType> storage_type, const std::string& serialized) 
const {
+  return VariantExtensionType::Make(std::move(storage_type));
+}
+
+std::string VariantExtensionType::Serialize() const { return ""; }
+
+std::shared_ptr<Array> VariantExtensionType::MakeArray(
+    std::shared_ptr<ArrayData> data) const {

Review Comment:
   I agree that we cannot change the signature of `MakeArray` since it is a 
virtual function from the base class.
   
   > Also, what would be the benefit of adding VariantArray over using Array?
   
   No obvious benefit but let's follow the convention like the json and uuid 
types.



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