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

Add parsing for common table expressions using WITH clause #8918

Merged
merged 8 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
17 changes: 17 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type (
SetLimit(*Limit)
SetLock(lock Lock)
SetInto(into *SelectInto)
SetWith(with *With)
MakeDistinct()
GetColumnCount() int
SetComments(comments Comments)
Expand Down Expand Up @@ -123,6 +124,18 @@ type (
DefaultVal Expr
}

// With contains the lists of common table expression and specifies if it is recursive or not
With struct {
ctes []*CommonTableExpr
Recursive bool
}

// CommonTableExpr is the structure for supporting common table expressions
CommonTableExpr struct {
TableID TableIdent
Columns Columns
Subquery *Subquery
}
// ChangeColumn is used to change the column definition, can also rename the column in alter table command
ChangeColumn struct {
OldColumn *ColName
Expand Down Expand Up @@ -211,6 +224,7 @@ type (
Comments Comments
SelectExprs SelectExprs
Where *Where
With *With
GroupBy GroupBy
Having *Where
OrderBy OrderBy
Expand Down Expand Up @@ -242,6 +256,7 @@ type (
Right SelectStatement
Distinct bool
OrderBy OrderBy
With *With
Limit *Limit
Lock Lock
Into *SelectInto
Expand Down Expand Up @@ -291,6 +306,7 @@ type (
// Update represents an UPDATE statement.
// If you add fields here, consider adding them to calls to validateUnshardedRoute.
Update struct {
With *With
Comments Comments
Ignore Ignore
TableExprs TableExprs
Expand All @@ -303,6 +319,7 @@ type (
// Delete represents a DELETE statement.
// If you add fields here, consider adding them to calls to validateUnshardedRoute.
Delete struct {
With *With
Ignore Ignore
Comments Comments
Targets TableNames
Expand Down
42 changes: 42 additions & 0 deletions go/vt/sqlparser/ast_clone.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 56 additions & 2 deletions go/vt/sqlparser/ast_equals.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (

// Format formats the node.
func (node *Select) Format(buf *TrackedBuffer) {
if node.With != nil {
buf.astPrintf(node, "%v", node.With)
}
buf.astPrintf(node, "select %v", node.Comments)

if node.Distinct {
Expand Down Expand Up @@ -116,15 +119,40 @@ func (node *Insert) Format(buf *TrackedBuffer) {

}

// Format formats the node.
func (node *With) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "with ")

if node.Recursive {
buf.astPrintf(node, "recursive ")
}
ctesLength := len(node.ctes)
for i := 0; i < ctesLength-1; i++ {
buf.astPrintf(node, "%v, ", node.ctes[i])
}
buf.astPrintf(node, "%v", node.ctes[ctesLength-1])
}

// Format formats the node.
func (node *CommonTableExpr) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "%v%v as %v ", node.TableID, node.Columns, node.Subquery)
}

// Format formats the node.
func (node *Update) Format(buf *TrackedBuffer) {
if node.With != nil {
buf.astPrintf(node, "%v", node.With)
}
buf.astPrintf(node, "update %v%s%v set %v%v%v%v",
node.Comments, node.Ignore.ToString(), node.TableExprs,
node.Exprs, node.Where, node.OrderBy, node.Limit)
}

// Format formats the node.
func (node *Delete) Format(buf *TrackedBuffer) {
if node.With != nil {
buf.astPrintf(node, "%v", node.With)
}
buf.astPrintf(node, "delete %v", node.Comments)
if node.Ignore {
buf.WriteString("ignore ")
Expand Down
33 changes: 33 additions & 0 deletions go/vt/sqlparser/ast_format_fast.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading