Skip to content

Commit

Permalink
buildinFunc: add a strcmp function
Browse files Browse the repository at this point in the history
Add a function needed in #310 .
  • Loading branch information
zxylvlp committed Feb 29, 2016
1 parent 73d6ce5 commit 431879e
Show file tree
Hide file tree
Showing 8 changed files with 5,721 additions and 5,596 deletions.
1 change: 1 addition & 0 deletions expression/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ var Funcs = map[string]Func{
"repeat": {builtinRepeat, 2, 2, true, false},
"replace": {builtinReplace, 3, 3, true, false},
"upper": {builtinUpper, 1, 1, true, false},
"strcmp": {builtinStrcmp, 2, 2, true, false},

// information functions
"current_user": {builtinCurrentUser, 0, 0, false, false},
Expand Down
17 changes: 17 additions & 0 deletions expression/builtin/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,23 @@ func builtinUpper(args []interface{}, ctx map[interface{}]interface{}) (interfac
}
}

// See: https://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html
func builtinStrcmp(args []interface{}, ctx map[interface{}]interface{}) (interface{}, error) {
if args[0] == nil || args[1] == nil {
return nil, nil
}
left, err := types.ToString(args[0])
if err != nil {
return nil, errors.Trace(err)
}
right, err := types.ToString(args[1])
if err != nil {
return nil, errors.Trace(err)
}
res := types.CompareString(left, right)
return res, nil
}

// See: https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace
func builtinReplace(args []interface{}, ctx map[interface{}]interface{}) (interface{}, error) {
for _, arg := range args {
Expand Down
27 changes: 27 additions & 0 deletions expression/builtin/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,33 @@ func (s *testBuiltinSuite) TestLowerAndUpper(c *C) {
}
}

func (s *testBuiltinSuite) TestStrcmp(c *C) {
tbl := []struct {
Input []interface{}
Expect interface{}
}{
{[]interface{}{"1", "2"}, -1},
{[]interface{}{"2", "1"}, 1},
{[]interface{}{"123", "2"}, -1},
{[]interface{}{"1", "213"}, -1},
{[]interface{}{"123", "123"}, 0},
{[]interface{}{"", "123"}, -1},
{[]interface{}{"123", ""}, 1},
{[]interface{}{"", ""}, 0},
{[]interface{}{nil, "123"}, nil},
{[]interface{}{"123", nil}, nil},
{[]interface{}{nil, nil}, nil},
{[]interface{}{"", nil}, nil},
{[]interface{}{nil, ""}, nil},
}

for _, t := range tbl {
v, err := builtinStrcmp(t.Input, nil)
c.Assert(err, IsNil)
c.Assert(v, Equals, t.Expect)
}
}

func (s *testBuiltinSuite) TestReplace(c *C) {
tbl := []struct {
Input []interface{}
Expand Down
2 changes: 2 additions & 0 deletions optimizer/typeinferer.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ func (v *typeInferrer) handleFuncCallExpr(x *ast.FuncCallExpr) {
"concat", "concat_ws", "left", "lower", "repeat", "replace", "upper":
tp = types.NewFieldType(mysql.TypeVarString)
chs = v.defaultCharset
case "strcmp":
tp = types.NewFieldType(mysql.TypeLonglong)
case "connection_id":
tp = types.NewFieldType(mysql.TypeLonglong)
tp.Flag |= mysql.UnsignedFlag
Expand Down
Loading

0 comments on commit 431879e

Please sign in to comment.