2016-03-07 01:08:01 +00:00
|
|
|
#include <memory>
|
2017-04-01 09:19:00 +00:00
|
|
|
#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
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int SYNTAX_ERROR;
|
2016-03-07 01:08:01 +00:00
|
|
|
}
|
|
|
|
|
2011-08-28 00:31:30 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserSelectQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2011-08-28 00:31:30 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto select_query = std::make_shared<ASTSelectQuery>();
|
|
|
|
node = select_query;
|
|
|
|
|
2017-06-18 03:07:03 +00:00
|
|
|
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");
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
ParserNotEmptyExpressionList exp_list(false);
|
2017-08-13 09:20:05 +00:00
|
|
|
ParserNotEmptyExpressionList exp_list_for_with_clause(false, true); /// Set prefer_alias_to_column_name for each alias.
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserNotEmptyExpressionList exp_list_for_select_clause(true); /// Allows aliases without AS keyword.
|
2017-08-13 09:20:05 +00:00
|
|
|
ParserExpressionWithOptionalAlias exp_elem(false);
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserOrderByExpressionList order_list;
|
|
|
|
|
2017-08-10 14:46:46 +00:00
|
|
|
/// WITH expr list
|
|
|
|
{
|
|
|
|
if (s_with.ignore(pos, expected))
|
|
|
|
{
|
2017-08-13 09:20:05 +00:00
|
|
|
if (!exp_list_for_with_clause.parse(pos, select_query->with_expression_list, expected))
|
2017-08-10 14:46:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// SELECT [DISTINCT] expr list
|
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!s_select.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_distinct.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
select_query->distinct = true;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!exp_list_for_select_clause.parse(pos, select_query->select_expression_list, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-25 00:50:53 +00:00
|
|
|
/// FROM database.table or FROM table or FROM (subquery) or FROM tableFunction(...)
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_from.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!ParserTablesInSelectQuery().parse(pos, select_query->tables, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PREWHERE expr
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_prewhere.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!exp_elem.parse(pos, select_query->prewhere_expression, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// WHERE expr
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_where.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!exp_elem.parse(pos, select_query->where_expression, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-08-28 00:31:30 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// GROUP BY expr list
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_group_by.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!exp_list.parse(pos, select_query->group_expression_list, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// WITH TOTALS
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_with.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!s_totals.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2013-05-04 15:46:50 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
select_query->group_by_with_totals = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HAVING expr
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_having.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!exp_elem.parse(pos, select_query->having_expression, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-08-28 00:31:30 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// ORDER BY expr ASC|DESC COLLATE 'locale' list
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_order_by.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!order_list.parse(pos, select_query->order_expression_list, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-08-28 00:31:30 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// LIMIT length | LIMIT offset, length | LIMIT count BY expr-list
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_limit.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
ParserToken s_comma(TokenType::Comma);
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserNumber num;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!num.parse(pos, select_query->limit_length, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_comma.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
select_query->limit_offset = select_query->limit_length;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!num.parse(pos, select_query->limit_length, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-10 03:28:12 +00:00
|
|
|
else if (s_by.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
select_query->limit_by_value = select_query->limit_length;
|
|
|
|
select_query->limit_length = nullptr;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!exp_list.parse(pos, select_query->limit_by_expression_list, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LIMIT length | LIMIT offset, length
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_limit.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!select_query->limit_by_value || select_query->limit_length)
|
|
|
|
return false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
ParserToken s_comma(TokenType::Comma);
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserNumber num;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!num.parse(pos, select_query->limit_length, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_comma.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
select_query->limit_offset = select_query->limit_length;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!num.parse(pos, select_query->limit_length, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// SETTINGS key1 = value1, key2 = value2, ...
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_settings.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
ParserSetQuery parser_settings(true);
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!parser_settings.parse(pos, select_query->settings, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-10 14:46:46 +00:00
|
|
|
if (select_query->with_expression_list)
|
|
|
|
select_query->children.push_back(select_query->with_expression_list);
|
2017-04-01 07:20:54 +00:00
|
|
|
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);
|
2017-06-18 03:07:03 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2011-08-28 00:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|