Repository navigation
Fix DateTime64, Date32, Time and Time64 constants past the range of their type in queries sent to remote shards and parallel replicas - #126262
Conversation
A DateTime64 constant printed into the query for a shard or replica (by
ConstantNode::toASTImpl and by the ReadFromRemote filter pushdown, both via
columnConstantToExactLiteralAST) was sent as a decimal carrier cast back to
DateTime64. That conversion is range-checked: past 9999-12-31 23:59:59 it
saturates (or throws with date_time_overflow_behavior = 'throw'), while
DateTime64 arithmetic produces such values with no check. So
`valid_to < toDateTime64('9999-12-31 23:59:59', 0, 'UTC') + INTERVAL 1 SECOND`
over remote(), a Distributed table or parallel replicas compared against
9999-12-31 23:59:59 and returned fewer rows than the same query locally.
Send the raw Int64 ticks reinterpreted into the type instead, under an
identity _CAST so that the receiver keeps naming the constant like the
initiator (ConstantNode::receivedFromInitiatorServer). The String -> Int64
hop keeps the source 8 bytes wide: a bare integer literal is typed as the
smallest integer type, which reinterpret would zero-fill. reinterpret to
DateTime64 exists on every supported version (26.3 and 26.8 measured).
Time64 keeps the decimal carrier: reinterpret does not support it.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the DateTime64 and Time64 case comments in columnConstantToExactLiteralASTImpl to the invariants they state. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20261016-215400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261016-210400 |
|
Workflow [PR], commit [1ce97f7] Summary: ❌
AI ReviewSummaryPreserves out-of-range Final VerdictStatus: ✅ Approve |
|
cc @yakov-olkhovskiy @KochetovNicolai, could you review this? A |
…er exactly
A constant printed into the query for another server (`ConstantNode::toASTImpl`, and the filter
pushdown in `ReadFromRemote`) is converted back there by a cast. For these three types every such
form is range-checked, so a value past the range, which arithmetic produces freely
(`toDate32('9999-12-31') + 1`, `toTime('999:59:59') + 1`), reaches the receiver as the boundary:
- `Date32` was sent as date text, which saturates at `0000-01-01` / `9999-12-31` already on the
sender, or in the pushdown as the raw day number, which a cast reads as a Unix timestamp past
`DATE_LUT_MAX_EXTEND_DAY_NUM` (2932897 -> day 33);
- `Time` as its second count, and `Int -> Time` saturates at 999:59:59 on 26.8+;
- `Time64` through the exact decimal carrier of ClickHouse#95055, and `Decimal -> Time64` saturates since ClickHouse#117667.
So `remote()`, `Distributed`, parallel replicas and the pushdown filtered on another value than the
initiator (local 1 row, remote 0), and `date_time_overflow_behavior = 'throw'` made the receiver throw.
Such a value is now sent as an in-range value of the same type plus an offset, because arithmetic is
the one operation that is not range-checked:
`_CAST(plus(_CAST(0, 'Date32'), 2932897), 'Date32')`, `_CAST(plus(_CAST(0, 'Time'), 3600000), 'Time')`,
and for `Time64` the sub-second part through the existing carrier plus the whole seconds:
`_CAST(plus(_CAST(_CAST('0.999', 'Decimal32(3)'), 'Time64(3)'), 3600000), 'Time64(3)')`.
`reinterpret`, used for `DateTime64` in the previous commit, does not accept these types on any
released version (`Code: 43`), so it would turn a wrong answer into an error on every older receiver.
The forms above were measured exact on 26.3.33.24, 26.8.21.12 and 26.9 receivers under `ignore`,
`throw` and `saturate`, including the Int32 extremes for Date32/Time, the Int64 extremes for Time64(0),
and Time64(9) near the Int64 tick limit.
Only values past the range change their wire text. `constantNeedsExactLiteralSerialization` sends a
constant through the exact walk when its type needs it or when it holds an out-of-range `Date32`/`Time`
value, so every in-range top-level constant keeps its text byte for byte (measured). Inside the walk an
in-range `Date32`/`Time` leaf is cast to its type, so an array mixing in-range and out-of-range values
has one element type; without the cast the receiver throws `NO_COMMON_TYPE` under
`use_variant_as_common_type = 0`. The walk also learns `LowCardinality`, which `allow_suspicious_low_cardinality_types`
allows around `Date32`/`Time` and which used to send `_CAST(2932897, 'LowCardinality(Date32)')` (day 33).
JSON typed paths are not affected: the value is already saturated when the JSON constant is built,
locally too. The test of the previous commit is renamed to a type-neutral name and extended.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop two comments (one restating a function name, one that does not hold for a LowCardinality dictionary), shorten the makeOffsetCastAST and constantNeedsExactLiteralSerialization comments to one line, and keep the columnConstantToExactLiteralAST doc closer to master's wording. Co-Authored-By: Claude Opus 5.5 <[email protected]>
DateTime64 constants outside 0000-01-01..9999-12-31 in queries sent to remote shards and parallel replicasDateTime64, Date32, Time and Time64 constants past the range of their type in queries sent to remote shards and parallel replicas
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units82 translation units recompiled, 539 s compile time in total, 82 of them have a recent master baseline. |
CI finish ledger - 1ce97f7Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
#126396 reverts #124131 on master, and #126397 re-lands it with the test adjusted. The only other Session id: cron:our-pr-ci-monitor:20261017-100010 |
…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]>
Related: #124831
Related: #95055
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results of
remote,Distributedand parallel-replicas queries with aDateTime64,Date32,TimeorTime64constant past the range of its type, such asWHERE d < toDate32('9999-12-31') + 1orWHERE valid_to < toDateTime64('9999-12-31 23:59:59', 0, 'UTC') + INTERVAL 1 SECOND: the other server compared against a different value, usually the boundary of the range, or threwVALUE_IS_OUT_OF_RANGE_OF_DATA_TYPEwithdate_time_overflow_behavior = 'throw'.Description
On 26.8.21.12, with a
Date32table holding0000-01-01,9999-12-31and the day beyond each,SELECT count() FROM remote('127.0.0.1', db.t) WHERE d < toDate32('9999-12-31') + 1returns2, while the same filter on the local table returns3.Time(toTime('999:59:59') + 1),DateTime64and, on master,Time64behave the same throughremote(),Distributed, parallel replicas and the filter pushdown.Arithmetic produces such values without a range check. Both places that print a constant into the query for another server (
ConstantNode::toASTImpland the filter pushdown inReadFromRemote, viacolumnConstantToExactLiteralAST) sent forms the receiver converts with a range-checked cast:Date32text (already saturated by the sender), aTimesecond count, theDecimalcarrier forDateTime64/Time64. The cast saturates, or throws underthrow.A
DateTime64leaf is now sent as its raw ticks,_CAST(reinterpret(_CAST('253402300800', 'Int64'), T), T).reinterpretdoes not accept the other three types on any release, so aDate32/Time/Time64value past its range is sent as an in-range value plus an offset, which is not range-checked:_CAST(plus(_CAST(0, 'Date32'), 2932897), 'Date32'); forTime64the sub-second part keeps the carrier. Other constants keep their text, except thatDate32/Timevalues nested in a constant already sent exactly (e.g. one holding aDecimal) are now cast to their type, so an array mixing in-range and out-of-range values has one element type. 26.3, 26.8 and 26.9 receivers evaluate the new forms exactly.Validation: the 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/126262]