fix(caching): include shape/dtype/schema in fingerprint hashing#1617
Closed
elijahbenizzy wants to merge 1 commit into
Closed
fix(caching): include shape/dtype/schema in fingerprint hashing#1617elijahbenizzy wants to merge 1 commit into
elijahbenizzy wants to merge 1 commit into
Conversation
The numpy, pandas, and polars fingerprint hashes in hamilton/caching/fingerprinting.py previously hashed only the underlying values, dropping shape, dtype, column names, and Series names. This caused cache key collisions between semantically distinct inputs:
* np.array([1,2,3,4,5,6]) vs np.array([[1,2,3],[4,5,6]])
(same .tobytes(), different shapes)
* np.array([1,2,3], int32) vs np.array([1,0,2,0,3,0], int16)
(same .tobytes(), different dtypes)
* pd.DataFrame({customer_revenue: [100,200]})
vs pd.DataFrame({product_cost: [100,200]})
(same row values, different column names)
* pl.DataFrame({customer_revenue: [100,200]})
vs pl.DataFrame({product_cost: [100,200]})
(same row values, different column names)
With caching enabled, the cache returned the prior result for the second input silently -- producing incorrect outputs with no warning.
The fix incorporates the missing metadata into the hash:
- hash_numpy_array: prepends shape and dtype to the byte buffer.
- hash_polars_dataframe: includes obj.schema alongside row hashes.
- hash_pandas_obj: includes column-name/dtype pairs (DataFrame) or name/dtype (Series/Index) alongside row hashes.
Adds focused regression tests for each collision case plus identical-input determinism sanity checks. Existing golden hashes for test_hash_pandas / test_hash_numpy are updated to reflect the new schema-aware computation.
Contributor
Author
|
Fixed in #1616 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The numpy, pandas, and polars fingerprint hashes in
hamilton/caching/fingerprinting.pypreviously hashed only the underlying values, dropping shape, dtype, column names, and Series names. This caused cache key collisions between semantically distinct inputs:np.array([1,2,3,4,5,6])vsnp.array([[1,2,3],[4,5,6]])(same.tobytes(), different shapes)np.array([1,2,3], int32)vsnp.array([1,0,2,0,3,0], int16)(same bytes, different dtypes)pd.DataFrame({"customer_revenue": [100,200]})vspd.DataFrame({"product_cost": [100,200]})(same row values, different column names)With caching enabled, the cache returned the prior result for the second input silently — producing incorrect outputs with no warning.
What this PR does
hash_numpy_array: prependsshapeanddtypeto the byte buffer.hash_polars_dataframe: includesobj.schemaalongside row hashes.hash_pandas_obj: includes(column, dtype)pairs (DataFrame) or(name, dtype)(Series/Index) alongside row hashes.Test plan
tests/caching/test_fingerprinting.pycovering each collision case + identical-input determinism.test_hash_pandasandtest_hash_numpygolden hashes for the new schema-aware computation.tests/caching/tests pass locally.