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

builtin: add elt built-in function #2870

Merged
merged 11 commits into from
Mar 20, 2017
23 changes: 22 additions & 1 deletion expression/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,28 @@ type builtinEltSig struct {

// See https://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_elt
func (b *builtinEltSig) eval(row []types.Datum) (d types.Datum, err error) {
return d, errFunctionNotExists.GenByArgs("elt")
args, err := b.evalArgs(row)
if err != nil {
return d, errors.Trace(err)
}

index, err := args[0].ToInt64(b.ctx.GetSessionVars().StmtCtx)
if err != nil {
return d, errors.Trace(err)
}

argsLength := int64(len(args))
if index < 1 || index > (argsLength-1) {
return d, nil
}

result, err := args[index].ToString()
if err != nil {
return d, errors.Trace(err)
}
d.SetString(result)

return d, nil
}

type exportSetFunctionClass struct {
Expand Down
23 changes: 23 additions & 0 deletions expression/builtin_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1054,3 +1054,26 @@ func (s *testEvaluatorSuite) TestOct(c *C) {
c.Assert(err, IsNil)
c.Assert(r.IsNull(), IsTrue)
}

func (s *testEvaluatorSuite) TestElt(c *C) {
defer testleak.AfterTest(c)()

tbl := []struct {
argLst []interface{}
ret interface{}
}{
{[]interface{}{1, "Hej", "ej", "Heja", "hej", "foo"}, "Hej"},
{[]interface{}{9, "Hej", "ej", "Heja", "hej", "foo"}, nil},
{[]interface{}{-1, "Hej", "ej", "Heja", "ej", "hej", "foo"}, nil},
{[]interface{}{0, 2, 3, 11, 1}, nil},
{[]interface{}{3, 2, 3, 11, 1}, "11"},
{[]interface{}{1.1, "2.1", "3.1", "11.1", "1.1"}, "2.1"},
}
for _, t := range tbl {
fc := funcs[ast.Elt]
f, err := fc.getFunction(datumsToConstants(types.MakeDatums(t.argLst...)), s.ctx)
c.Assert(err, IsNil)
r, err := f.eval(nil)
c.Assert(r, testutil.DatumEquals, types.NewDatum(t.ret))
}
}
2 changes: 1 addition & 1 deletion plan/typeinferer.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (v *typeInferrer) handleFuncCallExpr(x *ast.FuncCallExpr) {
tp = types.NewFieldType(mysql.TypeDatetime)
case "dayname", "version", "database", "user", "current_user", "schema",
"concat", "concat_ws", "left", "lcase", "lower", "repeat",
"replace", "ucase", "upper", "convert", "substring",
"replace", "ucase", "upper", "convert", "substring", "elt",
"substring_index", "trim", "ltrim", "rtrim", "reverse", "hex", "unhex",
"date_format", "rpad", "lpad", "char_func", "conv", "make_set", "oct":
tp = types.NewFieldType(mysql.TypeVarString)
Expand Down
1 change: 1 addition & 0 deletions plan/typeinferer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func (ts *testTypeInferrerSuite) TestInferType(c *C) {
{"DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y')", mysql.TypeVarString, "utf8"},
{"rpad('TiDB', 12, 'go')", mysql.TypeVarString, charset.CharsetUTF8},
{"lpad('TiDB', 12, 'go')", mysql.TypeVarString, charset.CharsetUTF8},
{"elt(1, 'TiDB', 17, 'go')", mysql.TypeVarString, charset.CharsetUTF8},
{"bit_length('TiDB')", mysql.TypeLonglong, charset.CharsetBin},
{"char(66)", mysql.TypeVarString, charset.CharsetUTF8},
{"char_length('TiDB')", mysql.TypeLonglong, charset.CharsetBin},
Expand Down