Repository navigation
Saturate a 128- or 256-bit integer converted to DateTime, DateTime64, Time64 - #118717
Conversation
… Time64 The special-case branch lists of `ConvertImpl` for the `DateTime`, `DateTime64` and `Time64` targets enumerated `Int8`/`Int16`/`Int32`, `(U)Int64` and the floats, so `(U)Int128` and `(U)Int256` fell through to the generic numeric path - a plain narrowing cast. The `Date` target already lists them. So `toDateTime(toUInt128(4294967301))` returned `1970-01-01 00:00:05` where the same `UInt64` value saturates to `2106-02-07 06:28:15`, a negative `Int128` wrapped into the far future instead of clamping to 1970, and `toDateTime64`/`toTime64` stored raw ticks beyond the documented maximum. And because `ToDateTimeMonotonicity` reports a numeric source as monotonic, primary-key analysis over a `UInt128` key trusted a wrapping conversion: a part whose maximum wrapped made the transformed range look empty and the part was discarded, so `SELECT count() FROM big WHERE toDateTime(n) = toDateTime(toUInt64(1000000))` returned 0 while `countIf` counts 1. The wide integers are listed in those branches now, so they use the same saturating transforms; with saturation the monotonicity the functions claim holds again. The transforms themselves clamped by casting the value to `time_t`/`Int64` first, which truncates a 128- or 256-bit value into the range before the clamp sees it, so they compare in the source domain when the source is wider - the same reasoning the existing unsigned-domain comparison for `UInt64` documents. Closes: #117682 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [5ae9033] Summary: ✅
AI ReviewSummaryThe current diff adds the remaining wide-integer saturation fix for PR Metadata
Final Verdict✅ No blocking code issues in the current diff. Update the changelog entry so it matches the remaining LLVM Coverage ReportMeasured on commit 5ae9033.
Changed lines: Changed C/C++ lines covered: 15/16 (93.75%) · Uncovered code |
`UInt32` was not listed in the unsigned `DateTime64`/`Time64` branch, so `toTime64(toUInt32(4000000), 0)` still stored a value above the `Time64` maximum while the same `UInt64` value clamps. Every `UInt32` is in range for `DateTime64`, so routing it through the same transform answers exactly what the generic path answered there. The structured documentation of `toDateTime32` still said that the wide integers wrap instead of saturating; it now lists them among the supported sources. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Addressed both points: |
`clang-tidy` rejects an uninitialized local under `cppcoreguidelines-init-variables`, even though both branches of the following `if constexpr` assign it.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units40 translation units recompiled, 273 s compile time in total, 40 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
…time-overflow-117682 Master landed a superseding implementation of the `DateTime64` and `Time64` saturation (it now clamps to the maximum tick and routes the wide integer sources through the saturating transforms), so the conflicting hunks in `ToDateTime64TransformUnsigned`, `ToDateTime64TransformSigned`, `ToTime64TransformUnsigned`, `ToTime64TransformSigned` and the `ConvertImpl` dispatch are resolved in favour of master. What this branch still adds is the `DateTime` target, which master does not cover: the big-integer clamp in `ToDateTimeTransform64Signed` and the `(U)Int128`/`(U)Int256` dispatch entries for `DataTypeDateTime`.
…_overflow_behavior-ignored Master merged #118717, which saturates numeric conversions to `DateTime64` / `Time64` to the maximum or minimum tick in every non-throwing mode and routes the wide integers through the saturating transforms. The transforms are taken from master; this branch keeps threading `date_time_overflow_behavior` into the numeric dispatch so that `throw` is honored, makes the float `throw` check tick-precise, and keeps the `UInt8` / `BFloat16` message-formatter guards. The `04402` section claiming numeric `toDateTime64` ignores the setting stays deleted. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…nored `05042` and `05156` (from #118717) pinned numeric `toDateTime64` / `toTime64` saturating under `throw`; they now expect `VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE`. `NaN` keeps master's `DECIMAL_OVERFLOW` in every mode in `04050`, and `04402` takes master's `toStartOfSecond` comparison for the `Date32` -> `DateTime64(9)` boundary. Co-Authored-By: Claude Fable 5.1 <[email protected]>
ConvertImpl's if-constexpr chain reaches Enum8/Enum16 as genuine FromDataTypes
(callOnTypeIndex.h maps TypeIndex::Enum8 to DataTypeEnum<Int8>), but the two
special-case branch lists for a date/time target enumerate the concrete integer
and float types and never name the Enums, and IsDataTypeNumber<DataTypeEnum<T>>
is unspecialised, so the IsDataTypeNumber-gated branches miss them too. An enum
source therefore fell through to the generic numeric path and was converted by
a plain static_cast, which is modular rather than saturating:
static_cast<UInt32>(Int8(-1)) is 4294967295.
ToDateTimeMonotonicity meanwhile accepts any source for which
isValueRepresentedByNumber() holds, true for Enum, and returns always-monotonic,
so KeyCondition::applyMonotonicFunctionsChainToRange mapped the key range
through the wrapping conversion with no guard against left > right. A range
spanning zero came out inverted: granules holding matching rows were discarded,
and MergeTreeSetIndex::checkInRange tripped its own begin > end consistency
check, which throws LOGICAL_ERROR under ifndef NDEBUG and returns {true, true}
in release. So the debug abort is the CI signature and the wrong query results
are the release-visible face of one defect.
Adding the two types to the two branch lists makes an enum equivalent to the
integer it is stored as at this seam: same FieldType, same ColumnVector, same
transform instantiation, and the same non-strict Monotonicity the struct already
returns for Int8/Int16, which is right for a saturating many-to-one map. No
monotonicity struct is touched and no new monotonicity semantics are introduced.
This is the design ClickHouse#118717 used for (U)Int128/(U)Int256 against the same defect,
and it restores the invariant ClickHouse#117394 states for numeric arguments.
Measured on 26.8.11.7 (LTS), each enum beside the integer it is stored as:
toDateTime(Enum8('neg' = -1)) 2106-02-07 06:28:15 vs 1970-01-01 00:00:00
toDate(Enum8('neg' = -1)) 2149-06-06 vs 1970-01-01
toDateTime(Enum16('lo' = -32768)) 2106-02-06 21:22:08 vs 1970-01-01 00:00:00
toDate(Enum16('lo' = -32768)) 2059-09-19 vs 1970-01-01
Over a MergeTree table keyed by Enum16('neg' = -1, 'zero' = 0) holding both
members, SELECT count() FROM t WHERE toDateTime(x) = toDateTime(0) returned 0
where the same predicate counted without the index returned 1, and the IN form
aborted a debug build in checkInRange. In-range enum values are unaffected: every
positive member is inside both result ranges.
Four other branch lists name Int8/Int16 and still omit the Enums, and are
deliberately not mirrored here because no carrier was measured behind them:
to Date32 (static_cast<Int32> of Int8/Int16 is exact over the whole enum range),
to Time (Time caps at plus or minus 3599999 seconds, so the whole enum range is
inside it), and to DateTime64 and Time64 (an enum source is rejected with
Code: 70, so the conversion is unreachable).
date_time_overflow_behavior has no effect for an enum source before or after
this change: the GENERATE_OVERFLOW_MODE_CASE switch sits inside a predicate that
is false for Enum, so ConvertImpl is always instantiated with the default Ignore.
Measured under ignore, saturate and throw, an enum agrees with Int8 in all three.
That is the pre-existing family-wide gap ClickHouse#118717's own merged test records, and
it is out of scope here.
05076_datetime_conversion_monotonicity_static_cast is deleted rather than
rebaselined: its wide-integer arms encode the pre-ClickHouse#118717 wrapping semantics, so
four of its reference values now pin behaviour master deliberately changed. The
new 05238_enum_to_datetime_overflow covers the conversion image beside the
integer, in-range controls, the CAST and wrapper forms, the overflow modes, and
the primary-key arms including the one that reproduces the assertion.
Related: ClickHouse#113515
Related: ClickHouse#116946
Related: ClickHouse#118717
Co-Authored-By: Claude Opus 5 <[email protected]>
Closes: #117682
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Conversions of 128- and 256-bit integers to
DateTime,DateTime64andTime64now saturate like the 64-bit ones instead of wrapping or storing an out-of-range value, which also silently dropped rows through primary-key pruning.Description
The special-case branch lists of
ConvertImplfor theDateTime,DateTime64andTime64targets enumeratedInt8/Int16/Int32,(U)Int64and the floats, so(U)Int128and(U)Int256fell through to the generic numeric path - a plain narrowing cast. TheDatetarget already lists them.So
toDateTime(toUInt128(4294967301))returned1970-01-01 00:00:05where theUInt64value saturates to2106-02-07 06:28:15, a negativeInt128wrapped into the far future instead of clamping to 1970, andtoDateTime64/toTime64stored raw ticks beyond the documented maximum. And becauseToDateTimeMonotonicityreports a numeric source as monotonic, primary-key analysis over aUInt128key trusted a wrapping conversion: a part whose maximum wrapped made the transformed range look empty and the whole part was discarded, soSELECT count() FROM big WHERE toDateTime(n) = toDateTime(toUInt64(1000000))returned 0 whilecountIfcounts 1.The wide integers are now listed in those branches, so they use the same saturating transforms; with saturation the monotonicity the functions claim holds again.
The transforms themselves clamped by casting the value to
time_t/Int64first, which truncates a 128- or 256-bit value into the range before the clamp sees it, so they now compare in the source domain when the source is wider - the same reasoning the existing unsigned-domain comparison forUInt64documents.Workflow [PR]
Sync PR [sync-upstream/pr/118717]