kunwp1 commented on code in PR #8955:
URL: https://github.com/apache/texera/pull/8955#discussion_r4648932736


##########
amber/src/main/python/core/models/tuple.py:
##########
@@ -172,6 +172,11 @@ def java_hash_bytes(bytes: Iterator[int], init: int, salt: 
int):
     return h
 
 
+def java_hash_string(value: str, salt: int):
+    units = (unit[0] for unit in struct.iter_unpack(">H", 
value.encode("utf-16-be")))
+    return java_hash_bytes(units, 0, salt)

Review Comment:
   A Java `String` can hold a lone surrogate, but `value.encode("utf-16-be")` 
raises `UnicodeEncodeError` on one, so hashing the tuple would crash. 
`encode("utf-16-be", "surrogatepass")` keeps it as its own code unit, as Java 
does. A JVM gives `-1716779497` for a one-field tuple holding `s3://b/\ud800`, 
which fits as another case in `test_large_binary_hash_matches_java`.
   
   A docstring and `-> int` like the sibling helpers would also help, e.g. 
noting that this runs over UTF-16 code units rather than code points.



##########
amber/src/main/python/core/models/tuple.py:
##########
@@ -476,6 +481,7 @@ def __hash__(self) -> int:
             AttributeType.BINARY: lambda f: java_hash_bytes(
                 (b if b < 128 else b - 256 for b in f), 1, salt
             ),
+            AttributeType.LARGE_BINARY: lambda f: salt + 
java_hash_string(f.uri, salt),

Review Comment:
   This matches Java: `LargeBinary.hashCode` is `Objects.hash(uri)`, i.e. `31 * 
1 + uri.hashCode()`. A one-line comment saying so would explain the bare `salt 
+`.
   
   Separately, and not new here: the STRING entry a few lines up still hashes 
`map(ord, f)`, which is code points, so a non-BMP string key hashes differently 
from Java. For `"a😀"`, Java's tuple hash is `1866147` and this gives `131550`. 
Using the new helper there, `java_hash_string(f, salt)`, gives `1866147`. It 
changes routing for existing non-BMP keys, so a follow-up PR may fit better 
than folding it in here.



-- 
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]

Reply via email to