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

support LATERAL syntax #250

Merged
merged 9 commits into from
Jul 18, 2023
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
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",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, didn't know about this one

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

recursive CTE example would be a nice to have

},
{
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