Skip to content

Fix parenthesized storage keys backward compat - #110840

Closed
alesapin wants to merge 5 commits into
masterfrom
fix-parenthesized-storage-keys-backward-compat
Closed

alesapin wants to merge 5 commits into
masterfrom
fix-parenthesized-storage-keys-backward-compat

Conversation

@alesapin

@alesapin alesapin commented Jul 17, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix bug introduces in #92340 by partially reverting logic of the PR for CREATE and ALTER queries. Now for "replicated" queries old parenthesis behvior is preserved.

alesapin and others added 2 commits July 17, 2026 13:01
#92340 started to preserve
user-written parentheses in all expressions, including DDL definition
clauses: `PARTITION BY (a)` was formatted as `(a)` instead of the
canonical `a`, and the same happened to `ORDER BY`, `PRIMARY KEY`,
`SAMPLE BY`, `UNIQUE KEY`, table and column `TTL`, column
`DEFAULT`/`MATERIALIZED`, `INDEX`, `CONSTRAINT` and projection
definitions.

Stored table metadata (the `.sql` files on disk, the ZooKeeper metadata,
`SHOW CREATE` and `system.tables`) and its comparisons rely on the
canonical form, so preserving the parentheses broke compatibility with
tables created by older versions: `ATTACH PARTITION FROM`,
`CREATE ... CLONE AS` and replica metadata checks failed with exceptions
like "Tables have different partition key" (BAD_ARGUMENTS) for keys that
are actually the same.

Drop the top-level `parenthesized` flag at parse time in DDL definition
positions, restoring the exact formatting of version 26.4. Aliased
expressions (e.g. `PRIMARY KEY (c0 AS a)`) keep the parentheses: they
are required for a consistent round trip and were formatted with
parentheses by older versions as well. All ZooKeeper metadata
comparisons reparse the stored strings, so tables created by the
affected versions heal on upgrade.

Co-Authored-By: Claude Fable 5 <[email protected]>
The test runs a real 26.4 server (`clickhouse/clickhouse-server:26.4` image):
a replica on the current version joins a replicated table created by 26.4
with parenthesized key clauses and column definitions (and the reverse),
and a node upgraded from 26.4 loads the old table, keeps the canonical
`SHOW CREATE` form, and accepts `ATTACH PARTITION FROM` between tables
created before and after the upgrade.

Related: #92340

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0bc3a60]

Summary: ✅


AI Review

Summary

This PR restores canonical formatting for parenthesized MergeTree definition expressions so metadata comparisons stop treating equivalent definitions as different ones. The remaining gap is projection definitions: aliased projection SELECT items and the projection WITH list still preserve redundant parentheses, so mixed-version replicas and ATTACH PARTITION FROM can still disagree on semantically identical projections.

PR Metadata
  • Changelog category: Bug Fix matches the change.
  • Changelog entry: required, but the current text is developer-facing, has typos, and does not clearly describe the user-visible failure mode.
  • Suggested replacement: Fixes backward compatibility for MergeTree table definitions that use redundant parentheses, so mixed-version replicas and partition attach operations do not fail on equivalent metadata.
Findings

⚠️ Majors

  • [src/Parsers/ParserProjectionSelectQuery.cpp:58] ProjectionsDescription::toString() and MergeTreeData::checkStructureAndGetMergeTreeData still compare the full formatted projection definition_ast, but the new canonicalization skips aliased projection SELECT items and never touches the projection WITH list. Definitions such as PROJECTION p (WITH (a + 1) AS x SELECT (b + 1) AS y ORDER BY y) can therefore still stringify differently from pre-#92340 metadata and keep failing with METADATA_MISMATCH / Tables have different projections.
    Suggested fix: canonicalize with_expression_list too, and strip top-level parentheses from projection SELECT items at the projection clause boundary even when they have an alias.
Tests
  • ⚠️ Add a focused regression that exercises projection metadata comparison for WITH (expr) AS alias and SELECT (expr) AS alias, ideally through mixed-version ReplicatedMergeTree creation or ATTACH PARTITION FROM. The current tests cover only unaliased projection expressions.
Final Verdict
  • Status: ⚠️ Request changes

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 17, 2026
…rmatting

`UNIQUE KEY (id)` is now formatted as `UNIQUE KEY id`, the same
canonical form as the other key clauses in the storage definition.

Co-Authored-By: Claude Fable 5 <[email protected]>
Address review: the `GROUP BY` branch of `ParserTTLElement` left the group
by keys and the right-hand sides of `SET` assignments with the
`parenthesized` flag, so definitions like
`TTL d GROUP BY (id) SET v = (max(v))` still formatted with the
parentheses and could mismatch metadata of replicas on older versions.

#110840 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
… keys

Address review: projection definitions are compared as formatted strings
in ZooKeeper metadata and in `ATTACH PARTITION FROM`, and version 26.4
canonicalizes them (`PROJECTION p (SELECT (a), (b + 1))` is stored as
`SELECT a, b + 1` and `PROJECTION pi INDEX (a) TYPE basic` as
`INDEX a TYPE basic`), so parentheses preserved in these positions could
still cause `METADATA_MISMATCH` against older replicas. `WHERE` in
projections does not exist in 26.4, but is canonicalized the same way
for consistency.

The cross-version integration test now includes projections with
parenthesized `SELECT` elements, `ORDER BY` key and projection `INDEX`
in the replicated table definition.

#110840 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
/// are redundant and are dropped to keep the canonical form that stored table metadata
/// relies on. See `stripParenthesesUnlessAliased`.
for (const auto & element : select_expression_list->children)
stripParenthesesUnlessAliased(element);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ProjectionsDescription::toString() and MergeTreeData::checkStructureAndGetMergeTreeData still compare the whole formatted definition_ast, so projection metadata has to canonicalize every query-level carrier, not just unaliased expressions. Here we still keep parentheses on aliased SELECT items because stripParenthesesUnlessAliased bails out, and the optional WITH list above is not canonicalized at all.

That leaves the original compatibility problem for definitions like PROJECTION p (WITH (a + 1) AS x SELECT (b + 1) AS y ORDER BY y): pre-#92340 metadata was stored as WITH a + 1 AS x SELECT b + 1 AS y ..., while the new side still formats (a + 1) AS x / (b + 1) AS y. Mixed-version replicas and ATTACH PARTITION FROM can therefore still fail with METADATA_MISMATCH / Tables have different projections.

Can we canonicalize with_expression_list too, and strip top-level parens from projection SELECT items at this clause boundary even when they have an alias?

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 seems legitimate, worth adding a test at least

@clickhouse-gh

clickhouse-gh Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.00% 86.00% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 78.10% 78.10% +0.00%

Changed lines: Changed C/C++ lines covered: 89/103 (86.41%) · Uncovered code

Full report · Diff report

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jul 20, 2026
…element key form

Subtest 3 (table-level PRIMARY KEY (a), single element) now emits SHOW CREATE
ORDER BY a without parentheses after the storage-key canonicalization in this
PR. Update its grep from ORDER BY (a) to ORDER BY a, matching ClickHouse#110840.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@george-larionov george-larionov self-assigned this Jul 24, 2026
@george-larionov

george-larionov commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

Question: What about a DB that created new tables in 26.5/6 (after the bug was introduced), will this change cause a new back-incompatibility with those DDLs? Probably worth adding a test from 26.5/6 in addition to 26.4.

Nevermind, I now understand that both the old and new DDL would be re-parsed by the new code before comparison. Maybe still worth adding the test.

@george-larionov

george-larionov commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

Double parens in some cases aren't stripped, stuff like:

  ORDER BY ((a) + b)          → ORDER BY (a) + b
  PARTITION BY ((a) + b)      → PARTITION BY (a) + b
  INDEX ix ((a) * b)          → INDEX ix (a) * b
  CHECK ((a) > 0)             → CHECK (a) > 0
  DEFAULT ((a) + 1)           → DEFAULT (a) + 1
  TTL ((d) + INTERVAL 1 DAY)  → TTL (d) + toIntervalDay(1)

Here's a specific test that fails (but it is a little pathological):

  CREATE TABLE src_in (a UInt32, b UInt32) ENGINE = MergeTree PARTITION BY ((a) + b) ORDER BY a;
  CREATE TABLE dst_in (a UInt32, b UInt32) ENGINE = MergeTree PARTITION BY (a + b)   ORDER BY a;
  ALTER TABLE dst_in ATTACH PARTITION 2 FROM src_in;

Received exception from server (version 26.7.1):
Code: 36. DB::Exception: Received from localhost:9000. DB::Exception: Tables have different partition key. (BAD_ARGUMENTS)

I'm not sure if it is worth fixing here, I doubt people are writing queries like this, but I thought I would mention it.

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

It looks good to me if you can test/fix the issue with the WITH statements.

@alesapin

Copy link
Copy Markdown
Member Author

Closed in favor to #110833

@alesapin alesapin closed this Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default v26.6-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants