Skip to content

Commit

Permalink
expression: fix values null value issue (pingcap#4923)
Browse files Browse the repository at this point in the history
  • Loading branch information
winkyao committed Oct 27, 2017
1 parent 42f7b2e commit 154a00e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
20 changes: 13 additions & 7 deletions expression/builtin_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (b *builtinValuesIntSig) evalInt(_ []types.Datum) (int64, bool, error) {
}
row := values.([]types.Datum)
if b.offset < len(row) {
return row[b.offset].GetInt64(), false, nil
return row[b.offset].GetInt64(), row[b.offset].IsNull(), nil
}
return 0, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", len(row), b.offset)
}
Expand All @@ -216,7 +216,7 @@ func (b *builtinValuesRealSig) evalReal(_ []types.Datum) (float64, bool, error)
}
row := values.([]types.Datum)
if b.offset < len(row) {
return row[b.offset].GetFloat64(), false, nil
return row[b.offset].GetFloat64(), row[b.offset].IsNull(), nil
}
return 0, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", len(row), b.offset)
}
Expand All @@ -236,6 +236,9 @@ func (b *builtinValuesDecimalSig) evalDecimal(_ []types.Datum) (*types.MyDecimal
}
row := values.([]types.Datum)
if b.offset < len(row) {
if row[b.offset].IsNull() {
return nil, true, nil
}
return row[b.offset].GetMysqlDecimal(), false, nil
}
return nil, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", len(row), b.offset)
Expand All @@ -256,10 +259,7 @@ func (b *builtinValuesStringSig) evalString(_ []types.Datum) (string, bool, erro
}
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 row[b.offset].GetString(), row[b.offset].IsNull(), 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 @@ -279,6 +279,9 @@ func (b *builtinValuesTimeSig) evalTime(_ []types.Datum) (types.Time, bool, erro
}
row := values.([]types.Datum)
if b.offset < len(row) {
if row[b.offset].IsNull() {
return types.Time{}, true, nil
}
return row[b.offset].GetMysqlTime(), false, nil
}
return types.Time{}, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", len(row), b.offset)
Expand All @@ -299,7 +302,7 @@ func (b *builtinValuesDurationSig) evalDuration(_ []types.Datum) (types.Duration
}
row := values.([]types.Datum)
if b.offset < len(row) {
return row[b.offset].GetMysqlDuration(), false, nil
return row[b.offset].GetMysqlDuration(), row[b.offset].IsNull(), nil
}
return types.Duration{}, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", len(row), b.offset)
}
Expand All @@ -319,6 +322,9 @@ func (b *builtinValuesJSONSig) evalJSON(_ []types.Datum) (json.JSON, bool, error
}
row := values.([]types.Datum)
if b.offset < len(row) {
if row[b.offset].IsNull() {
return json.JSON{}, true, nil
}
return row[b.offset].GetMysqlJSON(), false, nil
}
return json.JSON{}, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", len(row), b.offset)
Expand Down
26 changes: 26 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2678,6 +2678,32 @@ func (s *testIntegrationSuite) TestOtherBuiltin(c *C) {
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>"))

tk.MustExec("drop table if exists test;")
tk.MustExec(`create table test(
id int not null,
a text,
b blob,
c varchar(20),
d int,
e float,
f DECIMAL(6,4),
g JSON,
primary key(id));`)

tk.MustExec(`insert into test values(1,'txt hello', 'blb hello', 'vc hello', 1, 1.1, 1.0, '{"key1": "value1", "key2": "value2"}');`)
tk.MustExec(`insert into test values(1, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
on duplicate key update
a = values(a),
b = values(b),
c = values(c),
d = values(d),
e = values(e),
f = values(f),
g = values(g);`)

result = tk.MustQuery("select * from test;")
result.Check(testkit.Rows("1 <nil> <nil> <nil> <nil> <nil> <nil> <nil>"))
}

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

0 comments on commit 154a00e

Please sign in to comment.