-
Notifications
You must be signed in to change notification settings - Fork 984
/
ColumnReader.java
339 lines (274 loc) · 11.2 KB
/
ColumnReader.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
/**
* 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.
*/
package org.apache.drill.exec.store.parquet.columnreaders;
import io.netty.buffer.DrillBuf;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import org.apache.drill.common.exceptions.ExecutionSetupException;
import org.apache.drill.common.exceptions.UserException;
import org.apache.drill.exec.ExecConstants;
import org.apache.drill.exec.vector.BaseDataValueVector;
import org.apache.drill.exec.vector.ValueVector;
import org.apache.parquet.column.ColumnDescriptor;
import org.apache.parquet.format.SchemaElement;
import org.apache.parquet.hadoop.metadata.ColumnChunkMetaData;
import org.apache.parquet.schema.PrimitiveType;
import org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName;
public abstract class ColumnReader<V extends ValueVector> {
static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ColumnReader.class);
final ParquetRecordReader parentReader;
// Value Vector for this column
final V valueVec;
ColumnDescriptor getColumnDescriptor() {
return columnDescriptor;
}
// column description from the parquet library
final ColumnDescriptor columnDescriptor;
// metadata of the column, from the parquet library
final ColumnChunkMetaData columnChunkMetaData;
// status information on the current page
PageReader pageReader;
final SchemaElement schemaElement;
boolean usingDictionary;
// quick reference to see if the field is fixed length (as this requires an instanceof)
final boolean isFixedLength;
// counter for the total number of values read from one or more pages
// when a batch is filled all of these values should be the same for all of the columns
int totalValuesRead;
// counter for the values that have been read in this pass (a single call to the next() method)
int valuesReadInCurrentPass;
// length of single data value in bits, if the length is fixed
int dataTypeLengthInBits;
int bytesReadInCurrentPass;
protected DrillBuf vectorData;
// when reading definition levels for nullable columns, it is a one-way stream of integers
// when reading var length data, where we don't know if all of the records will fit until we've read all of them
// we must store the last definition level and use it at the start of the next batch
int currDefLevel;
// variables for a single read pass
long readStartInBytes = 0, readLength = 0, readLengthInBits = 0, recordsReadInThisIteration = 0;
private ExecutorService threadPool;
protected ColumnReader(ParquetRecordReader parentReader, int allocateSize, ColumnDescriptor descriptor,
ColumnChunkMetaData columnChunkMetaData, boolean fixedLength, V v, SchemaElement schemaElement) throws ExecutionSetupException {
this.parentReader = parentReader;
this.columnDescriptor = descriptor;
this.columnChunkMetaData = columnChunkMetaData;
this.isFixedLength = fixedLength;
this.schemaElement = schemaElement;
this.valueVec = v;
boolean useAsyncPageReader = parentReader.getFragmentContext().getOptions()
.getOption(ExecConstants.PARQUET_PAGEREADER_ASYNC).bool_val;
if (useAsyncPageReader) {
this.pageReader =
new AsyncPageReader(this, parentReader.getFileSystem(), parentReader.getHadoopPath(),
columnChunkMetaData);
} else {
this.pageReader =
new PageReader(this, parentReader.getFileSystem(), parentReader.getHadoopPath(),
columnChunkMetaData);
}
if (columnDescriptor.getType() != PrimitiveType.PrimitiveTypeName.BINARY) {
if (columnDescriptor.getType() == PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY) {
dataTypeLengthInBits = columnDescriptor.getTypeLength() * 8;
} else {
dataTypeLengthInBits = ParquetRecordReader.getTypeLengthInBits(columnDescriptor.getType());
}
}
if(threadPool == null) {
threadPool = parentReader.getOperatorContext().getScanDecodeExecutor();
}
}
public int getRecordsReadInCurrentPass() {
return valuesReadInCurrentPass;
}
public Future<Long> processPagesAsync(long recordsToReadInThisPass){
Future<Long> r = threadPool.submit(new ColumnReaderProcessPagesTask(recordsToReadInThisPass));
return r;
}
public void processPages(long recordsToReadInThisPass) throws IOException {
reset();
if(recordsToReadInThisPass>0) {
do {
determineSize(recordsToReadInThisPass, 0);
} while (valuesReadInCurrentPass < recordsToReadInThisPass && pageReader.hasPage());
}
logger.trace("Column Reader: {} - Values read in this pass: {} - ",
this.getColumnDescriptor().toString(), valuesReadInCurrentPass);
valueVec.getMutator().setValueCount(valuesReadInCurrentPass);
}
public void clear() {
valueVec.clear();
pageReader.clear();
}
public void readValues(long recordsToRead) {
try {
readField(recordsToRead);
valuesReadInCurrentPass += recordsReadInThisIteration;
pageReader.valuesRead += recordsReadInThisIteration;
pageReader.readPosInBytes = readStartInBytes + readLength;
} catch (Exception e) {
UserException ex = UserException.dataReadError(e)
.message("Error reading from Parquet file")
.pushContext("Row Group Start: ", this.columnChunkMetaData.getStartingPos())
.pushContext("Column: ", this.schemaElement.getName())
.pushContext("File: ", this.parentReader.getHadoopPath().toString() )
.build(logger);
throw ex;
}
}
protected abstract void readField(long recordsToRead);
/**
* Determines the size of a single value in a variable column.
*
* Return value indicates if we have finished a row group and should stop reading
*
* @param recordsReadInCurrentPass
* @param lengthVarFieldsInCurrentRecord
* @return - true if we should stop reading
* @throws IOException
*/
public boolean determineSize(long recordsReadInCurrentPass, Integer lengthVarFieldsInCurrentRecord) throws IOException {
boolean doneReading = readPage();
if (doneReading) {
return true;
}
doneReading = processPageData((int) recordsReadInCurrentPass);
if (doneReading) {
return true;
}
// Never used in this code path. Hard to remove because the method is overidden by subclasses
lengthVarFieldsInCurrentRecord = -1;
doneReading = checkVectorCapacityReached();
if (doneReading) {
return true;
}
return false;
}
protected Future<Integer> readRecordsAsync(int recordsToRead){
Future<Integer> r = threadPool.submit(new ColumnReaderReadRecordsTask(recordsToRead));
return r;
}
protected void readRecords(int recordsToRead) {
for (int i = 0; i < recordsToRead; i++) {
readField(i);
}
pageReader.valuesRead += recordsToRead;
}
protected boolean processPageData(int recordsToReadInThisPass) throws IOException {
readValues(recordsToReadInThisPass);
return true;
}
public void updatePosition() {}
public void updateReadyToReadPosition() {}
public void reset() {
readStartInBytes = 0;
readLength = 0;
readLengthInBits = 0;
recordsReadInThisIteration = 0;
bytesReadInCurrentPass = 0;
vectorData = ((BaseDataValueVector) valueVec).getBuffer();
}
public int capacity() {
return (int) (valueVec.getValueCapacity() * dataTypeLengthInBits / 8.0);
}
public Future<Boolean> readPageAsync() {
Future<Boolean> f = threadPool.submit(new Callable<Boolean>() {
@Override public Boolean call() throws Exception {
return new Boolean(readPage());
}
});
return f;
}
// Read a page if we need more data, returns true if we need to exit the read loop
public boolean readPage() throws IOException {
if (!pageReader.hasPage()
|| totalValuesReadAndReadyToReadInPage() == pageReader.currentPageCount) {
readRecords(pageReader.valuesReadyToRead);
if (pageReader.hasPage()) {
totalValuesRead += pageReader.currentPageCount;
}
if (!pageReader.next()) {
hitRowGroupEnd();
return true;
}
postPageRead();
}
return false;
}
protected int totalValuesReadAndReadyToReadInPage() {
return pageReader.valuesRead + pageReader.valuesReadyToRead;
}
protected void postPageRead() {
pageReader.valuesReadyToRead = 0;
}
protected void hitRowGroupEnd() {}
protected boolean checkVectorCapacityReached() {
if (bytesReadInCurrentPass + dataTypeLengthInBits > capacity()) {
logger.debug("Reached the capacity of the data vector in a variable length value vector.");
return true;
}
else if (valuesReadInCurrentPass > valueVec.getValueCapacity()) {
return true;
}
return false;
}
// copied out of parquet library, didn't want to deal with the uneeded throws statement they had declared
public static int readIntLittleEndian(DrillBuf in, int offset) {
int ch4 = in.getByte(offset) & 0xff;
int ch3 = in.getByte(offset + 1) & 0xff;
int ch2 = in.getByte(offset + 2) & 0xff;
int ch1 = in.getByte(offset + 3) & 0xff;
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}
private class ColumnReaderProcessPagesTask implements Callable<Long> {
private final ColumnReader parent = ColumnReader.this;
private final long recordsToReadInThisPass;
public ColumnReaderProcessPagesTask(long recordsToReadInThisPass){
this.recordsToReadInThisPass = recordsToReadInThisPass;
}
@Override public Long call() throws IOException{
String oldname = Thread.currentThread().getName();
try {
Thread.currentThread().setName(oldname + "Decode-" + this.parent.columnChunkMetaData.toString());
this.parent.processPages(recordsToReadInThisPass);
return recordsToReadInThisPass;
} finally {
Thread.currentThread().setName(oldname);
}
}
}
private class ColumnReaderReadRecordsTask implements Callable<Integer> {
private final ColumnReader parent = ColumnReader.this;
private final int recordsToRead;
public ColumnReaderReadRecordsTask(int recordsToRead){
this.recordsToRead = recordsToRead;
}
@Override public Integer call() throws IOException{
String oldname = Thread.currentThread().getName();
try {
Thread.currentThread().setName("Decode-" + this.parent.columnChunkMetaData.toString());
this.parent.readRecords(recordsToRead);
return recordsToRead;
} finally {
Thread.currentThread().setName(oldname);
}
}
}
}