Skip to content

Bug #82981, hash large whole BigDecimal/BigInteger by exact value in a one-column distinct - #9199

Merged
NeverStopFMH merged 1 commit into
mainfrom
bug-82981-r2
Dec 2, 2026
Merged

NeverStopFMH merged 1 commit into
mainfrom
bug-82981-r2

Conversation

@Nelson-cao

Copy link
Copy Markdown
Contributor

Redmine: Bug #82981 (Feedback round 2)

Root cause

DistinctTableLens.Tuple.hashNumber hashed every BigDecimal/BigInteger beyond the long range by Double.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 with Tool.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 its stripTrailingZeros form (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.equals compares 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

…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>
@Nelson-cao

Copy link
Copy Markdown
Contributor Author

Review round 1 (community PR #9199), head reviewed: 002e418. Verdict: clean, no blocker or important findings.

Checked from the commit diff against origin/main:

  • Cause coverage: the fix is keyed on number type, sign, magnitude and scale (BigDecimal and BigInteger, 2^63..1e19 window, beyond long, beyond double range, fractions with more than 15 significant digits), not on the DECIMAL(30,0) repro literal. The branch depends only on the stripTrailingZeros form, so every pair Tool.equals equates among exact types (Long/Integer/Short/Byte/Atomic*/BigInteger/BigDecimal at any scale) hashes alike. Zero, -0.0, NaN, Infinity and the values around 2^63 were walked and behave as documented.
  • The O(1) handling of huge exponents is correct (toBigIntegerExact only when scale <= 0 and integer digits <= 19); the extra >309-digit shortcut is correct because such a value is beyond Double.MAX_VALUE.
  • Refuter changes are applied: huge exponent, Atomic* kept, residual documented in the code comment and PR body.
  • Tests: the spread test (>= 99% distinct hashes) fails on the old hash and passes with the fix; merge tests only use exact doubles; no timing assertion. Grep of all src/test trees of the worktree found no other test asserting hash values or the old merge behavior; Tuple.hash is private to the one-column / stable hash distinct.
  • Per-hash cost is small (about 25 ms per 100k rows measured by the diagnosis).

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.

@Nelson-cao

Copy link
Copy Markdown
Contributor Author

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 NeverStopFMH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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=false in 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:

  1. Hash/equals consistency among exact types (Long, Integer, Short, Byte, Atomic*, BigInteger, BigDecimal at any scale) holds. Everything is reduced to the stripTrailingZeros form, and zero is normalised to BigDecimal.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.
  2. Huge exponents are O(1). precision() - scale() is computed as a long, toBigIntegerExact runs only when scale <= 0 and the magnitude is at most 19 digits, and the more-than-309-digit shortcut hashes by stripped.hashCode() without expanding. The new BigDecimal(dd) check applies only to values with at most 309 integer digits, so it is bounded.
  3. Overflow: there is none. precision() - scale() is computed in long.
  4. Performance: the exact-value path costs one stripTrailingZeros plus, for values with more than 15 digits, one doubleValue and one new BigDecimal(double). This is small and only affects non-long-range or wide values.
  5. 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.
  6. Tests are good. The hash-spread test via reflection of the private hash fails 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 qualified java.util.* names instead of imports, and the reflection test is coupled to the private Tuple.hash method. Neither matters.
  7. The Tuple.hash change is private to the one-column and stable-hash distinct paths. The existing Distinct and SetOperation tests pass.

@NeverStopFMH
NeverStopFMH merged commit 1c3ef56 into main Dec 2, 2026
4 checks passed
@NeverStopFMH
NeverStopFMH deleted the bug-82981-r2 branch December 2, 2026 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants