github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/milevadb-server/statistics/sample_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package statistics
    15  
    16  import (
    17  	"time"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    21  	"github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx"
    22  	"github.com/whtcorpsinc/milevadb/types"
    23  	"github.com/whtcorpsinc/milevadb/soliton/collate"
    24  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    25  	"github.com/whtcorpsinc/milevadb/soliton/sqlexec"
    26  )
    27  
    28  var _ = Suite(&testSampleSuite{})
    29  
    30  type testSampleSuite struct {
    31  	count int
    32  	rs    sqlexec.RecordSet
    33  }
    34  
    35  func (s *testSampleSuite) SetUpSuite(c *C) {
    36  	s.count = 10000
    37  	rs := &recordSet{
    38  		data:      make([]types.Causet, s.count),
    39  		count:     s.count,
    40  		cursor:    0,
    41  		firstIsID: true,
    42  	}
    43  	rs.setFields(allegrosql.TypeLonglong, allegrosql.TypeLonglong)
    44  	start := 1000 // 1000 values is null
    45  	for i := start; i < rs.count; i++ {
    46  		rs.data[i].SetInt64(int64(i))
    47  	}
    48  	for i := start; i < rs.count; i += 3 {
    49  		rs.data[i].SetInt64(rs.data[i].GetInt64() + 1)
    50  	}
    51  	for i := start; i < rs.count; i += 5 {
    52  		rs.data[i].SetInt64(rs.data[i].GetInt64() + 2)
    53  	}
    54  	s.rs = rs
    55  }
    56  
    57  func (s *testSampleSuite) TestDefCauslectDeferredCausetStats(c *C) {
    58  	sc := mock.NewContext().GetStochastikVars().StmtCtx
    59  	builder := SampleBuilder{
    60  		Sc:              sc,
    61  		RecordSet:       s.rs,
    62  		DefCausLen:          1,
    63  		PkBuilder:       NewSortedBuilder(sc, 256, 1, types.NewFieldType(allegrosql.TypeLonglong)),
    64  		MaxSampleSize:   10000,
    65  		MaxBucketSize:   256,
    66  		MaxFMSketchSize: 1000,
    67  		CMSketchWidth:   2048,
    68  		CMSketchDepth:   8,
    69  		DefCauslators:       make([]collate.DefCauslator, 1),
    70  		DefCaussFieldType:   []*types.FieldType{types.NewFieldType(allegrosql.TypeLonglong)},
    71  	}
    72  	c.Assert(s.rs.Close(), IsNil)
    73  	collectors, pkBuilder, err := builder.DefCauslectDeferredCausetStats()
    74  	c.Assert(err, IsNil)
    75  	c.Assert(collectors[0].NullCount+collectors[0].Count, Equals, int64(s.count))
    76  	c.Assert(collectors[0].FMSketch.NDV(), Equals, int64(6232))
    77  	c.Assert(collectors[0].CMSketch.TotalCount(), Equals, uint64(collectors[0].Count))
    78  	c.Assert(pkBuilder.Count, Equals, int64(s.count))
    79  	c.Assert(pkBuilder.Hist().NDV, Equals, int64(s.count))
    80  }
    81  
    82  func (s *testSampleSuite) TestMergeSampleDefCauslector(c *C) {
    83  	builder := SampleBuilder{
    84  		Sc:              mock.NewContext().GetStochastikVars().StmtCtx,
    85  		RecordSet:       s.rs,
    86  		DefCausLen:          2,
    87  		MaxSampleSize:   1000,
    88  		MaxBucketSize:   256,
    89  		MaxFMSketchSize: 1000,
    90  		CMSketchWidth:   2048,
    91  		CMSketchDepth:   8,
    92  		DefCauslators:       make([]collate.DefCauslator, 2),
    93  		DefCaussFieldType:   []*types.FieldType{types.NewFieldType(allegrosql.TypeLonglong), types.NewFieldType(allegrosql.TypeLonglong)},
    94  	}
    95  	c.Assert(s.rs.Close(), IsNil)
    96  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
    97  	collectors, pkBuilder, err := builder.DefCauslectDeferredCausetStats()
    98  	c.Assert(err, IsNil)
    99  	c.Assert(pkBuilder, IsNil)
   100  	c.Assert(len(collectors), Equals, 2)
   101  	collectors[0].IsMerger = true
   102  	collectors[0].MergeSampleDefCauslector(sc, collectors[1])
   103  	c.Assert(collectors[0].FMSketch.NDV(), Equals, int64(9280))
   104  	c.Assert(len(collectors[0].Samples), Equals, 1000)
   105  	c.Assert(collectors[0].NullCount, Equals, int64(1000))
   106  	c.Assert(collectors[0].Count, Equals, int64(19000))
   107  	c.Assert(collectors[0].CMSketch.TotalCount(), Equals, uint64(collectors[0].Count))
   108  }
   109  
   110  func (s *testSampleSuite) TestDefCauslectorProtoConversion(c *C) {
   111  	builder := SampleBuilder{
   112  		Sc:              mock.NewContext().GetStochastikVars().StmtCtx,
   113  		RecordSet:       s.rs,
   114  		DefCausLen:          2,
   115  		MaxSampleSize:   10000,
   116  		MaxBucketSize:   256,
   117  		MaxFMSketchSize: 1000,
   118  		CMSketchWidth:   2048,
   119  		CMSketchDepth:   8,
   120  		DefCauslators:       make([]collate.DefCauslator, 2),
   121  		DefCaussFieldType:   []*types.FieldType{types.NewFieldType(allegrosql.TypeLonglong), types.NewFieldType(allegrosql.TypeLonglong)},
   122  	}
   123  	c.Assert(s.rs.Close(), IsNil)
   124  	collectors, pkBuilder, err := builder.DefCauslectDeferredCausetStats()
   125  	c.Assert(err, IsNil)
   126  	c.Assert(pkBuilder, IsNil)
   127  	for _, collector := range collectors {
   128  		p := SampleDefCauslectorToProto(collector)
   129  		s := SampleDefCauslectorFromProto(p)
   130  		c.Assert(collector.Count, Equals, s.Count)
   131  		c.Assert(collector.NullCount, Equals, s.NullCount)
   132  		c.Assert(collector.CMSketch.TotalCount(), Equals, s.CMSketch.TotalCount())
   133  		c.Assert(collector.FMSketch.NDV(), Equals, s.FMSketch.NDV())
   134  		c.Assert(collector.TotalSize, Equals, s.TotalSize)
   135  		c.Assert(len(collector.Samples), Equals, len(s.Samples))
   136  	}
   137  }