Skip to content

Fix query parameters: infer a type that holds ClickHouseDecimal values instead of Decimal128(9) - #643

Merged
alex-clickhouse merged 1 commit into
mainfrom
polyglot/clickhousedecimal-parameter-scale
Nov 16, 2026
Merged

alex-clickhouse merged 1 commit into
mainfrom
polyglot/clickhousedecimal-parameter-scale

Conversation

@polyglotAI-bot

@polyglotAI-bot polyglotAI-bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes the scalar part of #637. The array and composite part stays open (see "Scope" below), so this PR does not close the issue.

An @name parameter whose value is a ClickHouseDecimal, with no ClickHouseType, {name:Type} hint, or custom resolver, went to step 5 of ParameterTypeResolution.ResolveTypeName. Step 5 uses TypeConverter.ReverseMapping, which maps ClickHouseDecimal to a fixed Decimal128(9). The client sent the full value string, but the server parsed it as Decimal128(9). So the server dropped all fractional digits after the 9th with no error (0.0000000001 became 0), and rejected values with more than 29 integer digits with ARGUMENT_OUT_OF_BOUND. The ADO.NET ClickHouseCommand, both HTTP transports and ClickHouseDbParameter.QueryForm all resolve through this one method.

The fix adds a ClickHouseDecimal case to step 4, next to the existing decimal case:

  • If Decimal128(9) holds the value exactly, the type stays Decimal128(9). Queries that work now send the same SQL as before.
  • If not, the scale is the scale of the value, raised to 9 if the type has room (a value with more than 67 integer digits gets less). The width is Decimal128 up to 38 digits, else Decimal256.
  • Trailing fractional zeros are not counted, because the server also parses them exactly at a smaller scale. Thus 1.000000000000 stays Decimal128(9), and a zero with any scale stays Decimal128(9).
  • A value that needs more than 76 significant digits throws ArgumentOutOfRangeException on the client. Before, the server truncated it or rejected it. The exception type and message follow TypeConverter.InferDecimalType, with the parameter name added.

Why not the InferDecimalType policy from #467?

The narrowest-width policy that #467 uses for Dynamic gives different results in parameter expressions. I checked these queries on 26.9.1.1629:

  • {a:Decimal32(8)} * {b:Decimal32(8)} with 1.5 fails: Scale 16 is out of bounds (max scale: 9). With the current Decimal128(9) it returns 2.25.
  • Using the scale of the value would change the result of division. {a:Decimal128(2)} / {b:Decimal128(2)} returns 0.33, but Decimal128(9) returns 0.333333333.

So this PR keeps Decimal128(9) wherever it is exact, and makes the type wider only when the value needs more digits. System.Decimal is not changed.

Behavior changes for values that Decimal128(9) does not hold

  • The value now reaches the server unchanged. For example, a non-terminating ClickHouseDecimal division result (MaxDivisionPrecision = 50) is now sent as about Decimal256(51), not truncated to 9 decimals. An expression on two such parameters can now exceed the server's scale limit (for example @a * @b → Scale N is out of bounds), where before it ran on truncated inputs.
  • A value with more than 76 significant digits now throws ArgumentOutOfRangeException on the client. Every parameter in the collection is resolved, so this also happens if the SQL does not use the parameter. Unknown parameter types already throw in the same way.

Scope

Decimals inside arrays, tuples and maps (ClickHouseDecimal[], decimal[], …) are still inferred as Array(Decimal128(9)). Their type comes from the element-type inference in TypeConverter, which is a different mechanism. All elements also need one common type, so that part is left for a follow-up. The docs say so and give the workaround (ClickHouseType or a {name:Type} hint).

Changes

  • ADO/Parameters/ParameterTypeResolution.cs: a ClickHouseDecimal case in step 4 (ResolveClickHouseDecimalTypeName).
  • docs/http.mdx: a paragraph about how decimal parameter types are inferred.
  • ClickHouse.Driver/AGENTS.md: added the ClickHouseDecimal case to the description of precedence step 4.
  • changelog.d/637-clickhousedecimal-parameter-scale.fixes.md.

Tests

  • ParameterTypeResolutionTests.ResolveTypeName_ClickHouseDecimalWithoutType_ReturnsTypeThatHoldsValue: one parametrized test for the resolved type. It covers:
    • values that keep Decimal128(9), including 29 integer digits + 9 fractional digits;
    • trailing zeros: 1.000000000000, 1 at scale 80 (more than 76 raw digits), zero at scale 100, and 0.000000001900000 → Decimal128(10);
    • more than 9 fractional digits, up to Decimal128(38) and Decimal256(40);
    • more than 29 integer digits: Decimal256(9), Decimal256(6) for 70 digits, Decimal256(0) for 76 digits;
    • negative values.
  • ResolveTypeName_ClickHouseDecimalWiderThanDecimal256_Throws: 77 integer digits, and 1 integer digit + 80 significant fractional digits.
  • ResolveTypeName_Resolver_WinsOverClickHouseDecimalSpecialCase: a custom resolver still takes precedence.
  • ParameterTypeResolutionIntegrationTests.ExecuteReaderAsync_ClickHouseDecimalWithoutType_KeepsFullValue (both transports) runs SELECT toTypeName(@p), toString(@p) against the server and checks the exact value. The cases are the ones from the issue, plus 70 integer digits (Decimal(76, 6)) and a value with 71 trailing zeros at scale 80. The second extra case shows that the server parses the zeros exactly at Decimal(38, 9). Every expected string ends in a non-zero digit, so the result does not depend on output_format_decimal_trailing_zeros.

On main, 20 of the 31 new cases fail (wrong type, server ARGUMENT_OUT_OF_BOUND, no client error). With the fix, all pass. dotnet test --framework net10.0 for all of ClickHouse.Driver.Tests passes. scripts/changelog.cs --check passes.

Checklist

  • Unit and integration tests covering the common scenarios were added
  • A human-readable description of the changes was provided to include in CHANGELOG (changelog.d/637-clickhousedecimal-parameter-scale.fixes.md)

Pre-PR validation gate

  • Deterministic repro on main (new tests fail)
  • Root cause documented above
  • Fix targets the root cause (step 4 of the precedence chain, the single resolution seam)
  • New tests fail without the fix and pass with it; tests go through the real query path
  • No existing tests changed
  • Conventions per AGENTS.md / ClickHouse.Driver/AGENTS.md (parametrized tests, changelog fragment, docs)
  • No public API change

🤖 Generated with Claude Code


Note

Medium Risk
Changes parameter-type inference on the hot query path; values that previously worked via silent truncation may get wider decimal types and can affect downstream SQL scale limits, though explicit types and custom resolvers are unchanged.

Overview
Untyped ClickHouseDecimal query parameters no longer always resolve to Decimal128(9). Resolution now runs in step 4 of ParameterTypeResolution (alongside the existing decimal special case), picking Decimal128 or Decimal256 with a scale that can represent the value—still Decimal128(9) when that is exact, otherwise scale after stripping trailing fractional zeros, boosted toward 9 when precision allows. Values needing more than 76 digits raise ArgumentOutOfRangeException on the client instead of being truncated or rejected on the server.

Docs and changelog describe the new inference rules; AGENTS.md documents the precedence change. Unit and integration tests cover resolution edge cases and end-to-end toTypeName / toString against ClickHouse (both HTTP parameter modes). Arrays/composites remain on fixed Decimal128(9) element inference—out of scope here.

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

…s instead of Decimal128(9)

An @name parameter with a ClickHouseDecimal value and no explicit type was
always declared Decimal128(9), so the server truncated digits after the 9th
decimal place and rejected values with more than 29 integer digits.

Keep Decimal128(9) when it holds the value exactly. Otherwise use the scale
of the value without trailing zeros, raised to 9 where there is room, in
Decimal128 or, above 38 digits, Decimal256. Throw ArgumentOutOfRangeException
above 76 digits. Composite parameters are not changed.

Part of: #637

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 1, 2026 13:28

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 review overview

🟢 Approval recommended

The inference logic is correct, comprehensively tested across boundaries and transports, and appropriately documented.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes scalar ClickHouseDecimal parameter inference in the HTTP client to preserve precision instead of always using Decimal128(9).

Changes:

  • Infers suitable Decimal128 or Decimal256 types from each scalar value.
  • Adds boundary, precedence, and both HTTP transport tests.
  • Updates documentation, development guidance, and changelog.
File Description
docs/​http.mdx Documents decimal inference and composite limitations.
ClickHouse.Driver/​AGENTS.md Updates type-resolution precedence guidance.
ClickHouse.Driver/​ADO/​Parameters/​ParameterTypeResolution.cs Implements value-aware scalar inference.
ClickHouse.Driver.Tests/​SQL/​ParameterTypeResolutionIntegrationTests.cs Verifies server-side precision preservation.
ClickHouse.Driver.Tests/​ParameterTypeResolutionTests.cs Covers inference boundaries and precedence.
changelog.d/​637-clickhousedecimal-parameter-scale.fixes.md Records the user-visible fix.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Oct 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown

AI review gate — Human approval in place of AI review

Approved on the head commit by alex-clickhouse (write access, not the author), standing in for the AI review:

  • Cursor Bugbot is still reviewing this commit.

What to do:

  • Nothing to do yet if the review is running. The gate turns green when it finishes clean.
  • No review showing up? Post bugbot run as a new top-level PR comment. If that does nothing, Bugbot is probably not enabled for this repository: ask a repository administrator.

Evaluated at 1654c493. This required workflow does not re-run on its own once the AI review later concludes — check the Cursor Bugbot check on this commit directly for its current state.

@security-ai-gate-reviewer security-ai-gate-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Auto-approved by the AI review gate on 1654c49. Details in the approver's comment on this pull request.

@security-ai-gate-reviewer

Copy link
Copy Markdown

AI review gate — auto-approved

1654c493caba is approved by security-ai-gate-reviewer[bot], an automated identity, not a human. The approval stands on the required AI review: Cursor Bugbot concluded green on this commit and the AI review gate is SUCCESS (its own comment has the verdict), and the change touches no path that requires a human reviewer. It is bound to this commit: a new push dismisses it and the approver re-evaluates.

History (latest 5 heads, newest first)

  • 1654c493caba approved — green: Cursor Bugbot

@alex-clickhouse
alex-clickhouse merged commit ee130f4 into main Nov 16, 2026
39 of 41 checks passed
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.

3 participants