Apache DataFusion SQL Query Engine
ARROW-6700: [Rust] [DataFusion] Use new Arrow Parquet reader
Andy Grovef2c6853283ec
diff --git a/rust/datafusion/examples/parquet_sql.rs b/rust/datafusion/examples/parquet_sql.rsindex d8bb9f8647..142ef090f8 100644--- a/rust/datafusion/examples/parquet_sql.rs+++ b/rust/datafusion/examples/parquet_sql.rs@@ -39,7 +39,7 @@ fn main() -> Result<()> {)?;// simple selection- let sql = "SELECT int_col, double_col, date_string_col FROM alltypes_plain WHERE id > 1 AND tinyint_col < double_col";+ let sql = "SELECT int_col, double_col, CAST(date_string_col as VARCHAR) FROM alltypes_plain WHERE id > 1 AND tinyint_col < double_col";// create the query planlet plan = ctx.create_logical_plan(&sql)?;diff --git a/rust/datafusion/src/datasource/csv.rs b/rust/datafusion/src/datasource/csv.rsindex a879abe1b5..fb3014e737 100644--- a/rust/datafusion/src/datasource/csv.rs+++ b/rust/datafusion/src/datasource/csv.rs@@ -50,8 +50,8 @@ impl CsvFile {}impl TableProvider for CsvFile {- fn schema(&self) -> &Arc<Schema> {- &self.schema+ fn schema(&self) -> Arc<Schema> {+ self.schema.clone()}fn scan(diff --git a/rust/datafusion/src/datasource/datasource.rs b/rust/datafusion/src/datasource/datasource.rsindex 7249731d06..48f5219667 100644--- a/rust/datafusion/src/datasource/datasource.rs+++ b/rust/datafusion/src/datasource/datasource.rs@@ -31,7 +31,7 @@ pub type ScanResult = Arc<Mutex<dyn BatchIterator>>;/// Source tablepub trait TableProvider {/// Get a reference to the schema for this table- fn schema(&self) -> &Arc<Schema>;+ fn schema(&self) -> Arc<Schema>;/// Perform a scan of a table and return a sequence of iterators over the data (one/// iterator per partition)diff --git a/rust/datafusion/src/datasource/memory.rs b/rust/datafusion/src/datasource/memory.rsindex 0fec2d6189..787aa3b50e 100644--- a/rust/datafusion/src/datasource/memory.rs+++ b/rust/datafusion/src/datasource/memory.rs@@ -66,8 +66,8 @@ impl MemTable {}impl TableProvider for MemTable {- fn schema(&self) -> &Arc<Schema> {- &self.schema+ fn schema(&self) -> Arc<Schema> {+ self.schema.clone()}fn scan(diff --git a/rust/datafusion/src/datasource/parquet.rs b/rust/datafusion/src/datasource/parquet.rsindex 25e72694c8..a725e31090 100644--- a/rust/datafusion/src/datasource/parquet.rs+++ b/rust/datafusion/src/datasource/parquet.rs@@ -17,54 +17,38 @@//! Parquet data source-use std::fs::File;use std::string::String;-use std::sync::{Arc, Mutex};-use std::thread;+use std::sync::Arc;-use crossbeam::channel::{unbounded, Receiver, Sender};--use arrow::array::{- Array, PrimitiveArray, PrimitiveBuilder, StringBuilder, TimestampNanosecondBuilder,-};use arrow::datatypes::*;-use arrow::record_batch::RecordBatch;--use parquet::arrow::schema::parquet_to_arrow_schema;-use parquet::column::reader::*;-use parquet::data_type::{ByteArray, Int96};-use parquet::file::reader::*;use crate::datasource::{ScanResult, TableProvider};-use crate::error::{ExecutionError, Result};-use crate::execution::physical_plan::common;-use crate::execution::physical_plan::BatchIterator;+use crate::error::Result;+use crate::execution::physical_plan::parquet::ParquetExec;+use crate::execution::physical_plan::{common, ExecutionPlan};/// Table-based representation of a `ParquetFile`pub struct ParquetTable {- filenames: Vec<String>,+ path: String,schema: Arc<Schema>,}impl ParquetTable {/// Attempt to initialize a new `ParquetTable` from a file pathpub fn try_new(path: &str) -> Result<Self> {- let mut filenames: Vec<String> = vec![];- common::build_file_list(path, &mut filenames, ".parquet")?;- if filenames.is_empty() {- Err(ExecutionError::General("No files found".to_string()))- } else {- let parquet_file = ParquetFile::open(&filenames[0], None, 0)?;- let schema = parquet_file.projection_schema.clone();- Ok(Self { filenames, schema })- }+ let parquet_exec = ParquetExec::try_new(path, None, 0)?;+ let schema = parquet_exec.schema();+ Ok(Self {+ path: path.to_string(),+ schema,+ })}}impl TableProvider for ParquetTable {/// Get the schema for this parquet file- fn schema(&self) -> &Arc<Schema> {- &self.schema+ fn schema(&self) -> Arc<Schema> {+ self.schema.clone()}/// Scan the file(s), using the provided projection, and return one BatchIterator per@@ -74,475 +58,19 @@ impl TableProvider for ParquetTable {projection: &Option<Vec<usize>>,batch_size: usize,) -> Result<Vec<ScanResult>> {- Ok(self- .filenames- .iter()- .map(|filename| {- ParquetScanPartition::try_new(filename, projection.clone(), batch_size)- .and_then(|part| {- Ok(Arc::new(Mutex::new(part)) as Arc<Mutex<dyn BatchIterator>>)- })- })- .collect::<Result<Vec<_>>>()?)- }-}--/// Loader and reader for parquet data-pub struct ParquetFile {- reader: SerializedFileReader<File>,- /// Projection expressed as column indices into underlying parquet reader- projection: Vec<usize>,- /// The schema of the projection- projection_schema: Arc<Schema>,- batch_size: usize,- row_group_index: usize,- current_row_group: Option<Box<dyn RowGroupReader>>,- column_readers: Vec<ColumnReader>,-}--/// Thread-safe wrapper around a ParquetFile-struct ParquetScanPartition {- schema: Arc<Schema>,- request_tx: Sender<()>,- response_rx: Receiver<Result<Option<RecordBatch>>>,-}--impl ParquetScanPartition {- pub fn try_new(- filename: &str,- projection: Option<Vec<usize>>,- batch_size: usize,- ) -> Result<Self> {- // determine the schema after the projection is applied- let schema = match &projection {- Some(p) => {- let table = ParquetFile::open(&filename, Some(p.clone()), batch_size)?;- table.schema().clone()- }- None => {- let table = ParquetFile::open(&filename, None, batch_size)?;- table.schema().clone()- }- };-- // because the parquet implementation is not thread-safe, it is necessary to execute- // on a thread and communicate with channels- let (request_tx, request_rx): (Sender<()>, Receiver<()>) = unbounded();- let (response_tx, response_rx): (- Sender<Result<Option<RecordBatch>>>,- Receiver<Result<Option<RecordBatch>>>,- ) = unbounded();- let filename = filename.to_string();- thread::spawn(move || {- match ParquetFile::open(&filename, projection, batch_size) {- Ok(mut table) => {- while let Ok(_) = request_rx.recv() {- response_tx.send(table.next()).unwrap();- }- }- Err(e) => {- response_tx.send(Err(e)).unwrap();- }- }- });-- Ok(Self {- schema,- request_tx,- response_rx,- })- }-}--impl BatchIterator for ParquetScanPartition {- fn schema(&self) -> Arc<Schema> {- self.schema.clone()- }-- fn next(&mut self) -> Result<Option<RecordBatch>> {- match self.request_tx.send(()) {- Ok(_) => match self.response_rx.recv() {- Ok(batch) => batch,- Err(e) => Err(ExecutionError::General(format!(- "Error receiving batch: {:?}",- e- ))),- },- _ => Err(ExecutionError::General(- "Error sending request for next batch".to_string(),- )),- }- }-}--macro_rules! read_binary_column {- ($SELF:ident, $R:ident, $INDEX:expr) => {{- let mut read_buffer: Vec<ByteArray> =- vec![ByteArray::default(); $SELF.batch_size];- let mut def_levels: Vec<i16> = vec![0; $SELF.batch_size];- let (_, levels_read) = $R.read_batch(- $SELF.batch_size,- Some(&mut def_levels),- None,- &mut read_buffer,- )?;- let mut builder = StringBuilder::new(levels_read);- let mut value_index = 0;- for i in 0..levels_read {- if def_levels[i] > 0 {- builder.append_value(- &String::from_utf8(read_buffer[value_index].data().to_vec()).unwrap(),- )?;- value_index += 1;- } else {- builder.append_null()?;- }- }- Arc::new(builder.finish())- }};-}--trait ArrowReader<T>-where- T: ArrowPrimitiveType,-{- fn read(- &mut self,- batch_size: usize,- is_nullable: bool,- ) -> Result<Arc<PrimitiveArray<T>>>;-}--impl<A, P> ArrowReader<A> for ColumnReaderImpl<P>-where- A: ArrowPrimitiveType,- P: parquet::data_type::DataType,- P::T: std::convert::From<A::Native>,- A::Native: std::convert::From<P::T>,-{- fn read(- &mut self,- batch_size: usize,- is_nullable: bool,- ) -> Result<Arc<PrimitiveArray<A>>> {- // create read buffer- let mut read_buffer: Vec<P::T> = vec![A::default_value().into(); batch_size];-- if is_nullable {- let mut def_levels: Vec<i16> = vec![0; batch_size];-- let (values_read, levels_read) = self.read_batch(- batch_size,- Some(&mut def_levels),- None,- &mut read_buffer,- )?;- let mut builder = PrimitiveBuilder::<A>::new(levels_read);- let converted_buffer: Vec<A::Native> =- read_buffer.into_iter().map(|v| v.into()).collect();- if values_read == levels_read {- builder.append_slice(&converted_buffer[0..values_read])?;- } else {- let mut value_index = 0;- for i in 0..levels_read {- if def_levels[i] != 0 {- builder.append_value(converted_buffer[value_index].into())?;- value_index += 1;- } else {- builder.append_null()?;- }- }- }- Ok(Arc::new(builder.finish()))- } else {- let (values_read, _) =- self.read_batch(batch_size, None, None, &mut read_buffer)?;-- let mut builder = PrimitiveBuilder::<A>::new(values_read);- let converted_buffer: Vec<A::Native> =- read_buffer.into_iter().map(|v| v.into()).collect();- builder.append_slice(&converted_buffer[0..values_read])?;- Ok(Arc::new(builder.finish()))- }- }-}--impl ParquetFile {- /// Read parquet data from a `File`- pub fn open(- filename: &str,- projection: Option<Vec<usize>>,- batch_size: usize,- ) -> Result<Self> {- let file = File::open(filename)?;- let reader = SerializedFileReader::new(file)?;-- let metadata = reader.metadata();- let schema =- parquet_to_arrow_schema(metadata.file_metadata().schema_descr_ptr())?;-- // even if we aren't referencing structs or lists in our projection, column reader- // indexes will be off until we have support for nested schemas- for i in 0..schema.fields().len() {- match schema.field(i).data_type() {- DataType::List(_) => {- return Err(ExecutionError::NotImplemented(- "Parquet datasource does not support LIST".to_string(),- ));- }- DataType::Struct(_) => {- return Err(ExecutionError::NotImplemented(- "Parquet datasource does not support STRUCT".to_string(),- ));- }- _ => {}- }- }-- let projection = match projection {- Some(p) => p,- None => {- let mut p = Vec::with_capacity(schema.fields().len());- for i in 0..schema.fields().len() {- p.push(i);- }- p- }- };-- let projected_schema = schema_projection(&schema, &projection)?;-- Ok(ParquetFile {- reader: reader,- row_group_index: 0,- projection_schema: projected_schema,- projection,- batch_size,- current_row_group: None,- column_readers: vec![],- })- }-- fn load_next_row_group(&mut self) -> Result<()> {- if self.row_group_index < self.reader.num_row_groups() {- let reader = self.reader.get_row_group(self.row_group_index)?;-- self.column_readers.clear();- self.column_readers = Vec::with_capacity(self.projection.len());-- for i in 0..self.projection.len() {- self.column_readers- .push(reader.get_column_reader(self.projection[i])?);- }-- self.current_row_group = Some(reader);- self.row_group_index += 1;-- Ok(())- } else {- Err(ExecutionError::General(- "Attempt to read past final row group".to_string(),- ))- }- }-- fn load_batch(&mut self) -> Result<Option<RecordBatch>> {- match &self.current_row_group {- Some(reader) => {- let mut batch: Vec<Arc<dyn Array>> =- Vec::with_capacity(reader.num_columns());- for i in 0..self.column_readers.len() {- let dt = self.schema().field(i).data_type().clone();- let is_nullable = self.schema().field(i).is_nullable();- let array: Arc<dyn Array> = match self.column_readers[i] {- ColumnReader::BoolColumnReader(ref mut r) => {- ArrowReader::<BooleanType>::read(- r,- self.batch_size,- is_nullable,- )?- }- ColumnReader::Int32ColumnReader(ref mut r) => match dt {- DataType::Date32(DateUnit::Day) => {- ArrowReader::<Date32Type>::read(- r,- self.batch_size,- is_nullable,- )?- }- DataType::Time32(TimeUnit::Millisecond) => {- ArrowReader::<Time32MillisecondType>::read(- r,- self.batch_size,- is_nullable,- )?- }- _ => ArrowReader::<Int32Type>::read(- r,- self.batch_size,- is_nullable,- )?,- },- ColumnReader::Int64ColumnReader(ref mut r) => match dt {- DataType::Time64(TimeUnit::Microsecond) => {- ArrowReader::<Time64MicrosecondType>::read(- r,- self.batch_size,- is_nullable,- )?- }- DataType::Time64(TimeUnit::Nanosecond) => {- ArrowReader::<Time64NanosecondType>::read(- r,- self.batch_size,- is_nullable,- )?- }- DataType::Timestamp(TimeUnit::Millisecond, None) => {- ArrowReader::<TimestampMillisecondType>::read(- r,- self.batch_size,- is_nullable,- )?- }- DataType::Timestamp(TimeUnit::Microsecond, None) => {- ArrowReader::<TimestampMicrosecondType>::read(- r,- self.batch_size,- is_nullable,- )?- }- DataType::Timestamp(TimeUnit::Nanosecond, None) => {- ArrowReader::<TimestampMicrosecondType>::read(- r,- self.batch_size,- is_nullable,- )?- }- _ => ArrowReader::<Int64Type>::read(- r,- self.batch_size,- is_nullable,- )?,- },- ColumnReader::Int96ColumnReader(ref mut r) => {- let mut read_buffer: Vec<Int96> =- vec![Int96::new(); self.batch_size];-- let mut def_levels: Vec<i16> = vec![0; self.batch_size];- let (_, levels_read) = r.read_batch(- self.batch_size,- Some(&mut def_levels),- None,- &mut read_buffer,- )?;-- let mut builder =- TimestampNanosecondBuilder::new(levels_read);- let mut value_index = 0;- for i in 0..levels_read {- if def_levels[i] > 0 {- builder.append_value(convert_int96_timestamp(- read_buffer[value_index].data(),- ))?;- value_index += 1;- } else {- builder.append_null()?;- }- }- Arc::new(builder.finish())- }- ColumnReader::FloatColumnReader(ref mut r) => {- ArrowReader::<Float32Type>::read(- r,- self.batch_size,- is_nullable,- )?- }- ColumnReader::DoubleColumnReader(ref mut r) => {- ArrowReader::<Float64Type>::read(- r,- self.batch_size,- is_nullable,- )?- }- ColumnReader::FixedLenByteArrayColumnReader(ref mut r) => {- read_binary_column!(self, r, i)- }- ColumnReader::ByteArrayColumnReader(ref mut r) => {- read_binary_column!(self, r, i)- }- };-- batch.push(array);- }-- if batch.len() == 0 || batch[0].data().len() == 0 {- Ok(None)- } else {- Ok(Some(RecordBatch::try_new(- self.projection_schema.clone(),- batch,- )?))- }- }- _ => Ok(None),- }- }-}--/// Create a new schema by applying a projection to this schema's fields-fn schema_projection(schema: &Schema, projection: &[usize]) -> Result<Arc<Schema>> {- let mut fields: Vec<Field> = Vec::with_capacity(projection.len());- for i in projection {- if *i < schema.fields().len() {- fields.push(schema.field(*i).clone());- } else {- return Err(ExecutionError::InvalidColumn(format!(- "Invalid column index {} in projection",- i- )));- }- }- Ok(Arc::new(Schema::new(fields)))-}--/// convert a Parquet INT96 to an Arrow timestamp in nanoseconds-fn convert_int96_timestamp(v: &[u32]) -> i64 {- const JULIAN_DAY_OF_EPOCH: i64 = 2_440_588;- const SECONDS_PER_DAY: i64 = 86_400;- const MILLIS_PER_SECOND: i64 = 1_000;+ let mut filenames: Vec<String> = vec![];+ common::build_file_list(&self.path, &mut filenames, ".parquet")?;- let day = v[2] as i64;- let nanoseconds = ((v[1] as i64) << 32) + v[0] as i64;- let seconds = (day - JULIAN_DAY_OF_EPOCH) * SECONDS_PER_DAY;- seconds * MILLIS_PER_SECOND * 1_000_000 + nanoseconds-}+ let parquet_exec =+ ParquetExec::try_new(&self.path, projection.clone(), batch_size)?;-impl ParquetFile {- fn schema(&self) -> &Arc<Schema> {- &self.projection_schema- }+ let partitions = parquet_exec.partitions()?;- fn next(&mut self) -> Result<Option<RecordBatch>> {- // advance the row group reader if necessary- if self.current_row_group.is_none() {- self.load_next_row_group()?;- self.load_batch()- } else {- match self.load_batch() {- Ok(Some(b)) => Ok(Some(b)),- Ok(None) => {- if self.row_group_index < self.reader.num_row_groups() {- self.load_next_row_group()?;- self.load_batch()- } else {- Ok(None)- }- }- Err(e) => Err(e),- }- }+ let iterators = partitions+ .iter()+ .map(|p| p.execute())+ .collect::<Result<Vec<_>>>()?;+ Ok(iterators)}}@@ -550,7 +78,7 @@ impl ParquetFile {mod tests {use super::*;use arrow::array::{- BooleanArray, Float32Array, Float64Array, Int32Array, StringArray,+ BinaryArray, BooleanArray, Float32Array, Float64Array, Int32Array,TimestampNanosecondArray,};use std::env;@@ -594,8 +122,8 @@ mod tests {bigint_col: Int64\n\float_col: Float32\n\double_col: Float64\n\- date_string_col: Utf8\n\- string_col: Utf8\n\+ date_string_col: Binary\n\+ string_col: Binary\n\timestamp_col: Timestamp(Nanosecond, None)",y);@@ -744,7 +272,7 @@ mod tests {}#[test]- fn read_utf8_alltypes_plain_parquet() {+ fn read_binary_alltypes_plain_parquet() {let table = load_table("alltypes_plain.parquet");let projection = Some(vec![9]);@@ -758,11 +286,11 @@ mod tests {let array = batch.column(0).as_any()- .downcast_ref::<StringArray>()+ .downcast_ref::<BinaryArray>().unwrap();- let mut values: Vec<String> = vec![];+ let mut values: Vec<&str> = vec![];for i in 0..batch.num_rows() {- values.push(array.value(i).to_string());+ values.push(std::str::from_utf8(array.value(i)).unwrap());}assert_eq!(diff --git a/rust/datafusion/src/execution/physical_plan/expressions.rs b/rust/datafusion/src/execution/physical_plan/expressions.rsindex 8cc1fa4656..2c9c5a957f 100644--- a/rust/datafusion/src/execution/physical_plan/expressions.rs+++ b/rust/datafusion/src/execution/physical_plan/expressions.rs@@ -1018,6 +1018,8 @@ impl CastExpr {&& (is_numeric(&cast_type) || cast_type == DataType::Utf8){Ok(Self { expr, cast_type })+ } else if expr_type == DataType::Binary && cast_type == DataType::Utf8 {+ Ok(Self { expr, cast_type })} else {Err(ExecutionError::General(format!("Invalid CAST from {:?} to {:?}",diff --git a/rust/datafusion/src/execution/physical_plan/mod.rs b/rust/datafusion/src/execution/physical_plan/mod.rsindex 966075f6c5..44095f05f8 100644--- a/rust/datafusion/src/execution/physical_plan/mod.rs+++ b/rust/datafusion/src/execution/physical_plan/mod.rs@@ -92,5 +92,6 @@ pub mod expressions;pub mod hash_aggregate;pub mod limit;pub mod merge;+pub mod parquet;pub mod projection;pub mod selection;diff --git a/rust/datafusion/src/execution/physical_plan/parquet.rs b/rust/datafusion/src/execution/physical_plan/parquet.rsnew file mode 100644index 0000000000..4afd1eb3a2--- /dev/null+++ b/rust/datafusion/src/execution/physical_plan/parquet.rs@@ -0,0 +1,260 @@+// 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.++//! Execution plan for reading Parquet files++use std::fs::File;+use std::rc::Rc;+use std::sync::{Arc, Mutex};+use std::thread;++use crate::error::{ExecutionError, Result};+use crate::execution::physical_plan::common;+use crate::execution::physical_plan::{BatchIterator, ExecutionPlan, Partition};+use arrow::datatypes::Schema;+use arrow::record_batch::RecordBatch;+use parquet::file::reader::SerializedFileReader;++use crossbeam::channel::{unbounded, Receiver, Sender};+use parquet::arrow::{ArrowReader, ParquetFileArrowReader};++/// Execution plan for scanning a Parquet file+pub struct ParquetExec {+ /// Path to directory containing partitioned Parquet files with the same schema+ filenames: Vec<String>,+ /// Schema after projection is applied+ schema: Arc<Schema>,+ /// Projection for which columns to load+ projection: Vec<usize>,+ /// Batch size+ batch_size: usize,+}++impl ParquetExec {+ /// Create a new Parquet reader execution plan+ pub fn try_new(+ path: &str,+ projection: Option<Vec<usize>>,+ batch_size: usize,+ ) -> Result<Self> {+ let mut filenames: Vec<String> = vec![];+ common::build_file_list(path, &mut filenames, ".parquet")?;+ if filenames.is_empty() {+ Err(ExecutionError::General("No files found".to_string()))+ } else {+ let file = File::open(&filenames[0])?;+ let file_reader = Rc::new(SerializedFileReader::new(file)?);+ let mut arrow_reader = ParquetFileArrowReader::new(file_reader);+ let schema = arrow_reader.get_schema()?;++ let projection = match projection {+ Some(p) => p,+ None => (0..schema.fields().len()).collect(),+ };++ let projected_schema = Schema::new(+ projection+ .iter()+ .map(|i| schema.field(*i).clone())+ .collect(),+ );++ Ok(Self {+ filenames,+ schema: Arc::new(projected_schema),+ projection,+ batch_size,+ })+ }+ }+}++impl ExecutionPlan for ParquetExec {+ fn schema(&self) -> Arc<Schema> {+ self.schema.clone()+ }++ fn partitions(&self) -> Result<Vec<Arc<dyn Partition>>> {+ let partitions = self+ .filenames+ .iter()+ .map(|filename| {+ Arc::new(ParquetPartition::new(+ &filename,+ self.projection.clone(),+ self.schema.clone(),+ self.batch_size,+ )) as Arc<dyn Partition>+ })+ .collect();+ Ok(partitions)+ }+}++struct ParquetPartition {+ iterator: Arc<Mutex<dyn BatchIterator>>,+}++impl ParquetPartition {+ /// Create a new Parquet partition+ pub fn new(+ filename: &str,+ projection: Vec<usize>,+ schema: Arc<Schema>,+ batch_size: usize,+ ) -> Self {+ // because the parquet implementation is not thread-safe, it is necessary to execute+ // on a thread and communicate with channels+ let (request_tx, request_rx): (Sender<()>, Receiver<()>) = unbounded();+ let (response_tx, response_rx): (+ Sender<Result<Option<RecordBatch>>>,+ Receiver<Result<Option<RecordBatch>>>,+ ) = unbounded();++ let filename = filename.to_string();++ thread::spawn(move || {+ //TODO error handling, remove unwraps++ // open file+ let file = File::open(&filename).unwrap();+ match SerializedFileReader::new(file) {+ Ok(file_reader) => {+ let file_reader = Rc::new(file_reader);++ let mut arrow_reader = ParquetFileArrowReader::new(file_reader);++ match arrow_reader+ .get_record_reader_by_columns(projection, batch_size)+ {+ Ok(mut batch_reader) => {+ while let Ok(_) = request_rx.recv() {+ match batch_reader.next_batch() {+ Ok(Some(batch)) => {+ response_tx.send(Ok(Some(batch))).unwrap();+ }+ Ok(None) => {+ response_tx.send(Ok(None)).unwrap();+ break;+ }+ Err(e) => {+ response_tx+ .send(Err(ExecutionError::General(format!(+ "{:?}",+ e+ ))))+ .unwrap();+ break;+ }+ }+ }+ }++ Err(e) => {+ response_tx+ .send(Err(ExecutionError::General(format!("{:?}", e))))+ .unwrap();+ }+ }+ }++ Err(e) => {+ response_tx+ .send(Err(ExecutionError::General(format!("{:?}", e))))+ .unwrap();+ }+ }+ });++ let iterator = Arc::new(Mutex::new(ParquetIterator {+ schema,+ request_tx,+ response_rx,+ }));++ Self { iterator }+ }+}++impl Partition for ParquetPartition {+ fn execute(&self) -> Result<Arc<Mutex<dyn BatchIterator>>> {+ Ok(self.iterator.clone())+ }+}++struct ParquetIterator {+ schema: Arc<Schema>,+ request_tx: Sender<()>,+ response_rx: Receiver<Result<Option<RecordBatch>>>,+}++impl BatchIterator for ParquetIterator {+ fn schema(&self) -> Arc<Schema> {+ self.schema.clone()+ }++ fn next(&mut self) -> Result<Option<RecordBatch>> {+ match self.request_tx.send(()) {+ Ok(_) => match self.response_rx.recv() {+ Ok(batch) => batch,+ Err(e) => Err(ExecutionError::General(format!(+ "Error receiving batch: {:?}",+ e+ ))),+ },+ _ => Err(ExecutionError::General(+ "Error sending request for next batch".to_string(),+ )),+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use std::env;++ #[test]+ fn test() -> Result<()> {+ let testdata =+ env::var("PARQUET_TEST_DATA").expect("PARQUET_TEST_DATA not defined");+ let filename = format!("{}/alltypes_plain.parquet", testdata);+ let parquet_exec = ParquetExec::try_new(&filename, Some(vec![0, 1, 2]), 1024)?;+ let partitions = parquet_exec.partitions()?;+ assert_eq!(partitions.len(), 1);++ let results = partitions[0].execute()?;+ let mut results = results.lock().unwrap();+ let batch = results.next()?.unwrap();++ assert_eq!(8, batch.num_rows());+ assert_eq!(3, batch.num_columns());++ let field_names: Vec<&str> = batch+ .schema()+ .fields()+ .iter()+ .map(|f| f.name().as_str())+ .collect();+ assert_eq!(vec!["id", "bool_col", "tinyint_col"], field_names);++ let batch = results.next()?;+ assert!(batch.is_none());++ Ok(())+ }+}diff --git a/rust/datafusion/tests/sql.rs b/rust/datafusion/tests/sql.rsindex 103d436ec3..f626659d9a 100644--- a/rust/datafusion/tests/sql.rs+++ b/rust/datafusion/tests/sql.rs@@ -86,7 +86,9 @@ fn nyc() -> Result<()> {fn parquet_query() {let mut ctx = ExecutionContext::new();register_alltypes_parquet(&mut ctx);- let sql = "SELECT id, string_col FROM alltypes_plain";+ // NOTE that string_col is actually a binary column and does not have the UTF8 logical type+ // so we need an explicit cast+ let sql = "SELECT id, CAST(string_col AS varchar) FROM alltypes_plain";let actual = execute(&mut ctx, sql).join("\n");let expected ="4\t\"0\"\n5\t\"1\"\n6\t\"0\"\n7\t\"1\"\n2\t\"0\"\n3\t\"1\"\n0\t\"0\"\n1\t\"1\""