Skip to content
This repository was archived by the owner on Feb 21, 2024. It is now read-only.

Commit f490589

Browse files
committed
unbreak nested joins
It turns out that the problem with nested joins was that we were trying to cleverly invert them, but that seems to be incorrect and resulted in incorrect nesting. The test case for this is SELECT * FROM X INNER JOIN Y ON true INNER JOIN Z ON false this is now parsed as (X inner join Y on true) inner join z on false Which, as it turns out, is the structure that stringizes back to the original statement. We were previously parsing it as X inner join (y inner join z on false) on true which stringizes out to a different form, and is also, I think, just straightforwardly not what we want. So basically, we had special case code to recognize that we were doing a join on top of another join, and invert them in some way, and I have no idea why because that seems not to be correct, or at least, it produces nonsensical stringizing that we can't then parse.
1 parent 3f7ae75 commit f490589

2 files changed

Lines changed: 21 additions & 38 deletions

File tree

sql3/parser/parser.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,22 +2419,7 @@ func (p *Parser) parseSource() (source Source, err error) {
24192419
return source, err
24202420
}
24212421

2422-
// Rewrite last source to nest next join on right side.
2423-
if lhs, ok := source.(*JoinClause); ok {
2424-
source = &JoinClause{
2425-
X: lhs.X,
2426-
Operator: lhs.Operator,
2427-
Y: &JoinClause{
2428-
X: lhs.Y,
2429-
Operator: operator,
2430-
Y: y,
2431-
Constraint: constraint,
2432-
},
2433-
Constraint: lhs.Constraint,
2434-
}
2435-
} else {
2436-
source = &JoinClause{X: source, Operator: operator, Y: y, Constraint: constraint}
2437-
}
2422+
source = &JoinClause{X: source, Operator: operator, Y: y, Constraint: constraint}
24382423
}
24392424
}
24402425

sql3/parser/parser_test.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,38 +2055,36 @@ func TestParser_ParseStatement(t *testing.T) {
20552055
},
20562056
},
20572057
})
2058-
/*
2059-
// This one doesn't work right now because our stringify of this statement is wrong.
2060-
AssertParseStatement(t, `SELECT * FROM X INNER JOIN Y ON true INNER JOIN Z ON false`, &parser.SelectStatement{
2061-
Select: pos(0),
2062-
Columns: []*parser.ResultColumn{
2063-
{Star: pos(7)},
2064-
},
2065-
From: pos(9),
2066-
Source: &parser.JoinClause{
2058+
AssertParseStatement(t, `SELECT * FROM X INNER JOIN Y ON true INNER JOIN Z ON false`, &parser.SelectStatement{
2059+
Select: pos(0),
2060+
Columns: []*parser.ResultColumn{
2061+
{Star: pos(7)},
2062+
},
2063+
From: pos(9),
2064+
Source: &parser.JoinClause{
2065+
X: &parser.JoinClause{
20672066
X: &parser.QualifiedTableName{
20682067
Name: &parser.Ident{NamePos: pos(14), Name: "X"},
20692068
},
20702069
Operator: &parser.JoinOperator{Inner: pos(16), Join: pos(22)},
2071-
Y: &parser.JoinClause{
2072-
X: &parser.QualifiedTableName{
2073-
Name: &parser.Ident{NamePos: pos(27), Name: "Y"},
2074-
},
2075-
Operator: &parser.JoinOperator{Inner: pos(37), Join: pos(43)},
2076-
Y: &parser.QualifiedTableName{
2077-
Name: &parser.Ident{NamePos: pos(48), Name: "Z"},
2078-
},
2079-
Constraint: &parser.OnConstraint{
2080-
On: pos(50),
2081-
X: &parser.BoolLit{ValuePos: pos(53), Value: false},
2082-
},
2070+
Y: &parser.QualifiedTableName{
2071+
Name: &parser.Ident{NamePos: pos(27), Name: "Y"},
20832072
},
20842073
Constraint: &parser.OnConstraint{
20852074
On: pos(29),
20862075
X: &parser.BoolLit{ValuePos: pos(32), Value: true},
20872076
},
20882077
},
2089-
})*/
2078+
Operator: &parser.JoinOperator{Inner: pos(37), Join: pos(43)},
2079+
Y: &parser.QualifiedTableName{
2080+
Name: &parser.Ident{NamePos: pos(48), Name: "Z"},
2081+
},
2082+
Constraint: &parser.OnConstraint{
2083+
On: pos(50),
2084+
X: &parser.BoolLit{ValuePos: pos(53), Value: false},
2085+
},
2086+
},
2087+
})
20902088
AssertParseStatement(t, `SELECT * FROM foo LEFT OUTER JOIN bar`, &parser.SelectStatement{
20912089
Select: pos(0),
20922090
Columns: []*parser.ResultColumn{

0 commit comments

Comments
 (0)