Skip to content

Commit 35d1e5f

Browse files
kyleconroyclaude
andcommitted
Handle FROM (SELECT...) as clause keyword after trailing comma
When parsing column list with trailing comma, recognize FROM (SELECT...) and FROM (WITH...) as FROM clause with subquery, not as a function call. This fixes parsing of queries like: SELECT x, count(), FROM (SELECT ...) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent c370f54 commit 35d1e5f

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

parser/expression.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,16 @@ func (p *Parser) isClauseKeyword() bool {
185185
case token.RPAREN, token.SEMICOLON, token.EOF:
186186
return true
187187
// FROM is a clause keyword unless followed by ( or [ (function/index access)
188+
// Exception: FROM (SELECT ...) or FROM (WITH ...) is a subquery, not a function call
188189
case token.FROM:
189-
return !p.peekIs(token.LPAREN) && !p.peekIs(token.LBRACKET)
190+
if p.peekIs(token.LPAREN) {
191+
// Check if it's FROM (SELECT...) or FROM (WITH...) - that's a subquery
192+
if p.peekPeekIs(token.SELECT) || p.peekPeekIs(token.WITH) {
193+
return true
194+
}
195+
return false
196+
}
197+
return !p.peekIs(token.LBRACKET)
190198
// These keywords can be used as identifiers in ClickHouse
191199
// Only treat as clause keywords if NOT followed by expression-like tokens
192200
case token.WHERE, token.GROUP, token.HAVING, token.ORDER, token.LIMIT:
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt8": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt5": true
4-
}
5-
}
1+
{}

0 commit comments

Comments
 (0)