2018-12-28 15:38:58 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2019-04-03 16:06:05 +00:00
|
|
|
#include <Core/NamesAndTypes.h>
|
2018-12-17 16:20:15 +00:00
|
|
|
#include <Interpreters/JoinToSubqueryTransformVisitor.h>
|
2019-02-20 12:12:36 +00:00
|
|
|
#include <Interpreters/IdentifierSemantic.h>
|
|
|
|
#include <Interpreters/AsteriskSemantic.h>
|
|
|
|
#include <Interpreters/DatabaseAndTableWithAlias.h>
|
2019-04-03 16:06:05 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2018-12-17 16:20:15 +00:00
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
#include <Parsers/ParserTablesInSelectQuery.h>
|
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
|
2018-12-17 16:20:15 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int TOO_DEEP_AST;
|
2019-02-20 12:12:36 +00:00
|
|
|
extern const int AMBIGUOUS_COLUMN_NAME;
|
2019-02-20 15:28:53 +00:00
|
|
|
extern const int NOT_IMPLEMENTED;
|
2019-04-03 16:06:05 +00:00
|
|
|
extern const int UNKNOWN_IDENTIFIER;
|
2018-12-17 16:20:15 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 16:06:05 +00:00
|
|
|
NamesAndTypesList getNamesAndTypeListFromTableExpression(const ASTTableExpression & table_expression, const Context & context);
|
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2019-04-03 16:06:05 +00:00
|
|
|
/// Replace asterisks in select_expression_list with column identifiers
|
2019-04-04 12:14:10 +00:00
|
|
|
class ExtractAsterisksMatcher
|
2019-04-03 16:06:05 +00:00
|
|
|
{
|
2019-04-04 12:14:10 +00:00
|
|
|
public:
|
2019-04-03 16:06:05 +00:00
|
|
|
using Visitor = InDepthNodeVisitor<ExtractAsterisksMatcher, true>;
|
|
|
|
|
|
|
|
struct Data
|
|
|
|
{
|
|
|
|
std::unordered_map<String, NamesAndTypesList> table_columns;
|
2019-04-04 12:14:10 +00:00
|
|
|
std::vector<String> tables_order;
|
2019-04-03 16:06:05 +00:00
|
|
|
std::shared_ptr<ASTExpressionList> new_select_expression_list;
|
|
|
|
|
|
|
|
Data(const Context & context, const std::vector<const ASTTableExpression *> & table_expressions)
|
|
|
|
{
|
2019-04-04 12:14:10 +00:00
|
|
|
tables_order.reserve(table_expressions.size());
|
2019-04-03 16:06:05 +00:00
|
|
|
for (const auto & expr : table_expressions)
|
|
|
|
{
|
|
|
|
if (expr->subquery)
|
|
|
|
{
|
|
|
|
table_columns.clear();
|
2019-04-04 12:14:10 +00:00
|
|
|
tables_order.clear();
|
2019-04-03 16:06:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
String table_name = DatabaseAndTableWithAlias(*expr, context.getCurrentDatabase()).getQualifiedNamePrefix(false);
|
|
|
|
NamesAndTypesList columns = getNamesAndTypeListFromTableExpression(*expr, context);
|
2019-04-04 12:14:10 +00:00
|
|
|
tables_order.push_back(table_name);
|
2019-04-03 16:06:05 +00:00
|
|
|
table_columns.emplace(std::move(table_name), std::move(columns));
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 12:14:10 +00:00
|
|
|
|
|
|
|
void addTableColumns(const String & table_name)
|
|
|
|
{
|
|
|
|
auto it = table_columns.find(table_name);
|
|
|
|
if (it == table_columns.end())
|
|
|
|
throw Exception("Unknown qualified identifier: " + table_name, ErrorCodes::UNKNOWN_IDENTIFIER);
|
|
|
|
|
|
|
|
for (const auto & column : it->second)
|
|
|
|
new_select_expression_list->children.push_back(
|
|
|
|
std::make_shared<ASTIdentifier>(std::vector<String>{it->first, column.name}));
|
|
|
|
}
|
2019-04-03 16:06:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool needChildVisit(ASTPtr &, const ASTPtr &) { return false; }
|
|
|
|
|
|
|
|
static void visit(ASTPtr & ast, Data & data)
|
|
|
|
{
|
|
|
|
if (auto * t = ast->as<ASTSelectQuery>())
|
|
|
|
visit(*t, ast, data);
|
|
|
|
if (auto * t = ast->as<ASTExpressionList>())
|
|
|
|
visit(*t, ast, data);
|
|
|
|
}
|
|
|
|
|
2019-04-04 12:14:10 +00:00
|
|
|
private:
|
2019-04-03 16:06:05 +00:00
|
|
|
static void visit(ASTSelectQuery & node, ASTPtr &, Data & data)
|
|
|
|
{
|
|
|
|
if (data.table_columns.empty())
|
|
|
|
return;
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
Visitor(data).visit(node.refSelect());
|
2019-04-03 16:06:05 +00:00
|
|
|
if (!data.new_select_expression_list)
|
|
|
|
return;
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
node.setExpression(ASTSelectQuery::Expression::SELECT, std::move(data.new_select_expression_list));
|
2019-04-03 16:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void visit(ASTExpressionList & node, ASTPtr &, Data & data)
|
|
|
|
{
|
|
|
|
bool has_asterisks = false;
|
|
|
|
data.new_select_expression_list = std::make_shared<ASTExpressionList>();
|
|
|
|
data.new_select_expression_list->children.reserve(node.children.size());
|
|
|
|
|
|
|
|
for (auto & child : node.children)
|
|
|
|
{
|
|
|
|
if (child->as<ASTAsterisk>())
|
|
|
|
{
|
|
|
|
has_asterisks = true;
|
|
|
|
|
2019-04-04 12:14:10 +00:00
|
|
|
for (auto & table_name : data.tables_order)
|
|
|
|
data.addTableColumns(table_name);
|
2019-04-03 16:06:05 +00:00
|
|
|
}
|
|
|
|
else if (child->as<ASTQualifiedAsterisk>())
|
|
|
|
{
|
|
|
|
has_asterisks = true;
|
|
|
|
|
|
|
|
if (child->children.size() != 1)
|
|
|
|
throw Exception("Logical error: qualified asterisk must have exactly one child", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
ASTIdentifier & identifier = child->children[0]->as<ASTIdentifier &>();
|
|
|
|
|
2019-04-04 12:14:10 +00:00
|
|
|
data.addTableColumns(identifier.name);
|
2019-04-03 16:06:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
data.new_select_expression_list->children.push_back(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_asterisks)
|
|
|
|
data.new_select_expression_list.reset();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
/// Find columns with aliases to push them into rewritten subselects.
|
|
|
|
/// Normalize table aliases: table_name.column_name -> table_alias.column_name
|
|
|
|
/// Make aliases maps (alias -> column_name, column_name -> alias)
|
2019-02-20 15:28:53 +00:00
|
|
|
struct ColumnAliasesMatcher
|
2019-02-20 12:12:36 +00:00
|
|
|
{
|
2019-02-20 15:28:53 +00:00
|
|
|
struct Data
|
|
|
|
{
|
|
|
|
const std::vector<DatabaseAndTableWithAlias> tables;
|
2019-02-21 12:45:31 +00:00
|
|
|
bool public_names;
|
2019-03-05 12:34:48 +00:00
|
|
|
AsteriskSemantic::RevertedAliases rev_aliases; /// long_name -> aliases
|
|
|
|
std::unordered_map<String, String> aliases; /// alias -> long_name
|
2019-02-21 12:45:31 +00:00
|
|
|
std::vector<std::pair<ASTIdentifier *, bool>> compound_identifiers;
|
2019-03-05 12:34:48 +00:00
|
|
|
std::set<String> allowed_long_names; /// original names allowed as aliases '--t.x as t.x' (select expressions only).
|
2019-02-20 15:28:53 +00:00
|
|
|
|
2019-04-03 16:06:05 +00:00
|
|
|
Data(const std::vector<DatabaseAndTableWithAlias> && tables_)
|
2019-02-20 15:28:53 +00:00
|
|
|
: tables(tables_)
|
2019-02-21 12:45:31 +00:00
|
|
|
, public_names(false)
|
2019-02-20 15:28:53 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
void replaceIdentifiersWithAliases()
|
|
|
|
{
|
|
|
|
String hide_prefix = "--"; /// @note restriction: user should not use alises like `--table.column`
|
|
|
|
|
2019-02-21 12:45:31 +00:00
|
|
|
for (auto & [identifier, is_public] : compound_identifiers)
|
2019-02-20 15:28:53 +00:00
|
|
|
{
|
2019-03-05 12:34:48 +00:00
|
|
|
String long_name = identifier->name;
|
|
|
|
|
|
|
|
auto it = rev_aliases.find(long_name);
|
2019-02-20 15:28:53 +00:00
|
|
|
if (it == rev_aliases.end())
|
|
|
|
{
|
|
|
|
bool last_table = IdentifierSemantic::canReferColumnToTable(*identifier, tables.back());
|
|
|
|
if (!last_table)
|
|
|
|
{
|
|
|
|
String alias = hide_prefix + long_name;
|
|
|
|
aliases[alias] = long_name;
|
|
|
|
rev_aliases[long_name].push_back(alias);
|
|
|
|
|
|
|
|
identifier->setShortName(alias);
|
2019-02-21 12:45:31 +00:00
|
|
|
if (is_public)
|
2019-03-05 12:34:48 +00:00
|
|
|
{
|
2019-02-21 12:45:31 +00:00
|
|
|
identifier->setAlias(long_name);
|
2019-03-05 12:34:48 +00:00
|
|
|
allowed_long_names.insert(long_name);
|
|
|
|
}
|
2019-02-20 15:28:53 +00:00
|
|
|
}
|
2019-02-21 19:43:14 +00:00
|
|
|
else if (is_public)
|
2019-03-05 12:34:48 +00:00
|
|
|
identifier->setAlias(long_name); /// prevent crop long to short name
|
2019-02-20 15:28:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (it->second.empty())
|
2019-03-05 12:34:48 +00:00
|
|
|
throw Exception("No alias for '" + long_name + "'", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
if (is_public && allowed_long_names.count(long_name))
|
|
|
|
; /// leave original name unchanged for correct output
|
|
|
|
else
|
|
|
|
identifier->setShortName(it->second[0]);
|
2019-02-20 15:28:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-02-20 12:12:36 +00:00
|
|
|
|
2019-02-20 15:28:53 +00:00
|
|
|
static bool needChildVisit(ASTPtr & node, const ASTPtr &)
|
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
if (node->as<ASTQualifiedAsterisk>())
|
2019-02-20 15:28:53 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-20 12:12:36 +00:00
|
|
|
|
2019-02-22 13:33:56 +00:00
|
|
|
static void visit(ASTPtr & ast, Data & data)
|
2019-02-20 15:28:53 +00:00
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
if (auto * t = ast->as<ASTIdentifier>())
|
2019-02-20 15:28:53 +00:00
|
|
|
visit(*t, ast, data);
|
|
|
|
|
2019-03-11 13:22:51 +00:00
|
|
|
if (ast->as<ASTAsterisk>() || ast->as<ASTQualifiedAsterisk>())
|
2019-04-03 16:06:05 +00:00
|
|
|
throw Exception("Multiple JOIN do not support asterisks for complex queries yet", ErrorCodes::NOT_IMPLEMENTED);
|
2019-02-20 15:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void visit(ASTIdentifier & node, ASTPtr &, Data & data)
|
2019-02-20 12:12:36 +00:00
|
|
|
{
|
|
|
|
if (node.isShort())
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool last_table = false;
|
|
|
|
String long_name;
|
2019-02-20 15:28:53 +00:00
|
|
|
for (auto & table : data.tables)
|
2019-02-20 12:12:36 +00:00
|
|
|
{
|
|
|
|
if (IdentifierSemantic::canReferColumnToTable(node, table))
|
|
|
|
{
|
|
|
|
if (!long_name.empty())
|
|
|
|
throw Exception("Cannot refer column '" + node.name + "' to one table", ErrorCodes::AMBIGUOUS_COLUMN_NAME);
|
|
|
|
IdentifierSemantic::setColumnLongName(node, table); /// table_name.column_name -> table_alias.column_name
|
|
|
|
long_name = node.name;
|
2019-02-20 15:28:53 +00:00
|
|
|
if (&table == &data.tables.back())
|
2019-02-20 12:12:36 +00:00
|
|
|
last_table = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (long_name.empty())
|
|
|
|
throw Exception("Cannot refer column '" + node.name + "' to table", ErrorCodes::AMBIGUOUS_COLUMN_NAME);
|
|
|
|
|
|
|
|
String alias = node.tryGetAlias();
|
|
|
|
if (!alias.empty())
|
|
|
|
{
|
2019-02-20 15:28:53 +00:00
|
|
|
data.aliases[alias] = long_name;
|
|
|
|
data.rev_aliases[long_name].push_back(alias);
|
2019-02-20 12:12:36 +00:00
|
|
|
|
|
|
|
if (!last_table)
|
|
|
|
{
|
|
|
|
node.setShortName(alias);
|
|
|
|
node.setAlias("");
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 12:34:48 +00:00
|
|
|
else if (node.compound())
|
2019-02-21 12:45:31 +00:00
|
|
|
data.compound_identifiers.emplace_back(&node, data.public_names);
|
2019-02-20 12:12:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Attach additional semantic info to generated selects.
|
2018-12-17 19:30:08 +00:00
|
|
|
struct AppendSemanticVisitorData
|
2018-12-17 16:20:15 +00:00
|
|
|
{
|
|
|
|
using TypeToVisit = ASTSelectQuery;
|
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
AsteriskSemantic::RevertedAliasesPtr rev_aliases = {};
|
2018-12-17 16:20:15 +00:00
|
|
|
bool done = false;
|
|
|
|
|
|
|
|
void visit(ASTSelectQuery & select, ASTPtr &)
|
|
|
|
{
|
2019-04-09 14:22:35 +00:00
|
|
|
if (done || !rev_aliases || !select.select())
|
2018-12-17 16:20:15 +00:00
|
|
|
return;
|
2019-02-20 12:12:36 +00:00
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
for (auto & child : select.select()->children)
|
2019-02-20 12:12:36 +00:00
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
if (auto * node = child->as<ASTAsterisk>())
|
2019-02-20 12:12:36 +00:00
|
|
|
AsteriskSemantic::setAliases(*node, rev_aliases);
|
2019-03-11 13:22:51 +00:00
|
|
|
if (auto * node = child->as<ASTQualifiedAsterisk>())
|
2019-02-20 12:12:36 +00:00
|
|
|
AsteriskSemantic::setAliases(*node, rev_aliases);
|
|
|
|
}
|
|
|
|
|
2018-12-17 16:20:15 +00:00
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
|
|
|
|
/// Replaces table elements with pair.
|
2018-12-17 16:20:15 +00:00
|
|
|
struct RewriteTablesVisitorData
|
|
|
|
{
|
|
|
|
using TypeToVisit = ASTTablesInSelectQuery;
|
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
ASTPtr left;
|
|
|
|
ASTPtr right;
|
2018-12-17 16:20:15 +00:00
|
|
|
bool done = false;
|
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
/// @note Do not change ASTTablesInSelectQuery itself. No need to change select.tables.
|
2018-12-17 16:20:15 +00:00
|
|
|
void visit(ASTTablesInSelectQuery &, ASTPtr & ast)
|
|
|
|
{
|
|
|
|
if (done)
|
|
|
|
return;
|
2019-02-20 12:12:36 +00:00
|
|
|
std::vector<ASTPtr> new_tables{left, right};
|
|
|
|
ast->children.swap(new_tables);
|
2018-12-17 16:20:15 +00:00
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-03 16:06:05 +00:00
|
|
|
bool needRewrite(ASTSelectQuery & select, std::vector<const ASTTableExpression *> & table_expressions)
|
2018-12-18 18:28:02 +00:00
|
|
|
{
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!select.tables())
|
2019-02-01 14:26:36 +00:00
|
|
|
return false;
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
const auto * tables = select.tables()->as<ASTTablesInSelectQuery>();
|
2018-12-18 18:28:02 +00:00
|
|
|
if (!tables)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t num_tables = tables->children.size();
|
|
|
|
if (num_tables <= 2)
|
|
|
|
return false;
|
|
|
|
|
2019-04-08 12:35:26 +00:00
|
|
|
size_t num_array_join = 0;
|
|
|
|
size_t num_using = 0;
|
|
|
|
|
2019-04-03 16:06:05 +00:00
|
|
|
table_expressions.reserve(num_tables);
|
|
|
|
for (size_t i = 0; i < num_tables; ++i)
|
2019-02-20 12:12:36 +00:00
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * table = tables->children[i]->as<ASTTablesInSelectQueryElement>();
|
2019-04-08 12:35:26 +00:00
|
|
|
if (!table)
|
|
|
|
throw Exception("Table expected", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
if (table->table_expression)
|
2019-04-03 16:06:05 +00:00
|
|
|
if (const auto * expression = table->table_expression->as<ASTTableExpression>())
|
|
|
|
table_expressions.push_back(expression);
|
|
|
|
if (!i)
|
|
|
|
continue;
|
|
|
|
|
2019-04-08 12:35:26 +00:00
|
|
|
if (!table->table_join && !table->array_join)
|
|
|
|
throw Exception("Joined table expected", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
if (table->array_join)
|
|
|
|
{
|
|
|
|
++num_array_join;
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-20 12:12:36 +00:00
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
const auto & join = table->table_join->as<ASTTableJoin &>();
|
|
|
|
if (isComma(join.kind))
|
2019-03-12 11:06:54 +00:00
|
|
|
throw Exception("COMMA to CROSS JOIN rewriter is not enabled or cannot rewrite query", ErrorCodes::NOT_IMPLEMENTED);
|
2019-02-20 12:12:36 +00:00
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
if (join.using_expression_list)
|
2019-04-08 12:35:26 +00:00
|
|
|
++num_using;
|
2019-02-20 12:12:36 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 12:35:26 +00:00
|
|
|
if (num_tables - num_array_join <= 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/// it's not trivial to support mix of JOIN ON & JOIN USING cause of short names
|
|
|
|
if (num_using)
|
|
|
|
throw Exception("Multiple JOIN does not support USING", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
if (num_array_join)
|
|
|
|
throw Exception("Multiple JOIN does not support mix with ARRAY JOINs", ErrorCodes::NOT_IMPLEMENTED);
|
2018-12-18 18:28:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
using RewriteMatcher = OneTypeMatcher<RewriteTablesVisitorData>;
|
|
|
|
using RewriteVisitor = InDepthNodeVisitor<RewriteMatcher, true>;
|
2019-04-03 16:06:05 +00:00
|
|
|
using ExtractAsterisksVisitor = ExtractAsterisksMatcher::Visitor;
|
2019-02-20 12:12:36 +00:00
|
|
|
using ColumnAliasesVisitor = InDepthNodeVisitor<ColumnAliasesMatcher, true>;
|
|
|
|
using AppendSemanticMatcher = OneTypeMatcher<AppendSemanticVisitorData>;
|
|
|
|
using AppendSemanticVisitor = InDepthNodeVisitor<AppendSemanticMatcher, true>;
|
|
|
|
|
|
|
|
} /// namelesspace
|
2018-12-17 16:20:15 +00:00
|
|
|
|
|
|
|
|
2019-02-22 13:33:56 +00:00
|
|
|
void JoinToSubqueryTransformMatcher::visit(ASTPtr & ast, Data & data)
|
2018-12-17 16:20:15 +00:00
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
if (auto * t = ast->as<ASTSelectQuery>())
|
2018-12-17 16:20:15 +00:00
|
|
|
visit(*t, ast, data);
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:06:05 +00:00
|
|
|
void JoinToSubqueryTransformMatcher::visit(ASTSelectQuery & select, ASTPtr & ast, Data & data)
|
2018-12-17 16:20:15 +00:00
|
|
|
{
|
2019-02-20 12:12:36 +00:00
|
|
|
using RevertedAliases = AsteriskSemantic::RevertedAliases;
|
2018-12-18 18:28:02 +00:00
|
|
|
|
2019-04-03 16:06:05 +00:00
|
|
|
std::vector<const ASTTableExpression *> table_expressions;
|
|
|
|
if (!needRewrite(select, table_expressions))
|
2018-12-18 18:28:02 +00:00
|
|
|
return;
|
2018-12-17 16:20:15 +00:00
|
|
|
|
2019-04-03 16:06:05 +00:00
|
|
|
ExtractAsterisksVisitor::Data asterisks_data(data.context, table_expressions);
|
|
|
|
ExtractAsterisksVisitor(asterisks_data).visit(ast);
|
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
ColumnAliasesVisitor::Data aliases_data(getDatabaseAndTables(select, ""));
|
2019-04-09 14:22:35 +00:00
|
|
|
if (select.select())
|
2019-02-21 12:45:31 +00:00
|
|
|
{
|
|
|
|
aliases_data.public_names = true;
|
2019-04-09 14:22:35 +00:00
|
|
|
ColumnAliasesVisitor(aliases_data).visit(select.refSelect());
|
2019-02-21 12:45:31 +00:00
|
|
|
aliases_data.public_names = false;
|
|
|
|
}
|
2019-04-09 14:22:35 +00:00
|
|
|
if (select.where())
|
|
|
|
ColumnAliasesVisitor(aliases_data).visit(select.refWhere());
|
|
|
|
if (select.prewhere())
|
|
|
|
ColumnAliasesVisitor(aliases_data).visit(select.refPrewhere());
|
|
|
|
if (select.having())
|
|
|
|
ColumnAliasesVisitor(aliases_data).visit(select.refHaving());
|
2019-02-20 12:12:36 +00:00
|
|
|
|
|
|
|
/// JOIN sections
|
2019-04-09 14:22:35 +00:00
|
|
|
for (auto & child : select.tables()->children)
|
2019-02-20 12:12:36 +00:00
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
auto * table = child->as<ASTTablesInSelectQueryElement>();
|
2019-02-20 12:12:36 +00:00
|
|
|
if (table->table_join)
|
|
|
|
{
|
2019-03-15 16:14:13 +00:00
|
|
|
auto & join = table->table_join->as<ASTTableJoin &>();
|
|
|
|
if (join.on_expression)
|
|
|
|
ColumnAliasesVisitor(aliases_data).visit(join.on_expression);
|
2019-02-20 12:12:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-17 16:20:15 +00:00
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
aliases_data.replaceIdentifiersWithAliases();
|
2018-12-17 16:20:15 +00:00
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
auto rev_aliases = std::make_shared<RevertedAliases>();
|
|
|
|
rev_aliases->swap(aliases_data.rev_aliases);
|
2018-12-17 19:30:08 +00:00
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
auto & src_tables = select.tables()->children;
|
2019-02-20 12:12:36 +00:00
|
|
|
ASTPtr left_table = src_tables[0];
|
2018-12-17 16:20:15 +00:00
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
for (size_t i = 1; i < src_tables.size() - 1; ++i)
|
|
|
|
{
|
|
|
|
left_table = replaceJoin(left_table, src_tables[i]);
|
|
|
|
if (!left_table)
|
|
|
|
throw Exception("Cannot replace tables with subselect", ErrorCodes::LOGICAL_ERROR);
|
2018-12-17 16:20:15 +00:00
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
/// attach data to generated asterisk
|
|
|
|
AppendSemanticVisitor::Data semantic_data{rev_aliases, false};
|
|
|
|
AppendSemanticVisitor(semantic_data).visit(left_table);
|
2018-12-17 16:20:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
/// replace tables in select with generated two-table join
|
|
|
|
RewriteVisitor::Data visitor_data{left_table, src_tables.back()};
|
2019-04-09 14:22:35 +00:00
|
|
|
RewriteVisitor(visitor_data).visit(select.refTables());
|
2018-12-17 16:20:15 +00:00
|
|
|
|
|
|
|
data.done = true;
|
|
|
|
}
|
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
static ASTPtr makeSubqueryTemplate()
|
2018-12-17 16:20:15 +00:00
|
|
|
{
|
2019-02-20 12:12:36 +00:00
|
|
|
ParserTablesInSelectQueryElement parser(true);
|
|
|
|
ASTPtr subquery_template = parseQuery(parser, "(select * from _t)", 0);
|
|
|
|
if (!subquery_template)
|
|
|
|
throw Exception("Cannot parse subquery template", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
return subquery_template;
|
|
|
|
}
|
2018-12-17 16:20:15 +00:00
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
ASTPtr JoinToSubqueryTransformMatcher::replaceJoin(ASTPtr ast_left, ASTPtr ast_right)
|
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * left = ast_left->as<ASTTablesInSelectQueryElement>();
|
|
|
|
const auto * right = ast_right->as<ASTTablesInSelectQueryElement>();
|
2018-12-17 16:20:15 +00:00
|
|
|
if (!left || !right)
|
|
|
|
throw Exception("Two TablesInSelectQueryElements expected", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
if (!right->table_join)
|
|
|
|
throw Exception("Table join expected", ErrorCodes::LOGICAL_ERROR);
|
2018-12-17 16:20:15 +00:00
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
static ASTPtr subquery_template = makeSubqueryTemplate();
|
2018-12-17 16:20:15 +00:00
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
/// replace '_t' with pair of joined tables
|
|
|
|
ASTPtr res = subquery_template->clone();
|
2019-01-17 17:01:48 +00:00
|
|
|
RewriteVisitor::Data visitor_data{ast_left, ast_right};
|
2018-12-17 16:20:15 +00:00
|
|
|
RewriteVisitor(visitor_data).visit(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|