Skip to content

Fix upsert counting nested NaN rows as changed (#4623) - #4627

Open
neo-bumblebee-ai wants to merge 2 commits into
apache:mainfrom
neo-bumblebee-ai:cursor/upsert-nested-nan-69d0
Open

neo-bumblebee-ai wants to merge 2 commits into
apache:mainfrom
neo-bumblebee-ai:cursor/upsert-nested-nan-69d0

Conversation

@neo-bumblebee-ai

Copy link
Copy Markdown

Rationale for this change

Table.upsert reports an unchanged row as updated when the only difference is a NaN nested inside a struct, list, or map. _values_differ compares flat columns with pc.equal. Nested types have no Arrow equality kernel, so they fall back to Python values from to_pylist(), and float('nan') != float('nan') propagates through dicts, lists, and tuples. The row is rewritten and a new snapshot is committed on every upsert. This is the same symptom as #4618, on the fallback path that #4622 does not change.

The fallback now walks those Python values and treats NaN as equal to NaN. Two nulls stay equal. Null and NaN stay different, in both directions. A real change next to a nested NaN is still an update.

This stands on its own relative to #4622. That change teaches the Arrow kernel path to treat two flat NaNs as equal. This change only replaces the Python fallback used when pc.equal raises. The diffs apply together: flat NaNs are handled by pc.is_nan, nested NaNs by the walk. Float16 also uses this fallback (pc.equal has no half-float kernel), so two float16 NaNs compare equal as well.

Are these changes tested?

Yes. New tests in tests/table/test_upsert.py failed before the change and pass after:

  • test_get_rows_to_update_treats_nested_nans_as_equal: struct, list, map, and a struct that contains a list. A row that differs only by nested NaN is not an update; a real change in the same column still is.
  • test_get_rows_to_update_detects_nested_nan_transitions: number-to-NaN, NaN-to-number, null-to-NaN, and NaN-to-null still count as updates for struct, list, and mixed values. A NaN-to-NaN row in the same batch does not.
  • test_upsert_unchanged_frame_with_nested_nan: re-upserting the same frame gives rows_updated=0 and the snapshot count stays at 1. A later upsert that changes a nested field still reports rows_updated=1.

make lint passes. uv run python -m pytest tests/table/test_upsert.py: 30 passed.

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 nested in a struct, list, or map. No API changes.

AI disclosure: this change was developed with AI assistance; I reviewed and tested it and can explain every line.

Closes #4623

Nested columns compare through Python equality, where NaN is unequal to
NaN, so an unchanged struct, list, or map was rewritten on every upsert.
Walk those values and treat NaN as equal to NaN. Null and NaN still differ.

Co-authored-by: Jignesh Patel <neo-bumblebee-ai@users.noreply.github.com>

@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.

Thanks @neo-bumblebee-ai! Nice catch, verified the new tests fail on main and pass with the change (30 passed), and that the fallback is only reached for types without an Arrow equality kernel. Two small observations inline. Also related: #4622 covers the flat pc.equal path, so together they should cover both.

Comment thread pyiceberg/table/upsert_util.py Outdated
Comment on lines +76 to +79
if isinstance(left, tuple) and isinstance(right, tuple):
return len(left) == len(right) and all(
_py_values_equal(left_item, right_item) for left_item, right_item in zip(left, right, strict=True)
)

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.

Nit: the list and tuple branches are identical apart from the type check. to_pylist() produces tuples for map entries, so both are needed, but they could share one branch:

Suggested change
if isinstance(left, tuple) and isinstance(right, tuple):
return len(left) == len(right) and all(
_py_values_equal(left_item, right_item) for left_item, right_item in zip(left, right, strict=True)
)
if isinstance(left, (list, tuple)) and isinstance(right, (list, tuple)):
return len(left) == len(right) and all(
_py_values_equal(left_item, right_item) for left_item, right_item in zip(left, right, strict=True)
)

Feel free to keep it as is if you prefer the explicit branches.

Comment thread pyiceberg/table/upsert_util.py Outdated
Comment on lines +72 to +75
if isinstance(left, list) and isinstance(right, list):
return len(left) == len(right) and all(
_py_values_equal(left_item, right_item) for left_item, right_item in zip(left, right, strict=True)
)

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.

Question: to_pylist() renders map entries as tuples, so this branch compares maps positionally. Two maps with the same entries in a different order would count as changed (structs, going through the dict branch, are order-insensitive). Is that on purpose? Happy either way, just flagging the inconsistency. And out of curiosity, did you run into this in practice?

to_pylist() renders a map as a list of key-value tuples, so entry order
was compared like a list. Match map entries by key and value, including
NaN. Lists stay positional. The list and tuple fallbacks share one branch.

Co-authored-by: Jignesh Patel <neo-bumblebee-ai@users.noreply.github.com>
@neo-bumblebee-ai

Copy link
Copy Markdown
Author

Thanks Fokko. Merged the list/tuple branches as you suggested.

On map order, good catch. It wasn't on purpose. to_pylist() gives maps as a list of (key, value) tuples, so it was comparing by position. I didn't hit this in practice, only noticed while testing the NaN case. Now when the Arrow type is a map, entries are matched by key and value irrespective of order (works for maps inside structs too), and lists still compare in order. Added a test for both.

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.

Upsert counts rows as changed when NaN is nested in a struct/list (Python fallback path)

3 participants