Skip to content

Commit ad7d26c

Browse files
committed
Remove partial DDL parsing; support options on DROP INDEX statements
BREAKING CHANGE: Partial matches are no longer supported, making ParseStrictDDL redundant, as it would be functionally equivalent to the new Parse; therefore, the former was also removed.
1 parent a734d12 commit ad7d26c

File tree

5 files changed

+3588
-3406
lines changed

5 files changed

+3588
-3406
lines changed

ast.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"encoding/json"
2323
"fmt"
2424
"io"
25-
"log"
2625
"strings"
2726

2827
"github.com/yext/sqlparser/dependency/querypb"
@@ -42,25 +41,8 @@ import (
4241
// This will help avoid name collisions.
4342

4443
// Parse parses the SQL in full and returns a Statement, which
45-
// is the AST representation of the query. If a DDL statement
46-
// is partially parsed but still contains a syntax error, the
47-
// error is ignored and the DDL is returned anyway.
44+
// is the AST representation of the query.
4845
func Parse(sql string) (Statement, error) {
49-
tokenizer := NewStringTokenizer(sql)
50-
if yyParse(tokenizer) != 0 {
51-
if tokenizer.partialDDL != nil {
52-
log.Printf("ignoring error parsing DDL '%s': %v", sql, tokenizer.LastError)
53-
tokenizer.ParseTree = tokenizer.partialDDL
54-
return tokenizer.ParseTree, nil
55-
}
56-
return nil, tokenizer.LastError
57-
}
58-
return tokenizer.ParseTree, nil
59-
}
60-
61-
// ParseStrictDDL is the same as Parse except it errors on
62-
// partially parsed DDL statements.
63-
func ParseStrictDDL(sql string) (Statement, error) {
6446
tokenizer := NewStringTokenizer(sql)
6547
if yyParse(tokenizer) != 0 {
6648
return nil, tokenizer.LastError
@@ -85,10 +67,6 @@ func ParseNext(tokenizer *Tokenizer) (Statement, error) {
8567
tokenizer.reset()
8668
tokenizer.multi = true
8769
if yyParse(tokenizer) != 0 {
88-
if tokenizer.partialDDL != nil {
89-
tokenizer.ParseTree = tokenizer.partialDDL
90-
return tokenizer.ParseTree, nil
91-
}
9270
return nil, tokenizer.LastError
9371
}
9472
return tokenizer.ParseTree, nil

parse_next_test.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ func TestParseNextErrors(t *testing.T) {
9898
// TestParseNextEdgeCases tests various ParseNext edge cases.
9999
func TestParseNextEdgeCases(t *testing.T) {
100100
tests := []struct {
101-
name string
102-
input string
103-
want []string
101+
name string
102+
input string
103+
want []string
104+
firstErr string
104105
}{{
105106
name: "Trailing ;",
106107
input: "select 1 from a; update a set b = 2;",
@@ -126,19 +127,29 @@ func TestParseNextEdgeCases(t *testing.T) {
126127
input: "set character set ';'; select 1 from a",
127128
want: []string{"set charset ';'", "select 1 from a"},
128129
}, {
129-
name: "Partial DDL",
130-
input: "create table a; select 1 from a",
131-
want: []string{"create table a", "select 1 from a"},
130+
name: "Invalid DDL",
131+
input: "create table a; select 1 from a",
132+
firstErr: "syntax error at position 15",
133+
want: []string{"select 1 from a"},
132134
}, {
133-
name: "Partial DDL",
134-
input: "create table a ignore me this is garbage; select 1 from a",
135-
want: []string{"create table a", "select 1 from a"},
135+
name: "Invalid DDL with trailing garbage",
136+
input: "create table a ignore me this is garbage; select 1 from a",
137+
firstErr: "syntax error at position 22 near 'ignore'",
138+
want: []string{"select 1 from a"},
136139
}}
137140

138141
for _, test := range tests {
139142
tokens := NewStringTokenizer(test.input)
143+
i := 0
140144

141-
for i, want := range test.want {
145+
if test.firstErr != "" {
146+
if _, err := ParseNext(tokens); err == nil || err.Error() != test.firstErr {
147+
t.Fatalf("[%d] ParseNext(%q) err = %q, want %q", i, test.input, err, test.firstErr)
148+
}
149+
i++
150+
}
151+
152+
for _, want := range test.want {
142153
tree, err := ParseNext(tokens)
143154
if err != nil {
144155
t.Fatalf("[%d] ParseNext(%q) err = %q, want nil", i, test.input, err)
@@ -148,6 +159,7 @@ func TestParseNextEdgeCases(t *testing.T) {
148159
if got := String(tree); got != want {
149160
t.Fatalf("[%d] ParseNext(%q) = %q, want %q", i, test.input, got, want)
150161
}
162+
i++
151163
}
152164

153165
// Read once more and it should be EOF.

0 commit comments

Comments
 (0)