Repository navigation
Fix query parameters: infer a type that holds ClickHouseDecimal values instead of Decimal128(9) - #643
Conversation
…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>
There was a problem hiding this comment.
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
Decimal128orDecimal256types 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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
AI review gate — Human approval in place of AI reviewApproved on the head commit by alex-clickhouse (write access, not the author), standing in for the AI review:
What to do:
Evaluated at |
There was a problem hiding this comment.
Auto-approved by the AI review gate on 1654c49. Details in the approver's comment on this pull request.
AI review gate — auto-approved
History (latest 5 heads, newest first)
|
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
@nameparameter whose value is aClickHouseDecimal, with noClickHouseType,{name:Type}hint, or custom resolver, went to step 5 ofParameterTypeResolution.ResolveTypeName. Step 5 usesTypeConverter.ReverseMapping, which mapsClickHouseDecimalto a fixedDecimal128(9). The client sent the full value string, but the server parsed it asDecimal128(9). So the server dropped all fractional digits after the 9th with no error (0.0000000001became0), and rejected values with more than 29 integer digits withARGUMENT_OUT_OF_BOUND. The ADO.NETClickHouseCommand, both HTTP transports andClickHouseDbParameter.QueryFormall resolve through this one method.The fix adds a
ClickHouseDecimalcase to step 4, next to the existingdecimalcase:Decimal128(9)holds the value exactly, the type staysDecimal128(9). Queries that work now send the same SQL as before.Decimal128up to 38 digits, elseDecimal256.1.000000000000staysDecimal128(9), and a zero with any scale staysDecimal128(9).ArgumentOutOfRangeExceptionon the client. Before, the server truncated it or rejected it. The exception type and message followTypeConverter.InferDecimalType, with the parameter name added.Why not the
InferDecimalTypepolicy from #467?The narrowest-width policy that #467 uses for
Dynamicgives different results in parameter expressions. I checked these queries on 26.9.1.1629:{a:Decimal32(8)} * {b:Decimal32(8)}with1.5fails:Scale 16 is out of bounds (max scale: 9). With the currentDecimal128(9)it returns2.25.{a:Decimal128(2)} / {b:Decimal128(2)}returns0.33, butDecimal128(9)returns0.333333333.So this PR keeps
Decimal128(9)wherever it is exact, and makes the type wider only when the value needs more digits.System.Decimalis not changed.Behavior changes for values that
Decimal128(9)does not holdClickHouseDecimaldivision result (MaxDivisionPrecision= 50) is now sent as aboutDecimal256(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.ArgumentOutOfRangeExceptionon 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 asArray(Decimal128(9)). Their type comes from the element-type inference inTypeConverter, 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 (ClickHouseTypeor a{name:Type}hint).Changes
ADO/Parameters/ParameterTypeResolution.cs: aClickHouseDecimalcase in step 4 (ResolveClickHouseDecimalTypeName).docs/http.mdx: a paragraph about how decimal parameter types are inferred.ClickHouse.Driver/AGENTS.md: added theClickHouseDecimalcase 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:Decimal128(9), including 29 integer digits + 9 fractional digits;1.000000000000,1at scale 80 (more than 76 raw digits), zero at scale 100, and0.000000001900000→Decimal128(10);Decimal128(38)andDecimal256(40);Decimal256(9),Decimal256(6)for 70 digits,Decimal256(0)for 76 digits;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) runsSELECT 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 atDecimal(38, 9). Every expected string ends in a non-zero digit, so the result does not depend onoutput_format_decimal_trailing_zeros.On
main, 20 of the 31 new cases fail (wrong type, serverARGUMENT_OUT_OF_BOUND, no client error). With the fix, all pass.dotnet test --framework net10.0for all ofClickHouse.Driver.Testspasses.scripts/changelog.cs --checkpasses.Checklist
changelog.d/637-clickhousedecimal-parameter-scale.fixes.md)Pre-PR validation gate
main(new tests fail)AGENTS.md/ClickHouse.Driver/AGENTS.md(parametrized tests, changelog fragment, docs)🤖 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
ClickHouseDecimalquery parameters no longer always resolve toDecimal128(9). Resolution now runs in step 4 ofParameterTypeResolution(alongside the existingdecimalspecial case), pickingDecimal128orDecimal256with a scale that can represent the value—stillDecimal128(9)when that is exact, otherwise scale after stripping trailing fractional zeros, boosted toward 9 when precision allows. Values needing more than 76 digits raiseArgumentOutOfRangeExceptionon the client instead of being truncated or rejected on the server.Docs and changelog describe the new inference rules;
AGENTS.mddocuments the precedence change. Unit and integration tests cover resolution edge cases and end-to-endtoTypeName/toStringagainst ClickHouse (both HTTP parameter modes). Arrays/composites remain on fixedDecimal128(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.