fix(types): hash 0.0 and -0.0 to the same value - #1507
Open
MaxFreedomPollard wants to merge 1 commit into
Open
Conversation
The Float and Double wrappers compare zeros numerically, so 0.0 == -0.0, but they hashed the raw bit pattern, which differs by the sign bit. That breaks the Hash and Eq contract: a HashMap or HashSet keyed on these values, such as the cross-shard GROUP BY buffer, puts equal keys in separate buckets. Hash the bits of positive zero instead, matching what Postgres does in hashfloat4 and hashfloat8.
|
|
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.
The
Floatwrapper in pgdog-config and pgdog-vector and theDoublewrapper in pgdog-postgres-types compare zeros numerically, so0.0and-0.0are equal, but theirHashimpls hashed the raw bit pattern, which differs by the sign bit, so two equal values hash differently and aHashMaporHashSetkeyed on them keeps them apart.DatumderivesHash, so this reaches the cross-shard GROUP BY buffer inbackend::pool::connection::aggregate, where a-0.0returned by one shard forms its own group instead of merging with the0.0from another. Each of the threeHashimpls now hashes the bits of positive zero when the value is zero, the same way Postgres normalizes zero inhashfloat4andhashfloat8. New tests cover it in all three crates (test_negative_zero_hashes_like_zeroin pgdog-config and pgdog-vector,test_negative_zero_datums_hash_like_zerooverDatum::Float,Datum::DoubleandDatum::Vector,test_double_negative_zero_groups_with_zero, andtest_distinct_values_still_hash_apartso zero does not swallow other values), the two existing hash consistency tests that asserted the old behaviour now assert equality, andcargo fmt --all -- --check,cargo clippy --all-targets -- -D warningsandcargo nextest runare clean for the three crates.