Parquet rows skipped when NaN in Float
Checks
- I have checked that this issue has not already been reported.
- I have confirmed this bug exists on the latest version of Polars.
Reproducible example
"""polars 1.44.2, DEFAULT write settings: one NaN in a Float column makes write_parquet
drop the min/max of the whole data page that contains it, so the row-group statistics
understate the range and scan_parquet silently skips rows that are really there."""
import tempfile
import os
import polars as pl
import pyarrow.parquet as pq
path = os.path.join(tempfile.mkdtemp(), "a.parquet")
vals = [float(i) for i in range(1_000_000)]
vals[5] = float("nan") # a single NaN anywhere in the column
pl.DataFrame({"c0": vals}).write_parquet(path) # no options at all
st = pq.ParquetFile(path).metadata.row_group(0).column(0).statistics
print(
"row group 0 stats:", st.min, "..", st.max, " (true min is 0.0)"
) # 116508.0 .. 124999.0
eager = pl.read_parquet(path).filter(pl.col("c0") == 100.0).height
lazy = pl.scan_parquet(path).filter(pl.col("c0") == 100.0).collect().height
print("read_parquet+filter:", eager, " scan_parquet+filter:", lazy) # 1 vs 0
assert eager == lazy, (
"SILENT DATA LOSS: scan_parquet skipped a row group that holds the row"
)Log output
koen@wsl ~/my-polars/fuzz/findings $ python mre_pq_nan_page_stats.py
row group 0 stats: 116508.0 .. 124999.0 (true min is 0.0)
read_parquet+filter: 1 scan_parquet+filter: 0
Traceback (most recent call last):
File "/home/koen/my-polars/fuzz/findings/mre_pq_nan_page_stats.py", line 23, in <module>
assert eager == lazy, (
^^^^^^^^^^^^^
AssertionError: SILENT DATA LOSS: scan_parquet skipped a row group that holds the rowIssue description
See repro, scan_parquet incorrectly skips a row group that holds the filtered data.
Quote AI:
The failure takes three steps.
The page gets NaN stats. build_statistics in arrow/write/primitive/basic.rs computes each page's bounds with NaN-propagating kernels. So the page containing the NaN gets min = max = NaN in its header. I decoded the headers: row group 0 has two pages. Rows 0–116507 carry NaN/NaN, and rows 116508–124999 carry 116508/124999. This already breaks the Parquet rule that NaNs should not be written to min or max statistics fields. github
The writer reads those stats back as "missing". To build the row-group stats, write_column_chunk reads each page's stats back through Statistics::deserialize. That function applies the reader rule and turns NaN into None.
The merge treats "missing" as "no contribution". reduce_primitive merges pages with reduce_single, where (Some(x), None) => Some(x). Here None means "this page's bound is unknown", so the NaN page silently drops out. The row group ends up advertising only page 1's range, which is exactly the 116508..124999 you saw.
Expected behavior
See repro.
Installed versions
Reproduces on 1.44.2 and latest main.
Source: pola-rs/polars