Skip to content
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

Fix JOIN conditions in correlated JOINs #257

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,11 @@ type Join struct {
Method JoinMethod
Hint *Hint // optional
Left, Right TableExpr
Cond JoinCondition // nil when Op is CrossJoin, otherwise it must be set.

// nil when Op is CrossJoin
// optional when Right is PathTableExpr or Unnest
// otherwise it must be set.
Cond JoinCondition
}

// On is ON condition of JOIN expression.
Expand Down
23 changes: 14 additions & 9 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,18 +953,16 @@ func (p *Parser) parseTableExpr(toplevel bool) ast.TableExpr {
hint := p.tryParseHint()
right := p.parseSimpleTableExpr()

if op == ast.CrossJoin || op == ast.CommaJoin {
join = &ast.Join{
Op: op,
Method: method,
Hint: hint,
Left: join,
Right: right,
var cond ast.JoinCondition
if op != ast.CrossJoin && op != ast.CommaJoin {
switch right.(type) {
case *ast.PathTableExpr, *ast.Unnest:
cond = p.tryParseJoinCondition()
default:
cond = p.parseJoinCondition()
}
continue
}

cond := p.parseJoinCondition()
join = &ast.Join{
Op: op,
Method: method,
Expand Down Expand Up @@ -1155,6 +1153,13 @@ func (p *Parser) parseJoinCondition() ast.JoinCondition {
panic(p.errorfAtToken(&p.Token, "expected token: ON, USING, but: %s", p.Token.Kind))
}

func (p *Parser) tryParseJoinCondition() ast.JoinCondition {
if p.Token.Kind != "ON" && p.Token.Kind != "USING" {
return nil
}
return p.parseJoinCondition()
}

func (p *Parser) parseOn() *ast.On {
pos := p.expect("ON").Pos
expr := p.parseExpr()
Expand Down
13 changes: 13 additions & 0 deletions testdata/input/query/select_from_inner_join_path_table_expr.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- https://cloud.google.com/spanner/docs/reference/standard-sql/query-syntax#correlated_join
SELECT A.name, item
FROM
UNNEST(
[
STRUCT(
'first' AS name,
[1, 2, 3, 4] AS items),
STRUCT(
'second' AS name,
[] AS items)]) AS A
INNER JOIN
A.items AS item
12 changes: 12 additions & 0 deletions testdata/input/query/select_from_join_unnest.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- https://cloud.google.com/spanner/docs/reference/standard-sql/query-syntax#correlated_join
SELECT *
FROM
Roster
JOIN
UNNEST(
ARRAY(
SELECT AS STRUCT *
FROM PlayerStats
WHERE PlayerStats.OpponentID = Roster.SchoolID
)) AS PlayerMatches
ON PlayerMatches.LastName = 'Buchanan'
13 changes: 13 additions & 0 deletions testdata/input/query/select_from_left_join_path_table_expr.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- https://cloud.google.com/spanner/docs/reference/standard-sql/query-syntax#correlated_join
SELECT A.name, item, ARRAY_LENGTH(A.items) item_count_for_name
FROM
UNNEST(
[
STRUCT(
'first' AS name,
[1, 2, 3, 4] AS items),
STRUCT(
'second' AS name,
[] AS items)]) AS A
LEFT JOIN
A.items AS item
196 changes: 196 additions & 0 deletions testdata/result/query/select_from_inner_join_path_table_expr.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
--- select_from_inner_join_path_table_expr.sql
-- https://cloud.google.com/spanner/docs/reference/standard-sql/query-syntax#correlated_join
SELECT A.name, item
FROM
UNNEST(
[
STRUCT(
'first' AS name,
[1, 2, 3, 4] AS items),
STRUCT(
'second' AS name,
[] AS items)]) AS A
INNER JOIN
A.items AS item

--- AST
&ast.QueryStatement{
Query: &ast.Select{
Select: 93,
Results: []ast.SelectItem{
&ast.ExprSelectItem{
Expr: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 100,
NameEnd: 101,
Name: "A",
},
&ast.Ident{
NamePos: 102,
NameEnd: 106,
Name: "name",
},
},
},
},
&ast.ExprSelectItem{
Expr: &ast.Ident{
NamePos: 108,
NameEnd: 112,
Name: "item",
},
},
},
From: &ast.From{
From: 113,
Source: &ast.Join{
Op: "INNER JOIN",
Left: &ast.Unnest{
Unnest: 120,
Rparen: 268,
Expr: &ast.ArrayLiteral{
Array: -1,
Lbrack: 132,
Rbrack: 267,
Values: []ast.Expr{
&ast.TypelessStructLiteral{
Struct: 140,
Rparen: 202,
Values: []ast.TypelessStructLiteralArg{
&ast.Alias{
Expr: &ast.StringLiteral{
ValuePos: 156,
ValueEnd: 163,
Value: "first",
},
As: &ast.AsAlias{
As: 164,
Alias: &ast.Ident{
NamePos: 167,
NameEnd: 171,
Name: "name",
},
},
},
&ast.Alias{
Expr: &ast.ArrayLiteral{
Array: -1,
Lbrack: 181,
Rbrack: 192,
Values: []ast.Expr{
&ast.IntLiteral{
ValuePos: 182,
ValueEnd: 183,
Base: 10,
Value: "1",
},
&ast.IntLiteral{
ValuePos: 185,
ValueEnd: 186,
Base: 10,
Value: "2",
},
&ast.IntLiteral{
ValuePos: 188,
ValueEnd: 189,
Base: 10,
Value: "3",
},
&ast.IntLiteral{
ValuePos: 191,
ValueEnd: 192,
Base: 10,
Value: "4",
},
},
},
As: &ast.AsAlias{
As: 194,
Alias: &ast.Ident{
NamePos: 197,
NameEnd: 202,
Name: "items",
},
},
},
},
},
&ast.TypelessStructLiteral{
Struct: 211,
Rparen: 266,
Values: []ast.TypelessStructLiteralArg{
&ast.Alias{
Expr: &ast.StringLiteral{
ValuePos: 229,
ValueEnd: 237,
Value: "second",
},
As: &ast.AsAlias{
As: 238,
Alias: &ast.Ident{
NamePos: 241,
NameEnd: 245,
Name: "name",
},
},
},
&ast.Alias{
Expr: &ast.ArrayLiteral{
Array: -1,
Lbrack: 255,
Rbrack: 256,
},
As: &ast.AsAlias{
As: 258,
Alias: &ast.Ident{
NamePos: 261,
NameEnd: 266,
Name: "items",
},
},
},
},
},
},
},
As: &ast.AsAlias{
As: 270,
Alias: &ast.Ident{
NamePos: 273,
NameEnd: 274,
Name: "A",
},
},
},
Right: &ast.PathTableExpr{
Path: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 292,
NameEnd: 293,
Name: "A",
},
&ast.Ident{
NamePos: 294,
NameEnd: 299,
Name: "items",
},
},
},
As: &ast.AsAlias{
As: 300,
Alias: &ast.Ident{
NamePos: 303,
NameEnd: 307,
Name: "item",
},
},
},
},
},
},
}

--- SQL
SELECT A.name, item FROM UNNEST([STRUCT("first" AS name, [1, 2, 3, 4] AS items), STRUCT("second" AS name, [] AS items)]) AS A INNER JOIN A.items AS item
Loading
Loading