summaryrefslogtreecommitdiffstats
path: root/contrib/libs/apache/arrow_next/cpp/src/arrow/extension/opaque.cc
blob: 5c340e1af0a4dba4f6939e68ff88605ff07525a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 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 "contrib/libs/apache/arrow_next/cpp/src/arrow/extension/opaque.h"

#include <sstream>

#include "contrib/libs/apache/arrow_next/cpp/src/arrow/json/rapidjson_defs.h"  // IWYU pragma: keep
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/logging.h"

#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <rapidjson/writer.h>

namespace arrow20::extension {

std::string OpaqueType::ToString(bool show_metadata) const {
  std::stringstream ss;
  ss << "extension<" << this->extension_name()
     << "[storage_type=" << storage_type_->ToString(show_metadata)
     << ", type_name=" << type_name_ << ", vendor_name=" << vendor_name_ << "]>";
  return ss.str();
}

bool OpaqueType::ExtensionEquals(const ExtensionType& other) const {
  if (extension_name() != other.extension_name()) {
    return false;
  }
  const auto& opaque = internal::checked_cast<const OpaqueType&>(other);
  return storage_type()->Equals(*opaque.storage_type()) &&
         type_name() == opaque.type_name() && vendor_name() == opaque.vendor_name();
}

std::string OpaqueType::Serialize() const {
  rapidjson::Document document;
  document.SetObject();
  rapidjson::Document::AllocatorType& allocator = document.GetAllocator();

  rapidjson::Value type_name(rapidjson::StringRef(type_name_));
  document.AddMember(rapidjson::Value("type_name", allocator), type_name, allocator);
  rapidjson::Value vendor_name(rapidjson::StringRef(vendor_name_));
  document.AddMember(rapidjson::Value("vendor_name", allocator), vendor_name, allocator);

  rapidjson::StringBuffer buffer;
  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  document.Accept(writer);
  return buffer.GetString();
}

Result<std::shared_ptr<DataType>> OpaqueType::Deserialize(
    std::shared_ptr<DataType> storage_type, const std::string& serialized_data) const {
  rapidjson::Document document;
  const auto& parsed = document.Parse(serialized_data.data(), serialized_data.length());
  if (parsed.HasParseError()) {
    return Status::Invalid("Invalid serialized JSON data for OpaqueType: ",
                           rapidjson::GetParseError_En(parsed.GetParseError()), ": ",
                           serialized_data);
  } else if (!document.IsObject()) {
    return Status::Invalid("Invalid serialized JSON data for OpaqueType: not an object");
  }
  if (!document.HasMember("type_name")) {
    return Status::Invalid(
        "Invalid serialized JSON data for OpaqueType: missing type_name");
  } else if (!document.HasMember("vendor_name")) {
    return Status::Invalid(
        "Invalid serialized JSON data for OpaqueType: missing vendor_name");
  }

  const auto& type_name = document["type_name"];
  const auto& vendor_name = document["vendor_name"];
  if (!type_name.IsString()) {
    return Status::Invalid(
        "Invalid serialized JSON data for OpaqueType: type_name is not a string");
  } else if (!vendor_name.IsString()) {
    return Status::Invalid(
        "Invalid serialized JSON data for OpaqueType: vendor_name is not a string");
  }

  return opaque(std::move(storage_type), type_name.GetString(), vendor_name.GetString());
}

std::shared_ptr<Array> OpaqueType::MakeArray(std::shared_ptr<ArrayData> data) const {
  DCHECK_EQ(data->type->id(), Type::EXTENSION);
  DCHECK_EQ("arrow.opaque",
            internal::checked_cast<const ExtensionType&>(*data->type).extension_name());
  return std::make_shared<OpaqueArray>(data);
}

std::shared_ptr<DataType> opaque(std::shared_ptr<DataType> storage_type,
                                 std::string type_name, std::string vendor_name) {
  return std::make_shared<OpaqueType>(std::move(storage_type), std::move(type_name),
                                      std::move(vendor_name));
}

}  // namespace arrow20::extension