Skip to content

Fix off-by-one in ToDateMonotonicity boundary check - #98276

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-todate-monotonicity-boundary
Mar 1, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix-todate-monotonicity-boundary

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Feb 27, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC) or LOGICAL_ERROR

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

Fixed LOGICAL_ERROR exception "Invalid binary search result in MergeTreeSetIndex" triggered by toDate conversion on key columns with data crossing the 65535 boundary.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

No documentation needed — this is a bug fix with no user-facing behavior change.


Fixed off-by-one error in ToDateMonotonicity::get that caused LOGICAL_ERROR: Invalid binary search result in MergeTreeSetIndex in debug builds. The toDate conversion boundary at DATE_LUT_MAX_DAY_NUM (65535) was checked with < 0xFFFF instead of <= DATE_LUT_MAX_DAY_NUM, missing the range [65535, 65536] where the function is non-monotonic (jumps from day 65535 to ~day 0). The AST fuzzer found this via a Merge engine table with x Date reading from a MergeTree with x UInt64 ORDER BY x, where index_granularity=1 creates a mark range [65535, 65536] crossing the boundary.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=98138&sha=f6eac0762f9799ea48b8e800ef4d22b08c29b699&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29
#98138

Version info

  • Merged into: 26.3.1.182
  • Backported to: 26.2.2.7, 26.1.4.36, 25.12.9.30, 25.8.17.37, 25.3.15.16

The `toDate` conversion treats values <= `DATE_LUT_MAX_DAY_NUM` (65535)
as day numbers and values > 65535 as unix timestamps. This creates a
discontinuity at the boundary: `toDate(65535)` = day 65535 but
`toDate(65536)` ≈ day 0.

The monotonicity check used `left < 0xFFFF` to detect non-monotonic
ranges, but this missed the range [65535, 65536] since `65535 < 65535`
is false. This caused `MergeTreeSetIndex::checkInRange` to receive an
inverted range, triggering LOGICAL_ERROR "Invalid binary search result
in MergeTreeSetIndex" in debug builds.

Fix the boundary check to use `<= DATE_LUT_MAX_DAY_NUM` and
`> DATE_LUT_MAX_DAY_NUM`, matching the actual conversion logic.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=98138&sha=f6eac0762f9799ea48b8e800ef4d22b08c29b699&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Feb 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [07a79dd]

Summary: ✅

-- Previously caused LOGICAL_ERROR "Invalid binary search result in MergeTreeSetIndex" in debug builds.
-- https://github.com/ClickHouse/ClickHouse/issues/90461

DROP TABLE IF EXISTS t_todate_mono;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The test is good.

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Feb 27, 2026
@alexey-milovidov alexey-milovidov self-assigned this Mar 1, 2026
@alexey-milovidov
alexey-milovidov merged commit 5931a95 into master Mar 1, 2026
287 of 288 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-todate-monotonicity-boundary branch March 1, 2026 02:50
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 1, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Mar 1, 2026
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Mar 1, 2026
clickhouse-gh Bot added a commit that referenced this pull request Mar 1, 2026
Backport #98276 to 26.1: Fix off-by-one in ToDateMonotonicity boundary check
clickhouse-gh Bot added a commit that referenced this pull request Mar 1, 2026
Backport #98276 to 26.2: Fix off-by-one in ToDateMonotonicity boundary check
clickhouse-gh Bot added a commit that referenced this pull request Mar 1, 2026
Backport #98276 to 25.8: Fix off-by-one in ToDateMonotonicity boundary check
alexey-milovidov added a commit that referenced this pull request Mar 1, 2026
…ndary

Fix off-by-one in ToDateMonotonicity boundary check
alexey-milovidov added a commit that referenced this pull request Mar 17, 2026
Backport #98276 to 25.3: Fix off-by-one in ToDateMonotonicity boundary check
alexey-milovidov added a commit that referenced this pull request Mar 17, 2026
Backport #98276 to 25.12: Fix off-by-one in ToDateMonotonicity boundary check
@clickgapai

Copy link
Copy Markdown
Contributor

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

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

@clickgapai

Copy link
Copy Markdown
Contributor

Apologies for the duplicate notification — we had a race condition in our notification system where multiple processes posted simultaneously. The duplicate has been removed and we've added a strict 3-layer dedup guard (DB lock + immediate key + GitHub comment check) to ensure this cannot happen again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants