Skip to content

Commit

Permalink
parser: support sql_mode "PIPES_AS_CONCAT" (#5012)
Browse files Browse the repository at this point in the history
  • Loading branch information
spongedu authored and coocood committed Nov 9, 2017
1 parent 502d292 commit e188235
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions mysql/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,11 @@ func (m SQLMode) HasStrictMode() bool {
return m&ModeStrictTransTables == ModeStrictTransTables || m&ModeStrictAllTables == ModeStrictAllTables
}

// HasPipesAsConcatMode detects if 'PIPES_AS_CONCAT' mode is set in SQLMode
func (m SQLMode) HasPipesAsConcatMode() bool {
return m&ModePipesAsConcat == ModePipesAsConcat
}

// HasNoUnsignedSubtractionMode detects if 'NO_UNSIGNED_SUBTRACTION' mode is set in SQLMode
func (m SQLMode) HasNoUnsignedSubtractionMode() bool {
return m&ModeNoUnsignedSubtraction == ModeNoUnsignedSubtraction
Expand Down
7 changes: 7 additions & 0 deletions mysql/const_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ func (s *testMySQLConstSuite) TestRealAsFloatMode(c *C) {
c.Assert(row[1], Equals, "float")
}

func (s *testMySQLConstSuite) TestPipesAsConcatMode(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("SET sql_mode='PIPES_AS_CONCAT';")
r := tk.MustQuery(`SELECT 'hello' || 'world';`)
r.Check(testkit.Rows("helloworld"))
}

func (s *testMySQLConstSuite) TestNoUnsignedSubtractionMode(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set sql_mode='NO_UNSIGNED_SUBTRACTION'")
Expand Down
4 changes: 4 additions & 0 deletions parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ func (s *Scanner) Lex(v *yySymType) int {
tok = identifier
}

if tok == pipes && !(s.sqlMode.HasPipesAsConcatMode()) {
return pipesAsOr
}

if tok == not && s.sqlMode.HasHighNotPrecedenceMode() {
return not2
}
Expand Down
2 changes: 1 addition & 1 deletion parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func init() {
initTokenByte('?', paramMarker)
initTokenByte('=', eq)

initTokenString("||", oror)
initTokenString("||", pipes)
initTokenString("&&", andand)
initTokenString("&^", andnot)
initTokenString(":=", assignmentEq)
Expand Down
14 changes: 10 additions & 4 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import (
hintBegin "hintBegin is a virtual token for optimizer hint grammar"
hintEnd "hintEnd is a virtual token for optimizer hint grammar"
andand "&&"
oror "||"
pipes "||"

/* The following tokens belong to ReservedKeyword. */
add "ADD"
Expand Down Expand Up @@ -323,6 +323,7 @@ import (
only "ONLY"
password "PASSWORD"
partitions "PARTITIONS"
pipesAsOr
plugins "PLUGINS"
prepare "PREPARE"
privileges "PRIVILEGES"
Expand Down Expand Up @@ -783,7 +784,7 @@ import (
%precedence lowerThanOn
%precedence on using
%right assignmentEq
%left oror or
%left pipes or pipesAsOr
%left xor
%left andand and
%left between
Expand Down Expand Up @@ -1865,7 +1866,7 @@ Expression:
Value: $3,
}
}
| Expression logOr Expression %prec oror
| Expression logOr Expression %prec pipes
{
$$ = &ast.BinaryOperationExpr{Op: opcode.LogicOr, L: $1, R: $3}
}
Expand Down Expand Up @@ -1898,7 +1899,8 @@ Expression:


logOr:
"||" | "OR"
pipesAsOr
| "OR"

logAnd:
"&&" | "AND"
Expand Down Expand Up @@ -2769,6 +2771,10 @@ SimpleExpr:
{
$$ = &ast.UnaryOperationExpr{Op: opcode.Plus, V: $2}
}
| SimpleExpr pipes SimpleExpr
{
$$ = &ast.FuncCallExpr{FnName: model.NewCIStr(ast.Concat), Args: []ast.ExprNode{$1, $3}}
}
| not2 SimpleExpr %prec neg
{
$$ = &ast.UnaryOperationExpr{Op: opcode.Not, V: $2}
Expand Down

0 comments on commit e188235

Please sign in to comment.