mapleFU commented on code in PR #45375: URL: https://github.com/apache/arrow/pull/45375#discussion_r2013572559
########## 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 { + 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. + // + // TODO(neilechao) 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) { + return storage_type->field(0)->type()->storage_id() == Type::BINARY && Review Comment: I remember there is also a nullable check for variant type: https://github.com/apache/parquet-format/blob/master/VariantEncoding.md#variant-in-parquet . Should we also check that? ########## cpp/src/parquet/arrow/variant.h: ########## @@ -0,0 +1,65 @@ +// 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. +class PARQUET_EXPORT VariantExtensionType : public ::arrow::ExtensionType { + public: + explicit VariantExtensionType(const std::shared_ptr<::arrow::DataType>& storage_type) + : ::arrow::ExtensionType(storage_type), storage_type_(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); } + + std::shared_ptr<::arrow::Field> value_field() const { return children_.at(1); } + + private: + std::shared_ptr<::arrow::DataType> storage_type_; Review Comment: Didn't understand why this `storage_type_` is needed? ########## 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), {metadata_node, value_node}); Review Comment: ```suggestion GroupNode::Make("variant", RepetitionFromNullable(nullable), {std::move(metadata_node), std::move(value_node)}); ``` ########## 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) { + std::vector<NodePtr> parquet_fields; + std::vector<std::shared_ptr<Field>> arrow_fields; + + // Unshredded variant + // optional group variant_col { + // required binary metadata; + // required binary value; + // } + + // TODO(neilechao) add shredded variants Review Comment: ditto ########## 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: Would const reference better for this? Or `return std::make_shared<::arrow::ExtensionArray>(std::move(data));`? Should `VariantArray` also be added? ########## cpp/src/parquet/arrow/arrow_schema_test.cc: ########## @@ -1299,6 +1335,40 @@ TEST_F(TestConvertArrowSchema, ParquetMaps) { ASSERT_NO_FATAL_FAILURE(CheckFlatSchema(parquet_fields)); } +TEST_F(TestConvertArrowSchema, ParquetVariant) { + std::vector<NodePtr> parquet_fields; + std::vector<std::shared_ptr<Field>> arrow_fields; + + // Unshredded variant + // optional group variant_col { + // required binary metadata; + // required binary value; + // } + + // TODO(neilechao) add shredded variants Review Comment: GH-Issue? ########## 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 { + 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. + // + // TODO(neilechao) In shredded variants, the binary value field can be replaced Review Comment: Could we create a github issue for this? -- 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]
