paleolimbot commented on code in PR #45375: URL: https://github.com/apache/arrow/pull/45375#discussion_r2027529041
########## cpp/src/parquet/arrow/variant.h: ########## @@ -0,0 +1,80 @@ +// 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 { + +class PARQUET_EXPORT VariantArray : public ::arrow::ExtensionArray { + public: + using ::arrow::ExtensionArray::ExtensionArray; +}; + +/// 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); Review Comment: I imagine this was already discussed so feel free to point me there, but this is a strange place to define an Arrow extension type (i.e., if anybody were looking, I am not sure they would look here). I would personally put this in `test_util.h/cc` (because it is helpful for writing non-trivial tests but hasn't received any discussion from the Arrow end of things). ########## cpp/src/parquet/arrow/schema.cc: ########## @@ -434,6 +454,11 @@ Status FieldToNode(const std::string& name, const std::shared_ptr<Field>& field, type = ParquetType::BYTE_ARRAY; logical_type = LogicalType::JSON(); break; + } else if (ext_type->extension_name() == std::string("parquet.variant")) { + auto variant_type = std::static_pointer_cast<VariantExtensionType>(field->type()); Review Comment: If you move the `VariantExtensionType` to the test utilities, you can still instantiate it using `auto maybe_variant_type = ::arrow::GetExtensionType("parquet.variant"); maybe_variant_type->Deserialize(struct(...), "")`. ########## cpp/src/parquet/arrow/variant.cc: ########## @@ -0,0 +1,127 @@ +// 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; + +VariantExtensionType::VariantExtensionType( + const std::shared_ptr<::arrow::DataType>& storage_type) + : ::arrow::ExtensionType(std::move(storage_type)) { + // GH-45948: Shredded variants will need to handle an optional shredded_value as + // well as value_ becoming optional. + + // IsSupportedStorageType should have been called already, asserting that both + // metadata and value are present. + if (storage_type->field(0)->name() == "metadata") { + metadata_ = storage_type->field(0); + value_ = storage_type->field(1); + } else { + value_ = storage_type->field(0); + metadata_ = storage_type->field(1); + } +} + +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<VariantArray>(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) { + // Ordering of metadata and value fields does not matter, as we will assign + // these to the VariantExtensionType's member shared_ptrs in the constructor. + // Here we just need to check that they are both present. + + const auto& field0 = storage_type->field(0); + const auto& field1 = storage_type->field(1); + + bool metadata_and_value_present = + (field0->name() == "metadata" && field1->name() == "value") || + (field1->name() == "metadata" && field0->name() == "value"); + + if (metadata_and_value_present) { + // Both metadata and value must be non-nullable binary types for unshredded + // variants. This will change in GH-46948, when we will require a Visitor + // to traverse the structure of the variant. + return field0->type()->storage_id() == Type::BINARY && + field1->type()->storage_id() == Type::BINARY && !field0->nullable() && + !field1->nullable(); Review Comment: Is `LARGE_BINARY` worth allowing here (totally fine if that is a battle for another day). ########## cpp/src/parquet/arrow/schema.cc: ########## @@ -1053,6 +1078,19 @@ Result<bool> ApplyOriginalMetadata(const Field& origin_field, SchemaField* infer // inferred_type is arrow::extension::json(arrow::utf8()) auto origin_storage_field = origin_field.WithType(ex_type.storage_type()); + // Apply metadata recursively to storage type + RETURN_NOT_OK(ApplyOriginalStorageMetadata(*origin_storage_field, inferred)); + inferred->field = inferred->field->WithType(origin_type); + } else if (inferred_type->id() == ::arrow::Type::EXTENSION && + ex_type.extension_name() == std::string("parquet.variant")) { Review Comment: I believe there's one other case to handle here (where the inferred type was not an extension type, i.e. Arrow extensions are disabled in Parquet). There are also a series of tests for these that I don't see here, e.g.: https://github.com/apache/arrow/blob/4565b613a488516b9bcbc664ad21d92051ea4fe6/cpp/src/parquet/arrow/arrow_schema_test.cc#L892-L972 -- 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]
