mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-21 15:12:02 +00:00
Fix column alias rewriting
This commit is contained in:
parent
f1702d356e
commit
d74aa64f6b
@ -81,6 +81,7 @@ void ColumnAliasesMatcher::visit(ASTIdentifier & node, ASTPtr & ast, Data & data
|
||||
else
|
||||
ast->setAlias(*column_name);
|
||||
|
||||
data.changed = true;
|
||||
// revisit ast to track recursive alias columns
|
||||
Visitor(data).visit(ast);
|
||||
}
|
||||
|
@ -60,6 +60,9 @@ public:
|
||||
/// private_aliases are from lambda, so these are local names.
|
||||
NameSet private_aliases;
|
||||
|
||||
/// Check if query is changed by this visitor.
|
||||
bool changed = false;
|
||||
|
||||
Data(const ColumnsDescription & columns_, const NameToNameMap & array_join_result_columns_, ContextPtr context_)
|
||||
: columns(columns_), context(context_)
|
||||
{
|
||||
|
@ -951,8 +951,13 @@ TreeRewriterResultPtr TreeRewriter::analyzeSelect(
|
||||
/// rewrite filters for select query, must go after getArrayJoinedColumns
|
||||
if (settings.optimize_respect_aliases && result.metadata_snapshot)
|
||||
{
|
||||
replaceAliasColumnsInQuery(query, result.metadata_snapshot->getColumns(), result.array_join_result_to_source, getContext());
|
||||
result.collectUsedColumns(query, true);
|
||||
/// If query is changed, we need to redo some work to correct name resolution.
|
||||
if (replaceAliasColumnsInQuery(query, result.metadata_snapshot->getColumns(), result.array_join_result_to_source, getContext()))
|
||||
{
|
||||
result.aggregates = getAggregates(query, *select_query);
|
||||
result.window_function_asts = getWindowFunctions(query, *select_query);
|
||||
result.collectUsedColumns(query, true);
|
||||
}
|
||||
}
|
||||
|
||||
result.ast_join = select_query->join();
|
||||
|
@ -6,12 +6,13 @@
|
||||
namespace DB
|
||||
{
|
||||
|
||||
void replaceAliasColumnsInQuery(
|
||||
bool replaceAliasColumnsInQuery(
|
||||
ASTPtr & ast, const ColumnsDescription & columns, const NameToNameMap & array_join_result_to_source, ContextPtr context)
|
||||
{
|
||||
ColumnAliasesVisitor::Data aliases_column_data(columns, array_join_result_to_source, context);
|
||||
ColumnAliasesVisitor aliases_column_visitor(aliases_column_data);
|
||||
aliases_column_visitor.visit(ast);
|
||||
return aliases_column_data.changed;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,7 +10,8 @@ namespace DB
|
||||
|
||||
class ColumnsDescription;
|
||||
|
||||
void replaceAliasColumnsInQuery(
|
||||
/// Replace storage alias columns in select query if possible. Return true if the query is changed.
|
||||
bool replaceAliasColumnsInQuery(
|
||||
ASTPtr & ast, const ColumnsDescription & columns, const NameToNameMap & array_join_result_to_source, ContextPtr context);
|
||||
|
||||
}
|
||||
|
@ -61,3 +61,4 @@ second-index
|
||||
1
|
||||
1
|
||||
1
|
||||
1 1
|
||||
|
@ -127,3 +127,11 @@ select sum(i) from pd group by dt_m settings allow_experimental_projection_optim
|
||||
|
||||
drop table pd;
|
||||
drop table pl;
|
||||
|
||||
drop table if exists t;
|
||||
|
||||
create temporary table t (x UInt64, y alias x);
|
||||
insert into t values (1);
|
||||
select sum(x), sum(y) from t;
|
||||
|
||||
drop table t;
|
||||
|
Loading…
Reference in New Issue
Block a user