Skip to content

Fix upsert treating NaN rows as changed (#4618) - #4622

Open
ksaiharshavinay wants to merge 2 commits into
apache:mainfrom
ksaiharshavinay:fix/upsert-nan-matching-4618
Open

ksaiharshavinay wants to merge 2 commits into
apache:mainfrom
ksaiharshavinay:fix/upsert-nan-matching-4618

Conversation

@ksaiharshavinay

Copy link
Copy Markdown

Rationale for this change

Table.upsert treats an unchanged row as updated when a float column holds NaN: _values_differ compares with pc.equal, where NaN never equals NaN, so re-upserting identical data reports rows_updated=1 and commits two extra snapshots. Two nulls are already treated as equal; this extends the same treatment to two NaNs for floating-point columns. Each is_nan result is filled with False first, because is_nan(null) is null and would otherwise mask a genuine null-to-NaN change. Thanks to @KartikGupta2007 for the report and the repro script.

Scope note: NaN nested inside struct/list/map values still compares through the Python fallback path and counts as changed. That is a separate path from the one reported here, so I left it out of this PR; happy to file it as a follow-up.

Are these changes tested?

Yes. Three new tests in tests/table/test_upsert.py, each verified to fail on main and pass with this change:

  • test_get_rows_to_update_treats_nans_as_equal — a NaN-to-NaN row is not returned as an update; a changed row still is.
  • test_get_rows_to_update_detects_nan_transitions — number-to-NaN, NaN-to-number, null-to-NaN and NaN-to-null all still count as updates.
  • test_upsert_unchanged_frame_with_nan_row — the reported scenario end to end: re-upserting the same frame gives rows_updated=0 and the snapshot count stays at 1.

The full tests/table unit suite shows the same pass/fail set as on main (the failures there are pre-existing environment issues in my sandbox, unrelated to this change). Lint via the repo's prek hooks (ruff, ruff-format, mypy strict, codespell) passes on both changed files.

Are there any user-facing changes?

Yes — a behavior fix: upsert no longer reports updates, or writes new snapshots, for rows whose only "difference" is a NaN in a float column. No API changes. (This looks like it wants the changelog label; I can't add labels myself.)

Disclosure: I work with AI assistance and review everything myself before posting.

pc.equal never matches NaN to NaN, so _values_differ reported a row as changed whenever a float column held NaN on both sides, and re-upserting identical data rewrote the row and committed two snapshots. Treat two NaNs in a floating-point column as equal, the same way two nulls are already treated.

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @ksaiharshavinay! I verified that NaN to NaN rows no longer count as updates, and that number to NaN, NaN to number, null to NaN and NaN to null transitions are all still reported. Would you mind filing the follow-up for NaN nested in structs/lists (and float16, which also goes through the Python fallback), since that path still treats them as changed? Thanks @KartikGupta2007 for the report too 🙌

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.

2 participants