Skip to content

Backport #101793 to 26.7: Fix ALTER TABLE MODIFY TTL with DateTime causing data loss on 32-bit overflow - #119004

Merged
clickhouse-gh[bot] merged 3 commits into
26.7from
backport/26.7/101793
Sep 11, 2026
Merged

clickhouse-gh[bot] merged 3 commits into
26.7from
backport/26.7/101793

Conversation

@robot-clickhouse-ci-1

@robot-clickhouse-ci-1 robot-clickhouse-ci-1 commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Original pull-request #101793
Cherry-pick pull-request #117987

This pull-request is a last step of an automated backporting.
Treat it as a standard pull-request: look at the checks and resolve conflicts.
Merge it only if you intend to backport changes to the target branch, otherwise just close it.

The PR source

The PR is created in the CI job

Version info

  • Merged into: 26.7.7.79

@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backport Changes, backported to release branch. Do not use manually - automated use only! label Sep 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [BackportPR], commit [96b9965]

Summary: ✅

The backport dropped the master-only `required_source_columns` out-parameter of
`buildExpressionAndSets`, but this call site passed it positionally as `nullptr`,
so it was left with one argument too many.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…xpression

`04077_datetime32_overflow` failed on this backport in case 12 (`MODIFY TTL ts +
INTERVAL 1 DAY DELETE WHERE toTypeName(ts) = 'DateTime'`) with:

    Code: 47. DB::Exception: Missing columns: 'ts' while processing:
    'toTypeName(ts) = 'DateTime'', required columns: 'ts', no source columns.
    (UNKNOWN_IDENTIFIER)

A stored TTL is an AST plus the list of columns it needs, and the pair is rebuilt
on every `INSERT`, every merge and every table load. On `26.7` that list was taken
from the *built* expression via `ExpressionActions::getRequiredColumnsWithTypes`,
so constant folding dropped columns the AST still refers to: `toTypeName(ts)` folds
to a literal, the stored column list comes out empty, and the next rebuild of the
same AST cannot resolve `ts` any more.

`master` and `26.8` do not have this problem because they take the list from the
syntax analysis, which sees the AST before folding. That change landed on `master`
in `f0bf4fce55edf6060e1c31509c75fb97ce0713c5` ("Keep a TTL expression rebuildable
and loadable"), which is not in `26.7`, so the backported test exercised a code path
the backport had no fix for.

Only the `required_source_columns` part of that commit is taken here - the part this
backport depends on. Its unrelated half (pinning `Variant` / `Dynamic` type-mismatch
strictness through `TypeMismatchStrictness`) is deliberately left out of a bug-fix
backport.

Because the timestamp expression is analyzed against widened (`Date32` /
`DateTime64`) columns, the reported list is mapped back to the table's original
narrow types, so it stays a subset of the columns the caller passed and every later
rebuild widens them again from the real table types. Subcolumn names reported by the
analysis (e.g. `j.ts` of a `JSON` column) are kept as reported; `04847_ttl_where_json_subcolumn`
from the original pull request covers that path and is added along with the mechanism
it needs.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 The Stateless tests (amd_asan_ubsan, distributed plan, parallel) red was this backport's own test, not a flake: case 12 of 04077_datetime32_overflow (MODIFY TTL ts + INTERVAL 1 DAY DELETE WHERE toTypeName(ts) = 'DateTime') failed with

Code: 47. DB::Exception: Missing columns: 'ts' while processing: 'toTypeName(ts) = ''DateTime''', required columns: 'ts', no source columns. (UNKNOWN_IDENTIFIER)

Root cause: on 26.7 the stored TTL column list is taken from the built expression (ExpressionActions::getRequiredColumnsWithTypes), so constant folding drops columns the stored AST still refers to — toTypeName(ts) folds to a literal, the list comes out empty, and the next rebuild of that AST cannot resolve ts. master and 26.8 take the list from the syntax analysis instead, which sees the AST before folding; that landed on master in f0bf4fce55edf6060e1c31509c75fb97ce0713c5 ("Keep a TTL expression rebuildable and loadable") and is not in 26.7, so the backported test exercised a path this backport had no fix for.

Pushed 96b9965489c4, which takes only the required_source_columns part of that commit — the part this backport depends on. Its unrelated half (pinning Variant / Dynamic type-mismatch strictness through TypeMismatchStrictness) is deliberately left out of a bug-fix backport. 04847_ttl_where_json_subcolumn from the original #101793 covers the subcolumn path of that mechanism and is added along with it.

The same prerequisite is missing in #119003 (26.6) and #119002 (26.3), which fail the same test for the same reason.

@clickhouse-gh
clickhouse-gh Bot merged commit fcd8af9 into 26.7 Sep 11, 2026
38 of 39 checks passed
@clickhouse-gh
clickhouse-gh Bot deleted the backport/26.7/101793 branch September 11, 2026 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backport Changes, backported to release branch. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants