forked from apache/orc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColumnReader.cc
1836 lines (1628 loc) · 61.8 KB
/
ColumnReader.cc
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 "orc/Int128.hh"
#include "Adaptor.hh"
#include "ByteRLE.hh"
#include "ColumnReader.hh"
#include "orc/Exceptions.hh"
#include "RLE.hh"
#include <math.h>
#include <iostream>
namespace orc {
StripeStreams::~StripeStreams() {
// PASS
}
inline RleVersion convertRleVersion(proto::ColumnEncoding_Kind kind) {
switch (static_cast<int64_t>(kind)) {
case proto::ColumnEncoding_Kind_DIRECT:
case proto::ColumnEncoding_Kind_DICTIONARY:
return RleVersion_1;
case proto::ColumnEncoding_Kind_DIRECT_V2:
case proto::ColumnEncoding_Kind_DICTIONARY_V2:
return RleVersion_2;
default:
throw ParseError("Unknown encoding in convertRleVersion");
}
}
ColumnReader::ColumnReader(const Type& type,
StripeStreams& stripe
): columnId(type.getColumnId()),
memoryPool(stripe.getMemoryPool()) {
std::unique_ptr<SeekableInputStream> stream =
stripe.getStream(columnId, proto::Stream_Kind_PRESENT, true);
if (stream.get()) {
notNullDecoder = createBooleanRleDecoder(std::move(stream));
}
}
ColumnReader::~ColumnReader() {
// PASS
}
uint64_t ColumnReader::skip(uint64_t numValues) {
ByteRleDecoder* decoder = notNullDecoder.get();
if (decoder) {
// page through the values that we want to skip
// and count how many are non-null
const size_t MAX_BUFFER_SIZE = 32768;
size_t bufferSize = std::min(MAX_BUFFER_SIZE,
static_cast<size_t>(numValues));
char buffer[MAX_BUFFER_SIZE];
uint64_t remaining = numValues;
while (remaining > 0) {
uint64_t chunkSize =
std::min(remaining,
static_cast<uint64_t>(bufferSize));
decoder->next(buffer, chunkSize, nullptr);
remaining -= chunkSize;
for(uint64_t i=0; i < chunkSize; ++i) {
if (!buffer[i]) {
numValues -= 1;
}
}
}
}
return numValues;
}
void ColumnReader::next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char* incomingMask) {
if (numValues > rowBatch.capacity) {
rowBatch.resize(numValues);
}
rowBatch.numElements = numValues;
ByteRleDecoder* decoder = notNullDecoder.get();
if (decoder) {
char* notNullArray = rowBatch.notNull.data();
decoder->next(notNullArray, numValues, incomingMask);
// check to see if there are nulls in this batch
for(uint64_t i=0; i < numValues; ++i) {
if (!notNullArray[i]) {
rowBatch.hasNulls = true;
return;
}
}
} else if (incomingMask) {
// If we don't have a notNull stream, copy the incomingMask
rowBatch.hasNulls = true;
memcpy(rowBatch.notNull.data(), incomingMask, numValues);
return;
}
rowBatch.hasNulls = false;
}
void ColumnReader::seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) {
if (notNullDecoder.get()) {
notNullDecoder->seek(positions.at(columnId));
}
}
/**
* Expand an array of bytes in place to the corresponding array of longs.
* Has to work backwards so that they data isn't clobbered during the
* expansion.
* @param buffer the array of chars and array of longs that need to be
* expanded
* @param numValues the number of bytes to convert to longs
*/
void expandBytesToLongs(int64_t* buffer, uint64_t numValues) {
for(size_t i=numValues - 1; i < numValues; --i) {
buffer[i] = reinterpret_cast<char *>(buffer)[i];
}
}
class BooleanColumnReader: public ColumnReader {
private:
std::unique_ptr<orc::ByteRleDecoder> rle;
public:
BooleanColumnReader(const Type& type, StripeStreams& stipe);
~BooleanColumnReader() override;
uint64_t skip(uint64_t numValues) override;
void next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char* notNull) override;
void seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) override;
};
BooleanColumnReader::BooleanColumnReader(const Type& type,
StripeStreams& stripe
): ColumnReader(type, stripe){
std::unique_ptr<SeekableInputStream> stream =
stripe.getStream(columnId, proto::Stream_Kind_DATA, true);
if (stream == nullptr)
throw ParseError("DATA stream not found in Boolean column");
rle = createBooleanRleDecoder(std::move(stream));
}
BooleanColumnReader::~BooleanColumnReader() {
// PASS
}
uint64_t BooleanColumnReader::skip(uint64_t numValues) {
numValues = ColumnReader::skip(numValues);
rle->skip(numValues);
return numValues;
}
void BooleanColumnReader::next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) {
ColumnReader::next(rowBatch, numValues, notNull);
// Since the byte rle places the output in a char* instead of long*,
// we cheat here and use the long* and then expand it in a second pass.
int64_t *ptr = dynamic_cast<LongVectorBatch&>(rowBatch).data.data();
rle->next(reinterpret_cast<char*>(ptr),
numValues, rowBatch.hasNulls ? rowBatch.notNull.data() : nullptr);
expandBytesToLongs(ptr, numValues);
}
void BooleanColumnReader::seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) {
ColumnReader::seekToRowGroup(positions);
rle->seek(positions.at(columnId));
}
class ByteColumnReader: public ColumnReader {
private:
std::unique_ptr<orc::ByteRleDecoder> rle;
public:
ByteColumnReader(const Type& type, StripeStreams& stipe);
~ByteColumnReader() override;
uint64_t skip(uint64_t numValues) override;
void next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char* notNull) override;
void seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) override;
};
ByteColumnReader::ByteColumnReader(const Type& type,
StripeStreams& stripe
): ColumnReader(type, stripe){
std::unique_ptr<SeekableInputStream> stream =
stripe.getStream(columnId, proto::Stream_Kind_DATA, true);
if (stream == nullptr)
throw ParseError("DATA stream not found in Byte column");
rle = createByteRleDecoder(std::move(stream));
}
ByteColumnReader::~ByteColumnReader() {
// PASS
}
uint64_t ByteColumnReader::skip(uint64_t numValues) {
numValues = ColumnReader::skip(numValues);
rle->skip(numValues);
return numValues;
}
void ByteColumnReader::next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) {
ColumnReader::next(rowBatch, numValues, notNull);
// Since the byte rle places the output in a char* instead of long*,
// we cheat here and use the long* and then expand it in a second pass.
int64_t *ptr = dynamic_cast<LongVectorBatch&>(rowBatch).data.data();
rle->next(reinterpret_cast<char*>(ptr),
numValues, rowBatch.hasNulls ? rowBatch.notNull.data() : nullptr);
expandBytesToLongs(ptr, numValues);
}
void ByteColumnReader::seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) {
ColumnReader::seekToRowGroup(positions);
rle->seek(positions.at(columnId));
}
class IntegerColumnReader: public ColumnReader {
protected:
std::unique_ptr<orc::RleDecoder> rle;
public:
IntegerColumnReader(const Type& type, StripeStreams& stripe);
~IntegerColumnReader() override;
uint64_t skip(uint64_t numValues) override;
void next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char* notNull) override;
void seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) override;
};
IntegerColumnReader::IntegerColumnReader(const Type& type,
StripeStreams& stripe
): ColumnReader(type, stripe) {
RleVersion vers = convertRleVersion(stripe.getEncoding(columnId).kind());
std::unique_ptr<SeekableInputStream> stream =
stripe.getStream(columnId, proto::Stream_Kind_DATA, true);
if (stream == nullptr)
throw ParseError("DATA stream not found in Integer column");
rle = createRleDecoder(std::move(stream), true, vers, memoryPool);
}
IntegerColumnReader::~IntegerColumnReader() {
// PASS
}
uint64_t IntegerColumnReader::skip(uint64_t numValues) {
numValues = ColumnReader::skip(numValues);
rle->skip(numValues);
return numValues;
}
void IntegerColumnReader::next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) {
ColumnReader::next(rowBatch, numValues, notNull);
rle->next(dynamic_cast<LongVectorBatch&>(rowBatch).data.data(),
numValues, rowBatch.hasNulls ? rowBatch.notNull.data() : nullptr);
}
void IntegerColumnReader::seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) {
ColumnReader::seekToRowGroup(positions);
rle->seek(positions.at(columnId));
}
class TimestampColumnReader: public ColumnReader {
private:
std::unique_ptr<orc::RleDecoder> secondsRle;
std::unique_ptr<orc::RleDecoder> nanoRle;
const Timezone& writerTimezone;
const int64_t epochOffset;
public:
TimestampColumnReader(const Type& type, StripeStreams& stripe);
~TimestampColumnReader() override;
uint64_t skip(uint64_t numValues) override;
void next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char* notNull) override;
void seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) override;
};
TimestampColumnReader::TimestampColumnReader(const Type& type,
StripeStreams& stripe
): ColumnReader(type, stripe),
writerTimezone(stripe.getWriterTimezone()),
epochOffset(writerTimezone.getEpoch()) {
RleVersion vers = convertRleVersion(stripe.getEncoding(columnId).kind());
std::unique_ptr<SeekableInputStream> stream =
stripe.getStream(columnId, proto::Stream_Kind_DATA, true);
if (stream == nullptr)
throw ParseError("DATA stream not found in Timestamp column");
secondsRle = createRleDecoder(std::move(stream), true, vers, memoryPool);
stream = stripe.getStream(columnId, proto::Stream_Kind_SECONDARY, true);
if (stream == nullptr)
throw ParseError("SECONDARY stream not found in Timestamp column");
nanoRle = createRleDecoder(std::move(stream), false, vers, memoryPool);
}
TimestampColumnReader::~TimestampColumnReader() {
// PASS
}
uint64_t TimestampColumnReader::skip(uint64_t numValues) {
numValues = ColumnReader::skip(numValues);
secondsRle->skip(numValues);
nanoRle->skip(numValues);
return numValues;
}
void TimestampColumnReader::next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) {
ColumnReader::next(rowBatch, numValues, notNull);
notNull = rowBatch.hasNulls ? rowBatch.notNull.data() : nullptr;
TimestampVectorBatch& timestampBatch =
dynamic_cast<TimestampVectorBatch&>(rowBatch);
int64_t *secsBuffer = timestampBatch.data.data();
secondsRle->next(secsBuffer, numValues, notNull);
int64_t *nanoBuffer = timestampBatch.nanoseconds.data();
nanoRle->next(nanoBuffer, numValues, notNull);
// Construct the values
for(uint64_t i=0; i < numValues; i++) {
if (notNull == nullptr || notNull[i]) {
uint64_t zeros = nanoBuffer[i] & 0x7;
nanoBuffer[i] >>= 3;
if (zeros != 0) {
for(uint64_t j = 0; j <= zeros; ++j) {
nanoBuffer[i] *= 10;
}
}
int64_t writerTime = secsBuffer[i] + epochOffset;
secsBuffer[i] = writerTimezone.convertToUTC(writerTime);
if (secsBuffer[i] < 0 && nanoBuffer[i] != 0) {
secsBuffer[i] -= 1;
}
}
}
}
void TimestampColumnReader::seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) {
ColumnReader::seekToRowGroup(positions);
secondsRle->seek(positions.at(columnId));
nanoRle->seek(positions.at(columnId));
}
class DoubleColumnReader: public ColumnReader {
public:
DoubleColumnReader(const Type& type, StripeStreams& stripe);
~DoubleColumnReader() override;
uint64_t skip(uint64_t numValues) override;
void next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char* notNull) override;
void seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) override;
private:
std::unique_ptr<SeekableInputStream> inputStream;
TypeKind columnKind;
const uint64_t bytesPerValue ;
const char *bufferPointer;
const char *bufferEnd;
unsigned char readByte() {
if (bufferPointer == bufferEnd) {
int length;
if (!inputStream->Next
(reinterpret_cast<const void**>(&bufferPointer), &length)) {
throw ParseError("bad read in DoubleColumnReader::next()");
}
bufferEnd = bufferPointer + length;
}
return static_cast<unsigned char>(*(bufferPointer++));
}
double readDouble() {
int64_t bits = 0;
for (uint64_t i=0; i < 8; i++) {
bits |= static_cast<int64_t>(readByte()) << (i*8);
}
double *result = reinterpret_cast<double*>(&bits);
return *result;
}
double readFloat() {
int32_t bits = 0;
for (uint64_t i=0; i < 4; i++) {
bits |= readByte() << (i*8);
}
float *result = reinterpret_cast<float*>(&bits);
return static_cast<double>(*result);
}
};
DoubleColumnReader::DoubleColumnReader(const Type& type,
StripeStreams& stripe
): ColumnReader(type, stripe),
columnKind(type.getKind()),
bytesPerValue((type.getKind() ==
FLOAT) ? 4 : 8),
bufferPointer(nullptr),
bufferEnd(nullptr) {
inputStream = stripe.getStream(columnId, proto::Stream_Kind_DATA, true);
if (inputStream == nullptr)
throw ParseError("DATA stream not found in Double column");
}
DoubleColumnReader::~DoubleColumnReader() {
// PASS
}
uint64_t DoubleColumnReader::skip(uint64_t numValues) {
numValues = ColumnReader::skip(numValues);
if (static_cast<size_t>(bufferEnd - bufferPointer) >=
bytesPerValue * numValues) {
bufferPointer += bytesPerValue * numValues;
} else {
size_t sizeToSkip = bytesPerValue * numValues -
static_cast<size_t>(bufferEnd - bufferPointer);
const size_t cap = static_cast<size_t>(std::numeric_limits<int>::max());
while (sizeToSkip != 0) {
size_t step = sizeToSkip > cap ? cap : sizeToSkip;
inputStream->Skip(static_cast<int>(step));
sizeToSkip -= step;
}
bufferEnd = nullptr;
bufferPointer = nullptr;
}
return numValues;
}
void DoubleColumnReader::next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) {
ColumnReader::next(rowBatch, numValues, notNull);
// update the notNull from the parent class
notNull = rowBatch.hasNulls ? rowBatch.notNull.data() : nullptr;
double* outArray = dynamic_cast<DoubleVectorBatch&>(rowBatch).data.data();
if (columnKind == FLOAT) {
if (notNull) {
for(size_t i=0; i < numValues; ++i) {
if (notNull[i]) {
outArray[i] = readFloat();
}
}
} else {
for(size_t i=0; i < numValues; ++i) {
outArray[i] = readFloat();
}
}
} else {
if (notNull) {
for(size_t i=0; i < numValues; ++i) {
if (notNull[i]) {
outArray[i] = readDouble();
}
}
} else {
for(size_t i=0; i < numValues; ++i) {
outArray[i] = readDouble();
}
}
}
}
void readFully(char* buffer, int64_t bufferSize, SeekableInputStream* stream) {
int64_t posn = 0;
while (posn < bufferSize) {
const void* chunk;
int length;
if (!stream->Next(&chunk, &length)) {
throw ParseError("bad read in readFully");
}
if (posn + length > bufferSize) {
throw ParseError("Corrupt dictionary blob in StringDictionaryColumn");
}
memcpy(buffer + posn, chunk, static_cast<size_t>(length));
posn += length;
}
}
void DoubleColumnReader::seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) {
ColumnReader::seekToRowGroup(positions);
inputStream->seek(positions.at(columnId));
}
class StringDictionaryColumnReader: public ColumnReader {
private:
std::shared_ptr<StringDictionary> dictionary;
std::unique_ptr<RleDecoder> rle;
public:
StringDictionaryColumnReader(const Type& type, StripeStreams& stipe);
~StringDictionaryColumnReader() override;
uint64_t skip(uint64_t numValues) override;
void next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) override;
void nextEncoded(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char* notNull) override;
void seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) override;
};
StringDictionaryColumnReader::StringDictionaryColumnReader
(const Type& type,
StripeStreams& stripe
): ColumnReader(type, stripe),
dictionary(new StringDictionary(stripe.getMemoryPool())) {
RleVersion rleVersion = convertRleVersion(stripe.getEncoding(columnId)
.kind());
uint32_t dictSize = stripe.getEncoding(columnId).dictionarysize();
rle = createRleDecoder(stripe.getStream(columnId,
proto::Stream_Kind_DATA,
true),
false, rleVersion, memoryPool);
std::unique_ptr<RleDecoder> lengthDecoder =
createRleDecoder(stripe.getStream(columnId,
proto::Stream_Kind_LENGTH,
false),
false, rleVersion, memoryPool);
dictionary->dictionaryOffset.resize(dictSize + 1);
int64_t* lengthArray = dictionary->dictionaryOffset.data();
lengthDecoder->next(lengthArray + 1, dictSize, nullptr);
lengthArray[0] = 0;
for(uint32_t i = 1; i < dictSize + 1; ++i) {
lengthArray[i] += lengthArray[i - 1];
}
dictionary->dictionaryBlob.resize(
static_cast<uint64_t>(lengthArray[dictSize]));
std::unique_ptr<SeekableInputStream> blobStream =
stripe.getStream(columnId, proto::Stream_Kind_DICTIONARY_DATA, false);
readFully(
dictionary->dictionaryBlob.data(),
lengthArray[dictSize],
blobStream.get());
}
StringDictionaryColumnReader::~StringDictionaryColumnReader() {
// PASS
}
uint64_t StringDictionaryColumnReader::skip(uint64_t numValues) {
numValues = ColumnReader::skip(numValues);
rle->skip(numValues);
return numValues;
}
void StringDictionaryColumnReader::next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) {
ColumnReader::next(rowBatch, numValues, notNull);
// update the notNull from the parent class
notNull = rowBatch.hasNulls ? rowBatch.notNull.data() : nullptr;
StringVectorBatch& byteBatch = dynamic_cast<StringVectorBatch&>(rowBatch);
char *blob = dictionary->dictionaryBlob.data();
int64_t *dictionaryOffsets = dictionary->dictionaryOffset.data();
char **outputStarts = byteBatch.data.data();
int64_t *outputLengths = byteBatch.length.data();
rle->next(outputLengths, numValues, notNull);
uint64_t dictionaryCount = dictionary->dictionaryOffset.size() - 1;
if (notNull) {
for(uint64_t i=0; i < numValues; ++i) {
if (notNull[i]) {
int64_t entry = outputLengths[i];
if (entry < 0 || static_cast<uint64_t>(entry) >= dictionaryCount ) {
throw ParseError("Entry index out of range in StringDictionaryColumn");
}
outputStarts[i] = blob + dictionaryOffsets[entry];
outputLengths[i] = dictionaryOffsets[entry+1] -
dictionaryOffsets[entry];
}
}
} else {
for(uint64_t i=0; i < numValues; ++i) {
int64_t entry = outputLengths[i];
if (entry < 0 || static_cast<uint64_t>(entry) >= dictionaryCount) {
throw ParseError("Entry index out of range in StringDictionaryColumn");
}
outputStarts[i] = blob + dictionaryOffsets[entry];
outputLengths[i] = dictionaryOffsets[entry+1] -
dictionaryOffsets[entry];
}
}
}
void StringDictionaryColumnReader::nextEncoded(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char* notNull) {
ColumnReader::next(rowBatch, numValues, notNull);
notNull = rowBatch.hasNulls ? rowBatch.notNull.data() : nullptr;
rowBatch.isEncoded = true;
EncodedStringVectorBatch& batch = dynamic_cast<EncodedStringVectorBatch&>(rowBatch);
batch.dictionary = this->dictionary;
// Length buffer is reused to save dictionary entry ids
rle->next(batch.index.data(), numValues, notNull);
}
void StringDictionaryColumnReader::seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) {
ColumnReader::seekToRowGroup(positions);
rle->seek(positions.at(columnId));
}
class StringDirectColumnReader: public ColumnReader {
private:
std::unique_ptr<RleDecoder> lengthRle;
std::unique_ptr<SeekableInputStream> blobStream;
const char *lastBuffer;
size_t lastBufferLength;
/**
* Compute the total length of the values.
* @param lengths the array of lengths
* @param notNull the array of notNull flags
* @param numValues the lengths of the arrays
* @return the total number of bytes for the non-null values
*/
size_t computeSize(const int64_t *lengths, const char *notNull,
uint64_t numValues);
public:
StringDirectColumnReader(const Type& type, StripeStreams& stipe);
~StringDirectColumnReader() override;
uint64_t skip(uint64_t numValues) override;
void next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) override;
void seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) override;
};
StringDirectColumnReader::StringDirectColumnReader
(const Type& type,
StripeStreams& stripe
): ColumnReader(type, stripe) {
RleVersion rleVersion = convertRleVersion(stripe.getEncoding(columnId)
.kind());
std::unique_ptr<SeekableInputStream> stream =
stripe.getStream(columnId, proto::Stream_Kind_LENGTH, true);
if (stream == nullptr)
throw ParseError("LENGTH stream not found in StringDirectColumn");
lengthRle = createRleDecoder(
std::move(stream), false, rleVersion, memoryPool);
blobStream = stripe.getStream(columnId, proto::Stream_Kind_DATA, true);
if (blobStream == nullptr)
throw ParseError("DATA stream not found in StringDirectColumn");
lastBuffer = nullptr;
lastBufferLength = 0;
}
StringDirectColumnReader::~StringDirectColumnReader() {
// PASS
}
uint64_t StringDirectColumnReader::skip(uint64_t numValues) {
const size_t BUFFER_SIZE = 1024;
numValues = ColumnReader::skip(numValues);
int64_t buffer[BUFFER_SIZE];
uint64_t done = 0;
size_t totalBytes = 0;
// read the lengths, so we know haw many bytes to skip
while (done < numValues) {
uint64_t step = std::min(BUFFER_SIZE,
static_cast<size_t>(numValues - done));
lengthRle->next(buffer, step, nullptr);
totalBytes += computeSize(buffer, nullptr, step);
done += step;
}
if (totalBytes <= lastBufferLength) {
// subtract the needed bytes from the ones left over
lastBufferLength -= totalBytes;
lastBuffer += totalBytes;
} else {
// move the stream forward after accounting for the buffered bytes
totalBytes -= lastBufferLength;
const size_t cap = static_cast<size_t>(std::numeric_limits<int>::max());
while (totalBytes != 0) {
size_t step = totalBytes > cap ? cap : totalBytes;
blobStream->Skip(static_cast<int>(step));
totalBytes -= step;
}
lastBufferLength = 0;
lastBuffer = nullptr;
}
return numValues;
}
size_t StringDirectColumnReader::computeSize(const int64_t* lengths,
const char* notNull,
uint64_t numValues) {
size_t totalLength = 0;
if (notNull) {
for(size_t i=0; i < numValues; ++i) {
if (notNull[i]) {
totalLength += static_cast<size_t>(lengths[i]);
}
}
} else {
for(size_t i=0; i < numValues; ++i) {
totalLength += static_cast<size_t>(lengths[i]);
}
}
return totalLength;
}
void StringDirectColumnReader::next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) {
ColumnReader::next(rowBatch, numValues, notNull);
// update the notNull from the parent class
notNull = rowBatch.hasNulls ? rowBatch.notNull.data() : nullptr;
StringVectorBatch& byteBatch = dynamic_cast<StringVectorBatch&>(rowBatch);
char **startPtr = byteBatch.data.data();
int64_t *lengthPtr = byteBatch.length.data();
// read the length vector
lengthRle->next(lengthPtr, numValues, notNull);
// figure out the total length of data we need from the blob stream
const size_t totalLength = computeSize(lengthPtr, notNull, numValues);
// Load data from the blob stream into our buffer until we have enough
// to get the rest directly out of the stream's buffer.
size_t bytesBuffered = 0;
byteBatch.blob.resize(totalLength);
char *ptr= byteBatch.blob.data();
while (bytesBuffered + lastBufferLength < totalLength) {
memcpy(ptr + bytesBuffered, lastBuffer, lastBufferLength);
bytesBuffered += lastBufferLength;
const void* readBuffer;
int readLength;
if (!blobStream->Next(&readBuffer, &readLength)) {
throw ParseError("failed to read in StringDirectColumnReader.next");
}
lastBuffer = static_cast<const char*>(readBuffer);
lastBufferLength = static_cast<size_t>(readLength);
}
if (bytesBuffered < totalLength) {
size_t moreBytes = totalLength - bytesBuffered;
memcpy(ptr + bytesBuffered, lastBuffer, moreBytes);
lastBuffer += moreBytes;
lastBufferLength -= moreBytes;
}
size_t filledSlots = 0;
ptr = byteBatch.blob.data();
if (notNull) {
while (filledSlots < numValues) {
if (notNull[filledSlots]) {
startPtr[filledSlots] = const_cast<char*>(ptr);
ptr += lengthPtr[filledSlots];
}
filledSlots += 1;
}
} else {
while (filledSlots < numValues) {
startPtr[filledSlots] = const_cast<char*>(ptr);
ptr += lengthPtr[filledSlots];
filledSlots += 1;
}
}
}
void StringDirectColumnReader::seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) {
ColumnReader::seekToRowGroup(positions);
blobStream->seek(positions.at(columnId));
lengthRle->seek(positions.at(columnId));
}
class StructColumnReader: public ColumnReader {
private:
std::vector<ColumnReader*> children;
public:
StructColumnReader(const Type& type, StripeStreams& stipe);
~StructColumnReader() override;
uint64_t skip(uint64_t numValues) override;
void next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) override;
void nextEncoded(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) override;
void seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) override;
private:
template<bool encoded>
void nextInternal(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull);
};
StructColumnReader::StructColumnReader(const Type& type,
StripeStreams& stripe
): ColumnReader(type, stripe) {
// count the number of selected sub-columns
const std::vector<bool> selectedColumns = stripe.getSelectedColumns();
switch (static_cast<int64_t>(stripe.getEncoding(columnId).kind())) {
case proto::ColumnEncoding_Kind_DIRECT:
for(unsigned int i=0; i < type.getSubtypeCount(); ++i) {
const Type& child = *type.getSubtype(i);
if (selectedColumns[static_cast<uint64_t>(child.getColumnId())]) {
children.push_back(buildReader(child, stripe).release());
}
}
break;
case proto::ColumnEncoding_Kind_DIRECT_V2:
case proto::ColumnEncoding_Kind_DICTIONARY:
case proto::ColumnEncoding_Kind_DICTIONARY_V2:
default:
throw ParseError("Unknown encoding for StructColumnReader");
}
}
StructColumnReader::~StructColumnReader() {
for (size_t i=0; i<children.size(); i++) {
delete children[i];
}
}
uint64_t StructColumnReader::skip(uint64_t numValues) {
numValues = ColumnReader::skip(numValues);
for(std::vector<ColumnReader*>::iterator ptr=children.begin(); ptr != children.end(); ++ptr) {
(*ptr)->skip(numValues);
}
return numValues;
}
void StructColumnReader::next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) {
nextInternal<false>(rowBatch, numValues, notNull);
}
void StructColumnReader::nextEncoded(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) {
nextInternal<true>(rowBatch, numValues, notNull);
}
template<bool encoded>
void StructColumnReader::nextInternal(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) {
ColumnReader::next(rowBatch, numValues, notNull);
uint64_t i=0;
notNull = rowBatch.hasNulls? rowBatch.notNull.data() : nullptr;
for(std::vector<ColumnReader*>::iterator ptr=children.begin();
ptr != children.end(); ++ptr, ++i) {
if (encoded) {
(*ptr)->nextEncoded(*(dynamic_cast<StructVectorBatch&>(rowBatch).fields[i]),
numValues, notNull);
} else {
(*ptr)->next(*(dynamic_cast<StructVectorBatch&>(rowBatch).fields[i]),
numValues, notNull);
}
}
}
void StructColumnReader::seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) {
ColumnReader::seekToRowGroup(positions);
for(std::vector<ColumnReader*>::iterator ptr = children.begin();
ptr != children.end();
++ptr) {
(*ptr)->seekToRowGroup(positions);
}
}
class ListColumnReader: public ColumnReader {
private:
std::unique_ptr<ColumnReader> child;
std::unique_ptr<RleDecoder> rle;
public:
ListColumnReader(const Type& type, StripeStreams& stipe);
~ListColumnReader() override;
uint64_t skip(uint64_t numValues) override;
void next(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) override;
void nextEncoded(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull) override;
void seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) override;
private:
template<bool encoded>
void nextInternal(ColumnVectorBatch& rowBatch,
uint64_t numValues,
char *notNull);
};
ListColumnReader::ListColumnReader(const Type& type,
StripeStreams& stripe
): ColumnReader(type, stripe) {
// count the number of selected sub-columns
const std::vector<bool> selectedColumns = stripe.getSelectedColumns();
RleVersion vers = convertRleVersion(stripe.getEncoding(columnId).kind());
std::unique_ptr<SeekableInputStream> stream =
stripe.getStream(columnId, proto::Stream_Kind_LENGTH, true);
if (stream == nullptr)
throw ParseError("LENGTH stream not found in List column");
rle = createRleDecoder(std::move(stream), false, vers, memoryPool);
const Type& childType = *type.getSubtype(0);
if (selectedColumns[static_cast<uint64_t>(childType.getColumnId())]) {
child = buildReader(childType, stripe);
}
}
ListColumnReader::~ListColumnReader() {
// PASS
}
uint64_t ListColumnReader::skip(uint64_t numValues) {
numValues = ColumnReader::skip(numValues);
ColumnReader *childReader = child.get();
if (childReader) {
const uint64_t BUFFER_SIZE = 1024;
int64_t buffer[BUFFER_SIZE];
uint64_t childrenElements = 0;
uint64_t lengthsRead = 0;
while (lengthsRead < numValues) {