2018-10-09 14:32:11 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2018-09-28 10:52:08 +00:00
|
|
|
#include <Interpreters/QueryAliasesVisitor.h>
|
2018-09-12 05:41:09 +00:00
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
|
|
|
#include <Parsers/ASTSelectWithUnionQuery.h>
|
2019-07-22 19:21:07 +00:00
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
2018-09-12 05:41:09 +00:00
|
|
|
#include <Parsers/formatAST.h>
|
|
|
|
#include <Parsers/ASTSubquery.h>
|
2019-10-08 18:42:22 +00:00
|
|
|
#include <Common/quoteString.h>
|
2018-09-12 05:41:09 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int MULTIPLE_EXPRESSIONS_FOR_ALIAS;
|
|
|
|
}
|
|
|
|
|
2021-04-20 10:16:29 +00:00
|
|
|
namespace
|
2018-09-12 05:41:09 +00:00
|
|
|
{
|
2021-04-20 10:16:29 +00:00
|
|
|
|
|
|
|
constexpr auto dummy_subquery_name_prefix = "_subquery";
|
|
|
|
|
|
|
|
String wrongAliasMessage(const ASTPtr & ast, const ASTPtr & prev_ast, const String & alias)
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString message;
|
|
|
|
message << "Different expressions with the same alias " << backQuoteIfNeed(alias) << ":\n";
|
|
|
|
formatAST(*ast, message, false, true);
|
|
|
|
message << "\nand\n";
|
|
|
|
formatAST(*prev_ast, message, false, true);
|
|
|
|
message << '\n';
|
|
|
|
return message.str();
|
|
|
|
}
|
|
|
|
|
2018-12-06 19:02:42 +00:00
|
|
|
}
|
2018-11-02 15:09:15 +00:00
|
|
|
|
2018-12-06 19:02:42 +00:00
|
|
|
|
2020-03-18 21:38:27 +00:00
|
|
|
bool QueryAliasesWithSubqueries::needChildVisit(const ASTPtr & node, const ASTPtr &)
|
2018-12-06 19:02:42 +00:00
|
|
|
{
|
|
|
|
/// Don't descent into table functions and subqueries and special case for ArrayJoin.
|
2020-03-09 00:28:05 +00:00
|
|
|
return !(node->as<ASTTableExpression>() || node->as<ASTSelectWithUnionQuery>() || node->as<ASTArrayJoin>());
|
2018-12-06 19:02:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 21:38:27 +00:00
|
|
|
bool QueryAliasesNoSubqueries::needChildVisit(const ASTPtr & node, const ASTPtr & child)
|
|
|
|
{
|
|
|
|
if (node->as<ASTSubquery>())
|
|
|
|
return false;
|
|
|
|
return QueryAliasesWithSubqueries::needChildVisit(node, child);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void QueryAliasesMatcher<T>::visit(const ASTPtr & ast, Data & data)
|
2018-12-06 19:02:42 +00:00
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
if (auto * s = ast->as<ASTSubquery>())
|
2019-02-22 13:33:56 +00:00
|
|
|
visit(*s, ast, data);
|
2019-07-22 19:21:07 +00:00
|
|
|
else if (auto * q = ast->as<ASTSelectQuery>())
|
|
|
|
visit(*q, ast, data);
|
2019-03-11 13:22:51 +00:00
|
|
|
else if (auto * aj = ast->as<ASTArrayJoin>())
|
2019-02-22 13:33:56 +00:00
|
|
|
visit(*aj, ast, data);
|
|
|
|
else
|
|
|
|
visitOther(ast, data);
|
2018-11-02 15:09:15 +00:00
|
|
|
}
|
2018-09-12 05:41:09 +00:00
|
|
|
|
2020-03-18 21:38:27 +00:00
|
|
|
template <typename T>
|
|
|
|
void QueryAliasesMatcher<T>::visit(const ASTSelectQuery & select, const ASTPtr &, Data &)
|
2019-07-22 19:21:07 +00:00
|
|
|
{
|
|
|
|
ASTPtr with = select.with();
|
|
|
|
if (!with)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto & child : with->children)
|
|
|
|
if (auto * ast_with_alias = dynamic_cast<ASTWithAlias *>(child.get()))
|
|
|
|
ast_with_alias->prefer_alias_to_column_name = true;
|
|
|
|
}
|
|
|
|
|
2018-11-02 15:09:15 +00:00
|
|
|
/// The top-level aliases in the ARRAY JOIN section have a special meaning, we will not add them
|
|
|
|
/// (skip the expression list itself and its children).
|
2020-03-18 21:38:27 +00:00
|
|
|
template <typename T>
|
|
|
|
void QueryAliasesMatcher<T>::visit(const ASTArrayJoin &, const ASTPtr & ast, Data & data)
|
2018-11-02 15:09:15 +00:00
|
|
|
{
|
2018-12-06 19:02:42 +00:00
|
|
|
visitOther(ast, data);
|
|
|
|
|
2019-02-11 18:26:40 +00:00
|
|
|
std::vector<ASTPtr> grand_children;
|
2018-11-02 15:09:15 +00:00
|
|
|
for (auto & child1 : ast->children)
|
|
|
|
for (auto & child2 : child1->children)
|
|
|
|
for (auto & child3 : child2->children)
|
2019-02-11 18:26:40 +00:00
|
|
|
grand_children.push_back(child3);
|
|
|
|
|
|
|
|
/// create own visitor to run bottom to top
|
|
|
|
for (auto & child : grand_children)
|
2019-02-22 13:33:56 +00:00
|
|
|
Visitor(data).visit(child);
|
2018-11-02 15:09:15 +00:00
|
|
|
}
|
2018-09-12 05:41:09 +00:00
|
|
|
|
2018-11-02 15:09:15 +00:00
|
|
|
/// set unique aliases for all subqueries. this is needed, because:
|
|
|
|
/// 1) content of subqueries could change after recursive analysis, and auto-generated column names could become incorrect
|
|
|
|
/// 2) result of different scalar subqueries can be cached inside expressions compilation cache and must have different names
|
2020-03-18 21:38:27 +00:00
|
|
|
template <typename T>
|
2020-03-20 12:36:35 +00:00
|
|
|
void QueryAliasesMatcher<T>::visit(const ASTSubquery & const_subquery, const ASTPtr & ast, Data & data)
|
2018-11-02 15:09:15 +00:00
|
|
|
{
|
2020-03-20 12:36:35 +00:00
|
|
|
auto & aliases = data;
|
2020-03-08 11:07:05 +00:00
|
|
|
ASTSubquery & subquery = const_cast<ASTSubquery &>(const_subquery);
|
2018-12-06 19:02:42 +00:00
|
|
|
|
2018-11-02 15:09:15 +00:00
|
|
|
static std::atomic_uint64_t subquery_index = 0;
|
2018-09-12 05:41:09 +00:00
|
|
|
|
2018-11-02 15:09:15 +00:00
|
|
|
if (subquery.alias.empty())
|
|
|
|
{
|
|
|
|
String alias;
|
|
|
|
do
|
|
|
|
{
|
2021-04-20 10:16:29 +00:00
|
|
|
alias = dummy_subquery_name_prefix + std::to_string(++subquery_index);
|
2018-11-02 15:09:15 +00:00
|
|
|
}
|
|
|
|
while (aliases.count(alias));
|
2018-09-12 05:41:09 +00:00
|
|
|
|
2018-11-02 15:09:15 +00:00
|
|
|
subquery.setAlias(alias);
|
|
|
|
aliases[alias] = ast;
|
|
|
|
}
|
|
|
|
else
|
2020-03-18 21:38:27 +00:00
|
|
|
visitOther(ast, aliases);
|
2019-07-22 19:21:07 +00:00
|
|
|
|
|
|
|
subquery.prefer_alias_to_column_name = true;
|
2018-09-27 19:25:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 21:38:27 +00:00
|
|
|
template <typename T>
|
2020-03-20 12:36:35 +00:00
|
|
|
void QueryAliasesMatcher<T>::visitOther(const ASTPtr & ast, Data & data)
|
2018-09-27 19:25:18 +00:00
|
|
|
{
|
2020-03-20 12:36:35 +00:00
|
|
|
auto & aliases = data;
|
2018-09-12 05:41:09 +00:00
|
|
|
String alias = ast->tryGetAlias();
|
|
|
|
if (!alias.empty())
|
|
|
|
{
|
|
|
|
if (aliases.count(alias) && ast->getTreeHash() != aliases[alias]->getTreeHash())
|
2018-12-06 19:02:42 +00:00
|
|
|
throw Exception(wrongAliasMessage(ast, aliases[alias], alias), ErrorCodes::MULTIPLE_EXPRESSIONS_FOR_ALIAS);
|
2018-09-12 05:41:09 +00:00
|
|
|
|
|
|
|
aliases[alias] = ast;
|
|
|
|
}
|
2021-04-20 10:16:29 +00:00
|
|
|
|
|
|
|
/** QueryAliasesVisitor is executed before ExecuteScalarSubqueriesVisitor.
|
|
|
|
For example we have subquery in our query (SELECT sum(number) FROM numbers(10)).
|
|
|
|
|
|
|
|
After running QueryAliasesVisitor it will be (SELECT sum(number) FROM numbers(10)) as _subquery_1
|
|
|
|
and prefer_alias_to_column_name for this subquery will be true.
|
|
|
|
|
|
|
|
After running ExecuteScalarSubqueriesVisitor it will be converted to (45 as _subquery_1)
|
|
|
|
and prefer_alias_to_column_name for ast literal will be true.
|
|
|
|
|
|
|
|
But if we send such query on remote host with Distributed engine for example we cannot send prefer_alias_to_column_name
|
|
|
|
information for our ast node with query string. And this alias will be dropped because prefer_alias_to_column_name for ASTWIthAlias
|
|
|
|
by default is false.
|
|
|
|
|
|
|
|
It is imporant that subquery can be converted to literal during ExecuteScalarSubqueriesVisitor.
|
|
|
|
And code below check if we previously set for subquery alias as _subquery, and if it is true then set prefer_alias_to_column_name = true.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (auto * ast_with_alias = dynamic_cast<ASTWithAlias *>(ast.get()))
|
|
|
|
{
|
|
|
|
if (startsWith(alias, dummy_subquery_name_prefix))
|
|
|
|
ast_with_alias->prefer_alias_to_column_name = true;
|
|
|
|
}
|
2018-11-02 15:09:15 +00:00
|
|
|
}
|
2018-09-12 05:41:09 +00:00
|
|
|
|
2020-03-18 21:38:27 +00:00
|
|
|
/// Explicit template instantiations
|
|
|
|
template class QueryAliasesMatcher<QueryAliasesWithSubqueries>;
|
|
|
|
template class QueryAliasesMatcher<QueryAliasesNoSubqueries>;
|
|
|
|
|
2018-09-12 05:41:09 +00:00
|
|
|
}
|