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

lexer: support more syntaxes regarding 'SET Syntax' #7020

Merged
merged 7 commits into from
Jul 10, 2018
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
24 changes: 21 additions & 3 deletions parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,26 @@ func startWithAt(s *Scanner) (tok int, pos Pos, lit string) {
pos = s.r.pos()
s.r.inc()
ch1 := s.r.peek()
if isIdentFirstChar(ch1) {
s.r.incAsLongAs(isIdentChar)
if ch1 == '\'' || ch1 == '"' {
nTok, nPos, nLit := startString(s)
if nTok == stringLit {
tok = singleAtIdentifier
pos = nPos
lit = nLit
} else {
tok = int('@')
}
} else if ch1 == '`' {
nTok, nPos, nLit := scanQuotedIdent(s)
if nTok == quotedIdentifier {
tok = singleAtIdentifier
pos = nPos
lit = nLit
} else {
tok = int('@')
}
} else if isUserVarChar(ch1) {
s.r.incAsLongAs(isUserVarChar)
tok, lit = singleAtIdentifier, s.r.data(&pos)
} else if ch1 == '@' {
s.r.inc()
Expand All @@ -435,7 +453,7 @@ func startWithAt(s *Scanner) (tok int, pos Pos, lit string) {
s.r.incAsLongAs(isIdentChar)
tok, lit = doubleAtIdentifier, s.r.data(&pos)
} else {
tok = int('@')
tok, lit = singleAtIdentifier, s.r.data(&pos)
}
return
}
Expand Down
12 changes: 10 additions & 2 deletions parser/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type testCaseItem struct {
func (s *testLexerSuite) TestSingleCharOther(c *C) {
defer testleak.AfterTest(c)()
table := []testCaseItem{
{"@", int('@')},
{"AT", identifier},
{"?", paramMarker},
{"PLACEHOLDER", identifier},
Expand All @@ -69,8 +68,17 @@ func (s *testLexerSuite) TestSingleCharOther(c *C) {
func (s *testLexerSuite) TestAtLeadingIdentifier(c *C) {
defer testleak.AfterTest(c)()
table := []testCaseItem{
{"@", singleAtIdentifier},
{"@''", singleAtIdentifier},
{"@1", singleAtIdentifier},
{"@.1_", singleAtIdentifier},
{"@-1.", singleAtIdentifier},
{"@~", singleAtIdentifier},
{"@$", singleAtIdentifier},
{"@a_3cbbc", singleAtIdentifier},
{"@-3cbbc", int('@')},
{"@`a_3cbbc`", singleAtIdentifier},
{"@-3cbbc", singleAtIdentifier},
{"@!3cbbc", singleAtIdentifier},
{"@@global.test", doubleAtIdentifier},
{"@@session.test", doubleAtIdentifier},
{"@@local.test", doubleAtIdentifier},
Expand Down
4 changes: 2 additions & 2 deletions parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func isIdentExtend(ch rune) bool {
return ch >= 0x80 && ch <= '\uffff'
}

func isIdentFirstChar(ch rune) bool {
return isLetter(ch) || ch == '_'
func isUserVarChar(ch rune) bool {
return isLetter(ch) || isDigit(ch) || ch == '_' || ch == '$' || ch == '.' || isIdentExtend(ch)
}

type trieNode struct {
Expand Down
9 changes: 9 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,17 @@ func (s *testParserSuite) TestDBAStmt(c *C) {
{"load stats '/tmp/stats.json'", true},
// set
// user defined
{"SET @ = 1", true},
{"SET @' ' = 1", true},
{"SET @! = 1", false},
{"SET @1 = 1", true},
{"SET @a = 1", true},
{"SET @b := 1", true},
{"SET @.c = 1", true},
{"SET @_d = 1", true},
{"SET @_e._$. = 1", true},
{"SET @~f = 1", false},
{"SET @`g,` = 1", true},
// session system variables
{"SET SESSION autocommit = 1", true},
{"SET @@session.autocommit = 1", true},
Expand Down