From 5b133dd1ce7dcdd457a50219f4f76947933bd698 Mon Sep 17 00:00:00 2001 From: alesapin Date: Thu, 2 Apr 2020 19:11:10 +0300 Subject: [PATCH] Add default columns alter --- .../Interpreters/RenameColumnVisitor.cpp | 0 .../Interpreters/RenameColumnVisitor.h | 0 dbms/Storages/AlterCommands.cpp | 14 +++++ dbms/Storages/MergeTree/MergeTreeData.cpp | 13 ++-- dbms/Storages/MergeTree/MergeTreeData.h | 5 +- dbms/Storages/StorageMergeTree.cpp | 2 +- dbms/Storages/StorageReplicatedMergeTree.cpp | 2 +- ...er_rename_with_default_zookeeper.reference | 17 +++++ ...13_alter_rename_with_default_zookeeper.sql | 63 +++++++++++++++++++ 9 files changed, 107 insertions(+), 9 deletions(-) rename dbms/{src => }/Interpreters/RenameColumnVisitor.cpp (100%) rename dbms/{src => }/Interpreters/RenameColumnVisitor.h (100%) create mode 100644 tests/queries/0_stateless/01213_alter_rename_with_default_zookeeper.reference create mode 100644 tests/queries/0_stateless/01213_alter_rename_with_default_zookeeper.sql diff --git a/dbms/src/Interpreters/RenameColumnVisitor.cpp b/dbms/Interpreters/RenameColumnVisitor.cpp similarity index 100% rename from dbms/src/Interpreters/RenameColumnVisitor.cpp rename to dbms/Interpreters/RenameColumnVisitor.cpp diff --git a/dbms/src/Interpreters/RenameColumnVisitor.h b/dbms/Interpreters/RenameColumnVisitor.h similarity index 100% rename from dbms/src/Interpreters/RenameColumnVisitor.h rename to dbms/Interpreters/RenameColumnVisitor.h diff --git a/dbms/Storages/AlterCommands.cpp b/dbms/Storages/AlterCommands.cpp index 1b135563095..16585190701 100644 --- a/dbms/Storages/AlterCommands.cpp +++ b/dbms/Storages/AlterCommands.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -451,6 +452,19 @@ void AlterCommand::apply(StorageInMemoryMetadata & metadata) const else if (type == RENAME_COLUMN) { metadata.columns.rename(column_name, rename_to); + RenameColumnData rename_data{column_name, rename_to}; + RenameColumnVisitor rename_visitor(rename_data); + for (auto & column : metadata.columns) + { + metadata.columns.modify(column.name, [&](ColumnDescription & column_to_modify) { + if (column_to_modify.default_desc.expression) + rename_visitor.visit(column_to_modify.default_desc.expression); + if (column_to_modify.ttl) + rename_visitor.visit(column_to_modify.ttl); + }); + } + if (metadata.ttl_for_table_ast) + rename_visitor.visit(metadata.ttl_for_table_ast); } else throw Exception("Wrong parameter type in ALTER query", ErrorCodes::LOGICAL_ERROR); diff --git a/dbms/Storages/MergeTree/MergeTreeData.cpp b/dbms/Storages/MergeTree/MergeTreeData.cpp index ab99c955701..bf0eedc4264 100644 --- a/dbms/Storages/MergeTree/MergeTreeData.cpp +++ b/dbms/Storages/MergeTree/MergeTreeData.cpp @@ -192,7 +192,7 @@ MergeTreeData::MergeTreeData( min_format_version = MERGE_TREE_DATA_MIN_FORMAT_VERSION_WITH_CUSTOM_PARTITIONING; } - setTTLExpressions(metadata.columns.getColumnTTLs(), metadata.ttl_for_table_ast); + setTTLExpressions(metadata.columns, metadata.ttl_for_table_ast); /// format_file always contained on any data path PathWithDisk version_file; @@ -610,14 +610,17 @@ void checkTTLExpression(const ExpressionActionsPtr & ttl_expression, const Strin } -void MergeTreeData::setTTLExpressions(const ColumnsDescription::ColumnTTLs & new_column_ttls, +void MergeTreeData::setTTLExpressions(const ColumnsDescription & columns, const ASTPtr & new_ttl_table_ast, bool only_check) { - auto create_ttl_entry = [this](ASTPtr ttl_ast) + + auto new_column_ttls = columns.getColumnTTLs(); + + auto create_ttl_entry = [this, &columns](ASTPtr ttl_ast) { TTLEntry result; - auto syntax_result = SyntaxAnalyzer(global_context).analyze(ttl_ast, getColumns().getAllPhysical()); + auto syntax_result = SyntaxAnalyzer(global_context).analyze(ttl_ast, columns.getAllPhysical()); result.expression = ExpressionAnalyzer(ttl_ast, syntax_result, global_context).getActions(false); result.destination_type = PartDestinationType::DELETE; result.result_column = ttl_ast->getColumnName(); @@ -1500,7 +1503,7 @@ void MergeTreeData::checkAlterIsPossible(const AlterCommands & commands, const S setProperties(metadata, /* only_check = */ true); - setTTLExpressions(metadata.columns.getColumnTTLs(), metadata.ttl_for_table_ast, /* only_check = */ true); + setTTLExpressions(metadata.columns, metadata.ttl_for_table_ast, /* only_check = */ true); if (settings_ast) { diff --git a/dbms/Storages/MergeTree/MergeTreeData.h b/dbms/Storages/MergeTree/MergeTreeData.h index 5a9a8a61376..120ebd2e467 100644 --- a/dbms/Storages/MergeTree/MergeTreeData.h +++ b/dbms/Storages/MergeTree/MergeTreeData.h @@ -868,8 +868,9 @@ protected: void initPartitionKey(); - void setTTLExpressions(const ColumnsDescription::ColumnTTLs & new_column_ttls, - const ASTPtr & new_ttl_table_ast, bool only_check = false); + void setTTLExpressions(const ColumnsDescription & columns, + const ASTPtr & new_ttl_table_ast, bool only_check = false); + void checkStoragePolicy(const StoragePolicyPtr & new_storage_policy); void setStoragePolicy(const String & new_storage_policy_name, bool only_check = false); diff --git a/dbms/Storages/StorageMergeTree.cpp b/dbms/Storages/StorageMergeTree.cpp index 059f5c671e9..a0ca6964cbf 100644 --- a/dbms/Storages/StorageMergeTree.cpp +++ b/dbms/Storages/StorageMergeTree.cpp @@ -237,7 +237,7 @@ void StorageMergeTree::alter( /// Reinitialize primary key because primary key column types might have changed. setProperties(metadata); - setTTLExpressions(metadata.columns.getColumnTTLs(), metadata.ttl_for_table_ast); + setTTLExpressions(metadata.columns, metadata.ttl_for_table_ast); DatabaseCatalog::instance().getDatabase(table_id.database_name)->alterTable(context, table_id.table_name, metadata); diff --git a/dbms/Storages/StorageReplicatedMergeTree.cpp b/dbms/Storages/StorageReplicatedMergeTree.cpp index c739070ef54..0d8755ed99a 100644 --- a/dbms/Storages/StorageReplicatedMergeTree.cpp +++ b/dbms/Storages/StorageReplicatedMergeTree.cpp @@ -496,7 +496,7 @@ void StorageReplicatedMergeTree::setTableStructure(ColumnsDescription new_column /// Even if the primary/sorting keys didn't change we must reinitialize it /// because primary key column types might have changed. setProperties(metadata); - setTTLExpressions(new_columns.getColumnTTLs(), metadata.ttl_for_table_ast); + setTTLExpressions(new_columns, metadata.ttl_for_table_ast); } diff --git a/tests/queries/0_stateless/01213_alter_rename_with_default_zookeeper.reference b/tests/queries/0_stateless/01213_alter_rename_with_default_zookeeper.reference new file mode 100644 index 00000000000..06f136d6dbc --- /dev/null +++ b/tests/queries/0_stateless/01213_alter_rename_with_default_zookeeper.reference @@ -0,0 +1,17 @@ +date key value1 value2 +2019-10-02 1 1 Hello 1 +CREATE TABLE default.table_rename_with_default (`date` Date, `key` UInt64, `value1` String, `value2` String DEFAULT concat(\'Hello \', value1), `value3` String ALIAS concat(\'Word \', value1)) ENGINE = MergeTree() PARTITION BY date ORDER BY key SETTINGS index_granularity = 8192 +date key renamed_value1 value2 +2019-10-02 1 1 Hello 1 +CREATE TABLE default.table_rename_with_default (`date` Date, `key` UInt64, `renamed_value1` String, `value2` String DEFAULT concat(\'Hello \', renamed_value1), `value3` String ALIAS concat(\'Word \', renamed_value1)) ENGINE = MergeTree() PARTITION BY date ORDER BY key SETTINGS index_granularity = 8192 +Hello 1 +Word 1 +date1 date2 value1 value2 +2019-10-02 2018-10-02 1 1 +CREATE TABLE default.table_rename_with_ttl (`date1` Date, `date2` Date, `value1` String, `value2` String TTL date1 + toIntervalMonth(10000)) ENGINE = ReplicatedMergeTree(\'/clickhouse/test/table_rename_with_ttl\', \'1\') ORDER BY tuple() TTL date2 + toIntervalMonth(10000) SETTINGS index_granularity = 8192 +renamed_date1 date2 value1 value2 +2019-10-02 2018-10-02 1 1 +CREATE TABLE default.table_rename_with_ttl (`renamed_date1` Date, `date2` Date, `value1` String, `value2` String TTL renamed_date1 + toIntervalMonth(10000)) ENGINE = ReplicatedMergeTree(\'/clickhouse/test/table_rename_with_ttl\', \'1\') ORDER BY tuple() TTL date2 + toIntervalMonth(10000) SETTINGS index_granularity = 8192 +renamed_date1 renamed_date2 value1 value2 +2019-10-02 2018-10-02 1 1 +CREATE TABLE default.table_rename_with_ttl (`renamed_date1` Date, `renamed_date2` Date, `value1` String, `value2` String TTL renamed_date1 + toIntervalMonth(10000)) ENGINE = ReplicatedMergeTree(\'/clickhouse/test/table_rename_with_ttl\', \'1\') ORDER BY tuple() TTL renamed_date2 + toIntervalMonth(10000) SETTINGS index_granularity = 8192 diff --git a/tests/queries/0_stateless/01213_alter_rename_with_default_zookeeper.sql b/tests/queries/0_stateless/01213_alter_rename_with_default_zookeeper.sql new file mode 100644 index 00000000000..fa4c02aa58c --- /dev/null +++ b/tests/queries/0_stateless/01213_alter_rename_with_default_zookeeper.sql @@ -0,0 +1,63 @@ +DROP TABLE IF EXISTS table_rename_with_default; + +CREATE TABLE table_rename_with_default +( + date Date, + key UInt64, + value1 String, + value2 String DEFAULT concat('Hello ', value1), + value3 String ALIAS concat('Word ', value1) +) +ENGINE = MergeTree() +PARTITION BY date +ORDER BY key; + +INSERT INTO table_rename_with_default (date, key, value1) SELECT toDate('2019-10-01') + number % 3, number, toString(number) from numbers(9); + +SELECT * FROM table_rename_with_default WHERE key = 1 FORMAT TSVWithNames; + +SHOW CREATE TABLE table_rename_with_default; + +ALTER TABLE table_rename_with_default RENAME COLUMN value1 TO renamed_value1; + +SELECT * FROM table_rename_with_default WHERE key = 1 FORMAT TSVWithNames; + +SHOW CREATE TABLE table_rename_with_default; + +SELECT value2 FROM table_rename_with_default WHERE key = 1; +SELECT value3 FROM table_rename_with_default WHERE key = 1; + +DROP TABLE IF EXISTS table_rename_with_default; + +DROP TABLE IF EXISTS table_rename_with_ttl; + +CREATE TABLE table_rename_with_ttl +( + date1 Date, + date2 Date, + value1 String, + value2 String TTL date1 + INTERVAL 10000 MONTH +) +ENGINE = ReplicatedMergeTree('/clickhouse/test/table_rename_with_ttl', '1') +ORDER BY tuple() +TTL date2 + INTERVAL 10000 MONTH; + +INSERT INTO table_rename_with_ttl SELECT toDate('2019-10-01') + number % 3, toDate('2018-10-01') + number % 3, toString(number), toString(number) from numbers(9); + +SELECT * FROM table_rename_with_ttl WHERE value1 = '1' FORMAT TSVWithNames; + +SHOW CREATE TABLE table_rename_with_ttl; + +ALTER TABLE table_rename_with_ttl RENAME COLUMN date1 TO renamed_date1; + +SELECT * FROM table_rename_with_ttl WHERE value1 = '1' FORMAT TSVWithNames; + +SHOW CREATE TABLE table_rename_with_ttl; + +ALTER TABLE table_rename_with_ttl RENAME COLUMN date2 TO renamed_date2; + +SELECT * FROM table_rename_with_ttl WHERE value1 = '1' FORMAT TSVWithNames; + +SHOW CREATE TABLE table_rename_with_ttl; + +DROP TABLE IF EXISTS table_rename_with_ttl;