-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(spansql): support subquery in View Join #13266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4655,8 +4655,26 @@ func (p *parser) parseLit() (Expr, *parseError) { | |
| return BytesLiteral(tok.string), nil | ||
| } | ||
|
|
||
| // Handle parenthesized expressions. | ||
| // Handle parenthesized expressions and scalar subqueries. | ||
| // NOTE: The opening "(" has already been consumed by p.next() above (line 4638). | ||
| // The parser is now positioned right after the "(", ready to parse the contents. | ||
| if tok.value == "(" { | ||
| // Look ahead to see if this is a subquery like: (SELECT ...) | ||
| // p.sniff("SELECT") peeks at the next token without consuming it. | ||
| if p.sniff("SELECT") { | ||
| // Parse the subquery starting from the current position (after the "(") | ||
| q, err := p.parseQuery() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err := p.expect(")"); err != nil { | ||
| return nil, err | ||
| } | ||
| return ScalarSubquery{Query: q}, nil | ||
| } | ||
|
|
||
| // Regular parenthesized expression like: (1 + 2) | ||
| // Parse the inner expression starting from the current position (after the "(") | ||
| e, err := p.parseExpr() | ||
| if err != nil { | ||
| return nil, err | ||
|
Comment on lines
4661
to
4680
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic for handling parenthesized expressions and scalar subqueries appears to have a critical bug. The opening parenthesis
The fix is to consume the opening parenthesis if tok.value == "(" {
p.next() // Consume "(".
// Check if this is a scalar subquery.
tok := p.peek()
if tok.kind == keyword && strings.EqualFold(tok.value, "SELECT") {
q, err := p.parseQuery()
if err != nil {
return nil, err
}
if err := p.expect(")"); err != nil {
return nil, err
}
return ScalarSubquery{Query: q}, nil
}
// It's a regular parenthesized expression.
e, err := p.parseExpr()
if err != nil {
return nil, err
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No this is correct, I have just pushed a commit which validates it with tests and added an extra comment. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current lookahead only checks for
SELECTto identify a subquery. However, a subquery can also start with aWITHclause. This will cause parsing to fail for valid scalar subqueries that use common table expressions (CTEs).To fix this, you should also check for the
WITHkeyword.For example, a query like
SELECT (WITH t AS (SELECT 1) SELECT * FROM t)would fail to parse correctly.I'd also recommend adding a test case to
TestParseExprto cover subqueries that start with aWITHclause to prevent future regressions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WITH is currently not supported and this PR does not aim to resolve it either as it would grow to large.