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: length(binary(m)) should return 'm' instead of the concrete string length(#3644) #3745

Closed
wants to merge 3 commits into from
Closed
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
7 changes: 7 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,13 @@ func (s *testSuite) TestStringBuiltin(c *C) {
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>"))
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a int, b binary(22));")
tk.MustExec("insert into t set b='test';")
tk.MustExec("alter table t modify b binary(33);")
tk.MustExec("insert into t set b='test1';")
result = tk.MustQuery("select length(b) from t;")
result.Check(testkit.Rows("33", "33"))

// for concat
tk.MustExec("drop table if exists t")
Expand Down
27 changes: 25 additions & 2 deletions expression/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ var (

var (
_ builtinFunc = &builtinLengthSig{}
_ builtinFunc = &builtinLength4BinarySig{}
_ builtinFunc = &builtinASCIISig{}
_ builtinFunc = &builtinConcatSig{}
_ builtinFunc = &builtinConcatWSSig{}
Expand Down Expand Up @@ -132,19 +133,33 @@ type lengthFunctionClass struct {
}

func (c *lengthFunctionClass) getFunction(args []Expression, ctx context.Context) (builtinFunc, error) {
err := c.verifyArgs(args)
if err != nil {
return nil, errors.Trace(err)
}
bf, err := newBaseBuiltinFuncWithTp(args, ctx, tpInt, tpString)
if err != nil {
return nil, errors.Trace(err)
}
bf.tp.Flen = 10
sig := &builtinLengthSig{baseIntBuiltinFunc{bf}}
return sig.setSelf(sig), errors.Trace(c.verifyArgs(args))
var sig builtinFunc
argTp := args[0].GetType()
if argTp.Tp == mysql.TypeString && mysql.HasBinaryFlag(argTp.Flag) {
Copy link
Contributor

Choose a reason for hiding this comment

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

need varbinary be treated as this?

Copy link
Member Author

Choose a reason for hiding this comment

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

no, just binary

DROP TABLE IF EXISTS table_binary;
CREATE TABLE table_binary (
    a BINARY(20),
    b VARBINARY(20),
    c TINYBLOB,
    d BLOB(20),
    e MEDIUMBLOB,
    f LONGBLOB,
    g BIGINT,
    UNIQUE KEY(a),
    UNIQUE KEY(g)
);
INSERT INTO table_binary (a, b, c, d, e, f, g) VALUES ("a ", "b ", "c ", "d ", "e ", "f ", 2);
select length(a), length(b), length(c), length(d), length(e), length(f), length(g) from table_binary;
+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
| length(a) | length(b) | length(c) | length(d) | length(e) | length(f) | length(g) |
+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
|        20 |         2 |         2 |         2 |         2 |         2 |         1 |
+-----------+-----------+-----------+-----------+-----------+-----------+-----------+

sig = &builtinLength4BinarySig{baseIntBuiltinFunc{bf}}
} else {
sig = &builtinLengthSig{baseIntBuiltinFunc{bf}}
}
return sig.setSelf(sig), nil
}

type builtinLengthSig struct {
baseIntBuiltinFunc
}

type builtinLength4BinarySig struct {
baseIntBuiltinFunc
}

// evalInt evaluates a builtinLengthSig.
// See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html
func (b *builtinLengthSig) evalInt(row []types.Datum) (int64, bool, error) {
Expand All @@ -155,6 +170,14 @@ func (b *builtinLengthSig) evalInt(row []types.Datum) (int64, bool, error) {
return int64(len([]byte(val))), false, nil
}

func (b *builtinLength4BinarySig) evalInt(row []types.Datum) (int64, bool, error) {
_, isNull, err := b.args[0].EvalString(row, b.ctx.GetSessionVars().StmtCtx)
if isNull || err != nil {
return 0, isNull, errors.Trace(err)
}
return int64(b.args[0].GetType().Flen), false, nil
}

type asciiFunctionClass struct {
baseFunctionClass
}
Expand Down