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

Corrects the comment handling in vitess #7581

Merged
merged 1 commit into from
Mar 2, 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
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),
}
}

Comment on lines -67 to -75
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

// 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