Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pyiceberg/table/upsert_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def has_duplicate_rows(df: pyarrow_table, join_cols: list[str]) -> bool:
def _values_differ(
source_values: pa.Array | pa.ChunkedArray, target_values: pa.Array | pa.ChunkedArray
) -> pa.Array | pa.ChunkedArray:
"""Return a mask of the positions where the values are not equal, treating two nulls as equal."""
"""Return a mask of the positions where the values are not equal, treating two nulls and two NaNs as equal."""
try:
equal = pc.fill_null(pc.equal(source_values, target_values), False)
except (pa.ArrowNotImplementedError, pa.ArrowInvalid, pa.ArrowTypeError):
Expand All @@ -69,7 +69,15 @@ def _values_differ(
)

both_null = pc.and_(pc.is_null(source_values), pc.is_null(target_values))
return pc.invert(pc.or_(equal, both_null))
equal = pc.or_(equal, both_null)
if pa.types.is_floating(source_values.type) and pa.types.is_floating(target_values.type):
# NaN never equals NaN under pc.equal, but two NaNs in the same column hold the same value
both_nan = pc.and_(
pc.fill_null(pc.is_nan(source_values), False),
pc.fill_null(pc.is_nan(target_values), False),
)
equal = pc.or_(equal, both_nan)
return pc.invert(equal)


def get_rows_to_update(source_table: pa.Table, target_table: pa.Table, join_cols: list[str]) -> pa.Table:
Expand Down
45 changes: 45 additions & 0 deletions tests/table/test_upsert.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.
from datetime import datetime
from math import isnan
from pathlib import PosixPath

import pyarrow as pa
Expand Down Expand Up @@ -955,3 +956,47 @@ def test_get_rows_to_update_compares_nested_values() -> None:
target = pa.table({"pk": [1, 2], "value": pa.array([{"a": 1}, {"a": 3}])})

assert get_rows_to_update(source, target, ["pk"]).to_pydict() == {"pk": [2], "value": [{"a": 2}]}


def test_get_rows_to_update_treats_nans_as_equal() -> None:
source = pa.table({"pk": [1, 2], "value": pa.array([float("nan"), 1.0], type=pa.float64())})
target = pa.table({"pk": [1, 2], "value": pa.array([float("nan"), 2.0], type=pa.float64())})

assert get_rows_to_update(source, target, ["pk"]).to_pydict() == {"pk": [2], "value": [1.0]}


def test_get_rows_to_update_detects_nan_transitions() -> None:
source = pa.table(
{"pk": [1, 2, 3, 4, 5], "value": pa.array([1.0, float("nan"), float("nan"), None, float("nan")], type=pa.float64())}
)
target = pa.table(
{"pk": [1, 2, 3, 4, 5], "value": pa.array([float("nan"), 2.0, float("nan"), float("nan"), None], type=pa.float64())}
)

result = get_rows_to_update(source, target, ["pk"])

assert result.column("pk").to_pylist() == [1, 2, 4, 5]
values = result.column("value").to_pylist()
assert values[0] == 1.0
assert isnan(values[1])
assert values[2] is None
assert isnan(values[3])


def test_upsert_unchanged_frame_with_nan_row(catalog: Catalog) -> None:
identifier = "default.test_upsert_unchanged_frame_with_nan_row"
_drop_table(catalog, identifier)

arrow_schema = pa.schema([pa.field("id", pa.int32(), nullable=False), pa.field("value", pa.float64())])
df = pa.Table.from_pylist(
[{"id": 1, "value": 1.0}, {"id": 2, "value": None}, {"id": 3, "value": float("nan")}],
schema=arrow_schema,
)
tbl = catalog.create_table(identifier, arrow_schema)
tbl.append(df)
assert len(list(tbl.snapshots())) == 1

res = tbl.upsert(df, join_cols=["id"])

assert_upsert_result(res, 0, 0)
assert len(list(tbl.snapshots())) == 1
Loading