mbutrovich commented on code in PR #3683:
URL: https://github.com/apache/iceberg-rust/pull/3683#discussion_r4589690762
##########
crates/iceberg/src/arrow/record_batch_transformer.rs:
##########
@@ -494,19 +524,49 @@ impl PromotePlan {
*sorted,
)?))
}
+ PromotePlan::Unshred {
+ target_type,
+ name,
+ field_id,
+ } => {
+ let (storage_fields, columns, nulls) = array
+ .as_struct_opt()
+ .ok_or_else(|| promote_err(array, "struct"))?
+ .clone()
+ .into_parts();
+ // `unshred_variant` skips null rows, so mark the rows under a
null parent as
+ // null. The output keeps the nulls of `array`, because Arrow
rejects a null in
+ // a required child of a valid row.
+ let skip = arrow_buffer::NullBuffer::union(parent_nulls,
nulls.as_ref());
+ let storage = StructArray::try_new(storage_fields, columns,
skip)?;
+ let unshredded = VariantArray::try_new(&storage)
+ .and_then(|variant| unshred_variant(&variant))
+ .map_err(|e| {
+ invalid_data!(
+ "Variant column `{name}` (field id {field_id}) has
invalid shredded data: {e}"
+ )
+ })?;
+ let (fields, columns, _) = cast(&ArrayRef::from(unshredded),
target_type)?
+ .as_struct()
+ .clone()
+ .into_parts();
+ Ok(Arc::new(StructArray::try_new(fields, columns, nulls)?))
+ }
}
}
fn apply_struct(
source: &StructArray,
fields: &Fields,
children: &[ChildPlan],
+ parent_nulls: Option<&arrow_buffer::NullBuffer>,
) -> Result<StructArray> {
+ let nulls = arrow_buffer::NullBuffer::union(parent_nulls,
source.nulls());
Review Comment:
This union now runs for every struct the transformer promotes. When a struct
and one of its ancestors both have nulls,
[`NullBuffer::union`](https://github.com/apache/arrow-rs/blob/b391e5af7dff9f6909af45c01f7dddb68ba08c0f/arrow-buffer/src/buffer/null.rs#L79-L87)
allocates a new bitmap for each batch, even when no variant is below the
struct. Only the `Unshred` arm reads `parent_nulls`. Could we compute the union
only when a child needs it? One way is a flag on `PromotePlan::Struct`, set
when the plan is built if a descendant is `Unshred`.
##########
crates/iceberg/src/arrow/schema.rs:
##########
@@ -158,13 +156,22 @@ pub(crate) fn validate_unshredded_variant(field: &Field,
variant_field_id: i32)
));
}
Some(_) => {}
+ // The shredding spec lets a writer omit `value` if `typed_value`
holds all values.
+ None if shredded && name == "value" => {}
Review Comment:
Could we add an end-to-end read of a shredded variant that omits `value`?
This line accepts that layout, but only
[`test_arrow_projection_mask_validates_variant_storage`](https://github.com/apache/iceberg-rust/blob/9964df486f7252747ded2bbb4f693ee71d214fab/crates/iceberg/src/arrow/reader/projection.rs#L1406-L1414)
covers it, and that test checks only the projection mask. The read through
`VariantArray::try_new` and `unshred_variant` has no test.
A shredded variant as a map value would close the other gap. The new read
tests cover a struct and a list, and a map's entries go through `apply_struct`
with no parent nulls.
I tried both against 9964df4: an `Int64`-shredded column with `value`
removed, and an optional and a required map value with a null map row. Both
read correctly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]