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 2 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
27 changes: 12 additions & 15 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)
types.SetBinChsClnFlag(tp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider field length?

sig := &builtinLengthSig{baseIntBuiltinFunc{newBaseBuiltinFuncWithTp(args, tp, ctx)}}
return sig.setSelf(sig), errors.Trace(c.verifyArgs(args))
}

type builtinLengthSig struct {
baseBuiltinFunc
baseIntBuiltinFunc
}

// eval evals a builtinLengthSig.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update this comments.

// 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)
func (b *builtinLengthSig) evalInt(row []types.Datum) (int64, bool, error) {
ctx, sc := b.ctx, b.ctx.GetSessionVars().StmtCtx
arg0, err := WrapWithCastAsString(b.args[0], ctx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not add cast here.

if err != nil {
return types.Datum{}, errors.Trace(err)
return 0, false, 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
val, isNull, err := arg0.EvalString(row, sc)
if isNull || err != nil {
return 0, isNull, errors.Trace(err)
}
return int64(len([]byte(val))), false, nil
}

type asciiFunctionClass struct {
Expand Down
61 changes: 36 additions & 25 deletions expression/builtin_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,45 @@ 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))
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)
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,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