Repository navigation
Fix upsert counting nested NaN rows as changed (#4623) - #4627
neo-bumblebee-ai wants to merge 2 commits into
Conversation
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
left a comment
There was a problem hiding this comment.
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.
| 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) | ||
| ) |
There was a problem hiding this comment.
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:
| 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.
| 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) | ||
| ) |
There was a problem hiding this comment.
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>
|
Thanks Fokko. Merged the list/tuple branches as you suggested. On map order, good catch. It wasn't on purpose. |
Rationale for this change
Table.upsertreports an unchanged row as updated when the only difference is a NaN nested inside a struct, list, or map._values_differcompares flat columns withpc.equal. Nested types have no Arrow equality kernel, so they fall back to Python values fromto_pylist(), andfloat('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.equalraises. The diffs apply together: flat NaNs are handled bypc.is_nan, nested NaNs by the walk. Float16 also uses this fallback (pc.equalhas no half-float kernel), so two float16 NaNs compare equal as well.Are these changes tested?
Yes. New tests in
tests/table/test_upsert.pyfailed 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 givesrows_updated=0and the snapshot count stays at 1. A later upsert that changes a nested field still reportsrows_updated=1.make lintpasses.uv run python -m pytest tests/table/test_upsert.py: 30 passed.Are there any user-facing changes?
Yes, a behavior fix.
upsertno 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