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

parser: support sql_mode 'IGNORE SPACE' #5106

Merged
merged 18 commits into from
Dec 7, 2017
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
14 changes: 8 additions & 6 deletions ast/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ func (ts *testAstFormatSuite) TestAstFormat(c *C) {
{` json_extract ( a,'$.b',"$.\"c d\"" ) `, "json_extract(`a`, \"$.b\", \"$.\\\"c d\\\"\")"},
{` length ( a )`, "length(`a`)"},
// Cast, Convert and Binary.
{` cast ( a as signed ) `, "CAST(`a` AS SIGNED)"},
{` cast ( a as unsigned integer) `, "CAST(`a` AS UNSIGNED)"},
{` cast ( a as char(3) binary) `, "CAST(`a` AS CHAR(3) BINARY)"},
{` cast ( a as decimal ) `, "CAST(`a` AS DECIMAL(11))"},
{` cast ( a as decimal (3) ) `, "CAST(`a` AS DECIMAL(3))"},
{` cast ( a as decimal (3,3) ) `, "CAST(`a` AS DECIMAL(3, 3))"},
// There should not be spaces between 'cast' and '(' unless 'IGNORE_SPACE' mode is set.
// see: https://dev.mysql.com/doc/refman/5.7/en/function-resolution.html
{` cast( a as signed ) `, "CAST(`a` AS SIGNED)"},
{` cast( a as unsigned integer) `, "CAST(`a` AS UNSIGNED)"},
{` cast( a as char(3) binary) `, "CAST(`a` AS CHAR(3) BINARY)"},
{` cast( a as decimal ) `, "CAST(`a` AS DECIMAL(11))"},
{` cast( a as decimal (3) ) `, "CAST(`a` AS DECIMAL(3))"},
{` cast( a as decimal (3,3) ) `, "CAST(`a` AS DECIMAL(3, 3))"},
{` convert (a, signed) `, "CONVERT(`a`, SIGNED)"},
{` binary "hello"`, `BINARY "hello"`},
}
Expand Down
5 changes: 5 additions & 0 deletions mysql/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,11 @@ func (m SQLMode) HasNoBackslashEscapesMode() bool {
return m&ModeNoBackslashEscapes == ModeNoBackslashEscapes
}

// HasIgnoreSpaceMode detects if 'IGNORE_SPACE' mode is set in SQLMode
func (m SQLMode) HasIgnoreSpaceMode() bool {
return m&ModeIgnoreSpace == ModeIgnoreSpace
}

// consts for sql modes.
const (
ModeNone SQLMode = 0
Expand Down
61 changes: 61 additions & 0 deletions mysql/const_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,67 @@ func (s *testMySQLConstSuite) TestHighNotPrecedenceMode(c *C) {
r.Check(testkit.Rows("1"))
}

func (s *testMySQLConstSuite) TestIgnoreSpaceMode(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set sql_mode=''")
tk.MustExec("CREATE TABLE COUNT (a bigint);")
tk.MustExec("DROP TABLE COUNT;")
tk.MustExec("CREATE TABLE `COUNT` (a bigint);")
tk.MustExec("DROP TABLE COUNT;")
_, err := tk.Exec("CREATE TABLE COUNT(a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE test.COUNT(a bigint);")
tk.MustExec("DROP TABLE COUNT;")

tk.MustExec("CREATE TABLE BIT_AND (a bigint);")
tk.MustExec("DROP TABLE BIT_AND;")
tk.MustExec("CREATE TABLE `BIT_AND` (a bigint);")
tk.MustExec("DROP TABLE BIT_AND;")
_, err = tk.Exec("CREATE TABLE BIT_AND(a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE test.BIT_AND(a bigint);")
tk.MustExec("DROP TABLE BIT_AND;")

tk.MustExec("CREATE TABLE NOW (a bigint);")
tk.MustExec("DROP TABLE NOW;")
tk.MustExec("CREATE TABLE `NOW` (a bigint);")
tk.MustExec("DROP TABLE NOW;")
_, err = tk.Exec("CREATE TABLE NOW(a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE test.NOW(a bigint);")
tk.MustExec("DROP TABLE NOW;")

tk.MustExec("set sql_mode='IGNORE_SPACE'")
_, err = tk.Exec("CREATE TABLE COUNT (a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE `COUNT` (a bigint);")
tk.MustExec("DROP TABLE COUNT;")
_, err = tk.Exec("CREATE TABLE COUNT(a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE test.COUNT(a bigint);")
tk.MustExec("DROP TABLE COUNT;")

_, err = tk.Exec("CREATE TABLE BIT_AND (a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE `BIT_AND` (a bigint);")
tk.MustExec("DROP TABLE BIT_AND;")
_, err = tk.Exec("CREATE TABLE BIT_AND(a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE test.BIT_AND(a bigint);")
tk.MustExec("DROP TABLE BIT_AND;")

_, err = tk.Exec("CREATE TABLE NOW (a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE `NOW` (a bigint);")
tk.MustExec("DROP TABLE NOW;")
_, err = tk.Exec("CREATE TABLE NOW(a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE test.NOW(a bigint);")
tk.MustExec("DROP TABLE NOW;")

}

func (s *testMySQLConstSuite) TestPadCharToFullLengthMode(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
53 changes: 52 additions & 1 deletion parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,42 @@ var tokenMap = map[string]int{
"ZEROFILL": zerofill,
}

// See https://dev.mysql.com/doc/refman/5.7/en/function-resolution.html for details
var btFuncTokenMap = map[string]int{
"ADDDATE": builtinAddDate,
"BIT_AND": builtinBitAnd,
"BIT_OR": builtinBitOr,
"BIT_XOR": builtinBitXor,
"CAST": builtinCast,
"COUNT": builtinCount,
"CURDATE": builtinCurDate,
"CURTIME": builtinCurTime,
"DATE_ADD": builtinDateAdd,
"DATE_SUB": builtinDateSub,
"EXTRACT": builtinExtract,
"GROUP_CONCAT": builtinGroupConcat,
"MAX": builtinMax,
"MID": builtinSubstring,
"MIN": builtinMin,
"NOW": builtinNow,
"POSITION": builtinPosition,
"SESSION_USER": builtinUser,
"STD": builtinStddevPop,
"STDDEV": builtinStddevPop,
"STDDEV_POP": builtinStddevPop,
"STDDEV_SAMP": builtinVarSamp,
"SUBDATE": builtinSubDate,
"SUBSTR": builtinSubstring,
"SUBSTRING": builtinSubstring,
"SUM": builtinSum,
"SYSDATE": builtinSysDate,
"SYSTEM_USER": builtinUser,
"TRIM": builtinTrim,
"VARIANCE": builtinVarPop,
"VAR_POP": builtinVarPop,
"VAR_SAMP": builtinVarSamp,
}

// aliases are strings directly map to another string and use the same token.
var aliases = map[string]string{
"SCHEMA": "DATABASE",
Expand Down Expand Up @@ -521,7 +557,22 @@ func (s *Scanner) isTokenIdentifier(lit string, offset int) int {
data[i] = lit[i]
}
}
tok := tokenMap[hack.String(data)]

checkBtFuncToken, tokenStr := false, hack.String(data)
if s.r.peek() == '(' {
checkBtFuncToken = true
} else if s.sqlMode.HasIgnoreSpaceMode() {
s.skipWhitespace()
if s.r.peek() == '(' {
checkBtFuncToken = true
}
}
if checkBtFuncToken {
if tok := btFuncTokenMap[tokenStr]; tok != 0 {
return tok
}
}
tok := tokenMap[tokenStr]
return tok
}

Expand Down
Loading