From 007170481a966674e17f6e47e5c5abe8a87d3747 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Tue, 28 Apr 2020 22:41:23 +0300 Subject: [PATCH 1/3] Add a test for ALIAS with type different to the type of column --- .../01269_alias_type_differs.reference | 4 ++++ .../0_stateless/01269_alias_type_differs.sql | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/queries/0_stateless/01269_alias_type_differs.reference create mode 100644 tests/queries/0_stateless/01269_alias_type_differs.sql diff --git a/tests/queries/0_stateless/01269_alias_type_differs.reference b/tests/queries/0_stateless/01269_alias_type_differs.reference new file mode 100644 index 00000000000..f837a2bd382 --- /dev/null +++ b/tests/queries/0_stateless/01269_alias_type_differs.reference @@ -0,0 +1,4 @@ +UInt8 +0 +UInt8 +0 diff --git a/tests/queries/0_stateless/01269_alias_type_differs.sql b/tests/queries/0_stateless/01269_alias_type_differs.sql new file mode 100644 index 00000000000..3b99e5e7eec --- /dev/null +++ b/tests/queries/0_stateless/01269_alias_type_differs.sql @@ -0,0 +1,20 @@ +DROP TABLE IF EXISTS data_01269; +CREATE TABLE data_01269 +( + key Int32, + value Nullable(Int32), + alias UInt8 ALIAS value>0 +) +ENGINE = MergeTree() +ORDER BY key; +INSERT INTO data_01269 VALUES (1, 0); + +-- after PR#10441 +SELECT toTypeName(alias) FROM data_01269; +SELECT any(alias) FROM data_01269; + +-- even without PR#10441 +ALTER TABLE data_01269 DROP COLUMN alias; +ALTER TABLE data_01269 ADD COLUMN alias UInt8 ALIAS value>0; +SELECT toTypeName(alias) FROM data_01269; +SELECT any(alias) FROM data_01269; From 53aea23de0033b1a111bdd8a31241fddb7bd4c5a Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Tue, 28 Apr 2020 22:41:23 +0300 Subject: [PATCH 2/3] Fix SELECT of column ALIAS which default expression type different from column type --- src/Interpreters/InterpreterSelectQuery.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 2f98addd975..b74ce0da5aa 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -44,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -1210,7 +1211,12 @@ void InterpreterSelectQuery::executeFetchColumns( const auto column_default = storage_columns.getDefault(column); bool is_alias = column_default && column_default->kind == ColumnDefaultKind::Alias; if (is_alias) - column_expr = setAlias(column_default->expression->clone(), column); + { + auto column_decl = storage_columns.get(column); + /// TODO: can make CAST only if the type is different (but requires SyntaxAnalyzer). + auto cast_column_default = addTypeConversionToAST(column_default->expression->clone(), column_decl.type->getName()); + column_expr = setAlias(cast_column_default->clone(), column); + } else column_expr = std::make_shared(column); From d2c813fa7a7e5a8e3254e0c6bd17a2995e410775 Mon Sep 17 00:00:00 2001 From: alexey-milovidov Date: Mon, 11 May 2020 06:23:58 +0300 Subject: [PATCH 3/3] Update 01269_alias_type_differs.sql --- tests/queries/0_stateless/01269_alias_type_differs.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/queries/0_stateless/01269_alias_type_differs.sql b/tests/queries/0_stateless/01269_alias_type_differs.sql index 3b99e5e7eec..b78e46f62c8 100644 --- a/tests/queries/0_stateless/01269_alias_type_differs.sql +++ b/tests/queries/0_stateless/01269_alias_type_differs.sql @@ -18,3 +18,5 @@ ALTER TABLE data_01269 DROP COLUMN alias; ALTER TABLE data_01269 ADD COLUMN alias UInt8 ALIAS value>0; SELECT toTypeName(alias) FROM data_01269; SELECT any(alias) FROM data_01269; + +DROP TABLE data_01269;