Skip to content

Fix ALTER TABLE MODIFY TTL with DateTime causing data loss on 32-bit overflow - #101793

Merged
alexey-milovidov merged 44 commits into
ClickHouse:masterfrom
BoloniniD:master
Sep 4, 2026
Merged

alexey-milovidov merged 44 commits into
ClickHouse:masterfrom
BoloniniD:master

Conversation

@BoloniniD

@BoloniniD BoloniniD commented Apr 4, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #101763

Changelog category (leave one):

  • Bug Fix

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

Fixes silent data loss for TTL expressions that apply large intervals directly to Date / DateTime source columns by widening the arithmetic away from 16/32-bit overflow.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

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

Version info

  • Merged into: 26.9.1.657 (included in 26.9 and later)
  • Backported to: 26.8.3.61, 26.7.7.79, 26.6.6.8, 26.3.33.77

@BoloniniD BoloniniD changed the title Fix ALTER TABLE MODIFY TTL with DateTime causing data loss on 32-bit overflow WIP Fix ALTER TABLE MODIFY TTL with DateTime causing data loss on 32-bit overflow Apr 4, 2026
@BoloniniD
BoloniniD marked this pull request as ready for review April 5, 2026 10:34
@BoloniniD BoloniniD changed the title WIP Fix ALTER TABLE MODIFY TTL with DateTime causing data loss on 32-bit overflow Fix ALTER TABLE MODIFY TTL with DateTime causing data loss on 32-bit overflow Apr 5, 2026
@zlareb1 zlareb1 added the can be tested Allows running workflows for external contributors label Apr 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9c295bb]

Summary: ✅


AI Review

Summary

This PR fixes the direct Date / DateTime TTL overflow path by rebuilding the main TTL expression against widened Date32 / DateTime64 source types and casting the runtime block to match. That closes a lot of the original surface, but the patch is still not safe to merge as-is: it leaves the pre-upgrade fast paths able to drop data before the widened expression ever runs, and it widens the whole TTL expression DAG rather than only the interval arithmetic that actually needs the larger domain.

Missing context / blind spots
  • ⚠️ Public CI for head 9c295bb377b7 was still running on 2026-08-26 when I reviewed; only Fast test, Style check, Build (arm_tidy), and Build (wasm_parser) had finished. A completed matrix would close that validation gap.
Findings

❌ Blockers

  • [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/101793#discussion_r3651699472] [src/Storages/TTLDescription.cpp:1282] buildExpressionAndSets widens the entire NamesAndTypesList, not just the operands that flow into add* interval arithmetic. That silently changes valid deterministic TTLs whose logic depends on the original static type. A concrete case is TTL if(toTypeName(ts) = 'DateTime', ts + INTERVAL 100 YEAR, ts + INTERVAL 1 DAY): before this patch the condition folds to true, after the widened analysis it folds to false, so rows expire after one day instead of one hundred years. Suggested fix: either rewrite only the temporal operands that feed interval arithmetic, or move checked overflow handling into the Date / DateTime add-interval implementations so the surrounding expression keeps its original types.
  • [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/101793#discussion_r3369599491] [src/Processors/Transforms/TTLTransform.cpp:68-77; src/Processors/Transforms/TTLDeleteFilterTransform.cpp:63-72] The widened DAG only helps after rows are re-evaluated, but both delete fast paths still trust persisted table_ttl.max from the old narrow evaluation and can set all_data_dropped before computing the widened TTL once. A table created before upgrade with TTL ts + INTERVAL 100 YEAR can therefore still lose an entire part on the first post-upgrade merge or filter pass if ttl.txt already contains the wrapped 32-bit max timestamp. Suggested fix: version or invalidate stale row-TTL metadata for widened Date / DateTime expressions, or force a recomputation before the whole-part shortcut, and add an upgrade-style regression that starts from serialized old ttl.txt.
  • [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/101793#discussion_r3610692105] [src/Processors/Transforms/TTLTransform.cpp:118-135; src/Processors/TTL/TTLColumnAlgorithm.cpp:22-29,53-69] The same upgrade hole remains for column TTLs. TTLTransform still seeds TTLColumnAlgorithm from persisted columns_ttl[name], and TTLColumnAlgorithm::execute still takes the isMaxTTLExpired() whole-column shortcut before calling executeExpressionAndGetColumn. So a pre-upgrade part with wrapped columns_ttl[name].max can still have the whole column replaced with defaults on the first post-upgrade merge even though the widened expression would keep the rows alive. Suggested fix: ignore or recompute stale columns_ttl entries for widened temporal TTLs before the whole-column fast path, and cover it with the same upgrade-style regression strategy.
Final Verdict

Not ready. The direct source-column overflow path is much better covered now, but the current patch still changes semantics for valid type-sensitive TTLs and still leaves pre-upgrade table_ttl / columns_ttl fast paths able to drop data before the widened TTL is evaluated.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 0.00% 87.20% +87.20%
Functions 0.00% 91.90% +91.90%
Branches 0.00% 79.30% +79.30%

Full report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 10, 2026
Comment thread src/Storages/TTLDescription.cpp Resolved
Comment thread src/Storages/TTLDescription.cpp Resolved
Comment thread src/Storages/TTLDescription.cpp Resolved
Comment thread src/Storages/TTLDescription.cpp Resolved
Comment thread src/Storages/TTLDescription.cpp Resolved
Comment thread src/Storages/TTLDescription.cpp Resolved
Comment thread src/Storages/TTLDescription.cpp Resolved
Comment thread src/Processors/TTL/ITTLAlgorithm.h Resolved
Comment thread src/Storages/TTLDescription.cpp Resolved
Comment thread src/Storages/TTLDescription.cpp Resolved
Comment thread src/Storages/TTLDescription.cpp Resolved
@BoloniniD

Copy link
Copy Markdown
Contributor Author

Hi @zlareb1, most checks are now passing. Would appreciate a review when you have time.

P.S. Thanks to @fastio (#102020). I incorporated the widening approach from their PR to make this fix more robust.

Comment thread src/Storages/TTLDescription.cpp Resolved

@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.

This is doubtful. A complex logic for a simple change. Maybe we can find a better way?

@alexey-milovidov alexey-milovidov self-assigned this May 1, 2026
@zlareb1

zlareb1 commented Sep 4, 2026

Copy link
Copy Markdown
Member

Two follow-ups on the merged fix.

First, the fix does not protect parts whose ttl.txt was already written with wrapped values by pre-fix versions: the whole-part drop shortcut (all_data_dropped in TTLTransform, and its sibling in TTLDeleteFilterTransform) trusts the stored max and never re-evaluates rows with the fixed expression, so upgrading does not stop the data loss for existing parts — the first TTL merge after the stored (wrapped) deadline drops the whole part even though the fixed expression puts every row's TTL far in the future. Reproduced both ways across a pre-fix and this PR's binary, with MATERIALIZE TTL as a verified heal: #117990.

Second, a scoping issue in the new fallback itself. The widened-first analysis in buildExpressionAndSets falls back to the narrow (wrapping) analysis on any DB::Exception, but the carve-out only needs type-resolution failures (functions that reject DateTime64, like the window-TVF helpers). buildExpression is re-run per insert and per merge, and the analysis allocates under the memory tracker, so a transient exception — MEMORY_LIMIT_EXCEEDED under server-wide pressure is the realistic one, and background merges run exactly there — silently flips that one insert or merge back to wrapped arithmetic: a merge evaluates rows with the pre-fix semantics (the exact loss this PR fixes), an insert writes wrapped values into the new part's ttl.txt (which the first follow-up then turns into a whole-part drop later, making a transient error durable), and two replicas executing the same merge log entry can disagree on the result (checksum mismatch churn). It also converts a genuine LOGICAL_ERROR in the widened analysis into a silent semantics change instead of a loud failure. Scoping the catch to the specific error codes the carve-out needs (ILLEGAL_TYPE_OF_ARGUMENT, NO_COMMON_TYPE, BAD_ARGUMENTS), or deciding widened-vs-narrow once at DDL time and pinning the verdict in TTLDescription so runtime rebuilds can never flip domains, would close this.

@clickgapai

Copy link
Copy Markdown
Contributor

@BoloniniD @alexey-milovidov — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

clickhouse-gh Bot added a commit that referenced this pull request Sep 8, 2026
Backport #101793 to 26.8: Fix ALTER TABLE MODIFY TTL with DateTime causing data loss on 32-bit overflow
PedroTadim added a commit that referenced this pull request Sep 9, 2026
Cherry pick #101793 to 26.3: Fix ALTER TABLE MODIFY TTL with DateTime causing data loss on 32-bit overflow
PedroTadim added a commit that referenced this pull request Sep 9, 2026
Cherry pick #101793 to 26.6: Fix ALTER TABLE MODIFY TTL with DateTime causing data loss on 32-bit overflow
PedroTadim added a commit that referenced this pull request Sep 9, 2026
Cherry pick #101793 to 26.7: Fix ALTER TABLE MODIFY TTL with DateTime causing data loss on 32-bit overflow
robot-clickhouse added a commit that referenced this pull request Sep 9, 2026
robot-clickhouse added a commit that referenced this pull request Sep 9, 2026
robot-clickhouse added a commit that referenced this pull request Sep 9, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 9, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 11, 2026
…ateTime causing data loss on 32-bit overflow
clickhouse-gh Bot added a commit that referenced this pull request Sep 11, 2026
Backport #101793 to 26.7: Fix ALTER TABLE MODIFY TTL with DateTime causing data loss on 32-bit overflow
clickhouse-gh Bot added a commit that referenced this pull request Sep 13, 2026
Backport #101793 to 26.6: Fix ALTER TABLE MODIFY TTL with DateTime causing data loss on 32-bit overflow
PedroTadim added a commit that referenced this pull request Sep 14, 2026
Backport #101793 to 26.3: Fix ALTER TABLE MODIFY TTL with DateTime causing data loss on 32-bit overflow
alexey-milovidov added a commit that referenced this pull request Oct 24, 2026
Resolve the conflict in `src/Processors/TTL/ITTLAlgorithm.cpp` by taking
the 26.3 side: it already reads TTL inputs with
`getColumnOrSubcolumnByName` (adapted in the backport of #101793), and
additionally casts them to the types the expression expects. The code fix
of #105578 is therefore already present on 26.3; this backport only adds
the test `04266_ttl_with_subcolumn_expression`.

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

can be tested Allows running workflows for external contributors pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore 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.

ALTER TABLE MODIFY TTL with DateTime silently causes data loss on 32-bit overflow

8 participants