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

Add check for empty schema in parquet::schema::types::from_thrift_helper #6990

Merged
merged 3 commits into from
Jan 19, 2025
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
30 changes: 30 additions & 0 deletions parquet/src/arrow/arrow_writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3409,4 +3409,34 @@ mod tests {
"Arrow: Incompatible type. Field 'temperature' has type Float64, array has type Int32"
);
}

#[test]
// https://github.com/apache/arrow-rs/issues/6988
fn test_roundtrip_empty_schema() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is @samgqroberts's reproducer from #6988

// create empty record batch with empty schema
let empty_batch = RecordBatch::try_new_with_options(
Arc::new(Schema::empty()),
vec![],
&RecordBatchOptions::default().with_row_count(Some(0)),
)
.unwrap();

// write to parquet
let mut parquet_bytes: Vec<u8> = Vec::new();
let mut writer =
ArrowWriter::try_new(&mut parquet_bytes, empty_batch.schema(), None).unwrap();
writer.write(&empty_batch).unwrap();
writer.close().unwrap();

// read from parquet
let bytes = Bytes::from(parquet_bytes);
let reader = ParquetRecordBatchReaderBuilder::try_new(bytes).unwrap();
assert_eq!(reader.schema(), &empty_batch.schema());
let batches: Vec<_> = reader
.build()
.unwrap()
.collect::<ArrowResult<Vec<_>>>()
.unwrap();
assert_eq!(batches.len(), 0);
}
}
7 changes: 7 additions & 0 deletions parquet/src/schema/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,13 @@ fn from_thrift_helper(elements: &[SchemaElement], index: usize) -> Result<(usize
));
}
let element = &elements[index];

// Check for empty schema
if let (true, None | Some(0)) = (is_root_node, element.num_children) {
let builder = Type::group_type_builder(&element.name);
return Ok((index + 1, Arc::new(builder.build().unwrap())));
}

let converted_type = ConvertedType::try_from(element.converted_type)?;
// LogicalType is only present in v2 Parquet files. ConvertedType is always
// populated, regardless of the version of the file (v1 or v2).
Expand Down
Loading