|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "arrow/extension/opaque.h" |
| 19 | + |
| 20 | +#include <sstream> |
| 21 | + |
| 22 | +#include "arrow/json/rapidjson_defs.h" // IWYU pragma: keep |
| 23 | +#include "arrow/util/logging.h" |
| 24 | + |
| 25 | +#include <rapidjson/document.h> |
| 26 | +#include <rapidjson/error/en.h> |
| 27 | +#include <rapidjson/writer.h> |
| 28 | + |
| 29 | +namespace arrow::extension { |
| 30 | + |
| 31 | +std::string OpaqueType::ToString(bool show_metadata) const { |
| 32 | + std::stringstream ss; |
| 33 | + ss << "extension<" << this->extension_name() |
| 34 | + << "[storage_type=" << storage_type_->ToString(show_metadata) |
| 35 | + << ", type_name=" << type_name_ << ", vendor_name=" << vendor_name_ << "]>"; |
| 36 | + return ss.str(); |
| 37 | +} |
| 38 | + |
| 39 | +bool OpaqueType::ExtensionEquals(const ExtensionType& other) const { |
| 40 | + if (extension_name() != other.extension_name()) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + const auto& opaque = internal::checked_cast<const OpaqueType&>(other); |
| 44 | + return storage_type()->Equals(*opaque.storage_type()) && |
| 45 | + type_name() == opaque.type_name() && vendor_name() == opaque.vendor_name(); |
| 46 | +} |
| 47 | + |
| 48 | +std::string OpaqueType::Serialize() const { |
| 49 | + rapidjson::Document document; |
| 50 | + document.SetObject(); |
| 51 | + rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); |
| 52 | + |
| 53 | + rapidjson::Value type_name(rapidjson::StringRef(type_name_)); |
| 54 | + document.AddMember(rapidjson::Value("type_name", allocator), type_name, allocator); |
| 55 | + rapidjson::Value vendor_name(rapidjson::StringRef(vendor_name_)); |
| 56 | + document.AddMember(rapidjson::Value("vendor_name", allocator), vendor_name, allocator); |
| 57 | + |
| 58 | + rapidjson::StringBuffer buffer; |
| 59 | + rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); |
| 60 | + document.Accept(writer); |
| 61 | + return buffer.GetString(); |
| 62 | +} |
| 63 | + |
| 64 | +Result<std::shared_ptr<DataType>> OpaqueType::Deserialize( |
| 65 | + std::shared_ptr<DataType> storage_type, const std::string& serialized_data) const { |
| 66 | + rapidjson::Document document; |
| 67 | + const auto& parsed = document.Parse(serialized_data.data(), serialized_data.length()); |
| 68 | + if (parsed.HasParseError()) { |
| 69 | + return Status::Invalid("Invalid serialized JSON data for OpaqueType: ", |
| 70 | + rapidjson::GetParseError_En(parsed.GetParseError()), ": ", |
| 71 | + serialized_data); |
| 72 | + } else if (!document.IsObject()) { |
| 73 | + return Status::Invalid("Invalid serialized JSON data for OpaqueType: not an object"); |
| 74 | + } |
| 75 | + if (!document.HasMember("type_name")) { |
| 76 | + return Status::Invalid( |
| 77 | + "Invalid serialized JSON data for OpaqueType: missing type_name"); |
| 78 | + } else if (!document.HasMember("vendor_name")) { |
| 79 | + return Status::Invalid( |
| 80 | + "Invalid serialized JSON data for OpaqueType: missing vendor_name"); |
| 81 | + } |
| 82 | + |
| 83 | + const auto& type_name = document["type_name"]; |
| 84 | + const auto& vendor_name = document["vendor_name"]; |
| 85 | + if (!type_name.IsString()) { |
| 86 | + return Status::Invalid( |
| 87 | + "Invalid serialized JSON data for OpaqueType: type_name is not a string"); |
| 88 | + } else if (!vendor_name.IsString()) { |
| 89 | + return Status::Invalid( |
| 90 | + "Invalid serialized JSON data for OpaqueType: vendor_name is not a string"); |
| 91 | + } |
| 92 | + |
| 93 | + return opaque(std::move(storage_type), type_name.GetString(), vendor_name.GetString()); |
| 94 | +} |
| 95 | + |
| 96 | +std::shared_ptr<Array> OpaqueType::MakeArray(std::shared_ptr<ArrayData> data) const { |
| 97 | + DCHECK_EQ(data->type->id(), Type::EXTENSION); |
| 98 | + DCHECK_EQ("arrow.opaque", |
| 99 | + internal::checked_cast<const ExtensionType&>(*data->type).extension_name()); |
| 100 | + return std::make_shared<OpaqueArray>(data); |
| 101 | +} |
| 102 | + |
| 103 | +std::shared_ptr<DataType> opaque(std::shared_ptr<DataType> storage_type, |
| 104 | + std::string type_name, std::string vendor_name) { |
| 105 | + return std::make_shared<OpaqueType>(std::move(storage_type), std::move(type_name), |
| 106 | + std::move(vendor_name)); |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace arrow::extension |
0 commit comments