Skip to content

Fix silent data loss caused by Date overflow in TTL expressions - #102020

Closed
fastio wants to merge 2 commits into
ClickHouse:masterfrom
fastio:bugfix-avoid-to-delete-data-unexpactedly-when-ttl-expression-overflowed
Closed

fastio wants to merge 2 commits into
ClickHouse:masterfrom
fastio:bugfix-avoid-to-delete-data-unexpactedly-when-ttl-expression-overflowed

Conversation

@fastio

@fastio fastio commented Apr 8, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

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

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

Why this matters: silent data loss

When a user writes TTL day + toIntervalDay(46000), their intent is clear: "keep data for ~126 years." However, because Date is backed by UInt16 (max representable year: 2149), the addition silently wraps around to a past date. ClickHouse then concludes "all data has expired" and silently deletes everything.

The user wanted data to live longer; the system deleted all of it. The outcome is the exact opposite of the intent.

Prevent silent data deletion when TTL expressions overflow Date/DateTime range. Previously, Date + toIntervalDay(large_number) silently wrapped around due to UInt16 arithmetic, corrupting part min/max TTL metadata and causing entire parts to be dropped during merge. The fix detects overflow by evaluating the TTL expression with widened types (Date32/DateTime64) and throws an error if the results differ.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

When a TTL expression such as day + toIntervalDay(46000) overflows the Date (UInt16) range, ClickHouse now throws a VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE error instead of silently corrupting TTL metadata. Users should either reduce the interval or use wider column types (Date32/DateTime64) in their TTL expressions.

@fastio fastio changed the title Guard TTL evaluation against Date/DateTime overflow Critical fix: prevent unexpected data deletion when TTL expression overflows Apr 8, 2026
@fastio

fastio commented Apr 13, 2026

Copy link
Copy Markdown
Contributor Author

Hi @tavplubix, Could I kindly request a review on this PR?

This addresses a silent data loss scenario: when a TTL expression like day + toIntervalDay(46000) overflows the Date (UInt16) range, the result wraps around to a past date, causing ClickHouse to treat all rows as expired and delete them during merge — with no warning at all.

This has already caused real data loss for production users. The user's intent was to retain data longer, but the system silently deleted everything.

The fix is minimal and low-risk: it evaluates the TTL expression a second time using widened types (Date32/DateTime64) and raises an error if the results diverge — stopping the deletion before any rows are removed. Normal (non-overflowing) TTL paths are completely unaffected.

Thanks you for your time!

@fastio fastio changed the title Critical fix: prevent unexpected data deletion when TTL expression overflows Fix silent data loss caused by Date overflow in TTL expressions Apr 13, 2026
@SaltTan

SaltTan commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Does this PR fix #101763?

@fastio fastio closed this Apr 15, 2026
@fastio

fastio commented Apr 15, 2026

Copy link
Copy Markdown
Contributor Author

Does this PR fix #101763?

You're right, it's a duplicate — I'll close this one. Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants