Skip to content

PyArrow: Collect NaN value counts for nested fields when writing Parquet files - #4131

Open
DG47 wants to merge 5 commits into
apache:mainfrom
DG47:fix/collect-nan-value-counts
Open

DG47 wants to merge 5 commits into
apache:mainfrom
DG47:fix/collect-nan-value-counts

Conversation

@DG47

@DG47 DG47 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Closes #4130

Rationale for this change

Parquet statistics do not track NaN values. #4401 now counts NaNs while writing, but only for top-level float and double columns, and its description leaves nested fields for a follow-up. This PR is that follow-up. After merging main into this branch, it extends _nan_value_counts_from_arrow to also count float and double fields inside structs, list elements, and map keys and values.

Without nested counts, a file whose s.v has no NaNs cannot be pruned on s.v IS NAN, since a missing count means "unknown". With this change the writer records nan_value_counts[s.v] = 0 and the scan skips the file.

The helper now takes the file's Iceberg schema and walks it in the same way for structs, lists and maps. It still skips columns whose metrics mode is none and leaves out columns it cannot count. Top-level columns are counted the same way #4401 counts them. Files without float or double fields do no extra work.

Are these changes tested?

Yes.

Are there any user-facing changes?

Data files written by PyIceberg now include nan_value_counts for nested float and double fields, so scans can prune on them.

DG47 added 3 commits October 21, 2026 06:27
Parquet statistics do not track NaN values, so data files written by
PyIceberg never carried nan_value_counts. The strict metrics evaluator
reads a missing NaN count as "no NaN values", so a delete such as
`value < 5.0` dropped whole files and lost NaN rows that do not match
the filter. Count NaN values of float and double columns while writing,
as the Java Parquet writers do.
…-counts

# Conflicts:
#	tests/catalog/test_catalog_behaviors.py
@DG47

DG47 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

I'd like a review on this one from @Fokko or @kevinjqliu. It closes #4130: data files written by PyIceberg never carried nan_value_counts, so the strict metrics evaluator treated a missing count as "no NaN values" and Table.delete("x < 5.0") dropped a whole file containing [1.0, NaN], losing the NaN row. The change counts NaN values for float and double columns in ParquetFormatWriter.write, including nested struct fields, list elements and map values, and skips columns whose metrics mode is none to match the Java writers. Previously written files and files added through add_files are left as they are.

…-counts

# Conflicts:
#	tests/catalog/test_catalog_behaviors.py
@DG47

DG47 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Since PyIceberg never writes nan_value_counts, Table.delete("x < 5.0") currently drops a whole file containing [1.0, NaN] and loses the NaN row. This PR's change in ParquetFormatWriter.write counts NaN values for float and double columns (closes #4130), and I'm following up to ask @Fokko or @kevinjqliu for a review.

…ue-counts

# Conflicts:
#	pyiceberg/io/pyarrow.py
#	tests/io/test_format_writers.py
@DG47 DG47 changed the title PyArrow: Collect NaN value counts when writing Parquet files PyArrow: Collect NaN value counts for nested fields when writing Parquet files Nov 23, 2026
@DG47

DG47 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

#4401 landed on main with NaN counts for top-level float and double columns, so I merged main into this branch (6670ecb) and rebuilt the change on top of it. What's left is the part #4401 leaves for later: _nan_value_counts_from_arrow now also counts struct fields, list elements and map keys and values. I dropped my metrics-mode test because it duplicated the one #4401 added, and I updated the description and title.

For example, if a file has no NaNs in s.v, it now gets nan_value_counts[s.v] = 0 and is skipped by s.v IS NAN. On main that scan still plans the file. The delete() reproduction from #4130 passes on main now, so it stays here only as a regression test.

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.

Data files written by PyIceberg have no nan_value_counts, so delete() drops NaN rows that do not match the filter

1 participant