Skip to content

fix: reorder record batch #629

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

Closed
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
27 changes: 23 additions & 4 deletions crates/iceberg/src/arrow/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ impl ArrowReader {

// Create a projection mask for the batch stream to select which columns in the
// Parquet file that we want in the response
let projection_mask = Self::get_arrow_projection_mask(
// Since Parquet projection mask will lose the order of the columns, we need to reorder.
let (projection_mask, reorder) = Self::get_arrow_projection_mask(
&task.project_field_ids,
&task.schema,
record_batch_stream_builder.parquet_schema(),
Expand Down Expand Up @@ -234,6 +235,11 @@ impl ArrowReader {
// to the requester.
let mut record_batch_stream = record_batch_stream_builder.build()?;
while let Some(batch) = record_batch_stream.try_next().await? {
let batch = if let Some(reorder) = reorder.as_ref() {
batch.project(reorder).expect("must be able to reorder")
} else {
batch
};
tx.send(Ok(batch)).await?
}

Expand Down Expand Up @@ -261,9 +267,9 @@ impl ArrowReader {
iceberg_schema_of_task: &Schema,
parquet_schema: &SchemaDescriptor,
arrow_schema: &ArrowSchemaRef,
) -> Result<ProjectionMask> {
) -> Result<(ProjectionMask, Option<Vec<usize>>)> {
if field_ids.is_empty() {
Ok(ProjectionMask::all())
Ok((ProjectionMask::all(), None))
} else {
// Build the map between field id and column index in Parquet schema.
let mut column_map = HashMap::new();
Expand Down Expand Up @@ -322,7 +328,20 @@ impl ArrowReader {
));
}
}
Ok(ProjectionMask::leaves(parquet_schema, indices))

// projection mask is order by indices
let mut mask_indices = indices.clone();
mask_indices.sort_by_key(|&x| x);
// try to reorder the mask_indices to indices
let reorder = indices
.iter()
.map(|idx| mask_indices.iter().position(|&i| i == *idx).unwrap())
.collect::<Vec<_>>();

Ok((
ProjectionMask::leaves(parquet_schema, indices),
Some(reorder),
))
}
}

Expand Down
Loading