Skip to content

fix: make ClickHouseDecimal hash codes scale-independent (#639) - #699

Open
bjsneto wants to merge 1 commit into
ClickHouse:mainfrom
bjsneto:fix/clickhouse-decimal-gethashcode-scale
Open

bjsneto wants to merge 1 commit into
ClickHouse:mainfrom
bjsneto:fix/clickhouse-decimal-gethashcode-scale

Conversation

@bjsneto

@bjsneto bjsneto commented Oct 25, 2026 •

Copy link
Copy Markdown

Summary

Problem

ClickHouseDecimal.Equals compares numeric values after aligning their scales, but
GetHashCode previously included the raw scale. As a result, values such as 1.5
and 1.5000 were equal but produced different hash codes.

This caused incorrect behavior in HashSet<ClickHouseDecimal>,
Dictionary<ClickHouseDecimal, TValue>, and LINQ operations such as Distinct
and GroupBy.

Fix

GetHashCode now hashes the canonical representation produced by removing trailing
fractional zeros while preserving the original Mantissa and Scale stored by the
instance.

Tests

  • Added regression coverage for equal values with different scales.
  • Verified HashSet<ClickHouseDecimal>.Contains.
  • Verified that GetHashCode() does not change the stored representation.
  • Ran the ClickHouse.Driver.Tests test suite.

Fixes #639


Note

Low Risk
Localized change to hash computation with tests; behavior change only for previously inconsistent equal values in hash-based collections.

Overview
Fixes ClickHouseDecimal violating the Equals/GetHashCode contract when the same numeric value is stored with different mantissa/scale pairs (e.g. 1.5 vs 15000 at scale 4), which broke HashSet, Dictionary, and LINQ deduplication.

GetHashCode now hashes a normalized mantissa and scale (trailing fractional zeros stripped, same idea as equality), using local copies so the instance’s stored Mantissa and Scale are unchanged.

Adds regression tests for equal values across scales (matching hash codes and HashSet.Contains) and a changelog note for #639.

Reviewed by Cursor Bugbot for commit 7acf498. Bugbot is set up for automated code reviews on this repo. Configure here.

Copilot AI balanced review requested due to automatic review settings October 25, 2026 13:51

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

This branch has not been deployed

No deployments
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.

ClickHouseDecimal: GetHashCode depends on Scale, but Equals ignores Scale

2 participants