Skip to content

Commit

Permalink
Merge pull request #10563 from azat/SELECT-ALIAS-CAST
Browse files Browse the repository at this point in the history
Fix SELECT of column ALIAS which default expression type different from column type
  • Loading branch information
alexey-milovidov committed May 11, 2020
2 parents 6149638 + d2c813f commit 15e38c8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Interpreters/InterpreterSelectQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <Interpreters/InterpreterSetQuery.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Interpreters/convertFieldToType.h>
#include <Interpreters/addTypeConversionToAST.h>
#include <Interpreters/ExpressionAnalyzer.h>
#include <Interpreters/getTableExpressions.h>
#include <Interpreters/JoinToSubqueryTransformVisitor.h>
Expand Down Expand Up @@ -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<ASTIdentifier>(column);

Expand Down
4 changes: 4 additions & 0 deletions tests/queries/0_stateless/01269_alias_type_differs.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
UInt8
0
UInt8
0
22 changes: 22 additions & 0 deletions tests/queries/0_stateless/01269_alias_type_differs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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;

DROP TABLE data_01269;

0 comments on commit 15e38c8

Please sign in to comment.