Skip to content

Group NaNs with different sign bits into one identity partition - #4434

Open
rahulsmahadev wants to merge 1 commit into
apache:mainfrom
rahulsmahadev:rahul/nan-identity-partition
Open

rahulsmahadev wants to merge 1 commit into
apache:mainfrom
rahulsmahadev:rahul/nan-identity-partition

Conversation

@rahulsmahadev

Copy link
Copy Markdown
Contributor

Rationale for this change

Bug: an identity partition on a double or float column writes each NaN row into every NaN partition file when the NaNs differ in the sign bit. float("nan") has the sign bit clear and 0.0 * float("inf") has it set on x86. _determine_partitions groups by the raw bits, so these are two partitions. Each one selects its rows with is_nan, which matches every NaN row:

# table partitioned by identity(value), value is a double
tbl.append(pa.table({"id": [1, 2, 3], "value": [float("nan"), 0.0 * float("inf"), 1.5]}))
sorted(tbl.scan().to_arrow()["id"].to_pylist())  # [1, 1, 2, 2, 3], expected [1, 2, 3]

Fix: canonicalize NaN in the partition helper column before the group by, so every NaN is one partition value, as the spec defines partition equality (Scan Planning). The data column keeps its bits.

Java: Spark computes each row's PartitionKey, then FanoutWriter and ClusteredWriter use Comparators.forType to group all NaNs together. This PR gives _determine_partitions, shared by append and rewrites, the same grouping.

Remaining differences from Java:

Are these changes tested?

Yes, added test_determine_partitions_identity_nan_sign_bits (double and float, tests/io/test_pyarrow.py), and the existing test_append_nan_to_identity_partitioned_table (append and scan, tests/catalog/test_catalog_behaviors.py) now has a -NaN value. Both fail on main, where the scan returns ids 2 and 4 twice and _determine_partitions returns two NaN partitions, and pass with this change.

Are there any user-facing changes?

Yes, appends no longer write duplicate rows for NaN values with different sign bits.

Please add the changelog label.

This pull request and its description were written by Isaac.

_determine_partitions groups identity values by raw bits, so NaNs with different sign
bits form separate partitions. Each partition selects its rows with is_nan, which matches
every NaN row, so each NaN row was written into every NaN partition. Canonicalize NaN in
the partition helper column before grouping. Data is unchanged.

Co-authored-by: Isaac <no-reply@databricks.com>
@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

@rambleraptor, this is the NaN case of the duplication that #4274 fixes for -0.0 and 0.0, which you approved. @breken-ai, you added the NaN filter in #4030. Could you take a look when you have a chance?

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