Skip to content

Commit

Permalink
Merge pull request #7581 from GuptaManan100/comments-mysql
Browse files Browse the repository at this point in the history
Corrects the comment handling in vitess
  • Loading branch information
harshit-gangal committed Mar 2, 2021
2 parents a75c159 + b5bdfd2 commit 6493f55
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
2 changes: 1 addition & 1 deletion go/vt/sqlparser/parse_next_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestParseNextValid(t *testing.T) {
sql.WriteRune(';')
}

tokens := NewTokenizer(&sql)
tokens := NewStringTokenizer(sql.String())
for i, tcase := range validSQL {
input := tcase.input + ";"
want := tcase.output
Expand Down
5 changes: 4 additions & 1 deletion go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var (
input: "select 1 from t # aa\n",
output: "select 1 from t",
}, {
input: "select 1 --aa\nfrom t",
input: "select 1 -- aa\nfrom t",
output: "select 1 from t",
}, {
input: "select 1 #aa\nfrom t",
Expand Down Expand Up @@ -840,6 +840,9 @@ var (
}, {
input: "set character set 'utf8'",
output: "set charset 'utf8'",
}, {
input: "set s = 1--4",
output: "set s = 1 - -4",
}, {
input: "set character set \"utf8\"",
output: "set charset 'utf8'",
Expand Down
37 changes: 13 additions & 24 deletions go/vt/sqlparser/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,19 @@ package sqlparser
import (
"bytes"
"fmt"
"io"
"strings"

"vitess.io/vitess/go/bytes2"
"vitess.io/vitess/go/sqltypes"
)

const (
defaultBufSize = 4096
eofChar = 0x100
eofChar = 0x100
)

// Tokenizer is the struct used to generate SQL
// tokens for the parser.
type Tokenizer struct {
InStream io.Reader
AllowComments bool
SkipSpecialComments bool
SkipToEnd bool
Expand Down Expand Up @@ -64,15 +61,6 @@ func NewStringTokenizer(sql string) *Tokenizer {
}
}

// NewTokenizer creates a new Tokenizer reading a sql
// string from the io.Reader.
func NewTokenizer(r io.Reader) *Tokenizer {
return &Tokenizer{
InStream: r,
buf: make([]byte, defaultBufSize),
}
}

// keywords is a map of mysql keywords that fall into two categories:
// 1) keywords considered reserved by MySQL
// 2) keywords for us to handle specially in sql.y
Expand Down Expand Up @@ -691,8 +679,11 @@ func (tkn *Tokenizer) Scan() (int, []byte) {
case '-':
switch tkn.lastChar {
case '-':
tkn.next()
return tkn.scanCommentType1("--")
nextChar := tkn.peek(0)
if nextChar == ' ' || nextChar == '\n' || nextChar == '\t' || nextChar == '\r' || nextChar == eofChar {
tkn.next()
return tkn.scanCommentType1("--")
}
case '>':
tkn.next()
if tkn.lastChar == '>' {
Expand Down Expand Up @@ -1052,15 +1043,6 @@ func (tkn *Tokenizer) consumeNext(buffer *bytes2.Buffer) {
}

func (tkn *Tokenizer) next() {
if tkn.bufPos >= tkn.bufSize && tkn.InStream != nil {
// Try and refill the buffer
var err error
tkn.bufPos = 0
if tkn.bufSize, err = tkn.InStream.Read(tkn.buf); err != io.EOF && err != nil {
tkn.LastError = err
}
}

if tkn.bufPos >= tkn.bufSize {
if tkn.lastChar != eofChar {
tkn.Position++
Expand All @@ -1073,6 +1055,13 @@ func (tkn *Tokenizer) next() {
}
}

func (tkn *Tokenizer) peek(dist int) uint16 {
if tkn.bufPos+dist >= tkn.bufSize {
return eofChar
}
return uint16(tkn.buf[tkn.bufPos+dist])
}

// reset clears any internal state.
func (tkn *Tokenizer) reset() {
tkn.ParseTree = nil
Expand Down

0 comments on commit 6493f55

Please sign in to comment.