Repository navigation
Range-check a UInt64 constant converted to DateTime - #118367
Conversation
A `DateTime` stores a `UInt32`, so a `UInt64` constant that does not fit it
cannot equal any value of the column. The conversion took such a constant
unchanged - `UInt64` is the canonical `Field` type of `DateTime`, so it looked
like nothing had to be done - and the column insertion downstream truncated it
modulo 2^32:
SELECT count() FROM t WHERE dt = toUInt64(4294967296); -- 0
SELECT count() FROM t WHERE dt IN (toUInt64(4294967296)); -- 1: matched the epoch row
SELECT count() FROM t WHERE dt NOT IN (toUInt64(4294967296)); -- 2: a row lost
At default settings the disjunction rewrite turns an `OR` chain of equalities
into `IN`, so a `WHERE` in which every comparison is individually false returned
every row the constants wrapped onto.
The `Date`, `Date32` and `Time` branches next to it already range-check for the
same reason; `DateTime` now does too. A constant outside the range is excluded
from a set, as an out-of-range integer for a numeric column already was, and the
`values` table function rejects it the way it rejects one for `Date` or `UInt8`.
Closes: #117244
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [b50a2a4] Summary: ✅
AI ReviewSummaryThis PR fixes the Missing context / blind spots
Final Verdict✅ No findings in the current diff. LLVM Coverage ReportMeasured on commit b50a2a4.
Changed lines: Changed C/C++ lines covered: 3/3 (100.00%) · Uncovered code |
|
TL;DR: the fix itself looks right and fail-safe on every consumer I traced (set members get excluded exactly,
Happy to file the |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 9 s compile time in total, 7 of them have a recent master baseline. |
```sql SELECT count() FROM t64 WHERE dt = toUInt64(18446744073709551615); -- 0 SELECT count() FROM t64 WHERE dt IN (toUInt64(18446744073709551615)); -- 1, a row at 1969-12-31 23:59:59 SELECT count() FROM t64 WHERE dt NOT IN (toUInt64(18446744073709551615)); -- 2 of 3 rows, one is lost ``` The `DateTime64` and `Time64` branches of `convertFieldToTypeImpl` funnelled the constant through `FieldVisitorConvertToNumber<Int64>`, whose `UInt64` arm is a plain `T(x)`, so a value that does not fit `Int64` wrapped to a negative tick count and built a set that matches an unrelated pre-epoch row. PR ClickHouse#118367 fixed this class for `DateTime`; these two branches were left over, as its review noted. The number of whole seconds is now range-checked, and its multiplication by the scale is checked for overflow, so a constant that no `DateTime64` or `Time64` value can equal yields Null and stays out of the set and out of a key range - which is what `=` computes on the same data. Closes: ClickHouse#118437 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
A
DateTimestores aUInt32, so aUInt64constant that does not fit it cannot equal any value of the column. The conversion took such a constant unchanged -UInt64is the canonicalFieldtype ofDateTime, so it looked like nothing had to be done - and the column insertion downstream truncated it modulo 2^32:The
Date,Date32andTimebranches next to it already range-check for the same reason;DateTimenow does too. A constant outside the range is excluded from a set, as an out-of-range integer for a numeric column already was.One note on the
valuespath: the issue asks forvalues('x DateTime', toUInt64(4294967296))to produce whatCASTproduces (a wrapped2106-02-07 06:28:15). It now raisesARGUMENT_OUT_OF_BOUNDinstead, which is what that path already does forDate,Date32and evenUInt8-values('x UInt8', toUInt64(256))has always been an error whileCASTwraps. Rejecting it keepsDateTimeconsistent with every other type there.Closes: #117244
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fixed a
UInt64constant that does not fitUInt32matching an unrelated row of aDateTimecolumn inIN(and in anORchain of equalities, which is rewritten intoIN): the constant was truncated modulo 2^32 instead of being excluded from the set.Workflow [PR]
Sync PR [sync-upstream/pr/118367]