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/DatabaseAndTableWithAlias.h>
|
2020-06-16 16:32:32 +00:00
|
|
|
#include <Interpreters/RequiredSourceColumnsVisitor.h>
|
2018-12-17 16:20:15 +00:00
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
2019-08-29 18:45:34 +00:00
|
|
|
#include <Parsers/ASTSubquery.h>
|
2018-12-17 16:20:15 +00:00
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
2020-04-15 17:47:08 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2020-09-30 20:11:49 +00:00
|
|
|
#include <Parsers/ASTAsterisk.h>
|
2021-04-22 10:14:35 +00:00
|
|
|
#include <Parsers/ASTColumnsMatcher.h>
|
2022-04-19 17:22:04 +00:00
|
|
|
#include <Parsers/ASTColumnsTransformers.h>
|
2020-09-30 20:11:49 +00:00
|
|
|
#include <Parsers/ASTQualifiedAsterisk.h>
|
2018-12-17 16:20:15 +00:00
|
|
|
#include <Parsers/ParserTablesInSelectQuery.h>
|
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2020-04-15 20:28:05 +00:00
|
|
|
#include <Core/Defines.h>
|
2021-10-15 10:53:25 +00:00
|
|
|
#include <Common/StringUtils/StringUtils.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;
|
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-02-20 12:12:36 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2020-03-20 11:31:16 +00:00
|
|
|
/// @note we use `--` prefix for unique short names and `--.` for subqueries.
|
|
|
|
/// It expects that user do not use names starting with `--` and column names starting with dot.
|
2020-03-18 21:38:27 +00:00
|
|
|
ASTPtr makeSubqueryTemplate()
|
|
|
|
{
|
|
|
|
ParserTablesInSelectQueryElement parser(true);
|
2020-04-15 20:28:05 +00:00
|
|
|
ASTPtr subquery_template = parseQuery(parser, "(select * from _t) as `--.s`", 0, DBMS_DEFAULT_MAX_PARSER_DEPTH);
|
2020-03-18 21:38:27 +00:00
|
|
|
if (!subquery_template)
|
|
|
|
throw Exception("Cannot parse subquery template", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
return subquery_template;
|
|
|
|
}
|
|
|
|
|
2020-03-20 00:58:20 +00:00
|
|
|
ASTPtr makeSubqueryQualifiedAsterisk()
|
|
|
|
{
|
|
|
|
auto asterisk = std::make_shared<ASTQualifiedAsterisk>();
|
2022-11-02 06:11:37 +00:00
|
|
|
asterisk->qualifier = std::make_shared<ASTIdentifier>("--.s");
|
|
|
|
asterisk->children.push_back(asterisk->qualifier);
|
2020-03-20 00:58:20 +00:00
|
|
|
return asterisk;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
struct Data
|
|
|
|
{
|
|
|
|
std::unordered_map<String, NamesAndTypesList> table_columns;
|
2022-12-30 15:12:18 +00:00
|
|
|
std::unordered_map<String, String> table_name_alias;
|
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;
|
|
|
|
|
2020-03-20 17:16:36 +00:00
|
|
|
explicit Data(const std::vector<TableWithColumnNamesAndTypes> & tables)
|
2020-03-20 00:58:20 +00:00
|
|
|
{
|
|
|
|
tables_order.reserve(tables.size());
|
|
|
|
for (const auto & table : tables)
|
|
|
|
{
|
|
|
|
String table_name = table.table.getQualifiedNamePrefix(false);
|
|
|
|
NamesAndTypesList columns = table.columns;
|
|
|
|
tables_order.push_back(table_name);
|
2022-12-30 15:12:18 +00:00
|
|
|
table_name_alias.emplace(table.table.table /* table_name */, table_name /* alias_name */);
|
2020-03-20 00:58:20 +00:00
|
|
|
table_columns.emplace(std::move(table_name), std::move(columns));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 13:23:42 +00:00
|
|
|
using ShouldAddColumnPredicate = std::function<bool (const String&)>;
|
2021-04-22 10:14:35 +00:00
|
|
|
|
2021-04-22 13:23:42 +00:00
|
|
|
/// Add columns from table with table_name into select expression list
|
|
|
|
/// Use should_add_column_predicate for check if column name should be added
|
2021-04-22 14:19:47 +00:00
|
|
|
/// By default should_add_column_predicate returns true for any column name
|
2021-04-22 13:23:42 +00:00
|
|
|
void addTableColumns(
|
|
|
|
const String & table_name,
|
2022-04-19 17:22:04 +00:00
|
|
|
ASTs & columns,
|
2021-04-22 14:19:47 +00:00
|
|
|
ShouldAddColumnPredicate should_add_column_predicate = [](const String &) { return true; })
|
2019-04-04 12:14:10 +00:00
|
|
|
{
|
2022-12-30 15:12:18 +00:00
|
|
|
String name = table_name;
|
|
|
|
auto it = table_columns.find(name);
|
2019-04-04 12:14:10 +00:00
|
|
|
if (it == table_columns.end())
|
2023-01-02 08:40:49 +00:00
|
|
|
{
|
|
|
|
auto table_name_it = table_name_alias.find(table_name);
|
|
|
|
if (table_name_it != table_name_alias.end())
|
|
|
|
{
|
|
|
|
name = table_name_it->second;
|
|
|
|
it = table_columns.find(table_name_it->second);
|
|
|
|
if (it == table_columns.end())
|
|
|
|
throw Exception("Unknown qualified identifier: " + table_name, ErrorCodes::UNKNOWN_IDENTIFIER);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Unknown qualified identifier: " + table_name, ErrorCodes::UNKNOWN_IDENTIFIER);
|
|
|
|
}
|
2019-04-04 12:14:10 +00:00
|
|
|
|
|
|
|
for (const auto & column : it->second)
|
2021-04-22 10:14:35 +00:00
|
|
|
{
|
2021-04-22 13:23:42 +00:00
|
|
|
if (should_add_column_predicate(column.name))
|
2021-04-22 10:14:35 +00:00
|
|
|
{
|
2021-06-09 10:13:13 +00:00
|
|
|
ASTPtr identifier;
|
|
|
|
if (it->first.empty())
|
|
|
|
/// We want tables from JOIN to have aliases.
|
|
|
|
/// But it is possible to set joined_subquery_requires_alias = 0,
|
|
|
|
/// and write a query like `select * FROM (SELECT 1), (SELECT 1), (SELECT 1)`.
|
|
|
|
/// If so, table name will be empty here.
|
|
|
|
///
|
|
|
|
/// We cannot create compound identifier with empty part (there is an assert).
|
|
|
|
/// So, try our luck and use only column name.
|
|
|
|
/// (Rewriting AST for JOIN is not an efficient design).
|
|
|
|
identifier = std::make_shared<ASTIdentifier>(column.name);
|
|
|
|
else
|
|
|
|
identifier = std::make_shared<ASTIdentifier>(std::vector<String>{it->first, column.name});
|
|
|
|
|
2022-04-19 17:22:04 +00:00
|
|
|
columns.emplace_back(std::move(identifier));
|
2021-04-22 10:14:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 12:14:10 +00:00
|
|
|
}
|
2021-04-22 10:14:35 +00:00
|
|
|
|
2019-04-03 16:06:05 +00:00
|
|
|
};
|
|
|
|
|
2019-10-21 16:22:54 +00:00
|
|
|
static bool needChildVisit(const ASTPtr &, const ASTPtr &) { return false; }
|
2019-04-03 16:06:05 +00:00
|
|
|
|
2019-10-21 16:22:54 +00:00
|
|
|
static void visit(const ASTPtr & ast, Data & data)
|
2019-04-03 16:06:05 +00:00
|
|
|
{
|
|
|
|
if (auto * t = ast->as<ASTExpressionList>())
|
|
|
|
visit(*t, ast, data);
|
|
|
|
}
|
|
|
|
|
2019-04-04 12:14:10 +00:00
|
|
|
private:
|
2019-10-21 16:22:54 +00:00
|
|
|
static void visit(const ASTExpressionList & node, const ASTPtr &, Data & data)
|
2019-04-03 16:06:05 +00:00
|
|
|
{
|
|
|
|
bool has_asterisks = false;
|
|
|
|
data.new_select_expression_list = std::make_shared<ASTExpressionList>();
|
|
|
|
data.new_select_expression_list->children.reserve(node.children.size());
|
|
|
|
|
2020-04-22 06:01:33 +00:00
|
|
|
for (const auto & child : node.children)
|
2019-04-03 16:06:05 +00:00
|
|
|
{
|
2022-04-19 17:22:04 +00:00
|
|
|
ASTs columns;
|
|
|
|
if (const auto * asterisk = child->as<ASTAsterisk>())
|
2019-04-03 16:06:05 +00:00
|
|
|
{
|
|
|
|
has_asterisks = true;
|
|
|
|
|
2019-04-04 12:14:10 +00:00
|
|
|
for (auto & table_name : data.tables_order)
|
2022-04-19 17:22:04 +00:00
|
|
|
data.addTableColumns(table_name, columns);
|
|
|
|
|
2022-11-30 02:14:04 +00:00
|
|
|
if (asterisk->transformers)
|
|
|
|
{
|
|
|
|
for (const auto & transformer : asterisk->transformers->children)
|
|
|
|
IASTColumnsTransformer::transform(transformer, columns);
|
|
|
|
}
|
2019-04-03 16:06:05 +00:00
|
|
|
}
|
2022-04-19 17:22:04 +00:00
|
|
|
else if (const auto * qualified_asterisk = child->as<ASTQualifiedAsterisk>())
|
2019-04-03 16:06:05 +00:00
|
|
|
{
|
|
|
|
has_asterisks = true;
|
|
|
|
|
2022-11-02 06:11:37 +00:00
|
|
|
if (!qualified_asterisk->qualifier)
|
|
|
|
throw Exception("Logical error: qualified asterisk must have a qualifier", ErrorCodes::LOGICAL_ERROR);
|
2023-01-13 16:35:36 +00:00
|
|
|
|
2022-11-02 06:11:37 +00:00
|
|
|
auto & identifier = qualified_asterisk->qualifier->as<ASTIdentifier &>();
|
2019-04-03 16:06:05 +00:00
|
|
|
|
2022-04-19 17:22:04 +00:00
|
|
|
data.addTableColumns(identifier.name(), columns);
|
|
|
|
|
2022-11-30 02:14:04 +00:00
|
|
|
if (qualified_asterisk->transformers)
|
|
|
|
{
|
|
|
|
for (const auto & transformer : qualified_asterisk->transformers->children)
|
2023-01-13 16:35:36 +00:00
|
|
|
{
|
|
|
|
if (transformer.get()->as<ASTColumnsApplyTransformer>() ||
|
|
|
|
transformer.get()->as<ASTColumnsExceptTransformer>() ||
|
|
|
|
transformer.get()->as<ASTColumnsReplaceTransformer>())
|
|
|
|
IASTColumnsTransformer::transform(transformer, columns);
|
|
|
|
else
|
|
|
|
throw Exception("Logical error: qualified asterisk must only have children of IASTColumnsTransformer type", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
2022-11-30 02:14:04 +00:00
|
|
|
}
|
2019-04-03 16:06:05 +00:00
|
|
|
}
|
2022-04-19 17:22:04 +00:00
|
|
|
else if (const auto * columns_list_matcher = child->as<ASTColumnsListMatcher>())
|
|
|
|
{
|
|
|
|
has_asterisks = true;
|
|
|
|
|
|
|
|
for (const auto & ident : columns_list_matcher->column_list->children)
|
|
|
|
columns.emplace_back(ident->clone());
|
|
|
|
|
2022-11-30 02:14:04 +00:00
|
|
|
if (columns_list_matcher->transformers)
|
|
|
|
{
|
|
|
|
for (const auto & transformer : columns_list_matcher->transformers->children)
|
|
|
|
IASTColumnsTransformer::transform(transformer, columns);
|
|
|
|
}
|
2022-04-19 17:22:04 +00:00
|
|
|
}
|
|
|
|
else if (const auto * columns_regexp_matcher = child->as<ASTColumnsRegexpMatcher>())
|
2021-04-22 10:14:35 +00:00
|
|
|
{
|
|
|
|
has_asterisks = true;
|
|
|
|
|
|
|
|
for (auto & table_name : data.tables_order)
|
2022-04-19 17:22:04 +00:00
|
|
|
data.addTableColumns(
|
|
|
|
table_name,
|
|
|
|
columns,
|
|
|
|
[&](const String & column_name) { return columns_regexp_matcher->isColumnMatching(column_name); });
|
|
|
|
|
2022-11-30 02:14:04 +00:00
|
|
|
if (columns_regexp_matcher->transformers)
|
|
|
|
{
|
|
|
|
for (const auto & transformer : columns_regexp_matcher->transformers->children)
|
|
|
|
IASTColumnsTransformer::transform(transformer, columns);
|
|
|
|
}
|
2021-04-22 10:14:35 +00:00
|
|
|
}
|
2019-04-03 16:06:05 +00:00
|
|
|
else
|
|
|
|
data.new_select_expression_list->children.push_back(child);
|
2022-04-19 17:22:04 +00:00
|
|
|
|
|
|
|
data.new_select_expression_list->children.insert(
|
|
|
|
data.new_select_expression_list->children.end(),
|
|
|
|
std::make_move_iterator(columns.begin()),
|
|
|
|
std::make_move_iterator(columns.end()));
|
2019-04-03 16:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_asterisks)
|
|
|
|
data.new_select_expression_list.reset();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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;
|
2022-10-13 19:50:11 +00:00
|
|
|
ASTs new_tables{left, right};
|
2019-02-20 12:12:36 +00:00
|
|
|
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;
|
|
|
|
|
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 &>();
|
2022-07-29 16:30:50 +00:00
|
|
|
if (join.kind == JoinKind::Comma)
|
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-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_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-10-21 16:22:54 +00:00
|
|
|
using ExtractAsterisksVisitor = ConstInDepthNodeVisitor<ExtractAsterisksMatcher, true>;
|
2019-02-20 12:12:36 +00:00
|
|
|
|
2020-03-18 21:38:27 +00:00
|
|
|
/// V2 specific visitors
|
|
|
|
|
|
|
|
struct CollectColumnIdentifiersMatcher
|
|
|
|
{
|
2020-06-16 16:32:32 +00:00
|
|
|
using Visitor = ConstInDepthNodeVisitor<CollectColumnIdentifiersMatcher, true>;
|
|
|
|
|
|
|
|
struct Data
|
|
|
|
{
|
|
|
|
std::vector<ASTIdentifier *> & identifiers;
|
|
|
|
std::vector<std::unordered_set<String>> ignored;
|
|
|
|
|
|
|
|
explicit Data(std::vector<ASTIdentifier *> & identifiers_)
|
|
|
|
: identifiers(identifiers_)
|
|
|
|
{}
|
|
|
|
|
2021-04-22 10:14:35 +00:00
|
|
|
void addIdentifier(const ASTIdentifier & ident)
|
2020-06-16 16:32:32 +00:00
|
|
|
{
|
|
|
|
for (const auto & aliases : ignored)
|
2022-04-18 10:18:43 +00:00
|
|
|
if (aliases.contains(ident.name()))
|
2020-06-16 16:32:32 +00:00
|
|
|
return;
|
|
|
|
identifiers.push_back(const_cast<ASTIdentifier *>(&ident));
|
|
|
|
}
|
|
|
|
|
|
|
|
void pushIgnored(const Names & names)
|
|
|
|
{
|
|
|
|
ignored.emplace_back(std::unordered_set<String>(names.begin(), names.end()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void popIgnored()
|
|
|
|
{
|
|
|
|
ignored.pop_back();
|
|
|
|
}
|
|
|
|
};
|
2020-03-18 21:38:27 +00:00
|
|
|
|
|
|
|
static bool needChildVisit(const ASTPtr & node, const ASTPtr &)
|
|
|
|
{
|
2020-06-16 16:32:32 +00:00
|
|
|
/// "lambda" visit children itself.
|
|
|
|
if (const auto * f = node->as<ASTFunction>())
|
|
|
|
if (f->name == "lambda")
|
|
|
|
return false;
|
|
|
|
|
2020-03-20 00:58:20 +00:00
|
|
|
/// Do not go into subqueries. Do not collect table identifiers. Do not get identifier from 't.*'.
|
2020-03-18 21:38:27 +00:00
|
|
|
return !node->as<ASTSubquery>() &&
|
2020-03-20 00:58:20 +00:00
|
|
|
!node->as<ASTTablesInSelectQuery>() &&
|
|
|
|
!node->as<ASTQualifiedAsterisk>();
|
2020-03-18 21:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void visit(const ASTPtr & ast, Data & data)
|
|
|
|
{
|
|
|
|
if (auto * t = ast->as<ASTIdentifier>())
|
|
|
|
visit(*t, ast, data);
|
2020-06-16 16:32:32 +00:00
|
|
|
else if (auto * f = ast->as<ASTFunction>())
|
|
|
|
visit(*f, ast, data);
|
2020-03-18 21:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void visit(const ASTIdentifier & ident, const ASTPtr &, Data & data)
|
|
|
|
{
|
2021-04-22 10:14:35 +00:00
|
|
|
data.addIdentifier(ident);
|
2020-06-16 16:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void visit(const ASTFunction & func, const ASTPtr &, Data & data)
|
|
|
|
{
|
|
|
|
if (func.name == "lambda")
|
|
|
|
{
|
|
|
|
data.pushIgnored(RequiredSourceColumnsMatcher::extractNamesFromLambda(func));
|
|
|
|
|
|
|
|
Visitor(data).visit(func.arguments->children[1]);
|
|
|
|
|
|
|
|
data.popIgnored();
|
|
|
|
}
|
2020-03-18 21:38:27 +00:00
|
|
|
}
|
|
|
|
};
|
2020-06-16 16:32:32 +00:00
|
|
|
using CollectColumnIdentifiersVisitor = CollectColumnIdentifiersMatcher::Visitor;
|
2020-03-20 00:58:20 +00:00
|
|
|
|
|
|
|
struct CheckAliasDependencyVisitorData
|
|
|
|
{
|
|
|
|
using TypeToVisit = ASTIdentifier;
|
|
|
|
|
|
|
|
const Aliases & aliases;
|
|
|
|
const ASTIdentifier * dependency = nullptr;
|
|
|
|
|
|
|
|
void visit(ASTIdentifier & ident, ASTPtr &)
|
|
|
|
{
|
2022-04-18 10:18:43 +00:00
|
|
|
if (!dependency && aliases.contains(ident.name()))
|
2020-03-20 00:58:20 +00:00
|
|
|
dependency = &ident;
|
|
|
|
}
|
|
|
|
};
|
2022-06-16 15:50:03 +00:00
|
|
|
|
2020-03-20 00:58:20 +00:00
|
|
|
using CheckAliasDependencyMatcher = OneTypeMatcher<CheckAliasDependencyVisitorData>;
|
|
|
|
using CheckAliasDependencyVisitor = InDepthNodeVisitor<CheckAliasDependencyMatcher, true>;
|
|
|
|
|
|
|
|
struct RewriteWithAliasMatcher
|
|
|
|
{
|
|
|
|
using Data = std::unordered_map<String, ASTPtr>;
|
|
|
|
|
|
|
|
static bool needChildVisit(const ASTPtr & node, const ASTPtr &)
|
|
|
|
{
|
|
|
|
return !node->as<ASTSubquery>();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void visit(ASTPtr & ast, Data & data)
|
|
|
|
{
|
|
|
|
String alias = ast->tryGetAlias();
|
|
|
|
if (!alias.empty())
|
|
|
|
{
|
|
|
|
auto it = data.find(alias);
|
|
|
|
if (it != data.end() && it->second.get() == ast.get())
|
|
|
|
ast = std::make_shared<ASTIdentifier>(alias);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
using RewriteWithAliasVisitor = InDepthNodeVisitor<RewriteWithAliasMatcher, true>;
|
|
|
|
|
|
|
|
class SubqueryExpressionsRewriteMatcher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct Data
|
|
|
|
{
|
|
|
|
ASTPtr expression_list;
|
|
|
|
bool done = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool needChildVisit(ASTPtr & node, ASTPtr &)
|
|
|
|
{
|
|
|
|
return !node->as<ASTSelectQuery>();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void visit(ASTPtr & ast, Data & data)
|
|
|
|
{
|
|
|
|
if (auto * t = ast->as<ASTSelectQuery>())
|
|
|
|
visit(*t, ast, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void visit(ASTSelectQuery & select, ASTPtr &, Data & data)
|
|
|
|
{
|
|
|
|
if (!data.done)
|
2022-04-07 11:32:32 +00:00
|
|
|
{
|
|
|
|
if (data.expression_list->children.empty())
|
2022-11-30 02:14:04 +00:00
|
|
|
data.expression_list->children.emplace_back(std::make_shared<ASTAsterisk>());
|
2022-11-02 06:11:37 +00:00
|
|
|
|
2020-03-20 00:58:20 +00:00
|
|
|
select.setExpression(ASTSelectQuery::Expression::SELECT, std::move(data.expression_list));
|
2022-04-07 11:32:32 +00:00
|
|
|
}
|
2020-03-20 00:58:20 +00:00
|
|
|
data.done = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
using SubqueryExpressionsRewriteVisitor = InDepthNodeVisitor<SubqueryExpressionsRewriteMatcher, true>;
|
2020-03-18 21:38:27 +00:00
|
|
|
|
|
|
|
struct TableNeededColumns
|
|
|
|
{
|
|
|
|
const DatabaseAndTableWithAlias & table;
|
|
|
|
NameSet no_clashes = {};
|
2020-03-20 00:58:20 +00:00
|
|
|
NameSet alias_clashes = {};
|
|
|
|
std::unordered_map<String, String> column_clashes = {};
|
2020-03-18 21:38:27 +00:00
|
|
|
|
|
|
|
void fillExpressionList(ASTExpressionList & expression_list) const
|
|
|
|
{
|
|
|
|
size_t columns_count = no_clashes.size() + column_clashes.size() + alias_clashes.size();
|
|
|
|
expression_list.children.reserve(expression_list.children.size() + columns_count);
|
|
|
|
|
|
|
|
String table_name = table.getQualifiedNamePrefix(false);
|
|
|
|
|
2020-04-22 06:01:33 +00:00
|
|
|
for (const auto & column : no_clashes)
|
2020-03-18 21:38:27 +00:00
|
|
|
addShortName(column, expression_list);
|
|
|
|
|
2020-04-22 06:01:33 +00:00
|
|
|
for (const auto & column : alias_clashes)
|
2020-03-18 21:38:27 +00:00
|
|
|
addShortName(column, expression_list);
|
2020-03-20 00:58:20 +00:00
|
|
|
|
2020-04-22 06:01:33 +00:00
|
|
|
for (const auto & [column, alias] : column_clashes)
|
2020-03-20 00:58:20 +00:00
|
|
|
addAliasedName(table_name, column, alias, expression_list);
|
2020-03-18 21:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void addShortName(const String & column, ASTExpressionList & expression_list)
|
|
|
|
{
|
|
|
|
auto ident = std::make_shared<ASTIdentifier>(column);
|
|
|
|
expression_list.children.emplace_back(std::move(ident));
|
|
|
|
}
|
|
|
|
|
2020-03-20 00:58:20 +00:00
|
|
|
/// t.x as `some`
|
|
|
|
static void addAliasedName(const String & table, const String & column, const String & alias, ASTExpressionList & expression_list)
|
2020-03-18 21:38:27 +00:00
|
|
|
{
|
|
|
|
auto ident = std::make_shared<ASTIdentifier>(std::vector<String>{table, column});
|
2020-03-20 00:58:20 +00:00
|
|
|
ident->setAlias(alias);
|
2020-03-18 21:38:27 +00:00
|
|
|
expression_list.children.emplace_back(std::move(ident));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-20 00:58:20 +00:00
|
|
|
class UniqueShortNames
|
2020-03-18 21:38:27 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-03-20 11:31:16 +00:00
|
|
|
/// We know that long names are unique (do not clashes with others).
|
|
|
|
/// So we could make unique names base on this knolage by adding some unused prefix.
|
|
|
|
static constexpr const char * pattern = "--";
|
|
|
|
|
|
|
|
String longToShort(const String & long_name)
|
2020-03-18 21:38:27 +00:00
|
|
|
{
|
2020-03-20 11:31:16 +00:00
|
|
|
auto it = long_to_short.find(long_name);
|
|
|
|
if (it != long_to_short.end())
|
2020-03-20 00:58:20 +00:00
|
|
|
return it->second;
|
2020-03-18 21:38:27 +00:00
|
|
|
|
2020-03-20 11:31:16 +00:00
|
|
|
String short_name = generateUniqueName(long_name);
|
|
|
|
long_to_short.emplace(long_name, short_name);
|
|
|
|
return short_name;
|
2020-03-18 21:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-03-20 11:31:16 +00:00
|
|
|
std::unordered_map<String, String> long_to_short;
|
2020-03-20 00:58:20 +00:00
|
|
|
|
2020-03-23 02:12:31 +00:00
|
|
|
static String generateUniqueName(const String & long_name)
|
2020-03-18 21:38:27 +00:00
|
|
|
{
|
2020-03-20 11:31:16 +00:00
|
|
|
return String(pattern) + long_name;
|
2020-03-18 21:38:27 +00:00
|
|
|
}
|
2020-03-20 00:58:20 +00:00
|
|
|
};
|
|
|
|
|
2020-03-20 12:09:53 +00:00
|
|
|
size_t countTablesWithColumn(const std::vector<TableWithColumnNamesAndTypes> & tables, const String & short_name)
|
2020-03-20 00:58:20 +00:00
|
|
|
{
|
|
|
|
size_t count = 0;
|
2020-04-22 06:01:33 +00:00
|
|
|
for (const auto & table : tables)
|
2020-03-20 00:58:20 +00:00
|
|
|
if (table.hasColumn(short_name))
|
|
|
|
++count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2020-03-20 11:31:16 +00:00
|
|
|
/// 'select `--t.x`, `--t.x`, ...' -> 'select `--t.x` as `t.x`, `t.x`, ...'
|
|
|
|
void restoreName(ASTIdentifier & ident, const String & original_name, NameSet & restored_names)
|
|
|
|
{
|
|
|
|
if (!ident.tryGetAlias().empty())
|
|
|
|
return;
|
2022-06-16 15:50:03 +00:00
|
|
|
|
2020-03-20 11:31:16 +00:00
|
|
|
if (original_name.empty())
|
|
|
|
return;
|
|
|
|
|
2022-04-18 10:18:43 +00:00
|
|
|
if (!restored_names.contains(original_name))
|
2020-03-20 11:31:16 +00:00
|
|
|
{
|
|
|
|
ident.setAlias(original_name);
|
|
|
|
restored_names.emplace(original_name);
|
|
|
|
}
|
|
|
|
else
|
2022-06-16 15:50:03 +00:00
|
|
|
{
|
2020-03-20 11:31:16 +00:00
|
|
|
ident.setShortName(original_name);
|
2022-06-16 15:50:03 +00:00
|
|
|
}
|
2020-03-20 11:31:16 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 00:58:20 +00:00
|
|
|
/// Find clashes and normalize names
|
|
|
|
/// 1. If column name has no clashes make all its occurrences short: 'table.column' -> 'column', 'table_alias.column' -> 'column'.
|
|
|
|
/// 2. If column name can't be short cause of alias with same name generate and use unique name for it.
|
|
|
|
/// 3. If column clashes with another column generate and use unique names for them.
|
|
|
|
/// 4. If column clashes with another column and it's short - it's 'ambiguous column' error.
|
|
|
|
/// 5. If column clashes with alias add short column name to select list. It would be removed later if not needed.
|
|
|
|
std::vector<TableNeededColumns> normalizeColumnNamesExtractNeeded(
|
|
|
|
const std::vector<TableWithColumnNamesAndTypes> & tables,
|
|
|
|
const Aliases & aliases,
|
2020-03-20 11:31:16 +00:00
|
|
|
const std::vector<ASTIdentifier *> & identifiers,
|
|
|
|
const std::unordered_set<ASTIdentifier *> & public_identifiers,
|
|
|
|
UniqueShortNames & unique_names)
|
2020-03-20 00:58:20 +00:00
|
|
|
{
|
|
|
|
size_t last_table_pos = tables.size() - 1;
|
|
|
|
|
|
|
|
std::vector<TableNeededColumns> needed_columns;
|
|
|
|
needed_columns.reserve(tables.size());
|
2020-04-22 06:01:33 +00:00
|
|
|
for (const auto & table : tables)
|
2020-03-20 00:58:20 +00:00
|
|
|
needed_columns.push_back(TableNeededColumns{table.table});
|
2020-03-18 21:38:27 +00:00
|
|
|
|
2022-06-16 15:50:03 +00:00
|
|
|
NameSet restored_names;
|
2020-03-20 00:58:20 +00:00
|
|
|
for (ASTIdentifier * ident : identifiers)
|
2020-03-18 21:38:27 +00:00
|
|
|
{
|
2022-04-18 10:18:43 +00:00
|
|
|
bool got_alias = aliases.contains(ident->name());
|
2020-06-15 12:36:10 +00:00
|
|
|
bool allow_ambiguous = got_alias; /// allow ambiguous column overridden by an alias
|
2020-03-20 00:58:20 +00:00
|
|
|
|
2020-06-15 12:36:10 +00:00
|
|
|
if (auto table_pos = IdentifierSemantic::chooseTableColumnMatch(*ident, tables, allow_ambiguous))
|
2020-03-20 00:58:20 +00:00
|
|
|
{
|
|
|
|
if (!ident->isShort())
|
|
|
|
{
|
|
|
|
if (got_alias)
|
2021-01-20 15:34:17 +00:00
|
|
|
{
|
|
|
|
auto alias = aliases.find(ident->name())->second;
|
2021-05-31 15:00:04 +00:00
|
|
|
auto alias_ident = alias->clone();
|
2022-11-04 02:04:01 +00:00
|
|
|
if (auto * alias_ident_typed = alias_ident->as<ASTIdentifier>())
|
2022-11-02 03:34:15 +00:00
|
|
|
{
|
2022-11-04 02:04:01 +00:00
|
|
|
alias_ident_typed->restoreTable();
|
2022-11-02 03:34:15 +00:00
|
|
|
bool alias_equals_column_name = alias_ident->getColumnNameWithoutAlias() == ident->getColumnNameWithoutAlias();
|
|
|
|
if (!alias_equals_column_name)
|
|
|
|
throw Exception("Alias clashes with qualified column '" + ident->name() + "'", ErrorCodes::AMBIGUOUS_COLUMN_NAME);
|
|
|
|
}
|
2021-01-20 15:34:17 +00:00
|
|
|
}
|
2020-03-20 00:58:20 +00:00
|
|
|
String short_name = ident->shortName();
|
2020-03-20 11:31:16 +00:00
|
|
|
String original_long_name;
|
2022-04-18 10:18:43 +00:00
|
|
|
if (public_identifiers.contains(ident))
|
2020-10-24 18:46:10 +00:00
|
|
|
original_long_name = ident->name();
|
2020-03-20 11:31:16 +00:00
|
|
|
|
2020-03-20 12:09:53 +00:00
|
|
|
size_t count = countTablesWithColumn(tables, short_name);
|
2022-11-23 16:35:12 +00:00
|
|
|
const auto & table = tables[*table_pos];
|
2020-03-20 00:58:20 +00:00
|
|
|
|
2021-10-15 10:53:25 +00:00
|
|
|
/// isValidIdentifierBegin retuired to be consistent with TableJoin::deduplicateAndQualifyColumnNames
|
2022-04-18 10:18:43 +00:00
|
|
|
if (count > 1 || aliases.contains(short_name) || !isValidIdentifierBegin(short_name.at(0)))
|
2020-03-20 00:58:20 +00:00
|
|
|
{
|
|
|
|
IdentifierSemantic::setColumnLongName(*ident, table.table); /// table.column -> table_alias.column
|
2020-10-24 18:46:10 +00:00
|
|
|
const auto & unique_long_name = ident->name();
|
2020-03-20 00:58:20 +00:00
|
|
|
|
|
|
|
/// For tables moved into subselects we need unique short names for clashed names
|
|
|
|
if (*table_pos != last_table_pos)
|
|
|
|
{
|
2020-03-20 11:31:16 +00:00
|
|
|
String unique_short_name = unique_names.longToShort(unique_long_name);
|
2020-03-20 00:58:20 +00:00
|
|
|
ident->setShortName(unique_short_name);
|
|
|
|
needed_columns[*table_pos].column_clashes.emplace(short_name, unique_short_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-23 16:35:12 +00:00
|
|
|
if (!table.hasColumn(short_name))
|
|
|
|
{
|
|
|
|
throw Exception(ErrorCodes::UNKNOWN_IDENTIFIER,
|
|
|
|
"There's no column '{}' in table '{}'",
|
|
|
|
ident->name(),
|
|
|
|
table.table.getQualifiedNamePrefix(false));
|
|
|
|
}
|
2020-03-20 00:58:20 +00:00
|
|
|
ident->setShortName(short_name); /// table.column -> column
|
2020-03-20 11:31:16 +00:00
|
|
|
needed_columns[*table_pos].no_clashes.emplace(short_name);
|
2020-03-20 00:58:20 +00:00
|
|
|
}
|
2020-03-20 11:31:16 +00:00
|
|
|
|
|
|
|
restoreName(*ident, original_long_name, restored_names);
|
2020-03-20 00:58:20 +00:00
|
|
|
}
|
|
|
|
else if (got_alias)
|
|
|
|
needed_columns[*table_pos].alias_clashes.emplace(ident->shortName());
|
|
|
|
else
|
|
|
|
needed_columns[*table_pos].no_clashes.emplace(ident->shortName());
|
|
|
|
}
|
2020-03-18 21:38:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 00:58:20 +00:00
|
|
|
return needed_columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Make expression list for current subselect
|
|
|
|
std::shared_ptr<ASTExpressionList> subqueryExpressionList(
|
|
|
|
size_t table_pos,
|
|
|
|
const std::vector<TableNeededColumns> & needed_columns,
|
|
|
|
const std::vector<std::vector<ASTPtr>> & alias_pushdown)
|
|
|
|
{
|
|
|
|
auto expression_list = std::make_shared<ASTExpressionList>();
|
|
|
|
|
|
|
|
/// First time extract needed left table columns manually.
|
|
|
|
/// Next times extract left table columns via QualifiedAsterisk: `--s`.*
|
|
|
|
if (table_pos == 1)
|
|
|
|
needed_columns[0].fillExpressionList(*expression_list);
|
|
|
|
else
|
|
|
|
expression_list->children.emplace_back(makeSubqueryQualifiedAsterisk());
|
|
|
|
|
|
|
|
/// Add needed right table columns
|
|
|
|
needed_columns[table_pos].fillExpressionList(*expression_list);
|
|
|
|
|
2020-04-22 06:01:33 +00:00
|
|
|
for (const auto & expr : alias_pushdown[table_pos])
|
2022-03-02 17:22:12 +00:00
|
|
|
expression_list->children.emplace_back(expr);
|
2020-03-20 00:58:20 +00:00
|
|
|
|
|
|
|
return expression_list;
|
|
|
|
}
|
2020-03-18 21:38:27 +00:00
|
|
|
|
2019-02-20 12:12:36 +00:00
|
|
|
} /// namelesspace
|
2018-12-17 16:20:15 +00:00
|
|
|
|
|
|
|
|
2020-03-23 22:23:31 +00:00
|
|
|
bool JoinToSubqueryTransformMatcher::needChildVisit(ASTPtr & node, const ASTPtr &)
|
|
|
|
{
|
|
|
|
return !node->as<ASTSubquery>();
|
|
|
|
}
|
|
|
|
|
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>())
|
2020-09-30 20:11:49 +00:00
|
|
|
visit(*t, ast, data);
|
2020-03-18 21:38:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 00:58:20 +00:00
|
|
|
/// The reason for V2: not to alias columns without clashes.
|
|
|
|
/// It allows USING and 'select *' for queries with subselects. It doesn't need AsterisksSemantic and related stuff.
|
|
|
|
/// 1. Expand asterisks in select expression list.
|
|
|
|
/// 2. Normalize column names and find name clashes
|
|
|
|
/// 3. Rewrite multiple JOINs with subqueries:
|
2020-03-20 12:21:02 +00:00
|
|
|
/// SELECT ... FROM (SELECT `--.s`.*, ... FROM (...) AS `--.s` JOIN tableY ON ...) AS `--.s` JOIN tableZ ON ...'
|
2020-03-20 00:58:20 +00:00
|
|
|
/// 4. Push down expressions of aliases used in ON section into expression list of first reletad subquery
|
2020-09-30 20:11:49 +00:00
|
|
|
void JoinToSubqueryTransformMatcher::visit(ASTSelectQuery & select, ASTPtr & ast, Data & data)
|
2020-03-18 21:38:27 +00:00
|
|
|
{
|
|
|
|
std::vector<const ASTTableExpression *> table_expressions;
|
2022-03-28 13:42:04 +00:00
|
|
|
if (!needRewrite(select, table_expressions))
|
2020-03-18 21:38:27 +00:00
|
|
|
return;
|
|
|
|
|
2020-03-20 00:58:20 +00:00
|
|
|
auto & src_tables = select.tables()->children;
|
|
|
|
size_t tables_count = src_tables.size();
|
|
|
|
|
|
|
|
if (table_expressions.size() != data.tables.size() ||
|
|
|
|
tables_count != data.tables.size())
|
|
|
|
throw Exception("Inconsistent tables count in JOIN rewriter", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
/// Replace * and t.* with columns in select expression list.
|
|
|
|
{
|
|
|
|
ExtractAsterisksVisitor::Data asterisks_data(data.tables);
|
|
|
|
ExtractAsterisksVisitor(asterisks_data).visit(select.select());
|
|
|
|
if (asterisks_data.new_select_expression_list)
|
|
|
|
select.setExpression(ASTSelectQuery::Expression::SELECT, std::move(asterisks_data.new_select_expression_list));
|
|
|
|
}
|
2020-03-18 21:38:27 +00:00
|
|
|
|
|
|
|
/// Collect column identifiers
|
|
|
|
|
|
|
|
std::vector<ASTIdentifier *> identifiers;
|
2020-06-16 16:32:32 +00:00
|
|
|
CollectColumnIdentifiersVisitor::Data data_identifiers(identifiers);
|
|
|
|
CollectColumnIdentifiersVisitor(data_identifiers).visit(ast);
|
2020-03-18 21:38:27 +00:00
|
|
|
|
2020-03-20 00:58:20 +00:00
|
|
|
std::vector<ASTIdentifier *> using_identifiers;
|
|
|
|
std::vector<std::vector<ASTPtr>> alias_pushdown(tables_count);
|
|
|
|
std::unordered_map<String, ASTPtr> on_aliases;
|
|
|
|
|
|
|
|
/// Collect columns from JOIN sections. Detect if we have aliases there (they need pushdown).
|
|
|
|
for (size_t table_pos = 0; table_pos < tables_count; ++table_pos)
|
2020-03-18 21:38:27 +00:00
|
|
|
{
|
2020-03-20 00:58:20 +00:00
|
|
|
auto * table = src_tables[table_pos]->as<ASTTablesInSelectQueryElement>();
|
2020-03-18 21:38:27 +00:00
|
|
|
if (table->table_join)
|
|
|
|
{
|
|
|
|
auto & join = table->table_join->as<ASTTableJoin &>();
|
|
|
|
if (join.on_expression)
|
|
|
|
{
|
2020-03-20 00:58:20 +00:00
|
|
|
std::vector<ASTIdentifier *> on_identifiers;
|
2020-06-16 16:32:32 +00:00
|
|
|
CollectColumnIdentifiersVisitor::Data data_on_identifiers(on_identifiers);
|
|
|
|
CollectColumnIdentifiersVisitor(data_on_identifiers).visit(join.on_expression);
|
2020-03-20 00:58:20 +00:00
|
|
|
identifiers.insert(identifiers.end(), on_identifiers.begin(), on_identifiers.end());
|
2020-03-18 21:38:27 +00:00
|
|
|
|
2020-03-20 00:58:20 +00:00
|
|
|
/// Extract aliases used in ON section for pushdown. Exclude the last table.
|
|
|
|
if (table_pos < tables_count - 1)
|
2020-03-18 21:38:27 +00:00
|
|
|
{
|
2020-03-20 00:58:20 +00:00
|
|
|
for (auto * ident : on_identifiers)
|
|
|
|
{
|
2020-10-24 18:46:10 +00:00
|
|
|
auto it = data.aliases.find(ident->name());
|
2022-04-18 10:18:43 +00:00
|
|
|
if (!on_aliases.contains(ident->name()) && it != data.aliases.end())
|
2020-03-20 00:58:20 +00:00
|
|
|
{
|
|
|
|
auto alias_expression = it->second;
|
|
|
|
alias_pushdown[table_pos].push_back(alias_expression);
|
2020-10-24 18:46:10 +00:00
|
|
|
on_aliases[ident->name()] = alias_expression;
|
2020-03-20 00:58:20 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-18 21:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 00:58:20 +00:00
|
|
|
else if (join.using_expression_list)
|
2020-06-16 16:32:32 +00:00
|
|
|
{
|
|
|
|
CollectColumnIdentifiersVisitor::Data data_using_identifiers(using_identifiers);
|
|
|
|
CollectColumnIdentifiersVisitor(data_using_identifiers).visit(join.using_expression_list);
|
|
|
|
}
|
2020-03-18 21:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 00:58:20 +00:00
|
|
|
/// Check if alias expression is too complex to push it down.
|
|
|
|
for (auto & expr : on_aliases)
|
|
|
|
{
|
|
|
|
CheckAliasDependencyVisitor::Data check{data.aliases};
|
|
|
|
CheckAliasDependencyVisitor(check).visit(expr.second);
|
|
|
|
if (check.dependency)
|
|
|
|
throw Exception("Cannot rewrite JOINs. Alias '" + expr.first +
|
2020-10-24 18:46:10 +00:00
|
|
|
"' used in ON section depends on another alias '" + check.dependency->name() + "'",
|
2020-03-20 00:58:20 +00:00
|
|
|
ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check same name in aliases, USING and ON sections. Cannot push down alias to ON through USING cause of name masquerading.
|
|
|
|
for (auto * ident : using_identifiers)
|
2022-04-18 10:18:43 +00:00
|
|
|
if (on_aliases.contains(ident->name()))
|
2020-10-24 18:46:10 +00:00
|
|
|
throw Exception("Cannot rewrite JOINs. Alias '" + ident->name() + "' appears both in ON and USING", ErrorCodes::NOT_IMPLEMENTED);
|
2020-03-20 00:58:20 +00:00
|
|
|
using_identifiers.clear();
|
|
|
|
|
|
|
|
/// Replace pushdowned expressions with aliases names in original expression lists.
|
|
|
|
RewriteWithAliasVisitor(on_aliases).visit(ast);
|
|
|
|
on_aliases.clear();
|
|
|
|
|
2020-03-20 11:31:16 +00:00
|
|
|
/// We need to know if identifier is public. If so we have too keep its output name.
|
|
|
|
std::unordered_set<ASTIdentifier *> public_identifiers;
|
|
|
|
for (auto & top_level_child : select.select()->children)
|
|
|
|
if (auto * ident = top_level_child->as<ASTIdentifier>())
|
2022-06-16 15:50:03 +00:00
|
|
|
{
|
|
|
|
if (!data.try_to_keep_original_names || startsWith(ident->name(), UniqueShortNames::pattern))
|
|
|
|
public_identifiers.insert(ident);
|
|
|
|
}
|
2020-03-20 11:31:16 +00:00
|
|
|
|
|
|
|
UniqueShortNames unique_names;
|
2020-03-20 00:58:20 +00:00
|
|
|
std::vector<TableNeededColumns> needed_columns =
|
2020-03-20 11:31:16 +00:00
|
|
|
normalizeColumnNamesExtractNeeded(data.tables, data.aliases, identifiers, public_identifiers, unique_names);
|
2020-03-20 00:58:20 +00:00
|
|
|
|
|
|
|
/// Rewrite JOINs with subselects
|
2020-03-18 21:38:27 +00:00
|
|
|
|
|
|
|
ASTPtr left_table = src_tables[0];
|
|
|
|
|
|
|
|
static ASTPtr subquery_template = makeSubqueryTemplate();
|
|
|
|
|
|
|
|
for (size_t i = 1; i < src_tables.size() - 1; ++i)
|
|
|
|
{
|
2020-03-20 00:58:20 +00:00
|
|
|
auto expression_list = subqueryExpressionList(i, needed_columns, alias_pushdown);
|
2020-03-18 21:38:27 +00:00
|
|
|
|
|
|
|
ASTPtr subquery = subquery_template->clone();
|
2020-03-20 00:58:20 +00:00
|
|
|
SubqueryExpressionsRewriteVisitor::Data expr_rewrite_data{std::move(expression_list)};
|
2020-03-18 21:38:27 +00:00
|
|
|
SubqueryExpressionsRewriteVisitor(expr_rewrite_data).visit(subquery);
|
|
|
|
|
|
|
|
left_table = replaceJoin(left_table, src_tables[i], subquery);
|
|
|
|
}
|
|
|
|
|
|
|
|
RewriteVisitor::Data visitor_data{left_table, src_tables.back()};
|
|
|
|
RewriteVisitor(visitor_data).visit(select.refTables());
|
|
|
|
|
|
|
|
data.done = true;
|
2018-12-17 16:20:15 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 21:38:27 +00:00
|
|
|
ASTPtr JoinToSubqueryTransformMatcher::replaceJoin(ASTPtr ast_left, ASTPtr ast_right, ASTPtr subquery_template)
|
2019-02-20 12:12:36 +00:00
|
|
|
{
|
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
|
|
|
/// replace '_t' with pair of joined tables
|
2019-01-17 17:01:48 +00:00
|
|
|
RewriteVisitor::Data visitor_data{ast_left, ast_right};
|
2020-03-18 21:38:27 +00:00
|
|
|
RewriteVisitor(visitor_data).visit(subquery_template);
|
|
|
|
return subquery_template;
|
2018-12-17 16:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|