Skip to content

Commit

Permalink
stats: fix bug when build histograms for null json (pingcap#5545)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored and Haibin Xie committed Jan 4, 2018
1 parent 76fccd5 commit 64ea885
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
9 changes: 7 additions & 2 deletions statistics/builder.go
Expand Up @@ -164,8 +164,13 @@ func BuildColumn(ctx context.Context, numBuckets, id int64, collector *SampleCol
}
bucketIdx := 0
var lastCount int64
hg.Buckets[0].LowerBound = samples[0]
for i := int64(0); i < int64(len(samples)); i++ {
hg.Buckets[0] = Bucket{
LowerBound: samples[0],
UpperBound: samples[0],
Count: int64(sampleFactor),
Repeats: int64(ndvFactor),
}
for i := int64(1); i < int64(len(samples)); i++ {
cmp, err := hg.Buckets[bucketIdx].UpperBound.CompareDatum(sc, &samples[i])
if err != nil {
return nil, errors.Trace(err)
Expand Down
19 changes: 14 additions & 5 deletions statistics/statistics_test.go
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/types"
"github.com/pingcap/tidb/util/types/json"
)

func TestT(t *testing.T) {
Expand All @@ -43,11 +44,6 @@ type testStatisticsSuite struct {
pk ast.RecordSet
}

type dataTable struct {
count int64
samples []types.Datum
}

type recordSet struct {
data []types.Datum
count int64
Expand Down Expand Up @@ -271,6 +267,19 @@ func (s *testStatisticsSuite) TestBuild(c *C) {
count, err = col.lessRowCount(sc, types.NewIntDatum(99999))
c.Check(err, IsNil)
c.Check(int(count), Equals, 99999)

datum := types.Datum{}
datum.SetMysqlJSON(json.JSON{TypeCode: json.TypeCodeLiteral})
collector = &SampleCollector{
Count: 1,
NullCount: 0,
Samples: []types.Datum{datum},
Sketch: sketch,
}
col, err = BuildColumn(ctx, bucketCount, 2, collector)
c.Assert(err, IsNil)
c.Assert(len(col.Buckets), Equals, 1)
c.Assert(col.Buckets[0].LowerBound, DeepEquals, col.Buckets[0].UpperBound)
}

func (s *testStatisticsSuite) TestHistogramProtoConversion(c *C) {
Expand Down

0 comments on commit 64ea885

Please sign in to comment.