Repository navigation
Ignore redundant parentheses in stored table definitions - #114188
Conversation
The definition expressions of a table (keys, `TTL`, indices, projections, constraints, column defaults) are stored in ZooKeeper as text and compared as text. #92340 made the AST remember the redundant parentheses the user wrote, so `PARTITION BY (a)` stopped comparing equal to `PARTITION BY a`, and `ATTACH`/`REPLACE`/`MOVE PARTITION FROM` and adding a replica started to fail. Format those definitions with a new `ignore_redundant_parentheses` mode, which gives back the text every server produced before the parentheses were a part of the AST. What the user wrote is still stored in the table metadata and still shown by `SHOW CREATE`; only the comparisons and the Keeper payload became insensitive to it. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [680a97c] Summary: ✅
AI ReviewSummaryThis PR makes stored table-definition comparisons ignore redundant user-written parentheses across MergeTree metadata serialization, partition attach checks, replicated metadata recovery, and Findings
Final Verdict
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 157/167 (94.01%) · Uncovered code |
…es-in-stored-metadata
…ageQueue metadata The `columns` field of the ObjectStorageQueue metadata in ZooKeeper was compared with the local serialization verbatim, so a queue table created by a version that kept the redundant parentheses of the user in a column `DEFAULT`, `CODEC` or `TTL` expression (26.5-26.7) would fail with `METADATA_MISMATCH` on a server that does not store them (and the other way around). When the strings differ, compare the parsed `ColumnsDescription` structurally, like `StorageKeeperMap` already does. Co-Authored-By: Claude Fable 5 <[email protected]>
|
🕵 The only CI failure, |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units1797 translation units recompiled, 13082 s compile time in total, 1796 of them have a recent master baseline. |
…es-in-stored-metadata
… when reusing a partially created replica A retry of a partially created replica could find `/metadata` and `/columns` written by a server version with a different (redundantly parenthesized) spelling of the same table definition; the raw string comparison in `createReplicaAttempt` then failed with `REPLICA_ALREADY_EXISTS`. Fall back to structural comparison (`ReplicatedMergeTreeTableMetadata::checkEquals` / `ColumnsDescription::parse`) when the texts differ. Co-Authored-By: Claude Fable 5 <[email protected]>
…spellings The test hand-crafts the ZooKeeper nodes of a replica as a 26.5..26.7 server would have left them after failing between the ZooKeeper transaction and saving the local metadata: the same definitions, spelled with redundant parentheses. Retrying `CREATE TABLE` must reuse the empty replica instead of throwing `REPLICA_ALREADY_EXISTS`. Co-Authored-By: Claude Fable 5 <[email protected]>
| { | ||
| if (zk_metadata_str == local_metadata) | ||
| return true; | ||
| try |
There was a problem hiding this comment.
This is weird. Let exceptions propagate, do not catch unless we have a really good reason.
There was a problem hiding this comment.
🕵 Removed the try/catch in 97b10b4 — exceptions now propagate. The lambdas are only reached for an empty, never-active replica at our own path (the zk_host.empty() && ... chain short-circuits before them), so a structural mismatch now throws METADATA_MISMATCH from checkEquals and an unparseable node throws its parse error — both describe the problem better than the REPLICA_ALREADY_EXISTS the fall-through create attempt would produce. This also resolved the style-check finding about silent catch (...).
| if (!query) | ||
| return ""; | ||
| return query->formatWithSecretsOneLine(); | ||
| return query->formatIgnoringRedundantParentheses(); |
There was a problem hiding this comment.
For reviewers: this is the key change.
…es-in-stored-metadata
…adata propagate Per review: do not swallow exceptions in `createReplicaAttempt` when comparing the `metadata` and `columns` of an existing empty replica. A structural mismatch or an unparseable node now throws (e.g. `METADATA_MISMATCH`), which describes the problem better than the `REPLICA_ALREADY_EXISTS` of the fall-through create attempt. This also resolves the style-check finding about silent `catch (...)`. Co-Authored-By: Claude Fable 5 <[email protected]>
…04850 test The style check requires shell tests to include it in `ReplicatedMergeTree` paths to avoid overlaps between concurrent test runs. Co-Authored-By: Claude Fable 5 <[email protected]>
…ypick/26.3/114188 Only the part of #114188 that applies to 26.3 is taken. The `parenthesized` flag of `IAST` was introduced by #92340 in 26.5, so 26.3 never writes a table definition with the redundant parentheses the user has written, and the bug the pull request fixes cannot happen here. Everything built on `IAST::FormatSettings::ignore_redundant_parentheses` - `IAST::formatIgnoringRedundantParentheses` and all of its call sites - would be dead code on this branch, and the tests `04836_parenthesized_definitions_attach_partition_from` and `04837_parenthesized_definitions_replicated_metadata` assert that `SHOW CREATE` prints the parentheses back, which is 26.5 and later behavior. Those are left out. What is taken are the two places that have to accept a definition stored by 26.5..26.7, which 26.3 needs in order to read such a table at all: - `ObjectStorageQueueTableMetadata::checkImmutableFieldsEquals` compares the `columns` payload structurally when the stored and the local text differ, covered by the unit test `ObjectStorageQueueTableMetadata.ColumnsComparisonIgnoresRedundantParentheses`. - `StorageReplicatedMergeTree::createReplicaAttempt` recognizes an already-created empty replica by comparing its `/metadata` and `/columns` structurally, covered by `04850_parenthesized_definitions_replica_recovery`. Both fail on 26.3 without this change.
…ypick/25.8/114188 Only the part of #114188 that applies to 25.8 is taken. The `parenthesized` flag of `IAST` was introduced by #92340 in 26.5, so 25.8 never writes a table definition with the redundant parentheses the user has written, and the bug the pull request fixes cannot happen here. Everything built on `IAST::FormatSettings::ignore_redundant_parentheses` - `IAST::formatIgnoringRedundantParentheses` and all of its call sites - would be dead code on this branch, and the tests `04836_parenthesized_definitions_attach_partition_from` and `04837_parenthesized_definitions_replicated_metadata` assert that `SHOW CREATE` prints the parentheses back, which is 26.5 and later behavior. Those are left out. What is taken are the two places that have to accept a definition stored by 26.5..26.7, which 25.8 needs in order to read such a table at all: - `ObjectStorageQueueTableMetadata::checkImmutableFieldsEquals` compares the `columns` payload structurally when the stored and the local text differ, covered by the unit test `ObjectStorageQueueTableMetadata.ColumnsComparisonIgnoresRedundantParentheses`. - `StorageReplicatedMergeTree::createReplicaAttempt` recognizes an already-created empty replica by comparing its `/metadata` and `/columns` structurally, covered by `04850_parenthesized_definitions_replica_recovery`. This branch has no `ReplicatedMergeTreeTableMetadata::parseAndNormalize` (the implicit indices it normalizes appeared in 25.12), so `parse` is used, as everywhere else here. Both fail on 25.8 without this change.
Cherry pick #114188 to 25.8: Ignore redundant parentheses in stored table definitions
Cherry pick #114188 to 26.3: Ignore redundant parentheses in stored table definitions
Cherry pick #114188 to 26.5: Ignore redundant parentheses in stored table definitions
Cherry pick #114188 to 26.6: Ignore redundant parentheses in stored table definitions
Cherry pick #114188 to 26.7: Ignore redundant parentheses in stored table definitions
Backport #114188 to 26.7: Ignore redundant parentheses in stored table definitions
…on-tuple-key-normalization Master got the minimal text-level fix for the same bug in ClickHouse#114188 (`formatIgnoringRedundantParentheses`). Conflicts resolved in favour of this branch, whose AST comparison subsumes the text comparison, with two changes: * `ReplicatedMergeTreeTableMetadata::formatDefinition` and `formatDefinitionList` now produce the Keeper payload with `formatIgnoringRedundantParentheses` instead of walking the tree with `stripArtificialParens` - the new format-time mode does the same, more thoroughly, and without cloning to mutate. * `stripArtificialParens` stays only where the AST itself, not its text, has to be canonical: `Diff::getNewMetadata`, which stores the reparsed key expression in the local metadata.
Backport #114188 to 26.5: Ignore redundant parentheses in stored table definitions
Backport #114188 to 26.6: Ignore redundant parentheses in stored table definitions
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
ATTACH PARTITION FROM,REPLACE PARTITION,MOVE PARTITION TO TABLEand adding aReplicatedMergeTreereplica failing withTables have different ...,METADATA_MISMATCHorINCOMPATIBLE_COLUMNSfor tables whose definitions were written with redundant parentheses, such asPARTITION BY (a),ORDER BY (b),INDEX ix (b * c) TYPE minmax,PROJECTION p (SELECT (b) ...),CONSTRAINT c CHECK (a > 0),TTL (d + INTERVAL 10 YEAR)orDEFAULT (a + 1).Description
This is an alternative to #110833 that contains only the parentheses normalization, so that it is easy to backport. It does not touch
getTreeHashand does not change how definitions are compared - they are still compared as text.The definition expressions of a table (keys,
TTL, indices, projections, constraints, column defaults) are stored in ZooKeeper as text and compared as text. #92340 made the AST remember whether the user wrote redundant parentheses around an expression, so the same definition now has two spellings, and every comparison of a stored definition started to reject one against the other:An upgrade or a mixed-version cluster is not needed: both tables above are created by the same binary. Measured on released binaries, 25.8, 26.3 and 26.4 accept this and 26.5, 26.6 and 26.7 reject it, which matches the branches that contain
b38a892dfbfab6.The fix is a single formatting mode.
IAST::FormatSettings::ignore_redundant_parenthesessuppresses the parentheses of theparenthesizedflag - the one place that emits them,decideParensEmission, is skipped - andIAST::formatIgnoringRedundantParenthesesis the one-line formatter built on it. Because the formatter visits everything it prints, this covers nested parentheses (PARTITION BY (x + (1))) and every kind of definition, not just the top level of a key.It is used where a stored definition is serialized or compared:
ReplicatedMergeTreeTableMetadata::formattedAST/formattedASTNormalized(the ZooKeeper/metadatapayload and the replica-join comparison). This replaces the partialstripArtificialParensworkaround added by Fix inconsistent formatting by remembering if an expression was parenthesized #92340, which only cleared the flag on an expression list and its immediate children, and it no longer has to clone the AST.IndicesDescription::explicitToString/allToString,ProjectionsDescription::toString,ConstraintsDescription::toStringandColumnDescription::writeText- the serializers of theindices,projections,constraintsandcolumnsparts of the replicated metadata.ColumnsDescription::operator==compares two sets of columns through the same serializer.MergeTreeData::checkStructureAndGetMergeTreeData- theATTACH/REPLACE/MOVE PARTITION FROMstructure gate (sorting/partition/primary keys, secondary indices, projections).StorageReplicatedMergeTree::alter- the definitions anALTERwrites back into Keeper, so anALTERnever publishes a parenthesized definition either.AlterCommand::isTTLAlter(whether restating aTTLschedules aMATERIALIZE TTLmutation) and theMODIFY ORDER BYno-op detection inAlterCommands::prepare.The text these produce is exactly what every server produced before #92340, so a new replica and an old one agree in both directions. Nothing else changes: the parser and the formatter are untouched, the table metadata keeps what the user wrote, and
SHOW CREATEstill printsPARTITION BY (x + (1)). Definitions that differ in more than the parentheses are still rejected (avsb, a different index expression, a different column default).The
columnspayload ofStorageKeeperMapandObjectStorageQueuealso contains these expressions, but both storages now compare it structurally, so a table created by any version keeps working.StorageKeeperMapalready parses both sides and re-serializes them with the current serializer before comparing.ObjectStorageQueueTableMetadatacompared the stored string verbatim; it now falls back to comparing the parsedColumnsDescriptionwhen the strings differ, so a queue table created by 26.5-26.7 with redundant parentheses in a columnDEFAULT,CODECorTTLis accepted in both directions, and this also un-breaks such a table created by 25.8 and earlier (which is broken on 26.5-26.7 today).Retrying the creation of a replica that failed between the ZooKeeper transaction and saving the local metadata is also covered:
createReplicaAttemptrecognizes an already-created empty replica by comparing its/metadataand/columns, and now falls back to a structural comparison (ReplicatedMergeTreeTableMetadata::checkEquals, parsedColumnsDescription) when the raw strings differ, so such a retry works across the old and new spellings instead of failing withREPLICA_ALREADY_EXISTS.Tests:
04836_parenthesized_definitions_attach_partition_from,04837_parenthesized_definitions_replicated_metadataand04850_parenthesized_definitions_replica_recovery. Each case in them fails on master with the error it is named after. The unit testObjectStorageQueueTableMetadata.ColumnsComparisonIgnoresRedundantParenthesescovers the stored-JSON compatibility of the queue metadata.Version info
26.8.1.1335(included in26.8and later)26.7.4.31,26.6.3.38,26.5.7.50