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

empty statement error code change in sql parsing #7618

Merged
merged 1 commit into from
Mar 6, 2021
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
5 changes: 3 additions & 2 deletions go/mysql/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,8 @@ func (c *Conn) handleComPing() bool {
return true
}

var errEmptyStatement = NewSQLError(EREmptyQuery, SSClientError, "Query was empty")

func (c *Conn) handleComQuery(handler Handler, data []byte) (kontinue bool) {
c.startWriterBuffering()
defer func() {
Expand All @@ -1214,8 +1216,7 @@ func (c *Conn) handleComQuery(handler Handler, data []byte) (kontinue bool) {
}

if len(queries) == 0 {
err := NewSQLError(EREmptyQuery, SSClientError, "Query was empty")
return c.writeErrorPacketFromErrorAndLog(err)
return c.writeErrorPacketFromErrorAndLog(errEmptyStatement)
}

for index, sql := range queries {
Expand Down
1 change: 1 addition & 0 deletions go/mysql/sql_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ var stateToMysqlCode = map[vterrors.State]struct {
vterrors.DataOutOfRange: {num: ERDataOutOfRange, state: SSDataOutOfRange},
vterrors.DbCreateExists: {num: ERDbCreateExists, state: SSUnknownSQLState},
vterrors.DbDropExists: {num: ERDbDropExists, state: SSUnknownSQLState},
vterrors.EmptyQuery: {num: EREmptyQuery, state: SSClientError},
vterrors.InnodbReadOnly: {num: ERInnodbReadOnly, state: SSUnknownSQLState},
vterrors.LockOrActiveTransaction: {num: ERLockOrActiveTransaction, state: SSUnknownSQLState},
vterrors.NoDB: {num: ERNoDb, state: SSNoDB},
Expand Down
5 changes: 3 additions & 2 deletions go/test/endtoend/vtgate/unsharded/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,9 @@ func TestEmptyStatement(t *testing.T) {
require.Nil(t, err)
defer conn.Close()
defer exec(t, conn, `delete from t1`)
execAssertError(t, conn, " \t;", "Query was empty")
execMulti(t, conn, `insert into t1(c1, c2, c3, c4) values (300,100,300,'abc'); ;; insert into t1(c1, c2, c3, c4) values (301,101,301,'abcd');;`)
execAssertError(t, conn, " \t; \n;", "Query was empty")
execMulti(t, conn, `insert into t1(c1, c2, c3, c4) values (300,100,300,'abc'); ;; insert into t1(c1, c2, c3, c4) values (301,101,301,'abcd');;`)

assertMatches(t, conn, `select c1,c2,c3 from t1`, `[[INT64(300) INT64(100) INT64(300)] [INT64(301) INT64(101) INT64(301)]]`)
}

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 @@ -1957,7 +1957,7 @@ func TestInvalid(t *testing.T) {
err: "syntax error",
}, {
input: "/*!*/",
err: "empty statement",
err: "Query was empty",
}}

for _, tcase := range invalidSQL {
Expand Down Expand Up @@ -2263,7 +2263,7 @@ func TestConvert(t *testing.T) {
output: "syntax error at position 33",
}, {
input: "/* a comment */",
output: "empty statement",
output: "Query was empty",
}, {
input: "set transaction isolation level 12345",
output: "syntax error at position 38 near '12345'",
Expand Down
7 changes: 1 addition & 6 deletions go/vt/sqlparser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ limitations under the License.
package sqlparser

import (
"errors"
"fmt"
"io"
"runtime/debug"
"sync"

"vitess.io/vitess/go/vt/log"
Expand Down Expand Up @@ -97,7 +95,6 @@ func Parse(sql string) (Statement, error) {
return nil, vterrors.New(vtrpcpb.Code_INVALID_ARGUMENT, tokenizer.LastError.Error())
}
if tokenizer.ParseTree == nil {
log.Infof("Empty Statement: %s", debug.Stack())
return nil, ErrEmpty
}
return tokenizer.ParseTree, nil
Expand All @@ -111,8 +108,6 @@ func ParseStrictDDL(sql string) (Statement, error) {
return nil, tokenizer.LastError
}
if tokenizer.ParseTree == nil {
log.Infof("Empty Statement DDL: %s", debug.Stack())

return nil, ErrEmpty
}
return tokenizer.ParseTree, nil
Expand Down Expand Up @@ -164,7 +159,7 @@ func parseNext(tokenizer *Tokenizer, strict bool) (Statement, error) {
}

// ErrEmpty is a sentinel error returned when parsing empty statements.
var ErrEmpty = errors.New("empty statement")
var ErrEmpty = vterrors.NewErrorf(vtrpcpb.Code_INVALID_ARGUMENT, vterrors.EmptyQuery, "Query was empty")

// SplitStatement returns the first sql statement up to either a ; or EOF
// and the remainder from the given buffer
Expand Down
1 change: 1 addition & 0 deletions go/vt/vterrors/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
BadFieldError
CantUseOptionHere
DataOutOfRange
EmptyQuery
ForbidSchemaChange
NonUniqTable
SyntaxError
Expand Down
5 changes: 1 addition & 4 deletions go/vt/vttablet/tabletserver/query_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ func TestGetPlanPanicDuetoEmptyQuery(t *testing.T) {
ctx := context.Background()
logStats := tabletenv.NewLogStats(ctx, "GetPlanStats")
_, err := qe.GetPlan(ctx, logStats, "", false, false /* inReservedConn */)
want := "empty statement"
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("qe.GetPlan: %v, want %s", err, want)
}
require.EqualError(t, err, "Query was empty")
}

func addSchemaEngineQueries(db *fakesqldb.DB) {
Expand Down