From 45b8cb5b37cfedf2bcb8b9c44c3ebfbb72622ba7 Mon Sep 17 00:00:00 2001 From: alesapin Date: Wed, 22 Apr 2020 17:29:38 +0300 Subject: [PATCH] Remove casts from table creation by default --- src/DataStreams/TTLBlockInputStream.cpp | 7 +++-- src/DataStreams/TTLBlockInputStream.h | 1 + src/Interpreters/InterpreterCreateQuery.cpp | 6 ----- .../00700_decimal_defaults.reference | 6 ++--- ...1135_default_and_alter_zookeeper.reference | 2 ++ .../01135_default_and_alter_zookeeper.sql | 27 +++++++++++++++++++ 6 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 tests/queries/0_stateless/01135_default_and_alter_zookeeper.reference create mode 100644 tests/queries/0_stateless/01135_default_and_alter_zookeeper.sql diff --git a/src/DataStreams/TTLBlockInputStream.cpp b/src/DataStreams/TTLBlockInputStream.cpp index 28e650dd969..06ba0f5981b 100644 --- a/src/DataStreams/TTLBlockInputStream.cpp +++ b/src/DataStreams/TTLBlockInputStream.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace DB { @@ -31,7 +32,8 @@ TTLBlockInputStream::TTLBlockInputStream( children.push_back(input_); header = children.at(0)->getHeader(); - const auto & column_defaults = storage.getColumns().getDefaults(); + const auto & storage_columns = storage.getColumns(); + const auto & column_defaults = storage_columns.getDefaults(); ASTPtr default_expr_list = std::make_shared(); for (const auto & [name, ttl_info] : old_ttl_infos.columns_ttl) { @@ -44,8 +46,9 @@ TTLBlockInputStream::TTLBlockInputStream( if (it != column_defaults.end()) { + auto column = storage_columns.get(name); auto expression = it->second.expression->clone(); - default_expr_list->children.emplace_back(setAlias(expression, it->first)); + default_expr_list->children.emplace_back(setAlias(addTypeConversionToAST(std::move(expression), column.type->getName()), it->first)); } } else diff --git a/src/DataStreams/TTLBlockInputStream.h b/src/DataStreams/TTLBlockInputStream.h index b2dd4ce901b..db91669e995 100644 --- a/src/DataStreams/TTLBlockInputStream.h +++ b/src/DataStreams/TTLBlockInputStream.h @@ -47,6 +47,7 @@ private: Logger * log; DateLUTImpl date_lut; + /// TODO rewrite defaults logic to evaluteMissingDefaults std::unordered_map defaults_result_column; ExpressionActionsPtr defaults_expression; diff --git a/src/Interpreters/InterpreterCreateQuery.cpp b/src/Interpreters/InterpreterCreateQuery.cpp index be7bd238025..d7c9d3122c3 100644 --- a/src/Interpreters/InterpreterCreateQuery.cpp +++ b/src/Interpreters/InterpreterCreateQuery.cpp @@ -335,13 +335,7 @@ ColumnsDescription InterpreterCreateQuery::getColumnsDescription(const ASTExpres { ASTPtr default_expr = col_decl.default_expression->clone(); if (col_decl.type) - { - const auto & deduced_type = defaults_sample_block.getByName(column.name + "_tmp").type; column.type = name_type_it->type; - - if (!column.type->equals(*deduced_type)) - default_expr = addTypeConversionToAST(std::move(default_expr), column.type->getName()); - } else column.type = defaults_sample_block.getByName(column.name).type; diff --git a/tests/queries/0_stateless/00700_decimal_defaults.reference b/tests/queries/0_stateless/00700_decimal_defaults.reference index f7358cd311b..f3f1fba83e7 100644 --- a/tests/queries/0_stateless/00700_decimal_defaults.reference +++ b/tests/queries/0_stateless/00700_decimal_defaults.reference @@ -1,6 +1,6 @@ -a Decimal(9, 4) DEFAULT CAST(0, \'Decimal(9, 4)\') -b Decimal(18, 4) DEFAULT CAST(a / 2, \'Decimal(18, 4)\') -c Decimal(38, 4) DEFAULT CAST(b / 3, \'Decimal(38, 4)\') +a Decimal(9, 4) DEFAULT 0 +b Decimal(18, 4) DEFAULT a / 2 +c Decimal(38, 4) DEFAULT b / 3 d Decimal(9, 4) MATERIALIZED a + toDecimal32(\'0.2\', 1) e Decimal(18, 4) ALIAS b * 2 f Decimal(38, 4) ALIAS c * 6 diff --git a/tests/queries/0_stateless/01135_default_and_alter_zookeeper.reference b/tests/queries/0_stateless/01135_default_and_alter_zookeeper.reference new file mode 100644 index 00000000000..f7c4a48b4bc --- /dev/null +++ b/tests/queries/0_stateless/01135_default_and_alter_zookeeper.reference @@ -0,0 +1,2 @@ +4 +CREATE TABLE default.default_table\n(\n `id` UInt64, \n `enum_column` Enum8(\'undefined\' = 0, \'fox\' = 1, \'index\' = 2) DEFAULT \'fox\'\n)\nENGINE = ReplicatedMergeTree(\'/clickhouse/test/default_table\', \'1\')\nORDER BY tuple()\nSETTINGS index_granularity = 8192 diff --git a/tests/queries/0_stateless/01135_default_and_alter_zookeeper.sql b/tests/queries/0_stateless/01135_default_and_alter_zookeeper.sql new file mode 100644 index 00000000000..71d418ff48b --- /dev/null +++ b/tests/queries/0_stateless/01135_default_and_alter_zookeeper.sql @@ -0,0 +1,27 @@ +DROP TABLE IF EXISTS default_table; + +CREATE TABLE default_table +( + id UInt64, + enum_column Enum8('undefined' = 0, 'fox' = 1, 'index' = 2) +) +ENGINE ReplicatedMergeTree('/clickhouse/test/default_table', '1') +ORDER BY tuple(); + +INSERT INTO default_table VALUES(1, 'index'), (2, 'fox'); + +ALTER TABLE default_table MODIFY COLUMN enum_column Enum8('undefined' = 0, 'fox' = 1, 'index' = 2) DEFAULT 'undefined'; + +INSERT INTO default_table (id) VALUES(3), (4); + +DETACH TABLE default_table; + +ATTACH TABLE default_table; + +SELECT COUNT() from default_table; + +ALTER TABLE default_table MODIFY COLUMN enum_column Enum8('undefined' = 0, 'fox' = 1, 'index' = 2) DEFAULT 'fox'; + +SHOW CREATE TABLE default_table; + +DROP TABLE IF EXISTS default_table;