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

expression, executor: rewrite built-in func length using new expression evaluation architecture #3519

Merged
merged 18 commits into from
Jun 22, 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
9 changes: 8 additions & 1 deletion executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,11 +784,18 @@ func (s *testSuite) TestStringBuiltin(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

// for length
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b double, c datetime, d time, e char(20), f bit(10))")
tk.MustExec(`insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef", 0b10101)`)
result := tk.MustQuery("select length(a), length(b), length(c), length(d), length(e), length(f), length(null) from t")
result.Check(testkit.Rows("1 3 19 8 6 2 <nil>"))

// for concat
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b double, c datetime, d time, e char(20))")
tk.MustExec(`insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef")`)
result := tk.MustQuery("select concat(a, b, c, d, e) from t")
result = tk.MustQuery("select concat(a, b, c, d, e) from t")
result.Check(testkit.Rows("11.12017-01-01 12:01:0112:01:01abcdef"))
result = tk.MustQuery("select concat(null)")
result.Check(testkit.Rows("<nil>"))
Expand Down
33 changes: 15 additions & 18 deletions expression/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,32 +132,29 @@ type lengthFunctionClass struct {
}

func (c *lengthFunctionClass) getFunction(args []Expression, ctx context.Context) (builtinFunc, error) {
sig := &builtinLengthSig{newBaseBuiltinFunc(args, ctx)}
tp := types.NewFieldType(mysql.TypeLonglong)
tp.Flen = 10
types.SetBinChsClnFlag(tp)
bf, err := newBaseBuiltinFuncWithTp(args, tp, ctx, tpString)
if err != nil {
return nil, errors.Trace(err)
}
sig := &builtinLengthSig{baseIntBuiltinFunc{bf}}
return sig.setSelf(sig), errors.Trace(c.verifyArgs(args))
}

type builtinLengthSig struct {
baseBuiltinFunc
baseIntBuiltinFunc
}

// eval evals a builtinLengthSig.
// evalInt evaluates a builtinLengthSig.
// See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html
func (b *builtinLengthSig) eval(row []types.Datum) (d types.Datum, err error) {
args, err := b.evalArgs(row)
if err != nil {
return types.Datum{}, errors.Trace(err)
}
switch args[0].Kind() {
case types.KindNull:
return d, nil
default:
s, err := args[0].ToString()
if err != nil {
return d, errors.Trace(err)
}
d.SetInt64(int64(len(s)))
return d, nil
func (b *builtinLengthSig) evalInt(row []types.Datum) (int64, bool, error) {
val, isNull, err := b.args[0].EvalString(row, b.ctx.GetSessionVars().StmtCtx)
if isNull || err != nil {
return 0, isNull, errors.Trace(err)
}
return int64(len([]byte(val))), false, nil
}

type asciiFunctionClass struct {
Expand Down
66 changes: 41 additions & 25 deletions expression/builtin_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,51 @@ import (

func (s *testEvaluatorSuite) TestLength(c *C) {
defer testleak.AfterTest(c)()
fc := funcs[ast.Length]
f, err := fc.getFunction(datumsToConstants(types.MakeDatums(nil)), s.ctx)
c.Assert(err, IsNil)
d, err := f.eval(nil)
c.Assert(err, IsNil)
c.Assert(d.Kind(), Equals, types.KindNull)

tbl := []struct {
Input interface{}
Expected int64
cases := []struct {
args interface{}
expected int64
isNil bool
getErr bool
}{
{"abc", 3},
{1, 1},
{3.14, 4},
{types.Time{Time: types.FromGoTime(time.Now()), Fsp: 6, Type: mysql.TypeDatetime}, 26},
{types.Bit{Value: 1, Width: 8}, 1},
{types.Hex{Value: 1}, 1},
{types.Set{Value: 1, Name: "abc"}, 3},
{"abc", 3, false, false},
{"你好", 6, false, false},
{1, 1, false, false},
{3.14, 4, false, false},
{types.NewDecFromFloatForTest(123.123), 7, false, false},
{types.Time{Time: types.FromGoTime(time.Now()), Fsp: 6, Type: mysql.TypeDatetime}, 26, false, false},
{types.Bit{Value: 1, Width: 8}, 1, false, false},
{types.Hex{Value: 1}, 1, false, false},
{types.Set{Value: 1, Name: "abc"}, 3, false, false},
{types.Duration{Duration: time.Duration(12*time.Hour + 1*time.Minute + 1*time.Second), Fsp: types.DefaultFsp}, 8, false, false},
{nil, 0, true, false},
{errors.New("must error"), 0, false, true},
}

dtbl := tblToDtbl(tbl)

for _, t := range dtbl {
f, err := fc.getFunction(datumsToConstants(t["Input"]), s.ctx)
c.Assert(err, IsNil)
d, err = f.eval(nil)
c.Assert(err, IsNil)
c.Assert(d, testutil.DatumEquals, t["Expected"][0])
for _, t := range cases {
f, err := newFunctionForTest(s.ctx, ast.Length, primitiveValsToConstants([]interface{}{t.args})...)
c.Assert(err, IsNil)
tp := f.GetType()
c.Assert(tp.Tp, Equals, mysql.TypeLonglong)
c.Assert(tp.Charset, Equals, charset.CharsetBin)
c.Assert(tp.Collate, Equals, charset.CollationBin)
c.Assert(tp.Flag, Equals, uint(mysql.BinaryFlag))
c.Assert(tp.Flen, Equals, 10)
d, err := f.Eval(nil)
if t.getErr {
c.Assert(err, NotNil)
} else {
c.Assert(err, IsNil)
if t.isNil {
c.Assert(d.Kind(), Equals, types.KindNull)
} else {
c.Assert(d.GetInt64(), Equals, t.expected)
}
}
}

f, err := funcs[ast.Length].getFunction([]Expression{Zero}, s.ctx)
c.Assert(err, IsNil)
c.Assert(f.isDeterministic(), IsTrue)
}

func (s *testEvaluatorSuite) TestASCII(c *C) {
Expand Down
32 changes: 0 additions & 32 deletions expression/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,35 +583,3 @@ func (s *testEvaluatorSuite) TestMod(c *C) {
c.Assert(err, IsNil)
c.Assert(r, testutil.DatumEquals, types.NewDatum(1.5))
}

func (s *testEvaluatorSuite) TestDynamic(c *C) {
var dynamicFuncs = map[string]int{
ast.Rand: 0,
ast.ConnectionID: 0,
ast.CurrentUser: 0,
ast.User: 0,
ast.Database: 0,
ast.Schema: 0,
ast.FoundRows: 0,
ast.LastInsertId: 0,
ast.Version: 0,
ast.Sleep: 0,
ast.GetVar: 0,
ast.SetVar: 0,
ast.Values: 0,
ast.SessionUser: 0,
ast.SystemUser: 0,
ast.RowCount: 0,
ast.UUID: 0,
}
for name, fc := range funcs {
f, _ := fc.getFunction(nil, s.ctx)
if _, ok := dynamicFuncs[name]; ok {
c.Assert(f.isDeterministic(), IsFalse)
} else {
c.Assert(f.isDeterministic(), IsTrue)
}
}
values := NewValuesFunc(0, nil, nil)
c.Assert(values.Function.isDeterministic(), IsFalse)
}
2 changes: 1 addition & 1 deletion expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func evalExprToString(expr Expression, row []types.Datum, _ *variable.StatementC
if val.IsNull() || err != nil {
return res, val.IsNull(), errors.Trace(err)
}
if expr.GetTypeClass() == types.ClassString {
if expr.GetTypeClass() == types.ClassString || IsHybridType(expr) {
// We cannot use val.GetString() directly.
// For example, `Bit` is regarded as ClassString,
// while we can not use val.GetString() to get the value of a Bit variable,
Expand Down
6 changes: 3 additions & 3 deletions expression/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ var kindToMysqlType = map[byte]byte{
types.KindFloat64: mysql.TypeDouble,
types.KindString: mysql.TypeVarString,
types.KindBytes: mysql.TypeVarString,
types.KindMysqlBit: mysql.TypeVarString,
types.KindMysqlEnum: mysql.TypeVarString,
types.KindMysqlSet: mysql.TypeVarString,
types.KindMysqlBit: mysql.TypeBit,
types.KindMysqlEnum: mysql.TypeEnum,
types.KindMysqlSet: mysql.TypeSet,
types.KindRow: mysql.TypeVarString,
types.KindInterface: mysql.TypeVarString,
types.KindMysqlDecimal: mysql.TypeNewDecimal,
Expand Down