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 the support of DELETE FROM expression #4

Merged
merged 2 commits into from
Sep 25, 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
31 changes: 31 additions & 0 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3250,3 +3250,34 @@ func (s *SampleRatioExpr) String(level int) string {
}
return builder.String()
}

type DeleteFromExpr struct {
DeletePos Pos
Table *TableIdentifier
OnCluster *OnClusterExpr
WhereExpr Expr
}

func (d *DeleteFromExpr) Pos() Pos {
return d.DeletePos
}

func (d *DeleteFromExpr) End() Pos {
return d.WhereExpr.End()
}

func (d *DeleteFromExpr) String(level int) string {
var builder strings.Builder
builder.WriteString("DELETE FROM ")
builder.WriteString(d.Table.String(level))
if d.OnCluster != nil {
builder.WriteString(NewLine(level))
builder.WriteString(d.OnCluster.String(level))
}
if d.WhereExpr != nil {
builder.WriteString(NewLine(level))
builder.WriteString("WHERE ")
builder.WriteString(d.WhereExpr.String(level))
}
return builder.String()
}
34 changes: 34 additions & 0 deletions parser/parser_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,8 @@ func (p *Parser) parseStatement(pos Pos) (Expr, error) {
expr, err = p.parseDDL(pos)
case p.matchKeyword(KeywordSelect), p.matchKeyword(KeywordWith):
expr, err = p.parseSelectQuery(pos)
case p.matchKeyword(KeywordDelete):
expr, err = p.parseDeleteFrom(pos)
case p.matchKeyword(KeywordUse):
expr, err = p.parseUseStatement(pos)
case p.matchKeyword(KeywordSet):
Expand Down Expand Up @@ -996,3 +998,35 @@ func (p *Parser) parseTruncateTable(pos Pos) (*TruncateTable, error) {

return truncateTable, nil
}

func (p *Parser) parseDeleteFrom(pos Pos) (*DeleteFromExpr, error) {
if err := p.consumeKeyword(KeywordDelete); err != nil {
return nil, err
}
if err := p.consumeKeyword(KeywordFrom); err != nil {
return nil, err
}
tableIdentifier, err := p.parseTableIdentifier(p.Pos())
if err != nil {
return nil, err
}
onCluster, err := p.tryParseOnCluster(p.Pos())
if err != nil {
return nil, err
}

if err := p.consumeKeyword(KeywordWhere); err != nil {
return nil, err
}
whereExpr, err := p.parseExpr(p.Pos())
if err != nil {
return nil, err
}

return &DeleteFromExpr{
DeletePos: pos,
Table: tableIdentifier,
OnCluster: onCluster,
WhereExpr: whereExpr,
}, nil
}
4 changes: 2 additions & 2 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestParser_Compatible(t *testing.T) {
}

func TestParser_ParseStatements(t *testing.T) {
for _, dir := range []string{"./testdata/ddl", "./testdata/query", "./testdata/basic"} {
for _, dir := range []string{"./testdata/dml", "./testdata/ddl", "./testdata/query", "./testdata/basic"} {
outputDir := dir + "/output"
entries, err := os.ReadDir(dir)
if err != nil {
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestParser_ParseStatements(t *testing.T) {
}

func TestParser_Format(t *testing.T) {
for _, dir := range []string{"./testdata/ddl", "./testdata/query", "./testdata/basic"} {
for _, dir := range []string{"./testdata/dml", "./testdata/ddl", "./testdata/query", "./testdata/basic"} {
outputDir := dir + "/format"

entries, err := os.ReadDir(dir)
Expand Down
8 changes: 3 additions & 5 deletions parser/set_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package parser

import (
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

func TestSet(t *testing.T) {
Expand All @@ -28,8 +29,5 @@ func TestSet(t *testing.T) {
t.Errorf("Set should not contain 4")
}

expected := []int{1, 2, 3}
if !reflect.DeepEqual(s.Members(), expected) {
t.Errorf("Set members should be %v, but got %v", expected, s.Members())
}
require.EqualValues(t, []int{1, 2, 3}, s.Members())
}
1 change: 1 addition & 0 deletions parser/testdata/dml/delete_from.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM hits WHERE Title LIKE '%hello%';
7 changes: 7 additions & 0 deletions parser/testdata/dml/format/delete_from.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Origin SQL:
DELETE FROM hits WHERE Title LIKE '%hello%';


-- Format SQL:
DELETE FROM hits
WHERE Title LIKE '%hello%';
30 changes: 30 additions & 0 deletions parser/testdata/dml/output/delete_from.sql.golden.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"DeletePos": 0,
"Table": {
"Database": null,
"Table": {
"Name": "hits",
"Unquoted": false,
"NamePos": 12,
"NameEnd": 16
}
},
"OnCluster": null,
"WhereExpr": {
"LeftExpr": {
"Name": "Title",
"Unquoted": false,
"NamePos": 23,
"NameEnd": 28
},
"Operation": "LIKE",
"RightExpr": {
"LiteralPos": 35,
"LiteralEnd": 42,
"Literal": "%hello%"
},
"HasNot": false
}
}
]
Loading