2016-03-07 01:08:01 +00:00
|
|
|
#include <memory>
|
2021-06-22 11:25:14 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#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>
|
2020-09-12 17:00:04 +00:00
|
|
|
#include <Parsers/ParserWithElement.h>
|
2022-03-17 05:51:35 +00:00
|
|
|
#include <Parsers/ASTOrderByElement.h>
|
2022-03-24 19:29:29 +00:00
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
#include <Parsers/ASTInterpolateElement.h>
|
2022-03-28 23:15:53 +00:00
|
|
|
#include <Parsers/ASTIdentifier.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
|
|
|
|
{
|
2021-07-06 09:22:08 +00:00
|
|
|
extern const int FIRST_AND_NEXT_TOGETHER;
|
2019-11-05 15:49:13 +00:00
|
|
|
extern const int LIMIT_BY_WITH_TIES_IS_NOT_SUPPORTED;
|
2020-10-13 03:32:43 +00:00
|
|
|
extern const int ROW_AND_ROWS_TOGETHER;
|
2021-07-06 16:14:22 +00:00
|
|
|
extern const int SYNTAX_ERROR;
|
2021-07-06 09:22:08 +00:00
|
|
|
extern const int TOP_AND_LIMIT_TOGETHER;
|
|
|
|
extern const int WITH_TIES_WITHOUT_ORDER_BY;
|
2021-11-03 05:50:08 +00:00
|
|
|
extern const int OFFSET_FETCH_WITHOUT_ORDER_BY;
|
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");
|
2021-01-04 14:23:17 +00:00
|
|
|
ParserKeyword s_all("ALL");
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_distinct("DISTINCT");
|
2021-06-22 11:25:14 +00:00
|
|
|
ParserKeyword s_distinct_on("DISTINCT ON");
|
2017-06-18 03:07:03 +00:00
|
|
|
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");
|
2021-01-12 18:34:35 +00:00
|
|
|
ParserKeyword s_window("WINDOW");
|
2017-06-18 03:07:03 +00:00
|
|
|
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");
|
2021-02-09 18:40:10 +00:00
|
|
|
ParserKeyword s_grouping_sets("GROUPING SETS");
|
2018-08-10 11:12:43 +00:00
|
|
|
ParserKeyword s_top("TOP");
|
2019-04-18 08:24:06 +00:00
|
|
|
ParserKeyword s_with_ties("WITH TIES");
|
2018-08-10 11:12:43 +00:00
|
|
|
ParserKeyword s_offset("OFFSET");
|
2020-10-12 08:46:49 +00:00
|
|
|
ParserKeyword s_fetch("FETCH");
|
|
|
|
ParserKeyword s_only("ONLY");
|
|
|
|
ParserKeyword s_row("ROW");
|
|
|
|
ParserKeyword s_rows("ROWS");
|
|
|
|
ParserKeyword s_first("FIRST");
|
|
|
|
ParserKeyword s_next("NEXT");
|
2022-03-17 05:51:35 +00:00
|
|
|
ParserKeyword s_interpolate("INTERPOLATE");
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
ParserNotEmptyExpressionList exp_list(false);
|
2019-07-22 19:21:07 +00:00
|
|
|
ParserNotEmptyExpressionList exp_list_for_with_clause(false);
|
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;
|
2021-05-15 12:14:58 +00:00
|
|
|
ParserGroupingSetsExpressionList grouping_sets_list;
|
2022-03-17 05:51:35 +00:00
|
|
|
ParserInterpolateExpressionList interpolate_list;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-08-24 15:00:00 +00:00
|
|
|
ParserToken open_bracket(TokenType::OpeningRoundBracket);
|
|
|
|
ParserToken close_bracket(TokenType::ClosingRoundBracket);
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
ASTPtr with_expression_list;
|
|
|
|
ASTPtr select_expression_list;
|
|
|
|
ASTPtr tables;
|
|
|
|
ASTPtr prewhere_expression;
|
|
|
|
ASTPtr where_expression;
|
|
|
|
ASTPtr group_expression_list;
|
|
|
|
ASTPtr having_expression;
|
2021-01-12 18:34:35 +00:00
|
|
|
ASTPtr window_list;
|
2019-04-09 14:22:35 +00:00
|
|
|
ASTPtr order_expression_list;
|
2022-03-17 05:51:35 +00:00
|
|
|
ASTPtr interpolate_expression_list;
|
2019-04-29 13:12:39 +00:00
|
|
|
ASTPtr limit_by_length;
|
|
|
|
ASTPtr limit_by_offset;
|
2019-04-09 14:22:35 +00:00
|
|
|
ASTPtr limit_by_expression_list;
|
2021-07-06 09:17:26 +00:00
|
|
|
ASTPtr distinct_on_expression_list;
|
2019-04-09 14:22:35 +00:00
|
|
|
ASTPtr limit_offset;
|
|
|
|
ASTPtr limit_length;
|
2019-11-13 14:26:51 +00:00
|
|
|
ASTPtr top_length;
|
2019-04-09 14:22:35 +00:00
|
|
|
ASTPtr settings;
|
|
|
|
|
2021-07-06 09:17:26 +00:00
|
|
|
/// WITH expr_list
|
2017-08-10 14:46:46 +00:00
|
|
|
{
|
|
|
|
if (s_with.ignore(pos, expected))
|
|
|
|
{
|
2020-09-12 17:00:04 +00:00
|
|
|
if (!ParserList(std::make_unique<ParserWithElement>(), std::make_unique<ParserToken>(TokenType::Comma))
|
|
|
|
.parse(pos, with_expression_list, expected))
|
|
|
|
return false;
|
|
|
|
if (with_expression_list->children.empty())
|
2017-08-10 14:46:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-08 00:30:44 +00:00
|
|
|
/// FROM database.table or FROM table or FROM (subquery) or FROM tableFunction(...)
|
|
|
|
if (s_from.ignore(pos, expected))
|
|
|
|
{
|
2022-09-08 15:20:28 +00:00
|
|
|
if (!ParserTablesInSelectQuery(false).parse(pos, tables, expected))
|
2022-09-08 00:30:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-06 09:17:26 +00:00
|
|
|
/// SELECT [ALL/DISTINCT [ON (expr_list)]] [TOP N [WITH TIES]] expr_list
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2021-01-04 14:23:17 +00:00
|
|
|
bool has_all = false;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!s_select.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2021-01-04 14:23:17 +00:00
|
|
|
if (s_all.ignore(pos, expected))
|
|
|
|
has_all = true;
|
|
|
|
|
2021-06-22 11:47:17 +00:00
|
|
|
if (s_distinct_on.ignore(pos, expected))
|
|
|
|
{
|
2021-07-06 09:17:26 +00:00
|
|
|
if (open_bracket.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (!exp_list.parse(pos, distinct_on_expression_list, expected))
|
|
|
|
return false;
|
|
|
|
if (!close_bracket.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
2021-06-22 11:25:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-07-06 09:17:26 +00:00
|
|
|
else if (s_distinct.ignore(pos, expected))
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
select_query->distinct = true;
|
2021-07-06 09:17:26 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-01-04 14:23:17 +00:00
|
|
|
if (!has_all && s_all.ignore(pos, expected))
|
|
|
|
has_all = true;
|
|
|
|
|
2021-07-06 09:17:26 +00:00
|
|
|
if (has_all && (select_query->distinct || distinct_on_expression_list))
|
2021-01-05 04:54:02 +00:00
|
|
|
return false;
|
2021-01-04 14:23:17 +00:00
|
|
|
|
2018-08-10 11:12:43 +00:00
|
|
|
if (s_top.ignore(pos, expected))
|
|
|
|
{
|
2019-02-10 18:19:26 +00:00
|
|
|
ParserNumber num;
|
|
|
|
|
2018-08-10 11:12:43 +00:00
|
|
|
if (open_bracket.ignore(pos, expected))
|
|
|
|
{
|
2019-11-13 14:26:51 +00:00
|
|
|
if (!num.parse(pos, top_length, expected))
|
2018-08-10 11:12:43 +00:00
|
|
|
return false;
|
|
|
|
if (!close_bracket.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-13 14:26:51 +00:00
|
|
|
if (!num.parse(pos, top_length, expected))
|
2018-08-10 11:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-18 08:24:06 +00:00
|
|
|
|
|
|
|
if (s_with_ties.ignore(pos, expected))
|
|
|
|
select_query->limit_with_ties = true;
|
2018-08-10 11:12:43 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!exp_list_for_select_clause.parse(pos, 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(...)
|
2022-09-08 00:30:44 +00:00
|
|
|
if (!tables && s_from.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!ParserTablesInSelectQuery().parse(pos, 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
|
|
|
{
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!exp_elem.parse(pos, 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
|
|
|
{
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!exp_elem.parse(pos, 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
|
|
|
{
|
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;
|
2021-02-09 18:40:10 +00:00
|
|
|
else if (s_grouping_sets.ignore(pos, expected))
|
|
|
|
select_query->group_by_with_grouping_sets = true;
|
2022-10-12 16:50:56 +00:00
|
|
|
else if (s_all.ignore(pos, expected))
|
|
|
|
select_query->group_by_all = true;
|
2018-09-17 18:01:04 +00:00
|
|
|
|
2021-02-09 18:40:10 +00:00
|
|
|
if ((select_query->group_by_with_rollup || select_query->group_by_with_cube || select_query->group_by_with_grouping_sets) &&
|
|
|
|
!open_bracket.ignore(pos, expected))
|
2018-09-17 18:01:04 +00:00
|
|
|
return false;
|
2018-08-24 15:00:00 +00:00
|
|
|
|
2021-05-15 12:14:58 +00:00
|
|
|
if (select_query->group_by_with_grouping_sets)
|
|
|
|
{
|
|
|
|
if (!grouping_sets_list.parse(pos, group_expression_list, expected))
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-12 16:50:56 +00:00
|
|
|
else if (!select_query->group_by_all)
|
2021-05-15 12:14:58 +00:00
|
|
|
{
|
|
|
|
if (!exp_list.parse(pos, group_expression_list, expected))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:00:00 +00:00
|
|
|
|
2021-02-09 18:40:10 +00:00
|
|
|
if ((select_query->group_by_with_rollup || select_query->group_by_with_cube || select_query->group_by_with_grouping_sets) &&
|
|
|
|
!close_bracket.ignore(pos, expected))
|
2018-08-24 15:00:00 +00:00
|
|
|
return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 20:43:03 +00:00
|
|
|
/// WITH ROLLUP, CUBE, GROUPING SETS or TOTALS
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_with.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
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
|
2017-04-01 07:20:54 +00:00
|
|
|
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;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// HAVING expr
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_having.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!exp_elem.parse(pos, having_expression, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-08-28 00:31:30 +00:00
|
|
|
|
2021-01-12 18:34:35 +00:00
|
|
|
/// WINDOW clause
|
|
|
|
if (s_window.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
ParserWindowList window_list_parser;
|
|
|
|
if (!window_list_parser.parse(pos, window_list, expected))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!order_list.parse(pos, order_expression_list, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2022-03-17 05:51:35 +00:00
|
|
|
|
|
|
|
/// if any WITH FILL parse possible INTERPOLATE list
|
2022-03-17 06:31:01 +00:00
|
|
|
if (std::any_of(order_expression_list->children.begin(), order_expression_list->children.end(),
|
|
|
|
[](auto & child) { return child->template as<ASTOrderByElement>()->with_fill; }))
|
2022-03-17 05:51:35 +00:00
|
|
|
{
|
|
|
|
if (s_interpolate.ignore(pos, expected))
|
|
|
|
{
|
2022-03-24 19:29:29 +00:00
|
|
|
if (open_bracket.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (!interpolate_list.parse(pos, interpolate_expression_list, expected))
|
|
|
|
return false;
|
|
|
|
if (!close_bracket.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
} else
|
|
|
|
interpolate_expression_list = std::make_shared<ASTExpressionList>();
|
2022-03-17 05:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2011-08-28 00:31:30 +00:00
|
|
|
|
2019-11-13 14:26:51 +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
|
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
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!exp_elem.parse(pos, 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
|
|
|
{
|
2019-04-09 14:22:35 +00:00
|
|
|
limit_offset = limit_length;
|
|
|
|
if (!exp_elem.parse(pos, limit_length, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2019-04-18 08:24:06 +00:00
|
|
|
|
|
|
|
if (s_with_ties.ignore(pos, expected))
|
2019-11-13 14:26:51 +00:00
|
|
|
{
|
|
|
|
limit_with_ties_occured = true;
|
2019-04-18 08:24:06 +00:00
|
|
|
select_query->limit_with_ties = true;
|
2019-11-13 14:26:51 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2019-04-29 13:12:39 +00:00
|
|
|
else if (s_offset.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-04-29 13:12:39 +00:00
|
|
|
if (!exp_elem.parse(pos, limit_offset, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-18 08:24:06 +00:00
|
|
|
else if (s_with_ties.ignore(pos, expected))
|
2019-11-13 14:26:51 +00:00
|
|
|
{
|
|
|
|
limit_with_ties_occured = true;
|
2019-04-18 08:24:06 +00:00
|
|
|
select_query->limit_with_ties = true;
|
2019-11-13 14:26:51 +00:00
|
|
|
}
|
2019-08-14 13:40:07 +00:00
|
|
|
|
2021-07-06 09:17:26 +00:00
|
|
|
if (limit_with_ties_occured && distinct_on_expression_list)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LIMIT_BY_WITH_TIES_IS_NOT_SUPPORTED, "Can not use WITH TIES alongside LIMIT BY/DISTINCT ON");
|
2021-07-06 09:17:26 +00:00
|
|
|
|
2019-04-29 13:12:39 +00:00
|
|
|
if (s_by.ignore(pos, expected))
|
2018-08-10 11:12:43 +00:00
|
|
|
{
|
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.
|
2019-11-13 14:26:51 +00:00
|
|
|
if (limit_with_ties_occured)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LIMIT_BY_WITH_TIES_IS_NOT_SUPPORTED, "Can not use WITH TIES alongside LIMIT BY/DISTINCT ON");
|
2019-11-06 14:18:28 +00:00
|
|
|
|
2021-07-06 09:17:26 +00:00
|
|
|
if (distinct_on_expression_list)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::SYNTAX_ERROR, "Can not use DISTINCT ON alongside LIMIT BY");
|
2021-06-22 11:25:14 +00:00
|
|
|
|
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))
|
2018-08-10 11:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-13 14:26:51 +00:00
|
|
|
|
2019-11-19 15:00:11 +00:00
|
|
|
if (top_length && limit_length)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::TOP_AND_LIMIT_TOGETHER, "Can not use TOP and LIMIT together");
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2020-05-11 06:53:10 +00:00
|
|
|
else if (s_offset.ignore(pos, expected))
|
2020-05-04 12:52:38 +00:00
|
|
|
{
|
2020-10-13 03:32:43 +00:00
|
|
|
/// OFFSET offset_row_count {ROW | ROWS} FETCH {FIRST | NEXT} fetch_row_count {ROW | ROWS} {ONLY | WITH TIES}
|
|
|
|
bool offset_with_fetch_maybe = false;
|
|
|
|
|
2020-05-04 12:52:38 +00:00
|
|
|
if (!exp_elem.parse(pos, limit_offset, expected))
|
|
|
|
return false;
|
2020-10-13 03:32:43 +00:00
|
|
|
|
|
|
|
if (s_row.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (s_rows.ignore(pos, expected))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ROW_AND_ROWS_TOGETHER, "Can not use ROW and ROWS together");
|
2020-10-13 03:32:43 +00:00
|
|
|
offset_with_fetch_maybe = true;
|
|
|
|
}
|
|
|
|
else if (s_rows.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
offset_with_fetch_maybe = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset_with_fetch_maybe && s_fetch.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
/// OFFSET FETCH clause must exists with "ORDER BY"
|
|
|
|
if (!order_expression_list)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::OFFSET_FETCH_WITHOUT_ORDER_BY, "Can not use OFFSET FETCH clause without ORDER BY");
|
2020-10-13 03:32:43 +00:00
|
|
|
|
|
|
|
if (s_first.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (s_next.ignore(pos, expected))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::FIRST_AND_NEXT_TOGETHER, "Can not use FIRST and NEXT together");
|
2020-10-13 03:32:43 +00:00
|
|
|
}
|
|
|
|
else if (!s_next.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!exp_elem.parse(pos, limit_length, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (s_row.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (s_rows.ignore(pos, expected))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ROW_AND_ROWS_TOGETHER, "Can not use ROW and ROWS together");
|
2020-10-13 03:32:43 +00:00
|
|
|
}
|
|
|
|
else if (!s_rows.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (s_with_ties.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
select_query->limit_with_ties = true;
|
|
|
|
}
|
|
|
|
else if (s_only.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
select_query->limit_with_ties = false;
|
|
|
|
}
|
2020-10-14 07:05:02 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-13 03:32:43 +00:00
|
|
|
}
|
2020-05-04 12:52:38 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 09:17:26 +00:00
|
|
|
if (distinct_on_expression_list)
|
|
|
|
{
|
|
|
|
/// DISTINCT ON and LIMIT BY are mutually exclusive, checked before
|
|
|
|
assert (limit_by_expression_list == nullptr);
|
|
|
|
|
|
|
|
/// Transform `DISTINCT ON expr` to `LIMIT 1 BY expr`
|
|
|
|
limit_by_expression_list = distinct_on_expression_list;
|
2022-04-18 08:18:31 +00:00
|
|
|
limit_by_length = std::make_shared<ASTLiteral>(Field{static_cast<UInt8>(1)});
|
2021-07-07 09:48:39 +00:00
|
|
|
distinct_on_expression_list = nullptr;
|
2021-07-06 09:17:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 15:00:11 +00:00
|
|
|
/// Because TOP n in totally equals LIMIT n
|
|
|
|
if (top_length)
|
|
|
|
limit_length = top_length;
|
|
|
|
|
2019-04-18 08:24:06 +00:00
|
|
|
/// LIMIT length [WITH TIES] | LIMIT offset, length [WITH TIES]
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_limit.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-11-06 14:18:28 +00:00
|
|
|
if (!limit_by_length || limit_length)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
ParserToken s_comma(TokenType::Comma);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!exp_elem.parse(pos, 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
|
|
|
{
|
2019-04-09 14:22:35 +00:00
|
|
|
limit_offset = limit_length;
|
|
|
|
if (!exp_elem.parse(pos, limit_length, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
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;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::WITH_TIES_WITHOUT_ORDER_BY, "Can not use WITH TIES without ORDER BY");
|
2019-04-18 08:24:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// 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);
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!parser_settings.parse(pos, settings, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
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));
|
2021-01-12 18:34:35 +00:00
|
|
|
select_query->setExpression(ASTSelectQuery::Expression::WINDOW, std::move(window_list));
|
2019-04-09 14:22:35 +00:00
|
|
|
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));
|
2019-04-09 14:22:35 +00:00
|
|
|
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));
|
2022-03-17 05:51:35 +00:00
|
|
|
select_query->setExpression(ASTSelectQuery::Expression::INTERPOLATE, std::move(interpolate_expression_list));
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2011-08-28 00:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|