forked from rapidsai/cudf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorc_test.cpp
2289 lines (1861 loc) · 90.7 KB
/
orc_test.cpp
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
/*
* Copyright (c) 2019-2025, NVIDIA CORPORATION.
*
* Licensed 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 <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/cudf_gtest.hpp>
#include <cudf_test/io_metadata_utilities.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/random.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/testing_main.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/concatenate.hpp>
#include <cudf/copying.hpp>
#include <cudf/detail/iterator.cuh>
#include <cudf/io/data_sink.hpp>
#include <cudf/io/orc.hpp>
#include <cudf/io/orc_metadata.hpp>
#include <cudf/table/table.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/utilities/span.hpp>
#include <src/io/comp/nvcomp_adapter.hpp>
#include <array>
#include <numeric>
#include <type_traits>
namespace nvcomp = cudf::io::detail::nvcomp;
template <typename T, typename SourceElementT = T>
using column_wrapper =
std::conditional_t<std::is_same_v<T, cudf::string_view>,
cudf::test::strings_column_wrapper,
cudf::test::fixed_width_column_wrapper<T, SourceElementT>>;
using str_col = column_wrapper<cudf::string_view>;
using bool_col = column_wrapper<bool>;
using int8_col = column_wrapper<int8_t>;
using int16_col = column_wrapper<int16_t>;
using int32_col = column_wrapper<int32_t>;
using int64_col = column_wrapper<int64_t>;
using float32_col = column_wrapper<float>;
using float64_col = column_wrapper<double>;
using dec32_col = cudf::test::fixed_point_column_wrapper<numeric::decimal32::rep>;
using dec64_col = cudf::test::fixed_point_column_wrapper<numeric::decimal64::rep>;
using dec128_col = cudf::test::fixed_point_column_wrapper<numeric::decimal128::rep>;
using struct_col = cudf::test::structs_column_wrapper;
template <typename T>
using list_col = cudf::test::lists_column_wrapper<T>;
using column = cudf::column;
using table = cudf::table;
using table_view = cudf::table_view;
// Global environment for temporary files
auto const temp_env = static_cast<cudf::test::TempDirTestEnvironment*>(
::testing::AddGlobalTestEnvironment(new cudf::test::TempDirTestEnvironment));
template <typename T>
std::unique_ptr<cudf::table> create_random_fixed_table(cudf::size_type num_columns,
cudf::size_type num_rows,
bool include_validity)
{
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
std::vector<column_wrapper<T>> src_cols(num_columns);
for (int idx = 0; idx < num_columns; idx++) {
auto rand_elements =
cudf::detail::make_counting_transform_iterator(0, [](T i) { return rand(); });
if (include_validity) {
src_cols[idx] = column_wrapper<T>(rand_elements, rand_elements + num_rows, valids);
} else {
src_cols[idx] = column_wrapper<T>(rand_elements, rand_elements + num_rows);
}
}
std::vector<std::unique_ptr<cudf::column>> columns(num_columns);
std::transform(src_cols.begin(), src_cols.end(), columns.begin(), [](column_wrapper<T>& in) {
auto ret = in.release();
[[maybe_unused]] auto nulls = ret->has_nulls(); // pre-cache the null count
return ret;
});
return std::make_unique<cudf::table>(std::move(columns));
}
// Base test fixture for tests
struct OrcWriterTest : public cudf::test::BaseFixture {};
// Typed test fixture for numeric type tests
template <typename T>
struct OrcWriterNumericTypeTest : public OrcWriterTest {
auto type() { return cudf::data_type{cudf::type_to_id<T>()}; }
};
// Typed test fixture for timestamp type tests
template <typename T>
struct OrcWriterTimestampTypeTest : public OrcWriterTest {
auto type() { return cudf::data_type{cudf::type_to_id<T>()}; }
};
// Declare typed test cases
// TODO: Replace with `NumericTypes` when unsigned support is added. Issue #5351
using SupportedTypes = cudf::test::Types<int8_t, int16_t, int32_t, int64_t, bool, float, double>;
TYPED_TEST_SUITE(OrcWriterNumericTypeTest, SupportedTypes);
using SupportedTimestampTypes =
cudf::test::RemoveIf<cudf::test::ContainedIn<cudf::test::Types<cudf::timestamp_D>>,
cudf::test::TimestampTypes>;
TYPED_TEST_SUITE(OrcWriterTimestampTypeTest, SupportedTimestampTypes);
// Base test fixture for chunked writer tests
struct OrcChunkedWriterTest : public cudf::test::BaseFixture {};
// Typed test fixture for numeric type tests
template <typename T>
struct OrcChunkedWriterNumericTypeTest : public OrcChunkedWriterTest {
auto type() { return cudf::data_type{cudf::type_to_id<T>()}; }
};
// Declare typed test cases
TYPED_TEST_SUITE(OrcChunkedWriterNumericTypeTest, SupportedTypes);
// Test fixture for reader tests
struct OrcReaderTest : public cudf::test::BaseFixture {};
// Test fixture for statistics tests
struct OrcStatisticsTest : public cudf::test::BaseFixture {};
// Test fixture for metadata tests
struct OrcMetadataReaderTest : public cudf::test::BaseFixture {};
struct OrcCompressionTest : public cudf::test::BaseFixture,
public ::testing::WithParamInterface<cudf::io::compression_type> {};
namespace {
// Generates a vector of uniform random values of type T
template <typename T>
inline auto random_values(size_t size)
{
std::vector<T> values(size);
using T1 = T;
using uniform_distribution =
typename std::conditional_t<std::is_same_v<T1, bool>,
std::bernoulli_distribution,
std::conditional_t<std::is_floating_point_v<T1>,
std::uniform_real_distribution<T1>,
std::uniform_int_distribution<T1>>>;
static constexpr auto seed = 0xf00d;
static std::mt19937 engine{seed};
static uniform_distribution dist{};
std::generate_n(values.begin(), size, [&]() { return T{dist(engine)}; });
return values;
}
struct SkipRowTest {
int test_calls{0};
SkipRowTest() {}
std::unique_ptr<table> get_expected_result(std::string const& filepath,
int skip_rows,
int file_num_rows,
int read_num_rows)
{
auto sequence = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i; });
column_wrapper<int32_t, typename decltype(sequence)::value_type> input_col(
sequence, sequence + file_num_rows);
table_view input_table({input_col});
cudf::io::orc_writer_options out_opts =
cudf::io::orc_writer_options::builder(cudf::io::sink_info{filepath}, input_table);
cudf::io::write_orc(out_opts);
auto begin_sequence = sequence, end_sequence = sequence;
if (skip_rows < file_num_rows) {
begin_sequence += skip_rows;
end_sequence += std::min(skip_rows + read_num_rows, file_num_rows);
}
column_wrapper<int32_t, typename decltype(sequence)::value_type> output_col(begin_sequence,
end_sequence);
std::vector<std::unique_ptr<column>> output_cols;
output_cols.push_back(output_col.release());
return std::make_unique<table>(std::move(output_cols));
}
void test(int skip_rows, int file_num_rows, int read_num_rows)
{
auto filepath =
temp_env->get_temp_filepath("SkipRowTest" + std::to_string(test_calls++) + ".orc");
auto expected_result = get_expected_result(filepath, skip_rows, file_num_rows, read_num_rows);
cudf::io::orc_reader_options in_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath})
.use_index(false)
.skip_rows(skip_rows)
.num_rows(read_num_rows);
auto result = cudf::io::read_orc(in_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected_result->view(), result.tbl->view());
}
void test(int skip_rows, int file_num_rows)
{
auto filepath =
temp_env->get_temp_filepath("SkipRowTest" + std::to_string(test_calls++) + ".orc");
auto expected_result =
get_expected_result(filepath, skip_rows, file_num_rows, file_num_rows - skip_rows);
cudf::io::orc_reader_options in_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath})
.use_index(false)
.skip_rows(skip_rows);
auto result = cudf::io::read_orc(in_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected_result->view(), result.tbl->view());
}
};
} // namespace
TYPED_TEST(OrcWriterNumericTypeTest, SingleColumn)
{
auto sequence = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i; });
constexpr auto num_rows = 100;
column_wrapper<TypeParam, typename decltype(sequence)::value_type> col(sequence,
sequence + num_rows);
table_view expected({col});
auto filepath = temp_env->get_temp_filepath("OrcSingleColumn.orc");
cudf::io::orc_writer_options out_opts =
cudf::io::orc_writer_options::builder(cudf::io::sink_info{filepath}, expected);
cudf::io::write_orc(out_opts);
cudf::io::orc_reader_options in_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath}).use_index(false);
auto result = cudf::io::read_orc(in_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, result.tbl->view());
}
TYPED_TEST(OrcWriterNumericTypeTest, SingleColumnWithNulls)
{
auto sequence = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i; });
auto validity = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return (i % 2); });
constexpr auto num_rows = 100;
column_wrapper<TypeParam, typename decltype(sequence)::value_type> col(
sequence, sequence + num_rows, validity);
table_view expected({col});
auto filepath = temp_env->get_temp_filepath("OrcSingleColumnWithNulls.orc");
cudf::io::orc_writer_options out_opts =
cudf::io::orc_writer_options::builder(cudf::io::sink_info{filepath}, expected);
cudf::io::write_orc(out_opts);
cudf::io::orc_reader_options in_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath}).use_index(false);
auto result = cudf::io::read_orc(in_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, result.tbl->view());
}
TYPED_TEST(OrcWriterTimestampTypeTest, Timestamps)
{
auto sequence =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return (std::rand() / 10); });
constexpr auto num_rows = 100;
column_wrapper<TypeParam, typename decltype(sequence)::value_type> col(sequence,
sequence + num_rows);
table_view expected({col});
auto filepath = temp_env->get_temp_filepath("OrcTimestamps.orc");
cudf::io::orc_writer_options out_opts =
cudf::io::orc_writer_options::builder(cudf::io::sink_info{filepath}, expected);
cudf::io::write_orc(out_opts);
cudf::io::orc_reader_options in_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath})
.use_index(false)
.timestamp_type(this->type());
auto result = cudf::io::read_orc(in_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, result.tbl->view());
}
TYPED_TEST(OrcWriterTimestampTypeTest, TimestampsWithNulls)
{
auto sequence =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return (std::rand() / 10); });
auto validity =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return (i > 30) && (i < 60); });
constexpr auto num_rows = 100;
column_wrapper<TypeParam, typename decltype(sequence)::value_type> col(
sequence, sequence + num_rows, validity);
table_view expected({col});
auto filepath = temp_env->get_temp_filepath("OrcTimestampsWithNulls.orc");
cudf::io::orc_writer_options out_opts =
cudf::io::orc_writer_options::builder(cudf::io::sink_info{filepath}, expected);
cudf::io::write_orc(out_opts);
cudf::io::orc_reader_options in_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath})
.use_index(false)
.timestamp_type(this->type());
auto result = cudf::io::read_orc(in_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, result.tbl->view());
}
TYPED_TEST(OrcWriterTimestampTypeTest, TimestampOverflow)
{
constexpr int64_t max = std::numeric_limits<int64_t>::max();
auto sequence = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return max - i; });
constexpr auto num_rows = 100;
column_wrapper<TypeParam, typename decltype(sequence)::value_type> col(sequence,
sequence + num_rows);
table_view expected({col});
auto filepath = temp_env->get_temp_filepath("OrcTimestampOverflow.orc");
cudf::io::orc_writer_options out_opts =
cudf::io::orc_writer_options::builder(cudf::io::sink_info{filepath}, expected);
cudf::io::write_orc(out_opts);
cudf::io::orc_reader_options in_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath})
.use_index(false)
.timestamp_type(this->type());
auto result = cudf::io::read_orc(in_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, result.tbl->view());
}
TEST_F(OrcWriterTest, MultiColumn)
{
constexpr auto num_rows = 10;
auto col0_data = random_values<bool>(num_rows);
auto col1_data = random_values<int8_t>(num_rows);
auto col2_data = random_values<int16_t>(num_rows);
auto col3_data = random_values<int32_t>(num_rows);
auto col4_data = random_values<float>(num_rows);
auto col5_data = random_values<double>(num_rows);
auto col6_vals = random_values<int64_t>(num_rows);
bool_col col0(col0_data.begin(), col0_data.end());
int8_col col1(col1_data.begin(), col1_data.end());
int16_col col2(col2_data.begin(), col2_data.end());
int32_col col3(col3_data.begin(), col3_data.end());
float32_col col4(col4_data.begin(), col4_data.end());
float64_col col5(col5_data.begin(), col5_data.end());
dec128_col col6{col6_vals.begin(), col6_vals.end(), numeric::scale_type{12}};
dec128_col col7{col6_vals.begin(), col6_vals.end(), numeric::scale_type{-12}};
list_col<int64_t> col8{
{9, 8}, {7, 6, 5}, {}, {4}, {3, 2, 1, 0}, {20, 21, 22, 23, 24}, {}, {66, 666}, {}, {-1, -2}};
int32_col child_col{48, 27, 25, 31, 351, 351, 29, 15, -1, -99};
struct_col col9{child_col};
table_view expected({col0, col1, col2, col3, col4, col5, col6, col7, col8, col9});
cudf::io::table_input_metadata expected_metadata(expected);
expected_metadata.column_metadata[0].set_name("bools");
expected_metadata.column_metadata[1].set_name("int8s");
expected_metadata.column_metadata[2].set_name("int16s");
expected_metadata.column_metadata[3].set_name("int32s");
expected_metadata.column_metadata[4].set_name("floats");
expected_metadata.column_metadata[5].set_name("doubles");
expected_metadata.column_metadata[6].set_name("decimal_pos_scale");
expected_metadata.column_metadata[7].set_name("decimal_neg_scale");
expected_metadata.column_metadata[8].set_name("lists");
expected_metadata.column_metadata[9].set_name("structs");
auto filepath = temp_env->get_temp_filepath("OrcMultiColumn.orc");
cudf::io::orc_writer_options out_opts =
cudf::io::orc_writer_options::builder(cudf::io::sink_info{filepath}, expected)
.metadata(expected_metadata);
cudf::io::write_orc(out_opts);
cudf::io::orc_reader_options in_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath}).use_index(false);
auto result = cudf::io::read_orc(in_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, result.tbl->view());
cudf::test::expect_metadata_equal(expected_metadata, result.metadata);
}
TEST_F(OrcWriterTest, MultiColumnWithNulls)
{
constexpr auto num_rows = 10;
auto col0_data = random_values<bool>(num_rows);
auto col1_data = random_values<int8_t>(num_rows);
auto col2_data = random_values<int16_t>(num_rows);
auto col3_data = random_values<int32_t>(num_rows);
auto col4_data = random_values<float>(num_rows);
auto col5_data = random_values<double>(num_rows);
auto col6_vals = random_values<int32_t>(num_rows);
auto col0_mask =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return (i % 2); });
auto col1_mask =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return (i < 2); });
auto col3_mask =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return (i == (num_rows - 1)); });
auto col4_mask =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return (i >= 4 && i <= 6); });
auto col5_mask =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return (i > 8); });
auto col6_mask =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return (i % 3); });
bool_col col0{col0_data.begin(), col0_data.end(), col0_mask};
int8_col col1{col1_data.begin(), col1_data.end(), col1_mask};
int16_col col2(col2_data.begin(), col2_data.end());
int32_col col3{col3_data.begin(), col3_data.end(), col3_mask};
float32_col col4{col4_data.begin(), col4_data.end(), col4_mask};
float64_col col5{col5_data.begin(), col5_data.end(), col5_mask};
dec64_col col6{col6_vals.begin(), col6_vals.end(), col6_mask, numeric::scale_type{2}};
list_col<int32_t> col7{
{{9, 8}, {7, 6, 5}, {}, {4}, {3, 2, 1, 0}, {20, 21, 22, 23, 24}, {}, {66, 666}, {}, {-1, -2}},
col0_mask};
auto ages_col = cudf::test::fixed_width_column_wrapper<int32_t>{
{48, 27, 25, 31, 351, 351, 29, 15, -1, -99}, {1, 0, 1, 1, 0, 1, 1, 1, 0, 1}};
struct_col col8{{ages_col}, {0, 1, 1, 0, 1, 1, 0, 1, 1, 0}};
table_view expected({col0, col1, col2, col3, col4, col5, col6, col7, col8});
cudf::io::table_input_metadata expected_metadata(expected);
expected_metadata.column_metadata[0].set_name("bools");
expected_metadata.column_metadata[1].set_name("int8s");
expected_metadata.column_metadata[2].set_name("int16s");
expected_metadata.column_metadata[3].set_name("int32s");
expected_metadata.column_metadata[4].set_name("floats");
expected_metadata.column_metadata[5].set_name("doubles");
expected_metadata.column_metadata[6].set_name("decimal");
expected_metadata.column_metadata[7].set_name("lists");
expected_metadata.column_metadata[8].set_name("structs");
auto filepath = temp_env->get_temp_filepath("OrcMultiColumnWithNulls.orc");
cudf::io::orc_writer_options out_opts =
cudf::io::orc_writer_options::builder(cudf::io::sink_info{filepath}, expected)
.metadata(expected_metadata);
cudf::io::write_orc(out_opts);
cudf::io::orc_reader_options in_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath}).use_index(false);
auto result = cudf::io::read_orc(in_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, result.tbl->view());
cudf::test::expect_metadata_equal(expected_metadata, result.metadata);
}
TEST_F(OrcWriterTest, ReadZeroRows)
{
auto sequence = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i; });
constexpr auto num_rows = 10;
column_wrapper<int64_t, typename decltype(sequence)::value_type> col(sequence,
sequence + num_rows);
table_view expected({col});
auto filepath = temp_env->get_temp_filepath("OrcSingleColumn.orc");
cudf::io::orc_writer_options out_opts =
cudf::io::orc_writer_options::builder(cudf::io::sink_info{filepath}, expected);
cudf::io::write_orc(out_opts);
cudf::io::orc_reader_options in_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath})
.use_index(false)
.num_rows(0);
auto result = cudf::io::read_orc(in_opts);
EXPECT_EQ(0, result.tbl->num_rows());
EXPECT_EQ(1, result.tbl->num_columns());
}
TEST_F(OrcWriterTest, Strings)
{
std::vector<char const*> strings{
"Monday", "Monday", "Friday", "Monday", "Friday", "Friday", "Friday", "Funday"};
auto const num_rows = strings.size();
auto seq_col0 = random_values<int>(num_rows);
auto seq_col2 = random_values<float>(num_rows);
int32_col col0(seq_col0.begin(), seq_col0.end());
str_col col1(strings.begin(), strings.end());
float32_col col2(seq_col2.begin(), seq_col2.end());
table_view expected({col0, col1, col2});
cudf::io::table_input_metadata expected_metadata(expected);
expected_metadata.column_metadata[0].set_name("col_other");
expected_metadata.column_metadata[1].set_name("col_string");
expected_metadata.column_metadata[2].set_name("col_another");
auto filepath = temp_env->get_temp_filepath("OrcStrings.orc");
cudf::io::orc_writer_options out_opts =
cudf::io::orc_writer_options::builder(cudf::io::sink_info{filepath}, expected)
.metadata(expected_metadata);
cudf::io::write_orc(out_opts);
cudf::io::orc_reader_options in_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath}).use_index(false);
auto result = cudf::io::read_orc(in_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, result.tbl->view());
cudf::test::expect_metadata_equal(expected_metadata, result.metadata);
}
TEST_F(OrcWriterTest, SlicedTable)
{
// This test checks for writing zero copy, offsetted views into existing cudf tables
std::vector<char const*> strings{
"Monday", "Monday", "Friday", "Monday", "Friday", "Friday", "Friday", "Funday"};
auto const num_rows = strings.size();
auto seq_col0 = random_values<int32_t>(num_rows);
auto seq_col2 = random_values<float>(num_rows);
auto vals_col3 = random_values<int32_t>(num_rows);
int32_col col0(seq_col0.begin(), seq_col0.end());
str_col col1(strings.begin(), strings.end());
float32_col col2(seq_col2.begin(), seq_col2.end());
dec64_col col3{vals_col3.begin(), vals_col3.end(), numeric::scale_type{2}};
list_col<int64_t> col4{
{9, 8}, {7, 6, 5}, {}, {4}, {3, 2, 1, 0}, {20, 21, 22, 23, 24}, {}, {66, 666}};
int16_col ages_col{{48, 27, 25, 31, 351, 351, 29, 15}, cudf::test::iterators::null_at(5)};
struct_col col5{{ages_col}, cudf::test::iterators::null_at(4)};
table_view expected({col0, col1, col2, col3, col4, col5});
cudf::io::table_input_metadata expected_metadata(expected);
expected_metadata.column_metadata[0].set_name("col_other");
expected_metadata.column_metadata[1].set_name("col_string");
expected_metadata.column_metadata[2].set_name("col_another");
expected_metadata.column_metadata[3].set_name("col_decimal");
expected_metadata.column_metadata[4].set_name("lists");
expected_metadata.column_metadata[5].set_name("structs");
auto expected_slice = cudf::slice(expected, {2, static_cast<cudf::size_type>(num_rows)});
auto filepath = temp_env->get_temp_filepath("SlicedTable.orc");
cudf::io::orc_writer_options out_opts =
cudf::io::orc_writer_options::builder(cudf::io::sink_info{filepath}, expected_slice)
.metadata(expected_metadata);
cudf::io::write_orc(out_opts);
cudf::io::orc_reader_options in_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath});
auto result = cudf::io::read_orc(in_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected_slice, result.tbl->view());
cudf::test::expect_metadata_equal(expected_metadata, result.metadata);
}
TEST_F(OrcWriterTest, HostBuffer)
{
constexpr auto num_rows = 100 << 10;
auto const seq_col = random_values<int>(num_rows);
int32_col col(seq_col.begin(), seq_col.end());
table_view expected{{col}};
cudf::io::table_input_metadata expected_metadata(expected);
expected_metadata.column_metadata[0].set_name("col_other");
std::vector<char> out_buffer;
cudf::io::orc_writer_options out_opts =
cudf::io::orc_writer_options::builder(cudf::io::sink_info(&out_buffer), expected)
.metadata(expected_metadata);
cudf::io::write_orc(out_opts);
cudf::io::orc_reader_options in_opts =
cudf::io::orc_reader_options::builder(
cudf::io::source_info(out_buffer.data(), out_buffer.size()))
.use_index(false);
auto const result = cudf::io::read_orc(in_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, result.tbl->view());
cudf::test::expect_metadata_equal(expected_metadata, result.metadata);
}
TEST_F(OrcWriterTest, negTimestampsNano)
{
// This is a separate test because ORC format has a bug where writing a timestamp between -1 and 0
// seconds from UNIX epoch is read as that timestamp + 1 second. We mimic that behavior and so
// this test has to hardcode test values which are < -1 second.
// Details: https://github.com/rapidsai/cudf/pull/5529#issuecomment-648768925
auto timestamps_ns =
cudf::test::fixed_width_column_wrapper<cudf::timestamp_ns, cudf::timestamp_ns::rep>{
-131968727238000000,
-1530705634500000000,
-1674638741932929000,
};
cudf::table_view expected({timestamps_ns});
auto filepath = temp_env->get_temp_filepath("OrcNegTimestamp.orc");
cudf::io::orc_writer_options out_opts =
cudf::io::orc_writer_options::builder(cudf::io::sink_info{filepath}, expected);
cudf::io::write_orc(out_opts);
cudf::io::orc_reader_options in_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath}).use_index(false);
auto result = cudf::io::read_orc(in_opts);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(
expected.column(0), result.tbl->view().column(0), cudf::test::debug_output_level::ALL_ERRORS);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, result.tbl->view());
}
TEST_F(OrcWriterTest, Slice)
{
int32_col col{{1, 2, 3, 4, 5}, cudf::test::iterators::null_at(3)};
std::vector<cudf::size_type> indices{2, 5};
std::vector<cudf::column_view> result = cudf::slice(col, indices);
cudf::table_view tbl{result};
auto filepath = temp_env->get_temp_filepath("Slice.orc");
cudf::io::orc_writer_options out_opts =
cudf::io::orc_writer_options::builder(cudf::io::sink_info{filepath}, tbl);
cudf::io::write_orc(out_opts);
cudf::io::orc_reader_options in_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath});
auto read_table = cudf::io::read_orc(in_opts);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(read_table.tbl->view(), tbl);
}
TEST_F(OrcChunkedWriterTest, SingleTable)
{
srand(31337);
auto table1 = create_random_fixed_table<int>(5, 5, true);
auto filepath = temp_env->get_temp_filepath("ChunkedSingle.orc");
cudf::io::chunked_orc_writer_options opts =
cudf::io::chunked_orc_writer_options::builder(cudf::io::sink_info{filepath});
cudf::io::orc_chunked_writer(opts).write(*table1);
cudf::io::orc_reader_options read_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath});
auto result = cudf::io::read_orc(read_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(*result.tbl, *table1);
}
TEST_F(OrcChunkedWriterTest, SimpleTable)
{
srand(31337);
auto table1 = create_random_fixed_table<int>(5, 5, true);
auto table2 = create_random_fixed_table<int>(5, 5, true);
auto full_table = cudf::concatenate(std::vector<table_view>({*table1, *table2}));
auto filepath = temp_env->get_temp_filepath("ChunkedSimple.orc");
cudf::io::chunked_orc_writer_options opts =
cudf::io::chunked_orc_writer_options::builder(cudf::io::sink_info{filepath});
cudf::io::orc_chunked_writer(opts).write(*table1).write(*table2);
cudf::io::orc_reader_options read_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath});
auto result = cudf::io::read_orc(read_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(*result.tbl, *full_table);
}
TEST_F(OrcChunkedWriterTest, LargeTables)
{
srand(31337);
auto table1 = create_random_fixed_table<int>(512, 4096, true);
auto table2 = create_random_fixed_table<int>(512, 8192, true);
auto full_table = cudf::concatenate(std::vector<table_view>({*table1, *table2}));
auto filepath = temp_env->get_temp_filepath("ChunkedLarge.orc");
cudf::io::chunked_orc_writer_options opts =
cudf::io::chunked_orc_writer_options::builder(cudf::io::sink_info{filepath});
cudf::io::orc_chunked_writer(opts).write(*table1).write(*table2);
cudf::io::orc_reader_options read_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath});
auto result = cudf::io::read_orc(read_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(*result.tbl, *full_table);
}
TEST_F(OrcChunkedWriterTest, ManyTables)
{
srand(31337);
std::vector<std::unique_ptr<table>> tables;
std::vector<table_view> table_views;
constexpr int num_tables = 96;
for (int idx = 0; idx < num_tables; idx++) {
auto tbl = create_random_fixed_table<int>(16, 64, true);
table_views.push_back(*tbl);
tables.push_back(std::move(tbl));
}
auto expected = cudf::concatenate(table_views);
auto filepath = temp_env->get_temp_filepath("ChunkedManyTables.orc");
cudf::io::chunked_orc_writer_options opts =
cudf::io::chunked_orc_writer_options::builder(cudf::io::sink_info{filepath});
cudf::io::orc_chunked_writer writer(opts);
std::for_each(table_views.begin(), table_views.end(), [&writer](table_view const& tbl) {
writer.write(tbl);
});
writer.close();
cudf::io::orc_reader_options read_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath});
auto result = cudf::io::read_orc(read_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(*result.tbl, *expected);
}
TEST_F(OrcChunkedWriterTest, Metadata)
{
std::vector<char const*> strings{
"Monday", "Tuesday", "THURSDAY", "Wednesday", "Friday", "Sunday", "Saturday"};
auto const num_rows = strings.size();
auto seq_col0 = random_values<int>(num_rows);
auto seq_col2 = random_values<float>(num_rows);
int32_col col0(seq_col0.begin(), seq_col0.end());
str_col col1{strings.begin(), strings.end()};
float32_col col2(seq_col2.begin(), seq_col2.end());
table_view expected({col0, col1, col2});
cudf::io::table_input_metadata expected_metadata(expected);
expected_metadata.column_metadata[0].set_name("col_other");
expected_metadata.column_metadata[1].set_name("col_string");
expected_metadata.column_metadata[2].set_name("col_another");
auto filepath = temp_env->get_temp_filepath("ChunkedMetadata.orc");
cudf::io::chunked_orc_writer_options opts =
cudf::io::chunked_orc_writer_options::builder(cudf::io::sink_info{filepath})
.metadata(expected_metadata);
cudf::io::orc_chunked_writer(opts).write(expected).write(expected);
cudf::io::orc_reader_options read_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath});
auto result = cudf::io::read_orc(read_opts);
cudf::test::expect_metadata_equal(expected_metadata, result.metadata);
}
TEST_F(OrcChunkedWriterTest, Strings)
{
std::array mask1{true, true, false, true, true, true, true};
std::vector<char const*> h_strings1{"four", "score", "and", "seven", "years", "ago", "abcdefgh"};
str_col strings1(h_strings1.begin(), h_strings1.end(), mask1.data());
table_view tbl1({strings1});
std::array mask2{false, true, true, true, true, true, true};
std::vector<char const*> h_strings2{"ooooo", "ppppppp", "fff", "j", "cccc", "bbb", "zzzzzzzzzzz"};
str_col strings2(h_strings2.begin(), h_strings2.end(), mask2.data());
table_view tbl2({strings2});
auto expected = cudf::concatenate(std::vector<table_view>({tbl1, tbl2}));
auto filepath = temp_env->get_temp_filepath("ChunkedStrings.orc");
cudf::io::chunked_orc_writer_options opts =
cudf::io::chunked_orc_writer_options::builder(cudf::io::sink_info{filepath});
cudf::io::orc_chunked_writer(opts).write(tbl1).write(tbl2);
cudf::io::orc_reader_options read_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath});
auto result = cudf::io::read_orc(read_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(*result.tbl, *expected);
}
TEST_F(OrcChunkedWriterTest, MismatchedTypes)
{
srand(31337);
auto table1 = create_random_fixed_table<int>(4, 4, true);
auto table2 = create_random_fixed_table<float>(4, 4, true);
auto filepath = temp_env->get_temp_filepath("ChunkedMismatchedTypes.orc");
cudf::io::chunked_orc_writer_options opts =
cudf::io::chunked_orc_writer_options::builder(cudf::io::sink_info{filepath});
cudf::io::orc_chunked_writer writer(opts);
writer.write(*table1);
EXPECT_THROW(writer.write(*table2), cudf::logic_error);
}
TEST_F(OrcChunkedWriterTest, ChunkedWritingAfterClosing)
{
srand(31337);
auto table1 = create_random_fixed_table<int>(4, 4, true);
auto filepath = temp_env->get_temp_filepath("ChunkedWritingAfterClosing.orc");
cudf::io::chunked_orc_writer_options opts =
cudf::io::chunked_orc_writer_options::builder(cudf::io::sink_info{filepath});
cudf::io::orc_chunked_writer writer(opts);
writer.write(*table1);
writer.close();
EXPECT_THROW(writer.write(*table1), cudf::logic_error);
}
TEST_F(OrcChunkedWriterTest, MismatchedStructure)
{
srand(31337);
auto table1 = create_random_fixed_table<int>(4, 4, true);
auto table2 = create_random_fixed_table<int>(3, 4, true);
auto filepath = temp_env->get_temp_filepath("ChunkedMismatchedStructure.orc");
cudf::io::chunked_orc_writer_options opts =
cudf::io::chunked_orc_writer_options::builder(cudf::io::sink_info{filepath});
cudf::io::orc_chunked_writer writer(opts);
writer.write(*table1);
EXPECT_THROW(writer.write(*table2), cudf::logic_error);
}
TEST_F(OrcChunkedWriterTest, ReadStripes)
{
srand(31337);
auto table1 = create_random_fixed_table<int>(5, 5, true);
auto table2 = create_random_fixed_table<int>(5, 5, true);
auto full_table = cudf::concatenate(std::vector<table_view>({*table2, *table1, *table2}));
auto filepath = temp_env->get_temp_filepath("ChunkedStripes.orc");
cudf::io::chunked_orc_writer_options opts =
cudf::io::chunked_orc_writer_options::builder(cudf::io::sink_info{filepath});
cudf::io::orc_chunked_writer(opts).write(*table1).write(*table2);
cudf::io::orc_reader_options read_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath}).stripes({{1, 0, 1}});
auto result = cudf::io::read_orc(read_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(*result.tbl, *full_table);
}
TEST_F(OrcChunkedWriterTest, ReadStripesError)
{
srand(31337);
auto table1 = create_random_fixed_table<int>(5, 5, true);
auto filepath = temp_env->get_temp_filepath("ChunkedStripesError.orc");
cudf::io::chunked_orc_writer_options opts =
cudf::io::chunked_orc_writer_options::builder(cudf::io::sink_info{filepath});
cudf::io::orc_chunked_writer(opts).write(*table1);
cudf::io::orc_reader_options read_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath}).stripes({{0, 1}});
EXPECT_THROW(cudf::io::read_orc(read_opts), cudf::logic_error);
read_opts.set_stripes({{-1}});
EXPECT_THROW(cudf::io::read_orc(read_opts), cudf::logic_error);
}
TYPED_TEST(OrcChunkedWriterNumericTypeTest, UnalignedSize)
{
// write out two 31 row tables and make sure they get
// read back with all their validity bits in the right place
using T = TypeParam;
constexpr int num_els{31};
std::array<bool, num_els> mask{false, true, true, true, true, true, true, true, true, true, true,
true, true, true, true, true, true, true, true, true, true, true,
true, true, true, true, true, true, true, true, true};
std::array<T, num_els> c1a;
std::fill(c1a.begin(), c1a.end(), static_cast<T>(5));
std::array<T, num_els> c1b;
std::fill(c1b.begin(), c1b.end(), static_cast<T>(5));
column_wrapper<T> c1a_w(c1a.begin(), c1a.end(), mask.begin());
column_wrapper<T> c1b_w(c1b.begin(), c1b.end(), mask.begin());
table_view tbl1({c1a_w, c1b_w});
std::array<T, num_els> c2a;
std::fill(c2a.begin(), c2a.end(), static_cast<T>(8));
std::array<T, num_els> c2b;
std::fill(c2b.begin(), c2b.end(), static_cast<T>(9));
column_wrapper<T> c2a_w(c2a.begin(), c2a.end(), mask.begin());
column_wrapper<T> c2b_w(c2b.begin(), c2b.end(), mask.begin());
table_view tbl2({c2a_w, c2b_w});
auto expected = cudf::concatenate(std::vector<table_view>({tbl1, tbl2}));
auto filepath = temp_env->get_temp_filepath("ChunkedUnalignedSize.orc");
cudf::io::chunked_orc_writer_options opts =
cudf::io::chunked_orc_writer_options::builder(cudf::io::sink_info{filepath});
cudf::io::orc_chunked_writer(opts).write(tbl1).write(tbl2);
cudf::io::orc_reader_options read_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath});
auto result = cudf::io::read_orc(read_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(*result.tbl, *expected);
}
TYPED_TEST(OrcChunkedWriterNumericTypeTest, UnalignedSize2)
{
// write out two 33 row tables and make sure they get
// read back with all their validity bits in the right place
using T = TypeParam;
constexpr int num_els = 33;
std::array<bool, num_els> mask{false, true, true, true, true, true, true, true, true, true, true,
true, true, true, true, true, true, true, true, true, true, true,
true, true, true, true, true, true, true, true, true, true, true};
std::array<T, num_els> c1a;
std::fill(c1a.begin(), c1a.end(), static_cast<T>(5));
std::array<T, num_els> c1b;
std::fill(c1b.begin(), c1b.end(), static_cast<T>(5));
column_wrapper<T> c1a_w(c1a.begin(), c1a.end(), mask.begin());
column_wrapper<T> c1b_w(c1b.begin(), c1b.end(), mask.begin());
table_view tbl1({c1a_w, c1b_w});
std::array<T, num_els> c2a;
std::fill(c2a.begin(), c2a.end(), static_cast<T>(8));
std::array<T, num_els> c2b;
std::fill(c2b.begin(), c2b.end(), static_cast<T>(9));
column_wrapper<T> c2a_w(c2a.begin(), c2a.end(), mask.begin());
column_wrapper<T> c2b_w(c2b.begin(), c2b.end(), mask.begin());
table_view tbl2({c2a_w, c2b_w});
auto expected = cudf::concatenate(std::vector<table_view>({tbl1, tbl2}));
auto filepath = temp_env->get_temp_filepath("ChunkedUnalignedSize2.orc");
cudf::io::chunked_orc_writer_options opts =
cudf::io::chunked_orc_writer_options::builder(cudf::io::sink_info{filepath});
cudf::io::orc_chunked_writer(opts).write(tbl1).write(tbl2);
cudf::io::orc_reader_options read_opts =
cudf::io::orc_reader_options::builder(cudf::io::source_info{filepath});
auto result = cudf::io::read_orc(read_opts);
CUDF_TEST_EXPECT_TABLES_EQUAL(*result.tbl, *expected);
}
TEST_F(OrcReaderTest, CombinedSkipRowTest)
{
SkipRowTest skip_row;
skip_row.test(50, 75);
skip_row.test(2, 100);
skip_row.test(2, 100, 50);
skip_row.test(2, 100, 98);
skip_row.test(2, 100, 99);
skip_row.test(2, 100, 100);
skip_row.test(2, 100, 110);
}
TEST_F(OrcStatisticsTest, Basic)
{
auto sequence = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i; });
auto ts_sequence =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return (i - 4) * 1000002; });
auto dec_sequence =
cudf::detail::make_counting_transform_iterator(0, [&](auto i) { return i * 1001; });
auto validity = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2; });
std::vector<char const*> strings{
"Monday", "Monday", "Friday", "Monday", "Friday", "Friday", "Friday", "Wednesday", "Tuesday"};
int num_rows = strings.size();
column_wrapper<int32_t, typename decltype(sequence)::value_type> col1(
sequence, sequence + num_rows, validity);
column_wrapper<float, typename decltype(sequence)::value_type> col2(
sequence, sequence + num_rows, validity);
str_col col3{strings.begin(), strings.end()};
column_wrapper<cudf::timestamp_ns, typename decltype(sequence)::value_type> col4(
ts_sequence, ts_sequence + num_rows, validity);
column_wrapper<cudf::timestamp_us, typename decltype(sequence)::value_type> col5(
ts_sequence, ts_sequence + num_rows, validity);
bool_col col6({true, true, true, true, true, false, false, false, false}, validity);
cudf::test::fixed_point_column_wrapper<int64_t> col7(
dec_sequence, dec_sequence + num_rows, numeric::scale_type{-1});