ClickHouse/dbms/src/Parsers/ParserSelectQuery.cpp

221 lines
7.0 KiB
C++
Raw Normal View History

2016-03-07 01:08:01 +00:00
#include <memory>
#include <Parsers/ASTSelectQuery.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/IParserBase.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/ParserSetQuery.h>
#include <Parsers/ParserSampleRatio.h>
#include <Parsers/ParserSelectQuery.h>
#include <Parsers/ParserTablesInSelectQuery.h>
2016-07-18 00:14:24 +00:00
2011-08-28 00:31:30 +00:00
namespace DB
{
2016-03-07 01:08:01 +00:00
namespace ErrorCodes
{
extern const int SYNTAX_ERROR;
2016-03-07 01:08:01 +00:00
}
2011-08-28 00:31:30 +00:00
bool ParserSelectQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
2011-08-28 00:31:30 +00:00
{
Pos begin = pos;
auto select_query = std::make_shared<ASTSelectQuery>();
node = select_query;
ParserKeyword s_select("SELECT");
ParserKeyword s_distinct("DISTINCT");
ParserKeyword s_from("FROM");
ParserKeyword s_prewhere("PREWHERE");
ParserKeyword s_where("WHERE");
ParserKeyword s_group_by("GROUP BY");
ParserKeyword s_with("WITH");
ParserKeyword s_totals("TOTALS");
ParserKeyword s_having("HAVING");
ParserKeyword s_order_by("ORDER BY");
ParserKeyword s_limit("LIMIT");
ParserKeyword s_settings("SETTINGS");
ParserKeyword s_by("BY");
ParserNotEmptyExpressionList exp_list(false);
ParserNotEmptyExpressionList exp_list_for_with_clause(false, true); /// Set prefer_alias_to_column_name for each alias.
ParserNotEmptyExpressionList exp_list_for_select_clause(true); /// Allows aliases without AS keyword.
ParserExpressionWithOptionalAlias exp_elem(false);
ParserOrderByExpressionList order_list;
/// WITH expr list
{
if (s_with.ignore(pos, expected))
{
if (!exp_list_for_with_clause.parse(pos, select_query->with_expression_list, expected))
return false;
}
}
/// SELECT [DISTINCT] expr list
{
if (!s_select.ignore(pos, expected))
return false;
if (s_distinct.ignore(pos, expected))
select_query->distinct = true;
if (!exp_list_for_select_clause.parse(pos, select_query->select_expression_list, expected))
return false;
}
2017-04-02 17:37:49 +00:00
/// FROM database.table or FROM table or FROM (subquery) or FROM tableFunction
if (s_from.ignore(pos, expected))
{
if (!ParserTablesInSelectQuery().parse(pos, select_query->tables, expected))
return false;
}
/// PREWHERE expr
if (s_prewhere.ignore(pos, expected))
{
if (!exp_elem.parse(pos, select_query->prewhere_expression, expected))
return false;
}
/// WHERE expr
if (s_where.ignore(pos, expected))
{
if (!exp_elem.parse(pos, select_query->where_expression, expected))
return false;
}
2011-08-28 00:31:30 +00:00
/// GROUP BY expr list
if (s_group_by.ignore(pos, expected))
{
if (!exp_list.parse(pos, select_query->group_expression_list, expected))
return false;
}
/// WITH TOTALS
if (s_with.ignore(pos, expected))
{
if (!s_totals.ignore(pos, expected))
return false;
select_query->group_by_with_totals = true;
}
/// HAVING expr
if (s_having.ignore(pos, expected))
{
if (!exp_elem.parse(pos, select_query->having_expression, expected))
return false;
}
2011-08-28 00:31:30 +00:00
/// ORDER BY expr ASC|DESC COLLATE 'locale' list
if (s_order_by.ignore(pos, expected))
{
if (!order_list.parse(pos, select_query->order_expression_list, expected))
return false;
}
2011-08-28 00:31:30 +00:00
/// LIMIT length | LIMIT offset, length | LIMIT count BY expr-list
if (s_limit.ignore(pos, expected))
{
ParserToken s_comma(TokenType::Comma);
ParserNumber num;
if (!num.parse(pos, select_query->limit_length, expected))
return false;
if (s_comma.ignore(pos, expected))
{
select_query->limit_offset = select_query->limit_length;
if (!num.parse(pos, select_query->limit_length, expected))
return false;
}
else if (s_by.ignore(pos, expected))
{
select_query->limit_by_value = select_query->limit_length;
select_query->limit_length = nullptr;
if (!exp_list.parse(pos, select_query->limit_by_expression_list, expected))
return false;
}
}
/// LIMIT length | LIMIT offset, length
if (s_limit.ignore(pos, expected))
{
if (!select_query->limit_by_value || select_query->limit_length)
return false;
ParserToken s_comma(TokenType::Comma);
ParserNumber num;
if (!num.parse(pos, select_query->limit_length, expected))
return false;
if (s_comma.ignore(pos, expected))
{
select_query->limit_offset = select_query->limit_length;
if (!num.parse(pos, select_query->limit_length, expected))
return false;
}
}
/// SETTINGS key1 = value1, key2 = value2, ...
if (s_settings.ignore(pos, expected))
{
ParserSetQuery parser_settings(true);
if (!parser_settings.parse(pos, select_query->settings, expected))
return false;
}
// UNION ALL select query
if (ParserKeyword("UNION ALL").ignore(pos, expected))
{
ParserSelectQuery select_p;
if (!select_p.parse(pos, select_query->next_union_all, expected))
return false;
auto next_select_query = static_cast<ASTSelectQuery *>(&*select_query->next_union_all);
next_select_query->prev_union_all = node.get();
}
select_query->range = StringRange(begin, pos);
if (select_query->with_expression_list)
select_query->children.push_back(select_query->with_expression_list);
select_query->children.push_back(select_query->select_expression_list);
if (select_query->tables)
select_query->children.push_back(select_query->tables);
if (select_query->prewhere_expression)
select_query->children.push_back(select_query->prewhere_expression);
if (select_query->where_expression)
select_query->children.push_back(select_query->where_expression);
if (select_query->group_expression_list)
select_query->children.push_back(select_query->group_expression_list);
if (select_query->having_expression)
select_query->children.push_back(select_query->having_expression);
if (select_query->order_expression_list)
select_query->children.push_back(select_query->order_expression_list);
if (select_query->limit_by_value)
select_query->children.push_back(select_query->limit_by_value);
if (select_query->limit_by_expression_list)
select_query->children.push_back(select_query->limit_by_expression_list);
if (select_query->limit_offset)
select_query->children.push_back(select_query->limit_offset);
if (select_query->limit_length)
select_query->children.push_back(select_query->limit_length);
if (select_query->settings)
select_query->children.push_back(select_query->settings);
if (select_query->next_union_all)
select_query->children.push_back(select_query->next_union_all);
return true;
2011-08-28 00:31:30 +00:00
}
}