ClickHouse/dbms/src/Parsers/ParserSelectQuery.cpp

308 lines
10 KiB
C++
Raw Normal View History

2016-03-07 01:08:01 +00:00
#include <memory>
#include <Parsers/ASTSelectQuery.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;
2018-08-13 10:45:27 +00:00
extern const int TOP_AND_LIMIT_TOGETHER;
2019-04-18 12:34:10 +00:00
extern const int WITH_TIES_WITHOUT_ORDER_BY;
2019-11-05 15:49:13 +00:00
extern const int LIMIT_BY_WITH_TIES_IS_NOT_SUPPORTED;
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
{
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");
2018-08-21 16:08:45 +00:00
ParserKeyword s_rollup("ROLLUP");
2018-09-17 18:01:04 +00:00
ParserKeyword s_cube("CUBE");
ParserKeyword s_top("TOP");
2019-04-18 08:24:06 +00:00
ParserKeyword s_with_ties("WITH TIES");
ParserKeyword s_offset("OFFSET");
ParserNotEmptyExpressionList exp_list(false);
ParserNotEmptyExpressionList exp_list_for_with_clause(false);
ParserNotEmptyExpressionList exp_list_for_select_clause(true); /// Allows aliases without AS keyword.
ParserExpressionWithOptionalAlias exp_elem(false);
ParserOrderByExpressionList order_list;
2018-08-24 15:00:00 +00:00
ParserToken open_bracket(TokenType::OpeningRoundBracket);
ParserToken close_bracket(TokenType::ClosingRoundBracket);
ASTPtr with_expression_list;
ASTPtr select_expression_list;
ASTPtr tables;
ASTPtr prewhere_expression;
ASTPtr where_expression;
ASTPtr group_expression_list;
ASTPtr having_expression;
ASTPtr order_expression_list;
2019-04-29 13:12:39 +00:00
ASTPtr limit_by_length;
ASTPtr limit_by_offset;
ASTPtr limit_by_expression_list;
ASTPtr limit_offset;
ASTPtr limit_length;
ASTPtr top_length;
ASTPtr settings;
/// WITH expr list
{
if (s_with.ignore(pos, expected))
{
if (!exp_list_for_with_clause.parse(pos, with_expression_list, expected))
return false;
}
}
2019-04-18 08:24:06 +00:00
/// SELECT [DISTINCT] [TOP N [WITH TIES]] expr list
{
if (!s_select.ignore(pos, expected))
return false;
if (s_distinct.ignore(pos, expected))
select_query->distinct = true;
if (s_top.ignore(pos, expected))
{
ParserNumber num;
if (open_bracket.ignore(pos, expected))
{
if (!num.parse(pos, top_length, expected))
return false;
if (!close_bracket.ignore(pos, expected))
return false;
}
else
{
if (!num.parse(pos, top_length, expected))
return false;
}
2019-04-18 08:24:06 +00:00
if (s_with_ties.ignore(pos, expected))
select_query->limit_with_ties = true;
}
if (!exp_list_for_select_clause.parse(pos, select_expression_list, expected))
return false;
}
2018-02-25 00:50:53 +00:00
/// FROM database.table or FROM table or FROM (subquery) or FROM tableFunction(...)
if (s_from.ignore(pos, expected))
{
if (!ParserTablesInSelectQuery().parse(pos, tables, expected))
return false;
}
/// PREWHERE expr
if (s_prewhere.ignore(pos, expected))
{
if (!exp_elem.parse(pos, prewhere_expression, expected))
return false;
}
/// WHERE expr
if (s_where.ignore(pos, expected))
{
if (!exp_elem.parse(pos, where_expression, expected))
return false;
}
2011-08-28 00:31:30 +00:00
/// GROUP BY expr list
if (s_group_by.ignore(pos, expected))
{
2018-08-24 15:00:00 +00:00
if (s_rollup.ignore(pos, expected))
select_query->group_by_with_rollup = true;
2018-09-17 18:01:04 +00:00
else if (s_cube.ignore(pos, expected))
select_query->group_by_with_cube = true;
if ((select_query->group_by_with_rollup || select_query->group_by_with_cube) && !open_bracket.ignore(pos, expected))
return false;
2018-08-24 15:00:00 +00:00
if (!exp_list.parse(pos, group_expression_list, expected))
return false;
2018-08-24 15:00:00 +00:00
2018-09-17 18:01:04 +00:00
if ((select_query->group_by_with_rollup || select_query->group_by_with_cube) && !close_bracket.ignore(pos, expected))
2018-08-24 15:00:00 +00:00
return false;
}
2018-09-19 11:18:38 +00:00
/// WITH ROLLUP, CUBE or TOTALS
if (s_with.ignore(pos, expected))
{
2018-08-27 16:43:29 +00:00
if (s_rollup.ignore(pos, expected))
select_query->group_by_with_rollup = true;
2018-09-19 11:18:38 +00:00
else if (s_cube.ignore(pos, expected))
select_query->group_by_with_cube = true;
2018-08-27 16:43:29 +00:00
else if (s_totals.ignore(pos, expected))
select_query->group_by_with_totals = true;
else
return false;
2018-08-27 16:43:29 +00:00
}
2018-08-27 09:50:30 +00:00
2018-09-03 09:49:04 +00:00
/// WITH TOTALS
2018-08-27 16:43:29 +00:00
if (s_with.ignore(pos, expected))
{
if (select_query->group_by_with_totals || !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, 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, order_expression_list, expected))
return false;
}
2011-08-28 00:31:30 +00:00
/// This is needed for TOP expression, because it can also use WITH TIES.
bool limit_with_ties_occured = false;
2019-04-29 13:12:39 +00:00
/// LIMIT length | LIMIT offset, length | LIMIT count BY expr-list | LIMIT offset, length BY expr-list
if (s_limit.ignore(pos, expected))
{
ParserToken s_comma(TokenType::Comma);
if (!exp_elem.parse(pos, limit_length, expected))
return false;
if (s_comma.ignore(pos, expected))
{
limit_offset = limit_length;
if (!exp_elem.parse(pos, limit_length, expected))
return false;
2019-04-18 08:24:06 +00:00
if (s_with_ties.ignore(pos, expected))
{
limit_with_ties_occured = true;
2019-04-18 08:24:06 +00:00
select_query->limit_with_ties = true;
}
}
2019-04-29 13:12:39 +00:00
else if (s_offset.ignore(pos, expected))
{
2019-04-29 13:12:39 +00:00
if (!exp_elem.parse(pos, limit_offset, expected))
return false;
}
2019-04-18 08:24:06 +00:00
else if (s_with_ties.ignore(pos, expected))
{
limit_with_ties_occured = true;
2019-04-18 08:24:06 +00:00
select_query->limit_with_ties = true;
}
2019-04-29 13:12:39 +00:00
if (s_by.ignore(pos, expected))
{
2019-11-06 14:18:28 +00:00
/// WITH TIES was used alongside LIMIT BY
/// But there are other kind of queries like LIMIT n BY smth LIMIT m WITH TIES which are allowed.
/// So we have to ignore WITH TIES exactly in LIMIT BY state.
if (limit_with_ties_occured)
2019-11-06 14:18:28 +00:00
throw Exception("Can not use WITH TIES alongside LIMIT BY", ErrorCodes::LIMIT_BY_WITH_TIES_IS_NOT_SUPPORTED);
2019-04-29 13:12:39 +00:00
limit_by_length = limit_length;
limit_by_offset = limit_offset;
limit_length = nullptr;
limit_offset = nullptr;
if (!exp_list.parse(pos, limit_by_expression_list, expected))
return false;
}
2019-11-18 10:47:27 +00:00
if (top_length)
{
if (limit_length)
throw Exception("Can not use TOP and LIMIT together", ErrorCodes::TOP_AND_LIMIT_TOGETHER);
limit_length = top_length;
}
}
2019-04-18 08:24:06 +00:00
/// LIMIT length [WITH TIES] | LIMIT offset, length [WITH TIES]
if (s_limit.ignore(pos, expected))
{
2019-11-06 14:18:28 +00:00
if (!limit_by_length || limit_length)
return false;
ParserToken s_comma(TokenType::Comma);
if (!exp_elem.parse(pos, limit_length, expected))
return false;
if (s_comma.ignore(pos, expected))
{
limit_offset = limit_length;
if (!exp_elem.parse(pos, limit_length, expected))
return false;
}
2019-04-29 13:12:39 +00:00
else if (s_offset.ignore(pos, expected))
{
if (!exp_elem.parse(pos, limit_offset, expected))
return false;
}
2019-04-18 08:24:06 +00:00
if (s_with_ties.ignore(pos, expected))
select_query->limit_with_ties = true;
}
2019-04-18 12:34:10 +00:00
/// WITH TIES was used without ORDER BY
2019-04-18 08:24:06 +00:00
if (!order_expression_list && select_query->limit_with_ties)
2019-04-18 12:34:10 +00:00
throw Exception("Can not use WITH TIES without ORDER BY", ErrorCodes::WITH_TIES_WITHOUT_ORDER_BY);
2019-04-18 08:24:06 +00:00
/// SETTINGS key1 = value1, key2 = value2, ...
if (s_settings.ignore(pos, expected))
{
ParserSetQuery parser_settings(true);
if (!parser_settings.parse(pos, settings, expected))
return false;
}
select_query->setExpression(ASTSelectQuery::Expression::WITH, std::move(with_expression_list));
select_query->setExpression(ASTSelectQuery::Expression::SELECT, std::move(select_expression_list));
select_query->setExpression(ASTSelectQuery::Expression::TABLES, std::move(tables));
select_query->setExpression(ASTSelectQuery::Expression::PREWHERE, std::move(prewhere_expression));
select_query->setExpression(ASTSelectQuery::Expression::WHERE, std::move(where_expression));
select_query->setExpression(ASTSelectQuery::Expression::GROUP_BY, std::move(group_expression_list));
select_query->setExpression(ASTSelectQuery::Expression::HAVING, std::move(having_expression));
select_query->setExpression(ASTSelectQuery::Expression::ORDER_BY, std::move(order_expression_list));
2019-04-29 13:12:39 +00:00
select_query->setExpression(ASTSelectQuery::Expression::LIMIT_BY_OFFSET, std::move(limit_by_offset));
select_query->setExpression(ASTSelectQuery::Expression::LIMIT_BY_LENGTH, std::move(limit_by_length));
select_query->setExpression(ASTSelectQuery::Expression::LIMIT_BY, std::move(limit_by_expression_list));
select_query->setExpression(ASTSelectQuery::Expression::LIMIT_OFFSET, std::move(limit_offset));
select_query->setExpression(ASTSelectQuery::Expression::LIMIT_LENGTH, std::move(limit_length));
select_query->setExpression(ASTSelectQuery::Expression::SETTINGS, std::move(settings));
return true;
2011-08-28 00:31:30 +00:00
}
}