Repository navigation
Conversation
#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]>
|
Workflow [PR], commit [0bc3a60] Summary: ✅
AI ReviewSummaryThis 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 PR Metadata
Findings
Tests
Final Verdict
|
…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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
This seems legitimate, worth adding a test at least
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 89/103 (86.41%) · Uncovered code |
…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]>
|
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. |
|
Double parens in some cases aren't stripped, stuff like: Here's a specific test that fails (but it is a little pathological): 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
left a comment
There was a problem hiding this comment.
It looks good to me if you can test/fix the issue with the WITH statements.
|
Closed in favor to #110833 |
Changelog category (leave one):
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
CREATEandALTERqueries. Now for "replicated" queries old parenthesis behvior is preserved.