Remove casts from table creation by default

This commit is contained in:
alesapin 2020-04-22 17:29:38 +03:00
parent 886e54f188
commit 45b8cb5b37
6 changed files with 38 additions and 11 deletions

View File

@ -4,6 +4,7 @@
#include <Interpreters/SyntaxAnalyzer.h>
#include <Interpreters/ExpressionAnalyzer.h>
#include <Columns/ColumnConst.h>
#include <Interpreters/addTypeConversionToAST.h>
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<ASTExpressionList>();
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

View File

@ -47,6 +47,7 @@ private:
Logger * log;
DateLUTImpl date_lut;
/// TODO rewrite defaults logic to evaluteMissingDefaults
std::unordered_map<String, String> defaults_result_column;
ExpressionActionsPtr defaults_expression;

View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -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;