Skip to content

Commit

Permalink
expression: fix #4884, insert NULL on duplicate key by values function (
Browse files Browse the repository at this point in the history
  • Loading branch information
winkyao authored Oct 25, 2017
1 parent 3d71672 commit 5e441ad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
14 changes: 9 additions & 5 deletions expression/builtin_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type builtinRowSig struct {
baseBuiltinFunc
}

// rowFunc should always be flattened in expression rewrite phrase.
// evalString rowFunc should always be flattened in expression rewrite phrase.
func (b *builtinRowSig) evalString(row types.Row) (string, bool, error) {
panic("builtinRowSig.evalString() should never be called.")
}
Expand Down Expand Up @@ -258,6 +258,9 @@ func (b *builtinValuesStringSig) evalString(_ types.Row) (string, bool, error) {
}
row := values.([]types.Datum)
if b.offset < len(row) {
if row[b.offset].IsNull() {
return "", true, nil
}
return row[b.offset].GetString(), false, nil
}
return "", true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", len(row), b.offset)
Expand All @@ -269,7 +272,7 @@ type builtinValuesTimeSig struct {
offset int
}

// // evalTime evals a builtinValuesTimeSig.
// evalTime evals a builtinValuesTimeSig.
// See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_values
func (b *builtinValuesTimeSig) evalTime(_ types.Row) (types.Time, bool, error) {
values := b.ctx.GetSessionVars().CurrInsertValues
Expand All @@ -289,7 +292,7 @@ type builtinValuesDurationSig struct {
offset int
}

// // evalDuration evals a builtinValuesDurationSig.
// evalDuration evals a builtinValuesDurationSig.
// See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_values
func (b *builtinValuesDurationSig) evalDuration(_ types.Row) (types.Duration, bool, error) {
values := b.ctx.GetSessionVars().CurrInsertValues
Expand Down Expand Up @@ -361,12 +364,13 @@ func (b *builtinBitCountSig) evalInt(row types.Row) (int64, bool, error) {
return count, false, nil
}

// for plan cache of prepared statements
// getParamFunctionClass for plan cache of prepared statements
type getParamFunctionClass struct {
baseFunctionClass
}

//TODO: more typed functions will be added when typed parameters are supported.
// getFunction gets function
// TODO: more typed functions will be added when typed parameters are supported.
func (c *getParamFunctionClass) getFunction(ctx context.Context, args []Expression) (builtinFunc, error) {
if err := c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
Expand Down
10 changes: 10 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2668,6 +2668,16 @@ func (s *testIntegrationSuite) TestOtherBuiltin(c *C) {
tk.MustExec("INSERT INTO t (id,count)VALUES('abc',265.0) ON DUPLICATE KEY UPDATE count=if(VALUES(count) > count,VALUES(count),count)")
result = tk.MustQuery("select count from t where id = 'abc'")
result.Check(testkit.Rows("265.00"))

// for values(issue #4884)
tk.MustExec("drop table if exists t;")
tk.MustExec("create table test(id int not null, val text, primary key(id));")
tk.MustExec("insert into test values(1,'hello');")
result = tk.MustQuery("select * from test;")
result.Check(testkit.Rows("1 hello"))
tk.MustExec("insert into test values(1, NULL) on duplicate key update val = VALUES(val);")
result = tk.MustQuery("select * from test;")
result.Check(testkit.Rows("1 <nil>"))
}

func (s *testIntegrationSuite) TestDateBuiltin(c *C) {
Expand Down

0 comments on commit 5e441ad

Please sign in to comment.