Merge pull request #10441 from ClickHouse/remove_casts_from_defaults

Remove CAST from DEFAULT expressions during table creation.
This commit is contained in:
alexey-milovidov 2020-04-23 12:36:12 +03:00 committed by GitHub
commit 1ecfa27cd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 46 additions and 19 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,24 +1,24 @@
d Date DEFAULT CAST(\'2015-12-29\', \'Date\')
d Date DEFAULT \'2015-12-29\'
k UInt8 DEFAULT 0
e Enum8(\'hello\' = 1, \'world\' = 2)
sign Enum8(\'minus\' = -1, \'plus\' = 1)
letter Enum16(\'*\' = -256, \'a\' = 0, \'b\' = 1, \'c\' = 2)
2015-12-29 0 hello minus *
d Date DEFAULT CAST(\'2015-12-29\', \'Date\')
d Date DEFAULT \'2015-12-29\'
k UInt8 DEFAULT 0
e Enum8(\'hello\' = 1, \'world\' = 2, \'!\' = 3)
sign Enum8(\'minus\' = -1, \'plus\' = 1)
letter Enum16(\'*\' = -256, \'a\' = 0, \'b\' = 1, \'c\' = 2)
2015-12-29 0 hello minus *
2015-12-29 0 ! plus b
d Date DEFAULT CAST(\'2015-12-29\', \'Date\')
d Date DEFAULT \'2015-12-29\'
k UInt8 DEFAULT 0
e Enum16(\'hello\' = 1, \'world\' = 2, \'!\' = 3)
sign Enum16(\'minus\' = -1, \'plus\' = 1)
letter Enum16(\'no letter\' = -256, \'a\' = 0, \'b\' = 1, \'c\' = 2)
2015-12-29 0 hello minus no letter
2015-12-29 0 ! plus b
d Date DEFAULT CAST(\'2015-12-29\', \'Date\')
d Date DEFAULT \'2015-12-29\'
k UInt8 DEFAULT 0
e Enum8(\'hello\' = 1, \'world\' = 2, \'!\' = 3)
sign Enum8(\'minus\' = -1, \'plus\' = 1)

View File

@ -1,25 +1,25 @@
date Date
val UInt64
val2 UInt8 DEFAULT 42
val3 UInt8 DEFAULT CAST(val2 + 1, \'UInt8\')
val3 UInt8 DEFAULT val2 + 1
val4 UInt64 ALIAS val
-
date Date
val UInt64
val2 UInt8 DEFAULT 42
val3 UInt8 DEFAULT CAST(val2 + 1, \'UInt8\')
val3 UInt8 DEFAULT val2 + 1
val4 UInt64 ALIAS val
-
date Date
val UInt64
val2 UInt8 DEFAULT 42
val3 UInt8 DEFAULT CAST(val2 + 1, \'UInt8\')
val3 UInt8 DEFAULT val2 + 1
val4 UInt64 ALIAS val
-
date Date
val UInt64
val2 UInt8 DEFAULT 42
val3 UInt8 DEFAULT CAST(val2 + 1, \'UInt8\')
val3 UInt8 DEFAULT val2 + 1
val4 UInt64 ALIAS val
-
1 UInt8

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;