forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension_type_test.cc
More file actions
336 lines (272 loc) · 11.5 KB
/
Copy pathextension_type_test.cc
File metadata and controls
336 lines (272 loc) · 11.5 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// 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 <algorithm>
#include <cstdint>
#include <cstring>
#include <memory>
#include <sstream>
#include <string>
#include <gtest/gtest.h>
#include "arrow/array/array_nested.h"
#include "arrow/array/util.h"
#include "arrow/extension_type.h"
#include "arrow/io/memory.h"
#include "arrow/ipc/options.h"
#include "arrow/ipc/reader.h"
#include "arrow/ipc/writer.h"
#include "arrow/record_batch.h"
#include "arrow/status.h"
#include "arrow/testing/extension_type.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/type.h"
#include "arrow/util/key_value_metadata.h"
#include "arrow/util/logging.h"
namespace arrow {
class Parametric1Array : public ExtensionArray {
public:
using ExtensionArray::ExtensionArray;
};
class Parametric2Array : public ExtensionArray {
public:
using ExtensionArray::ExtensionArray;
};
// A parametric type where the extension_name() is always the same
class Parametric1Type : public ExtensionType {
public:
explicit Parametric1Type(int32_t parameter)
: ExtensionType(int32()), parameter_(parameter) {}
int32_t parameter() const { return parameter_; }
std::string extension_name() const override { return "parametric-type-1"; }
bool ExtensionEquals(const ExtensionType& other) const override {
const auto& other_ext = static_cast<const ExtensionType&>(other);
if (other_ext.extension_name() != this->extension_name()) {
return false;
}
return this->parameter() == static_cast<const Parametric1Type&>(other).parameter();
}
std::shared_ptr<Array> MakeArray(std::shared_ptr<ArrayData> data) const override {
return std::make_shared<Parametric1Array>(data);
}
Result<std::shared_ptr<DataType>> Deserialize(
std::shared_ptr<DataType> storage_type,
const std::string& serialized) const override {
DCHECK_EQ(4, serialized.size());
const int32_t parameter = *reinterpret_cast<const int32_t*>(serialized.data());
DCHECK(storage_type->Equals(int32()));
return std::make_shared<Parametric1Type>(parameter);
}
std::string Serialize() const override {
std::string result(" ");
memcpy(&result[0], ¶meter_, sizeof(int32_t));
return result;
}
private:
int32_t parameter_;
};
// A parametric type where the extension_name() is different for each
// parameter, and must be separately registered
class Parametric2Type : public ExtensionType {
public:
explicit Parametric2Type(int32_t parameter)
: ExtensionType(int32()), parameter_(parameter) {}
int32_t parameter() const { return parameter_; }
std::string extension_name() const override {
std::stringstream ss;
ss << "parametric-type-2<param=" << parameter_ << ">";
return ss.str();
}
bool ExtensionEquals(const ExtensionType& other) const override {
const auto& other_ext = static_cast<const ExtensionType&>(other);
if (other_ext.extension_name() != this->extension_name()) {
return false;
}
return this->parameter() == static_cast<const Parametric2Type&>(other).parameter();
}
std::shared_ptr<Array> MakeArray(std::shared_ptr<ArrayData> data) const override {
return std::make_shared<Parametric2Array>(data);
}
Result<std::shared_ptr<DataType>> Deserialize(
std::shared_ptr<DataType> storage_type,
const std::string& serialized) const override {
DCHECK_EQ(4, serialized.size());
const int32_t parameter = *reinterpret_cast<const int32_t*>(serialized.data());
DCHECK(storage_type->Equals(int32()));
return std::make_shared<Parametric2Type>(parameter);
}
std::string Serialize() const override {
std::string result(" ");
memcpy(&result[0], ¶meter_, sizeof(int32_t));
return result;
}
private:
int32_t parameter_;
};
// An extension type with a non-primitive storage type
class ExtStructArray : public ExtensionArray {
public:
using ExtensionArray::ExtensionArray;
};
class ExtStructType : public ExtensionType {
public:
ExtStructType()
: ExtensionType(
struct_({::arrow::field("a", int64()), ::arrow::field("b", float64())})) {}
std::string extension_name() const override { return "ext-struct-type"; }
bool ExtensionEquals(const ExtensionType& other) const override {
const auto& other_ext = static_cast<const ExtensionType&>(other);
if (other_ext.extension_name() != this->extension_name()) {
return false;
}
return true;
}
std::shared_ptr<Array> MakeArray(std::shared_ptr<ArrayData> data) const override {
return std::make_shared<ExtStructArray>(data);
}
Result<std::shared_ptr<DataType>> Deserialize(
std::shared_ptr<DataType> storage_type,
const std::string& serialized) const override {
if (serialized != "ext-struct-type-unique-code") {
return Status::Invalid("Type identifier did not match");
}
return std::make_shared<ExtStructType>();
}
std::string Serialize() const override { return "ext-struct-type-unique-code"; }
};
class TestExtensionType : public ::testing::Test {
public:
void SetUp() { ASSERT_OK(RegisterExtensionType(std::make_shared<UuidType>())); }
void TearDown() {
if (GetExtensionType("uuid")) {
ASSERT_OK(UnregisterExtensionType("uuid"));
}
}
};
TEST_F(TestExtensionType, ExtensionTypeTest) {
auto type_not_exist = GetExtensionType("uuid-unknown");
ASSERT_EQ(type_not_exist, nullptr);
auto registered_type = GetExtensionType("uuid");
ASSERT_NE(registered_type, nullptr);
auto type = uuid();
ASSERT_EQ(type->id(), Type::EXTENSION);
const auto& ext_type = static_cast<const ExtensionType&>(*type);
std::string serialized = ext_type.Serialize();
ASSERT_OK_AND_ASSIGN(auto deserialized,
ext_type.Deserialize(fixed_size_binary(16), serialized));
ASSERT_TRUE(deserialized->Equals(*type));
ASSERT_FALSE(deserialized->Equals(*fixed_size_binary(16)));
}
auto RoundtripBatch = [](const std::shared_ptr<RecordBatch>& batch,
std::shared_ptr<RecordBatch>* out) {
ASSERT_OK_AND_ASSIGN(auto out_stream, io::BufferOutputStream::Create());
ASSERT_OK(ipc::WriteRecordBatchStream({batch}, ipc::IpcWriteOptions::Defaults(),
out_stream.get()));
ASSERT_OK_AND_ASSIGN(auto complete_ipc_stream, out_stream->Finish());
io::BufferReader reader(complete_ipc_stream);
std::shared_ptr<RecordBatchReader> batch_reader;
ASSERT_OK_AND_ASSIGN(batch_reader, ipc::RecordBatchStreamReader::Open(&reader));
ASSERT_OK(batch_reader->ReadNext(out));
};
TEST_F(TestExtensionType, IpcRoundtrip) {
auto ext_arr = ExampleUuid();
auto batch = RecordBatch::Make(schema({field("f0", uuid())}), 4, {ext_arr});
std::shared_ptr<RecordBatch> read_batch;
RoundtripBatch(batch, &read_batch);
CompareBatch(*batch, *read_batch, false /* compare_metadata */);
// Wrap type in a ListArray and ensure it also makes it
auto offsets_arr = ArrayFromJSON(int32(), "[0, 0, 2, 4]");
ASSERT_OK_AND_ASSIGN(auto list_arr, ListArray::FromArrays(*offsets_arr, *ext_arr));
batch = RecordBatch::Make(schema({field("f0", list(uuid()))}), 3, {list_arr});
RoundtripBatch(batch, &read_batch);
CompareBatch(*batch, *read_batch, false /* compare_metadata */);
}
TEST_F(TestExtensionType, UnrecognizedExtension) {
auto ext_arr = ExampleUuid();
auto batch = RecordBatch::Make(schema({field("f0", uuid())}), 4, {ext_arr});
auto storage_arr = static_cast<const ExtensionArray&>(*ext_arr).storage();
// Write full IPC stream including schema, then unregister type, then read
// and ensure that a plain instance of the storage type is created
ASSERT_OK_AND_ASSIGN(auto out_stream, io::BufferOutputStream::Create());
ASSERT_OK(ipc::WriteRecordBatchStream({batch}, ipc::IpcWriteOptions::Defaults(),
out_stream.get()));
ASSERT_OK_AND_ASSIGN(auto complete_ipc_stream, out_stream->Finish());
ASSERT_OK(UnregisterExtensionType("uuid"));
auto ext_metadata =
key_value_metadata({{"ARROW:extension:name", "uuid"},
{"ARROW:extension:metadata", "uuid-serialized"}});
auto ext_field = field("f0", fixed_size_binary(16), true, ext_metadata);
auto batch_no_ext = RecordBatch::Make(schema({ext_field}), 4, {storage_arr});
io::BufferReader reader(complete_ipc_stream);
std::shared_ptr<RecordBatchReader> batch_reader;
ASSERT_OK_AND_ASSIGN(batch_reader, ipc::RecordBatchStreamReader::Open(&reader));
std::shared_ptr<RecordBatch> read_batch;
ASSERT_OK(batch_reader->ReadNext(&read_batch));
CompareBatch(*batch_no_ext, *read_batch);
}
std::shared_ptr<Array> ExampleParametric(std::shared_ptr<DataType> type,
const std::string& json_data) {
auto arr = ArrayFromJSON(int32(), json_data);
auto ext_data = arr->data()->Copy();
ext_data->type = type;
return MakeArray(ext_data);
}
TEST_F(TestExtensionType, ParametricTypes) {
auto p1_type = std::make_shared<Parametric1Type>(6);
auto p1 = ExampleParametric(p1_type, "[null, 1, 2, 3]");
auto p2_type = std::make_shared<Parametric1Type>(12);
auto p2 = ExampleParametric(p2_type, "[2, null, 3, 4]");
auto p3_type = std::make_shared<Parametric2Type>(2);
auto p3 = ExampleParametric(p3_type, "[5, 6, 7, 8]");
auto p4_type = std::make_shared<Parametric2Type>(3);
auto p4 = ExampleParametric(p4_type, "[5, 6, 7, 9]");
ASSERT_OK(RegisterExtensionType(std::make_shared<Parametric1Type>(-1)));
ASSERT_OK(RegisterExtensionType(p3_type));
ASSERT_OK(RegisterExtensionType(p4_type));
auto batch = RecordBatch::Make(schema({field("f0", p1_type), field("f1", p2_type),
field("f2", p3_type), field("f3", p4_type)}),
4, {p1, p2, p3, p4});
std::shared_ptr<RecordBatch> read_batch;
RoundtripBatch(batch, &read_batch);
CompareBatch(*batch, *read_batch, false /* compare_metadata */);
}
TEST_F(TestExtensionType, ParametricEquals) {
auto p1_type = std::make_shared<Parametric1Type>(6);
auto p2_type = std::make_shared<Parametric1Type>(6);
auto p3_type = std::make_shared<Parametric1Type>(3);
ASSERT_TRUE(p1_type->Equals(p2_type));
ASSERT_FALSE(p1_type->Equals(p3_type));
ASSERT_EQ(p1_type->fingerprint(), "");
}
std::shared_ptr<Array> ExampleStruct() {
auto ext_type = std::make_shared<ExtStructType>();
auto storage_type = ext_type->storage_type();
auto arr = ArrayFromJSON(storage_type, "[[1, 0.1], [2, 0.2]]");
auto ext_data = arr->data()->Copy();
ext_data->type = ext_type;
return MakeArray(ext_data);
}
TEST_F(TestExtensionType, ValidateExtensionArray) {
auto ext_arr1 = ExampleUuid();
auto p1_type = std::make_shared<Parametric1Type>(6);
auto ext_arr2 = ExampleParametric(p1_type, "[null, 1, 2, 3]");
auto ext_arr3 = ExampleStruct();
auto ext_arr4 = ExampleComplex128();
ASSERT_OK(ext_arr1->ValidateFull());
ASSERT_OK(ext_arr2->ValidateFull());
ASSERT_OK(ext_arr3->ValidateFull());
ASSERT_OK(ext_arr4->ValidateFull());
}
} // namespace arrow