Skip to content

Fix rows skipped when a sub-second DateTime64 key is compared with an integer by > or < - #123999

Merged
yariks5s merged 5 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-datetime64-key-integer-strict-bound
Oct 21, 2026
Merged

yariks5s merged 5 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-datetime64-key-integer-strict-bound

Conversation

@groeneai

@groeneai groeneai commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #101276
Closes: #113993
Related: #114001
Related: #98410

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

Fix rows silently skipped by primary key, partition key and minmax skip index analysis when a DateTime64 column with a fractional scale is compared with an integer or DateTime constant by > or <, for example WHERE dt > 1577836800 on a DateTime64(3) key. The regression appeared in 26.3.

Description

On 26.3 and later (measured 26.3.33.24 and 26.8.10.6), WHERE dt > 1577836800 on a DateTime64(3) primary key returns 2 rows instead of 3, and dt < 1577836801 returns 1 instead of 3; 25.3.14.14 returns 3 for both.

Root cause. Since #98410, KeyCondition keeps an integer constant as an integer Field against a DateTime64 key. The comparison is exact, but Range closes an open bound on an integer Field by stepping it by one, which here is one second, not one key tick: dt > 1577836800 becomes dt >= 1577836801 and every granule inside that second is pruned.

Change. For a DateTime64 key with scale > 0, the integer (or DateTime, Bool) constant is stored as a scale-0 Decimal256. Comparisons with the key are the same decimal comparison as before, and Range no longer steps the bound. Range itself, integer keys and DateTime64(0) are unchanged. #114001 takes the other route (moving the step out of Range); this one leaves Range untouched, so numbers() range extraction, which relies on that step, is unaffected.

Validation. The new test covers the primary key (plain, reversed, NOT, monotonic chain, toDateTime bound, Nullable), the partition key and a minmax skip index; 11 of its 12 lines fail on master. Also measured on scales 6 and 9, composite keys and Parquet row-group pruning, with no change for Time64, Decimal, DateTime or integer keys.


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

Version info

  • Merged into: 26.11.1.433-master (included in 26.11 and later)
  • Backported to: 26.10.2.64, 26.9.25.16, 26.8.34.14, 26.7.38.8

groeneai and others added 2 commits October 5, 2026 09:58
… integer by > or <

Since ClickHouse#98410, `cast_not_needed` in `KeyCondition::extractAtomFromTree` keeps an
integer constant (and a `DateTime` constant, whose `Field` is `UInt64` seconds)
as an integer `Field` against a `DateTime64(N)` key. The comparison is exact,
but `Range::createLeftBounded` / `createRightBounded` call
`shrinkToIncludedIfPossible`, which closes an open bound on an integer `Field`
by stepping it by one unit of the constant, i.e. one second rather than one key
tick. `dt > 1577836800` became `dt >= 1577836801` and `dt < 1577836801` became
`dt <= 1577836800`, so every granule, part or Parquet row group inside that
second was pruned (primary key, partition key, `minmax` skip index).

For a `DateTime64` key with scale > 0, store such a constant as
`DecimalField<Decimal256>(value, 0)`. `FieldVisitorAccurateLess` / `Equals`
already compared a decimal with an integer by building exactly that field, so
comparison results are unchanged and only the bogus step goes away.
`Decimal256` at scale 0 is used rather than the key's `Decimal64` at scale N
because `value * 10^N` overflows `Int64` for large constants and the decimal
comparison would throw `DECIMAL_OVERFLOW` during index analysis. `Bool` is
converted too, before `normalizeBoolFields` would turn it into a steppable
`UInt64`. `DateTime64(0)` keeps the integer form: its domain is whole seconds.
`Range` is unchanged, so integer keys and `numbers()` range extraction keep
their normalization.

Closes ClickHouse#101276
Closes ClickHouse#113993

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the DateTime64 integer-bound comment in KeyCondition to one line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 5, 2026
@groeneai

groeneai commented Oct 5, 2026

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

Pre-publication review by an independent model (codex; 1 finding; no fix round needed).

# Sev Finding Verdict Evidence
1 ⚠️ Most test arms assert results only, so a full-scan fallback would pass DISAGREE The force_primary_key = 1, max_rows_to_read = 1 control runs the same type-keyed branch, so declining the atom fails the test.

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-slot-9:20261005-100200

@clickhouse-gh

clickhouse-gh Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [228de07]

Summary: ✅


AI Review

Summary

Reviewed PR #123999 at 228de07e14196905b964df2535831ed0942eb348. The change prevents incorrect pruning of fractional DateTime64 values by preserving strict integer and DateTime bounds as scale-zero decimals. No actionable findings. The focused regression test passed in the PR's Fast test job.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 5, 2026
@groeneai
groeneai requested review from CurtizJ and nihalzp October 5, 2026 10:31
@yariks5s yariks5s self-assigned this Oct 5, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Oct 5, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 228de07e1 with master 207f7274f (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 25 s compile time in total, 7 of them have a recent master baseline.

Job report

…-bound

# Conflicts:
#	src/Storages/MergeTree/KeyCondition.cpp
@yariks5s

Copy link
Copy Markdown
Member

@groeneai please update the branch with master

@groeneai

Copy link
Copy Markdown
Collaborator Author

Done: merged current master (2a95d09) into the branch, new head 228de07. No conflicts, your commit is unchanged, and 05325_datetime64_key_integer_strict_bound passes on the merged build (it still fails on today's master).

clickhouse-gh Bot added a commit that referenced this pull request Oct 21, 2026
Backport #123999 to 26.10: Fix rows skipped when a sub-second `DateTime64` key is compared with an integer by `>` or `<`
robot-clickhouse added a commit that referenced this pull request Oct 22, 2026
…e64` key is compared with an integer by `>` or `<`
robot-clickhouse added a commit that referenced this pull request Oct 22, 2026
…e64` key is compared with an integer by `>` or `<`
robot-clickhouse added a commit that referenced this pull request Oct 22, 2026
…e64` key is compared with an integer by `>` or `<`
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 22, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 22, 2026
Backport #123999 to 26.9: Fix rows skipped when a sub-second `DateTime64` key is compared with an integer by `>` or `<`
clickhouse-gh Bot added a commit that referenced this pull request Oct 22, 2026
Backport #123999 to 26.8: Fix rows skipped when a sub-second `DateTime64` key is compared with an integer by `>` or `<`
clickhouse-gh Bot added a commit that referenced this pull request Oct 22, 2026
Backport #123999 to 26.7: Fix rows skipped when a sub-second `DateTime64` key is compared with an integer by `>` or `<`
@clickhouse-gh clickhouse-gh Bot added comp-key-condition Analysis of a filter against a key. KeyCondition turns a WHERE condition into ranges over the pri... and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 22, 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-key-condition Analysis of a filter against a key. KeyCondition turns a WHERE condition into ranges over the pri... groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.4-must-backport v26.6-must-backport

Projects

None yet

5 participants