Merge pull request #41095 from evillique/from-select

Add 'FROM table SELECT column' syntax
This commit is contained in:
Nikolay Degterinsky 2022-12-02 20:17:44 +01:00 committed by GitHub
commit c2ceb783ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 9 deletions

View File

@ -108,6 +108,13 @@ bool ParserSelectQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
}
}
/// FROM database.table or FROM table or FROM (subquery) or FROM tableFunction(...)
if (s_from.ignore(pos, expected))
{
if (!ParserTablesInSelectQuery(false).parse(pos, tables, expected))
return false;
}
/// SELECT [ALL/DISTINCT [ON (expr_list)]] [TOP N [WITH TIES]] expr_list
{
bool has_all = false;
@ -166,7 +173,7 @@ bool ParserSelectQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
}
/// FROM database.table or FROM table or FROM (subquery) or FROM tableFunction(...)
if (s_from.ignore(pos, expected))
if (!tables && s_from.ignore(pos, expected))
{
if (!ParserTablesInSelectQuery().parse(pos, tables, expected))
return false;

View File

@ -21,9 +21,9 @@ bool ParserTableExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
{
auto res = std::make_shared<ASTTableExpression>();
if (!ParserWithOptionalAlias(std::make_unique<ParserSubquery>(), true).parse(pos, res->subquery, expected)
&& !ParserWithOptionalAlias(std::make_unique<ParserFunction>(false, true), true).parse(pos, res->table_function, expected)
&& !ParserWithOptionalAlias(std::make_unique<ParserCompoundIdentifier>(true, true), true)
if (!ParserWithOptionalAlias(std::make_unique<ParserSubquery>(), allow_alias_without_as_keyword).parse(pos, res->subquery, expected)
&& !ParserWithOptionalAlias(std::make_unique<ParserFunction>(false, true), allow_alias_without_as_keyword).parse(pos, res->table_function, expected)
&& !ParserWithOptionalAlias(std::make_unique<ParserCompoundIdentifier>(true, true), allow_alias_without_as_keyword)
.parse(pos, res->database_and_table_name, expected))
return false;
@ -126,7 +126,7 @@ bool ParserTablesInSelectQueryElement::parseImpl(Pos & pos, ASTPtr & node, Expec
if (is_first)
{
if (!ParserTableExpression().parse(pos, res->table_expression, expected))
if (!ParserTableExpression(allow_alias_without_as_keyword).parse(pos, res->table_expression, expected))
return false;
}
else if (ParserArrayJoin().parse(pos, res->array_join, expected))
@ -200,7 +200,7 @@ bool ParserTablesInSelectQueryElement::parseImpl(Pos & pos, ASTPtr & node, Expec
return false;
}
if (!ParserTableExpression().parse(pos, res->table_expression, expected))
if (!ParserTableExpression(allow_alias_without_as_keyword).parse(pos, res->table_expression, expected))
return false;
if (table_join->kind != JoinKind::Comma
@ -261,12 +261,12 @@ bool ParserTablesInSelectQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & e
ASTPtr child;
if (ParserTablesInSelectQueryElement(true).parse(pos, child, expected))
if (ParserTablesInSelectQueryElement(true, allow_alias_without_as_keyword).parse(pos, child, expected))
res->children.emplace_back(child);
else
return false;
while (ParserTablesInSelectQueryElement(false).parse(pos, child, expected))
while (ParserTablesInSelectQueryElement(false, allow_alias_without_as_keyword).parse(pos, child, expected))
res->children.emplace_back(child);
node = res;

View File

@ -12,16 +12,24 @@ struct ASTTableJoin;
*/
class ParserTablesInSelectQuery : public IParserBase
{
public:
explicit ParserTablesInSelectQuery(bool allow_alias_without_as_keyword_ = true)
: allow_alias_without_as_keyword(allow_alias_without_as_keyword_) {}
protected:
const char * getName() const override { return "table, table function, subquery or list of joined tables"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
private:
bool allow_alias_without_as_keyword;
};
class ParserTablesInSelectQueryElement : public IParserBase
{
public:
explicit ParserTablesInSelectQueryElement(bool is_first_) : is_first(is_first_) {}
explicit ParserTablesInSelectQueryElement(bool is_first_, bool allow_alias_without_as_keyword_ = true)
: is_first(is_first_), allow_alias_without_as_keyword(allow_alias_without_as_keyword_) {}
protected:
const char * getName() const override { return "table, table function, subquery or list of joined tables"; }
@ -29,6 +37,7 @@ protected:
private:
bool is_first;
bool allow_alias_without_as_keyword;
static void parseJoinStrictness(Pos & pos, ASTTableJoin & table_join);
};
@ -36,9 +45,16 @@ private:
class ParserTableExpression : public IParserBase
{
public:
explicit ParserTableExpression(bool allow_alias_without_as_keyword_ = true)
: allow_alias_without_as_keyword(allow_alias_without_as_keyword_) {}
protected:
const char * getName() const override { return "table or subquery or table function"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
private:
bool allow_alias_without_as_keyword;
};

View File

@ -0,0 +1,4 @@
0
0
0
0

View File

@ -0,0 +1,4 @@
FROM numbers(1) SELECT number;
WITH 1 as n FROM numbers(1) SELECT number * n;
FROM (FROM numbers(1) SELECT *) SELECT number;
FROM (FROM numbers(1) SELECT *) AS select SELECT number;