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: handle ENUM in builtin function Values() #9225

Merged
merged 7 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
27 changes: 22 additions & 5 deletions expression/builtin_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,13 +615,30 @@ func (b *builtinValuesStringSig) evalString(_ chunk.Row) (string, bool, error) {
if row.IsEmpty() {
return "", true, errors.New("Session current insert values is nil")
}
if b.offset < row.Len() {
if row.IsNull(b.offset) {
return "", true, nil
if b.offset >= row.Len() {
return "", true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", row.Len(), b.offset)
}

if row.IsNull(b.offset) {
return "", true, nil
}

// Specially handle the ENUM/SET/BIT input value.
if retType := b.getRetTp(); retType.Hybrid() {
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
val := row.GetDatum(b.offset, retType)
res, err := val.ToString()
if err != nil {
return "", true, err
}
return row.GetString(b.offset), false, nil

resLen := len([]rune(res))
if b.ctx.GetSessionVars().StmtCtx.PadCharToFullLength && retType.Tp == mysql.TypeString && resLen < retType.Flen {
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
res = res + strings.Repeat(" ", retType.Flen-resLen)
}
return res, false, nil
}
return "", true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", row.Len(), b.offset)

return row.GetString(b.offset), false, nil
}

type builtinValuesTimeSig struct {
Expand Down
11 changes: 11 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3882,3 +3882,14 @@ func (s *testIntegrationSuite) TestValuesFloat32(c *C) {
tk.MustExec(`insert into t values (1, 0.02) on duplicate key update j = values (j);`)
tk.MustQuery(`select * from t;`).Check(testkit.Rows(`1 0.02`))
}

func (s *testIntegrationSuite) TestValuesEnum(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t (a bigint primary key, b enum('a','b','c'));`)
tk.MustExec(`insert into t values (1, "a");`)
tk.MustQuery(`select * from t;`).Check(testkit.Rows(`1 a`))
tk.MustExec(`insert into t values (1, "b") on duplicate key update b = values(b);`)
tk.MustQuery(`select * from t;`).Check(testkit.Rows(`1 b`))
}