Skip to content

Apply initial defaults under present struct parents - #4480

Open
rahulsmahadev wants to merge 1 commit into
apache:mainfrom
rahulsmahadev:rahul/nested-default-under-null-struct
Open

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

Conversation

@rahulsmahadev

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, <, <=, >, >=, IS NULL and IS NOT NULL, translate_column_names matches each row by its optional ancestor structs: the default when all of them are present, NULL otherwise. A NOT over one of these predicates, or over != and NOT IN, 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. This fixes Arrow's filter translation and projection. Arrow's < and <= still differ from Java's nulls-first generic evaluator.

ArrowScan also projects the ancestor structs of filter fields, so filter-after-read (deletes, deletion vectors) can check them. Arrow field paths are built from field IDs instead of splitting the name on dots, so a struct renamed from old.pt to new.pt works.

Scope: != and NOT IN on their own keep their main result, and so does NOT (c = 5). 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 them (notEq is !eq and notIn is !in, Evaluator.java:135-148), so pt.c != 5 still differs from Java here. NOT over != or NOT IN is now consistent with = and IN. STARTS WITH and NaN predicates are not changed. NOT keeps its result for missing top-level fields and fields with only required ancestors.

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, comparisons, NOT over AND and OR, NOT over != and NOT IN, a second optional ancestor, a required struct layer, a missing struct, and a child with a NULL default. test_translate_column_names_not_on_missing_top_level_column_keeps_its_result (tests/expressions/test_visitors.py) passes on main and pins the top-level NOT.

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 missing nested fields defaulting to NULL or NaN, even with a present parent. A filter on a top-level column whose name contains a dot (such as a.b) no longer raises ArrowInvalid. != and NOT IN on their own are unchanged.

Please add the changelog label.

This pull request and its description were written by Isaac.

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 <no-reply@databricks.com>

This branch has not been deployed

No deployments
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