Skip to content

Commit 41cafb0

Browse files
committed
Fix FROM-first SELECT syntax with WITH clause and nested subqueries
Handle two cases that were not working: 1. WITH clause followed by FROM-first syntax: `WITH 1 as n FROM t SELECT n` 2. Nested FROM-first syntax in subqueries: `FROM (FROM t SELECT *) SELECT x` In parseSelect(), check for FROM token after WITH clause and parse the table expression before expecting SELECT. In parseTableExpression(), add FROM token check to recognize FROM-first subqueries. Fixes 3 statements in 02417_from_select_syntax.
1 parent c5f3dca commit 41cafb0

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

parser/parser.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,16 @@ func (p *Parser) parseSelect() *ast.SelectQuery {
806806
sel.With = p.parseWithClause()
807807
}
808808

809-
if !p.expect(token.SELECT) {
809+
// Handle FROM ... SELECT syntax (ClickHouse extension)
810+
// This can come after WITH clause: WITH 1 as n FROM ... SELECT ...
811+
if p.currentIs(token.FROM) {
812+
p.nextToken() // skip FROM
813+
sel.From = p.parseTablesInSelect()
814+
// Now expect SELECT
815+
if !p.expect(token.SELECT) {
816+
return nil
817+
}
818+
} else if !p.expect(token.SELECT) {
810819
return nil
811820
}
812821

@@ -1408,6 +1417,10 @@ func (p *Parser) parseTableExpression() *ast.TableExpression {
14081417
// SELECT, WITH, or nested (SELECT...) for UNION queries like ((SELECT 1) UNION ALL SELECT 2)
14091418
subquery := p.parseSelectWithUnion()
14101419
expr.Table = &ast.Subquery{Query: subquery}
1420+
} else if p.currentIs(token.FROM) {
1421+
// FROM ... SELECT (ClickHouse extension) - e.g., FROM (FROM numbers(1) SELECT *)
1422+
subquery := p.parseFromSelectSyntax()
1423+
expr.Table = &ast.Subquery{Query: subquery}
14111424
} else if p.currentIs(token.EXPLAIN) {
14121425
// EXPLAIN as subquery in FROM clause
14131426
explain := p.parseExplain()
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
11
{
2-
"explain_todo": {
3-
"stmt2": true,
4-
"stmt3": true,
5-
"stmt4": true
6-
}
72
}

0 commit comments

Comments
 (0)