-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathcolumn_writer_test.cc
More file actions
2474 lines (2150 loc) · 99.5 KB
/
column_writer_test.cc
File metadata and controls
2474 lines (2150 loc) · 99.5 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 <memory>
#include <utility>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "arrow/io/buffered.h"
#include "arrow/io/file.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/util/bit_util.h"
#include "arrow/util/bitmap_builders.h"
#include "arrow/util/config.h"
#include "arrow/util/key_value_metadata.h"
#include "parquet/bloom_filter.h"
#include "parquet/bloom_filter_writer.h"
#include "parquet/column_page.h"
#include "parquet/column_reader.h"
#include "parquet/column_writer.h"
#include "parquet/file_reader.h"
#include "parquet/file_writer.h"
#include "parquet/geospatial/statistics.h"
#include "parquet/metadata.h"
#include "parquet/page_index.h"
#include "parquet/platform.h"
#include "parquet/properties.h"
#include "parquet/statistics.h"
#include "parquet/test_util.h"
#include "parquet/types.h"
namespace bit_util = arrow::bit_util;
namespace parquet {
using schema::GroupNode;
using schema::NodePtr;
using schema::PrimitiveNode;
namespace test {
using ::testing::IsNull;
using ::testing::NotNull;
// The default size used in most tests.
const int SMALL_SIZE = 100;
#ifdef PARQUET_VALGRIND
// Larger size to test some corner cases, only used in some specific cases.
const int LARGE_SIZE = 10000;
// Very large size to test dictionary fallback.
const int VERY_LARGE_SIZE = 40000;
// Reduced dictionary page size to use for testing dictionary fallback with valgrind
const int64_t DICTIONARY_PAGE_SIZE = 1024;
#else
// Larger size to test some corner cases, only used in some specific cases.
const int LARGE_SIZE = 100000;
// Very large size to test dictionary fallback.
const int VERY_LARGE_SIZE = 400000;
// Dictionary page size to use for testing dictionary fallback
const int64_t DICTIONARY_PAGE_SIZE = 1024 * 1024;
#endif
template <typename TestType>
class TestPrimitiveWriter : public PrimitiveTypedTest<TestType> {
public:
void SetUp() {
this->SetupValuesOut(SMALL_SIZE);
writer_properties_ = default_writer_properties();
definition_levels_out_.resize(SMALL_SIZE);
repetition_levels_out_.resize(SMALL_SIZE);
this->SetUpSchema(Repetition::REQUIRED);
descr_ = this->schema_.Column(0);
}
Type::type type_num() { return TestType::type_num; }
void BuildReader(int64_t num_rows,
Compression::type compression = Compression::UNCOMPRESSED,
bool page_checksum_verify = false) {
ASSERT_OK_AND_ASSIGN(auto buffer, sink_->Finish());
auto source = std::make_shared<::arrow::io::BufferReader>(buffer);
ReaderProperties readerProperties;
readerProperties.set_page_checksum_verification(page_checksum_verify);
std::unique_ptr<PageReader> page_reader =
PageReader::Open(std::move(source), num_rows, compression, readerProperties);
reader_ = std::static_pointer_cast<TypedColumnReader<TestType>>(
ColumnReader::Make(this->descr_, std::move(page_reader)));
}
std::shared_ptr<TypedColumnWriter<TestType>> BuildWriter(
int64_t output_size = SMALL_SIZE,
const ColumnProperties& column_properties = ColumnProperties(),
const ParquetVersion::type version = ParquetVersion::PARQUET_1_0,
const ParquetDataPageVersion data_page_version = ParquetDataPageVersion::V1,
bool enable_checksum = false, int64_t page_size = kDefaultDataPageSize,
int64_t max_rows_per_page = kDefaultMaxRowsPerPage) {
sink_ = CreateOutputStream();
WriterProperties::Builder wp_builder;
wp_builder.version(version)->data_page_version(data_page_version);
if (column_properties.encoding() == Encoding::PLAIN_DICTIONARY ||
column_properties.encoding() == Encoding::RLE_DICTIONARY) {
wp_builder.enable_dictionary();
wp_builder.dictionary_pagesize_limit(DICTIONARY_PAGE_SIZE);
} else {
wp_builder.disable_dictionary();
wp_builder.encoding(column_properties.encoding());
}
if (enable_checksum) {
wp_builder.enable_page_checksum();
}
wp_builder.max_statistics_size(column_properties.max_statistics_size());
wp_builder.data_pagesize(page_size);
wp_builder.max_rows_per_page(max_rows_per_page);
writer_properties_ = wp_builder.build();
metadata_ = ColumnChunkMetaDataBuilder::Make(writer_properties_, this->descr_);
std::unique_ptr<PageWriter> pager = PageWriter::Open(
sink_, column_properties.compression(), metadata_.get(),
/* row_group_ordinal */ -1, /* column_chunk_ordinal*/ -1,
::arrow::default_memory_pool(), /* buffered_row_group */ false,
/* header_encryptor */ NULLPTR, /* data_encryptor */ NULLPTR, enable_checksum);
std::shared_ptr<ColumnWriter> writer =
ColumnWriter::Make(metadata_.get(), std::move(pager), writer_properties_.get());
return std::dynamic_pointer_cast<TypedColumnWriter<TestType>>(writer);
}
void ReadColumn(Compression::type compression = Compression::UNCOMPRESSED,
bool page_checksum_verify = false) {
BuildReader(static_cast<int64_t>(this->values_out_.size()), compression,
page_checksum_verify);
reader_->ReadBatch(static_cast<int>(this->values_out_.size()),
definition_levels_out_.data(), repetition_levels_out_.data(),
this->values_out_ptr_, &values_read_);
this->SyncValuesOut();
}
void ReadColumnFully(Compression::type compression = Compression::UNCOMPRESSED,
bool page_checksum_verify = false);
void TestRequiredWithEncoding(Encoding::type encoding) {
return TestRequiredWithSettings(encoding, Compression::UNCOMPRESSED, false, false);
}
void TestRequiredWithSettings(
Encoding::type encoding, Compression::type compression, bool enable_dictionary,
bool enable_statistics, int64_t num_rows = SMALL_SIZE,
int compression_level = Codec::UseDefaultCompressionLevel(),
bool enable_checksum = false) {
this->GenerateData(num_rows);
this->WriteRequiredWithSettings(encoding, compression, enable_dictionary,
enable_statistics, compression_level, num_rows,
enable_checksum);
ASSERT_NO_FATAL_FAILURE(this->ReadAndCompare(compression, num_rows, enable_checksum));
this->WriteRequiredWithSettingsSpaced(encoding, compression, enable_dictionary,
enable_statistics, num_rows, compression_level,
enable_checksum);
ASSERT_NO_FATAL_FAILURE(this->ReadAndCompare(compression, num_rows, enable_checksum));
}
void TestRequiredWithCodecOptions(Encoding::type encoding,
Compression::type compression, bool enable_dictionary,
bool enable_statistics, int64_t num_rows = SMALL_SIZE,
const std::shared_ptr<CodecOptions>& codec_options =
std::make_shared<CodecOptions>(),
bool enable_checksum = false) {
this->GenerateData(num_rows);
this->WriteRequiredWithCodecOptions(encoding, compression, enable_dictionary,
enable_statistics, codec_options, num_rows,
enable_checksum);
ASSERT_NO_FATAL_FAILURE(this->ReadAndCompare(compression, num_rows, enable_checksum));
}
void TestDictionaryFallbackEncoding(ParquetVersion::type version,
ParquetDataPageVersion data_page_version) {
this->GenerateData(VERY_LARGE_SIZE);
ColumnProperties column_properties;
column_properties.set_dictionary_enabled(true);
if (version == ParquetVersion::PARQUET_1_0) {
column_properties.set_encoding(Encoding::PLAIN_DICTIONARY);
} else {
column_properties.set_encoding(Encoding::RLE_DICTIONARY);
}
auto writer =
this->BuildWriter(VERY_LARGE_SIZE, column_properties, version, data_page_version);
writer->WriteBatch(this->values_.size(), nullptr, nullptr, this->values_ptr_);
writer->Close();
// Read all rows so we are sure that also the non-dictionary pages are read correctly
this->SetupValuesOut(VERY_LARGE_SIZE);
this->ReadColumnFully();
ASSERT_EQ(VERY_LARGE_SIZE, this->values_read_);
this->values_.resize(VERY_LARGE_SIZE);
ASSERT_EQ(this->values_, this->values_out_);
std::vector<Encoding::type> encodings_vector = this->metadata_encodings();
std::set<Encoding::type> encodings(encodings_vector.begin(), encodings_vector.end());
if (this->type_num() == Type::BOOLEAN) {
// Dictionary encoding is not allowed for boolean type
std::set<Encoding::type> expected;
if (version != ParquetVersion::PARQUET_1_0 &&
data_page_version == ParquetDataPageVersion::V2) {
// There is only 1 encoding (RLE) in a fallback case for version 2.0 and data page
// v2 enabled.
expected = {Encoding::RLE};
} else {
// There are 2 encodings (PLAIN, RLE) in a non dictionary encoding case for
// version 1.0 or data page v1. Note that RLE is used for DL/RL.
expected = {Encoding::PLAIN, Encoding::RLE};
}
ASSERT_EQ(encodings, expected);
} else if (version == ParquetVersion::PARQUET_1_0) {
// There are 3 encodings (PLAIN_DICTIONARY, PLAIN, RLE) in a fallback case
// for version 1.0
std::set<Encoding::type> expected(
{Encoding::PLAIN_DICTIONARY, Encoding::PLAIN, Encoding::RLE});
ASSERT_EQ(encodings, expected);
} else {
// There are 3 encodings (RLE_DICTIONARY, PLAIN, RLE) in a fallback case for
// version 2.0
std::set<Encoding::type> expected(
{Encoding::RLE_DICTIONARY, Encoding::PLAIN, Encoding::RLE});
ASSERT_EQ(encodings, expected);
}
std::vector<parquet::PageEncodingStats> encoding_stats =
this->metadata_encoding_stats();
if (this->type_num() == Type::BOOLEAN) {
ASSERT_EQ(encoding_stats[0].encoding,
version != ParquetVersion::PARQUET_1_0 &&
data_page_version == ParquetDataPageVersion::V2
? Encoding::RLE
: Encoding::PLAIN);
ASSERT_EQ(encoding_stats[0].page_type, PageType::DATA_PAGE);
} else if (version == ParquetVersion::PARQUET_1_0) {
std::vector<Encoding::type> expected(
{Encoding::PLAIN_DICTIONARY, Encoding::PLAIN, Encoding::PLAIN_DICTIONARY});
ASSERT_EQ(encoding_stats[0].encoding, expected[0]);
ASSERT_EQ(encoding_stats[0].page_type, PageType::DICTIONARY_PAGE);
for (size_t i = 1; i < encoding_stats.size(); i++) {
ASSERT_EQ(encoding_stats[i].encoding, expected[i]);
ASSERT_EQ(encoding_stats[i].page_type, PageType::DATA_PAGE);
}
} else {
std::vector<Encoding::type> expected(
{Encoding::PLAIN, Encoding::PLAIN, Encoding::RLE_DICTIONARY});
ASSERT_EQ(encoding_stats[0].encoding, expected[0]);
ASSERT_EQ(encoding_stats[0].page_type, PageType::DICTIONARY_PAGE);
for (size_t i = 1; i < encoding_stats.size(); i++) {
ASSERT_EQ(encoding_stats[i].encoding, expected[i]);
ASSERT_EQ(encoding_stats[i].page_type, PageType::DATA_PAGE);
}
}
}
void WriteRequiredWithSettings(Encoding::type encoding, Compression::type compression,
bool enable_dictionary, bool enable_statistics,
int compression_level, int64_t num_rows,
bool enable_checksum) {
ColumnProperties column_properties(encoding, compression, enable_dictionary,
enable_statistics);
column_properties.set_codec_options(
std::make_shared<CodecOptions>(compression_level));
std::shared_ptr<TypedColumnWriter<TestType>> writer =
this->BuildWriter(num_rows, column_properties, ParquetVersion::PARQUET_1_0,
ParquetDataPageVersion::V1, enable_checksum);
writer->WriteBatch(this->values_.size(), nullptr, nullptr, this->values_ptr_);
// The behaviour should be independent from the number of Close() calls
writer->Close();
writer->Close();
}
void WriteRequiredWithSettingsSpaced(Encoding::type encoding,
Compression::type compression,
bool enable_dictionary, bool enable_statistics,
int64_t num_rows, int compression_level,
bool enable_checksum) {
std::vector<uint8_t> valid_bits(
bit_util::BytesForBits(static_cast<uint32_t>(this->values_.size())) + 1, 255);
ColumnProperties column_properties(encoding, compression, enable_dictionary,
enable_statistics);
column_properties.set_codec_options(
std::make_shared<CodecOptions>(compression_level));
std::shared_ptr<TypedColumnWriter<TestType>> writer =
this->BuildWriter(num_rows, column_properties, ParquetVersion::PARQUET_1_0,
ParquetDataPageVersion::V1, enable_checksum);
writer->WriteBatchSpaced(this->values_.size(), nullptr, nullptr, valid_bits.data(), 0,
this->values_ptr_);
// The behaviour should be independent from the number of Close() calls
writer->Close();
writer->Close();
}
void WriteRequiredWithCodecOptions(Encoding::type encoding,
Compression::type compression,
bool enable_dictionary, bool enable_statistics,
const std::shared_ptr<CodecOptions>& codec_options,
int64_t num_rows, bool enable_checksum) {
ColumnProperties column_properties(encoding, compression, enable_dictionary,
enable_statistics);
column_properties.set_codec_options(codec_options);
std::shared_ptr<TypedColumnWriter<TestType>> writer =
this->BuildWriter(num_rows, column_properties, ParquetVersion::PARQUET_1_0,
ParquetDataPageVersion::V1, enable_checksum);
writer->WriteBatch(this->values_.size(), nullptr, nullptr, this->values_ptr_);
// The behaviour should be independent from the number of Close() calls
writer->Close();
writer->Close();
}
void ReadAndCompare(Compression::type compression, int64_t num_rows,
bool page_checksum_verify) {
this->SetupValuesOut(num_rows);
this->ReadColumnFully(compression, page_checksum_verify);
auto comparator = MakeComparator<TestType>(this->descr_);
for (size_t i = 0; i < this->values_.size(); i++) {
if (comparator->Compare(this->values_[i], this->values_out_[i]) ||
comparator->Compare(this->values_out_[i], this->values_[i])) {
ARROW_SCOPED_TRACE("i = ", i);
}
ASSERT_FALSE(comparator->Compare(this->values_[i], this->values_out_[i]));
ASSERT_FALSE(comparator->Compare(this->values_out_[i], this->values_[i]));
}
ASSERT_EQ(this->values_, this->values_out_);
}
int64_t metadata_num_values() {
// Metadata accessor must be created lazily.
// This is because the ColumnChunkMetaData semantics dictate the metadata object is
// complete (no changes to the metadata buffer can be made after instantiation)
auto metadata_accessor =
ColumnChunkMetaData::Make(metadata_->contents(), this->descr_);
return metadata_accessor->num_values();
}
bool metadata_is_stats_set() {
// Metadata accessor must be created lazily.
// This is because the ColumnChunkMetaData semantics dictate the metadata object is
// complete (no changes to the metadata buffer can be made after instantiation)
ApplicationVersion app_version(this->writer_properties_->created_by());
auto metadata_accessor = ColumnChunkMetaData::Make(
metadata_->contents(), this->descr_, default_reader_properties(), &app_version);
return metadata_accessor->is_stats_set();
}
std::pair<bool, bool> metadata_stats_has_min_max() {
// Metadata accessor must be created lazily.
// This is because the ColumnChunkMetaData semantics dictate the metadata object is
// complete (no changes to the metadata buffer can be made after instantiation)
ApplicationVersion app_version(this->writer_properties_->created_by());
auto metadata_accessor = ColumnChunkMetaData::Make(
metadata_->contents(), this->descr_, default_reader_properties(), &app_version);
auto encoded_stats = metadata_accessor->statistics()->Encode();
return {encoded_stats.HasMin(), encoded_stats.HasMax()};
}
std::vector<Encoding::type> metadata_encodings() {
// Metadata accessor must be created lazily.
// This is because the ColumnChunkMetaData semantics dictate the metadata object is
// complete (no changes to the metadata buffer can be made after instantiation)
auto metadata_accessor =
ColumnChunkMetaData::Make(metadata_->contents(), this->descr_);
return metadata_accessor->encodings();
}
std::vector<parquet::PageEncodingStats> metadata_encoding_stats() {
// Metadata accessor must be created lazily.
// This is because the ColumnChunkMetaData semantics dictate the metadata object is
// complete (no changes to the metadata buffer can be made after instantiation)
auto metadata_accessor =
ColumnChunkMetaData::Make(metadata_->contents(), this->descr_);
return metadata_accessor->encoding_stats();
}
std::shared_ptr<const KeyValueMetadata> metadata_key_value_metadata() {
// Metadata accessor must be created lazily.
// This is because the ColumnChunkMetaData semantics dictate the metadata object is
// complete (no changes to the metadata buffer can be made after instantiation)
auto metadata_accessor =
ColumnChunkMetaData::Make(metadata_->contents(), this->descr_);
return metadata_accessor->key_value_metadata();
}
std::unique_ptr<ColumnChunkMetaData> metadata_accessor() {
// Metadata accessor must be created lazily.
// This is because the ColumnChunkMetaData semantics dictate the metadata object is
// complete (no changes to the metadata buffer can be made after instantiation)
ApplicationVersion app_version(this->writer_properties_->created_by());
return ColumnChunkMetaData::Make(metadata_->contents(), this->descr_,
default_reader_properties(), &app_version);
}
EncodedStatistics metadata_encoded_stats() { return metadata_stats()->Encode(); }
std::shared_ptr<Statistics> metadata_stats() {
return metadata_accessor()->statistics();
}
std::shared_ptr<geospatial::GeoStatistics> metadata_geo_stats() {
return metadata_accessor()->geo_statistics();
}
protected:
int64_t values_read_;
// Keep the reader alive as for ByteArray the lifetime of the ByteArray
// content is bound to the reader.
std::shared_ptr<TypedColumnReader<TestType>> reader_;
std::vector<int16_t> definition_levels_out_;
std::vector<int16_t> repetition_levels_out_;
const ColumnDescriptor* descr_;
protected:
std::unique_ptr<ColumnChunkMetaDataBuilder> metadata_;
std::shared_ptr<::arrow::io::BufferOutputStream> sink_;
std::shared_ptr<WriterProperties> writer_properties_;
std::vector<std::vector<uint8_t>> data_buffer_;
};
template <typename TestType>
void TestPrimitiveWriter<TestType>::ReadColumnFully(Compression::type compression,
bool page_checksum_verify) {
int64_t total_values = static_cast<int64_t>(this->values_out_.size());
BuildReader(total_values, compression, page_checksum_verify);
values_read_ = 0;
while (values_read_ < total_values) {
int64_t values_read_recently = 0;
reader_->ReadBatch(
static_cast<int>(this->values_out_.size()) - static_cast<int>(values_read_),
definition_levels_out_.data() + values_read_,
repetition_levels_out_.data() + values_read_,
this->values_out_ptr_ + values_read_, &values_read_recently);
values_read_ += values_read_recently;
}
this->SyncValuesOut();
}
template <>
void TestPrimitiveWriter<Int96Type>::ReadAndCompare(Compression::type compression,
int64_t num_rows,
bool page_checksum_verify) {
this->SetupValuesOut(num_rows);
this->ReadColumnFully(compression, page_checksum_verify);
auto comparator = MakeComparator<Int96Type>(Type::INT96, SortOrder::SIGNED);
for (size_t i = 0; i < this->values_.size(); i++) {
if (comparator->Compare(this->values_[i], this->values_out_[i]) ||
comparator->Compare(this->values_out_[i], this->values_[i])) {
ARROW_SCOPED_TRACE("i = ", i);
}
ASSERT_FALSE(comparator->Compare(this->values_[i], this->values_out_[i]));
ASSERT_FALSE(comparator->Compare(this->values_out_[i], this->values_[i]));
}
ASSERT_EQ(this->values_, this->values_out_);
}
template <>
void TestPrimitiveWriter<FLBAType>::ReadColumnFully(Compression::type compression,
bool page_checksum_verify) {
int64_t total_values = static_cast<int64_t>(this->values_out_.size());
BuildReader(total_values, compression, page_checksum_verify);
this->data_buffer_.clear();
values_read_ = 0;
while (values_read_ < total_values) {
int64_t values_read_recently = 0;
reader_->ReadBatch(
static_cast<int>(this->values_out_.size()) - static_cast<int>(values_read_),
definition_levels_out_.data() + values_read_,
repetition_levels_out_.data() + values_read_,
this->values_out_ptr_ + values_read_, &values_read_recently);
// Copy contents of the pointers
std::vector<uint8_t> data(values_read_recently * this->descr_->type_length());
uint8_t* data_ptr = data.data();
for (int64_t i = 0; i < values_read_recently; i++) {
memcpy(data_ptr + this->descr_->type_length() * i,
this->values_out_[i + values_read_].ptr, this->descr_->type_length());
this->values_out_[i + values_read_].ptr =
data_ptr + this->descr_->type_length() * i;
}
data_buffer_.emplace_back(std::move(data));
values_read_ += values_read_recently;
}
this->SyncValuesOut();
}
template <>
void TestPrimitiveWriter<ByteArrayType>::ReadColumnFully(Compression::type compression,
bool page_checksum_verify) {
int64_t total_values = static_cast<int64_t>(this->values_out_.size());
BuildReader(total_values, compression, page_checksum_verify);
this->data_buffer_.clear();
values_read_ = 0;
while (values_read_ < total_values) {
int64_t values_read_recently = 0;
reader_->ReadBatch(
static_cast<int>(this->values_out_.size()) - static_cast<int>(values_read_),
definition_levels_out_.data() + values_read_,
repetition_levels_out_.data() + values_read_,
this->values_out_ptr_ + values_read_, &values_read_recently);
// Compute the total length of the data
int64_t total_length = 0;
for (int64_t i = 0; i < values_read_recently; i++) {
total_length += this->values_out_[i + values_read_].len;
}
// Copy contents of the pointers
std::vector<uint8_t> data(total_length);
uint8_t* data_ptr = data.data();
for (int64_t i = 0; i < values_read_recently; i++) {
const ByteArray& value = this->values_out_ptr_[i + values_read_];
memcpy(data_ptr, value.ptr, value.len);
this->values_out_[i + values_read_].ptr = data_ptr;
data_ptr += value.len;
}
data_buffer_.emplace_back(std::move(data));
values_read_ += values_read_recently;
}
this->SyncValuesOut();
}
typedef ::testing::Types<Int32Type, Int64Type, Int96Type, FloatType, DoubleType,
BooleanType, ByteArrayType, FLBAType>
TestTypes;
TYPED_TEST_SUITE(TestPrimitiveWriter, TestTypes);
using TestValuesWriterInt32Type = TestPrimitiveWriter<Int32Type>;
using TestValuesWriterInt64Type = TestPrimitiveWriter<Int64Type>;
using TestByteArrayValuesWriter = TestPrimitiveWriter<ByteArrayType>;
using TestFixedLengthByteArrayValuesWriter = TestPrimitiveWriter<FLBAType>;
using ::testing::HasSubstr;
TYPED_TEST(TestPrimitiveWriter, RequiredPlain) {
this->TestRequiredWithEncoding(Encoding::PLAIN);
}
TYPED_TEST(TestPrimitiveWriter, RequiredDictionary) {
this->TestRequiredWithEncoding(Encoding::PLAIN_DICTIONARY);
}
/*
TYPED_TEST(TestPrimitiveWriter, RequiredRLE) {
this->TestRequiredWithEncoding(Encoding::RLE);
}
TYPED_TEST(TestPrimitiveWriter, RequiredBitPacked) {
this->TestRequiredWithEncoding(Encoding::BIT_PACKED);
}
*/
TEST_F(TestValuesWriterInt32Type, RequiredDeltaBinaryPacked) {
this->TestRequiredWithEncoding(Encoding::DELTA_BINARY_PACKED);
}
TEST_F(TestValuesWriterInt32Type, RequiredByteStreamSplit) {
this->TestRequiredWithEncoding(Encoding::BYTE_STREAM_SPLIT);
}
TEST_F(TestValuesWriterInt64Type, RequiredDeltaBinaryPacked) {
this->TestRequiredWithEncoding(Encoding::DELTA_BINARY_PACKED);
}
TEST_F(TestValuesWriterInt64Type, RequiredByteStreamSplit) {
this->TestRequiredWithEncoding(Encoding::BYTE_STREAM_SPLIT);
}
TEST_F(TestByteArrayValuesWriter, RequiredDeltaLengthByteArray) {
this->TestRequiredWithEncoding(Encoding::DELTA_LENGTH_BYTE_ARRAY);
}
TEST_F(TestByteArrayValuesWriter, RequiredDeltaByteArray) {
this->TestRequiredWithEncoding(Encoding::DELTA_BYTE_ARRAY);
}
TEST_F(TestFixedLengthByteArrayValuesWriter, RequiredDeltaByteArray) {
this->TestRequiredWithEncoding(Encoding::DELTA_BYTE_ARRAY);
}
TEST_F(TestFixedLengthByteArrayValuesWriter, RequiredByteStreamSplit) {
this->TestRequiredWithEncoding(Encoding::BYTE_STREAM_SPLIT);
}
TYPED_TEST(TestPrimitiveWriter, RequiredRLEDictionary) {
this->TestRequiredWithEncoding(Encoding::RLE_DICTIONARY);
}
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithStats) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::UNCOMPRESSED, false, true,
LARGE_SIZE);
}
#ifdef ARROW_WITH_SNAPPY
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithSnappyCompression) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::SNAPPY, false, false,
LARGE_SIZE);
}
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithStatsAndSnappyCompression) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::SNAPPY, false, true,
LARGE_SIZE);
}
#endif
#ifdef ARROW_WITH_BROTLI
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithBrotliCompression) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::BROTLI, false, false,
LARGE_SIZE);
}
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithBrotliCompressionAndLevel) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::BROTLI, false, false,
LARGE_SIZE, 10);
}
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithStatsAndBrotliCompression) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::BROTLI, false, true,
LARGE_SIZE);
}
#endif
#ifdef ARROW_WITH_ZLIB
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithGzipCompression) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::GZIP, false, false,
LARGE_SIZE);
}
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithGzipCompressionAndLevel) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::GZIP, false, false,
LARGE_SIZE, 10);
}
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithStatsAndGzipCompression) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::GZIP, false, true,
LARGE_SIZE);
}
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithGzipCodecOptions) {
auto codec_options = std::make_shared<::arrow::util::GZipCodecOptions>();
codec_options->gzip_format = ::arrow::util::GZipFormat::GZIP;
codec_options->window_bits = 12;
this->TestRequiredWithCodecOptions(Encoding::PLAIN, Compression::GZIP, false, false,
LARGE_SIZE, codec_options);
}
#endif
#ifdef ARROW_WITH_LZ4
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithLz4Compression) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::LZ4, false, false,
LARGE_SIZE);
}
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithStatsAndLz4Compression) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::LZ4, false, true,
LARGE_SIZE);
}
#endif
#ifdef ARROW_WITH_ZSTD
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithZstdCompression) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::ZSTD, false, false,
LARGE_SIZE);
}
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithZstdCompressionAndLevel) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::ZSTD, false, false,
LARGE_SIZE, 6);
}
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithStatsAndZstdCompression) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::ZSTD, false, true,
LARGE_SIZE);
}
TYPED_TEST(TestPrimitiveWriter, RequiredPlainWithZstdCodecOptions) {
constexpr int ZSTD_c_windowLog = 101;
auto codec_options = std::make_shared<::arrow::util::ZstdCodecOptions>();
codec_options->compression_context_params = {{ZSTD_c_windowLog, 23}};
this->TestRequiredWithCodecOptions(Encoding::PLAIN, Compression::ZSTD, false, false,
LARGE_SIZE, codec_options);
}
#endif
TYPED_TEST(TestPrimitiveWriter, Optional) {
// Optional and non-repeated, with definition levels
// but no repetition levels
this->SetUpSchema(Repetition::OPTIONAL);
this->GenerateData(SMALL_SIZE);
std::vector<int16_t> definition_levels(SMALL_SIZE, 1);
definition_levels[1] = 0;
auto writer = this->BuildWriter();
writer->WriteBatch(this->values_.size(), definition_levels.data(), nullptr,
this->values_ptr_);
writer->Close();
// PARQUET-703
ASSERT_EQ(100, this->metadata_num_values());
this->ReadColumn();
ASSERT_EQ(99, this->values_read_);
this->values_out_.resize(99);
this->values_.resize(99);
ASSERT_EQ(this->values_, this->values_out_);
}
TYPED_TEST(TestPrimitiveWriter, OptionalSpaced) {
// Optional and non-repeated, with definition levels
// but no repetition levels
this->SetUpSchema(Repetition::OPTIONAL);
this->GenerateData(SMALL_SIZE);
std::vector<int16_t> definition_levels(SMALL_SIZE, 1);
std::vector<uint8_t> valid_bits(::arrow::bit_util::BytesForBits(SMALL_SIZE), 255);
definition_levels[SMALL_SIZE - 1] = 0;
::arrow::bit_util::ClearBit(valid_bits.data(), SMALL_SIZE - 1);
definition_levels[1] = 0;
::arrow::bit_util::ClearBit(valid_bits.data(), 1);
auto writer = this->BuildWriter();
writer->WriteBatchSpaced(this->values_.size(), definition_levels.data(), nullptr,
valid_bits.data(), 0, this->values_ptr_);
writer->Close();
// PARQUET-703
ASSERT_EQ(100, this->metadata_num_values());
this->ReadColumn();
ASSERT_EQ(98, this->values_read_);
this->values_out_.resize(98);
this->values_.resize(99);
this->values_.erase(this->values_.begin() + 1);
ASSERT_EQ(this->values_, this->values_out_);
}
TYPED_TEST(TestPrimitiveWriter, Repeated) {
// Optional and repeated, so definition and repetition levels
this->SetUpSchema(Repetition::REPEATED);
this->GenerateData(SMALL_SIZE);
std::vector<int16_t> definition_levels(SMALL_SIZE, 1);
definition_levels[1] = 0;
std::vector<int16_t> repetition_levels(SMALL_SIZE, 0);
auto writer = this->BuildWriter();
writer->WriteBatch(this->values_.size(), definition_levels.data(),
repetition_levels.data(), this->values_ptr_);
writer->Close();
this->ReadColumn();
ASSERT_EQ(SMALL_SIZE - 1, this->values_read_);
this->values_out_.resize(SMALL_SIZE - 1);
this->values_.resize(SMALL_SIZE - 1);
ASSERT_EQ(this->values_, this->values_out_);
}
TYPED_TEST(TestPrimitiveWriter, RequiredLargeChunk) {
this->GenerateData(LARGE_SIZE);
// Test case 1: required and non-repeated, so no definition or repetition levels
auto writer = this->BuildWriter(LARGE_SIZE);
writer->WriteBatch(this->values_.size(), nullptr, nullptr, this->values_ptr_);
writer->Close();
// Just read the first SMALL_SIZE rows to ensure we could read it back in
this->ReadColumn();
ASSERT_EQ(SMALL_SIZE, this->values_read_);
this->values_.resize(SMALL_SIZE);
ASSERT_EQ(this->values_, this->values_out_);
}
// Test cases for dictionary fallback encoding
TYPED_TEST(TestPrimitiveWriter, DictionaryFallbackVersion1_0) {
this->TestDictionaryFallbackEncoding(ParquetVersion::PARQUET_1_0,
ParquetDataPageVersion::V1);
}
TYPED_TEST(TestPrimitiveWriter, DictionaryFallbackVersion2_0) {
this->TestDictionaryFallbackEncoding(ParquetVersion::PARQUET_2_4,
ParquetDataPageVersion::V1);
this->TestDictionaryFallbackEncoding(ParquetVersion::PARQUET_2_4,
ParquetDataPageVersion::V2);
this->TestDictionaryFallbackEncoding(ParquetVersion::PARQUET_2_6,
ParquetDataPageVersion::V1);
this->TestDictionaryFallbackEncoding(ParquetVersion::PARQUET_2_6,
ParquetDataPageVersion::V2);
}
TEST(TestWriter, NullValuesBuffer) {
std::shared_ptr<::arrow::io::BufferOutputStream> sink = CreateOutputStream();
const auto item_node = schema::PrimitiveNode::Make(
"item", Repetition::REQUIRED, LogicalType::Int(32, true), Type::INT32);
const auto list_node =
schema::GroupNode::Make("list", Repetition::REPEATED, {item_node});
const auto column_node = schema::GroupNode::Make(
"array_of_ints_column", Repetition::OPTIONAL, {list_node}, LogicalType::List());
const auto schema_node =
schema::GroupNode::Make("schema", Repetition::REQUIRED, {column_node});
auto file_writer = ParquetFileWriter::Open(
sink, std::dynamic_pointer_cast<schema::GroupNode>(schema_node));
auto group_writer = file_writer->AppendRowGroup();
auto column_writer = group_writer->NextColumn();
auto typed_writer = dynamic_cast<Int32Writer*>(column_writer);
const int64_t num_values = 1;
const int16_t def_levels[] = {0};
const int16_t rep_levels[] = {0};
const uint8_t valid_bits[] = {0};
const int64_t valid_bits_offset = 0;
const int32_t* values = nullptr;
typed_writer->WriteBatchSpaced(num_values, def_levels, rep_levels, valid_bits,
valid_bits_offset, values);
}
TYPED_TEST(TestPrimitiveWriter, RequiredPlainChecksum) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::UNCOMPRESSED,
/* enable_dictionary */ false, false, SMALL_SIZE,
Codec::UseDefaultCompressionLevel(),
/* enable_checksum */ true);
}
TYPED_TEST(TestPrimitiveWriter, RequiredDictChecksum) {
this->TestRequiredWithSettings(Encoding::PLAIN, Compression::UNCOMPRESSED,
/* enable_dictionary */ true, false, SMALL_SIZE,
Codec::UseDefaultCompressionLevel(),
/* enable_checksum */ true);
}
// PARQUET-719
// Test case for NULL values
TEST_F(TestValuesWriterInt32Type, OptionalNullValueChunk) {
this->SetUpSchema(Repetition::OPTIONAL);
this->GenerateData(LARGE_SIZE);
std::vector<int16_t> definition_levels(LARGE_SIZE, 0);
std::vector<int16_t> repetition_levels(LARGE_SIZE, 0);
auto writer = this->BuildWriter(LARGE_SIZE);
// All values being written are NULL
writer->WriteBatch(this->values_.size(), definition_levels.data(),
repetition_levels.data(), nullptr);
writer->Close();
// Just read the first SMALL_SIZE rows to ensure we could read it back in
this->ReadColumn();
ASSERT_EQ(0, this->values_read_);
}
class TestBooleanValuesWriter : public TestPrimitiveWriter<BooleanType> {
public:
void TestWithEncoding(ParquetVersion::type version,
ParquetDataPageVersion data_page_version,
const std::set<Encoding::type>& expected_encodings) {
this->SetUpSchema(Repetition::REQUIRED);
auto writer =
this->BuildWriter(SMALL_SIZE, ColumnProperties(), version, data_page_version,
/*enable_checksum*/ false);
for (int i = 0; i < SMALL_SIZE; i++) {
bool value = (i % 2 == 0) ? true : false;
writer->WriteBatch(1, nullptr, nullptr, &value);
}
writer->Close();
this->ReadColumn();
for (int i = 0; i < SMALL_SIZE; i++) {
ASSERT_EQ((i % 2 == 0) ? true : false, this->values_out_[i]) << i;
}
auto metadata_encodings = this->metadata_encodings();
std::set<Encoding::type> metadata_encodings_set{metadata_encodings.begin(),
metadata_encodings.end()};
EXPECT_EQ(expected_encodings, metadata_encodings_set);
}
};
// PARQUET-764
// Correct bitpacking for boolean write at non-byte boundaries
TEST_F(TestBooleanValuesWriter, AlternateBooleanValues) {
for (auto data_page_version :
{ParquetDataPageVersion::V1, ParquetDataPageVersion::V2}) {
TestWithEncoding(ParquetVersion::PARQUET_1_0, data_page_version,
{Encoding::PLAIN, Encoding::RLE});
}
}
// Default encoding for boolean is RLE when both V2 format and V2 pages enabled.
TEST_F(TestBooleanValuesWriter, RleEncodedBooleanValues) {
TestWithEncoding(ParquetVersion::PARQUET_2_4, ParquetDataPageVersion::V1,
{Encoding::PLAIN, Encoding::RLE});
TestWithEncoding(ParquetVersion::PARQUET_2_4, ParquetDataPageVersion::V2,
{Encoding::RLE});
}
// PARQUET-979
// Prevent writing large MIN, MAX stats
TEST_F(TestByteArrayValuesWriter, OmitStats) {
int min_len = 1024 * 4;
int max_len = 1024 * 8;
this->SetUpSchema(Repetition::REQUIRED);
auto writer = this->BuildWriter();
values_.resize(SMALL_SIZE);
InitWideByteArrayValues(SMALL_SIZE, this->values_, this->buffer_, min_len, max_len);
writer->WriteBatch(SMALL_SIZE, nullptr, nullptr, this->values_.data());
writer->Close();
auto has_min_max = this->metadata_stats_has_min_max();
ASSERT_FALSE(has_min_max.first);
ASSERT_FALSE(has_min_max.second);
}
// PARQUET-1405
// Prevent writing large stats in the DataPageHeader
TEST_F(TestByteArrayValuesWriter, OmitDataPageStats) {
int min_len = static_cast<int>(std::pow(10, 7));
int max_len = static_cast<int>(std::pow(10, 7));
this->SetUpSchema(Repetition::REQUIRED);
ColumnProperties column_properties;
column_properties.set_statistics_enabled(false);
auto writer = this->BuildWriter(SMALL_SIZE, column_properties);
values_.resize(1);
InitWideByteArrayValues(1, this->values_, this->buffer_, min_len, max_len);
writer->WriteBatch(1, nullptr, nullptr, this->values_.data());
writer->Close();
ASSERT_NO_THROW(this->ReadColumn());
}
TEST_F(TestByteArrayValuesWriter, LimitStats) {
int min_len = 1024 * 4;
int max_len = 1024 * 8;
this->SetUpSchema(Repetition::REQUIRED);
ColumnProperties column_properties;
column_properties.set_max_statistics_size(static_cast<size_t>(max_len));
auto writer = this->BuildWriter(SMALL_SIZE, column_properties);
values_.resize(SMALL_SIZE);
InitWideByteArrayValues(SMALL_SIZE, this->values_, this->buffer_, min_len, max_len);
writer->WriteBatch(SMALL_SIZE, nullptr, nullptr, this->values_.data());
writer->Close();
ASSERT_TRUE(this->metadata_is_stats_set());
}
TEST_F(TestByteArrayValuesWriter, CheckDefaultStats) {
this->SetUpSchema(Repetition::REQUIRED);
auto writer = this->BuildWriter();
this->GenerateData(SMALL_SIZE);
writer->WriteBatch(SMALL_SIZE, nullptr, nullptr, this->values_ptr_);
writer->Close();
ASSERT_TRUE(this->metadata_is_stats_set());
}
// Test for https://github.com/apache/arrow/issues/47027.
// When writing a repeated column with page indexes enabled
// and batches that are aligned with list boundaries,
// pages should be written after reaching the page limit.
TEST_F(TestValuesWriterInt32Type, PagesSplitWithListAlignedWrites) {
this->SetUpSchema(Repetition::REPEATED);
constexpr int list_length = 10;
constexpr int num_rows = 100;