Fokko commented on code in PR #4627:
URL: https://github.com/apache/iceberg-python/pull/4627#discussion_r4812280140


##########
pyiceberg/table/upsert_util.py:
##########
@@ -53,6 +54,32 @@ def has_duplicate_rows(df: pyarrow_table, join_cols: 
list[str]) -> bool:
     return len(df.select(join_cols).group_by(join_cols).aggregate([([], 
"count_all")]).filter(pc.field("count_all") > 1)) > 0
 
 
+def _py_values_equal(left: object, right: object) -> bool:
+    """Return whether two Python values are equal, treating NaN as equal to 
NaN."""
+    # Python equality is false for NaN, and that result propagates through the 
dicts,
+    # lists, and tuples from to_pylist(). Null and NaN stay unequal.
+    if left is right or left == right:
+        return True
+    if isinstance(left, float) and isinstance(right, float) and 
math.isnan(left) and math.isnan(right):
+        return True
+    if isinstance(left, dict) and isinstance(right, dict):
+        if len(left) != len(right):
+            return False
+        for key, left_value in left.items():
+            if key not in right or not _py_values_equal(left_value, 
right[key]):
+                return False
+        return True
+    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)
+        )
+    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)
+        )

Review 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:
   
   ```suggestion
       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.



##########
pyiceberg/table/upsert_util.py:
##########
@@ -53,6 +54,32 @@ def has_duplicate_rows(df: pyarrow_table, join_cols: 
list[str]) -> bool:
     return len(df.select(join_cols).group_by(join_cols).aggregate([([], 
"count_all")]).filter(pc.field("count_all") > 1)) > 0
 
 
+def _py_values_equal(left: object, right: object) -> bool:
+    """Return whether two Python values are equal, treating NaN as equal to 
NaN."""
+    # Python equality is false for NaN, and that result propagates through the 
dicts,
+    # lists, and tuples from to_pylist(). Null and NaN stay unequal.
+    if left is right or left == right:
+        return True
+    if isinstance(left, float) and isinstance(right, float) and 
math.isnan(left) and math.isnan(right):
+        return True
+    if isinstance(left, dict) and isinstance(right, dict):
+        if len(left) != len(right):
+            return False
+        for key, left_value in left.items():
+            if key not in right or not _py_values_equal(left_value, 
right[key]):
+                return False
+        return True
+    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)
+        )

Review 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?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to