Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reading nested lists from parquet files #1517

Merged
merged 2 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions parquet/src/arrow/array_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1330,10 +1330,12 @@ mod tests {
use rand::distributions::uniform::SampleUniform;
use rand::{thread_rng, Rng};

use crate::arrow::{parquet_to_arrow_schema, ArrowWriter};
use arrow::array::{
Array, ArrayRef, LargeListArray, ListArray, PrimitiveArray, StringArray,
StructArray,
};
use arrow::datatypes::DataType::Struct;
use arrow::datatypes::{
ArrowPrimitiveType, DataType as ArrowType, Date32Type as ArrowDate32, Field,
Int32Type as ArrowInt32, Int64Type as ArrowInt64,
Expand All @@ -1348,6 +1350,8 @@ mod tests {
use crate::column::page::{Page, PageReader};
use crate::data_type::{ByteArray, ByteArrayType, DataType, Int32Type, Int64Type};
use crate::errors::Result;
use crate::file::properties::WriterProperties;
use crate::file::serialized_reader::SerializedFileReader;
use crate::schema::parser::parse_message_type;
use crate::schema::types::{ColumnDescPtr, SchemaDescriptor};
use crate::util::test_common::make_pages;
Expand Down Expand Up @@ -2268,4 +2272,67 @@ mod tests {
&PrimitiveArray::<ArrowInt32>::from(vec![Some(3), Some(4)])
);
}

#[test]
fn test_nested_lists() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested running this test without the rest of the code change in this PR and I got:

---- arrow::array_reader::tests::test_nested_lists stdout ----
thread 'arrow::array_reader::tests::test_nested_lists' panicked at 'called `Option::unwrap()` on a `None` value', parquet/src/arrow/array_reader/builder.rs:327:14
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

// Construct column schema
let message_type = "
message table {
REPEATED group table_info {
REQUIRED BYTE_ARRAY name;
REPEATED group cols {
REQUIRED BYTE_ARRAY name;
REQUIRED INT32 type;
OPTIONAL INT32 length;
}
REPEATED group tags {
REQUIRED BYTE_ARRAY name;
REQUIRED INT32 type;
OPTIONAL INT32 length;
}
}
}
";
Comment on lines +2280 to +2295
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message type is copied from the issue.


let schema = parse_message_type(message_type)
.map(|t| Arc::new(SchemaDescriptor::new(Arc::new(t))))
.unwrap();

let arrow_schema = parquet_to_arrow_schema(schema.as_ref(), &None).unwrap();

let file = tempfile::tempfile().unwrap();
let props = WriterProperties::builder()
.set_max_row_group_size(200)
.build();

let mut writer = ArrowWriter::try_new(
file.try_clone().unwrap(),
Arc::new(arrow_schema),
Some(props),
)
.unwrap();
writer.close().unwrap();

let file_reader: Arc<dyn FileReader> =
Arc::new(SerializedFileReader::new(file).unwrap());

let file_metadata = file_reader.metadata().file_metadata();
let arrow_schema = parquet_to_arrow_schema(
file_metadata.schema_descr(),
file_metadata.key_value_metadata(),
)
.unwrap();

let mut array_reader = build_array_reader(
file_reader.metadata().file_metadata().schema_descr_ptr(),
Arc::new(arrow_schema.clone()),
vec![0usize].into_iter(),
Box::new(file_reader),
)
.unwrap();

let batch = array_reader.next_batch(100).unwrap();
assert_eq!(batch.data_type(), &Struct(arrow_schema.fields().clone()));
assert_eq!(batch.len(), 0);
}
}
39 changes: 23 additions & 16 deletions parquet/src/arrow/array_reader/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,10 @@ impl<'a> TypeVisitor<Option<Box<dyn ArrayReader>>, &'a ArrayReaderBuilderContext
)?;

if cur_type.get_basic_info().repetition() == Repetition::REPEATED {
Err(ArrowError(
"Reading repeated field is not supported yet!".to_string(),
))
Err(ArrowError(format!(
"Reading repeated field ({:?}) is not supported yet!",
cur_type.name()
)))
} else {
Ok(Some(reader))
}
Expand Down Expand Up @@ -189,9 +190,10 @@ impl<'a> TypeVisitor<Option<Box<dyn ArrayReader>>, &'a ArrayReaderBuilderContext
if cur_type.get_basic_info().has_repetition()
&& cur_type.get_basic_info().repetition() == Repetition::REPEATED
{
Err(ArrowError(
"Reading repeated field is not supported yet!".to_string(),
))
Err(ArrowError(format!(
"Reading repeated field ({:?}) is not supported yet!",
cur_type.name(),
)))
} else {
Ok(Some(reader))
}
Expand Down Expand Up @@ -321,11 +323,8 @@ impl<'a> TypeVisitor<Option<Box<dyn ArrayReader>>, &'a ArrayReaderBuilderContext
_ => (),
}

let item_reader = self
.dispatch(item_type.clone(), &new_context)
.unwrap()
.unwrap();
Comment on lines -324 to -327
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a column is not included, dispatch will return None. unwrap here will get unexpected error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I found using a whitespace blind diff made the changes easier to understand: https://github.com/apache/arrow-rs/pull/1517/files?w=1


let reader = self.dispatch(item_type.clone(), &new_context);
if let Ok(Some(item_reader)) = reader {
let item_reader_type = item_reader.get_data_type().clone();

match item_reader_type {
Expand All @@ -339,7 +338,8 @@ impl<'a> TypeVisitor<Option<Box<dyn ArrayReader>>, &'a ArrayReaderBuilderContext
// a list is a group type with a single child. The list child's
// name comes from the child's field name.
// if the child's name is "list" and it has a child, then use this child
if list_child.name() == "list" && !list_child.get_fields().is_empty() {
if list_child.name() == "list" && !list_child.get_fields().is_empty()
{
list_child = list_child.get_fields().first().unwrap();
}
let arrow_type = self
Expand Down Expand Up @@ -386,6 +386,9 @@ impl<'a> TypeVisitor<Option<Box<dyn ArrayReader>>, &'a ArrayReaderBuilderContext
Ok(Some(list_array_reader))
}
}
} else {
reader
}
}
}

Expand Down Expand Up @@ -698,7 +701,12 @@ impl<'a> ArrayReaderBuilder {
ArrowType::Struct(fields) => {
field = fields.iter().find(|f| f.name() == part)
}
ArrowType::List(list_field) => field = Some(list_field.as_ref()),
ArrowType::List(list_field) => match list_field.data_type() {
ArrowType::Struct(fields) => {
field = fields.iter().find(|f| f.name() == part)
}
_ => field = Some(list_field.as_ref()),
},
Comment on lines +704 to +709
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was tested by using the example in #1515. Not sure if it is easy to add a unit test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we put the example parquet file into parquet-testing too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ideally we would construct a parquet file with this pattern programmatically (aka write it out in a test and then read it back in).

if that is not possible / feasible, adding an example in parquet-testing would be second best

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Let me try to make one. Thanks.

_ => field = None,
}
} else {
Expand All @@ -710,14 +718,13 @@ impl<'a> ArrayReaderBuilder {
}
}


#[cfg(test)]
mod tests {
use std::sync::Arc;
use super::*;
use crate::arrow::parquet_to_arrow_schema;
use crate::file::reader::{FileReader, SerializedFileReader};
use crate::util::test_common::get_test_file;
use super::*;
use std::sync::Arc;

#[test]
fn test_create_array_reader() {
Expand Down