Repository navigation
fix(arrow): keep struct presence when its requested children are missing - #3438
Conversation
When none of a struct's requested children are in a Parquet file and another column is projected, the projection mask selected no leaf under the struct, so it read as NULL on every row. Read one leaf under such a struct for its definition level, like Java's PresenceColumnSelector: inner structs first, then the leaf with the lowest repetition level, then the first. A leaf under a map reads the whole map, and a candidate must pass the projection's type check. Closes apache#3378
JeonDaehong
left a comment
There was a problem hiding this comment.
Compared with iceberg-java (core/parquet/data from main at 059e7ea94, which has apache/iceberg#17320): a file with s: struct<a> (id 3, row 3 s null), read with the table's current s: struct<b> (id 4) plus id:
- iceberg-java
GenericParquetReaders:spresent withbnull on rows 1 and 2, null on row 3 main:snull on all three rows- this PR: same as Java
cargo test -p iceberg --lib also passes with this merged into current main. LGTM.
|
Thanks @JeonDaehong much appreciated! @comphead / @laskoviymishka if you have a chance to take a look, at this one correctness followup from your reviews over in #3255. |
|
@comphead / @laskoviymishka gentle ping. This is follow-up from #3255 on struct-presence, and confirmed this fixes behavior to match iceberg-java. Thx! |
laskoviymishka
left a comment
There was a problem hiding this comment.
Thanks @ewoodbury for splitting this out of #3255. As I read it, the leaf selection follows Java's PresenceColumnSelector down to the first-wins tie-break on equal repetition levels, and @JeonDaehong's side-by-side against iceberg-java already covers the read results. The inline comments are nits.
| /// Nested types (struct/list/map) are flattened in Parquet's columnar format. | ||
| fn include_leaf_field_id(field: &NestedField, field_ids: &mut Vec<i32>) { | ||
| /// Also collects the ids of non-empty structs, which may need a presence leaf. | ||
| fn include_leaf_field_id( |
There was a problem hiding this comment.
Could the two accumulators travel as one value, e.g. struct ProjectedFields { leaf_ids: Vec<i32>, struct_ids: HashSet<i32> } with this walk as a method? The name would stop saying "leaf" while it also collects struct ids, and get_arrow_projection_mask_with_field_ids could take &ProjectedFields instead of a sixth positional param. leaf_ids should stay a Vec, since its order is used further down.
There was a problem hiding this comment.
Makes sense, done - it's now ProjectedFields::include, with leaf_ids kept as a Vec
| in_projected: bool, | ||
| ) -> (bool, Option<PresenceCandidate>) { | ||
| // Unwrap once, like `Fields::filter_leaves`, to count leaves the same way. | ||
| let data_type = match field.data_type() { |
There was a problem hiding this comment.
Could we add a case to test_projection_mask_reads_one_presence_leaf with a Dictionary or FixedSizeList field under the struct? This counter mirrors arrow's filter_leaves by hand, and its indexes feed both filter_leaves and ProjectionMask::leaves. Those arms are reachable through an ARROW:schema hint, but no test drives them through the walk, so a change in arrow's traversal would drift without a compile error.
There was a problem hiding this comment.
Added, and this turned out to actually catch a new bug: since Arrow 60 bump (merged yesterday), filter_leaves counts the columns inside a ListView, but the walk counted the whole ListView as one column so any column after it got a wrong index.
Updated so this walk now counts the same way Arrow does, and never picks a column inside a ListView, since Iceberg can't read those. The test covers that, including a struct nested inside a ListView.
…sence-pr # Conflicts: # crates/iceberg/src/arrow/reader/projection.rs
Which issue does this PR close?
What changes are included in this PR?
If every field inside a struct was dropped and re-added after a Parquet data file was written, reading that file returned NULL for the whole struct on every row whenever another column was also selected. The reader picks which Parquet columns to read by matching field ids. None of the struct's current fields exist in the file, so no column under the struct was read, and the reader lost track of which rows have the struct present. Selecting only the struct hid the bug because a reader can fall back to reading every column.
This PR is a followup from the review of #3255 where we determined this fix was out-of-scope there.
This PR reads one existing column under such a struct to recover which rows have it present, then drops that column. Iceberg Java does the same in
PresenceColumnSelector(apache/iceberg#17320), and the results now match Java. The column is chosen by the same rules:PruneColumns.mapalso keeps the whole map once its key is selected.Java reads no column for a struct that can never be null. This PR still reads one, because without it the struct is missing from the batch and the read fails with
Missing required field.The extra column must also pass the reader's existing type check: every field on its path needs a field id, and the columns it reads must convert to Iceberg types. Only files from non-Iceberg writers can fail that check. If no column under a struct passes, that struct reads as it does on
main: NULL, or for a map value struct, the existingMap field must have exactly 2 fieldserror.A struct whose only selected field is in the file but fails the reader's type-promotion check also still reads as NULL, as on
main. That check drops any column it rejects, so it is separate from this fix.Behavior changes:
mainwithMissing required field. It now reads.map<k, struct>whose value struct had every field re-added failed onmainwithMap field must have exactly 2 fields. It now reads.initial-defaultnow fails withFeatureUnsupportedinstead of reading the whole struct as NULL. Onmain, other reads of these structs already fail this way: with an old field kept, or with only the struct selected. This lets the fix for applying nested defaults stay tracked in existing issue RecordBatchTransformer does not apply initial-default for nested fields #3261 for a separate PR.Are these changes tested?
test_struct_presence_when_requested_children_missingreads, throughArrowReader, a file where every struct had all its fields re-added under new ids. It follows the no-default cases of Java'sReadFormatModelTestsfrom Parquet: Fix nested initial default applied when ancestor struct is null iceberg#17320 and includes null and empty lists and maps. I read each column next toidonmainand on this branch:mains: struct with a nested structt: struct holding only a listu: struct holding only a mapl: list of structsm: map with a struct valueMap field must have exactly 2 fieldsr: required structMissing required field: rtest_projection_mask_reads_one_presence_leafchecks which column is read. It skips a list element, a struct with no field id, and auint64column, then picks theintnext to thatuint64inside a nested struct, ahead of a later column at the same level. It also checks that selecting only the struct reads just that one column (onmainit read the whole file), and that a struct whose only column is a map with auint64value reads nothing extra.To check that the tests cover each part of the fix, I disabled the parts one at a time and confirmed that each change made a test fail.
These pass locally:
cargo test -p iceberg --lib --all-featurescargo clippy -p iceberg --all-targets --all-features -- -D warningscargo fmt --all -- --checkdev/check_license_notice.shAI Disclosure
Drafted with AI assistance (Grok and Claude). I fully own the fix strategy and its correctness.