From 6732e5c217063228d4a041e27e96a44ca7e41fe6 Mon Sep 17 00:00:00 2001 From: alesapin Date: Mon, 12 Oct 2020 14:02:35 +0300 Subject: [PATCH] Fix alter default validation --- src/Storages/AlterCommands.cpp | 26 ++++++++----------- .../01522_validate_alter_default.reference | 0 .../01522_validate_alter_default.sql | 16 ++++++++++++ 3 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 tests/queries/0_stateless/01522_validate_alter_default.reference create mode 100644 tests/queries/0_stateless/01522_validate_alter_default.sql diff --git a/src/Storages/AlterCommands.cpp b/src/Storages/AlterCommands.cpp index 3b062c2d689..7beb0a4d706 100644 --- a/src/Storages/AlterCommands.cpp +++ b/src/Storages/AlterCommands.cpp @@ -1109,24 +1109,21 @@ void AlterCommands::validate(const StorageInMemoryMetadata & metadata, const Con { if (command.default_expression) { - /// If we modify default, but not type - if (!command.data_type) - { - default_expr_list->children.emplace_back(setAlias(command.default_expression->clone(), column_name)); - } + DataTypePtr data_type_ptr; + /// If we modify default, but not type. + if (!command.data_type) /// it's not ADD COLUMN, because we cannot add column without type + data_type_ptr = all_columns.get(column_name).type; else - { - const auto & final_column_name = column_name; - const auto tmp_column_name = final_column_name + "_tmp"; - const auto data_type_ptr = command.data_type; + data_type_ptr = command.data_type; + const auto & final_column_name = column_name; + const auto tmp_column_name = final_column_name + "_tmp"; - default_expr_list->children.emplace_back(setAlias( - addTypeConversionToAST(std::make_shared(tmp_column_name), data_type_ptr->getName()), - final_column_name)); + default_expr_list->children.emplace_back(setAlias( + addTypeConversionToAST(std::make_shared(tmp_column_name), data_type_ptr->getName()), + final_column_name)); - default_expr_list->children.emplace_back(setAlias(command.default_expression->clone(), tmp_column_name)); - } + default_expr_list->children.emplace_back(setAlias(command.default_expression->clone(), tmp_column_name)); } /// if we change data type for column with default else if (all_columns.has(column_name) && command.data_type) { @@ -1139,7 +1136,6 @@ void AlterCommands::validate(const StorageInMemoryMetadata & metadata, const Con const auto tmp_column_name = final_column_name + "_tmp"; const auto data_type_ptr = command.data_type; - default_expr_list->children.emplace_back(setAlias( addTypeConversionToAST(std::make_shared(tmp_column_name), data_type_ptr->getName()), final_column_name)); diff --git a/tests/queries/0_stateless/01522_validate_alter_default.reference b/tests/queries/0_stateless/01522_validate_alter_default.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/01522_validate_alter_default.sql b/tests/queries/0_stateless/01522_validate_alter_default.sql new file mode 100644 index 00000000000..dbddffe369e --- /dev/null +++ b/tests/queries/0_stateless/01522_validate_alter_default.sql @@ -0,0 +1,16 @@ +DROP TABLE IF EXISTS table2; +CREATE TABLE table2 +( + EventDate Date, + Id Int32, + Value Int32 +) +Engine = MergeTree() +PARTITION BY toYYYYMM(EventDate) +ORDER BY Id; + +ALTER TABLE table2 MODIFY COLUMN `Value` DEFAULT 'some_string'; --{serverError 6} + +ALTER TABLE table2 ADD COLUMN `Value2` DEFAULT 'some_string'; --{serverError 36} + +DROP TABLE IF EXISTS table2;