Repository navigation
Bug #82981, hash large whole BigDecimal/BigInteger by exact value in a one-column distinct - #9199
Conversation
…a one-column distinct Tuple.hashNumber hashed every BigDecimal/BigInteger beyond the long range by its double, so consecutive values (16384 per double at 1e20) shared one hash and the hash distinct became quadratic. Hash such values, and fractions with more than 15 significant digits that are not exactly a double, by their exact stripped value. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Review round 1 (community PR #9199), head reviewed: 002e418. Verdict: clean, no blocker or important findings. Checked from the commit diff against origin/main:
Residual (documented): a BigDecimal/BigInteger with more than 15 significant digits that is not exactly a double no longer merges with a Double/Float of the same rounded value, and a BigDecimal beyond 1e309 no longer merges with Double Infinity. Both come from the non-transitive Tool.equals, exact-type columns are unaffected. |
|
Live verification on head 002e418 (shared instance, MySQL 8 DECIMAL(30,0) source, Composer mirror with Return Distinct Values, Merge SQL off): last rows of 20,000 / 100,000 consecutive values from 1e20 show in about 0.65 s / 0.56 s (base at real-class level: 2.7 s / 36.8 s); BIGINT from 2^60 control is in the same range. Targeted tests: 52 + 33 pass, the new spread test is red on the old hash. Build check green. |
NeverStopFMH
left a comment
There was a problem hiding this comment.
Verdict
Ready to merge. No blocking issues.
Open verdicts from the conversation
none. The author's "Review round 1" (clean) and "Live verification" comments are both at head 002e418, and there are no inline review comments or formal reviews.
Committed secrets
none. The PR has one commit (002e418). Its patch touches only DistinctTableLens.java and DistinctTableLensMixedTypesTest.java and contains no secret-like content.
Tests run
./mvnw -B test -T 1 -Dsurefire.forkCount=1 -pl core -am -Dtest='DistinctTableLens*Test,SetOperationLensesTest' -Dsurefire.failIfNoSpecifiedTests=falsein a private clone at the PR head. Result: BUILD SUCCESS, 52 tests, 0 failures. Per class: DistinctTableLensMixedTypesTest 13, DistinctTableLensSetFailureTest 10, DistinctTableLensTest 11, SetOperationLensesTest 18.- DistinctTableLensMixedTypesTest ran in the same command, with 13 tests run and none failed. I did not run it a second time on its own, because the 13-test count is already visible in the combined run.
- No build prerequisite problems.
Findings
Non-blocking:
- Hash/equals consistency among exact types (Long, Integer, Short, Byte, Atomic*, BigInteger, BigDecimal at any scale) holds. Everything is reduced to the
stripTrailingZerosform, and zero is normalised toBigDecimal.ZERO. Whole values that fit a long hash as that long. Values that a double holds exactly, or that have 15 or fewer significant digits, keep the double rule, so they still merge with Double/Float of the same value. - Huge exponents are O(1).
precision() - scale()is computed as a long,toBigIntegerExactruns only when scale <= 0 and the magnitude is at most 19 digits, and the more-than-309-digit shortcut hashes bystripped.hashCode()without expanding. Thenew BigDecimal(dd)check applies only to values with at most 309 integer digits, so it is bounded. - Overflow: there is none.
precision() - scale()is computed in long. - Performance: the exact-value path costs one
stripTrailingZerosplus, for values with more than 15 digits, onedoubleValueand onenew BigDecimal(double). This is small and only affects non-long-range or wide values. - Backward compatibility, documented and accepted: a BigDecimal/BigInteger with more than 15 digits that is not an exact double no longer merges with a Double/Float of the same rounded value. A BigDecimal beyond 1e309 no longer merges with Double Infinity. This comes from the non-transitive
Tool.equals(it compares through double), and exact-type columns are unaffected. The note is in the code comment and the PR body. - Tests are good. The hash-spread test via reflection of the private
hashfails on the old code, per the author's report. The merge-group tests use exact doubles only, and there are no timing assertions. Minor style point: the new tests use fully qualifiedjava.util.*names instead of imports, and the reflection test is coupled to the privateTuple.hashmethod. Neither matters. - The
Tuple.hashchange is private to the one-column and stable-hash distinct paths. The existing Distinct and SetOperation tests pass.
Redmine: Bug #82981 (Feedback round 2)
Root cause
DistinctTableLens.Tuple.hashNumberhashed every BigDecimal/BigInteger beyond the long range byDouble.hashCode(doubleValue()). At 1e20 one double covers 16384 consecutive integers, so DECIMAL(30,0) values share a couple of hash buckets (20,000 values gave 2 distinct hashes) and the one-column hash distinct probes long chains withTool.equals: quadratic. Affects BigDecimal and BigInteger, positive and negative, the 2^63..1e19 window, and fractions with more than 15 significant digits.Fix
Only
hashNumber: a BigDecimal/BigInteger is reduced to itsstripTrailingZerosform (so 1, 1.00, 1E+0 still hash alike). Whole values that fit a long keep the long hash. Values a double cannot hold exactly (whole beyond the long range, or more than 15 significant digits and not exactly a double) hash by the exact stripped value. Everything else (including exact doubles such as 1E+20 or 2^63) keeps the double rule, so exact-type equal values and Double/Float of exactly the same value still merge. A huge exponent (1E+1000000) is never expanded (O(1)).Known residual
Tool.equalscompares anything involving a Double/Float by the rounded double, which is not transitive. A BigDecimal/BigInteger with more than 15 significant digits that is not exactly a double (1e20+1, 12345678901234.56) no longer merges with a Double/Float of the same rounded value; same family as the already documented Long 2^60+1 vs Double 2^60. Columns of exact types (the reported DECIMAL case) are unaffected.Tests
Added to
DistinctTableLensMixedTypesTest: merge groups for exact doubles (1E+20 BigDecimal/BigInteger vs 1e20, the 2^63 triple, 1E+1000000 vs 10E+999999 and negative), a hash-spread guard (>= 99% distinct for BigDecimal/BigInteger 1e20+i, negative, 9.3e18+i, 18-digit fraction), consecutive DECIMAL(30,0) rows. The spread test fails on the old hash (2 distinct hashes for BigDecimal 1e20) and passes with the fix.Command:
./mvnw -B test -T 1 -Dsurefire.forkCount=1 -pl community/core -am -Dtest='DistinctTableLens*Test,SetOperationLensesTest'on head 002e418: 52 tests, 0 failures; the class alone: 13 run, 0 failures.🤖 Generated with Claude Code