Skip to content
Closed
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
4 changes: 2 additions & 2 deletions go/vt/sqlparser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Preview(sql string) StatementType {
// Comparison is done in order of priority.
loweredFirstWord := strings.ToLower(firstWord)
switch loweredFirstWord {
case "select":
case "select", "explain":
return StmtSelect
case "stream":
return StmtStream
Expand Down Expand Up @@ -108,7 +108,7 @@ func Preview(sql string) StatementType {
return StmtShow
case "use":
return StmtUse
case "analyze", "describe", "desc", "explain", "repair", "optimize":
case "analyze", "describe", "desc", "repair", "optimize":
return StmtOther
}
return StmtUnknown
Expand Down
9 changes: 5 additions & 4 deletions go/vt/sqlparser/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package sqlparser

import (
"github.com/stretchr/testify/assert"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -65,7 +66,7 @@ func TestPreview(t *testing.T) {
{"analyze", StmtOther},
{"describe", StmtOther},
{"desc", StmtOther},
{"explain", StmtOther},
{"explain select", StmtSelect},
{"repair", StmtOther},
{"optimize", StmtOther},
{"truncate", StmtDDL},
Expand All @@ -83,9 +84,9 @@ func TestPreview(t *testing.T) {
{"-- leading single line comment no end select ...", StmtUnknown},
}
for _, tcase := range testcases {
if got := Preview(tcase.sql); got != tcase.want {
t.Errorf("Preview(%s): %v, want %v", tcase.sql, got, tcase.want)
}
t.Run(tcase.sql, func(t *testing.T) {
assert.Equal(t, tcase.want, Preview(tcase.sql))
})
}
}

Expand Down
32 changes: 32 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3827,3 +3827,35 @@ func compliantName(in string) string {
}
return buf.String()
}

var _ Statement = (*Explain)(nil)

type Explain struct {
// TODO: this inner structure could be something else, but let's start here
InnerStatement Statement
}

func NewExplain(input interface{}) *Explain {
switch v := input.(type) {
case Statement:
return &Explain{v}
default:
log.Warning("got nil for the explain")
return &Explain{}
}
}


func (Explain) iStatement() {}

func (Explain) iInsertRows() {}

func (e *Explain) Format(buf *TrackedBuffer) {
buf.Myprintf("explain %v", e.InnerStatement)
}
func (e *Explain) walkSubtree(visit Visit) error {
return Walk(
visit,
e.InnerStatement,
)
}
35 changes: 21 additions & 14 deletions go/vt/sqlparser/normalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,29 @@ func TestNormalize(t *testing.T) {
outbv: map[string]*querypb.BindVariable{
"bv1": sqltypes.TestBindVariable([]interface{}{1, []byte("2")}),
},
}, {
// EXPLAIN is not stripped out
in: "explain select * from t",
outstmt: "explain select * from t",
outbv: map[string]*querypb.BindVariable{},
}}
for _, tc := range testcases {
stmt, err := Parse(tc.in)
if err != nil {
t.Error(err)
continue
}
bv := make(map[string]*querypb.BindVariable)
Normalize(stmt, bv, prefix)
outstmt := String(stmt)
if outstmt != tc.outstmt {
t.Errorf("Query:\n%s:\n%s, want\n%s", tc.in, outstmt, tc.outstmt)
}
if !reflect.DeepEqual(tc.outbv, bv) {
t.Errorf("Query:\n%s:\n%v, want\n%v", tc.in, bv, tc.outbv)
}
t.Run(tc.in, func(t *testing.T) {
stmt, err := Parse(tc.in)
if err != nil {
t.Error(err)
return
}
bv := make(map[string]*querypb.BindVariable)
Normalize(stmt, bv, prefix)
outstmt := String(stmt)
if outstmt != tc.outstmt {
t.Errorf("Query:\n%s:\n%s, want\n%s", tc.in, outstmt, tc.outstmt)
}
if !reflect.DeepEqual(tc.outbv, bv) {
t.Errorf("Query:\n%s:\n%v, want\n%v", tc.in, bv, tc.outbv)
}
})
}
}

Expand Down
28 changes: 15 additions & 13 deletions go/vt/sqlparser/parse_next_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,23 @@ func TestParseNextValid(t *testing.T) {

tokens := NewTokenizer(&sql)
for i, tcase := range validSQL {
input := tcase.input + ";"
want := tcase.output
if want == "" {
want = tcase.input
}
t.Run(tcase.input, func(t *testing.T) {
input := tcase.input + ";"
want := tcase.output
if want == "" {
want = tcase.input
}

tree, err := ParseNext(tokens)
if err != nil {
t.Fatalf("[%d] ParseNext(%q) err: %q, want nil", i, input, err)
continue
}
tree, err := ParseNext(tokens)
if err != nil {
t.Fatalf("[%d] ParseNext(%q) err: %q, want nil", i, input, err)
return
}

if got := String(tree); got != want {
t.Fatalf("[%d] ParseNext(%q) = %q, want %q", i, input, got, want)
}
if got := String(tree); got != want {
t.Fatalf("[%d] ParseNext(%q) = %q, want %q", i, input, got, want)
}
})
}

// Read once more and it should be EOF.
Expand Down
4 changes: 2 additions & 2 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1325,8 +1325,8 @@ var (
input: "desc foobar",
output: "otherread",
}, {
input: "explain foobar",
output: "otherread",
input: "explain select * from t",
output: "explain select * from t",
}, {
input: "truncate table foo",
output: "truncate table foo",
Expand Down
Loading