Skip to content

fix(arrow): keep struct presence when its requested children are missing - #3438

Merged
laskoviymishka merged 5 commits into
apache:mainfrom
ewoodbury:fix/3378-struct-presence-pr
Nov 12, 2026
Merged

laskoviymishka merged 5 commits into
apache:mainfrom
ewoodbury:fix/3378-struct-presence-pr

Conversation

@ewoodbury

@ewoodbury ewoodbury commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • An inner struct picks first, so one column can cover the structs around it.
  • The column with the lowest repetition level wins, then the first one in the file.
  • Only columns with field ids count.
  • A column under a map brings in the whole map, because parquet-rs drops a map unless both its key and value are read. Java's PruneColumns.map also 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 existing Map field must have exactly 2 fields error.

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:

  • A struct whose fields were all re-added now reads correctly. Before, it came back NULL on every row.
  • A required struct like that failed on main with Missing required field. It now reads.
  • A map<k, struct> whose value struct had every field re-added failed on main with Map field must have exactly 2 fields. It now reads.
  • When every old field of a struct is gone and another column is selected, a new field with an initial-default now fails with FeatureUnsupported instead of reading the whole struct as NULL. On main, 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_missing reads, through ArrowReader, a file where every struct had all its fields re-added under new ids. It follows the no-default cases of Java's ReadFormatModelTests from 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 to id on main and on this branch:

    Column main This branch
    s: struct with a nested struct NULL in all 4 rows correct
    t: struct holding only a list NULL in all 4 rows correct
    u: struct holding only a map NULL in all 4 rows correct
    l: list of structs NULL in all 4 rows correct
    m: map with a struct value Map field must have exactly 2 fields correct
    r: required struct Missing required field: r correct
  • test_projection_mask_reads_one_presence_leaf checks which column is read. It skips a list element, a struct with no field id, and a uint64 column, then picks the int next to that uint64 inside 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 (on main it read the whole file), and that a struct whose only column is a map with a uint64 value 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-features
  • cargo clippy -p iceberg --all-targets --all-features -- -D warnings
  • cargo fmt --all -- --check
  • dev/check_license_notice.sh

AI Disclosure

Drafted with AI assistance (Grok and Claude). I fully own the fix strategy and its correctness.

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 JeonDaehong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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: s present with b null on rows 1 and 2, null on row 3
  • main: s null on all three rows
  • this PR: same as Java

cargo test -p iceberg --lib also passes with this merged into current main. LGTM.

@ewoodbury

Copy link
Copy Markdown
Contributor Author

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.

@ewoodbury

Copy link
Copy Markdown
Contributor Author

@comphead / @laskoviymishka gentle ping. This is follow-up from #3255 on struct-presence, and confirmed this fixes behavior to match iceberg-java. Thx!

@laskoviymishka
laskoviymishka self-requested a review November 11, 2026 10:29

@laskoviymishka laskoviymishka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@laskoviymishka
laskoviymishka added this pull request to the merge queue Nov 12, 2026
Merged via the queue into apache:main with commit b03f4c2 Nov 12, 2026
24 checks passed
@ewoodbury
ewoodbury deleted the fix/3378-struct-presence-pr branch November 13, 2026 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Struct reads as NULL when all requested children are missing from the file and another column is projected

3 participants