forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype-test.cc
More file actions
444 lines (350 loc) · 12.8 KB
/
Copy pathtype-test.cc
File metadata and controls
444 lines (350 loc) · 12.8 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// 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.
// Unit tests for DataType (and subclasses), Field, and Schema
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include <gtest/gtest.h>
#include "arrow/test-util.h"
#include "arrow/type.h"
#include "arrow/util/key_value_metadata.h"
using std::shared_ptr;
using std::vector;
namespace arrow {
TEST(TestField, Basics) {
Field f0("f0", int32());
Field f0_nn("f0", int32(), false);
ASSERT_EQ(f0.name(), "f0");
ASSERT_EQ(f0.type()->ToString(), int32()->ToString());
ASSERT_TRUE(f0.nullable());
ASSERT_FALSE(f0_nn.nullable());
}
TEST(TestField, Equals) {
Field f0("f0", int32());
Field f0_nn("f0", int32(), false);
Field f0_other("f0", int32());
ASSERT_TRUE(f0.Equals(f0_other));
ASSERT_FALSE(f0.Equals(f0_nn));
}
TEST(TestField, TestMetadataConstruction) {
auto metadata = std::shared_ptr<KeyValueMetadata>(
new KeyValueMetadata({"foo", "bar"}, {"bizz", "buzz"}));
auto metadata2 = metadata->Copy();
auto f0 = field("f0", int32(), true, metadata);
auto f1 = field("f0", int32(), true, metadata2);
ASSERT_TRUE(metadata->Equals(*f0->metadata()));
ASSERT_TRUE(f0->Equals(*f1));
}
TEST(TestField, TestAddMetadata) {
auto metadata = std::shared_ptr<KeyValueMetadata>(
new KeyValueMetadata({"foo", "bar"}, {"bizz", "buzz"}));
auto f0 = field("f0", int32());
auto f1 = field("f0", int32(), true, metadata);
std::shared_ptr<Field> f2 = f0->AddMetadata(metadata);
ASSERT_FALSE(f2->Equals(*f0));
ASSERT_TRUE(f2->Equals(*f1));
// Not copied
ASSERT_TRUE(metadata.get() == f1->metadata().get());
}
TEST(TestField, TestRemoveMetadata) {
auto metadata = std::shared_ptr<KeyValueMetadata>(
new KeyValueMetadata({"foo", "bar"}, {"bizz", "buzz"}));
auto f0 = field("f0", int32());
auto f1 = field("f0", int32(), true, metadata);
std::shared_ptr<Field> f2 = f1->RemoveMetadata();
ASSERT_TRUE(f2->metadata() == nullptr);
}
class TestSchema : public ::testing::Test {
public:
void SetUp() {}
};
TEST_F(TestSchema, Basics) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f1_optional = field("f1", uint8());
auto f2 = field("f2", utf8());
auto schema = ::arrow::schema({f0, f1, f2});
ASSERT_EQ(3, schema->num_fields());
ASSERT_TRUE(f0->Equals(schema->field(0)));
ASSERT_TRUE(f1->Equals(schema->field(1)));
ASSERT_TRUE(f2->Equals(schema->field(2)));
auto schema2 = ::arrow::schema({f0, f1, f2});
vector<shared_ptr<Field>> fields3 = {f0, f1_optional, f2};
auto schema3 = std::make_shared<Schema>(fields3);
ASSERT_TRUE(schema->Equals(*schema2));
ASSERT_FALSE(schema->Equals(*schema3));
}
TEST_F(TestSchema, ToString) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f2 = field("f2", utf8());
auto f3 = field("f3", list(int16()));
auto schema = ::arrow::schema({f0, f1, f2, f3});
std::string result = schema->ToString();
std::string expected = R"(f0: int32
f1: uint8 not null
f2: string
f3: list<item: int16>)";
ASSERT_EQ(expected, result);
}
TEST_F(TestSchema, GetFieldByName) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f2 = field("f2", utf8());
auto f3 = field("f3", list(int16()));
auto schema = ::arrow::schema({f0, f1, f2, f3});
std::shared_ptr<Field> result;
result = schema->GetFieldByName("f1");
ASSERT_TRUE(f1->Equals(result));
result = schema->GetFieldByName("f3");
ASSERT_TRUE(f3->Equals(result));
result = schema->GetFieldByName("not-found");
ASSERT_TRUE(result == nullptr);
}
TEST_F(TestSchema, GetFieldIndex) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f2 = field("f2", utf8());
auto f3 = field("f3", list(int16()));
auto schema = ::arrow::schema({f0, f1, f2, f3});
ASSERT_EQ(0, schema->GetFieldIndex(f0->name()));
ASSERT_EQ(1, schema->GetFieldIndex(f1->name()));
ASSERT_EQ(2, schema->GetFieldIndex(f2->name()));
ASSERT_EQ(3, schema->GetFieldIndex(f3->name()));
ASSERT_EQ(-1, schema->GetFieldIndex("not-found"));
}
TEST_F(TestSchema, TestMetadataConstruction) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f2 = field("f2", utf8());
auto metadata = std::shared_ptr<KeyValueMetadata>(
new KeyValueMetadata({"foo", "bar"}, {"bizz", "buzz"}));
auto schema = ::arrow::schema({f0, f1, f2}, metadata);
ASSERT_TRUE(metadata->Equals(*schema->metadata()));
}
TEST_F(TestSchema, TestAddMetadata) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f2 = field("f2", utf8());
vector<shared_ptr<Field>> fields = {f0, f1, f2};
auto metadata = std::shared_ptr<KeyValueMetadata>(
new KeyValueMetadata({"foo", "bar"}, {"bizz", "buzz"}));
auto schema = std::make_shared<Schema>(fields);
std::shared_ptr<Schema> new_schema = schema->AddMetadata(metadata);
ASSERT_TRUE(metadata->Equals(*new_schema->metadata()));
// Not copied
ASSERT_TRUE(metadata.get() == new_schema->metadata().get());
}
TEST_F(TestSchema, TestRemoveMetadata) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f2 = field("f2", utf8());
vector<shared_ptr<Field>> fields = {f0, f1, f2};
KeyValueMetadata metadata({"foo", "bar"}, {"bizz", "buzz"});
auto schema = std::make_shared<Schema>(fields);
std::shared_ptr<Schema> new_schema = schema->RemoveMetadata();
ASSERT_TRUE(new_schema->metadata() == nullptr);
}
#define PRIMITIVE_TEST(KLASS, ENUM, NAME) \
TEST(TypesTest, TestPrimitive_##ENUM) { \
KLASS tp; \
\
ASSERT_EQ(tp.id(), Type::ENUM); \
ASSERT_EQ(tp.ToString(), std::string(NAME)); \
}
PRIMITIVE_TEST(Int8Type, INT8, "int8");
PRIMITIVE_TEST(Int16Type, INT16, "int16");
PRIMITIVE_TEST(Int32Type, INT32, "int32");
PRIMITIVE_TEST(Int64Type, INT64, "int64");
PRIMITIVE_TEST(UInt8Type, UINT8, "uint8");
PRIMITIVE_TEST(UInt16Type, UINT16, "uint16");
PRIMITIVE_TEST(UInt32Type, UINT32, "uint32");
PRIMITIVE_TEST(UInt64Type, UINT64, "uint64");
PRIMITIVE_TEST(FloatType, FLOAT, "float");
PRIMITIVE_TEST(DoubleType, DOUBLE, "double");
PRIMITIVE_TEST(BooleanType, BOOL, "bool");
TEST(TestBinaryType, ToString) {
BinaryType t1;
BinaryType e1;
StringType t2;
EXPECT_TRUE(t1.Equals(e1));
EXPECT_FALSE(t1.Equals(t2));
ASSERT_EQ(t1.id(), Type::BINARY);
ASSERT_EQ(t1.ToString(), std::string("binary"));
}
TEST(TestStringType, ToString) {
StringType str;
ASSERT_EQ(str.id(), Type::STRING);
ASSERT_EQ(str.ToString(), std::string("string"));
}
TEST(TestFixedSizeBinaryType, ToString) {
auto t = fixed_size_binary(10);
ASSERT_EQ(t->id(), Type::FIXED_SIZE_BINARY);
ASSERT_EQ("fixed_size_binary[10]", t->ToString());
}
TEST(TestFixedSizeBinaryType, Equals) {
auto t1 = fixed_size_binary(10);
auto t2 = fixed_size_binary(10);
auto t3 = fixed_size_binary(3);
ASSERT_TRUE(t1->Equals(t1));
ASSERT_TRUE(t1->Equals(t2));
ASSERT_FALSE(t1->Equals(t3));
}
TEST(TestListType, Basics) {
std::shared_ptr<DataType> vt = std::make_shared<UInt8Type>();
ListType list_type(vt);
ASSERT_EQ(list_type.id(), Type::LIST);
ASSERT_EQ("list", list_type.name());
ASSERT_EQ("list<item: uint8>", list_type.ToString());
ASSERT_EQ(list_type.value_type()->id(), vt->id());
ASSERT_EQ(list_type.value_type()->id(), vt->id());
std::shared_ptr<DataType> st = std::make_shared<StringType>();
std::shared_ptr<DataType> lt = std::make_shared<ListType>(st);
ASSERT_EQ("list<item: string>", lt->ToString());
ListType lt2(lt);
ASSERT_EQ("list<item: list<item: string>>", lt2.ToString());
}
TEST(TestDateTypes, Attrs) {
auto t1 = date32();
auto t2 = date64();
ASSERT_EQ("date32[day]", t1->ToString());
ASSERT_EQ("date64[ms]", t2->ToString());
ASSERT_EQ(32, static_cast<const FixedWidthType&>(*t1).bit_width());
ASSERT_EQ(64, static_cast<const FixedWidthType&>(*t2).bit_width());
}
TEST(TestTimeType, Equals) {
Time32Type t0;
Time32Type t1(TimeUnit::SECOND);
Time32Type t2(TimeUnit::MILLI);
Time64Type t3(TimeUnit::MICRO);
Time64Type t4(TimeUnit::NANO);
Time64Type t5(TimeUnit::MICRO);
ASSERT_EQ(32, t0.bit_width());
ASSERT_EQ(64, t3.bit_width());
ASSERT_TRUE(t0.Equals(t2));
ASSERT_TRUE(t1.Equals(t1));
ASSERT_FALSE(t1.Equals(t3));
ASSERT_FALSE(t3.Equals(t4));
ASSERT_TRUE(t3.Equals(t5));
}
TEST(TestTimeType, ToString) {
auto t1 = time32(TimeUnit::MILLI);
auto t2 = time64(TimeUnit::NANO);
auto t3 = time32(TimeUnit::SECOND);
auto t4 = time64(TimeUnit::MICRO);
ASSERT_EQ("time32[ms]", t1->ToString());
ASSERT_EQ("time64[ns]", t2->ToString());
ASSERT_EQ("time32[s]", t3->ToString());
ASSERT_EQ("time64[us]", t4->ToString());
}
TEST(TestTimestampType, Equals) {
TimestampType t1;
TimestampType t2;
TimestampType t3(TimeUnit::NANO);
TimestampType t4(TimeUnit::NANO);
ASSERT_TRUE(t1.Equals(t2));
ASSERT_FALSE(t1.Equals(t3));
ASSERT_TRUE(t3.Equals(t4));
}
TEST(TestTimestampType, ToString) {
auto t1 = timestamp(TimeUnit::MILLI);
auto t2 = timestamp(TimeUnit::NANO, "US/Eastern");
auto t3 = timestamp(TimeUnit::SECOND);
auto t4 = timestamp(TimeUnit::MICRO);
ASSERT_EQ("timestamp[ms]", t1->ToString());
ASSERT_EQ("timestamp[ns, tz=US/Eastern]", t2->ToString());
ASSERT_EQ("timestamp[s]", t3->ToString());
ASSERT_EQ("timestamp[us]", t4->ToString());
}
TEST(TestNestedType, Equals) {
auto create_struct = [](std::string inner_name,
std::string struct_name) -> shared_ptr<Field> {
auto f_type = field(inner_name, int32());
vector<shared_ptr<Field>> fields = {f_type};
auto s_type = std::make_shared<StructType>(fields);
return field(struct_name, s_type);
};
auto create_union = [](std::string inner_name,
std::string union_name) -> shared_ptr<Field> {
auto f_type = field(inner_name, int32());
vector<shared_ptr<Field>> fields = {f_type};
vector<uint8_t> codes = {Type::INT32};
auto u_type = std::make_shared<UnionType>(fields, codes, UnionMode::SPARSE);
return field(union_name, u_type);
};
auto s0 = create_struct("f0", "s0");
auto s0_other = create_struct("f0", "s0");
auto s0_bad = create_struct("f1", "s0");
auto s1 = create_struct("f1", "s1");
ASSERT_TRUE(s0->Equals(s0_other));
ASSERT_FALSE(s0->Equals(s1));
ASSERT_FALSE(s0->Equals(s0_bad));
auto u0 = create_union("f0", "u0");
auto u0_other = create_union("f0", "u0");
auto u0_bad = create_union("f1", "u0");
auto u1 = create_union("f1", "u1");
ASSERT_TRUE(u0->Equals(u0_other));
ASSERT_FALSE(u0->Equals(u1));
ASSERT_FALSE(u0->Equals(u0_bad));
}
TEST(TestStructType, Basics) {
auto f0_type = int32();
auto f0 = field("f0", f0_type);
auto f1_type = utf8();
auto f1 = field("f1", f1_type);
auto f2_type = uint8();
auto f2 = field("f2", f2_type);
vector<std::shared_ptr<Field>> fields = {f0, f1, f2};
StructType struct_type(fields);
ASSERT_TRUE(struct_type.child(0)->Equals(f0));
ASSERT_TRUE(struct_type.child(1)->Equals(f1));
ASSERT_TRUE(struct_type.child(2)->Equals(f2));
ASSERT_EQ(struct_type.ToString(), "struct<f0: int32, f1: string, f2: uint8>");
// TODO(wesm): out of bounds for field(...)
}
TEST(TypesTest, TestDecimal128Small) {
DecimalType t1(8, 4);
ASSERT_EQ(t1.id(), Type::DECIMAL);
ASSERT_EQ(t1.precision(), 8);
ASSERT_EQ(t1.scale(), 4);
ASSERT_EQ(t1.ToString(), std::string("decimal(8, 4)"));
// Test properties
ASSERT_EQ(t1.byte_width(), 16);
ASSERT_EQ(t1.bit_width(), 128);
}
TEST(TypesTest, TestDecimal128Medium) {
DecimalType t1(12, 5);
ASSERT_EQ(t1.id(), Type::DECIMAL);
ASSERT_EQ(t1.precision(), 12);
ASSERT_EQ(t1.scale(), 5);
ASSERT_EQ(t1.ToString(), std::string("decimal(12, 5)"));
// Test properties
ASSERT_EQ(t1.byte_width(), 16);
ASSERT_EQ(t1.bit_width(), 128);
}
TEST(TypesTest, TestDecimal128Large) {
DecimalType t1(27, 7);
ASSERT_EQ(t1.id(), Type::DECIMAL);
ASSERT_EQ(t1.precision(), 27);
ASSERT_EQ(t1.scale(), 7);
ASSERT_EQ(t1.ToString(), std::string("decimal(27, 7)"));
// Test properties
ASSERT_EQ(t1.byte_width(), 16);
ASSERT_EQ(t1.bit_width(), 128);
}
} // namespace arrow