forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_view_test.cc
More file actions
423 lines (353 loc) · 15.2 KB
/
Copy patharray_view_test.cc
File metadata and controls
423 lines (353 loc) · 15.2 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// 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 <string>
#include <gtest/gtest.h>
#include "arrow/array.h"
#include "arrow/extension_type.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/util.h"
#include "arrow/type.h"
#include "arrow/util/logging.h"
namespace arrow {
void CheckView(const std::shared_ptr<Array>& input,
const std::shared_ptr<DataType>& view_type,
const std::shared_ptr<Array>& expected) {
std::shared_ptr<Array> result;
ASSERT_OK(input->View(view_type, &result));
ASSERT_OK(result->Validate());
AssertArraysEqual(*expected, *result);
}
void CheckView(const std::shared_ptr<Array>& input,
const std::shared_ptr<Array>& expected_view) {
CheckView(input, expected_view->type(), expected_view);
}
void CheckViewFails(const std::shared_ptr<Array>& input,
const std::shared_ptr<DataType>& view_type) {
std::shared_ptr<Array> result;
ASSERT_RAISES(Invalid, input->View(view_type, &result));
}
class IPv4Type : public ExtensionType {
public:
IPv4Type() : ExtensionType(fixed_size_binary(4)) {}
std::string extension_name() const override { return "ipv4"; }
bool ExtensionEquals(const ExtensionType& other) const override {
return other.extension_name() == this->extension_name();
}
std::shared_ptr<Array> MakeArray(std::shared_ptr<ArrayData> data) const override {
DCHECK_EQ(data->type->id(), Type::EXTENSION);
DCHECK_EQ("ipv4", static_cast<const ExtensionType&>(*data->type).extension_name());
return std::make_shared<ExtensionArray>(data);
}
Status Deserialize(std::shared_ptr<DataType> storage_type,
const std::string& serialized,
std::shared_ptr<DataType>* out) const override {
return Status::NotImplemented("IPv4Type::Deserialize");
}
std::string Serialize() const override { return ""; }
};
TEST(TestArrayView, IdentityPrimitive) {
auto arr = ArrayFromJSON(int16(), "[0, -1, 42]");
CheckView(arr, arr->type(), arr);
arr = ArrayFromJSON(int16(), "[0, -1, 42, null]");
CheckView(arr, arr->type(), arr);
arr = ArrayFromJSON(boolean(), "[true, false, null]");
CheckView(arr, arr->type(), arr);
}
TEST(TestArrayView, IdentityNullType) {
auto arr = ArrayFromJSON(null(), "[null, null, null]");
CheckView(arr, arr->type(), arr);
}
TEST(TestArrayView, PrimitiveAsPrimitive) {
auto arr = ArrayFromJSON(int16(), "[0, -1, 42]");
auto expected = ArrayFromJSON(uint16(), "[0, 65535, 42]");
CheckView(arr, expected);
CheckView(expected, arr);
arr = ArrayFromJSON(int32(), "[0, 1069547520, -1071644672, null]");
expected = ArrayFromJSON(float32(), "[0.0, 1.5, -2.5, null]");
CheckView(arr, expected);
arr = ArrayFromJSON(timestamp(TimeUnit::SECOND),
R"(["1970-01-01","2000-02-29","3989-07-14","1900-02-28"])");
expected = ArrayFromJSON(int64(), "[0, 951782400, 63730281600, -2203977600]");
CheckView(arr, expected);
CheckView(expected, arr);
}
TEST(TestArrayView, PrimitiveAsFixedSizeBinary) {
auto arr = ArrayFromJSON(int32(), "[2020568934, 2054316386, null]");
auto expected = ArrayFromJSON(fixed_size_binary(4), R"(["foox", "barz", null])");
CheckView(arr, expected);
CheckView(expected, arr);
}
TEST(TestArrayView, StringAsBinary) {
auto arr = ArrayFromJSON(utf8(), R"(["foox", "barz", null])");
auto expected = ArrayFromJSON(binary(), R"(["foox", "barz", null])");
CheckView(arr, expected);
CheckView(expected, arr);
}
TEST(TestArrayView, PrimitiveWrongSize) {
auto arr = ArrayFromJSON(int16(), "[0, -1, 42]");
CheckViewFails(arr, int8());
CheckViewFails(arr, fixed_size_binary(3));
CheckViewFails(arr, null());
}
TEST(TestArrayView, StructAsStructSimple) {
auto ty1 = struct_({field("a", int8()), field("b", int32())});
auto ty2 = struct_({field("c", uint8()), field("d", float32())});
auto arr = ArrayFromJSON(ty1, "[[0, 0], [1, 1069547520], [-1, -1071644672]]");
auto expected = ArrayFromJSON(ty2, "[[0, 0], [1, 1.5], [255, -2.5]]");
CheckView(arr, expected);
CheckView(expected, arr);
// With nulls
arr = ArrayFromJSON(ty1, "[[0, 0], null, [-1, -1071644672]]");
expected = ArrayFromJSON(ty2, "[[0, 0], null, [255, -2.5]]");
CheckView(arr, expected);
CheckView(expected, arr);
// With nested nulls
arr = ArrayFromJSON(ty1, "[[0, null], null, [-1, -1071644672]]");
expected = ArrayFromJSON(ty2, "[[0, null], null, [255, -2.5]]");
CheckView(arr, expected);
CheckView(expected, arr);
ty2 = struct_({field("c", uint8()), field("d", fixed_size_binary(4))});
arr = ArrayFromJSON(ty1, "[[0, null], null, [-1, 2020568934]]");
expected = ArrayFromJSON(ty2, R"([[0, null], null, [255, "foox"]])");
CheckView(arr, expected);
CheckView(expected, arr);
}
TEST(TestArrayView, StructAsStructNonNullable) {
auto ty1 = struct_({field("a", int8()), field("b", int32())});
auto ty2 = struct_({field("c", uint8(), /*nullable=*/false), field("d", float32())});
auto arr = ArrayFromJSON(ty1, "[[0, 0], [1, 1069547520], [-1, -1071644672]]");
auto expected = ArrayFromJSON(ty2, "[[0, 0], [1, 1.5], [255, -2.5]]");
CheckView(arr, expected);
CheckView(expected, arr);
// With nested nulls
arr = ArrayFromJSON(ty1, "[[0, null], [-1, -1071644672]]");
expected = ArrayFromJSON(ty2, "[[0, null], [255, -2.5]]");
CheckView(arr, expected);
CheckView(expected, arr);
// Nested null cannot be viewed as non-null field
arr = ArrayFromJSON(ty1, "[[0, null], [null, -1071644672]]");
CheckViewFails(arr, ty2);
}
TEST(TestArrayView, StructAsStructWrongLayout) {
auto ty1 = struct_({field("a", int8()), field("b", int32())});
auto arr = ArrayFromJSON(ty1, "[[0, 0], [1, 1069547520], [-1, -1071644672]]");
auto ty2 = struct_({field("c", int16()), field("d", int32())});
CheckViewFails(arr, ty2);
ty2 = struct_({field("c", int32()), field("d", int8())});
CheckViewFails(arr, ty2);
ty2 = struct_({field("c", int8())});
CheckViewFails(arr, ty2);
ty2 = struct_({field("c", fixed_size_binary(5))});
CheckViewFails(arr, ty2);
}
TEST(TestArrayView, StructAsStructWithNullType) {
auto ty1 = struct_({field("a", int8()), field("b", null())});
auto ty2 = struct_({field("c", uint8()), field("d", null())});
auto arr = ArrayFromJSON(ty1, "[[0, null], [1, null], [-1, null]]");
auto expected = ArrayFromJSON(ty2, "[[0, null], [1, null], [255, null]]");
CheckView(arr, expected);
CheckView(expected, arr);
// With nulls and nested nulls
arr = ArrayFromJSON(ty1, "[null, [null, null], [-1, null]]");
expected = ArrayFromJSON(ty2, "[null, [null, null], [255, null]]");
CheckView(arr, expected);
CheckView(expected, arr);
// Moving the null types around
ty2 = struct_({field("c", null()), field("d", uint8())});
expected = ArrayFromJSON(ty2, "[null, [null, null], [null, 255]]");
CheckView(arr, expected);
CheckView(expected, arr);
// Removing the null type
ty2 = struct_({field("c", uint8())});
expected = ArrayFromJSON(ty2, "[null, [null], [255]]");
CheckView(arr, expected);
CheckView(expected, arr);
}
TEST(TestArrayView, StructAsFlat) {
auto ty1 = struct_({field("a", int16())});
auto arr = ArrayFromJSON(ty1, "[[0], [1], [-1]]");
auto expected = ArrayFromJSON(uint16(), "[0, 1, 65535]");
CheckView(arr, expected);
CheckView(expected, arr);
// With nulls
arr = ArrayFromJSON(ty1, "[[0], null, [-1]]");
expected = ArrayFromJSON(uint16(), "[0, null, 65535]");
// CheckView(arr, expected); // XXX currently fails
CheckView(expected, arr);
// With nested nulls => fails
arr = ArrayFromJSON(ty1, "[[0], [null], [-1]]");
CheckViewFails(arr, uint16());
}
TEST(TestArrayView, StructAsFlatWithNullType) {
auto ty1 = struct_({field("a", null()), field("b", int16()), field("c", null())});
auto arr = ArrayFromJSON(ty1, "[[null, 0, null], [null, -1, null]]");
auto expected = ArrayFromJSON(uint16(), "[0, 65535]");
CheckView(arr, expected);
CheckView(expected, arr);
// With nulls
arr = ArrayFromJSON(ty1, "[[null, 0, null], null, [null, -1, null]]");
expected = ArrayFromJSON(uint16(), "[0, null, 65535]");
// CheckView(arr, expected); // XXX currently fails
CheckView(expected, arr);
// With nested nulls => fails
arr = ArrayFromJSON(ty1, "[[null, null, null]]");
CheckViewFails(arr, uint16());
}
TEST(TestArrayView, StructAsStructNested) {
// Nesting tree shape need not be identical
auto ty1 = struct_({field("a", struct_({field("b", int8())})), field("d", int32())});
auto ty2 = struct_({field("a", uint8()), field("b", struct_({field("b", float32())}))});
auto arr = ArrayFromJSON(ty1, "[[[0], 1069547520], [[-1], -1071644672]]");
auto expected = ArrayFromJSON(ty2, "[[0, [1.5]], [255, [-2.5]]]");
CheckView(arr, expected);
CheckView(expected, arr);
// With null types
ty1 = struct_({field("a", struct_({field("xx", null()), field("b", int8())})),
field("d", int32())});
ty2 = struct_({field("a", uint8()),
field("b", struct_({field("b", float32()), field("xx", null())}))});
arr = ArrayFromJSON(ty1, "[[[null, 0], 1069547520], [[null, -1], -1071644672]]");
expected = ArrayFromJSON(ty2, "[[0, [1.5, null]], [255, [-2.5, null]]]");
CheckView(arr, expected);
CheckView(expected, arr);
// XXX With nulls (currently fails)
}
TEST(TestArrayView, ListAsListSimple) {
auto arr = ArrayFromJSON(list(int16()), "[[0, -1], [], [42]]");
auto expected = ArrayFromJSON(list(uint16()), "[[0, 65535], [], [42]]");
CheckView(arr, expected);
CheckView(expected, arr);
// With nulls
arr = ArrayFromJSON(list(int16()), "[[0, -1], null, [42]]");
expected = ArrayFromJSON(list(uint16()), "[[0, 65535], null, [42]]");
CheckView(arr, expected);
CheckView(expected, arr);
// With nested nulls
arr = ArrayFromJSON(list(int16()), "[[0, -1], null, [null, 42]]");
expected = ArrayFromJSON(list(uint16()), "[[0, 65535], null, [null, 42]]");
CheckView(arr, expected);
CheckView(expected, arr);
}
TEST(TestArrayView, FixedSizeListAsFixedSizeList) {
auto ty1 = fixed_size_list(int16(), 3);
auto ty2 = fixed_size_list(uint16(), 3);
auto arr = ArrayFromJSON(ty1, "[[0, -1, 42], [5, 6, -16384]]");
auto expected = ArrayFromJSON(ty2, "[[0, 65535, 42], [5, 6, 49152]]");
CheckView(arr, expected);
CheckView(expected, arr);
// With nested nulls
arr = ArrayFromJSON(ty1, "[[0, -1, null], null, [5, 6, -16384]]");
expected = ArrayFromJSON(ty2, "[[0, 65535, null], null, [5, 6, 49152]]");
CheckView(arr, expected);
CheckView(expected, arr);
}
TEST(TestArrayView, FixedSizeListAsFlat) {
auto ty1 = fixed_size_list(int16(), 3);
auto arr = ArrayFromJSON(ty1, "[[0, -1, 42], [5, 6, -16384]]");
auto expected = ArrayFromJSON(uint16(), "[0, 65535, 42, 5, 6, 49152]");
CheckView(arr, expected);
// CheckView(expected, arr); // XXX currently fails
// XXX With nulls (currently fails)
}
TEST(TestArrayView, SparseUnionAsStruct) {
auto child1 = ArrayFromJSON(int16(), "[0, -1, 42]");
auto child2 = ArrayFromJSON(int32(), "[0, 1069547520, -1071644672]");
auto indices = ArrayFromJSON(int8(), "[0, 0, 1]");
std::shared_ptr<Array> arr;
ASSERT_OK(UnionArray::MakeSparse(*indices, {child1, child2}, &arr));
ASSERT_OK(arr->Validate());
auto ty1 = struct_({field("a", int8()), field("b", uint16()), field("c", float32())});
auto expected = ArrayFromJSON(ty1, "[[0, 0, 0], [0, 65535, 1.5], [1, 42, -2.5]]");
CheckView(arr, expected);
CheckView(expected, arr);
// With nulls
indices = ArrayFromJSON(int8(), "[null, 0, 1]");
ASSERT_OK(UnionArray::MakeSparse(*indices, {child1, child2}, &arr));
ASSERT_OK(arr->Validate());
expected = ArrayFromJSON(ty1, "[null, [0, 65535, 1.5], [1, 42, -2.5]]");
CheckView(arr, expected);
// CheckView(expected, arr); // XXX currently fails
// With nested nulls
child1 = ArrayFromJSON(int16(), "[0, -1, null]");
child2 = ArrayFromJSON(int32(), "[0, null, -1071644672]");
ASSERT_OK(UnionArray::MakeSparse(*indices, {child1, child2}, &arr));
ASSERT_OK(arr->Validate());
expected = ArrayFromJSON(ty1, "[null, [0, 65535, null], [1, null, -2.5]]");
CheckView(arr, expected);
// CheckView(expected, arr); // XXX currently fails
}
TEST(TestArrayView, DecimalRoundTrip) {
auto ty1 = decimal(10, 4);
auto arr = ArrayFromJSON(ty1, R"(["123.4567", "-78.9000", null])");
auto ty2 = fixed_size_binary(16);
std::shared_ptr<Array> v, w;
ASSERT_OK(arr->View(ty2, &v));
ASSERT_OK(v->Validate());
ASSERT_OK(v->View(ty1, &w));
ASSERT_OK(w->Validate());
AssertArraysEqual(*arr, *w);
}
TEST(TestArrayView, Dictionaries) {
// ARROW-6049
auto ty1 = dictionary(int8(), float32());
auto ty2 = dictionary(int8(), int32());
auto indices = ArrayFromJSON(int8(), "[0, 2, null, 1]");
auto values = ArrayFromJSON(float32(), "[0.0, 1.5, -2.5]");
std::shared_ptr<Array> arr, expected, expected_dict;
ASSERT_OK(values->View(int32(), &expected_dict));
ASSERT_OK(DictionaryArray::FromArrays(ty1, indices, values, &arr));
ASSERT_OK(DictionaryArray::FromArrays(ty2, indices, expected_dict, &expected));
CheckView(arr, expected);
CheckView(expected, arr);
// Incompatible index type
auto ty3 = dictionary(int16(), int32());
CheckViewFails(arr, ty3);
// Incompatible dictionary type
auto ty4 = dictionary(int16(), float64());
CheckViewFails(arr, ty4);
// Check dictionary-encoded child
auto offsets = ArrayFromJSON(int32(), "[0, 2, 2, 4]");
std::shared_ptr<Array> list_arr, expected_list_arr;
ASSERT_OK(ListArray::FromArrays(*offsets, *arr, default_memory_pool(), &list_arr));
ASSERT_OK(ListArray::FromArrays(*offsets, *expected, default_memory_pool(),
&expected_list_arr));
CheckView(list_arr, expected_list_arr);
CheckView(expected_list_arr, list_arr);
}
TEST(TestArrayView, ExtensionType) {
auto ty1 = std::make_shared<IPv4Type>();
auto data = ArrayFromJSON(ty1->storage_type(), R"(["ABCD", null])")->data();
data->type = ty1;
auto arr = ty1->MakeArray(data);
auto expected = ArrayFromJSON(uint32(), "[1145258561, null]");
CheckView(arr, expected);
CheckView(expected, arr);
}
TEST(TestArrayView, NonZeroOffset) {
auto arr = ArrayFromJSON(int16(), "[10, 11, 12, 13]")->Slice(1);
CheckViewFails(arr, uint16());
}
TEST(TestArrayView, NonZeroNestedOffset) {
auto list_values = ArrayFromJSON(int16(), "[10, 11, 12, 13]")->Slice(1);
auto list_offsets = ArrayFromJSON(int32(), "[0, 2, 3]");
std::shared_ptr<Array> list_array;
ASSERT_OK(ListArray::FromArrays(*list_offsets, *list_values, default_memory_pool(),
&list_array));
ASSERT_OK(list_array->Validate());
CheckViewFails(list_array, list(uint16()));
}
} // namespace arrow