Skip to content

Fix key condition analysis for DateTime64 compared with integer constants - #98410

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
amosbird:datetime64-key-analysis
Mar 1, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
amosbird:datetime64-key-analysis

Conversation

@amosbird

@amosbird amosbird commented Mar 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

KeyCondition::extractAtomFromTree has a cast_not_needed optimization that recognizes when a key column and a constant can be compared directly without casting (e.g., DateTime vs native integer). However, it only checked for DateTime and missed DateTime64. This caused key analysis to fall through to the type casting path, which produced Condition: true — meaning no granule pruning for DateTime64 primary keys compared against integer constants.

The fix replaces isDateTime with isDateTimeOrDateTime64 in both sides of the check.

Changes:

  • Replace isDateTime with isDateTimeOrDateTime64 in KeyCondition.cpp cast_not_needed check
  • Add stateless test 03400_datetime64_key_condition_with_number verifying correct pruning

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix key condition analysis for DateTime64 primary keys compared with integer constants, which previously resulted in no granule pruning.

Documentation entry for user-facing changes

  • Documentation is not required (bug fix, no new user-facing feature)

Version info

  • Merged into: 26.3.1.219

@clickhouse-gh

clickhouse-gh Bot commented Mar 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [101cffe]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 1, 2026
…ants

The cast_not_needed check in KeyCondition::extractAtomFromTree only
recognized DateTime but not DateTime64, causing key analysis to fall
through to type casting logic which produced 'Condition: true' (no
pruning). This meant DateTime64 primary key columns could not benefit
from granule pruning when compared against integer constants (epoch
values), leading to full table scans.

Fix by replacing isDateTime() with isDateTimeOrDateTime64() in the
cast_not_needed check, consistent with the existing treatment of
DateTime.
@amosbird
amosbird force-pushed the datetime64-key-analysis branch from 5194ee8 to 101cffe Compare March 1, 2026 13:29

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM.

@alexey-milovidov alexey-milovidov self-assigned this Mar 1, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Mar 1, 2026
@alexey-milovidov alexey-milovidov added pr-performance Pull request with some performance improvements and removed pr-bugfix Pull request with bugfix, not backported by default labels Mar 1, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

Although it was still uncertain to me, if DateTime64 should be compared with an integer or with floating point...

Merged via the queue into ClickHouse:master with commit 5e5a65e Mar 1, 2026
144 checks passed
@alexey-milovidov
alexey-milovidov deleted the datetime64-key-analysis branch March 1, 2026 18:29
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 1, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @amosbird @alexey-milovidov — while reviewing this PR I found the following:

Happy to discuss — close anything that's wrong or already addressed.

EmeraldShift added a commit to EmeraldShift/ClickHouse that referenced this pull request Aug 9, 2026
Range previously tightened every Int64 or UInt64 endpoint without knowing the key type. Move normalization into KeyCondition, where the key type is available, and apply it only to types whose values are represented as integers.

Related: ClickHouse#113993

Caused by: ClickHouse#98410
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants