2011-08-28 00:31:30 +00:00
|
|
|
|
#pragma once
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/Parsers/IAST.h>
|
2013-02-20 13:14:12 +00:00
|
|
|
|
#include <DB/Parsers/ASTQueryWithOutput.h>
|
2013-11-08 17:43:03 +00:00
|
|
|
|
#include <DB/Parsers/ASTExpressionList.h>
|
|
|
|
|
#include <DB/Parsers/ASTFunction.h>
|
2014-12-25 14:17:08 +00:00
|
|
|
|
#include <DB/Parsers/ASTAsterisk.h>
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** SELECT запрос
|
|
|
|
|
*/
|
2013-02-20 13:14:12 +00:00
|
|
|
|
class ASTSelectQuery : public ASTQueryWithOutput
|
2010-06-24 19:12:10 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
2014-06-12 00:48:56 +00:00
|
|
|
|
bool distinct = false;
|
2011-08-28 00:31:30 +00:00
|
|
|
|
ASTPtr select_expression_list;
|
|
|
|
|
ASTPtr database;
|
2014-02-06 16:21:00 +00:00
|
|
|
|
ASTPtr table; /// Идентификатор, табличная функция или подзапрос (рекурсивно ASTSelectQuery)
|
2014-06-12 00:48:56 +00:00
|
|
|
|
ASTPtr array_join_expression_list; /// ARRAY JOIN
|
|
|
|
|
ASTPtr join; /// Обычный (не ARRAY) JOIN.
|
|
|
|
|
bool final = false;
|
2012-12-12 14:25:55 +00:00
|
|
|
|
ASTPtr sample_size;
|
2013-11-28 13:16:46 +00:00
|
|
|
|
ASTPtr prewhere_expression;
|
2011-08-28 00:31:30 +00:00
|
|
|
|
ASTPtr where_expression;
|
|
|
|
|
ASTPtr group_expression_list;
|
2014-06-12 00:48:56 +00:00
|
|
|
|
bool group_by_with_totals = false;
|
2011-08-28 00:31:30 +00:00
|
|
|
|
ASTPtr having_expression;
|
|
|
|
|
ASTPtr order_expression_list;
|
|
|
|
|
ASTPtr limit_offset;
|
|
|
|
|
ASTPtr limit_length;
|
2014-12-08 14:51:47 +00:00
|
|
|
|
ASTPtr next_union_all; /// Следующий запрос SELECT в цепочке UNION ALL, если такой есть
|
2012-08-22 18:46:09 +00:00
|
|
|
|
|
2014-12-17 15:26:24 +00:00
|
|
|
|
ASTSelectQuery() = default;
|
|
|
|
|
ASTSelectQuery(const StringRange range_) : ASTQueryWithOutput(range_) {}
|
2014-06-12 23:21:38 +00:00
|
|
|
|
|
2011-08-09 19:19:00 +00:00
|
|
|
|
/** Получить текст, который идентифицирует этот элемент. */
|
2014-12-17 15:26:24 +00:00
|
|
|
|
String getID() const override { return "SelectQuery"; };
|
2011-12-12 06:15:34 +00:00
|
|
|
|
|
2014-06-12 00:48:56 +00:00
|
|
|
|
/// Проверить наличие функции arrayJoin. (Не большого ARRAY JOIN.)
|
2014-03-27 19:49:52 +00:00
|
|
|
|
static bool hasArrayJoin(const ASTPtr & ast)
|
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
if (const ASTFunction * function = typeid_cast<const ASTFunction *>(&*ast))
|
2014-03-27 19:49:52 +00:00
|
|
|
|
if (function->kind == ASTFunction::ARRAY_JOIN)
|
|
|
|
|
return true;
|
2014-07-06 00:19:49 +00:00
|
|
|
|
|
2014-03-27 19:49:52 +00:00
|
|
|
|
for (const auto & child : ast->children)
|
|
|
|
|
if (hasArrayJoin(child))
|
|
|
|
|
return true;
|
2014-07-06 00:19:49 +00:00
|
|
|
|
|
2014-03-27 19:49:52 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-25 14:17:08 +00:00
|
|
|
|
bool hasAsterisk() const
|
|
|
|
|
{
|
|
|
|
|
if (const ASTExpressionList * node = typeid_cast<const ASTExpressionList *>(&*select_expression_list))
|
|
|
|
|
{
|
|
|
|
|
for (const auto & ast : node->children)
|
|
|
|
|
{
|
|
|
|
|
if (typeid_cast<const ASTAsterisk *>(&*ast) != nullptr)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-24 11:24:43 +00:00
|
|
|
|
/// Переименовать столбцы запроса в такие же имена, как в исходном запросе.
|
2014-12-23 15:33:36 +00:00
|
|
|
|
void renameColumns(const ASTSelectQuery & source)
|
|
|
|
|
{
|
|
|
|
|
const ASTs & from = source.select_expression_list->children;
|
2014-12-23 17:20:42 +00:00
|
|
|
|
ASTs & to = select_expression_list->children;
|
|
|
|
|
|
2014-12-23 15:33:36 +00:00
|
|
|
|
if (from.size() != to.size())
|
2014-12-23 17:20:42 +00:00
|
|
|
|
throw Exception("Size mismatch in UNION ALL chain",
|
|
|
|
|
DB::ErrorCodes::UNION_ALL_RESULT_STRUCTURES_MISMATCH);
|
|
|
|
|
|
2014-12-23 15:33:36 +00:00
|
|
|
|
for (size_t i = 0; i < from.size(); ++i)
|
2014-12-23 17:20:42 +00:00
|
|
|
|
{
|
2014-12-24 11:24:43 +00:00
|
|
|
|
/// Если столбец имеет алиас, то он должен совпадать с названием исходного столбца.
|
|
|
|
|
/// В противном случае мы ему присваиваем алиас, если требуется.
|
|
|
|
|
if (!to[i]->tryGetAlias().empty())
|
|
|
|
|
{
|
|
|
|
|
if (to[i]->tryGetAlias() != from[i]->getAliasOrColumnName())
|
|
|
|
|
throw Exception("Column alias mismatch in UNION ALL chain",
|
2014-12-23 17:20:42 +00:00
|
|
|
|
DB::ErrorCodes::UNION_ALL_COLUMN_ALIAS_MISMATCH);
|
2014-12-24 11:24:43 +00:00
|
|
|
|
}
|
|
|
|
|
else if (to[i]->getColumnName() != from[i]->getAliasOrColumnName())
|
2014-12-23 15:33:36 +00:00
|
|
|
|
to[i]->setAlias(from[i]->getAliasOrColumnName());
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-23 17:20:42 +00:00
|
|
|
|
|
2013-11-13 14:39:48 +00:00
|
|
|
|
/// Переписывает select_expression_list, чтобы вернуть только необходимые столбцы в правильном порядке.
|
2014-06-13 09:07:20 +00:00
|
|
|
|
void rewriteSelectExpressionList(const Names & column_names)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
|
|
|
|
ASTPtr result = new ASTExpressionList;
|
|
|
|
|
ASTs asts = select_expression_list->children;
|
2013-11-13 14:39:48 +00:00
|
|
|
|
|
2014-03-27 19:49:52 +00:00
|
|
|
|
/// Не будем выбрасывать выражения, содержащие функцию arrayJoin.
|
|
|
|
|
std::set<ASTPtr> unremovable_asts;
|
|
|
|
|
for (size_t j = 0; j < asts.size(); ++j)
|
|
|
|
|
{
|
|
|
|
|
if (hasArrayJoin(asts[j]))
|
|
|
|
|
{
|
|
|
|
|
result->children.push_back(asts[j]->clone());
|
|
|
|
|
unremovable_asts.insert(asts[j]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-06 00:19:49 +00:00
|
|
|
|
for (const auto & column_name : column_names)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
2014-07-06 00:19:49 +00:00
|
|
|
|
bool done = false;
|
2013-11-08 17:43:03 +00:00
|
|
|
|
for (size_t j = 0; j < asts.size(); ++j)
|
|
|
|
|
{
|
2014-07-06 00:19:49 +00:00
|
|
|
|
if (asts[j]->getAliasOrColumnName() == column_name)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
2014-03-27 19:49:52 +00:00
|
|
|
|
if (!unremovable_asts.count(asts[j]))
|
|
|
|
|
result->children.push_back(asts[j]->clone());
|
2014-07-06 00:19:49 +00:00
|
|
|
|
done = true;
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-13 09:07:20 +00:00
|
|
|
|
if (!done)
|
2014-07-04 01:42:08 +00:00
|
|
|
|
throw Exception("Error while rewriting expression list for select query."
|
2014-07-06 00:19:49 +00:00
|
|
|
|
" Could not find alias: " + column_name,
|
2013-11-08 17:43:03 +00:00
|
|
|
|
DB::ErrorCodes::UNKNOWN_IDENTIFIER);
|
|
|
|
|
}
|
2014-03-27 19:47:51 +00:00
|
|
|
|
|
|
|
|
|
for (auto & child : children)
|
|
|
|
|
{
|
|
|
|
|
if (child == select_expression_list)
|
|
|
|
|
{
|
|
|
|
|
child = result;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
select_expression_list = result;
|
2014-03-27 19:49:52 +00:00
|
|
|
|
|
|
|
|
|
/** NOTE: Может показаться, что мы могли испортить запрос, выбросив выражение с алиасом, который используется где-то еще.
|
|
|
|
|
* Такого произойти не может, потому что этот метод вызывается всегда для запроса, на котором хоть раз создавали
|
|
|
|
|
* ExpressionAnalyzer, что гарантирует, что в нем все алиасы уже подставлены. Не совсем очевидная логика :)
|
|
|
|
|
*/
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-17 15:26:24 +00:00
|
|
|
|
ASTPtr clone() const override
|
2011-12-12 06:15:34 +00:00
|
|
|
|
{
|
|
|
|
|
ASTSelectQuery * res = new ASTSelectQuery(*this);
|
2014-12-17 15:26:24 +00:00
|
|
|
|
ASTPtr ptr{res};
|
|
|
|
|
|
2011-12-12 06:15:34 +00:00
|
|
|
|
res->children.clear();
|
|
|
|
|
|
2014-06-12 00:48:56 +00:00
|
|
|
|
#define CLONE(member) if (member) { res->member = member->clone(); res->children.push_back(res->member); }
|
|
|
|
|
|
|
|
|
|
CLONE(select_expression_list)
|
|
|
|
|
CLONE(database)
|
|
|
|
|
CLONE(table)
|
|
|
|
|
CLONE(array_join_expression_list)
|
|
|
|
|
CLONE(join)
|
|
|
|
|
CLONE(sample_size)
|
|
|
|
|
CLONE(prewhere_expression)
|
|
|
|
|
CLONE(where_expression)
|
|
|
|
|
CLONE(group_expression_list)
|
|
|
|
|
CLONE(having_expression)
|
|
|
|
|
CLONE(order_expression_list)
|
|
|
|
|
CLONE(limit_offset)
|
|
|
|
|
CLONE(limit_length)
|
|
|
|
|
CLONE(format)
|
2014-12-08 14:51:47 +00:00
|
|
|
|
CLONE(next_union_all)
|
2014-06-12 00:48:56 +00:00
|
|
|
|
|
|
|
|
|
#undef CLONE
|
2011-12-12 06:15:34 +00:00
|
|
|
|
|
2014-12-17 15:26:24 +00:00
|
|
|
|
return ptr;
|
2011-12-12 06:15:34 +00:00
|
|
|
|
}
|
2010-06-24 19:12:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|