Skip to content

Apply initial defaults under present struct parents - #4480

Closed
rahulsmahadev wants to merge 4 commits into
apache:mainfrom
rahulsmahadev:rahul/nested-default-under-null-struct
Closed

rahulsmahadev wants to merge 4 commits into
apache:mainfrom
rahulsmahadev:rahul/nested-default-under-null-struct

Conversation

@rahulsmahadev

@rahulsmahadev rahulsmahadev commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for this change

Bug: for a field added under an optional struct with an initial default, the translator folds the default over the whole file, so a row whose struct is NULL is treated as having the default. Here pt is an optional struct, row 2 has pt NULL, and pt.c was added with initial-default 5:

tbl.scan(row_filter=EqualTo("pt.c", 5)).to_arrow()  # ids [1, 2, 3]; expected [1, 3]
tbl.scan(row_filter=IsNull("pt.c")).to_arrow()      # ids []; expected [2]

Fix: for =, IN, NOT IN, <, <=, >, >=, IS NULL, IS NOT NULL, STARTS WITH and IS NAN, translate_column_names matches each row by its optional ancestor structs: the default when all of them are present, NULL otherwise. These are the predicates for which PyIceberg's expression evaluator and Arrow give the same result on NULL. A NOT over one of them, or over !=, NOT STARTS WITH or NOT NAN, on such a field is pushed into its complement first, so NOT (c < 4) excludes the NULL-struct rows, as c >= 4 does. Java's Parquet reader preserves NULL structs while supplying defaults; GenericReader applies deletes, then evaluates residuals through Accessors. Only the PyArrow row filter changes; projection was already correct. For < and <=, and for NOT over > and >=, Java's generic Evaluator orders NULL first and keeps the NULL-struct rows (pt.c < 6 and NOT (pt.c > 6) return rows 1, 2 and 3 there, as on main). This PR follows Arrow, which drops NULLs, as PyIceberg does for NULLs stored in a file.

ArrowScan also projects the ancestor structs of filter fields, so filter-after-read (deletes, deletion vectors) can check them; for a file that lacks the filtered field, this reads its parent struct. Arrow field paths are built from field IDs instead of splitting the name on dots, so a struct whose name contains a dot works (the test renames old.pt to new.pt).

Scope: !=, NOT STARTS WITH and NOT NAN on their own keep their main result, and so do NOT (c = 5), NOT (c LIKE 'ab%') and NOT (c IS NAN), which become them. For these the expression evaluator matches NULL and Arrow does not, so their NULL handling is left as it is on main; #4276 (count() and to_arrow() disagree for != on nullable partitions) was closed on Nov 18 and is separate. Java keeps NULL-struct rows for != (notEq is !eq, Evaluator.java:135-148), so pt.c != 5 still differs from Java here. NOT IN now keeps the NULL-struct rows, as Java's notIn (!in) and a file that stores the NULLs do. NOT over !=, NOT IN, NOT STARTS WITH or NOT NAN is now consistent with =, IN, STARTS WITH and IS NAN. NOT keeps its main result for missing top-level fields and fields with only required ancestors (NOT (d > 4) on a column added without a default still returns every row); changing that is separate.

Are these changes tested?

Yes. test_missing_nested_field_under_null_struct_reads_as_null, test_missing_nested_field_under_renamed_null_struct and test_missing_fields_absent_from_file_read_as_null (tests/io/test_pyarrow.py) fail on main, where the first equality returns all three rows, and pass with this change. Together they cover plain and deletion-vector scans, IN and NOT IN, comparisons, LIKE, IS NAN, NOT over AND and OR, NOT over !=, NOT IN and NOT LIKE, a second optional ancestor, a required struct layer, a missing struct, and a child with a NULL default. They also check that NOT keeps its main result on a missing field without an optional struct ancestor (top level and under a required struct), and filter on a column named a.b and on a present field under the renamed new.pt struct, which raise ArrowInvalid on main. In a differential check of 98 filters, each of the 43 results this PR changes matches a file that stores the defaults and the NULLs.

Are there any user-facing changes?

Yes: these filters now honor NULL ancestors. Binding-time constant folding is unchanged, including required-child IS [NOT] NULL and out-of-range comparisons. Range NOT also changes results for a missing nested field with no default or a NaN default, on any format version and even under a present parent: NOT (pt.n < 4) now returns no rows, as a file that stores the NULLs does. Java agrees for NOT over < and <=, but keeps every row for NOT over > and >=, because it orders NULL first. For a NaN default, Java orders NaN last, so it keeps the present rows for NOT over < and <= and drops them for NOT over > and >=. A filter on a top-level column whose name contains a dot (such as a.b) no longer raises ArrowInvalid. !=, NOT STARTS WITH and NOT NAN on their own are unchanged.

Please add the changelog label.

This pull request and its description were written by Isaac.

rahulsmahadev and others added 3 commits November 24, 2026 18:38
A field added under an optional struct with an initial default was folded to that default for the whole file, so rows whose struct is NULL matched as if the field were present. Such predicates now read the field as NULL under a NULL ancestor struct, and a NOT over them (or over != and NOT IN) is pushed into its complement.

Arrow paths are built from field IDs, so a struct whose name contains a dot resolves after a rename.

Co-authored-by: Isaac <[email protected]>
The expression evaluator and Arrow agree on NULL for these predicates, as for
= and IN, so a row under a NULL struct no longer matches the default. NOT over
NOT STARTS WITH and NOT NAN is pushed into STARTS WITH and IS NAN. Drop the test
cases that asserted main's results for top-level and required-only NOT, which
a file storing the NULLs and Java do not return, and the visitor test that did
not reach the new code. Restore the comment on the rewrite_not line.

Co-authored-by: Isaac <[email protected]>
@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

@kevinjqliu could you take a look when you have a chance? Thanks!

Restore NOT (d > 4) and NOT (rq.c2 > 4): without an optional struct ancestor,
NOT keeps its result on a missing field, as in main and Java. Without these
cases, removing the optional-ancestor check in _is_missing_nested_field passed
every test. Also filter on a column named a.b and on a present field under the
renamed new.pt struct; both raise ArrowInvalid on main.

Co-authored-by: Isaac <[email protected]>
@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

Closing this one. It grew too many special cases around the constant fold; a smaller fix is to skip the fold for a missing field under an optional struct and filter the projected batch.

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.

1 participant