2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/CommonParsers.h>
|
|
|
|
#include <Parsers/ExpressionElementParsers.h>
|
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
|
|
|
#include <Parsers/ParserSelectQuery.h>
|
|
|
|
#include <Parsers/ParserSampleRatio.h>
|
|
|
|
#include <Parsers/ParserTablesInSelectQuery.h>
|
2016-07-18 00:14:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int SYNTAX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserTableExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2016-07-18 00:14:24 +00:00
|
|
|
{
|
|
|
|
auto res = std::make_shared<ASTTableExpression>();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-09-08 15:20:28 +00:00
|
|
|
if (!ParserWithOptionalAlias(std::make_unique<ParserSubquery>(), allow_alias_without_as_keyword).parse(pos, res->subquery, expected)
|
2022-11-29 18:11:15 +00:00
|
|
|
&& !ParserWithOptionalAlias(std::make_unique<ParserFunction>(false, true), allow_alias_without_as_keyword).parse(pos, res->table_function, expected)
|
2022-09-08 15:20:28 +00:00
|
|
|
&& !ParserWithOptionalAlias(std::make_unique<ParserCompoundIdentifier>(true, true), allow_alias_without_as_keyword)
|
2023-08-01 20:33:42 +00:00
|
|
|
.parse(pos, res->database_and_table_name, expected)
|
|
|
|
&& !ParserWithOptionalAlias(std::make_unique<ParserTableAsStringLiteralIdentifier>(), allow_alias_without_as_keyword)
|
2020-11-12 13:25:46 +00:00
|
|
|
.parse(pos, res->database_and_table_name, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
/// FINAL
|
2017-07-10 03:28:12 +00:00
|
|
|
if (ParserKeyword("FINAL").ignore(pos, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
res->final = true;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
/// SAMPLE number
|
2017-07-10 03:28:12 +00:00
|
|
|
if (ParserKeyword("SAMPLE").ignore(pos, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
{
|
|
|
|
ParserSampleRatio ratio;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!ratio.parse(pos, res->sample_size, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
/// OFFSET number
|
2017-07-10 03:28:12 +00:00
|
|
|
if (ParserKeyword("OFFSET").ignore(pos, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!ratio.parse(pos, res->sample_offset, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
if (res->database_and_table_name)
|
|
|
|
res->children.emplace_back(res->database_and_table_name);
|
|
|
|
if (res->table_function)
|
|
|
|
res->children.emplace_back(res->table_function);
|
|
|
|
if (res->subquery)
|
|
|
|
res->children.emplace_back(res->subquery);
|
|
|
|
if (res->sample_size)
|
|
|
|
res->children.emplace_back(res->sample_size);
|
|
|
|
if (res->sample_offset)
|
|
|
|
res->children.emplace_back(res->sample_offset);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-26 15:49:00 +00:00
|
|
|
assert(res->database_and_table_name || res->table_function || res->subquery);
|
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
node = res;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserArrayJoin::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2016-07-18 00:14:24 +00:00
|
|
|
{
|
|
|
|
auto res = std::make_shared<ASTArrayJoin>();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
/// [LEFT] ARRAY JOIN expr list
|
|
|
|
Pos saved_pos = pos;
|
|
|
|
bool has_array_join = false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (ParserKeyword("LEFT ARRAY JOIN").ignore(pos, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
{
|
|
|
|
res->kind = ASTArrayJoin::Kind::Left;
|
|
|
|
has_array_join = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos = saved_pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
/// INNER may be specified explicitly, otherwise it is assumed as default.
|
2017-07-10 03:28:12 +00:00
|
|
|
ParserKeyword("INNER").ignore(pos, expected);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (ParserKeyword("ARRAY JOIN").ignore(pos, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
{
|
|
|
|
res->kind = ASTArrayJoin::Kind::Inner;
|
|
|
|
has_array_join = true;
|
|
|
|
}
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
if (!has_array_join)
|
|
|
|
return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!ParserExpressionList(false).parse(pos, res->expression_list, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
if (res->expression_list)
|
|
|
|
res->children.emplace_back(res->expression_list);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
node = res;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-16 08:56:45 +00:00
|
|
|
void ParserTablesInSelectQueryElement::parseJoinStrictness(Pos & pos, ASTTableJoin & table_join)
|
|
|
|
{
|
|
|
|
if (ParserKeyword("ANY").ignore(pos))
|
2022-07-29 16:30:50 +00:00
|
|
|
table_join.strictness = JoinStrictness::Any;
|
2020-07-16 08:56:45 +00:00
|
|
|
else if (ParserKeyword("ALL").ignore(pos))
|
2022-07-29 16:30:50 +00:00
|
|
|
table_join.strictness = JoinStrictness::All;
|
2020-07-16 08:56:45 +00:00
|
|
|
else if (ParserKeyword("ASOF").ignore(pos))
|
2022-07-29 16:30:50 +00:00
|
|
|
table_join.strictness = JoinStrictness::Asof;
|
2020-07-16 08:56:45 +00:00
|
|
|
else if (ParserKeyword("SEMI").ignore(pos))
|
2022-07-29 16:30:50 +00:00
|
|
|
table_join.strictness = JoinStrictness::Semi;
|
2020-07-16 08:56:45 +00:00
|
|
|
else if (ParserKeyword("ANTI").ignore(pos) || ParserKeyword("ONLY").ignore(pos))
|
2022-07-29 16:30:50 +00:00
|
|
|
table_join.strictness = JoinStrictness::Anti;
|
2020-07-16 08:56:45 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserTablesInSelectQueryElement::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2016-07-18 00:14:24 +00:00
|
|
|
{
|
|
|
|
auto res = std::make_shared<ASTTablesInSelectQueryElement>();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
Make malformed `array join` statement fail early
Currently array join clause that have no parent table can go through the parser and generates some confusing error.
```
create table aa (a Array(UInt64)) Engine=Memory;
select * from array join aa;
Received exception from server (version 1.1.54311):
Code: 208. DB::Exception: Received from localhost:9000, ::1. DB::Exception: No columns in nested table aa.
```
This patch makes it fail at client side.
```
Syntax error: failed at position 21:
select * from array join aa;
Expected one of: SAMPLE, INNER, WITH, HAVING, SETTINGS, identifier, Dot, ORDER BY, AS, GROUP BY, INTO OUTFILE, UNION ALL, LEFT ARRAY JOIN, ARRAY JOIN, table, table function, subquery or list of joined tables, array join, alias, FINAL, PREWHERE, WHERE, token, FORMAT, LIMIT
```
However I'm not sure if `ParserCompoundIdentifier` should be stricter so that `array` wouldn't be consider as a table name.
2017-11-21 07:16:15 +00:00
|
|
|
if (is_first)
|
2016-07-18 00:14:24 +00:00
|
|
|
{
|
2022-09-08 15:20:28 +00:00
|
|
|
if (!ParserTableExpression(allow_alias_without_as_keyword).parse(pos, res->table_expression, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
Make malformed `array join` statement fail early
Currently array join clause that have no parent table can go through the parser and generates some confusing error.
```
create table aa (a Array(UInt64)) Engine=Memory;
select * from array join aa;
Received exception from server (version 1.1.54311):
Code: 208. DB::Exception: Received from localhost:9000, ::1. DB::Exception: No columns in nested table aa.
```
This patch makes it fail at client side.
```
Syntax error: failed at position 21:
select * from array join aa;
Expected one of: SAMPLE, INNER, WITH, HAVING, SETTINGS, identifier, Dot, ORDER BY, AS, GROUP BY, INTO OUTFILE, UNION ALL, LEFT ARRAY JOIN, ARRAY JOIN, table, table function, subquery or list of joined tables, array join, alias, FINAL, PREWHERE, WHERE, token, FORMAT, LIMIT
```
However I'm not sure if `ParserCompoundIdentifier` should be stricter so that `array` wouldn't be consider as a table name.
2017-11-21 07:16:15 +00:00
|
|
|
else if (ParserArrayJoin().parse(pos, res->array_join, expected))
|
|
|
|
{
|
|
|
|
}
|
2016-07-18 00:14:24 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
auto table_join = std::make_shared<ASTTableJoin>();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type == TokenType::Comma)
|
2016-07-18 00:14:24 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2022-07-29 16:30:50 +00:00
|
|
|
table_join->kind = JoinKind::Comma;
|
2016-07-18 00:14:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (ParserKeyword("GLOBAL").ignore(pos))
|
2022-07-29 16:30:50 +00:00
|
|
|
table_join->locality = JoinLocality::Global;
|
2017-07-10 03:28:12 +00:00
|
|
|
else if (ParserKeyword("LOCAL").ignore(pos))
|
2022-07-29 16:30:50 +00:00
|
|
|
table_join->locality = JoinLocality::Local;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-07-29 16:30:50 +00:00
|
|
|
table_join->strictness = JoinStrictness::Unspecified;
|
2020-07-16 08:56:45 +00:00
|
|
|
|
|
|
|
/// Legacy: allow JOIN type before JOIN kind
|
|
|
|
parseJoinStrictness(pos, *table_join);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-07-16 08:56:45 +00:00
|
|
|
bool no_kind = false;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (ParserKeyword("INNER").ignore(pos))
|
2022-07-29 16:30:50 +00:00
|
|
|
table_join->kind = JoinKind::Inner;
|
2017-07-10 03:28:12 +00:00
|
|
|
else if (ParserKeyword("LEFT").ignore(pos))
|
2022-07-29 16:30:50 +00:00
|
|
|
table_join->kind = JoinKind::Left;
|
2017-07-10 03:28:12 +00:00
|
|
|
else if (ParserKeyword("RIGHT").ignore(pos))
|
2022-07-29 16:30:50 +00:00
|
|
|
table_join->kind = JoinKind::Right;
|
2017-07-10 03:28:12 +00:00
|
|
|
else if (ParserKeyword("FULL").ignore(pos))
|
2022-07-29 16:30:50 +00:00
|
|
|
table_join->kind = JoinKind::Full;
|
2017-07-10 03:28:12 +00:00
|
|
|
else if (ParserKeyword("CROSS").ignore(pos))
|
2022-07-29 16:30:50 +00:00
|
|
|
table_join->kind = JoinKind::Cross;
|
2016-07-18 00:14:24 +00:00
|
|
|
else
|
2020-07-16 08:56:45 +00:00
|
|
|
no_kind = true;
|
|
|
|
|
|
|
|
/// Standard position: JOIN type after JOIN kind
|
|
|
|
parseJoinStrictness(pos, *table_join);
|
|
|
|
|
|
|
|
/// Optional OUTER keyword for outer joins.
|
2022-07-29 16:30:50 +00:00
|
|
|
if (table_join->kind == JoinKind::Left
|
|
|
|
|| table_join->kind == JoinKind::Right
|
|
|
|
|| table_join->kind == JoinKind::Full)
|
2020-07-16 08:56:45 +00:00
|
|
|
{
|
|
|
|
ParserKeyword("OUTER").ignore(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (no_kind)
|
2016-07-18 00:14:24 +00:00
|
|
|
{
|
2018-09-17 13:33:19 +00:00
|
|
|
/// Use INNER by default as in another DBMS.
|
2022-07-29 16:30:50 +00:00
|
|
|
if (table_join->strictness == JoinStrictness::Semi ||
|
|
|
|
table_join->strictness == JoinStrictness::Anti)
|
|
|
|
table_join->kind = JoinKind::Left;
|
2019-12-02 18:07:27 +00:00
|
|
|
else
|
2022-07-29 16:30:50 +00:00
|
|
|
table_join->kind = JoinKind::Inner;
|
2016-07-18 00:14:24 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-07-29 16:30:50 +00:00
|
|
|
if (table_join->strictness != JoinStrictness::Unspecified
|
|
|
|
&& table_join->kind == JoinKind::Cross)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::SYNTAX_ERROR, "You must not specify ANY or ALL for CROSS JOIN.");
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-07-29 16:30:50 +00:00
|
|
|
if ((table_join->strictness == JoinStrictness::Semi || table_join->strictness == JoinStrictness::Anti) &&
|
|
|
|
(table_join->kind != JoinKind::Left && table_join->kind != JoinKind::Right))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::SYNTAX_ERROR, "SEMI|ANTI JOIN should be LEFT or RIGHT.");
|
2019-12-02 18:07:27 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!ParserKeyword("JOIN").ignore(pos, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-09-08 15:20:28 +00:00
|
|
|
if (!ParserTableExpression(allow_alias_without_as_keyword).parse(pos, res->table_expression, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-07-29 16:30:50 +00:00
|
|
|
if (table_join->kind != JoinKind::Comma
|
|
|
|
&& table_join->kind != JoinKind::Cross)
|
2016-07-18 00:14:24 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (ParserKeyword("USING").ignore(pos, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
{
|
|
|
|
/// Expression for USING could be in parentheses or not.
|
2017-07-10 03:28:12 +00:00
|
|
|
bool in_parens = pos->type == TokenType::OpeningRoundBracket;
|
2016-07-18 00:14:24 +00:00
|
|
|
if (in_parens)
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!ParserExpressionList(false).parse(pos, table_join->using_expression_list, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
if (in_parens)
|
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type != TokenType::ClosingRoundBracket)
|
2016-07-18 00:14:24 +00:00
|
|
|
return false;
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2016-07-18 00:14:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-10 03:28:12 +00:00
|
|
|
else if (ParserKeyword("ON").ignore(pos, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
{
|
2022-09-07 21:41:37 +00:00
|
|
|
if (!ParserExpression().parse(pos, table_join->on_expression, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
if (table_join->using_expression_list)
|
|
|
|
table_join->children.emplace_back(table_join->using_expression_list);
|
|
|
|
if (table_join->on_expression)
|
|
|
|
table_join->children.emplace_back(table_join->on_expression);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
res->table_join = table_join;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
if (res->table_expression)
|
|
|
|
res->children.emplace_back(res->table_expression);
|
|
|
|
if (res->table_join)
|
|
|
|
res->children.emplace_back(res->table_join);
|
|
|
|
if (res->array_join)
|
|
|
|
res->children.emplace_back(res->array_join);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-18 00:14:24 +00:00
|
|
|
node = res;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserTablesInSelectQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2016-07-18 00:14:24 +00:00
|
|
|
{
|
|
|
|
auto res = std::make_shared<ASTTablesInSelectQuery>();
|
|
|
|
|
|
|
|
ASTPtr child;
|
|
|
|
|
2022-09-08 15:20:28 +00:00
|
|
|
if (ParserTablesInSelectQueryElement(true, allow_alias_without_as_keyword).parse(pos, child, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
res->children.emplace_back(child);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
2022-09-08 15:20:28 +00:00
|
|
|
while (ParserTablesInSelectQueryElement(false, allow_alias_without_as_keyword).parse(pos, child, expected))
|
2016-07-18 00:14:24 +00:00
|
|
|
res->children.emplace_back(child);
|
|
|
|
|
2016-07-18 01:20:53 +00:00
|
|
|
node = res;
|
2016-07-18 00:14:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|