Skip to content

Range-check an integer constant converted to DateTime64 or Time64 - #118656

Merged
alexey-milovidov merged 5 commits into
masterfrom
datetime64-in-set-range-check-118437
Sep 20, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
datetime64-in-set-range-check-118437

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

Closes: #118437
Related: #118367

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):

An out-of-range integer constant in an IN set for a DateTime64 or Time64 column no longer wraps to a negative timestamp and matches an unrelated row.

Description

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. #118367 fixed this class for DateTime; these two branches were left, as noted in its review.

The number of seconds is now range-checked, and the multiplication by the scale is checked for overflow, so a constant no DateTime64/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.


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

```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 #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: #118437

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1c359be]

Summary: ❌

job_name test_name status info comment
Build profile diff ERROR

AI Review

Summary

This PR range-checks whole-second UInt64/Int64 constants before converting them to DateTime64/Time64 ticks, so out-of-range IN constants no longer wrap to a negative value and match the wrong row. The current tree also includes the previously requested values regression coverage, and I did not find any remaining correctness, metadata, or test gaps in the touched surface.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 1c359be.

Metric Baseline Current Δ
Lines 88.40% 88.40% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 80.60% 80.60% +0.00%

Changed lines: Changed C/C++ lines covered: 21/21 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 7, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-datetime Date/DateTime/TimeZone datatypes and date-time semantics. label Sep 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing commit 1c359bef3f6fd4fe5f979b1683ad8c04da10ab70 with master failed: RuntimeError: CI logs cluster query failed: SELECT file, countIf(side = 'pr') AS pr_cou….

See the job log for details.

`convertFieldToType` also materializes a value in `values`, where an
unrepresentable constant is reported as `ARGUMENT_OUT_OF_BOUND` rather than
stored as a wrapped tick count. The scale-dependent bound is covered too, and an
in-range constant still materializes.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Addressed: 05144 now covers the values path too - values('x DateTime64(3, ''UTC'')', toUInt64(18446744073709551615)) and the scale-dependent bound toUInt64(9223372036854776) both report ARGUMENT_OUT_OF_BOUND, an in-range constant still materializes, and Time64 is covered the same way.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118656&sha=79247c484e62517c458b92a738b1e9af876950ce&name_0=PR&name_1=Upgrade%20check%20%28amd_release%29 and provide a fix in a separate PR. If the fix is already in progress, link it here.

The Upgrade check reports MutatePlainMergeTreeTask: Code: 47. DB::Exception: Unknown expression identifier \b`. Maybe you meant: ['a']. In scope b, b. (UNKNOWN_IDENTIFIER)on partall_1_1_1_2right after the upgrade. The CI DB shows the same message in the Upgrade check of about 30 unrelated pull requests between 2026-09-08 and 2026-09-16, so it is not caused by this change (a range check inconvertFieldToType`). I did not find an open issue for it.

@groeneai

Copy link
Copy Markdown
Collaborator

Not caused by your change. The fix is already open: #118499 (from 2026-09-07, not yet reviewed). There is no issue for this, so that PR is the tracker.

Your artifact pins the arm precisely. The table is test_7.t_stale_part_type_5 (792870e8-4abc-4ddd-bf70-0d7f4ae58708), section 5 of 04653_mutation_rewrite_stale_part_column_type, and all_1_1_1 is the empty part left behind by ALTER TABLE ... DETACH PART, active again after the restart. From clickhouse-server.upgrade.log:

15:42:29.164  Found 2 outdated data parts. They will be loaded asynchronously
15:42:35.011  MutatePlainMergeTreeTask: Code: 47 ... In scope b, b.  (on all_1_1_1_2)
15:42:35.749  Loaded 2 outdated data parts asynchronously
15:43:07.211  last of 156 error lines for that part, three per attempt
15:43:07.445  Will drop empty part all_1_1_1

Mutation selection wins a startup race against cleanup. StorageMergeTree::startup calls clearEmptyParts() before outdated parts are loaded, and clearEmptyParts returns 0 while outdated_data_parts_loading_finished is false, so the empty part stays active and is selected for mutation version 2 (MODIFY COLUMN b Nullable(String)), whose expression cannot resolve against that part's column list. The attempts repeat for 32 seconds until cleanup drops the part, so the final state is correct and only the server log error scan fails. The repeated identifier in In scope b, b. is the synthesized column_to_updated next to output_columns, not a second problem.

#118499 skips a zero row part in selectPartsToMutate under the same droppability conditions clearEmptyParts itself applies, asks cleanup for that removal immediately instead of waiting for the shared cleanup period, and records a postpone reason.

On the attribution: since 2026-09-08 the CI DB has 36 Upgrade check rows with this message across 34 distinct pull requests and 0 non-PR runs, so 79247c4 is one carrier of many.

@alexey-milovidov alexey-milovidov self-assigned this Sep 20, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 20, 2026
Merged via the queue into master with commit 8986eb6 Sep 20, 2026
348 of 351 checks passed
@alexey-milovidov
alexey-milovidov deleted the datetime64-in-set-range-check-118437 branch September 20, 2026 05:08
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 20, 2026
alexey-milovidov added a commit that referenced this pull request Sep 23, 2026
…s-out-of-column-type-range

The only conflict is in `src/Interpreters/convertFieldToType.cpp`: master landed
`convertWholeSecondsToDateTimeType` (#118656), which range-checks a `UInt64`/`Int64`
constant and the scale-up for `DateTime64` and `Time64` the same way this branch did.
Take master's version; the tests of this branch keep covering the behavior.
alexey-milovidov added a commit that referenced this pull request Oct 8, 2026
Since the master merge that took master's `convertWholeSecondsToDateTimeType`
(#118656), this PR does not change `convertFieldToType` at all. The tests
`05055_datetime64_integer_bound_scale_overflow`,
`05223_time64_integer_bound_scale_overflow` and the added
`ConvertFieldToTypeStrictness` gtests only pin master's behavior (already
covered by master's `05218_datetime64_in_set_out_of_range_constant`), so they
pass on master and make "Bugfix validation" red. Keep the PR scoped to
`WITH FILL`: `04657_with_fill_bounds_out_of_column_type_range` is the
regression test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-datetime Date/DateTime/TimeZone datatypes and date-time semantics. 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

3 participants