From ba78a737dc6f96eefde9abab128914b532071c80 Mon Sep 17 00:00:00 2001 From: alesapin Date: Thu, 2 Sep 2021 17:47:00 +0300 Subject: [PATCH] Fix detach/attach for ReplicatedVersionedCollapsingMergeTree after alter --- .../MergeTree/ReplicatedMergeTreeTableMetadata.cpp | 10 +++++++++- .../01526_alter_add_and_modify_order_zookeeper.sql | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Storages/MergeTree/ReplicatedMergeTreeTableMetadata.cpp b/src/Storages/MergeTree/ReplicatedMergeTreeTableMetadata.cpp index db1c2bc89af..be8cf26dee6 100644 --- a/src/Storages/MergeTree/ReplicatedMergeTreeTableMetadata.cpp +++ b/src/Storages/MergeTree/ReplicatedMergeTreeTableMetadata.cpp @@ -46,7 +46,15 @@ ReplicatedMergeTreeTableMetadata::ReplicatedMergeTreeTableMetadata(const MergeTr primary_key = formattedAST(metadata_snapshot->getPrimaryKey().expression_list_ast); if (metadata_snapshot->isPrimaryKeyDefined()) - sorting_key = formattedAST(metadata_snapshot->getSortingKey().expression_list_ast); + { + /// We don't use preparsed AST `sorting_key.expression_list_ast` because + /// it contain version column for VersionedCollapsingMergeTree, which + /// shouldn't be defined as a part of key definition AST. So the best + /// compatible way is just to convert definition_ast to list and + /// serialize it. In all other places key.expression_list_ast should be + /// used. + sorting_key = formattedAST(extractKeyExpressionList(metadata_snapshot->getSortingKey().definition_ast)); + } data_format_version = data.format_version; diff --git a/tests/queries/0_stateless/01526_alter_add_and_modify_order_zookeeper.sql b/tests/queries/0_stateless/01526_alter_add_and_modify_order_zookeeper.sql index b718ba199c1..aaf003c53f4 100644 --- a/tests/queries/0_stateless/01526_alter_add_and_modify_order_zookeeper.sql +++ b/tests/queries/0_stateless/01526_alter_add_and_modify_order_zookeeper.sql @@ -19,10 +19,20 @@ SETTINGS index_granularity = 8192; INSERT INTO table_for_alter VALUES(toDate('2019-10-01'), 'a', 1, 'aa', 1, 1, 1); +DETACH TABLE table_for_alter; + +ATTACH TABLE table_for_alter; + + SELECT * FROM table_for_alter; ALTER TABLE table_for_alter ADD COLUMN order UInt32, MODIFY ORDER BY (d, order); + +DETACH TABLE table_for_alter; + +ATTACH TABLE table_for_alter; + SELECT * FROM table_for_alter; SHOW CREATE TABLE table_for_alter; @@ -35,4 +45,8 @@ SELECT * FROM table_for_alter ORDER BY d; SHOW CREATE TABLE table_for_alter; +DETACH TABLE table_for_alter; + +ATTACH TABLE table_for_alter; + DROP TABLE IF EXISTS table_for_alter;