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>
|
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:
|
2013-06-01 07:43:57 +00:00
|
|
|
|
bool distinct;
|
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)
|
2013-10-17 13:32:32 +00:00
|
|
|
|
ASTPtr array_join_expression_list;
|
2013-04-23 11:08:41 +00:00
|
|
|
|
bool final;
|
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;
|
2013-05-04 15:46:50 +00:00
|
|
|
|
bool group_by_with_totals;
|
2011-08-28 00:31:30 +00:00
|
|
|
|
ASTPtr having_expression;
|
|
|
|
|
ASTPtr order_expression_list;
|
|
|
|
|
ASTPtr limit_offset;
|
|
|
|
|
ASTPtr limit_length;
|
2012-08-22 18:46:09 +00:00
|
|
|
|
|
2013-06-01 07:43:57 +00:00
|
|
|
|
ASTSelectQuery() : distinct(false), final(false), group_by_with_totals(false) {}
|
|
|
|
|
ASTSelectQuery(StringRange range_) : ASTQueryWithOutput(range_), distinct(false), final(false), group_by_with_totals(false) {}
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
2011-08-09 19:19:00 +00:00
|
|
|
|
/** Получить текст, который идентифицирует этот элемент. */
|
2012-12-27 16:23:12 +00:00
|
|
|
|
String getID() const { return "SelectQuery"; };
|
2011-12-12 06:15:34 +00:00
|
|
|
|
|
2013-11-13 14:39:48 +00:00
|
|
|
|
|
2014-03-27 19:49:52 +00:00
|
|
|
|
static bool hasArrayJoin(const ASTPtr & ast)
|
|
|
|
|
{
|
|
|
|
|
if (const ASTFunction * function = dynamic_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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-13 14:39:48 +00:00
|
|
|
|
/// Переписывает select_expression_list, чтобы вернуть только необходимые столбцы в правильном порядке.
|
|
|
|
|
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]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-08 17:43:03 +00:00
|
|
|
|
for (size_t i = 0; i < column_names.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
bool done = 0;
|
|
|
|
|
for (size_t j = 0; j < asts.size(); ++j)
|
|
|
|
|
{
|
|
|
|
|
if (asts[j]->getAlias() == column_names[i])
|
|
|
|
|
{
|
2014-03-27 19:49:52 +00:00
|
|
|
|
if (!unremovable_asts.count(asts[j]))
|
|
|
|
|
result->children.push_back(asts[j]->clone());
|
2013-11-08 17:43:03 +00:00
|
|
|
|
done = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!done)
|
2013-11-13 14:39:48 +00:00
|
|
|
|
throw Exception("Error while rewriting expressioin list for select query."
|
2013-11-08 17:43:03 +00:00
|
|
|
|
" Could not find alias: " + column_names[i],
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2011-12-12 06:15:34 +00:00
|
|
|
|
ASTPtr clone() const
|
|
|
|
|
{
|
|
|
|
|
ASTSelectQuery * res = new ASTSelectQuery(*this);
|
|
|
|
|
res->children.clear();
|
|
|
|
|
|
|
|
|
|
if (select_expression_list) { res->select_expression_list = select_expression_list->clone(); res->children.push_back(res->select_expression_list); }
|
|
|
|
|
if (database) { res->database = database->clone(); res->children.push_back(res->database); }
|
|
|
|
|
if (table) { res->table = table->clone(); res->children.push_back(res->table); }
|
2013-10-17 13:32:32 +00:00
|
|
|
|
if (array_join_expression_list) { res->array_join_expression_list = array_join_expression_list->clone(); res->children.push_back(res->array_join_expression_list); }
|
2012-12-12 14:25:55 +00:00
|
|
|
|
if (sample_size) { res->sample_size = sample_size->clone(); res->children.push_back(res->sample_size); }
|
2013-11-28 13:16:46 +00:00
|
|
|
|
if (prewhere_expression) { res->prewhere_expression = prewhere_expression->clone(); res->children.push_back(res->prewhere_expression); }
|
2011-12-12 06:15:34 +00:00
|
|
|
|
if (where_expression) { res->where_expression = where_expression->clone(); res->children.push_back(res->where_expression); }
|
|
|
|
|
if (group_expression_list) { res->group_expression_list = group_expression_list->clone(); res->children.push_back(res->group_expression_list); }
|
|
|
|
|
if (having_expression) { res->having_expression = having_expression->clone(); res->children.push_back(res->having_expression); }
|
|
|
|
|
if (order_expression_list) { res->order_expression_list = order_expression_list->clone(); res->children.push_back(res->order_expression_list); }
|
|
|
|
|
if (limit_offset) { res->limit_offset = limit_offset->clone(); res->children.push_back(res->limit_offset); }
|
|
|
|
|
if (limit_length) { res->limit_length = limit_length->clone(); res->children.push_back(res->limit_length); }
|
|
|
|
|
if (format) { res->format = format->clone(); res->children.push_back(res->format); }
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2010-06-24 19:12:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|