Skip to content

Commit

Permalink
Merge pull request #250 from dolthub/james/lateral
Browse files Browse the repository at this point in the history
support `LATERAL` syntax
  • Loading branch information
jycor authored Jul 18, 2023
2 parents 865ffe8 + 09e6e6f commit 42bab25
Show file tree
Hide file tree
Showing 4 changed files with 8,464 additions and 8,266 deletions.
8 changes: 8 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -4050,6 +4050,7 @@ type AliasedTableExpr struct {
As TableIdent
Hints *IndexHints
AsOf *AsOf
Lateral bool
}

type AsOf struct {
Expand All @@ -4069,18 +4070,24 @@ func (node *AsOf) walkSubtree(visit Visit) error {

// Format formats the node.
func (node *AliasedTableExpr) Format(buf *TrackedBuffer) {
if node.Lateral {
buf.Myprintf("%s ", keywordStrings[LATERAL])
}

switch node.Expr.(type) {
case *ValuesStatement:
buf.Myprintf("(%v)", node.Expr)
default:
buf.Myprintf("%v%v", node.Expr, node.Partitions)
}

if node.AsOf != nil {
buf.Myprintf(" %v", node.AsOf)
}
if !node.As.IsEmpty() {
buf.Myprintf(" as %v", node.As)
}

switch node := node.Expr.(type) {
case *ValuesStatement:
if len(node.Columns) > 0 {
Expand All @@ -4091,6 +4098,7 @@ func (node *AliasedTableExpr) Format(buf *TrackedBuffer) {
buf.Myprintf(" %v", node.Columns)
}
}

if node.Hints != nil {
// Hint node provides the space padding.
buf.Myprintf("%v", node.Hints)
Expand Down
42 changes: 36 additions & 6 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,33 @@ var (
input: "select a from (select 1 as a from tbl1 union select 2 from tbl2) as t",
}, {
input: "select a from (select 1 as a from tbl1 union select 2 from tbl2) as t (a, b)",
}, {
},
{
input: "select a from (values row(1, 2), row('a', 'b')) as t (a, b)",
}, {
},
{
input: "select a from (values row(1, 2), row('a', 'b')) as t1 join (values row(3, 4), row('c', 'd')) as t2",
}, {
},
{
input: "select a from (values row(1, 2), row('a', 'b')) as t1 (w, x) join (values row(3, 4), row('c', 'd')) as t2 (y, z)",
}, {
},
{
input: "select a from (values row(1, 2), row('a', 'b')) as t1 (w, x) join lateral (values row(3, 4), row('c', 'd')) as t2 (y, z)",
},
{
input: "select a from t1, lateral (select b from t2) as sq",
},
{
input: "select a from t1 join lateral (select b from t2) as sq",
},
{
input: "select a from t1 natural join lateral (select b from t2) as sq",
},
{
input: "select a from t1 join lateral (select b from t2) sq",
output: "select a from t1 join lateral (select b from t2) as sq",
},
{
// TODO: These forms are not yet supported due to a grammar conflict
// input: "values row(1, 2), row('a', 'b')",
// }, {
Expand Down Expand Up @@ -1083,10 +1103,12 @@ var (
}, {
input: "delete a, b from a, b where a.id = b.id and b.name = 'test'",
output: "delete a, b from a, b where a.id = b.id and b.`name` = 'test'",
}, {
},
{
input: "delete from a1, a2 using t1 as a1 inner join t2 as a2 where a1.id=a2.id",
output: "delete a1, a2 from t1 as a1 join t2 as a2 where a1.id = a2.id",
}, {
},
{
input: "savepoint abc",
}, {
input: "savepoint `ab_cd`",
Expand Down Expand Up @@ -4074,6 +4096,14 @@ func TestInvalid(t *testing.T) {
"name 'name'",
err: "multiple definitions of attribute name",
},
{
input: "select * from t join lateral t2",
err: "syntax error",
},
{
input: "select * from t join lateral (select * from t2)",
err: "Every derived table must have its own alias",
},
}

for _, tcase := range invalidSQL {
Expand Down
Loading

0 comments on commit 42bab25

Please sign in to comment.