2015-10-12 07:05:54 +00:00
|
|
|
|
#include <DB/Core/FieldVisitors.h>
|
2015-08-05 21:38:31 +00:00
|
|
|
|
#include <DB/Parsers/ASTSetQuery.h>
|
2016-11-20 12:43:20 +00:00
|
|
|
|
#include <DB/Parsers/ASTFunction.h>
|
|
|
|
|
#include <DB/Parsers/ASTAsterisk.h>
|
|
|
|
|
#include <DB/Parsers/ASTIdentifier.h>
|
2015-06-02 11:16:02 +00:00
|
|
|
|
#include <DB/Parsers/ASTSelectQuery.h>
|
2016-11-20 12:43:20 +00:00
|
|
|
|
#include <DB/Parsers/ASTSubquery.h>
|
2016-07-21 15:48:11 +00:00
|
|
|
|
#include <DB/Parsers/ASTTablesInSelectQuery.h>
|
2016-11-20 12:43:20 +00:00
|
|
|
|
#include <DB/Common/typeid_cast.h>
|
2015-06-02 11:16:02 +00:00
|
|
|
|
|
2015-08-05 21:38:31 +00:00
|
|
|
|
|
2015-06-02 11:16:02 +00:00
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
|
namespace ErrorCodes
|
|
|
|
|
{
|
|
|
|
|
extern const int UNION_ALL_COLUMN_ALIAS_MISMATCH;
|
|
|
|
|
extern const int UNION_ALL_RESULT_STRUCTURES_MISMATCH;
|
|
|
|
|
extern const int UNKNOWN_IDENTIFIER;
|
2016-07-21 15:48:11 +00:00
|
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-02 11:16:02 +00:00
|
|
|
|
ASTSelectQuery::ASTSelectQuery(const StringRange range_) : ASTQueryWithOutput(range_)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ASTSelectQuery::hasArrayJoin(const ASTPtr & ast)
|
|
|
|
|
{
|
|
|
|
|
if (const ASTFunction * function = typeid_cast<const ASTFunction *>(&*ast))
|
|
|
|
|
if (function->kind == ASTFunction::ARRAY_JOIN)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
for (const auto & child : ast->children)
|
|
|
|
|
if (hasArrayJoin(child))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ASTSelectQuery::hasAsterisk() const
|
|
|
|
|
{
|
|
|
|
|
for (const auto & ast : select_expression_list->children)
|
|
|
|
|
if (typeid_cast<const ASTAsterisk *>(&*ast) != nullptr)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ASTSelectQuery::renameColumns(const ASTSelectQuery & source)
|
|
|
|
|
{
|
|
|
|
|
const ASTs & from = source.select_expression_list->children;
|
|
|
|
|
ASTs & to = select_expression_list->children;
|
|
|
|
|
|
|
|
|
|
if (from.size() != to.size())
|
|
|
|
|
throw Exception("Size mismatch in UNION ALL chain",
|
|
|
|
|
DB::ErrorCodes::UNION_ALL_RESULT_STRUCTURES_MISMATCH);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < from.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
/// Если столбец имеет алиас, то он должен совпадать с названием исходного столбца.
|
|
|
|
|
/// В противном случае мы ему присваиваем алиас, если требуется.
|
|
|
|
|
if (!to[i]->tryGetAlias().empty())
|
|
|
|
|
{
|
|
|
|
|
if (to[i]->tryGetAlias() != from[i]->getAliasOrColumnName())
|
|
|
|
|
throw Exception("Column alias mismatch in UNION ALL chain",
|
|
|
|
|
DB::ErrorCodes::UNION_ALL_COLUMN_ALIAS_MISMATCH);
|
|
|
|
|
}
|
|
|
|
|
else if (to[i]->getColumnName() != from[i]->getAliasOrColumnName())
|
|
|
|
|
to[i]->setAlias(from[i]->getAliasOrColumnName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-20 17:10:00 +00:00
|
|
|
|
void ASTSelectQuery::rewriteSelectExpressionList(const Names & required_column_names)
|
2015-06-02 11:16:02 +00:00
|
|
|
|
{
|
2016-05-28 15:42:22 +00:00
|
|
|
|
ASTPtr result = std::make_shared<ASTExpressionList>();
|
2015-06-02 11:16:02 +00:00
|
|
|
|
ASTs asts = select_expression_list->children;
|
|
|
|
|
|
|
|
|
|
/// Создать отображение.
|
|
|
|
|
|
|
|
|
|
/// Элемент отображения.
|
|
|
|
|
struct Arrow
|
|
|
|
|
{
|
|
|
|
|
Arrow() = default;
|
|
|
|
|
Arrow(size_t to_position_) :
|
|
|
|
|
to_position(to_position_), is_selected(true)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
size_t to_position = 0;
|
|
|
|
|
bool is_selected = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Отображение одного SELECT выражения в другое.
|
|
|
|
|
using Mapping = std::vector<Arrow>;
|
|
|
|
|
|
|
|
|
|
Mapping mapping(asts.size());
|
2015-09-09 00:52:35 +00:00
|
|
|
|
|
|
|
|
|
/// На какой позиции в SELECT-выражении находится соответствующий столбец из column_names.
|
2016-08-20 17:10:00 +00:00
|
|
|
|
std::vector<size_t> positions_of_required_columns(required_column_names.size());
|
2015-06-02 11:16:02 +00:00
|
|
|
|
|
|
|
|
|
/// Не будем выбрасывать выражения, содержащие функцию arrayJoin.
|
|
|
|
|
for (size_t i = 0; i < asts.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
if (hasArrayJoin(asts[i]))
|
|
|
|
|
mapping[i] = Arrow(i);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-20 17:10:00 +00:00
|
|
|
|
for (size_t i = 0; i < required_column_names.size(); ++i)
|
2015-06-02 11:16:02 +00:00
|
|
|
|
{
|
2016-08-20 17:10:00 +00:00
|
|
|
|
size_t j = 0;
|
|
|
|
|
for (; j < asts.size(); ++j)
|
2015-06-02 11:16:02 +00:00
|
|
|
|
{
|
2016-08-20 17:10:00 +00:00
|
|
|
|
if (asts[j]->getAliasOrColumnName() == required_column_names[i])
|
2015-06-02 11:16:02 +00:00
|
|
|
|
{
|
2016-08-20 17:10:00 +00:00
|
|
|
|
positions_of_required_columns[i] = j;
|
2015-06-02 11:16:02 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-20 17:10:00 +00:00
|
|
|
|
if (j == asts.size())
|
2015-06-02 11:16:02 +00:00
|
|
|
|
throw Exception("Error while rewriting expression list for select query."
|
2016-08-20 17:10:00 +00:00
|
|
|
|
" Could not find alias: " + required_column_names[i],
|
2015-06-02 11:16:02 +00:00
|
|
|
|
DB::ErrorCodes::UNKNOWN_IDENTIFIER);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-20 17:10:00 +00:00
|
|
|
|
std::vector<size_t> positions_of_required_columns_in_subquery_order = positions_of_required_columns;
|
|
|
|
|
std::sort(positions_of_required_columns_in_subquery_order.begin(), positions_of_required_columns_in_subquery_order.end());
|
2015-09-09 00:52:35 +00:00
|
|
|
|
|
2016-08-20 17:10:00 +00:00
|
|
|
|
for (size_t i = 0; i < required_column_names.size(); ++i)
|
|
|
|
|
mapping[positions_of_required_columns_in_subquery_order[i]] = Arrow(positions_of_required_columns[i]);
|
2015-06-02 11:16:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Составить новое выражение.
|
|
|
|
|
for (const auto & arrow : mapping)
|
|
|
|
|
{
|
|
|
|
|
if (arrow.is_selected)
|
|
|
|
|
result->children.push_back(asts[arrow.to_position]->clone());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto & child : children)
|
|
|
|
|
{
|
|
|
|
|
if (child == select_expression_list)
|
|
|
|
|
{
|
|
|
|
|
child = result;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
select_expression_list = result;
|
|
|
|
|
|
|
|
|
|
/** NOTE: Может показаться, что мы могли испортить запрос, выбросив выражение с алиасом, который используется где-то еще.
|
|
|
|
|
* Такого произойти не может, потому что этот метод вызывается всегда для запроса, на котором хоть раз создавали
|
2015-09-09 00:52:35 +00:00
|
|
|
|
* ExpressionAnalyzer, что гарантирует, что в нем все алиасы уже подставлены. Не совсем очевидная логика.
|
2015-06-02 11:16:02 +00:00
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASTPtr ASTSelectQuery::clone() const
|
2015-07-11 00:44:45 +00:00
|
|
|
|
{
|
2015-07-11 15:16:59 +00:00
|
|
|
|
ASTPtr ptr = cloneImpl(true);
|
2015-07-11 00:44:45 +00:00
|
|
|
|
|
|
|
|
|
/// Установить указатели на предыдущие запросы SELECT.
|
|
|
|
|
ASTPtr current = ptr;
|
2015-07-13 15:02:29 +00:00
|
|
|
|
static_cast<ASTSelectQuery *>(&*current)->prev_union_all = nullptr;
|
2015-07-11 00:44:45 +00:00
|
|
|
|
ASTPtr next = static_cast<ASTSelectQuery *>(&*current)->next_union_all;
|
2016-05-28 15:46:03 +00:00
|
|
|
|
while (next != nullptr)
|
2015-07-11 00:44:45 +00:00
|
|
|
|
{
|
|
|
|
|
ASTSelectQuery * next_select_query = static_cast<ASTSelectQuery *>(&*next);
|
2015-09-08 22:13:43 +00:00
|
|
|
|
next_select_query->prev_union_all = current.get();
|
2015-07-11 00:44:45 +00:00
|
|
|
|
current = next;
|
|
|
|
|
next = next_select_query->next_union_all;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-11 15:16:59 +00:00
|
|
|
|
ASTPtr ASTSelectQuery::cloneFirstSelect() const
|
|
|
|
|
{
|
|
|
|
|
ASTPtr res = cloneImpl(false);
|
|
|
|
|
static_cast<ASTSelectQuery *>(&*res)->prev_union_all = nullptr;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASTPtr ASTSelectQuery::cloneImpl(bool traverse_union_all) const
|
2015-06-02 11:16:02 +00:00
|
|
|
|
{
|
2016-05-28 15:42:22 +00:00
|
|
|
|
auto res = std::make_shared<ASTSelectQuery>(*this);
|
2015-06-02 11:16:02 +00:00
|
|
|
|
res->children.clear();
|
|
|
|
|
|
|
|
|
|
#define CLONE(member) if (member) { res->member = member->clone(); res->children.push_back(res->member); }
|
|
|
|
|
|
|
|
|
|
/** NOTE Члены должны клонироваться точно в таком же порядке,
|
|
|
|
|
* в каком они были вставлены в children в ParserSelectQuery.
|
|
|
|
|
* Это важно, потому что из имён children-ов составляется идентификатор (getTreeID),
|
|
|
|
|
* который может быть использован для идентификаторов столбцов в случае подзапросов в операторе IN.
|
|
|
|
|
* При распределённой обработке запроса, в случае, если один из серверов localhost, а другой - нет,
|
|
|
|
|
* запрос на localhost выполняется в рамках процесса и при этом клонируется,
|
|
|
|
|
* а на удалённый сервер запрос отправляется в текстовом виде по TCP.
|
|
|
|
|
* И если порядок при клонировании не совпадает с порядком при парсинге,
|
|
|
|
|
* то на разных серверах получатся разные идентификаторы.
|
|
|
|
|
*/
|
|
|
|
|
CLONE(select_expression_list)
|
2016-07-18 00:14:24 +00:00
|
|
|
|
CLONE(tables)
|
2015-06-02 11:16:02 +00:00
|
|
|
|
CLONE(prewhere_expression)
|
|
|
|
|
CLONE(where_expression)
|
|
|
|
|
CLONE(group_expression_list)
|
|
|
|
|
CLONE(having_expression)
|
|
|
|
|
CLONE(order_expression_list)
|
2016-12-29 08:22:19 +00:00
|
|
|
|
CLONE(limit_by_value)
|
|
|
|
|
CLONE(limit_by_expression_list)
|
2015-06-02 11:16:02 +00:00
|
|
|
|
CLONE(limit_offset)
|
|
|
|
|
CLONE(limit_length)
|
2015-06-05 21:28:04 +00:00
|
|
|
|
CLONE(settings)
|
2015-06-02 11:16:02 +00:00
|
|
|
|
CLONE(format)
|
|
|
|
|
|
|
|
|
|
#undef CLONE
|
|
|
|
|
|
2015-07-11 15:16:59 +00:00
|
|
|
|
if (traverse_union_all)
|
2015-07-11 13:43:48 +00:00
|
|
|
|
{
|
2015-07-11 15:16:59 +00:00
|
|
|
|
if (next_union_all)
|
|
|
|
|
{
|
|
|
|
|
res->next_union_all = static_cast<const ASTSelectQuery *>(&*next_union_all)->cloneImpl(true);
|
|
|
|
|
res->children.push_back(res->next_union_all);
|
|
|
|
|
}
|
2015-07-11 13:43:48 +00:00
|
|
|
|
}
|
2015-07-11 15:16:59 +00:00
|
|
|
|
else
|
|
|
|
|
res->next_union_all = nullptr;
|
2015-07-11 13:43:48 +00:00
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
return res;
|
2015-06-02 11:16:02 +00:00
|
|
|
|
}
|
2015-06-25 17:38:54 +00:00
|
|
|
|
|
|
|
|
|
const IAST * ASTSelectQuery::getFormat() const
|
|
|
|
|
{
|
|
|
|
|
const ASTSelectQuery * query = this;
|
2016-05-28 15:46:03 +00:00
|
|
|
|
while (query->next_union_all != nullptr)
|
2015-06-25 17:38:54 +00:00
|
|
|
|
query = static_cast<const ASTSelectQuery *>(query->next_union_all.get());
|
|
|
|
|
return query->format.get();
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-05 21:38:31 +00:00
|
|
|
|
|
2015-08-06 03:26:27 +00:00
|
|
|
|
void ASTSelectQuery::formatImpl(const FormatSettings & s, FormatState & state, FormatStateStacked frame) const
|
2015-08-05 21:38:31 +00:00
|
|
|
|
{
|
2015-08-06 21:32:51 +00:00
|
|
|
|
frame.current_select = this;
|
2015-08-05 21:38:31 +00:00
|
|
|
|
frame.need_parens = false;
|
2015-08-06 03:26:27 +00:00
|
|
|
|
std::string indent_str = s.one_line ? "" : std::string(4 * frame.indent, ' ');
|
2015-08-05 21:38:31 +00:00
|
|
|
|
|
2015-08-06 03:26:27 +00:00
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << indent_str << "SELECT " << (distinct ? "DISTINCT " : "") << (s.hilite ? hilite_none : "");
|
2015-08-05 21:38:31 +00:00
|
|
|
|
|
2015-08-06 03:26:27 +00:00
|
|
|
|
s.one_line
|
|
|
|
|
? select_expression_list->formatImpl(s, state, frame)
|
|
|
|
|
: typeid_cast<const ASTExpressionList &>(*select_expression_list).formatImplMultiline(s, state, frame);
|
2015-08-05 21:38:31 +00:00
|
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
|
if (tables)
|
2015-08-05 21:38:31 +00:00
|
|
|
|
{
|
2015-08-06 03:26:27 +00:00
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "FROM " << (s.hilite ? hilite_none : "");
|
2016-07-18 00:14:24 +00:00
|
|
|
|
tables->formatImpl(s, state, frame);
|
2015-08-05 21:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prewhere_expression)
|
|
|
|
|
{
|
2015-08-06 03:26:27 +00:00
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "PREWHERE " << (s.hilite ? hilite_none : "");
|
|
|
|
|
prewhere_expression->formatImpl(s, state, frame);
|
2015-08-05 21:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (where_expression)
|
|
|
|
|
{
|
2015-08-06 03:26:27 +00:00
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "WHERE " << (s.hilite ? hilite_none : "");
|
|
|
|
|
where_expression->formatImpl(s, state, frame);
|
2015-08-05 21:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (group_expression_list)
|
|
|
|
|
{
|
2015-08-06 03:26:27 +00:00
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "GROUP BY " << (s.hilite ? hilite_none : "");
|
|
|
|
|
s.one_line
|
|
|
|
|
? group_expression_list->formatImpl(s, state, frame)
|
|
|
|
|
: typeid_cast<const ASTExpressionList &>(*group_expression_list).formatImplMultiline(s, state, frame);
|
2015-08-05 21:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (group_by_with_totals)
|
2015-08-06 03:26:27 +00:00
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << (s.one_line ? "" : " ") << "WITH TOTALS" << (s.hilite ? hilite_none : "");
|
2015-08-05 21:38:31 +00:00
|
|
|
|
|
|
|
|
|
if (having_expression)
|
|
|
|
|
{
|
2015-08-06 03:26:27 +00:00
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "HAVING " << (s.hilite ? hilite_none : "");
|
|
|
|
|
having_expression->formatImpl(s, state, frame);
|
2015-08-05 21:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (order_expression_list)
|
|
|
|
|
{
|
2015-08-06 03:26:27 +00:00
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "ORDER BY " << (s.hilite ? hilite_none : "");
|
|
|
|
|
s.one_line
|
|
|
|
|
? order_expression_list->formatImpl(s, state, frame)
|
|
|
|
|
: typeid_cast<const ASTExpressionList &>(*order_expression_list).formatImplMultiline(s, state, frame);
|
2015-08-05 21:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-29 08:22:19 +00:00
|
|
|
|
if (limit_by_value)
|
|
|
|
|
{
|
2017-01-09 13:24:54 +00:00
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "LIMIT " << (s.hilite ? hilite_none : "");
|
|
|
|
|
limit_by_value->formatImpl(s, state, frame);
|
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << " BY " << (s.hilite ? hilite_none : "");
|
2016-12-29 08:22:19 +00:00
|
|
|
|
s.one_line
|
|
|
|
|
? limit_by_expression_list->formatImpl(s, state, frame)
|
|
|
|
|
: typeid_cast<const ASTExpressionList &>(*limit_by_expression_list).formatImplMultiline(s, state, frame);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-05 21:38:31 +00:00
|
|
|
|
if (limit_length)
|
|
|
|
|
{
|
2015-08-06 03:26:27 +00:00
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "LIMIT " << (s.hilite ? hilite_none : "");
|
2015-08-05 21:38:31 +00:00
|
|
|
|
if (limit_offset)
|
|
|
|
|
{
|
2015-08-06 03:26:27 +00:00
|
|
|
|
limit_offset->formatImpl(s, state, frame);
|
|
|
|
|
s.ostr << ", ";
|
2015-08-05 21:38:31 +00:00
|
|
|
|
}
|
2015-08-06 03:26:27 +00:00
|
|
|
|
limit_length->formatImpl(s, state, frame);
|
2015-08-05 21:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (settings)
|
|
|
|
|
{
|
2015-08-06 03:26:27 +00:00
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "SETTINGS " << (s.hilite ? hilite_none : "");
|
2015-08-05 21:38:31 +00:00
|
|
|
|
|
|
|
|
|
const ASTSetQuery & ast_set = typeid_cast<const ASTSetQuery &>(*settings);
|
|
|
|
|
for (ASTSetQuery::Changes::const_iterator it = ast_set.changes.begin(); it != ast_set.changes.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
if (it != ast_set.changes.begin())
|
2015-08-06 03:26:27 +00:00
|
|
|
|
s.ostr << ", ";
|
2015-08-05 21:38:31 +00:00
|
|
|
|
|
2017-01-06 17:41:19 +00:00
|
|
|
|
s.ostr << it->name << " = " << applyVisitor(FieldVisitorToString(), it->value);
|
2015-08-05 21:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (format)
|
|
|
|
|
{
|
2015-08-06 03:26:27 +00:00
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "FORMAT " << (s.hilite ? hilite_none : "");
|
|
|
|
|
format->formatImpl(s, state, frame);
|
2015-08-05 21:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (next_union_all)
|
|
|
|
|
{
|
2015-08-06 03:26:27 +00:00
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "UNION ALL " << s.nl_or_ws << (s.hilite ? hilite_none : "");
|
2015-08-05 21:38:31 +00:00
|
|
|
|
|
|
|
|
|
// NOTE Мы можем безопасно применить static_cast вместо typeid_cast, потому что знаем, что в цепочке UNION ALL
|
|
|
|
|
// имеются только деревья типа SELECT.
|
|
|
|
|
const ASTSelectQuery & next_ast = static_cast<const ASTSelectQuery &>(*next_union_all);
|
|
|
|
|
|
2015-08-06 03:26:27 +00:00
|
|
|
|
next_ast.formatImpl(s, state, frame);
|
2015-08-05 21:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-21 15:48:11 +00:00
|
|
|
|
|
|
|
|
|
/// Compatibility functions. TODO Remove.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const ASTTableExpression * getFirstTableExpression(const ASTSelectQuery & select)
|
|
|
|
|
{
|
|
|
|
|
if (!select.tables)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
const ASTTablesInSelectQuery & tables_in_select_query = static_cast<const ASTTablesInSelectQuery &>(*select.tables);
|
|
|
|
|
if (tables_in_select_query.children.empty())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
const ASTTablesInSelectQueryElement & tables_element = static_cast<const ASTTablesInSelectQueryElement &>(*tables_in_select_query.children[0]);
|
|
|
|
|
if (!tables_element.table_expression)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return static_cast<const ASTTableExpression *>(tables_element.table_expression.get());
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-22 20:39:28 +00:00
|
|
|
|
static ASTTableExpression * getFirstTableExpression(ASTSelectQuery & select)
|
|
|
|
|
{
|
|
|
|
|
if (!select.tables)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
ASTTablesInSelectQuery & tables_in_select_query = static_cast<ASTTablesInSelectQuery &>(*select.tables);
|
|
|
|
|
if (tables_in_select_query.children.empty())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
ASTTablesInSelectQueryElement & tables_element = static_cast<ASTTablesInSelectQueryElement &>(*tables_in_select_query.children[0]);
|
|
|
|
|
if (!tables_element.table_expression)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return static_cast<ASTTableExpression *>(tables_element.table_expression.get());
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-21 15:48:11 +00:00
|
|
|
|
static const ASTArrayJoin * getFirstArrayJoin(const ASTSelectQuery & select)
|
|
|
|
|
{
|
|
|
|
|
if (!select.tables)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
const ASTTablesInSelectQuery & tables_in_select_query = static_cast<const ASTTablesInSelectQuery &>(*select.tables);
|
|
|
|
|
if (tables_in_select_query.children.empty())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
const ASTArrayJoin * array_join = nullptr;
|
|
|
|
|
for (const auto & child : tables_in_select_query.children)
|
|
|
|
|
{
|
|
|
|
|
const ASTTablesInSelectQueryElement & tables_element = static_cast<const ASTTablesInSelectQueryElement &>(*child);
|
|
|
|
|
if (tables_element.array_join)
|
|
|
|
|
{
|
|
|
|
|
if (!array_join)
|
|
|
|
|
array_join = static_cast<const ASTArrayJoin *>(tables_element.array_join.get());
|
|
|
|
|
else
|
|
|
|
|
throw Exception("Support for more than one ARRAY JOIN in query is not implemented", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array_join;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const ASTTablesInSelectQueryElement * getFirstTableJoin(const ASTSelectQuery & select)
|
|
|
|
|
{
|
|
|
|
|
if (!select.tables)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
const ASTTablesInSelectQuery & tables_in_select_query = static_cast<const ASTTablesInSelectQuery &>(*select.tables);
|
|
|
|
|
if (tables_in_select_query.children.empty())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
const ASTTablesInSelectQueryElement * joined_table = nullptr;
|
|
|
|
|
for (const auto & child : tables_in_select_query.children)
|
|
|
|
|
{
|
|
|
|
|
const ASTTablesInSelectQueryElement & tables_element = static_cast<const ASTTablesInSelectQueryElement &>(*child);
|
|
|
|
|
if (tables_element.table_join)
|
|
|
|
|
{
|
|
|
|
|
if (!joined_table)
|
|
|
|
|
joined_table = &tables_element;
|
|
|
|
|
else
|
|
|
|
|
throw Exception("Support for more than one JOIN in query is not implemented", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return joined_table;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ASTPtr ASTSelectQuery::database() const
|
|
|
|
|
{
|
|
|
|
|
const ASTTableExpression * table_expression = getFirstTableExpression(*this);
|
2016-07-23 02:25:09 +00:00
|
|
|
|
if (!table_expression || !table_expression->database_and_table_name || table_expression->database_and_table_name->children.empty())
|
2016-07-21 15:48:11 +00:00
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
if (table_expression->database_and_table_name->children.size() != 2)
|
|
|
|
|
throw Exception("Logical error: more than two components in table expression", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
|
|
return table_expression->database_and_table_name->children[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ASTPtr ASTSelectQuery::table() const
|
|
|
|
|
{
|
|
|
|
|
const ASTTableExpression * table_expression = getFirstTableExpression(*this);
|
|
|
|
|
if (!table_expression)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
if (table_expression->database_and_table_name)
|
|
|
|
|
{
|
|
|
|
|
if (table_expression->database_and_table_name->children.empty())
|
|
|
|
|
return table_expression->database_and_table_name;
|
|
|
|
|
|
|
|
|
|
if (table_expression->database_and_table_name->children.size() != 2)
|
|
|
|
|
throw Exception("Logical error: more than two components in table expression", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
|
|
return table_expression->database_and_table_name->children[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (table_expression->table_function)
|
|
|
|
|
return table_expression->table_function;
|
|
|
|
|
|
|
|
|
|
if (table_expression->subquery)
|
|
|
|
|
return static_cast<const ASTSubquery *>(table_expression->subquery.get())->children.at(0);
|
|
|
|
|
|
|
|
|
|
throw Exception("Logical error: incorrect table expression", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ASTPtr ASTSelectQuery::sample_size() const
|
|
|
|
|
{
|
|
|
|
|
const ASTTableExpression * table_expression = getFirstTableExpression(*this);
|
|
|
|
|
if (!table_expression)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return table_expression->sample_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ASTPtr ASTSelectQuery::sample_offset() const
|
|
|
|
|
{
|
|
|
|
|
const ASTTableExpression * table_expression = getFirstTableExpression(*this);
|
|
|
|
|
if (!table_expression)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return table_expression->sample_offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ASTSelectQuery::final() const
|
|
|
|
|
{
|
|
|
|
|
const ASTTableExpression * table_expression = getFirstTableExpression(*this);
|
|
|
|
|
if (!table_expression)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return table_expression->final;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ASTPtr ASTSelectQuery::array_join_expression_list() const
|
|
|
|
|
{
|
|
|
|
|
const ASTArrayJoin * array_join = getFirstArrayJoin(*this);
|
|
|
|
|
if (!array_join)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return array_join->expression_list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ASTSelectQuery::array_join_is_left() const
|
|
|
|
|
{
|
|
|
|
|
const ASTArrayJoin * array_join = getFirstArrayJoin(*this);
|
|
|
|
|
if (!array_join)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return array_join->kind == ASTArrayJoin::Kind::Left;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ASTTablesInSelectQueryElement * ASTSelectQuery::join() const
|
|
|
|
|
{
|
|
|
|
|
return getFirstTableJoin(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-22 20:39:28 +00:00
|
|
|
|
|
|
|
|
|
void ASTSelectQuery::setDatabaseIfNeeded(const String & database_name)
|
|
|
|
|
{
|
|
|
|
|
ASTTableExpression * table_expression = getFirstTableExpression(*this);
|
|
|
|
|
if (!table_expression)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!table_expression->database_and_table_name)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (table_expression->database_and_table_name->children.empty())
|
|
|
|
|
{
|
|
|
|
|
ASTPtr database = std::make_shared<ASTIdentifier>(StringRange(), database_name, ASTIdentifier::Database);
|
|
|
|
|
ASTPtr table = table_expression->database_and_table_name;
|
|
|
|
|
|
|
|
|
|
const String & old_name = static_cast<ASTIdentifier &>(*table_expression->database_and_table_name).name;
|
|
|
|
|
table_expression->database_and_table_name = std::make_shared<ASTIdentifier>(StringRange(), database_name + "." + old_name, ASTIdentifier::Table);
|
|
|
|
|
table_expression->database_and_table_name->children = {database, table};
|
|
|
|
|
}
|
|
|
|
|
else if (table_expression->database_and_table_name->children.size() != 2)
|
|
|
|
|
{
|
|
|
|
|
throw Exception("Logical error: more than two components in table expression", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ASTSelectQuery::replaceDatabaseAndTable(const String & database_name, const String & table_name)
|
|
|
|
|
{
|
|
|
|
|
ASTTableExpression * table_expression = getFirstTableExpression(*this);
|
2016-07-23 02:25:09 +00:00
|
|
|
|
|
2016-07-22 20:39:28 +00:00
|
|
|
|
if (!table_expression)
|
2016-07-23 02:25:09 +00:00
|
|
|
|
{
|
|
|
|
|
auto tables_list = std::make_shared<ASTTablesInSelectQuery>();
|
|
|
|
|
auto element = std::make_shared<ASTTablesInSelectQueryElement>();
|
|
|
|
|
auto table_expr = std::make_shared<ASTTableExpression>();
|
|
|
|
|
element->table_expression = table_expr;
|
|
|
|
|
element->children.emplace_back(table_expr);
|
|
|
|
|
tables_list->children.emplace_back(element);
|
|
|
|
|
tables = tables_list;
|
|
|
|
|
children.emplace_back(tables_list);
|
|
|
|
|
table_expression = table_expr.get();
|
|
|
|
|
}
|
2016-07-22 20:39:28 +00:00
|
|
|
|
|
|
|
|
|
ASTPtr table = std::make_shared<ASTIdentifier>(StringRange(), table_name, ASTIdentifier::Table);
|
|
|
|
|
|
2016-07-23 02:25:09 +00:00
|
|
|
|
if (!database_name.empty())
|
|
|
|
|
{
|
|
|
|
|
ASTPtr database = std::make_shared<ASTIdentifier>(StringRange(), database_name, ASTIdentifier::Database);
|
|
|
|
|
|
|
|
|
|
table_expression->database_and_table_name = std::make_shared<ASTIdentifier>(
|
|
|
|
|
StringRange(), database_name + "." + table_name, ASTIdentifier::Table);
|
|
|
|
|
table_expression->database_and_table_name->children = {database, table};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
table_expression->database_and_table_name = std::make_shared<ASTIdentifier>(
|
|
|
|
|
StringRange(), table_name, ASTIdentifier::Table);
|
|
|
|
|
}
|
2016-07-22 20:39:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-02 11:16:02 +00:00
|
|
|
|
};
|
|
|
|
|
|