forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_common.cc
More file actions
1120 lines (931 loc) · 44 KB
/
Copy pathtest_common.cc
File metadata and controls
1120 lines (931 loc) · 44 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.
#include <algorithm>
#include <cstdint>
#include <functional>
#include <memory>
#include <numeric>
#include <string>
#include <type_traits>
#include <vector>
#include "arrow/array.h"
#include "arrow/array/builder_binary.h"
#include "arrow/array/builder_primitive.h"
#include "arrow/array/builder_time.h"
#include "arrow/ipc/test_common.h"
#include "arrow/pretty_print.h"
#include "arrow/record_batch.h"
#include "arrow/status.h"
#include "arrow/tensor.h"
#include "arrow/testing/builder.h"
#include "arrow/testing/extension_type.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/random.h"
#include "arrow/testing/util.h"
#include "arrow/type.h"
#include "arrow/type_fwd.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/util/logging.h"
namespace arrow {
using internal::checked_cast;
namespace ipc {
namespace test {
void CompareArraysDetailed(int index, const Array& result, const Array& expected) {
if (!expected.Equals(result)) {
std::stringstream pp_result;
std::stringstream pp_expected;
ASSERT_OK(PrettyPrint(expected, 0, &pp_expected));
ASSERT_OK(PrettyPrint(result, 0, &pp_result));
FAIL() << "Index: " << index << " Expected: " << pp_expected.str()
<< "\nGot: " << pp_result.str();
}
}
void CompareBatchColumnsDetailed(const RecordBatch& result, const RecordBatch& expected) {
for (int i = 0; i < expected.num_columns(); ++i) {
auto left = result.column(i);
auto right = expected.column(i);
CompareArraysDetailed(i, *left, *right);
}
}
Status MakeRandomInt32Array(int64_t length, bool include_nulls, MemoryPool* pool,
std::shared_ptr<Array>* out, uint32_t seed, int32_t min,
int32_t max) {
random::RandomArrayGenerator rand(seed);
const double null_probability = include_nulls ? 0.5 : 0.0;
*out = rand.Int32(length, min, max, null_probability);
return Status::OK();
}
Status MakeRandomInt64Array(int64_t length, bool include_nulls, MemoryPool* pool,
std::shared_ptr<Array>* out, uint32_t seed) {
random::RandomArrayGenerator rand(seed);
const double null_probability = include_nulls ? 0.5 : 0.0;
*out = rand.Int64(length, 0, 1000, null_probability);
return Status::OK();
}
namespace {
template <typename ArrayType>
Status MakeRandomArray(int64_t length, bool include_nulls, MemoryPool* pool,
std::shared_ptr<Array>* out, uint32_t seed) {
random::RandomArrayGenerator rand(seed);
const double null_probability = include_nulls ? 0.5 : 0.0;
*out = rand.Numeric<ArrayType>(length, 0, 1000, null_probability);
return Status::OK();
}
template <>
Status MakeRandomArray<Int8Type>(int64_t length, bool include_nulls, MemoryPool* pool,
std::shared_ptr<Array>* out, uint32_t seed) {
random::RandomArrayGenerator rand(seed);
const double null_probability = include_nulls ? 0.5 : 0.0;
*out = rand.Numeric<Int8Type>(length, 0, 127, null_probability);
return Status::OK();
}
template <>
Status MakeRandomArray<UInt8Type>(int64_t length, bool include_nulls, MemoryPool* pool,
std::shared_ptr<Array>* out, uint32_t seed) {
random::RandomArrayGenerator rand(seed);
const double null_probability = include_nulls ? 0.5 : 0.0;
*out = rand.Numeric<UInt8Type>(length, 0, 127, null_probability);
return Status::OK();
}
template <typename TypeClass>
Status MakeListArray(const std::shared_ptr<Array>& child_array, int num_lists,
bool include_nulls, MemoryPool* pool, std::shared_ptr<Array>* out) {
using offset_type = typename TypeClass::offset_type;
using ArrayType = typename TypeTraits<TypeClass>::ArrayType;
// Create the null list values
std::vector<uint8_t> valid_lists(num_lists);
const double null_percent = include_nulls ? 0.1 : 0;
random_null_bytes(num_lists, null_percent, valid_lists.data());
// Create list offsets
const int max_list_size = 10;
std::vector<offset_type> list_sizes(num_lists, 0);
std::vector<offset_type> offsets(
num_lists + 1, 0); // +1 so we can shift for nulls. See partial sum below.
const auto seed = static_cast<uint32_t>(child_array->length());
if (num_lists > 0) {
rand_uniform_int(num_lists, seed, 0, max_list_size, list_sizes.data());
// make sure sizes are consistent with null
std::transform(list_sizes.begin(), list_sizes.end(), valid_lists.begin(),
list_sizes.begin(),
[](offset_type size, uint8_t valid) { return valid == 0 ? 0 : size; });
std::partial_sum(list_sizes.begin(), list_sizes.end(), ++offsets.begin());
// Force invariants
const auto child_length = static_cast<offset_type>(child_array->length());
offsets[0] = 0;
std::replace_if(
offsets.begin(), offsets.end(),
[child_length](offset_type offset) { return offset > child_length; },
child_length);
}
offsets[num_lists] = static_cast<offset_type>(child_array->length());
/// TODO(wesm): Implement support for nulls in ListArray::FromArrays
std::shared_ptr<Buffer> null_bitmap, offsets_buffer;
RETURN_NOT_OK(GetBitmapFromVector(valid_lists, &null_bitmap));
RETURN_NOT_OK(CopyBufferFromVector(offsets, pool, &offsets_buffer));
*out = std::make_shared<ArrayType>(std::make_shared<TypeClass>(child_array->type()),
num_lists, offsets_buffer, child_array, null_bitmap,
kUnknownNullCount);
return (**out).Validate();
}
} // namespace
Status MakeRandomListArray(const std::shared_ptr<Array>& child_array, int num_lists,
bool include_nulls, MemoryPool* pool,
std::shared_ptr<Array>* out) {
return MakeListArray<ListType>(child_array, num_lists, include_nulls, pool, out);
}
Status MakeRandomLargeListArray(const std::shared_ptr<Array>& child_array, int num_lists,
bool include_nulls, MemoryPool* pool,
std::shared_ptr<Array>* out) {
return MakeListArray<LargeListType>(child_array, num_lists, include_nulls, pool, out);
}
Status MakeRandomMapArray(const std::shared_ptr<Array>& key_array,
const std::shared_ptr<Array>& item_array, int num_maps,
bool include_nulls, MemoryPool* pool,
std::shared_ptr<Array>* out) {
auto pair_type = struct_(
{field("key", key_array->type(), false), field("value", item_array->type())});
auto pair_array = std::make_shared<StructArray>(pair_type, key_array->length(),
ArrayVector{key_array, item_array});
RETURN_NOT_OK(MakeRandomListArray(pair_array, num_maps, include_nulls, pool, out));
auto map_data = (*out)->data();
map_data->type = map(key_array->type(), item_array->type());
out->reset(new MapArray(map_data));
return (**out).Validate();
}
Status MakeRandomBooleanArray(const int length, bool include_nulls,
std::shared_ptr<Array>* out) {
std::vector<uint8_t> values(length);
random_null_bytes(length, 0.5, values.data());
ARROW_ASSIGN_OR_RAISE(auto data, internal::BytesToBits(values));
if (include_nulls) {
std::vector<uint8_t> valid_bytes(length);
ARROW_ASSIGN_OR_RAISE(auto null_bitmap, internal::BytesToBits(valid_bytes));
random_null_bytes(length, 0.1, valid_bytes.data());
*out = std::make_shared<BooleanArray>(length, data, null_bitmap, -1);
} else {
*out = std::make_shared<BooleanArray>(length, data, NULLPTR, 0);
}
return Status::OK();
}
Status MakeBooleanBatchSized(const int length, std::shared_ptr<RecordBatch>* out) {
// Make the schema
auto f0 = field("f0", boolean());
auto f1 = field("f1", boolean());
auto schema = ::arrow::schema({f0, f1});
std::shared_ptr<Array> a0, a1;
RETURN_NOT_OK(MakeRandomBooleanArray(length, true, &a0));
RETURN_NOT_OK(MakeRandomBooleanArray(length, false, &a1));
*out = RecordBatch::Make(schema, length, {a0, a1});
return Status::OK();
}
Status MakeBooleanBatch(std::shared_ptr<RecordBatch>* out) {
return MakeBooleanBatchSized(1000, out);
}
Status MakeIntBatchSized(int length, std::shared_ptr<RecordBatch>* out, uint32_t seed) {
// Make the schema
auto f0 = field("f0", int8());
auto f1 = field("f1", uint8());
auto f2 = field("f2", int16());
auto f3 = field("f3", uint16());
auto f4 = field("f4", int32());
auto f5 = field("f5", uint32());
auto f6 = field("f6", int64());
auto f7 = field("f7", uint64());
auto schema = ::arrow::schema({f0, f1, f2, f3, f4, f5, f6, f7});
// Example data
std::shared_ptr<Array> a0, a1, a2, a3, a4, a5, a6, a7;
MemoryPool* pool = default_memory_pool();
RETURN_NOT_OK(MakeRandomArray<Int8Type>(length, false, pool, &a0, seed));
RETURN_NOT_OK(MakeRandomArray<UInt8Type>(length, true, pool, &a1, seed));
RETURN_NOT_OK(MakeRandomArray<Int16Type>(length, true, pool, &a2, seed));
RETURN_NOT_OK(MakeRandomArray<UInt16Type>(length, false, pool, &a3, seed));
RETURN_NOT_OK(MakeRandomArray<Int32Type>(length, false, pool, &a4, seed));
RETURN_NOT_OK(MakeRandomArray<UInt32Type>(length, true, pool, &a5, seed));
RETURN_NOT_OK(MakeRandomArray<Int64Type>(length, true, pool, &a6, seed));
RETURN_NOT_OK(MakeRandomArray<UInt64Type>(length, false, pool, &a7, seed));
*out = RecordBatch::Make(schema, length, {a0, a1, a2, a3, a4, a5, a6, a7});
return Status::OK();
}
Status MakeIntRecordBatch(std::shared_ptr<RecordBatch>* out) {
return MakeIntBatchSized(10, out);
}
Status MakeFloat3264BatchSized(int length, std::shared_ptr<RecordBatch>* out,
uint32_t seed) {
// Make the schema
auto f0 = field("f0", float32());
auto f1 = field("f1", float64());
auto schema = ::arrow::schema({f0, f1});
// Example data
std::shared_ptr<Array> a0, a1;
MemoryPool* pool = default_memory_pool();
RETURN_NOT_OK(MakeRandomArray<FloatType>(length, false, pool, &a0, seed));
RETURN_NOT_OK(MakeRandomArray<DoubleType>(length, true, pool, &a1, seed + 1));
*out = RecordBatch::Make(schema, length, {a0, a1});
return Status::OK();
}
Status MakeFloat3264Batch(std::shared_ptr<RecordBatch>* out) {
return MakeFloat3264BatchSized(10, out);
}
Status MakeFloatBatchSized(int length, std::shared_ptr<RecordBatch>* out, uint32_t seed) {
// Make the schema
auto f0 = field("f0", float16());
auto f1 = field("f1", float32());
auto f2 = field("f2", float64());
auto schema = ::arrow::schema({f0, f1, f2});
// Example data
std::shared_ptr<Array> a0, a1, a2;
MemoryPool* pool = default_memory_pool();
RETURN_NOT_OK(MakeRandomArray<HalfFloatType>(length, false, pool, &a0, seed));
RETURN_NOT_OK(MakeRandomArray<FloatType>(length, false, pool, &a1, seed + 1));
RETURN_NOT_OK(MakeRandomArray<DoubleType>(length, true, pool, &a2, seed + 2));
*out = RecordBatch::Make(schema, length, {a0, a1, a2});
return Status::OK();
}
Status MakeFloatBatch(std::shared_ptr<RecordBatch>* out) {
return MakeFloatBatchSized(10, out);
}
Status MakeRandomStringArray(int64_t length, bool include_nulls, MemoryPool* pool,
std::shared_ptr<Array>* out) {
const std::vector<std::string> values = {"", "", "abc", "123",
"efg", "456!@#!@#", "12312"};
StringBuilder builder(pool);
const size_t values_len = values.size();
for (int64_t i = 0; i < length; ++i) {
int64_t values_index = i % values_len;
if (include_nulls && values_index == 0) {
RETURN_NOT_OK(builder.AppendNull());
} else {
const auto& value = values[values_index];
RETURN_NOT_OK(builder.Append(value));
}
}
return builder.Finish(out);
}
template <class BuilderType>
static Status MakeBinaryArrayWithUniqueValues(int64_t length, bool include_nulls,
MemoryPool* pool,
std::shared_ptr<Array>* out) {
BuilderType builder(pool);
for (int64_t i = 0; i < length; ++i) {
if (include_nulls && (i % 7 == 0)) {
RETURN_NOT_OK(builder.AppendNull());
} else {
RETURN_NOT_OK(builder.Append(std::to_string(i)));
}
}
return builder.Finish(out);
}
Status MakeStringTypesRecordBatch(std::shared_ptr<RecordBatch>* out, bool with_nulls) {
const int64_t length = 500;
auto f0 = field("strings", utf8());
auto f1 = field("binaries", binary());
auto f2 = field("large_strings", large_utf8());
auto f3 = field("large_binaries", large_binary());
auto schema = ::arrow::schema({f0, f1, f2, f3});
std::shared_ptr<Array> a0, a1, a2, a3;
MemoryPool* pool = default_memory_pool();
// Quirk with RETURN_NOT_OK macro and templated functions
{
auto s =
MakeBinaryArrayWithUniqueValues<StringBuilder>(length, with_nulls, pool, &a0);
RETURN_NOT_OK(s);
}
{
auto s =
MakeBinaryArrayWithUniqueValues<BinaryBuilder>(length, with_nulls, pool, &a1);
RETURN_NOT_OK(s);
}
{
auto s = MakeBinaryArrayWithUniqueValues<LargeStringBuilder>(length, with_nulls, pool,
&a2);
RETURN_NOT_OK(s);
}
{
auto s = MakeBinaryArrayWithUniqueValues<LargeBinaryBuilder>(length, with_nulls, pool,
&a3);
RETURN_NOT_OK(s);
}
*out = RecordBatch::Make(schema, length, {a0, a1, a2, a3});
return Status::OK();
}
Status MakeStringTypesRecordBatchWithNulls(std::shared_ptr<RecordBatch>* out) {
return MakeStringTypesRecordBatch(out, true);
}
Status MakeNullRecordBatch(std::shared_ptr<RecordBatch>* out) {
const int64_t length = 500;
auto f0 = field("f0", null());
auto schema = ::arrow::schema({f0});
std::shared_ptr<Array> a0 = std::make_shared<NullArray>(length);
*out = RecordBatch::Make(schema, length, {a0});
return Status::OK();
}
Status MakeListRecordBatch(std::shared_ptr<RecordBatch>* out) {
// Make the schema
auto f0 = field("f0", list(int32()));
auto f1 = field("f1", list(list(int32())));
auto f2 = field("f2", large_list(int32()));
auto schema = ::arrow::schema({f0, f1, f2});
// Example data
MemoryPool* pool = default_memory_pool();
const int length = 200;
std::shared_ptr<Array> leaf_values, list_array, list_list_array, large_list_array;
const bool include_nulls = true;
RETURN_NOT_OK(MakeRandomInt32Array(1000, include_nulls, pool, &leaf_values));
RETURN_NOT_OK(
MakeRandomListArray(leaf_values, length, include_nulls, pool, &list_array));
RETURN_NOT_OK(
MakeRandomListArray(list_array, length, include_nulls, pool, &list_list_array));
RETURN_NOT_OK(MakeRandomLargeListArray(leaf_values, length, include_nulls, pool,
&large_list_array));
*out =
RecordBatch::Make(schema, length, {list_array, list_list_array, large_list_array});
return Status::OK();
}
Status MakeFixedSizeListRecordBatch(std::shared_ptr<RecordBatch>* out) {
// Make the schema
auto f0 = field("f0", fixed_size_list(int32(), 1));
auto f1 = field("f1", fixed_size_list(list(int32()), 3));
auto f2 = field("f2", int32());
auto schema = ::arrow::schema({f0, f1, f2});
// Example data
MemoryPool* pool = default_memory_pool();
const int length = 200;
std::shared_ptr<Array> leaf_values, list_array, list_list_array, flat_array;
const bool include_nulls = true;
RETURN_NOT_OK(MakeRandomInt32Array(1000, include_nulls, pool, &leaf_values));
RETURN_NOT_OK(
MakeRandomListArray(leaf_values, length * 3, include_nulls, pool, &list_array));
list_list_array = std::make_shared<FixedSizeListArray>(f1->type(), length, list_array);
list_array = std::make_shared<FixedSizeListArray>(f0->type(), length,
leaf_values->Slice(0, length));
RETURN_NOT_OK(MakeRandomInt32Array(length, include_nulls, pool, &flat_array));
*out = RecordBatch::Make(schema, length, {list_array, list_list_array, flat_array});
return Status::OK();
}
Status MakeZeroLengthRecordBatch(std::shared_ptr<RecordBatch>* out) {
// Make the schema
auto f0 = field("f0", list(int32()));
auto f1 = field("f1", list(list(int32())));
auto f2 = field("f2", int32());
auto schema = ::arrow::schema({f0, f1, f2});
// Example data
MemoryPool* pool = default_memory_pool();
const bool include_nulls = true;
std::shared_ptr<Array> leaf_values, list_array, list_list_array, flat_array;
RETURN_NOT_OK(MakeRandomInt32Array(0, include_nulls, pool, &leaf_values));
RETURN_NOT_OK(MakeRandomListArray(leaf_values, 0, include_nulls, pool, &list_array));
RETURN_NOT_OK(
MakeRandomListArray(list_array, 0, include_nulls, pool, &list_list_array));
RETURN_NOT_OK(MakeRandomInt32Array(0, include_nulls, pool, &flat_array));
*out = RecordBatch::Make(schema, 0, {list_array, list_list_array, flat_array});
return Status::OK();
}
Status MakeNonNullRecordBatch(std::shared_ptr<RecordBatch>* out) {
// Make the schema
auto f0 = field("f0", list(int32()));
auto f1 = field("f1", list(list(int32())));
auto f2 = field("f2", int32());
auto schema = ::arrow::schema({f0, f1, f2});
// Example data
MemoryPool* pool = default_memory_pool();
const int length = 50;
std::shared_ptr<Array> leaf_values, list_array, list_list_array, flat_array;
RETURN_NOT_OK(MakeRandomInt32Array(1000, true, pool, &leaf_values));
bool include_nulls = false;
RETURN_NOT_OK(
MakeRandomListArray(leaf_values, length, include_nulls, pool, &list_array));
RETURN_NOT_OK(
MakeRandomListArray(list_array, length, include_nulls, pool, &list_list_array));
RETURN_NOT_OK(MakeRandomInt32Array(length, include_nulls, pool, &flat_array));
*out = RecordBatch::Make(schema, length, {list_array, list_list_array, flat_array});
return Status::OK();
}
Status MakeDeeplyNestedList(std::shared_ptr<RecordBatch>* out) {
const int batch_length = 5;
auto type = int32();
MemoryPool* pool = default_memory_pool();
std::shared_ptr<Array> array;
const bool include_nulls = true;
RETURN_NOT_OK(MakeRandomInt32Array(1000, include_nulls, pool, &array));
for (int i = 0; i < 63; ++i) {
type = std::static_pointer_cast<DataType>(list(type));
RETURN_NOT_OK(MakeRandomListArray(array, batch_length, include_nulls, pool, &array));
}
auto f0 = field("f0", type);
auto schema = ::arrow::schema({f0});
std::vector<std::shared_ptr<Array>> arrays = {array};
*out = RecordBatch::Make(schema, batch_length, arrays);
return Status::OK();
}
Status MakeStruct(std::shared_ptr<RecordBatch>* out) {
// reuse constructed list columns
std::shared_ptr<RecordBatch> list_batch;
RETURN_NOT_OK(MakeListRecordBatch(&list_batch));
std::vector<std::shared_ptr<Array>> columns = {
list_batch->column(0), list_batch->column(1), list_batch->column(2)};
auto list_schema = list_batch->schema();
// Define schema
std::shared_ptr<DataType> type(new StructType(
{list_schema->field(0), list_schema->field(1), list_schema->field(2)}));
auto f0 = field("non_null_struct", type);
auto f1 = field("null_struct", type);
auto schema = ::arrow::schema({f0, f1});
// construct individual nullable/non-nullable struct arrays
std::shared_ptr<Array> no_nulls(new StructArray(type, list_batch->num_rows(), columns));
std::vector<uint8_t> null_bytes(list_batch->num_rows(), 1);
null_bytes[0] = 0;
ARROW_ASSIGN_OR_RAISE(auto null_bitmap, internal::BytesToBits(null_bytes));
std::shared_ptr<Array> with_nulls(
new StructArray(type, list_batch->num_rows(), columns, null_bitmap, 1));
// construct batch
std::vector<std::shared_ptr<Array>> arrays = {no_nulls, with_nulls};
*out = RecordBatch::Make(schema, list_batch->num_rows(), arrays);
return Status::OK();
}
Status MakeUnion(std::shared_ptr<RecordBatch>* out) {
// Define schema
std::vector<std::shared_ptr<Field>> union_fields(
{field("u0", int32()), field("u1", uint8())});
std::vector<int8_t> type_codes = {5, 10};
auto sparse_type = sparse_union(union_fields, type_codes);
auto dense_type = dense_union(union_fields, type_codes);
auto f0 = field("sparse", sparse_type);
auto f1 = field("dense", dense_type);
auto schema = ::arrow::schema({f0, f1});
// Create data
std::vector<std::shared_ptr<Array>> sparse_children(2);
std::vector<std::shared_ptr<Array>> dense_children(2);
const int64_t length = 7;
std::shared_ptr<Buffer> type_ids_buffer;
std::vector<uint8_t> type_ids = {5, 10, 5, 5, 10, 10, 5};
RETURN_NOT_OK(CopyBufferFromVector(type_ids, default_memory_pool(), &type_ids_buffer));
std::vector<int32_t> u0_values = {0, 1, 2, 3, 4, 5, 6};
ArrayFromVector<Int32Type, int32_t>(u0_values, &sparse_children[0]);
std::vector<uint8_t> u1_values = {10, 11, 12, 13, 14, 15, 16};
ArrayFromVector<UInt8Type, uint8_t>(u1_values, &sparse_children[1]);
// dense children
u0_values = {0, 2, 3, 7};
ArrayFromVector<Int32Type, int32_t>(u0_values, &dense_children[0]);
u1_values = {11, 14, 15};
ArrayFromVector<UInt8Type, uint8_t>(u1_values, &dense_children[1]);
std::shared_ptr<Buffer> offsets_buffer;
std::vector<int32_t> offsets = {0, 0, 1, 2, 1, 2, 3};
RETURN_NOT_OK(CopyBufferFromVector(offsets, default_memory_pool(), &offsets_buffer));
auto sparse = std::make_shared<SparseUnionArray>(sparse_type, length, sparse_children,
type_ids_buffer);
auto dense = std::make_shared<DenseUnionArray>(dense_type, length, dense_children,
type_ids_buffer, offsets_buffer);
// construct batch
std::vector<std::shared_ptr<Array>> arrays = {sparse, dense};
*out = RecordBatch::Make(schema, length, arrays);
return Status::OK();
}
Status MakeDictionary(std::shared_ptr<RecordBatch>* out) {
const int64_t length = 6;
std::vector<bool> is_valid = {true, true, false, true, true, true};
auto dict_ty = utf8();
auto dict1 = ArrayFromJSON(dict_ty, "[\"foo\", \"bar\", \"baz\"]");
auto dict2 = ArrayFromJSON(dict_ty, "[\"fo\", \"bap\", \"bop\", \"qup\"]");
auto f0_type = arrow::dictionary(arrow::int32(), dict_ty);
auto f1_type = arrow::dictionary(arrow::int8(), dict_ty, true);
auto f2_type = arrow::dictionary(arrow::int32(), dict_ty);
std::shared_ptr<Array> indices0, indices1, indices2;
std::vector<int32_t> indices0_values = {1, 2, -1, 0, 2, 0};
std::vector<int8_t> indices1_values = {0, 0, 2, 2, 1, 1};
std::vector<int32_t> indices2_values = {3, 0, 2, 1, 0, 2};
ArrayFromVector<Int32Type, int32_t>(is_valid, indices0_values, &indices0);
ArrayFromVector<Int8Type, int8_t>(is_valid, indices1_values, &indices1);
ArrayFromVector<Int32Type, int32_t>(is_valid, indices2_values, &indices2);
auto a0 = std::make_shared<DictionaryArray>(f0_type, indices0, dict1);
auto a1 = std::make_shared<DictionaryArray>(f1_type, indices1, dict1);
auto a2 = std::make_shared<DictionaryArray>(f2_type, indices2, dict2);
// Lists of dictionary-encoded strings
auto f3_type = list(f1_type);
auto indices3 = ArrayFromJSON(int8(), "[0, 1, 2, 0, 1, 1, 2, 1, 0]");
auto offsets3 = ArrayFromJSON(int32(), "[0, 0, 2, 2, 5, 6, 9]");
std::shared_ptr<Buffer> null_bitmap;
RETURN_NOT_OK(GetBitmapFromVector(is_valid, &null_bitmap));
std::shared_ptr<Array> a3 = std::make_shared<ListArray>(
f3_type, length, std::static_pointer_cast<PrimitiveArray>(offsets3)->values(),
std::make_shared<DictionaryArray>(f1_type, indices3, dict1), null_bitmap, 1);
// Dictionary-encoded lists of integers
auto dict4_ty = list(int8());
auto f4_type = dictionary(int8(), dict4_ty);
auto indices4 = ArrayFromJSON(int8(), "[0, 1, 2, 0, 2, 2]");
auto dict4 = ArrayFromJSON(dict4_ty, "[[44, 55], [], [66]]");
auto a4 = std::make_shared<DictionaryArray>(f4_type, indices4, dict4);
std::vector<std::shared_ptr<Field>> fields = {
field("dict1", f0_type), field("dict2", f1_type), field("dict3", f2_type),
field("list<encoded utf8>", f3_type), field("encoded list<int8>", f4_type)};
std::vector<std::shared_ptr<Array>> arrays = {a0, a1, a2, a3, a4};
// Ensure all dictionary index types are represented
int field_index = 5;
for (auto index_ty : all_dictionary_index_types()) {
std::stringstream ss;
ss << "dict" << field_index++;
auto ty = arrow::dictionary(index_ty, dict_ty);
auto indices = ArrayFromJSON(index_ty, "[0, 1, 2, 0, 2, 2]");
fields.push_back(field(ss.str(), ty));
arrays.push_back(std::make_shared<DictionaryArray>(ty, indices, dict1));
}
// construct batch
*out = RecordBatch::Make(::arrow::schema(fields), length, arrays);
return Status::OK();
}
Status MakeDictionaryFlat(std::shared_ptr<RecordBatch>* out) {
const int64_t length = 6;
std::vector<bool> is_valid = {true, true, false, true, true, true};
auto dict_ty = utf8();
auto dict1 = ArrayFromJSON(dict_ty, "[\"foo\", \"bar\", \"baz\"]");
auto dict2 = ArrayFromJSON(dict_ty, "[\"foo\", \"bar\", \"baz\", \"qux\"]");
auto f0_type = arrow::dictionary(arrow::int32(), dict_ty);
auto f1_type = arrow::dictionary(arrow::int8(), dict_ty);
auto f2_type = arrow::dictionary(arrow::int32(), dict_ty);
std::shared_ptr<Array> indices0, indices1, indices2;
std::vector<int32_t> indices0_values = {1, 2, -1, 0, 2, 0};
std::vector<int8_t> indices1_values = {0, 0, 2, 2, 1, 1};
std::vector<int32_t> indices2_values = {3, 0, 2, 1, 0, 2};
ArrayFromVector<Int32Type, int32_t>(is_valid, indices0_values, &indices0);
ArrayFromVector<Int8Type, int8_t>(is_valid, indices1_values, &indices1);
ArrayFromVector<Int32Type, int32_t>(is_valid, indices2_values, &indices2);
auto a0 = std::make_shared<DictionaryArray>(f0_type, indices0, dict1);
auto a1 = std::make_shared<DictionaryArray>(f1_type, indices1, dict1);
auto a2 = std::make_shared<DictionaryArray>(f2_type, indices2, dict2);
// construct batch
auto schema = ::arrow::schema(
{field("dict1", f0_type), field("dict2", f1_type), field("dict3", f2_type)});
std::vector<std::shared_ptr<Array>> arrays = {a0, a1, a2};
*out = RecordBatch::Make(schema, length, arrays);
return Status::OK();
}
Status MakeNestedDictionary(std::shared_ptr<RecordBatch>* out) {
const int64_t length = 7;
auto values0 = ArrayFromJSON(utf8(), "[\"foo\", \"bar\", \"baz\"]");
auto values1 = ArrayFromJSON(int64(), "[1234567890, 987654321]");
// NOTE: it is important to test several levels of nesting, with non-trivial
// numbers of child fields, to exercise structural mapping of fields to dict ids.
// Field 0: dict(int32, list(dict(int8, utf8)))
ARROW_ASSIGN_OR_RAISE(auto inner0,
DictionaryArray::FromArrays(
dictionary(int8(), values0->type()),
/*indices=*/ArrayFromJSON(int8(), "[0, 1, 2, null, 2, 1, 0]"),
/*dictionary=*/values0));
ARROW_ASSIGN_OR_RAISE(auto nested_values0,
ListArray::FromArrays(
/*offsets=*/*ArrayFromJSON(int32(), "[0, 3, 3, 6, 7]"),
/*values=*/*inner0));
ARROW_ASSIGN_OR_RAISE(
auto outer0, DictionaryArray::FromArrays(
dictionary(int32(), nested_values0->type()),
/*indices=*/ArrayFromJSON(int32(), "[0, 1, 3, 3, null, 3, 2]"),
/*dictionary=*/nested_values0));
DCHECK_EQ(outer0->length(), length);
// Field 1: struct(a: dict(int8, int64), b: dict(int16, utf8))
ARROW_ASSIGN_OR_RAISE(
auto inner1, DictionaryArray::FromArrays(
dictionary(int8(), values1->type()),
/*indices=*/ArrayFromJSON(int8(), "[0, 1, 1, null, null, 1, 0]"),
/*dictionary=*/values1));
ARROW_ASSIGN_OR_RAISE(
auto inner2, DictionaryArray::FromArrays(
dictionary(int16(), values0->type()),
/*indices=*/ArrayFromJSON(int16(), "[2, 1, null, null, 2, 1, 0]"),
/*dictionary=*/values0));
ARROW_ASSIGN_OR_RAISE(
auto outer1, StructArray::Make({inner1, inner2}, {field("a", inner1->type()),
field("b", inner2->type())}));
DCHECK_EQ(outer1->length(), length);
// Field 2: dict(int8, struct(c: dict(int8, int64), d: dict(int16, list(dict(int8,
// utf8)))))
ARROW_ASSIGN_OR_RAISE(auto nested_values2,
ListArray::FromArrays(
/*offsets=*/*ArrayFromJSON(int32(), "[0, 1, 5, 5, 7]"),
/*values=*/*inner0));
ARROW_ASSIGN_OR_RAISE(
auto inner3, DictionaryArray::FromArrays(
dictionary(int16(), nested_values2->type()),
/*indices=*/ArrayFromJSON(int16(), "[0, 1, 3, null, 3, 2, 1]"),
/*dictionary=*/nested_values2));
ARROW_ASSIGN_OR_RAISE(
auto inner4, StructArray::Make({inner1, inner3}, {field("c", inner1->type()),
field("d", inner3->type())}));
ARROW_ASSIGN_OR_RAISE(auto outer2,
DictionaryArray::FromArrays(
dictionary(int8(), inner4->type()),
/*indices=*/ArrayFromJSON(int8(), "[0, 2, 4, 6, 1, 3, 5]"),
/*dictionary=*/inner4));
DCHECK_EQ(outer2->length(), length);
auto schema = ::arrow::schema({
field("f0", outer0->type()),
field("f1", outer1->type()),
field("f2", outer2->type()),
});
*out = RecordBatch::Make(schema, length, {outer0, outer1, outer2});
return Status::OK();
}
Status MakeMap(std::shared_ptr<RecordBatch>* out) {
constexpr int64_t kNumRows = 3;
std::shared_ptr<Array> a0, a1;
auto key_array = ArrayFromJSON(utf8(), R"(["k1", "k2", "k1", "k3", "k1", "k4"])");
auto item_array = ArrayFromJSON(int16(), "[0, -1, 2, -3, 4, null]");
RETURN_NOT_OK(MakeRandomMapArray(key_array, item_array, kNumRows,
/*include_nulls=*/false, default_memory_pool(), &a0));
RETURN_NOT_OK(MakeRandomMapArray(key_array, item_array, kNumRows,
/*include_nulls=*/true, default_memory_pool(), &a1));
auto f0 = field("f0", a0->type());
auto f1 = field("f1", a1->type());
*out = RecordBatch::Make(::arrow::schema({f0, f1}), kNumRows, {a0, a1});
return Status::OK();
}
Status MakeMapOfDictionary(std::shared_ptr<RecordBatch>* out) {
// Exercises ARROW-9660
constexpr int64_t kNumRows = 3;
std::shared_ptr<Array> a0, a1;
auto key_array = DictArrayFromJSON(dictionary(int32(), utf8()), "[0, 1, 0, 2, 0, 3]",
R"(["k1", "k2", "k3", "k4"])");
auto item_array = ArrayFromJSON(int16(), "[0, -1, 2, -3, 4, null]");
RETURN_NOT_OK(MakeRandomMapArray(key_array, item_array, kNumRows,
/*include_nulls=*/false, default_memory_pool(), &a0));
RETURN_NOT_OK(MakeRandomMapArray(key_array, item_array, kNumRows,
/*include_nulls=*/true, default_memory_pool(), &a1));
auto f0 = field("f0", a0->type());
auto f1 = field("f1", a1->type());
*out = RecordBatch::Make(::arrow::schema({f0, f1}), kNumRows, {a0, a1});
return Status::OK();
}
Status MakeDates(std::shared_ptr<RecordBatch>* out) {
std::vector<bool> is_valid = {true, true, true, false, true, true, true};
auto f0 = field("f0", date32());
auto f1 = field("f1", date64());
auto schema = ::arrow::schema({f0, f1});
std::vector<int32_t> date32_values = {0, 1, 2, 3, 4, 5, 6};
std::shared_ptr<Array> date32_array;
ArrayFromVector<Date32Type, int32_t>(is_valid, date32_values, &date32_array);
std::vector<int64_t> date64_values = {86400000, 172800000, 259200000, 1489272000000,
345600000, 432000000, 518400000};
std::shared_ptr<Array> date64_array;
ArrayFromVector<Date64Type, int64_t>(is_valid, date64_values, &date64_array);
*out = RecordBatch::Make(schema, date32_array->length(), {date32_array, date64_array});
return Status::OK();
}
Status MakeTimestamps(std::shared_ptr<RecordBatch>* out) {
std::vector<bool> is_valid = {true, true, true, false, true, true, true};
auto f0 = field("f0", timestamp(TimeUnit::MILLI));
auto f1 = field("f1", timestamp(TimeUnit::NANO, "America/New_York"));
auto f2 = field("f2", timestamp(TimeUnit::SECOND));
auto schema = ::arrow::schema({f0, f1, f2});
std::vector<int64_t> ts_values = {1489269000000, 1489270000000, 1489271000000,
1489272000000, 1489272000000, 1489273000000};
std::shared_ptr<Array> a0, a1, a2;
ArrayFromVector<TimestampType, int64_t>(f0->type(), is_valid, ts_values, &a0);
ArrayFromVector<TimestampType, int64_t>(f1->type(), is_valid, ts_values, &a1);
ArrayFromVector<TimestampType, int64_t>(f2->type(), is_valid, ts_values, &a2);
*out = RecordBatch::Make(schema, a0->length(), {a0, a1, a2});
return Status::OK();
}
Status MakeIntervals(std::shared_ptr<RecordBatch>* out) {
std::vector<bool> is_valid = {true, true, true, false, true, true, true};
auto f0 = field("f0", duration(TimeUnit::MILLI));
auto f1 = field("f1", duration(TimeUnit::NANO));
auto f2 = field("f2", duration(TimeUnit::SECOND));
auto f3 = field("f3", day_time_interval());
auto f4 = field("f4", month_interval());
auto f5 = field("f5", month_day_nano_interval());
auto schema = ::arrow::schema({f0, f1, f2, f3, f4, f5});
std::vector<int64_t> ts_values = {1489269000000, 1489270000000, 1489271000000,
1489272000000, 1489272000000, 1489273000000};
std::shared_ptr<Array> a0, a1, a2, a3, a4, a5;
ArrayFromVector<DurationType, int64_t>(f0->type(), is_valid, ts_values, &a0);
ArrayFromVector<DurationType, int64_t>(f1->type(), is_valid, ts_values, &a1);
ArrayFromVector<DurationType, int64_t>(f2->type(), is_valid, ts_values, &a2);
ArrayFromVector<DayTimeIntervalType, DayTimeIntervalType::DayMilliseconds>(
f3->type(), is_valid, {{0, 0}, {0, 1}, {1, 1}, {2, 1}, {3, 4}, {-1, -1}}, &a3);
ArrayFromVector<MonthIntervalType, int32_t>(f4->type(), is_valid, {0, -1, 1, 2, -2, 24},
&a4);
ArrayFromVector<MonthDayNanoIntervalType, MonthDayNanoIntervalType::MonthDayNanos>(
f5->type(), is_valid,
{{0, 0, 0}, {0, 0, 1}, {-1, 0, 1}, {-1, -2, -3}, {2, 4, 6}, {-3, -4, -5}}, &a5);
*out = RecordBatch::Make(schema, a0->length(), {a0, a1, a2, a3, a4, a5});
return Status::OK();
}
Status MakeTimes(std::shared_ptr<RecordBatch>* out) {
std::vector<bool> is_valid = {true, true, true, false, true, true, true};
auto f0 = field("f0", time32(TimeUnit::MILLI));
auto f1 = field("f1", time64(TimeUnit::NANO));
auto f2 = field("f2", time32(TimeUnit::SECOND));
auto f3 = field("f3", time64(TimeUnit::NANO));
auto schema = ::arrow::schema({f0, f1, f2, f3});
std::vector<int32_t> t32_values = {14896, 14897, 14892, 1489272000, 14893, 14895};
std::vector<int64_t> t64_values = {1489269000000, 1489270000000, 1489271000000,
1489272000000, 1489272000000, 1489273000000};
std::shared_ptr<Array> a0, a1, a2, a3;
ArrayFromVector<Time32Type, int32_t>(f0->type(), is_valid, t32_values, &a0);
ArrayFromVector<Time64Type, int64_t>(f1->type(), is_valid, t64_values, &a1);
ArrayFromVector<Time32Type, int32_t>(f2->type(), is_valid, t32_values, &a2);
ArrayFromVector<Time64Type, int64_t>(f3->type(), is_valid, t64_values, &a3);
*out = RecordBatch::Make(schema, a0->length(), {a0, a1, a2, a3});
return Status::OK();
}
template <typename BuilderType, typename T>
static void AppendValues(const std::vector<bool>& is_valid, const std::vector<T>& values,
BuilderType* builder) {
for (size_t i = 0; i < values.size(); ++i) {
if (is_valid[i]) {
ASSERT_OK(builder->Append(values[i]));
} else {
ASSERT_OK(builder->AppendNull());
}
}
}
Status MakeFWBinary(std::shared_ptr<RecordBatch>* out) {
std::vector<bool> is_valid = {true, true, true, false};
auto f0 = field("f0", fixed_size_binary(4));
auto f1 = field("f1", fixed_size_binary(0));
auto schema = ::arrow::schema({f0, f1});
std::shared_ptr<Array> a1, a2;
FixedSizeBinaryBuilder b1(f0->type());
FixedSizeBinaryBuilder b2(f1->type());
std::vector<std::string> values1 = {"foo1", "foo2", "foo3", "foo4"};
AppendValues(is_valid, values1, &b1);
std::vector<std::string> values2 = {"", "", "", ""};
AppendValues(is_valid, values2, &b2);
RETURN_NOT_OK(b1.Finish(&a1));
RETURN_NOT_OK(b2.Finish(&a2));
*out = RecordBatch::Make(schema, a1->length(), {a1, a2});
return Status::OK();
}
Status MakeDecimal(std::shared_ptr<RecordBatch>* out) {
constexpr int kLength = 10;
auto type = decimal128(38, 4);
auto f0 = field("f0", type);
auto f1 = field("f1", type);
auto schema = ::arrow::schema({f0, f1});
auto gen = random::RandomArrayGenerator(/*seed=*/1);
auto a1 = gen.Decimal128(type, kLength, /*null_probability=*/0.1);
auto a2 = std::make_shared<Decimal128Array>(type, kLength, a1->data()->buffers[1]);
*out = RecordBatch::Make(schema, kLength, {a1, a2});
return Status::OK();
}
Status MakeNull(std::shared_ptr<RecordBatch>* out) {
auto f0 = field("f0", null());
// Also put a non-null field to make sure we handle the null array buffers properly
auto f1 = field("f1", int64());
auto schema = ::arrow::schema({f0, f1});
auto a1 = std::make_shared<NullArray>(10);
std::vector<int64_t> int_values = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<bool> is_valid = {true, true, true, false, false,
true, true, true, true, true};
std::shared_ptr<Array> a2;
ArrayFromVector<Int64Type, int64_t>(f1->type(), is_valid, int_values, &a2);
*out = RecordBatch::Make(schema, a1->length(), {a1, a2});
return Status::OK();
}
Status MakeUuid(std::shared_ptr<RecordBatch>* out) {
auto uuid_type = uuid();
auto storage_type = checked_cast<const ExtensionType&>(*uuid_type).storage_type();
auto f0 = field("f0", uuid_type);
auto f1 = field("f1", uuid_type, /*nullable=*/false);
auto schema = ::arrow::schema({f0, f1});
auto a0 = std::make_shared<UuidArray>(
uuid_type, ArrayFromJSON(storage_type, R"(["0123456789abcdef", null])"));
auto a1 = std::make_shared<UuidArray>(
uuid_type,
ArrayFromJSON(storage_type, R"(["ZYXWVUTSRQPONMLK", "JIHGFEDBA9876543"])"));
*out = RecordBatch::Make(schema, a1->length(), {a0, a1});
return Status::OK();
}
Status MakeComplex128(std::shared_ptr<RecordBatch>* out) {
auto type = complex128();
auto storage_type = checked_cast<const ExtensionType&>(*type).storage_type();
auto f0 = field("f0", type);
auto f1 = field("f1", type, /*nullable=*/false);
auto schema = ::arrow::schema({f0, f1});
auto a0 = ExtensionType::WrapArray(complex128(),
ArrayFromJSON(storage_type, "[[1.0, -2.5], null]"));
auto a1 = ExtensionType::WrapArray(
complex128(), ArrayFromJSON(storage_type, "[[1.0, -2.5], [3.0, -4.0]]"));
*out = RecordBatch::Make(schema, a1->length(), {a0, a1});
return Status::OK();
}
Status MakeDictExtension(std::shared_ptr<RecordBatch>* out) {