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
1973 lines (1585 loc) · 66.3 KB
/
Copy pathtype_test.cc
File metadata and controls
1973 lines (1585 loc) · 66.3 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 <algorithm>
#include <cctype>
#include <cstdint>
#include <memory>
#include <string>
#include <unordered_set>
#include <vector>
#include <gmock/gmock.h>
#include "arrow/memory_pool.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/util.h"
#include "arrow/type.h"
#include "arrow/type_traits.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/key_value_metadata.h"
namespace arrow {
using testing::ElementsAre;
using internal::checked_cast;
using internal::checked_pointer_cast;
TEST(TestTypeId, AllTypeIds) {
const auto all_ids = AllTypeIds();
ASSERT_EQ(static_cast<int>(all_ids.size()), Type::MAX_ID);
}
template <typename ReprFunc>
void CheckTypeIdReprs(ReprFunc&& repr_func, bool expect_uppercase) {
std::unordered_set<std::string> unique_reprs;
const auto all_ids = AllTypeIds();
for (const auto id : all_ids) {
std::string repr = repr_func(id);
ASSERT_TRUE(std::all_of(repr.begin(), repr.end(),
[=](const char c) {
return c == '_' || std::isdigit(c) ||
(expect_uppercase ? std::isupper(c)
: std::islower(c));
}))
<< "Invalid type id repr: '" << repr << "'";
unique_reprs.insert(std::move(repr));
}
// No duplicates
ASSERT_EQ(unique_reprs.size(), all_ids.size());
}
TEST(TestTypeId, ToString) {
// Should be all uppercase strings (corresponding to the enum member names)
CheckTypeIdReprs([](Type::type id) { return internal::ToString(id); },
/* expect_uppercase=*/true);
}
TEST(TestTypeId, ToTypeName) {
// Should be all lowercase strings (corresponding to TypeClass::type_name())
CheckTypeIdReprs([](Type::type id) { return internal::ToTypeName(id); },
/* expect_uppercase=*/false);
}
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, ToString) {
auto metadata = key_value_metadata({"foo", "bar"}, {"bizz", "buzz"});
auto f0 = field("f0", int32(), false, metadata);
std::string result = f0->ToString(/*print_metadata=*/true);
std::string expected = R"(f0: int32 not null
-- metadata --
foo: bizz
bar: buzz)";
ASSERT_EQ(expected, result);
result = f0->ToString();
expected = "f0: int32 not null";
ASSERT_EQ(expected, result);
}
TEST(TestField, Equals) {
auto meta1 = key_value_metadata({{"a", "1"}, {"b", "2"}});
// Different from meta1
auto meta2 = key_value_metadata({{"a", "1"}, {"b", "3"}});
// Equal to meta1, though in different order
auto meta3 = key_value_metadata({{"b", "2"}, {"a", "1"}});
Field f0("f0", int32());
Field f0_nn("f0", int32(), false);
Field f0_other("f0", int32());
Field f0_with_meta1("f0", int32(), true, meta1);
Field f0_with_meta2("f0", int32(), true, meta2);
Field f0_with_meta3("f0", int32(), true, meta3);
AssertFieldEqual(f0, f0_other);
AssertFieldNotEqual(f0, f0_nn);
AssertFieldNotEqual(f0, f0_with_meta1, /*check_metadata=*/true);
AssertFieldNotEqual(f0_with_meta1, f0_with_meta2, /*check_metadata=*/true);
AssertFieldEqual(f0_with_meta1, f0_with_meta3, /*check_metadata=*/true);
AssertFieldEqual(f0, f0_with_meta1);
AssertFieldEqual(f0, f0_with_meta2);
AssertFieldEqual(f0_with_meta1, f0_with_meta2);
// operator==(), where check_metadata == false
ASSERT_EQ(f0, f0_other);
ASSERT_NE(f0, f0_nn);
ASSERT_EQ(f0, f0_with_meta1);
ASSERT_EQ(f0_with_meta1, f0_with_meta2);
}
#define ASSERT_COMPATIBLE_IMPL(NAME, TYPE, PLURAL) \
void Assert##NAME##Compatible(const TYPE& left, const TYPE& right) { \
ASSERT_TRUE(left.IsCompatibleWith(right)) \
<< PLURAL << left.ToString() << "' and '" << right.ToString() \
<< "' should be compatible"; \
} \
\
void Assert##NAME##Compatible(const std::shared_ptr<TYPE>& left, \
const std::shared_ptr<TYPE>& right) { \
ASSERT_NE(left, nullptr); \
ASSERT_NE(right, nullptr); \
Assert##NAME##Compatible(*left, *right); \
} \
\
void Assert##NAME##NotCompatible(const TYPE& left, const TYPE& right) { \
ASSERT_FALSE(left.IsCompatibleWith(right)) \
<< PLURAL << left.ToString() << "' and '" << right.ToString() \
<< "' should not be compatible"; \
} \
\
void Assert##NAME##NotCompatible(const std::shared_ptr<TYPE>& left, \
const std::shared_ptr<TYPE>& right) { \
ASSERT_NE(left, nullptr); \
ASSERT_NE(right, nullptr); \
Assert##NAME##NotCompatible(*left, *right); \
}
ASSERT_COMPATIBLE_IMPL(Field, Field, "fields")
#undef ASSERT_COMPATIBLE_IMPL
TEST(TestField, IsCompatibleWith) {
auto meta1 = key_value_metadata({{"a", "1"}, {"b", "2"}});
// Different from meta1
auto meta2 = key_value_metadata({{"a", "1"}, {"b", "3"}});
// Equal to meta1, though in different order
auto meta3 = key_value_metadata({{"b", "2"}, {"a", "1"}});
Field f0("f0", int32());
Field f0_nn("f0", int32(), false);
Field f0_nt("f0", null());
Field f0_other("f0", int32());
Field f0_with_meta1("f0", int32(), true, meta1);
Field f0_with_meta2("f0", int32(), true, meta2);
Field f0_with_meta3("f0", int32(), true, meta3);
Field other("other", int64());
AssertFieldCompatible(f0, f0_other);
AssertFieldCompatible(f0, f0_with_meta1);
AssertFieldCompatible(f0, f0_nn);
AssertFieldCompatible(f0, f0_nt);
AssertFieldCompatible(f0_nt, f0_with_meta1);
AssertFieldCompatible(f0_with_meta1, f0_with_meta2);
AssertFieldCompatible(f0_with_meta1, f0_with_meta3);
AssertFieldNotCompatible(f0, other);
}
TEST(TestField, TestMetadataConstruction) {
auto metadata = key_value_metadata({"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()));
AssertFieldEqual(f0, f1);
}
TEST(TestField, TestWithMetadata) {
auto metadata = key_value_metadata({"foo", "bar"}, {"bizz", "buzz"});
auto f0 = field("f0", int32());
auto f1 = field("f0", int32(), true, metadata);
std::shared_ptr<Field> f2 = f0->WithMetadata(metadata);
AssertFieldEqual(f0, f2);
AssertFieldNotEqual(f0, f2, /*check_metadata=*/true);
AssertFieldEqual(f1, f2);
AssertFieldEqual(f1, f2, /*check_metadata=*/true);
// Ensure pointer equality for zero-copy
ASSERT_EQ(metadata.get(), f1->metadata().get());
}
TEST(TestField, TestWithMergedMetadata) {
auto metadata = key_value_metadata({"foo", "bar"}, {"bizz", "buzz"});
auto f0 = field("f0", int32(), true, metadata);
auto f1 = field("f0", int32());
auto metadata2 = key_value_metadata({"bar", "baz"}, {"bozz", "bazz"});
auto f2 = f0->WithMergedMetadata(metadata2);
auto expected = field("f0", int32(), true, metadata->Merge(*metadata2));
AssertFieldEqual(expected, f2);
auto f3 = f1->WithMergedMetadata(metadata2);
expected = field("f0", int32(), true, metadata2);
AssertFieldEqual(expected, f3);
}
TEST(TestField, TestRemoveMetadata) {
auto metadata = key_value_metadata({"foo", "bar"}, {"bizz", "buzz"});
auto f0 = field("f0", int32());
auto f1 = field("f0", int32(), true, metadata);
std::shared_ptr<Field> f2 = f1->RemoveMetadata();
ASSERT_EQ(f2->metadata(), nullptr);
}
TEST(TestField, TestEmptyMetadata) {
// Empty metadata should be equivalent to no metadata at all
auto metadata1 = key_value_metadata({});
auto metadata2 = key_value_metadata({"foo"}, {"foo value"});
auto f0 = field("f0", int32());
auto f1 = field("f0", int32(), true, metadata1);
auto f2 = field("f0", int32(), true, metadata2);
AssertFieldEqual(f0, f1);
AssertFieldEqual(f0, f2);
AssertFieldEqual(f0, f1, /*check_metadata =*/true);
AssertFieldNotEqual(f0, f2, /*check_metadata =*/true);
}
TEST(TestField, TestFlatten) {
auto metadata = key_value_metadata({"foo", "bar"}, {"bizz", "buzz"});
auto f0 = field("f0", int32(), true /* nullable */, metadata);
auto vec = f0->Flatten();
ASSERT_EQ(vec.size(), 1);
AssertFieldEqual(vec[0], f0);
auto f1 = field("f1", float64(), false /* nullable */);
auto ff = field("nest", struct_({f0, f1}));
vec = ff->Flatten();
ASSERT_EQ(vec.size(), 2);
auto expected0 = field("nest.f0", int32(), true /* nullable */, metadata);
// nullable parent implies nullable flattened child
auto expected1 = field("nest.f1", float64(), true /* nullable */);
AssertFieldEqual(vec[0], expected0);
AssertFieldEqual(vec[1], expected1);
ff = field("nest", struct_({f0, f1}), false /* nullable */);
vec = ff->Flatten();
ASSERT_EQ(vec.size(), 2);
expected0 = field("nest.f0", int32(), true /* nullable */, metadata);
expected1 = field("nest.f1", float64(), false /* nullable */);
AssertFieldEqual(vec[0], expected0);
AssertFieldEqual(vec[1], expected1);
}
TEST(TestField, TestReplacement) {
auto metadata = key_value_metadata({"foo", "bar"}, {"bizz", "buzz"});
auto f0 = field("f0", int32(), true, metadata);
auto fzero = f0->WithType(utf8());
auto f1 = f0->WithName("f1");
AssertFieldNotEqual(f0, fzero);
AssertFieldNotCompatible(f0, fzero);
AssertFieldNotEqual(fzero, f1);
AssertFieldNotCompatible(fzero, f1);
AssertFieldNotEqual(f1, f0);
AssertFieldNotCompatible(f1, f0);
ASSERT_EQ(fzero->name(), "f0");
AssertTypeEqual(fzero->type(), utf8());
ASSERT_TRUE(fzero->metadata()->Equals(*metadata));
ASSERT_EQ(f1->name(), "f1");
AssertTypeEqual(f1->type(), int32());
ASSERT_TRUE(f1->metadata()->Equals(*metadata));
}
TEST(TestField, TestMerge) {
auto metadata1 = key_value_metadata({"foo"}, {"v"});
auto metadata2 = key_value_metadata({"bar"}, {"v"});
{
// different name.
ASSERT_RAISES(Invalid, field("f0", int32())->MergeWith(field("f1", int32())));
}
{
// Same type.
auto f1 = field("f", int32())->WithMetadata(metadata1);
auto f2 = field("f", int32())->WithMetadata(metadata2);
std::shared_ptr<Field> result;
ASSERT_OK_AND_ASSIGN(result, f1->MergeWith(f2));
ASSERT_TRUE(result->Equals(f1));
ASSERT_OK_AND_ASSIGN(result, f2->MergeWith(f1));
ASSERT_TRUE(result->Equals(f2));
}
{
// promote_nullability == false
auto f = field("f", int32());
auto null_field = field("f", null());
Field::MergeOptions options;
options.promote_nullability = false;
ASSERT_RAISES(Invalid, f->MergeWith(null_field, options));
ASSERT_RAISES(Invalid, null_field->MergeWith(f, options));
// Also rejects fields with different nullability.
ASSERT_RAISES(Invalid,
f->WithNullable(true)->MergeWith(f->WithNullable(false), options));
}
{
// promote_nullability == true; merge with a null field.
Field::MergeOptions options;
options.promote_nullability = true;
auto f = field("f", int32())->WithNullable(false)->WithMetadata(metadata1);
auto null_field = field("f", null())->WithMetadata(metadata2);
std::shared_ptr<Field> result;
ASSERT_OK_AND_ASSIGN(result, f->MergeWith(null_field, options));
ASSERT_TRUE(result->Equals(f->WithNullable(true)->WithMetadata(metadata1)));
ASSERT_OK_AND_ASSIGN(result, null_field->MergeWith(f, options));
ASSERT_TRUE(result->Equals(f->WithNullable(true)->WithMetadata(metadata2)));
}
{
// promote_nullability == true; merge a nullable field and a in-nullable field.
Field::MergeOptions options;
options.promote_nullability = true;
auto f1 = field("f", int32())->WithNullable(false);
auto f2 = field("f", int32())->WithNullable(true);
std::shared_ptr<Field> result;
ASSERT_OK_AND_ASSIGN(result, f1->MergeWith(f2, options));
ASSERT_TRUE(result->Equals(f1->WithNullable(true)));
ASSERT_OK_AND_ASSIGN(result, f2->MergeWith(f1, options));
ASSERT_TRUE(result->Equals(f2));
}
}
TEST(TestFieldPath, Basics) {
auto f0 = field("alpha", int32());
auto f1 = field("beta", int32());
auto f2 = field("alpha", int32());
auto f3 = field("beta", int32());
Schema s({f0, f1, f2, f3});
// retrieving a field with single-element FieldPath is equivalent to Schema::field
for (int index = 0; index < s.num_fields(); ++index) {
ASSERT_OK_AND_EQ(s.field(index), FieldPath({index}).Get(s));
}
EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid,
testing::HasSubstr("empty indices cannot be traversed"),
FieldPath().Get(s));
EXPECT_RAISES_WITH_MESSAGE_THAT(IndexError, testing::HasSubstr("index out of range"),
FieldPath({s.num_fields() * 2}).Get(s));
}
TEST(TestFieldRef, Basics) {
auto f0 = field("alpha", int32());
auto f1 = field("beta", int32());
auto f2 = field("alpha", int32());
auto f3 = field("beta", int32());
Schema s({f0, f1, f2, f3});
// lookup by index returns Indices{index}
for (int index = 0; index < s.num_fields(); ++index) {
EXPECT_THAT(FieldRef(index).FindAll(s), ElementsAre(FieldPath{index}));
}
// out of range index results in a failure to match
EXPECT_THAT(FieldRef(s.num_fields() * 2).FindAll(s), ElementsAre());
// lookup by name returns the Indices of both matching fields
EXPECT_THAT(FieldRef("alpha").FindAll(s), ElementsAre(FieldPath{0}, FieldPath{2}));
EXPECT_THAT(FieldRef("beta").FindAll(s), ElementsAre(FieldPath{1}, FieldPath{3}));
}
TEST(TestFieldRef, FromDotPath) {
ASSERT_OK_AND_EQ(FieldRef("alpha"), FieldRef::FromDotPath(R"(.alpha)"));
ASSERT_OK_AND_EQ(FieldRef("", ""), FieldRef::FromDotPath(R"(..)"));
ASSERT_OK_AND_EQ(FieldRef(2), FieldRef::FromDotPath(R"([2])"));
ASSERT_OK_AND_EQ(FieldRef("beta", 3), FieldRef::FromDotPath(R"(.beta[3])"));
ASSERT_OK_AND_EQ(FieldRef(5, "gamma", "delta", 7),
FieldRef::FromDotPath(R"([5].gamma.delta[7])"));
ASSERT_OK_AND_EQ(FieldRef("hello world"), FieldRef::FromDotPath(R"(.hello world)"));
ASSERT_OK_AND_EQ(FieldRef(R"([y]\tho.\)"), FieldRef::FromDotPath(R"(.\[y\]\\tho\.\)"));
ASSERT_OK_AND_EQ(FieldRef(), FieldRef::FromDotPath(R"()"));
ASSERT_RAISES(Invalid, FieldRef::FromDotPath(R"(alpha)"));
ASSERT_RAISES(Invalid, FieldRef::FromDotPath(R"([134234)"));
ASSERT_RAISES(Invalid, FieldRef::FromDotPath(R"([1stuf])"));
}
TEST(TestFieldRef, DotPathRoundTrip) {
auto check_roundtrip = [](const FieldRef& ref) {
auto dot_path = ref.ToDotPath();
ASSERT_OK_AND_EQ(ref, FieldRef::FromDotPath(dot_path));
};
check_roundtrip(FieldRef());
check_roundtrip(FieldRef("foo"));
check_roundtrip(FieldRef("foo", 1, "bar", 2, 3));
check_roundtrip(FieldRef(1, 2, 3));
check_roundtrip(FieldRef("foo", 1, FieldRef("bar", 2, 3), FieldRef()));
}
TEST(TestFieldPath, Nested) {
auto f0 = field("alpha", int32());
auto f1_0 = field("alpha", int32());
auto f1 = field("beta", struct_({f1_0}));
auto f2_0 = field("alpha", int32());
auto f2_1_0 = field("alpha", int32());
auto f2_1_1 = field("alpha", int32());
auto f2_1 = field("gamma", struct_({f2_1_0, f2_1_1}));
auto f2 = field("beta", struct_({f2_0, f2_1}));
Schema s({f0, f1, f2});
// retrieving fields with nested indices
EXPECT_EQ(FieldPath({0}).Get(s), f0);
EXPECT_EQ(FieldPath({1, 0}).Get(s), f1_0);
EXPECT_EQ(FieldPath({2, 0}).Get(s), f2_0);
EXPECT_EQ(FieldPath({2, 1, 0}).Get(s), f2_1_0);
EXPECT_EQ(FieldPath({2, 1, 1}).Get(s), f2_1_1);
}
TEST(TestFieldRef, Nested) {
auto f0 = field("alpha", int32());
auto f1_0 = field("alpha", int32());
auto f1 = field("beta", struct_({f1_0}));
auto f2_0 = field("alpha", int32());
auto f2_1_0 = field("alpha", int32());
auto f2_1_1 = field("alpha", int32());
auto f2_1 = field("gamma", struct_({f2_1_0, f2_1_1}));
auto f2 = field("beta", struct_({f2_0, f2_1}));
Schema s({f0, f1, f2});
EXPECT_THAT(FieldRef("beta", "alpha").FindAll(s),
ElementsAre(FieldPath{1, 0}, FieldPath{2, 0}));
EXPECT_THAT(FieldRef("beta", "gamma", "alpha").FindAll(s),
ElementsAre(FieldPath{2, 1, 0}, FieldPath{2, 1, 1}));
}
TEST(TestFieldRef, Flatten) {
FieldRef ref;
auto assert_name = [](const FieldRef& ref, const std::string& expected) {
ASSERT_TRUE(ref.IsName());
ASSERT_EQ(*ref.name(), expected);
};
auto assert_path = [](const FieldRef& ref, const std::vector<int>& expected) {
ASSERT_TRUE(ref.IsFieldPath());
ASSERT_EQ(ref.field_path()->indices(), expected);
};
auto assert_nested = [](const FieldRef& ref, const std::vector<FieldRef>& expected) {
ASSERT_TRUE(ref.IsNested());
ASSERT_EQ(*ref.nested_refs(), expected);
};
assert_path(FieldRef(), {});
assert_path(FieldRef(1, 2, 3), {1, 2, 3});
// If all leaves are field paths, they are fully flattened
assert_path(FieldRef(1, FieldRef(2, 3)), {1, 2, 3});
assert_path(FieldRef(1, FieldRef(2, 3), FieldRef(), FieldRef(FieldRef(4), FieldRef(5))),
{1, 2, 3, 4, 5});
assert_path(FieldRef(FieldRef(), FieldRef(FieldRef(), FieldRef())), {});
assert_name(FieldRef("foo"), "foo");
// Nested empty field refs are optimized away
assert_nested(FieldRef("foo", 1, FieldRef(), FieldRef(FieldRef(), "bar")),
{FieldRef("foo"), FieldRef(1), FieldRef("bar")});
// For now, subsequences of indices are not concatenated
assert_nested(FieldRef("foo", FieldRef("bar"), FieldRef(1, 2), FieldRef(3)),
{FieldRef("foo"), FieldRef("bar"), FieldRef(1, 2), FieldRef(3)});
}
using TestSchema = ::testing::Test;
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());
AssertFieldEqual(*f0, *schema->field(0));
AssertFieldEqual(*f1, *schema->field(1));
AssertFieldEqual(*f2, *schema->field(2));
auto schema2 = ::arrow::schema({f0, f1, f2});
std::vector<std::shared_ptr<Field>> fields3 = {f0, f1_optional, f2};
auto schema3 = std::make_shared<Schema>(fields3);
AssertSchemaEqual(schema, schema2);
AssertSchemaNotEqual(schema, schema3);
ASSERT_EQ(*schema, *schema2);
ASSERT_NE(*schema, *schema3);
ASSERT_EQ(schema->fingerprint(), schema2->fingerprint());
ASSERT_NE(schema->fingerprint(), schema3->fingerprint());
auto schema4 = ::arrow::schema({f0}, Endianness::Little);
auto schema5 = ::arrow::schema({f0}, Endianness::Little);
auto schema6 = ::arrow::schema({f0}, Endianness::Big);
auto schema7 = ::arrow::schema({f0});
AssertSchemaEqual(schema4, schema5);
AssertSchemaNotEqual(schema4, schema6);
#if ARROW_LITTLE_ENDIAN
AssertSchemaEqual(schema4, schema7);
AssertSchemaNotEqual(schema6, schema7);
#else
AssertSchemaNotEqual(schema4, schema6);
AssertSchemaEqual(schema6, schema7);
#endif
ASSERT_EQ(schema4->fingerprint(), schema5->fingerprint());
ASSERT_NE(schema4->fingerprint(), schema6->fingerprint());
#if ARROW_LITTLE_ENDIAN
ASSERT_EQ(schema4->fingerprint(), schema7->fingerprint());
ASSERT_NE(schema6->fingerprint(), schema7->fingerprint());
#else
ASSERT_NE(schema4->fingerprint(), schema7->fingerprint());
ASSERT_EQ(schema6->fingerprint(), schema7->fingerprint());
#endif
}
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 metadata = key_value_metadata({"foo"}, {"bar"});
auto schema = ::arrow::schema({f0, f1, f2, f3}, metadata);
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);
result = schema->ToString(/*print_metadata=*/true);
std::string expected_with_metadata = expected + R"(
-- metadata --
foo: bar)";
ASSERT_EQ(expected_with_metadata, result);
// With swapped endianness
#if ARROW_LITTLE_ENDIAN
schema = schema->WithEndianness(Endianness::Big);
expected = R"(f0: int32
f1: uint8 not null
f2: string
f3: list<item: int16>
-- endianness: big --)";
#else
schema = schema->WithEndianness(Endianness::Little);
expected = R"(f0: int32
f1: uint8 not null
f2: string
f3: list<item: int16>
-- endianness: little --)";
#endif
result = schema->ToString();
ASSERT_EQ(expected, result);
result = schema->ToString(/*print_metadata=*/true);
expected_with_metadata = expected + R"(
-- metadata --
foo: bar)";
ASSERT_EQ(expected_with_metadata, 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");
AssertFieldEqual(f1, result);
result = schema->GetFieldByName("f3");
AssertFieldEqual(f3, result);
result = schema->GetFieldByName("not-found");
ASSERT_EQ(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, GetFieldDuplicates) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f2 = field("f2", utf8());
auto f3 = field("f1", list(int16()));
auto schema = ::arrow::schema({f0, f1, f2, f3});
ASSERT_EQ(0, schema->GetFieldIndex(f0->name()));
ASSERT_EQ(-1, schema->GetFieldIndex(f1->name())); // duplicate
ASSERT_EQ(2, schema->GetFieldIndex(f2->name()));
ASSERT_EQ(-1, schema->GetFieldIndex("not-found"));
ASSERT_EQ(std::vector<int>{0}, schema->GetAllFieldIndices(f0->name()));
ASSERT_EQ(std::vector<int>({1, 3}), schema->GetAllFieldIndices(f1->name()));
ASSERT_TRUE(::arrow::schema({f0, f1, f2})->HasDistinctFieldNames());
ASSERT_FALSE(schema->HasDistinctFieldNames());
std::vector<std::shared_ptr<Field>> results;
results = schema->GetAllFieldsByName(f0->name());
ASSERT_EQ(results.size(), 1);
AssertFieldEqual(results[0], f0);
results = schema->GetAllFieldsByName(f1->name());
ASSERT_EQ(results.size(), 2);
if (results[0]->type()->id() == Type::UINT8) {
AssertFieldEqual(results[0], f1);
AssertFieldEqual(results[1], f3);
} else {
AssertFieldEqual(results[0], f3);
AssertFieldEqual(results[1], f1);
}
results = schema->GetAllFieldsByName("not-found");
ASSERT_EQ(results.size(), 0);
}
TEST_F(TestSchema, CanReferenceFieldsByNames) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f2 = field("f2", utf8());
auto f3 = field("f1", list(int16()));
auto schema = ::arrow::schema({f0, f1, f2, f3});
ASSERT_OK(schema->CanReferenceFieldsByNames({"f0", "f2"}));
ASSERT_OK(schema->CanReferenceFieldsByNames({"f2", "f0"}));
// Not found
ASSERT_RAISES(Invalid, schema->CanReferenceFieldsByNames({"nope"}));
ASSERT_RAISES(Invalid, schema->CanReferenceFieldsByNames({"f0", "nope"}));
// Duplicates
ASSERT_RAISES(Invalid, schema->CanReferenceFieldsByNames({"f1"}));
ASSERT_RAISES(Invalid, schema->CanReferenceFieldsByNames({"f0", "f1"}));
// Both
ASSERT_RAISES(Invalid, schema->CanReferenceFieldsByNames({"f0", "f1", "nope"}));
}
TEST_F(TestSchema, TestMetadataConstruction) {
auto metadata0 = key_value_metadata({{"foo", "bar"}, {"bizz", "buzz"}});
auto metadata1 = key_value_metadata({{"foo", "baz"}});
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f2 = field("f2", utf8(), true);
auto f3 = field("f2", utf8(), true, metadata1->Copy());
auto schema0 = ::arrow::schema({f0, f1, f2}, metadata0);
auto schema1 = ::arrow::schema({f0, f1, f2}, metadata1);
auto schema2 = ::arrow::schema({f0, f1, f2}, metadata0->Copy());
auto schema3 = ::arrow::schema({f0, f1, f3}, metadata0->Copy());
ASSERT_TRUE(metadata0->Equals(*schema0->metadata()));
ASSERT_TRUE(metadata1->Equals(*schema1->metadata()));
ASSERT_TRUE(metadata0->Equals(*schema2->metadata()));
AssertSchemaEqual(schema0, schema2);
AssertSchemaEqual(schema0, schema1);
AssertSchemaNotEqual(schema0, schema1, /*check_metadata=*/true);
AssertSchemaEqual(schema2, schema1);
AssertSchemaNotEqual(schema2, schema1, /*check_metadata=*/true);
// Field has different metatadata
AssertSchemaEqual(schema2, schema3);
AssertSchemaNotEqual(schema2, schema3, /*check_metadata=*/true);
ASSERT_EQ(schema0->fingerprint(), schema1->fingerprint());
ASSERT_EQ(schema0->fingerprint(), schema2->fingerprint());
ASSERT_EQ(schema0->fingerprint(), schema3->fingerprint());
ASSERT_NE(schema0->metadata_fingerprint(), schema1->metadata_fingerprint());
ASSERT_EQ(schema0->metadata_fingerprint(), schema2->metadata_fingerprint());
ASSERT_NE(schema0->metadata_fingerprint(), schema3->metadata_fingerprint());
}
TEST_F(TestSchema, TestNestedMetadataComparison) {
auto item0 = field("item", int32(), true);
auto item1 = field("item", int32(), true, key_value_metadata({{"foo", "baz"}}));
Schema schema0({field("f", list(item0))});
Schema schema1({field("f", list(item1))});
ASSERT_EQ(schema0.fingerprint(), schema1.fingerprint());
ASSERT_NE(schema0.metadata_fingerprint(), schema1.metadata_fingerprint());
AssertSchemaEqual(schema0, schema1);
AssertSchemaNotEqual(schema0, schema1, /* check_metadata = */ true);
}
TEST_F(TestSchema, TestDeeplyNestedMetadataComparison) {
auto item0 = field("item", int32(), true);
auto item1 = field("item", int32(), true, key_value_metadata({{"foo", "baz"}}));
Schema schema0(
{field("f", list(list(sparse_union({field("struct", struct_({item0}))}))))});
Schema schema1(
{field("f", list(list(sparse_union({field("struct", struct_({item1}))}))))});
ASSERT_EQ(schema0.fingerprint(), schema1.fingerprint());
ASSERT_NE(schema0.metadata_fingerprint(), schema1.metadata_fingerprint());
AssertSchemaEqual(schema0, schema1);
AssertSchemaNotEqual(schema0, schema1, /* check_metadata = */ true);
}
TEST_F(TestSchema, TestFieldsDifferOnlyInMetadata) {
auto f0 = field("f", utf8(), true, nullptr);
auto f1 = field("f", utf8(), true, key_value_metadata({{"foo", "baz"}}));
Schema schema0({f0, f1});
Schema schema1({f1, f0});
AssertSchemaEqual(schema0, schema1);
AssertSchemaNotEqual(schema0, schema1, /* check_metadata = */ true);
ASSERT_EQ(schema0.fingerprint(), schema1.fingerprint());
ASSERT_NE(schema0.metadata_fingerprint(), schema1.metadata_fingerprint());
}
TEST_F(TestSchema, TestEmptyMetadata) {
// Empty metadata should be equivalent to no metadata at all
auto f1 = field("f1", int32());
auto metadata1 = key_value_metadata({});
auto metadata2 = key_value_metadata({"foo"}, {"foo value"});
auto schema1 = ::arrow::schema({f1});
auto schema2 = ::arrow::schema({f1}, metadata1);
auto schema3 = ::arrow::schema({f1}, metadata2);
AssertSchemaEqual(schema1, schema2);
AssertSchemaNotEqual(schema1, schema3, /*check_metadata=*/true);
ASSERT_EQ(schema1->fingerprint(), schema2->fingerprint());
ASSERT_EQ(schema1->fingerprint(), schema3->fingerprint());
ASSERT_EQ(schema1->metadata_fingerprint(), schema2->metadata_fingerprint());
ASSERT_NE(schema1->metadata_fingerprint(), schema3->metadata_fingerprint());
}
TEST_F(TestSchema, TestWithMetadata) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f2 = field("f2", utf8());
std::vector<std::shared_ptr<Field>> fields = {f0, f1, f2};
auto metadata = key_value_metadata({"foo", "bar"}, {"bizz", "buzz"});
auto schema = std::make_shared<Schema>(fields);
std::shared_ptr<Schema> new_schema = schema->WithMetadata(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());
std::vector<std::shared_ptr<Field>> fields = {f0, f1, f2};
auto schema = std::make_shared<Schema>(fields);
std::shared_ptr<Schema> new_schema = schema->RemoveMetadata();
ASSERT_TRUE(new_schema->metadata() == nullptr);
}
void AssertSchemaBuilderYield(const SchemaBuilder& builder,
const std::shared_ptr<Schema>& expected) {
ASSERT_OK_AND_ASSIGN(auto schema, builder.Finish());
AssertSchemaEqual(schema, expected);
}
TEST(TestSchemaBuilder, DefaultBehavior) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f2 = field("f2", utf8());
SchemaBuilder builder;
ASSERT_OK(builder.AddField(f0));
ASSERT_OK(builder.AddField(f1));
ASSERT_OK(builder.AddField(f2));
AssertSchemaBuilderYield(builder, schema({f0, f1, f2}));
builder.Reset();
ASSERT_OK(builder.AddFields({f0, f1, f2->WithNullable(false)}));
AssertSchemaBuilderYield(builder, schema({f0, f1, f2->WithNullable(false)}));
builder.Reset();
ASSERT_OK(builder.AddSchema(schema({f2, f0})));
AssertSchemaBuilderYield(builder, schema({f2, f0}));
builder.Reset();
ASSERT_OK(builder.AddSchemas({schema({f1, f2}), schema({f2, f0})}));
AssertSchemaBuilderYield(builder, schema({f1, f2, f2, f0}));
}
TEST(TestSchemaBuilder, WithMetadata) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto metadata = key_value_metadata({{"foo", "bar"}});
SchemaBuilder builder;
ASSERT_OK(builder.AddMetadata(*metadata));
ASSERT_OK_AND_ASSIGN(auto schema, builder.Finish());
AssertSchemaEqual(schema, ::arrow::schema({})->WithMetadata(metadata));
ASSERT_OK(builder.AddField(f0));
ASSERT_OK_AND_ASSIGN(schema, builder.Finish());
AssertSchemaEqual(schema, ::arrow::schema({f0})->WithMetadata(metadata));
SchemaBuilder other_builder{::arrow::schema({})->WithMetadata(metadata)};
ASSERT_OK(other_builder.AddField(f1));
ASSERT_OK_AND_ASSIGN(schema, other_builder.Finish());
AssertSchemaEqual(schema, ::arrow::schema({f1})->WithMetadata(metadata));
other_builder.Reset();
ASSERT_OK(other_builder.AddField(f1->WithMetadata(metadata)));
ASSERT_OK_AND_ASSIGN(schema, other_builder.Finish());
AssertSchemaEqual(schema, ::arrow::schema({f1->WithMetadata(metadata)}));
}
TEST(TestSchemaBuilder, IncrementalConstruction) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f2 = field("f2", utf8());
SchemaBuilder builder;
std::shared_ptr<Schema> actual;
ASSERT_OK_AND_ASSIGN(actual, builder.Finish());
AssertSchemaEqual(actual, ::arrow::schema({}));
ASSERT_OK(builder.AddField(f0));
ASSERT_OK_AND_ASSIGN(actual, builder.Finish());
AssertSchemaEqual(actual, ::arrow::schema({f0}));
ASSERT_OK(builder.AddField(f1));
ASSERT_OK_AND_ASSIGN(actual, builder.Finish());
AssertSchemaEqual(actual, ::arrow::schema({f0, f1}));
ASSERT_OK(builder.AddField(f2));
AssertSchemaBuilderYield(builder, schema({f0, f1, f2}));
}
TEST(TestSchemaBuilder, PolicyIgnore) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8());
auto f0_req = field("f0", utf8(), false);
SchemaBuilder builder{SchemaBuilder::CONFLICT_IGNORE};
ASSERT_OK(builder.AddFields({f0, f1}));
AssertSchemaBuilderYield(builder, schema({f0, f1}));
ASSERT_OK(builder.AddField(f0_req));
AssertSchemaBuilderYield(builder, schema({f0, f1}));
ASSERT_OK(builder.AddField(f0));
AssertSchemaBuilderYield(builder, schema({f0, f1}));
}
TEST(TestSchemaBuilder, PolicyReplace) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8());
auto f0_req = field("f0", utf8(), false);
SchemaBuilder builder{SchemaBuilder::CONFLICT_REPLACE};
ASSERT_OK(builder.AddFields({f0, f1}));
AssertSchemaBuilderYield(builder, schema({f0, f1}));
ASSERT_OK(builder.AddField(f0_req));
AssertSchemaBuilderYield(builder, schema({f0_req, f1}));
ASSERT_OK(builder.AddField(f0));
AssertSchemaBuilderYield(builder, schema({f0, f1}));
}
TEST(TestSchemaBuilder, PolicyMerge) {
auto f0 = field("f0", int32(), true);
auto f1 = field("f1", uint8());
// Same as f0, but not required.
auto f0_opt = field("f0", int32());
// Another type, can't merge
auto f0_other = field("f0", utf8(), false);
SchemaBuilder builder{SchemaBuilder::CONFLICT_MERGE};
ASSERT_OK(builder.AddFields({f0, f1}));
AssertSchemaBuilderYield(builder, schema({f0, f1}));
ASSERT_OK(builder.AddField(f0_opt));
AssertSchemaBuilderYield(builder, schema({f0_opt, f1}));
// Unsupported merge with a different type
ASSERT_RAISES(Invalid, builder.AddField(f0_other));
// Builder should still contain state
AssertSchemaBuilderYield(builder, schema({f0, f1}));
builder.Reset();
// Create a schema with duplicate fields
builder.SetPolicy(SchemaBuilder::CONFLICT_APPEND);
ASSERT_OK(builder.AddFields({f0, f0}));
builder.SetPolicy(SchemaBuilder::CONFLICT_MERGE);
// Even if the field is compatible, it can't know with which field to merge.
ASSERT_RAISES(Invalid, builder.AddField(f0_opt));
AssertSchemaBuilderYield(builder, schema({f0, f0}));
}
TEST(TestSchemaBuilder, PolicyError) {
auto f0 = field("f0", int32(), true);
auto f1 = field("f1", uint8());
// Same as f0, but not required.
auto f0_opt = field("f0", int32());
// Another type, can't merge
auto f0_other = field("f0", utf8(), false);
SchemaBuilder builder{SchemaBuilder::CONFLICT_ERROR};
ASSERT_OK(builder.AddFields({f0, f1}));
AssertSchemaBuilderYield(builder, schema({f0, f1}));
ASSERT_RAISES(Invalid, builder.AddField(f0));
ASSERT_RAISES(Invalid, builder.AddField(f0_opt));
ASSERT_RAISES(Invalid, builder.AddField(f0_other));
AssertSchemaBuilderYield(builder, schema({f0, f1}));
}
TEST(TestSchemaBuilder, Merge) {
auto f0 = field("f0", int32(), true);
auto f1 = field("f1", uint8());
// Same as f0, but not required.