-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
SliceDictionaryStreamReader.java
346 lines (297 loc) · 14 KB
/
SliceDictionaryStreamReader.java
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
/*
* 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.
*/
package com.facebook.presto.orc.reader;
import com.facebook.presto.orc.OrcCorruptionException;
import com.facebook.presto.orc.StreamDescriptor;
import com.facebook.presto.orc.metadata.ColumnEncoding;
import com.facebook.presto.orc.stream.BooleanStream;
import com.facebook.presto.orc.stream.ByteArrayStream;
import com.facebook.presto.orc.stream.LongStream;
import com.facebook.presto.orc.stream.RowGroupDictionaryLengthStream;
import com.facebook.presto.orc.stream.StreamSource;
import com.facebook.presto.orc.stream.StreamSources;
import com.facebook.presto.spi.block.Block;
import com.facebook.presto.spi.block.DictionaryBlock;
import com.facebook.presto.spi.block.SliceArrayBlock;
import com.facebook.presto.spi.type.Type;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import static com.facebook.presto.orc.metadata.Stream.StreamKind.DATA;
import static com.facebook.presto.orc.metadata.Stream.StreamKind.DICTIONARY_DATA;
import static com.facebook.presto.orc.metadata.Stream.StreamKind.IN_DICTIONARY;
import static com.facebook.presto.orc.metadata.Stream.StreamKind.LENGTH;
import static com.facebook.presto.orc.metadata.Stream.StreamKind.PRESENT;
import static com.facebook.presto.orc.metadata.Stream.StreamKind.ROW_GROUP_DICTIONARY;
import static com.facebook.presto.orc.metadata.Stream.StreamKind.ROW_GROUP_DICTIONARY_LENGTH;
import static com.facebook.presto.orc.stream.MissingStreamSource.missingStreamSource;
import static com.facebook.presto.spi.type.Chars.isCharType;
import static com.facebook.presto.spi.type.Chars.trimSpacesAndTruncateToLength;
import static com.facebook.presto.spi.type.Varchars.isVarcharType;
import static com.facebook.presto.spi.type.Varchars.truncateToLength;
import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;
public class SliceDictionaryStreamReader
implements StreamReader
{
private final StreamDescriptor streamDescriptor;
private int readOffset;
private int nextBatchSize;
@Nonnull
private StreamSource<BooleanStream> presentStreamSource = missingStreamSource(BooleanStream.class);
@Nullable
private BooleanStream presentStream;
private boolean[] isNullVector = new boolean[0];
@Nonnull
private StreamSource<ByteArrayStream> stripeDictionaryDataStreamSource = missingStreamSource(ByteArrayStream.class);
private boolean stripeDictionaryOpen;
private int stripeDictionarySize;
@Nonnull
private Slice[] stripeDictionary = new Slice[1];
private SliceArrayBlock dictionaryBlock = new SliceArrayBlock(stripeDictionary.length, stripeDictionary, true);
@Nonnull
private StreamSource<LongStream> stripeDictionaryLengthStreamSource = missingStreamSource(LongStream.class);
@Nonnull
private StreamSource<BooleanStream> inDictionaryStreamSource = missingStreamSource(BooleanStream.class);
@Nullable
private BooleanStream inDictionaryStream;
private boolean[] inDictionary = new boolean[0];
@Nonnull
private StreamSource<ByteArrayStream> rowGroupDictionaryDataStreamSource = missingStreamSource(ByteArrayStream.class);
@Nonnull
private Slice[] rowGroupDictionary = new Slice[0];
@Nonnull
private StreamSource<RowGroupDictionaryLengthStream> rowGroupDictionaryLengthStreamSource = missingStreamSource(RowGroupDictionaryLengthStream.class);
@Nonnull
private int[] rowGroupDictionaryLength = new int[0];
@Nonnull
private StreamSource<LongStream> dataStreamSource = missingStreamSource(LongStream.class);
@Nullable
private LongStream dataStream;
private boolean rowGroupOpen;
public SliceDictionaryStreamReader(StreamDescriptor streamDescriptor)
{
this.streamDescriptor = requireNonNull(streamDescriptor, "stream is null");
}
@Override
public void prepareNextRead(int batchSize)
{
readOffset += nextBatchSize;
nextBatchSize = batchSize;
}
@Override
public Block readBlock(Type type)
throws IOException
{
if (!rowGroupOpen) {
openRowGroup(type);
}
if (readOffset > 0) {
if (presentStream != null) {
// skip ahead the present bit reader, but count the set bits
// and use this as the skip size for the length reader
readOffset = presentStream.countBitsSet(readOffset);
}
if (readOffset > 0) {
if (dataStream == null) {
throw new OrcCorruptionException("Value is not null but data stream is not present");
}
if (inDictionaryStream != null) {
inDictionaryStream.skip(readOffset);
}
dataStream.skip(readOffset);
}
}
if (isNullVector.length < nextBatchSize) {
isNullVector = new boolean[nextBatchSize];
}
int[] dataVector = new int[nextBatchSize];
if (presentStream == null) {
if (dataStream == null) {
throw new OrcCorruptionException("Value is not null but data stream is not present");
}
Arrays.fill(isNullVector, false);
dataStream.nextIntVector(nextBatchSize, dataVector);
}
else {
int nullValues = presentStream.getUnsetBits(nextBatchSize, isNullVector);
if (nullValues != nextBatchSize) {
if (dataStream == null) {
throw new OrcCorruptionException("Value is not null but data stream is not present");
}
dataStream.nextIntVector(nextBatchSize, dataVector, isNullVector);
}
}
if (inDictionary.length < nextBatchSize) {
inDictionary = new boolean[nextBatchSize];
}
if (inDictionaryStream == null) {
Arrays.fill(inDictionary, true);
}
else {
inDictionaryStream.getSetBits(nextBatchSize, inDictionary, isNullVector);
}
// create the dictionary ids
for (int i = 0; i < nextBatchSize; i++) {
if (isNullVector[i]) {
// null is the last entry in the slice dictionary
dataVector[i] = dictionaryBlock.getPositionCount() - 1;
}
else if (inDictionary[i]) {
// stripe dictionary elements have the same dictionary id
}
else {
// row group dictionary elements are after the main dictionary
dataVector[i] += stripeDictionarySize;
}
}
// copy ids into a private array for this block since data vector is reused
Block block = new DictionaryBlock(nextBatchSize, dictionaryBlock, dataVector);
readOffset = 0;
nextBatchSize = 0;
return block;
}
private void setDictionaryBlockData(Slice[] dictionary)
{
// only update the block if the array changed to prevent creation of new Block objects, since
// the engine currently uses identity equality to test if dictionaries are the same
if (dictionaryBlock.getValues() != dictionary) {
dictionaryBlock = new SliceArrayBlock(dictionary.length, dictionary, true);
}
}
private void openRowGroup(Type type)
throws IOException
{
// read the dictionary
if (!stripeDictionaryOpen) {
// We must always create a new dictionary array because the previous dictionary may still be referenced
// add one extra entry for null
stripeDictionary = new Slice[stripeDictionarySize + 1];
if (stripeDictionarySize > 0) {
int[] dictionaryLength = new int[stripeDictionarySize];
// read the lengths
LongStream lengthStream = stripeDictionaryLengthStreamSource.openStream();
if (lengthStream == null) {
throw new OrcCorruptionException("Dictionary is not empty but dictionary length stream is not present");
}
lengthStream.nextIntVector(stripeDictionarySize, dictionaryLength);
// read dictionary values
ByteArrayStream dictionaryDataStream = stripeDictionaryDataStreamSource.openStream();
readDictionary(dictionaryDataStream, stripeDictionarySize, dictionaryLength, 0, stripeDictionary, type);
}
}
stripeDictionaryOpen = true;
// read row group dictionary
RowGroupDictionaryLengthStream dictionaryLengthStream = rowGroupDictionaryLengthStreamSource.openStream();
if (dictionaryLengthStream != null) {
int rowGroupDictionarySize = dictionaryLengthStream.getEntryCount();
// We must always create a new dictionary array because the previous dictionary may still be referenced
// The first elements of the dictionary are from the stripe dictionary, then the row group dictionary elements, and then a null
rowGroupDictionary = Arrays.copyOf(stripeDictionary, stripeDictionarySize + rowGroupDictionarySize + 1);
setDictionaryBlockData(rowGroupDictionary);
// resize the dictionary lengths array if necessary
if (rowGroupDictionaryLength.length < rowGroupDictionarySize) {
rowGroupDictionaryLength = new int[rowGroupDictionarySize];
}
// read the lengths
dictionaryLengthStream.nextIntVector(rowGroupDictionarySize, rowGroupDictionaryLength);
// read dictionary values
ByteArrayStream dictionaryDataStream = rowGroupDictionaryDataStreamSource.openStream();
readDictionary(dictionaryDataStream, rowGroupDictionarySize, rowGroupDictionaryLength, stripeDictionarySize, rowGroupDictionary, type);
}
else {
// there is no row group dictionary so use the stripe dictionary
setDictionaryBlockData(stripeDictionary);
}
presentStream = presentStreamSource.openStream();
inDictionaryStream = inDictionaryStreamSource.openStream();
dataStream = dataStreamSource.openStream();
rowGroupOpen = true;
}
private static void readDictionary(
@Nullable ByteArrayStream dictionaryDataStream,
int dictionarySize,
int[] dictionaryLength,
int dictionaryOutputOffset,
Slice[] dictionary,
Type type)
throws IOException
{
// build dictionary slices
for (int i = 0; i < dictionarySize; i++) {
int length = dictionaryLength[i];
if (length == 0) {
dictionary[dictionaryOutputOffset + i] = Slices.EMPTY_SLICE;
}
else {
Slice value = Slices.wrappedBuffer(dictionaryDataStream.next(length));
if (isVarcharType(type)) {
value = truncateToLength(value, type);
}
if (isCharType(type)) {
value = trimSpacesAndTruncateToLength(value, type);
}
dictionary[dictionaryOutputOffset + i] = value;
}
}
}
@Override
public void startStripe(StreamSources dictionaryStreamSources, List<ColumnEncoding> encoding)
throws IOException
{
stripeDictionaryDataStreamSource = dictionaryStreamSources.getStreamSource(streamDescriptor, DICTIONARY_DATA, ByteArrayStream.class);
stripeDictionaryLengthStreamSource = dictionaryStreamSources.getStreamSource(streamDescriptor, LENGTH, LongStream.class);
stripeDictionarySize = encoding.get(streamDescriptor.getStreamId()).getDictionarySize();
stripeDictionaryOpen = false;
presentStreamSource = missingStreamSource(BooleanStream.class);
dataStreamSource = missingStreamSource(LongStream.class);
inDictionaryStreamSource = missingStreamSource(BooleanStream.class);
rowGroupDictionaryLengthStreamSource = missingStreamSource(RowGroupDictionaryLengthStream.class);
rowGroupDictionaryDataStreamSource = missingStreamSource(ByteArrayStream.class);
readOffset = 0;
nextBatchSize = 0;
presentStream = null;
inDictionaryStream = null;
dataStream = null;
rowGroupOpen = false;
}
@Override
public void startRowGroup(StreamSources dataStreamSources)
throws IOException
{
presentStreamSource = dataStreamSources.getStreamSource(streamDescriptor, PRESENT, BooleanStream.class);
dataStreamSource = dataStreamSources.getStreamSource(streamDescriptor, DATA, LongStream.class);
// the "in dictionary" stream signals if the value is in the stripe or row group dictionary
inDictionaryStreamSource = dataStreamSources.getStreamSource(streamDescriptor, IN_DICTIONARY, BooleanStream.class);
rowGroupDictionaryLengthStreamSource = dataStreamSources.getStreamSource(streamDescriptor, ROW_GROUP_DICTIONARY_LENGTH, RowGroupDictionaryLengthStream.class);
rowGroupDictionaryDataStreamSource = dataStreamSources.getStreamSource(streamDescriptor, ROW_GROUP_DICTIONARY, ByteArrayStream.class);
readOffset = 0;
nextBatchSize = 0;
presentStream = null;
inDictionaryStream = null;
dataStream = null;
rowGroupOpen = false;
}
@Override
public String toString()
{
return toStringHelper(this)
.addValue(streamDescriptor)
.toString();
}
}