-
Notifications
You must be signed in to change notification settings - Fork 867
/
Copy pathasync_reader.rs
489 lines (420 loc) · 16 KB
/
async_reader.rs
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
// 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.
//! Provides `async` API for reading parquet files as
//! [`RecordBatch`]es
//!
//! ```
//! # #[tokio::main(flavor="current_thread")]
//! # async fn main() {
//! #
//! use arrow::record_batch::RecordBatch;
//! use arrow::util::pretty::pretty_format_batches;
//! use futures::TryStreamExt;
//! use tokio::fs::File;
//!
//! use parquet::arrow::ParquetRecordBatchStreamBuilder;
//!
//! # fn assert_batches_eq(batches: &[RecordBatch], expected_lines: &[&str]) {
//! # let formatted = pretty_format_batches(batches).unwrap().to_string();
//! # let actual_lines: Vec<_> = formatted.trim().lines().collect();
//! # assert_eq!(
//! # &actual_lines, expected_lines,
//! # "\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
//! # expected_lines, actual_lines
//! # );
//! # }
//!
//! let testdata = arrow::util::test_util::parquet_test_data();
//! let path = format!("{}/alltypes_plain.parquet", testdata);
//! let file = tokio::fs::File::open(path).await.unwrap();
//!
//! let builder = ParquetRecordBatchStreamBuilder::new(file)
//! .await
//! .unwrap()
//! .with_projection(vec![1, 2, 6])
//! .with_batch_size(3);
//!
//! let stream = builder.build().unwrap();
//!
//! let results = stream.try_collect::<Vec<_>>().await.unwrap();
//! assert_eq!(results.len(), 3);
//!
//! assert_batches_eq(
//! &results,
//! &[
//! "+----------+-------------+-----------+",
//! "| bool_col | tinyint_col | float_col |",
//! "+----------+-------------+-----------+",
//! "| true | 0 | 0 |",
//! "| false | 1 | 1.1 |",
//! "| true | 0 | 0 |",
//! "| false | 1 | 1.1 |",
//! "| true | 0 | 0 |",
//! "| false | 1 | 1.1 |",
//! "| true | 0 | 0 |",
//! "| false | 1 | 1.1 |",
//! "+----------+-------------+-----------+",
//! ],
//! );
//! # }
//! ```
use std::collections::VecDeque;
use std::fmt::Formatter;
use std::io::{Cursor, SeekFrom};
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use byteorder::{ByteOrder, LittleEndian};
use futures::future::{BoxFuture, FutureExt};
use futures::stream::Stream;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncSeek, AsyncSeekExt};
use arrow::datatypes::SchemaRef;
use arrow::record_batch::RecordBatch;
use crate::arrow::array_reader::{build_array_reader, RowGroupCollection};
use crate::arrow::arrow_reader::ParquetRecordBatchReader;
use crate::arrow::schema::parquet_to_arrow_schema;
use crate::basic::Compression;
use crate::column::page::{PageIterator, PageReader};
use crate::errors::{ParquetError, Result};
use crate::file::footer::parse_metadata_buffer;
use crate::file::metadata::ParquetMetaData;
use crate::file::reader::SerializedPageReader;
use crate::file::PARQUET_MAGIC;
use crate::schema::types::{ColumnDescPtr, SchemaDescPtr};
use crate::util::memory::ByteBufferPtr;
/// A builder used to construct a [`ParquetRecordBatchStream`] for a parquet file
///
/// In particular, this handles reading the parquet file metadata, allowing consumers
/// to use this information to select what specific columns, row groups, etc...
/// they wish to be read by the resulting stream
///
pub struct ParquetRecordBatchStreamBuilder<T> {
input: T,
metadata: Arc<ParquetMetaData>,
schema: SchemaRef,
batch_size: usize,
row_groups: Option<Vec<usize>>,
projection: Option<Vec<usize>>,
}
impl<T: AsyncRead + AsyncSeek + Unpin> ParquetRecordBatchStreamBuilder<T> {
/// Create a new [`ParquetRecordBatchStreamBuilder`] with the provided parquet file
pub async fn new(mut input: T) -> Result<Self> {
let metadata = Arc::new(read_footer(&mut input).await?);
let schema = Arc::new(parquet_to_arrow_schema(
metadata.file_metadata().schema_descr(),
metadata.file_metadata().key_value_metadata(),
)?);
Ok(Self {
input,
metadata,
schema,
batch_size: 1024,
row_groups: None,
projection: None,
})
}
/// Returns a reference to the [`ParquetMetaData`] for this parquet file
pub fn metadata(&self) -> &Arc<ParquetMetaData> {
&self.metadata
}
/// Returns the arrow [`SchemaRef`] for this parquet file
pub fn schema(&self) -> &SchemaRef {
&self.schema
}
/// Set the size of [`RecordBatch`] to produce
pub fn with_batch_size(self, batch_size: usize) -> Self {
Self { batch_size, ..self }
}
/// Only read data from the provided row group indexes
pub fn with_row_groups(self, row_groups: Vec<usize>) -> Self {
Self {
row_groups: Some(row_groups),
..self
}
}
/// Only read data from the provided column indexes
pub fn with_projection(self, projection: Vec<usize>) -> Self {
Self {
projection: Some(projection),
..self
}
}
/// Build a new [`ParquetRecordBatchStream`]
pub fn build(self) -> Result<ParquetRecordBatchStream<T>> {
let num_columns = self.schema.fields().len();
let num_row_groups = self.metadata.row_groups().len();
let columns = match self.projection {
Some(projection) => {
if let Some(col) = projection.iter().find(|x| **x >= num_columns) {
return Err(general_err!(
"column projection {} outside bounds of schema 0..{}",
col,
num_columns
));
}
projection
}
None => (0..num_columns).collect::<Vec<_>>(),
};
let row_groups = match self.row_groups {
Some(row_groups) => {
if let Some(col) = row_groups.iter().find(|x| **x >= num_row_groups) {
return Err(general_err!(
"row group {} out of bounds 0..{}",
col,
num_row_groups
));
}
row_groups.into()
}
None => (0..self.metadata.row_groups().len()).collect(),
};
Ok(ParquetRecordBatchStream {
row_groups,
columns: columns.into(),
batch_size: self.batch_size,
metadata: self.metadata,
schema: self.schema,
input: Some(self.input),
state: StreamState::Init,
})
}
}
enum StreamState<T> {
/// At the start of a new row group, or the end of the parquet stream
Init,
/// Decoding a batch
Decoding(ParquetRecordBatchReader),
/// Reading data from input
Reading(BoxFuture<'static, Result<(T, InMemoryRowGroup)>>),
/// Error
Error,
}
impl<T> std::fmt::Debug for StreamState<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
StreamState::Init => write!(f, "StreamState::Init"),
StreamState::Decoding(_) => write!(f, "StreamState::Decoding"),
StreamState::Reading(_) => write!(f, "StreamState::Reading"),
StreamState::Error => write!(f, "StreamState::Error"),
}
}
}
/// An asynchronous [`Stream`] of [`RecordBatch`] for a parquet file
pub struct ParquetRecordBatchStream<T> {
metadata: Arc<ParquetMetaData>,
schema: SchemaRef,
batch_size: usize,
columns: Arc<[usize]>,
row_groups: VecDeque<usize>,
/// This is an option so it can be moved into a future
input: Option<T>,
state: StreamState<T>,
}
impl<T> std::fmt::Debug for ParquetRecordBatchStream<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ParquetRecordBatchStream")
.field("metadata", &self.metadata)
.field("schema", &self.schema)
.field("batch_size", &self.batch_size)
.field("columns", &self.columns)
.field("state", &self.state)
.finish()
}
}
impl<T> ParquetRecordBatchStream<T> {
/// Returns the [`SchemaRef`] for this parquet file
pub fn schema(&self) -> &SchemaRef {
&self.schema
}
}
impl<T: AsyncRead + AsyncSeek + Unpin + Send + 'static> Stream
for ParquetRecordBatchStream<T>
{
type Item = Result<RecordBatch>;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
loop {
match &mut self.state {
StreamState::Decoding(batch_reader) => match batch_reader.next() {
Some(Ok(batch)) => return Poll::Ready(Some(Ok(batch))),
Some(Err(e)) => {
self.state = StreamState::Error;
return Poll::Ready(Some(Err(ParquetError::ArrowError(
e.to_string(),
))));
}
None => self.state = StreamState::Init,
},
StreamState::Init => {
let row_group_idx = match self.row_groups.pop_front() {
Some(idx) => idx,
None => return Poll::Ready(None),
};
let metadata = self.metadata.clone();
let mut input = match self.input.take() {
Some(input) => input,
None => {
self.state = StreamState::Error;
return Poll::Ready(Some(Err(general_err!(
"input stream lost"
))));
}
};
let columns = Arc::clone(&self.columns);
self.state = StreamState::Reading(
async move {
let row_group_metadata = metadata.row_group(row_group_idx);
let mut column_chunks =
vec![None; row_group_metadata.columns().len()];
for column_idx in columns.iter() {
let column = row_group_metadata.column(*column_idx);
let (start, length) = column.byte_range();
let end = start + length;
input.seek(SeekFrom::Start(start)).await?;
let mut buffer = vec![0_u8; (end - start) as usize];
input.read_exact(buffer.as_mut_slice()).await?;
column_chunks[*column_idx] = Some(InMemoryColumnChunk {
num_values: column.num_values(),
compression: column.compression(),
physical_type: column.column_type(),
data: ByteBufferPtr::new(buffer),
});
}
Ok((
input,
InMemoryRowGroup {
schema: metadata.file_metadata().schema_descr_ptr(),
row_count: row_group_metadata.num_rows() as usize,
column_chunks,
},
))
}
.boxed(),
)
}
StreamState::Reading(f) => {
let result = futures::ready!(f.poll_unpin(cx));
self.state = StreamState::Init;
let row_group: Box<dyn RowGroupCollection> = match result {
Ok((input, row_group)) => {
self.input = Some(input);
Box::new(row_group)
}
Err(e) => {
self.state = StreamState::Error;
return Poll::Ready(Some(Err(e)));
}
};
let parquet_schema = self.metadata.file_metadata().schema_descr_ptr();
let array_reader = build_array_reader(
parquet_schema,
self.schema.clone(),
self.columns.iter().cloned(),
row_group,
)?;
let batch_reader =
ParquetRecordBatchReader::try_new(self.batch_size, array_reader)
.expect("reader");
self.state = StreamState::Decoding(batch_reader)
}
StreamState::Error => return Poll::Pending,
}
}
}
}
async fn read_footer<T: AsyncRead + AsyncSeek + Unpin>(
input: &mut T,
) -> Result<ParquetMetaData> {
input.seek(SeekFrom::End(-8)).await?;
let mut buf = [0_u8; 8];
input.read_exact(&mut buf).await?;
if buf[4..] != PARQUET_MAGIC {
return Err(general_err!("Invalid Parquet file. Corrupt footer"));
}
let metadata_len = LittleEndian::read_i32(&buf[..4]) as i64;
if metadata_len < 0 {
return Err(general_err!(
"Invalid Parquet file. Metadata length is less than zero ({})",
metadata_len
));
}
input.seek(SeekFrom::End(-8 - metadata_len)).await?;
let mut buf = Vec::with_capacity(metadata_len as usize + 8);
input.read_to_end(&mut buf).await?;
parse_metadata_buffer(&mut Cursor::new(buf))
}
struct InMemoryRowGroup {
schema: SchemaDescPtr,
column_chunks: Vec<Option<InMemoryColumnChunk>>,
row_count: usize,
}
impl RowGroupCollection for InMemoryRowGroup {
fn schema(&self) -> Result<SchemaDescPtr> {
Ok(self.schema.clone())
}
fn num_rows(&self) -> usize {
self.row_count
}
fn column_chunks(&self, i: usize) -> Result<Box<dyn PageIterator>> {
let page_reader = self.column_chunks[i].as_ref().unwrap().pages();
Ok(Box::new(ColumnChunkIterator {
schema: self.schema.clone(),
column_schema: self.schema.columns()[i].clone(),
reader: Some(page_reader),
}))
}
}
#[derive(Clone)]
struct InMemoryColumnChunk {
num_values: i64,
compression: Compression,
physical_type: crate::basic::Type,
data: ByteBufferPtr,
}
impl InMemoryColumnChunk {
fn pages(&self) -> Result<Box<dyn PageReader>> {
let page_reader = SerializedPageReader::new(
Cursor::new(self.data.clone()),
self.num_values,
self.compression,
self.physical_type,
)?;
Ok(Box::new(page_reader))
}
}
struct ColumnChunkIterator {
schema: SchemaDescPtr,
column_schema: ColumnDescPtr,
reader: Option<Result<Box<dyn PageReader>>>,
}
impl Iterator for ColumnChunkIterator {
type Item = Result<Box<dyn PageReader>>;
fn next(&mut self) -> Option<Self::Item> {
self.reader.take()
}
}
impl PageIterator for ColumnChunkIterator {
fn schema(&mut self) -> Result<SchemaDescPtr> {
Ok(self.schema.clone())
}
fn column_schema(&mut self) -> Result<ColumnDescPtr> {
Ok(self.column_schema.clone())
}
}