Skip to content

Fix ARGUMENT_OUT_OF_BOUND for a Decimal constant with more digits than its precision in queries sent to remote shards and parallel replicas - #126652

Merged
PedroTadim merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-decimal-constant-beyond-precision-remote
Oct 28, 2026
Merged

PedroTadim merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-decimal-constant-beyond-precision-remote

Conversation

@groeneai

@groeneai groeneai commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #126018 (comment)
Related: #124680

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed ARGUMENT_OUT_OF_BOUND (Decimal value is too big) in remote, Distributed and parallel-replicas queries with a Decimal constant that has more digits than the precision of its type, such as toDecimal32(0.6, 9) + toDecimal32(0.6, 9) (a Decimal(9, 9) equal to 1.2) or toDecimal64(1, 18).

Description

On 26.8.21.12 (and 26.3.33.24), with a Decimal(9, 9) column e holding 1.2, SELECT count() FROM remote('127.0.0.1', db.t) WHERE e = toDecimal32(0.6, 9) + toDecimal32(0.6, 9) SETTINGS prefer_localhost_replica = 0 fails with ARGUMENT_OUT_OF_BOUND, while the same filter on the local table returns 1. On master it also fails through Distributed, parallel replicas, IN sets, the filter pushdown and serialize_query_plan = 1. It is the failure of 05339_decimal_wide_comparison_scale_overflow reported on #126018: under parallel replicas the other replicas failed on _CAST('1', 'Decimal(18, 18)') in every run, and the client saw it only when the error arrived before the coordinator cancelled them.

A Decimal(P, S) value is bounded only by its native integer, so arithmetic and toDecimal* can produce values with more than P digits. The analyzer printed every Decimal constant for another server as _CAST('<text>', '<type>'), and the receiver's String to Decimal conversion rejects more digits than the precision.

Such a value is now sent as its native integer reinterpreted into the type, _CAST(reinterpret(_CAST('1000000000000000000', 'Int64'), 'Decimal(18, 18)'), 'Decimal(18, 18)'), the same form #126262 uses for DateTime64. Values within the precision keep their text. 26.3, 26.8 and 26.9 receivers evaluate the new form exactly.

Validation: the new test fails on master and passes with the fix, including 50 randomized runs and the parallel-replicas and replicated-database configurations.


Workflow [PR]
Sync PR [sync-upstream/pr/126652]

Version info

  • Merged into: 26.11.1.872-master (included in 26.11 and later)

…o other servers

A `Decimal(P, S)` value is bounded only by its native integer, not by `P`:
`toDecimal64(1, 18)` is a `Decimal(18, 18)` holding 1, and
`toDecimal32(0.6, 9) + toDecimal32(0.6, 9)` is a `Decimal(9, 9)` equal to 1.2.
The analyzer printed every `Decimal` constant for another server as
`_CAST('<text>', '<type>')`, and the receiver's `String -> Decimal` conversion
(`readDecimalText`) rejects more integer digits than `P - S`, so `remote()`,
`Distributed`, parallel replicas, `IN` sets, the filter pushdown and
`serialize_query_plan = 1` failed with `ARGUMENT_OUT_OF_BOUND` where the local
query succeeded. Released 26.3 and 26.8 behave the same.

Under the ParallelReplicas CI configuration (`parallel_replicas_local_plan = 1`)
the other replicas hit this error in every run of
`05339_decimal_wide_comparison_scale_overflow`, but the coordinator usually
cancels them first, which is why the test failed only once (PR ClickHouse#126018).

A value with `|raw| >= 10^P` (exactly the condition `readDecimalText` rejects) is
now sent as its native integer reinterpreted into the type,
`_CAST(reinterpret(_CAST('<raw>', 'IntN'), T), T)`: a same-width `reinterpret`
copies the bytes, and the outer `_CAST` over constants keeps
`receivedFromInitiatorServer()` true, so the receiver names the column like the
initiator. Values within the precision keep their text form byte-for-byte.
26.3, 26.8 and 26.9 receivers evaluate the new form exactly. Same form as
ClickHouse#126262 uses for `DateTime64` ticks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (2 nits).

# Sev Finding Verdict Evidence / action
1 💡 No test arm pins the receiver's column naming of the new form under WithMergeableState (the remote arms are single-shard) AGREE, noted receivedFromInitiatorServer() is true for _CAST(reinterpret(...), T) (outer _CAST over deterministic constants), so the shard names the constant by value and type like the initiator
2 💡 The _CAST(reinterpret(_CAST('<int>', 'IntN'), T), T) construction is the same as in #126262 for DateTime64 AGREE, noted #126262 is not merged yet; whichever of the two lands second shares one helper. Both apply cleanly on top of each other

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261019-081900

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. SELECT count() FROM remote('127.0.0.2', db, t) WHERE d = toDecimal64(1, 18) SETTINGS prefer_localhost_replica = 0 (or a constant toDecimal32(0.6, 9) + toDecimal32(0.6, 9)) fails with ARGUMENT_OUT_OF_BOUND on every run on master; parallel replicas with parallel_replicas_local_plan = 0 too.
b Root cause explained? A Decimal(P, S) value is bounded by its native integer, not by P. The analyzer printed it for another server as _CAST('<text>', '<type>'), and the receiver's String to Decimal conversion rejects more than P digits. With parallel_replicas_local_plan = 1 the replicas fail in every run but are usually cancelled first, so 05339 failed once in CI.
c Fix matches root cause? Yes. The serializer leaf that every sender path uses now sends such a value as its native integer reinterpreted into the type; no parser relaxation and no test change.
d Test intent preserved / new tests added? New test 05410_decimal_constant_beyond_precision_remote; 05339 is unchanged and passes in the parallel-replicas configuration.
e Both directions demonstrated? Master: 5/5 FAIL (return code: 69). Fix: 50/50 randomized, 20/20 at each parallel_replicas_local_plan value, 10/10 with a replicated database. On the 05339 shape the replicas failed 100 and 150 times in 50 runs on master, 0 with the fix.
f Fix is general across code paths? Scalar constants, IN sets, nested Array/Tuple/Map/Nullable/Variant/Dynamic, the filter pushdown and serialize_query_plan = 1 all go through this leaf; each has a test arm. LowCardinality(Decimal) cannot be created; JSON is sent as JSON text.
g Fix generalizes across inputs (params/datatypes/wrappers)? All four widths, negative values, the native minimum and maximum, 10^P - 1 vs 10^P, zero, and a constant produced by arithmetic.
h Backward compatible? (maintainer-approved exception only) Yes. Values within the precision keep their text byte-for-byte; 26.3, 26.8 and 26.9 receivers evaluate the new form exactly; no setting.
i Invariants and contracts preserved? Same value (same-width reinterpret is a byte copy), same type, and the receiver names the constant like the initiator (outer _CAST), checked by the arms with the constant in the SELECT list.

Session id: cron:clickhouse-impl-slot-4:20261019-071800

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @yakov-olkhovskiy @alexey-milovidov, could you review this and add the can be tested label so CI runs? A Decimal(P, S) constant can hold more than P digits (for example toDecimal64(1, 18)), and the analyzer sent it to other servers as _CAST('<text>', T), which the receiver rejects because its String to Decimal conversion enforces the precision; such a value is now sent as its native integer reinterpreted into the type, and values within the precision keep their text.

@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c9cfee5]

Summary: ✅


AI Review

Summary

The PR preserves Decimal constants beyond their declared precision when sending queries to remote shards and parallel replicas, reconstructing them from their native integers. Reviewed head c9cfee543257b68dd11c23f0033d36f7bf34ef3d and all prior discussion; no actionable findings. The focused regression test, including two-shard aggregation, passes in Fast test CI. No local build or tests were run.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). labels Oct 19, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 21, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - b6d6662

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (arm_asan_ubsan, targeted) / job-level, no test ran infra, not from this PR: test selection got HTTP 500 (total) memory limit exceeded from CIDB and the job errored before running any test (this PR changes only how a Decimal constant is sent to remote shards) #125613 (ours, open)
PR praktika rollup of the row above #125613 (ours, open)
Sync - CH Inc sync (private, not actionable by me)

CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261021-090014

@PedroTadim

Copy link
Copy Markdown
Member

@groeneai 🕵️ Looks good, two small requests:

  1. Add a two-shard aggregation case to the test. Every remote/Distributed case in 05410 uses a single shard, so nothing covers the case where each shard sends partial results back and the initiator matches their columns to its own expected names. That's the case where a constant the shard received as _CAST(reinterpret(...), T) could come back under a different name. Something like:

    SELECT toDecimal64(1, 18) AS c, count() FROM remote('127.0.0.{1,2}', system.one) GROUP BY c;
    SELECT count() FROM remote('127.0.0.{1,2}', currentDatabase(), t_05410) WHERE d64 = toDecimal64(1, 18) GROUP BY d64;
  2. Comment on the outer _CAST in makeDecimalLiteralASTImpl. _CAST(reinterpret(..., T), T) looks redundant, since reinterpret already returns T. If the outer _CAST is there so the receiving server names the constant like every other _CAST it receives from the initiator, a one-line comment saying so would keep someone from removing it later.

…o shards

The beyond-precision branch of `makeDecimalLiteralASTImpl` now returns
`reinterpret(_CAST('<raw>', 'IntN'), T)`. Both callers of
`columnConstantToExactLiteralAST` (`ConstantNode::toASTImpl` and
`ReadFromRemote`) wrap a result that is not already a `_CAST` to `T` with
`makeCastToTypeNameAST`, so a top-level constant still reaches the other
server as `_CAST(reinterpret(...), T)` and is named like the initiator names
it: the query text the shards receive is byte-identical. Constants nested in
`Array`, `Tuple`, `Map` and `IN` sets lose only an identity cast; the whole
test, and 26.3, 26.8 and 26.9 receivers, give the same results.

The new test cases aggregate `toDecimal64(1, 18)` over two shards, as a
`GROUP BY` key and in a filter, so the initiator merges partial results
whose columns it matches by name. Both fail on master with
`ARGUMENT_OUT_OF_BOUND`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Thanks, both addressed in c9cfee5.

  1. Added both queries under -- partial aggregation results from two shards. Each shard receives _CAST(reinterpret(_CAST('1000000000000000000', 'Int64'), 'Decimal(18, 18)'), 'Decimal(18, 18)'); both queries fail on master with ARGUMENT_OUT_OF_BOUND and pass here.

  2. It is redundant, so I removed it instead of commenting it. A _CAST is needed for the naming (a secondary names a constant folded from a bare reinterpret(...) after that expression), but both callers of columnConstantToExactLiteralAST (ConstantNode::toASTImpl and ReadFromRemote) already wrap a result that is not a _CAST to T with makeCastToTypeNameAST. Without it the shards receive byte-identical query text; only constants nested in Array/Tuple/Map/IN sets lose an identity cast, and the whole test, plus 26.3, 26.8 and 26.9 receivers, give the same results.

@PedroTadim
PedroTadim added this pull request to the merge queue Oct 28, 2026
Merged via the queue into ClickHouse:master with commit 6a019e2 Oct 28, 2026
178 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants