Skip to content

Commit

Permalink
types: handle NULL datum if we need convert it to string. (pingcap#…
Browse files Browse the repository at this point in the history
…6761)

* types: handle `NULL` when we need to
  • Loading branch information
winoros authored and ngaut committed Jun 6, 2018
1 parent fbed209 commit 96a5109
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ func (e *InsertValues) getKeysNeedCheck(rows []types.DatumRow) ([][]keyWithDupEr
if !distinct {
continue
}
colValStr, err1 := types.DatumsToString(colVals)
colValStr, err1 := types.DatumsToString(colVals, false)
if err1 != nil {
return nil, errors.Trace(err1)
}
Expand Down
9 changes: 9 additions & 0 deletions executor/show_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,12 @@ func (s *testSuite) TestShowStatsHealthy(c *C) {
do.StatsHandle().Update(do.InfoSchema())
tk.MustQuery("show stats_healthy").Check(testkit.Rows("test t 0"))
}

func (s *testSuite) TestShowStatsHasNullValue(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t (a int, index idx(a))")
tk.MustExec("insert into t values(NULL)")
tk.MustExec("analyze table t")
tk.MustQuery("show stats_buckets").Check(testkit.Rows("test t idx 1 0 1 1 NULL NULL"))
}
2 changes: 1 addition & 1 deletion server/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func (ts *HTTPHandlerTestSuite) TestDecodeColumnValue(c *C) {
var data interface{}
err = decoder.Decode(&data)
c.Assert(err, IsNil, Commentf("url:%v\ndata%v", url, data))
colVal, err := types.DatumsToString([]types.Datum{row[col.id-1]})
colVal, err := types.DatumsToString([]types.Datum{row[col.id-1]}, false)
c.Assert(err, IsNil)
c.Assert(data, Equals, colVal, Commentf("url:%v", url))
}
Expand Down
2 changes: 1 addition & 1 deletion statistics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func ValueToString(value *types.Datum, idxCols int) (string, error) {
if err != nil {
return "", errors.Trace(err)
}
str, err := types.DatumsToString(decodedVals)
str, err := types.DatumsToString(decodedVals, true)
if err != nil {
return "", errors.Trace(err)
}
Expand Down
6 changes: 5 additions & 1 deletion types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1807,9 +1807,13 @@ func handleTruncateError(sc *stmtctx.StatementContext) error {
}

// DatumsToString converts several datums to formatted string.
func DatumsToString(datums []Datum) (string, error) {
func DatumsToString(datums []Datum, handleNULL bool) (string, error) {
var strs []string
for _, datum := range datums {
if datum.Kind() == KindNull && handleNULL {
strs = append(strs, "NULL")
continue
}
str, err := datum.ToString()
if err != nil {
return "", errors.Trace(err)
Expand Down

0 comments on commit 96a5109

Please sign in to comment.