forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_binary_test.cc
More file actions
913 lines (721 loc) · 28.6 KB
/
Copy patharray_binary_test.cc
File metadata and controls
913 lines (721 loc) · 28.6 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
// 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 <cstdint>
#include <cstring>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>
#include "arrow/array.h"
#include "arrow/array/builder_binary.h"
#include "arrow/buffer.h"
#include "arrow/memory_pool.h"
#include "arrow/status.h"
#include "arrow/testing/builder.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/util.h"
#include "arrow/type.h"
#include "arrow/type_traits.h"
#include "arrow/util/bit_util.h"
#include "arrow/util/bitmap_builders.h"
#include "arrow/util/checked_cast.h"
#include "arrow/visit_data_inline.h"
namespace arrow {
using internal::checked_cast;
// ----------------------------------------------------------------------
// String / Binary tests
template <typename ArrayType>
void CheckStringArray(const ArrayType& array, const std::vector<std::string>& strings,
const std::vector<uint8_t>& is_valid, int repeats = 1) {
int64_t length = array.length();
int64_t base_length = static_cast<int64_t>(strings.size());
ASSERT_EQ(base_length, static_cast<int64_t>(is_valid.size()));
ASSERT_EQ(base_length * repeats, length);
int32_t value_pos = 0;
for (int i = 0; i < length; ++i) {
auto j = i % base_length;
if (is_valid[j]) {
ASSERT_FALSE(array.IsNull(i));
auto view = array.GetView(i);
ASSERT_EQ(value_pos, array.value_offset(i));
ASSERT_EQ(strings[j].size(), view.size());
ASSERT_EQ(std::string_view(strings[j]), view);
value_pos += static_cast<int32_t>(view.size());
} else {
ASSERT_TRUE(array.IsNull(i));
}
}
}
template <typename T>
class TestStringArray : public ::testing::Test {
public:
using TypeClass = T;
using offset_type = typename TypeClass::offset_type;
using ArrayType = typename TypeTraits<TypeClass>::ArrayType;
using BuilderType = typename TypeTraits<TypeClass>::BuilderType;
void SetUp() {
chars_ = {'a', 'b', 'b', 'c', 'c', 'c'};
offsets_ = {0, 1, 1, 1, 3, 6};
valid_bytes_ = {1, 1, 0, 1, 1};
expected_ = {"a", "", "", "bb", "ccc"};
MakeArray();
}
void MakeArray() {
length_ = static_cast<int64_t>(offsets_.size()) - 1;
value_buf_ = Buffer::Wrap(chars_);
offsets_buf_ = Buffer::Wrap(offsets_);
ASSERT_OK_AND_ASSIGN(null_bitmap_, internal::BytesToBits(valid_bytes_));
null_count_ = CountNulls(valid_bytes_);
strings_ = std::make_shared<ArrayType>(length_, offsets_buf_, value_buf_,
null_bitmap_, null_count_);
}
void TestArrayBasics() {
ASSERT_EQ(length_, strings_->length());
ASSERT_EQ(1, strings_->null_count());
ASSERT_OK(strings_->ValidateFull());
TestInitialized(*strings_);
AssertZeroPadded(*strings_);
}
void TestArrayIndexOperator() {
const auto& arr = *strings_;
for (int64_t i = 0; i < arr.length(); ++i) {
if (valid_bytes_[i]) {
ASSERT_TRUE(arr[i].has_value());
ASSERT_EQ(expected_[i], arr[i].value());
} else {
ASSERT_FALSE(arr[i].has_value());
}
}
}
void TestArrayCtors() {
// ARROW-8863: ArrayData::null_count set to 0 when no validity bitmap
// provided
ArrayType arr(length_, offsets_buf_, value_buf_);
ASSERT_EQ(arr.data()->null_count, 0);
}
void TestTotalValuesLength() {
auto ty = TypeTraits<T>::type_singleton();
auto arr = ArrayFromJSON(ty, R"(["a", null, "bbb", "cccc", "ddddd"])");
offset_type values_length = arr.total_values_length();
ASSERT_EQ(values_length, static_cast<offset_type>(13));
offset_type sliced_values_length =
checked_cast<const ArrayType&>(*arr.Slice(3)).total_values_length();
ASSERT_EQ(sliced_values_length, static_cast<offset_type>(9));
// Zero-length array is a special case
offset_type zero_size_length =
checked_cast<const ArrayType&>(*arr.Slice(0, 0)).total_values_length();
ASSERT_EQ(zero_size_length, static_cast<offset_type>(0));
}
void TestType() {
std::shared_ptr<DataType> type = this->strings_->type();
if (std::is_same<TypeClass, StringType>::value) {
ASSERT_EQ(Type::STRING, type->id());
ASSERT_EQ(Type::STRING, this->strings_->type_id());
} else if (std::is_same<TypeClass, LargeStringType>::value) {
ASSERT_EQ(Type::LARGE_STRING, type->id());
ASSERT_EQ(Type::LARGE_STRING, this->strings_->type_id());
} else if (std::is_same<TypeClass, BinaryType>::value) {
ASSERT_EQ(Type::BINARY, type->id());
ASSERT_EQ(Type::BINARY, this->strings_->type_id());
} else if (std::is_same<TypeClass, LargeBinaryType>::value) {
ASSERT_EQ(Type::LARGE_BINARY, type->id());
ASSERT_EQ(Type::LARGE_BINARY, this->strings_->type_id());
} else {
FAIL();
}
}
void TestListFunctions() {
int64_t pos = 0;
for (size_t i = 0; i < expected_.size(); ++i) {
ASSERT_EQ(pos, strings_->value_offset(i));
ASSERT_EQ(expected_[i].size(), strings_->value_length(i));
pos += expected_[i].size();
}
}
void TestDestructor() {
auto arr = std::make_shared<ArrayType>(length_, offsets_buf_, value_buf_,
null_bitmap_, null_count_);
}
void TestGetString() {
for (size_t i = 0; i < expected_.size(); ++i) {
if (valid_bytes_[i] == 0) {
ASSERT_TRUE(strings_->IsNull(i));
} else {
ASSERT_FALSE(strings_->IsNull(i));
ASSERT_EQ(expected_[i], strings_->GetString(i));
}
}
}
void TestEmptyStringComparison() {
offsets_ = {0, 0, 0, 0, 0, 0};
offsets_buf_ = Buffer::Wrap(offsets_);
length_ = static_cast<int64_t>(offsets_.size() - 1);
auto strings_a = std::make_shared<ArrayType>(length_, offsets_buf_, nullptr,
null_bitmap_, null_count_);
auto strings_b = std::make_shared<ArrayType>(length_, offsets_buf_, nullptr,
null_bitmap_, null_count_);
ASSERT_TRUE(strings_a->Equals(strings_b));
}
void TestCompareNullByteSlots() {
BuilderType builder;
BuilderType builder2;
BuilderType builder3;
ASSERT_OK(builder.Append("foo"));
ASSERT_OK(builder2.Append("foo"));
ASSERT_OK(builder3.Append("foo"));
ASSERT_OK(builder.Append("bar"));
ASSERT_OK(builder2.AppendNull());
// same length, but different
ASSERT_OK(builder3.Append("xyz"));
ASSERT_OK(builder.Append("baz"));
ASSERT_OK(builder2.Append("baz"));
ASSERT_OK(builder3.Append("baz"));
std::shared_ptr<Array> array, array2, array3;
FinishAndCheckPadding(&builder, &array);
ASSERT_OK(builder2.Finish(&array2));
ASSERT_OK(builder3.Finish(&array3));
const auto& a1 = checked_cast<const ArrayType&>(*array);
const auto& a2 = checked_cast<const ArrayType&>(*array2);
const auto& a3 = checked_cast<const ArrayType&>(*array3);
// The validity bitmaps are the same, the data is different, but the unequal
// portion is masked out
ArrayType equal_array(3, a1.value_offsets(), a1.value_data(), a2.null_bitmap(), 1);
ArrayType equal_array2(3, a3.value_offsets(), a3.value_data(), a2.null_bitmap(), 1);
ASSERT_TRUE(equal_array.Equals(equal_array2));
ASSERT_TRUE(a2.RangeEquals(equal_array2, 0, 3, 0));
ASSERT_TRUE(equal_array.Array::Slice(1)->Equals(equal_array2.Array::Slice(1)));
ASSERT_TRUE(
equal_array.Array::Slice(1)->RangeEquals(0, 2, 0, equal_array2.Array::Slice(1)));
}
void TestSliceGetString() {
BuilderType builder;
ASSERT_OK(builder.Append("a"));
ASSERT_OK(builder.Append("b"));
ASSERT_OK(builder.Append("c"));
std::shared_ptr<Array> array;
ASSERT_OK(builder.Finish(&array));
auto s = array->Slice(1, 10);
auto arr = std::dynamic_pointer_cast<ArrayType>(s);
ASSERT_EQ(arr->GetString(0), "b");
}
Status ValidateFull(int64_t length, std::vector<offset_type> offsets,
std::string_view data, int64_t offset = 0) {
ArrayType arr(length, Buffer::Wrap(offsets), std::make_shared<Buffer>(data),
/*null_bitmap=*/nullptr, /*null_count=*/0, offset);
return arr.ValidateFull();
}
Status ValidateFull(const std::string& json) {
auto ty = TypeTraits<T>::type_singleton();
auto arr = ArrayFromJSON(ty, json);
return arr->ValidateFull();
}
void TestValidateOffsets() {
ASSERT_OK(ValidateFull(0, {0}, ""));
ASSERT_OK(ValidateFull(1, {0, 4}, "data"));
ASSERT_OK(ValidateFull(2, {0, 4, 4}, "data"));
ASSERT_OK(ValidateFull(2, {0, 5, 9}, "some data"));
// Non-zero array offset
ASSERT_OK(ValidateFull(0, {0, 4}, "data", 1));
ASSERT_OK(ValidateFull(1, {0, 5, 9}, "some data", 1));
ASSERT_OK(ValidateFull(0, {0, 5, 9}, "some data", 2));
// Not enough offsets
ASSERT_RAISES(Invalid, ValidateFull(1, {}, ""));
ASSERT_RAISES(Invalid, ValidateFull(1, {0}, ""));
ASSERT_RAISES(Invalid, ValidateFull(2, {0, 4}, "data"));
ASSERT_RAISES(Invalid, ValidateFull(1, {0, 4}, "data", 1));
// Offset out of bounds
ASSERT_RAISES(Invalid, ValidateFull(1, {0, 5}, "data"));
// Negative offset
ASSERT_RAISES(Invalid, ValidateFull(1, {-1, 0}, "data"));
ASSERT_RAISES(Invalid, ValidateFull(1, {0, -1}, "data"));
ASSERT_RAISES(Invalid, ValidateFull(1, {0, -1, -1}, "data", 1));
// Offsets non-monotonic
ASSERT_RAISES(Invalid, ValidateFull(2, {0, 5, 4}, "some data"));
}
void TestValidateData() {
// Valid UTF8
ASSERT_OK(ValidateFull(R"(["Voix", "ambiguë", "d’un", "cœur"])"));
ASSERT_OK(ValidateFull(R"(["いろはにほへと", "ちりぬるを", "わかよたれそ"])"));
ASSERT_OK(ValidateFull(R"(["😀", "😄"])"));
ASSERT_OK(ValidateFull(1, {0, 4}, "\xf4\x8f\xbf\xbf")); // \U0010ffff
// Invalid UTF8
auto ty = TypeTraits<T>::type_singleton();
auto st1 = ValidateFull(3, {0, 4, 6, 9}, "abc \xff def");
// Hypothetical \U00110000
auto st2 = ValidateFull(1, {0, 4}, "\xf4\x90\x80\x80");
// Single UTF8 character straddles two entries
auto st3 = ValidateFull(2, {0, 1, 2}, "\xc3\xa9");
if (T::is_utf8) {
ASSERT_RAISES(Invalid, st1);
ASSERT_RAISES(Invalid, st2);
ASSERT_RAISES(Invalid, st3);
} else {
ASSERT_OK(st1);
ASSERT_OK(st2);
ASSERT_OK(st3);
}
}
protected:
std::vector<offset_type> offsets_;
std::vector<char> chars_;
std::vector<uint8_t> valid_bytes_;
std::vector<std::string> expected_;
std::shared_ptr<Buffer> value_buf_;
std::shared_ptr<Buffer> offsets_buf_;
std::shared_ptr<Buffer> null_bitmap_;
int64_t null_count_;
int64_t length_;
std::shared_ptr<ArrayType> strings_;
};
TYPED_TEST_SUITE(TestStringArray, BaseBinaryArrowTypes);
TYPED_TEST(TestStringArray, TestArrayBasics) { this->TestArrayBasics(); }
TYPED_TEST(TestStringArray, TestArrayIndexOperator) { this->TestArrayIndexOperator(); }
TYPED_TEST(TestStringArray, TestArrayCtors) { this->TestArrayCtors(); }
TYPED_TEST(TestStringArray, TestType) { this->TestType(); }
TYPED_TEST(TestStringArray, TestListFunctions) { this->TestListFunctions(); }
TYPED_TEST(TestStringArray, TestDestructor) { this->TestDestructor(); }
TYPED_TEST(TestStringArray, TestGetString) { this->TestGetString(); }
TYPED_TEST(TestStringArray, TestEmptyStringComparison) {
this->TestEmptyStringComparison();
}
TYPED_TEST(TestStringArray, CompareNullByteSlots) { this->TestCompareNullByteSlots(); }
TYPED_TEST(TestStringArray, TestSliceGetString) { this->TestSliceGetString(); }
TYPED_TEST(TestStringArray, TestValidateOffsets) { this->TestValidateOffsets(); }
TYPED_TEST(TestStringArray, TestValidateData) { this->TestValidateData(); }
template <typename T>
class TestUTF8Array : public ::testing::Test {
public:
using TypeClass = T;
using offset_type = typename TypeClass::offset_type;
using ArrayType = typename TypeTraits<TypeClass>::ArrayType;
Status ValidateUTF8(int64_t length, std::vector<offset_type> offsets,
std::string_view data, int64_t offset = 0) {
ArrayType arr(length, Buffer::Wrap(offsets), std::make_shared<Buffer>(data),
/*null_bitmap=*/nullptr, /*null_count=*/0, offset);
return arr.ValidateUTF8();
}
Status ValidateUTF8(const std::string& json) {
auto ty = TypeTraits<T>::type_singleton();
auto arr = ArrayFromJSON(ty, json);
return checked_cast<const ArrayType&>(*arr).ValidateUTF8();
}
void TestValidateUTF8() {
ASSERT_OK(ValidateUTF8(R"(["Voix", "ambiguë", "d’un", "cœur"])"));
ASSERT_OK(ValidateUTF8(1, {0, 4}, "\xf4\x8f\xbf\xbf")); // \U0010ffff
ASSERT_RAISES(Invalid, ValidateUTF8(1, {0, 1}, "\xf4"));
// More tests in TestValidateData() above
// (ValidateFull() calls ValidateUTF8() internally)
}
};
TYPED_TEST_SUITE(TestUTF8Array, StringArrowTypes);
TYPED_TEST(TestUTF8Array, TestValidateUTF8) { this->TestValidateUTF8(); }
// ----------------------------------------------------------------------
// String builder tests
template <typename T>
class TestStringBuilder : public ::testing::Test {
public:
using TypeClass = T;
using offset_type = typename TypeClass::offset_type;
using ArrayType = typename TypeTraits<TypeClass>::ArrayType;
using BuilderType = typename TypeTraits<TypeClass>::BuilderType;
void SetUp() { builder_.reset(new BuilderType(pool_)); }
void Done() {
std::shared_ptr<Array> out;
FinishAndCheckPadding(builder_.get(), &out);
result_ = std::dynamic_pointer_cast<ArrayType>(out);
ASSERT_OK(result_->ValidateFull());
}
void TestScalarAppend() {
std::vector<std::string> strings = {"", "bb", "a", "", "ccc"};
std::vector<uint8_t> is_valid = {1, 1, 1, 0, 1};
int N = static_cast<int>(strings.size());
int reps = 10;
for (int j = 0; j < reps; ++j) {
for (int i = 0; i < N; ++i) {
if (!is_valid[i]) {
ASSERT_OK(builder_->AppendNull());
} else {
ASSERT_OK(builder_->Append(strings[i]));
}
}
}
Done();
ASSERT_EQ(reps * N, result_->length());
ASSERT_EQ(reps, result_->null_count());
ASSERT_EQ(reps * 6, result_->value_data()->size());
CheckStringArray(*result_, strings, is_valid, reps);
}
void TestScalarAppendUnsafe() {
std::vector<std::string> strings = {"", "bb", "a", "", "ccc"};
std::vector<uint8_t> is_valid = {1, 1, 1, 0, 1};
int N = static_cast<int>(strings.size());
int reps = 13;
int64_t total_length = 0;
for (const auto& s : strings) {
total_length += static_cast<int64_t>(s.size());
}
ASSERT_OK(builder_->Reserve(N * reps));
ASSERT_OK(builder_->ReserveData(total_length * reps));
for (int j = 0; j < reps; ++j) {
for (int i = 0; i < N; ++i) {
if (!is_valid[i]) {
builder_->UnsafeAppendNull();
} else {
builder_->UnsafeAppend(strings[i]);
}
}
}
ASSERT_EQ(builder_->value_data_length(), total_length * reps);
Done();
ASSERT_OK(result_->ValidateFull());
ASSERT_EQ(reps * N, result_->length());
ASSERT_EQ(reps, result_->null_count());
ASSERT_EQ(reps * total_length, result_->value_data()->size());
CheckStringArray(*result_, strings, is_valid, reps);
}
void TestExtendCurrent() {
std::vector<std::string> strings = {"", "bbbb", "aaaaa", "", "ccc"};
std::vector<uint8_t> is_valid = {1, 1, 1, 0, 1};
int N = static_cast<int>(strings.size());
int reps = 10;
for (int j = 0; j < reps; ++j) {
for (int i = 0; i < N; ++i) {
if (!is_valid[i]) {
ASSERT_OK(builder_->AppendNull());
} else if (strings[i].length() > 3) {
ASSERT_OK(builder_->Append(strings[i].substr(0, 3)));
ASSERT_OK(builder_->ExtendCurrent(strings[i].substr(3)));
} else {
ASSERT_OK(builder_->Append(strings[i]));
}
}
}
Done();
ASSERT_EQ(reps * N, result_->length());
ASSERT_EQ(reps, result_->null_count());
ASSERT_EQ(reps * 12, result_->value_data()->size());
CheckStringArray(*result_, strings, is_valid, reps);
}
void TestExtendCurrentUnsafe() {
std::vector<std::string> strings = {"", "bbbb", "aaaaa", "", "ccc"};
std::vector<uint8_t> is_valid = {1, 1, 1, 0, 1};
int N = static_cast<int>(strings.size());
int reps = 13;
int64_t total_length = 0;
for (const auto& s : strings) {
total_length += static_cast<int64_t>(s.size());
}
ASSERT_OK(builder_->Reserve(N * reps));
ASSERT_OK(builder_->ReserveData(total_length * reps));
for (int j = 0; j < reps; ++j) {
for (int i = 0; i < N; ++i) {
if (!is_valid[i]) {
builder_->UnsafeAppendNull();
} else if (strings[i].length() > 3) {
builder_->UnsafeAppend(strings[i].substr(0, 3));
builder_->UnsafeExtendCurrent(strings[i].substr(3));
} else {
builder_->UnsafeAppend(strings[i]);
}
}
}
ASSERT_EQ(builder_->value_data_length(), total_length * reps);
Done();
ASSERT_EQ(reps * N, result_->length());
ASSERT_EQ(reps, result_->null_count());
ASSERT_EQ(reps * 12, result_->value_data()->size());
CheckStringArray(*result_, strings, is_valid, reps);
}
void TestVectorAppend() {
std::vector<std::string> strings = {"", "bb", "a", "", "ccc"};
std::vector<uint8_t> valid_bytes = {1, 1, 1, 0, 1};
int N = static_cast<int>(strings.size());
int reps = 1000;
for (int j = 0; j < reps; ++j) {
ASSERT_OK(builder_->AppendValues(strings, valid_bytes.data()));
}
Done();
ASSERT_EQ(reps * N, result_->length());
ASSERT_EQ(reps, result_->null_count());
ASSERT_EQ(reps * 6, result_->value_data()->size());
CheckStringArray(*result_, strings, valid_bytes, reps);
}
void TestAppendCStringsWithValidBytes() {
const char* strings[] = {nullptr, "aaa", nullptr, "ignored", ""};
std::vector<uint8_t> valid_bytes = {1, 1, 1, 0, 1};
int N = static_cast<int>(sizeof(strings) / sizeof(strings[0]));
int reps = 1000;
for (int j = 0; j < reps; ++j) {
ASSERT_OK(builder_->AppendValues(strings, N, valid_bytes.data()));
}
Done();
ASSERT_EQ(reps * N, result_->length());
ASSERT_EQ(reps * 3, result_->null_count());
ASSERT_EQ(reps * 3, result_->value_data()->size());
CheckStringArray(*result_, {"", "aaa", "", "", ""}, {0, 1, 0, 0, 1}, reps);
}
void TestAppendCStringsWithoutValidBytes() {
const char* strings[] = {"", "bb", "a", nullptr, "ccc"};
int N = static_cast<int>(sizeof(strings) / sizeof(strings[0]));
int reps = 1000;
for (int j = 0; j < reps; ++j) {
ASSERT_OK(builder_->AppendValues(strings, N));
}
Done();
ASSERT_EQ(reps * N, result_->length());
ASSERT_EQ(reps, result_->null_count());
ASSERT_EQ(reps * 6, result_->value_data()->size());
CheckStringArray(*result_, {"", "bb", "a", "", "ccc"}, {1, 1, 1, 0, 1}, reps);
}
void TestCapacityReserve() {
std::vector<std::string> strings = {"aaaaa", "bbbbbbbbbb", "ccccccccccccccc",
"dddddddddd"};
int N = static_cast<int>(strings.size());
int reps = 15;
int64_t length = 0;
int64_t capacity = 1000;
int64_t expected_capacity = bit_util::RoundUpToMultipleOf64(capacity);
ASSERT_OK(builder_->ReserveData(capacity));
ASSERT_EQ(length, builder_->value_data_length());
ASSERT_EQ(expected_capacity, builder_->value_data_capacity());
for (int j = 0; j < reps; ++j) {
for (int i = 0; i < N; ++i) {
ASSERT_OK(builder_->Append(strings[i]));
length += static_cast<int64_t>(strings[i].size());
ASSERT_EQ(length, builder_->value_data_length());
ASSERT_EQ(expected_capacity, builder_->value_data_capacity());
}
}
int extra_capacity = 500;
expected_capacity = bit_util::RoundUpToMultipleOf64(length + extra_capacity);
ASSERT_OK(builder_->ReserveData(extra_capacity));
ASSERT_EQ(length, builder_->value_data_length());
int64_t actual_capacity = builder_->value_data_capacity();
ASSERT_GE(actual_capacity, expected_capacity);
ASSERT_EQ(actual_capacity & 63, 0);
Done();
ASSERT_EQ(reps * N, result_->length());
ASSERT_EQ(0, result_->null_count());
ASSERT_EQ(reps * 40, result_->value_data()->size());
}
void TestOverflowCheck() {
auto max_size = builder_->memory_limit();
ASSERT_OK(builder_->ValidateOverflow(1));
ASSERT_OK(builder_->ValidateOverflow(max_size));
ASSERT_RAISES(CapacityError, builder_->ValidateOverflow(max_size + 1));
ASSERT_OK(builder_->Append("bb"));
ASSERT_OK(builder_->ValidateOverflow(max_size - 2));
ASSERT_RAISES(CapacityError, builder_->ValidateOverflow(max_size - 1));
ASSERT_OK(builder_->AppendNull());
ASSERT_OK(builder_->ValidateOverflow(max_size - 2));
ASSERT_RAISES(CapacityError, builder_->ValidateOverflow(max_size - 1));
ASSERT_OK(builder_->Append("ccc"));
ASSERT_OK(builder_->ValidateOverflow(max_size - 5));
ASSERT_RAISES(CapacityError, builder_->ValidateOverflow(max_size - 4));
}
void TestZeroLength() {
// All buffers are null
Done();
ASSERT_EQ(result_->length(), 0);
ASSERT_EQ(result_->null_count(), 0);
}
protected:
MemoryPool* pool_ = default_memory_pool();
std::unique_ptr<BuilderType> builder_;
std::shared_ptr<ArrayType> result_;
};
TYPED_TEST_SUITE(TestStringBuilder, BaseBinaryArrowTypes);
TYPED_TEST(TestStringBuilder, TestScalarAppend) { this->TestScalarAppend(); }
TYPED_TEST(TestStringBuilder, TestScalarAppendUnsafe) { this->TestScalarAppendUnsafe(); }
TYPED_TEST(TestStringBuilder, TestExtendCurrent) { this->TestExtendCurrent(); }
TYPED_TEST(TestStringBuilder, TestExtendCurrentUnsafe) {
this->TestExtendCurrentUnsafe();
}
TYPED_TEST(TestStringBuilder, TestVectorAppend) { this->TestVectorAppend(); }
TYPED_TEST(TestStringBuilder, TestAppendCStringsWithValidBytes) {
this->TestAppendCStringsWithValidBytes();
}
TYPED_TEST(TestStringBuilder, TestAppendCStringsWithoutValidBytes) {
this->TestAppendCStringsWithoutValidBytes();
}
TYPED_TEST(TestStringBuilder, TestCapacityReserve) { this->TestCapacityReserve(); }
TYPED_TEST(TestStringBuilder, TestZeroLength) { this->TestZeroLength(); }
TYPED_TEST(TestStringBuilder, TestOverflowCheck) { this->TestOverflowCheck(); }
// ----------------------------------------------------------------------
// ChunkedBinaryBuilder tests
class TestChunkedBinaryBuilder : public ::testing::Test {
public:
void SetUp() {}
void Init(int32_t chunksize) {
builder_.reset(new internal::ChunkedBinaryBuilder(chunksize));
}
void Init(int32_t chunksize, int32_t chunklength) {
builder_.reset(new internal::ChunkedBinaryBuilder(chunksize, chunklength));
}
protected:
std::unique_ptr<internal::ChunkedBinaryBuilder> builder_;
};
TEST_F(TestChunkedBinaryBuilder, BasicOperation) {
const int32_t chunksize = 1000;
Init(chunksize);
const int elem_size = 10;
uint8_t buf[elem_size];
BinaryBuilder unchunked_builder;
const int iterations = 1000;
for (int i = 0; i < iterations; ++i) {
random_bytes(elem_size, i, buf);
ASSERT_OK(unchunked_builder.Append(buf, elem_size));
ASSERT_OK(builder_->Append(buf, elem_size));
}
std::shared_ptr<Array> unchunked;
ASSERT_OK(unchunked_builder.Finish(&unchunked));
ArrayVector chunks;
ASSERT_OK(builder_->Finish(&chunks));
// This assumes that everything is evenly divisible
ArrayVector expected_chunks;
const int elems_per_chunk = chunksize / elem_size;
for (int i = 0; i < iterations / elems_per_chunk; ++i) {
expected_chunks.emplace_back(unchunked->Slice(i * elems_per_chunk, elems_per_chunk));
}
ASSERT_EQ(expected_chunks.size(), chunks.size());
for (size_t i = 0; i < chunks.size(); ++i) {
AssertArraysEqual(*expected_chunks[i], *chunks[i]);
}
}
TEST_F(TestChunkedBinaryBuilder, Reserve) {
// ARROW-6060
const int32_t chunksize = 1000;
Init(chunksize);
ASSERT_OK(builder_->Reserve(chunksize / 2));
auto bytes_after_first_reserve = default_memory_pool()->bytes_allocated();
for (int i = 0; i < 8; ++i) {
ASSERT_OK(builder_->Reserve(chunksize / 2));
}
// no new memory will be allocated since capacity was sufficient for the loop's
// Reserve() calls
ASSERT_EQ(default_memory_pool()->bytes_allocated(), bytes_after_first_reserve);
}
TEST_F(TestChunkedBinaryBuilder, NoData) {
Init(1000);
ArrayVector chunks;
ASSERT_OK(builder_->Finish(&chunks));
ASSERT_EQ(1, chunks.size());
ASSERT_EQ(0, chunks[0]->length());
}
TEST_F(TestChunkedBinaryBuilder, LargeElements) {
Init(100);
const int bufsize = 101;
uint8_t buf[bufsize];
const int iterations = 100;
for (int i = 0; i < iterations; ++i) {
random_bytes(bufsize, i, buf);
ASSERT_OK(builder_->Append(buf, bufsize));
}
ArrayVector chunks;
ASSERT_OK(builder_->Finish(&chunks));
ASSERT_EQ(iterations, static_cast<int>(chunks.size()));
int64_t total_data_size = 0;
for (auto chunk : chunks) {
ASSERT_EQ(1, chunk->length());
total_data_size +=
static_cast<int64_t>(static_cast<const BinaryArray&>(*chunk).GetView(0).size());
}
ASSERT_EQ(iterations * bufsize, total_data_size);
}
TEST_F(TestChunkedBinaryBuilder, LargeElementCount) {
int32_t max_chunk_length = 100;
Init(100, max_chunk_length);
auto length = max_chunk_length + 1;
// ChunkedBinaryBuilder can reserve memory for more than its configured maximum
// (per chunk) element count
ASSERT_OK(builder_->Reserve(length));
for (int64_t i = 0; i < 2 * length; ++i) {
// Appending more elements than have been reserved memory simply overflows to the next
// chunk
ASSERT_OK(builder_->Append(""));
}
ArrayVector chunks;
ASSERT_OK(builder_->Finish(&chunks));
// should have two chunks full of empty strings and another with two more empty strings
ASSERT_EQ(chunks.size(), 3);
ASSERT_EQ(chunks[0]->length(), max_chunk_length);
ASSERT_EQ(chunks[1]->length(), max_chunk_length);
ASSERT_EQ(chunks[2]->length(), 2);
for (auto&& boxed_chunk : chunks) {
const auto& chunk = checked_cast<const BinaryArray&>(*boxed_chunk);
ASSERT_EQ(chunk.value_offset(0), chunk.value_offset(chunk.length()));
}
}
TEST(TestChunkedStringBuilder, BasicOperation) {
const int chunksize = 100;
internal::ChunkedStringBuilder builder(chunksize);
std::string value = "0123456789";
const int iterations = 100;
for (int i = 0; i < iterations; ++i) {
ASSERT_OK(builder.Append(value));
}
ArrayVector chunks;
ASSERT_OK(builder.Finish(&chunks));
ASSERT_EQ(10, chunks.size());
// Type is correct
for (auto chunk : chunks) {
ASSERT_TRUE(chunk->type()->Equals(utf8()));
}
}
// ----------------------------------------------------------------------
// ArraySpanVisitor<binary-like> tests
struct BinaryAppender {
Status VisitNull() {
data.emplace_back("(null)");
return Status::OK();
}
Status VisitValue(std::string_view v) {
data.push_back(v);
return Status::OK();
}
std::vector<std::string_view> data;
};
template <typename T>
class TestBaseBinaryDataVisitor : public ::testing::Test {
public:
using TypeClass = T;
void SetUp() override { type_ = TypeTraits<TypeClass>::type_singleton(); }
void TestBasics() {
auto array = ArrayFromJSON(type_, R"(["foo", null, "bar"])");
BinaryAppender appender;
ArraySpanVisitor<TypeClass> visitor;
ASSERT_OK(visitor.Visit(*array->data(), &appender));
ASSERT_THAT(appender.data, ::testing::ElementsAreArray({"foo", "(null)", "bar"}));
ARROW_UNUSED(visitor); // Workaround weird MSVC warning
}
void TestSliced() {
auto array = ArrayFromJSON(type_, R"(["ab", null, "cd", "ef"])")->Slice(1, 2);
BinaryAppender appender;
ArraySpanVisitor<TypeClass> visitor;
ASSERT_OK(visitor.Visit(*array->data(), &appender));
ASSERT_THAT(appender.data, ::testing::ElementsAreArray({"(null)", "cd"}));
ARROW_UNUSED(visitor); // Workaround weird MSVC warning
}
protected:
std::shared_ptr<DataType> type_;
};
TYPED_TEST_SUITE(TestBaseBinaryDataVisitor, BaseBinaryArrowTypes);
TYPED_TEST(TestBaseBinaryDataVisitor, Basics) { this->TestBasics(); }
TYPED_TEST(TestBaseBinaryDataVisitor, Sliced) { this->TestSliced(); }
} // namespace arrow