Skip to content

Commit

Permalink
Fix JoinMethod handling (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
apstndb authored Nov 26, 2024
1 parent 75946e1 commit bb8d101
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 318 deletions.
6 changes: 3 additions & 3 deletions ast/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ const (
JoinTypeJoinHint JoinHintKey = "JOIN_TYPE"
)

// JoinMethod is used for prefix of JOIN, not for hint.
type JoinMethod string

const (
HashJoinMethod JoinMethod = "HASH"
ApplyJoinMethod JoinMethod = "APPLY"
LoopJoinMethod JoinMethod = "LOOP" // Undocumented, but the Spanner accept this value at least.
HashJoinMethod JoinMethod = "HASH"
LookupJoinMethod JoinMethod = "LOOKUP" // Undocumented, but the GoogleSQL can parse this value.
)

type PositionKeyword string
Expand Down
8 changes: 2 additions & 6 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,13 +737,9 @@ func (p *Parser) parseTableExpr(toplevel bool) ast.TableExpr {
p.nextToken()
method = ast.HashJoinMethod
needJoin = true
case p.Token.IsKeywordLike("APPLY"):
case p.Token.IsKeywordLike("LOOKUP"):
p.nextToken()
method = ast.ApplyJoinMethod
needJoin = true
case p.Token.IsKeywordLike("LOOP"):
p.nextToken()
method = ast.LoopJoinMethod
method = ast.LookupJoinMethod
needJoin = true
}
}
Expand Down
8 changes: 1 addition & 7 deletions testdata/input/query/select_singer_with_hash_join.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,4 @@ FROM
Singers A
HASH JOIN
Singers B
ON A.SingerID = B.SingerID
APPLY JOIN
Singer C
ON B.SingerID = C.SingerID
LOOP JOIN
Singer D
ON C.SingerID = D.SingerID
ON A.SingerID = B.SingerID
183 changes: 32 additions & 151 deletions testdata/result/query/select_singer_with_hash_join.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ FROM
HASH JOIN
Singers B
ON A.SingerID = B.SingerID
APPLY JOIN
Singer C
ON B.SingerID = C.SingerID
LOOP JOIN
Singer D
ON C.SingerID = D.SingerID

--- AST
&ast.QueryStatement{
Hint: (*ast.Hint)(nil),
Expand All @@ -30,182 +23,70 @@ FROM
From: 11,
Source: &ast.Join{
Op: "INNER JOIN",
Method: "LOOP",
Method: "HASH",
Hint: (*ast.Hint)(nil),
Left: &ast.Join{
Op: "INNER JOIN",
Method: "APPLY",
Hint: (*ast.Hint)(nil),
Left: &ast.Join{
Op: "INNER JOIN",
Method: "HASH",
Hint: (*ast.Hint)(nil),
Left: &ast.TableName{
Table: &ast.Ident{
NamePos: 18,
NameEnd: 25,
Name: "Singers",
},
Hint: (*ast.Hint)(nil),
As: &ast.AsAlias{
As: -1,
Alias: &ast.Ident{
NamePos: 26,
NameEnd: 27,
Name: "A",
},
},
Sample: (*ast.TableSample)(nil),
},
Right: &ast.TableName{
Table: &ast.Ident{
NamePos: 42,
NameEnd: 49,
Name: "Singers",
},
Hint: (*ast.Hint)(nil),
As: &ast.AsAlias{
As: -1,
Alias: &ast.Ident{
NamePos: 50,
NameEnd: 51,
Name: "B",
},
},
Sample: (*ast.TableSample)(nil),
},
Cond: &ast.On{
On: 54,
Expr: &ast.BinaryExpr{
Op: "=",
Left: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 57,
NameEnd: 58,
Name: "A",
},
&ast.Ident{
NamePos: 59,
NameEnd: 67,
Name: "SingerID",
},
},
},
Right: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 70,
NameEnd: 71,
Name: "B",
},
&ast.Ident{
NamePos: 72,
NameEnd: 80,
Name: "SingerID",
},
},
},
},
},
},
Right: &ast.TableName{
Table: &ast.Ident{
NamePos: 96,
NameEnd: 102,
Name: "Singer",
},
Hint: (*ast.Hint)(nil),
As: &ast.AsAlias{
As: -1,
Alias: &ast.Ident{
NamePos: 103,
NameEnd: 104,
Name: "C",
},
},
Sample: (*ast.TableSample)(nil),
Left: &ast.TableName{
Table: &ast.Ident{
NamePos: 18,
NameEnd: 25,
Name: "Singers",
},
Cond: &ast.On{
On: 107,
Expr: &ast.BinaryExpr{
Op: "=",
Left: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 110,
NameEnd: 111,
Name: "B",
},
&ast.Ident{
NamePos: 112,
NameEnd: 120,
Name: "SingerID",
},
},
},
Right: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 123,
NameEnd: 124,
Name: "C",
},
&ast.Ident{
NamePos: 125,
NameEnd: 133,
Name: "SingerID",
},
},
},
Hint: (*ast.Hint)(nil),
As: &ast.AsAlias{
As: -1,
Alias: &ast.Ident{
NamePos: 26,
NameEnd: 27,
Name: "A",
},
},
Sample: (*ast.TableSample)(nil),
},
Right: &ast.TableName{
Table: &ast.Ident{
NamePos: 148,
NameEnd: 154,
Name: "Singer",
NamePos: 42,
NameEnd: 49,
Name: "Singers",
},
Hint: (*ast.Hint)(nil),
As: &ast.AsAlias{
As: -1,
Alias: &ast.Ident{
NamePos: 155,
NameEnd: 156,
Name: "D",
NamePos: 50,
NameEnd: 51,
Name: "B",
},
},
Sample: (*ast.TableSample)(nil),
},
Cond: &ast.On{
On: 159,
On: 54,
Expr: &ast.BinaryExpr{
Op: "=",
Left: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 162,
NameEnd: 163,
Name: "C",
NamePos: 57,
NameEnd: 58,
Name: "A",
},
&ast.Ident{
NamePos: 164,
NameEnd: 172,
NamePos: 59,
NameEnd: 67,
Name: "SingerID",
},
},
},
Right: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 175,
NameEnd: 176,
Name: "D",
NamePos: 70,
NameEnd: 71,
Name: "B",
},
&ast.Ident{
NamePos: 177,
NameEnd: 185,
NamePos: 72,
NameEnd: 80,
Name: "SingerID",
},
},
Expand All @@ -223,4 +104,4 @@ FROM
}

--- SQL
SELECT * FROM Singers A INNER JOIN Singers B ON A.SingerID = B.SingerID INNER JOIN Singer C ON B.SingerID = C.SingerID INNER JOIN Singer D ON C.SingerID = D.SingerID
SELECT * FROM Singers A INNER JOIN Singers B ON A.SingerID = B.SingerID
Loading

0 comments on commit bb8d101

Please sign in to comment.