github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/milevadb-server/statistics/histogram_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  	. "github.com/whtcorpsinc/check"
    18  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    19  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    20  	"github.com/whtcorpsinc/milevadb/types"
    21  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    22  	"github.com/whtcorpsinc/milevadb/soliton/codec"
    23  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    24  	"github.com/whtcorpsinc/milevadb/soliton/ranger"
    25  )
    26  
    27  func (s *testStatisticsSuite) TestNewHistogramBySelectivity(c *C) {
    28  	coll := &HistDefCausl{
    29  		Count:   330,
    30  		DeferredCausets: make(map[int64]*DeferredCauset),
    31  		Indices: make(map[int64]*Index),
    32  	}
    33  	ctx := mock.NewContext()
    34  	sc := ctx.GetStochastikVars().StmtCtx
    35  	intDefCaus := &DeferredCauset{}
    36  	intDefCaus.Histogram = *NewHistogram(1, 30, 30, 0, types.NewFieldType(allegrosql.TypeLonglong), chunk.InitialCapacity, 0)
    37  	intDefCaus.IsHandle = true
    38  	for i := 0; i < 10; i++ {
    39  		intDefCaus.Bounds.AppendInt64(0, int64(i*3))
    40  		intDefCaus.Bounds.AppendInt64(0, int64(i*3+2))
    41  		intDefCaus.Buckets = append(intDefCaus.Buckets, Bucket{Repeat: 10, Count: int64(30*i + 30)})
    42  	}
    43  	coll.DeferredCausets[1] = intDefCaus
    44  	node := &StatsNode{ID: 1, Tp: PkType, Selectivity: 0.56}
    45  	node.Ranges = append(node.Ranges, &ranger.Range{LowVal: types.MakeCausets(nil), HighVal: types.MakeCausets(nil)})
    46  	node.Ranges = append(node.Ranges, &ranger.Range{LowVal: []types.Causet{types.MinNotNullCauset()}, HighVal: types.MakeCausets(2)})
    47  	node.Ranges = append(node.Ranges, &ranger.Range{LowVal: types.MakeCausets(5), HighVal: types.MakeCausets(6)})
    48  	node.Ranges = append(node.Ranges, &ranger.Range{LowVal: types.MakeCausets(8), HighVal: types.MakeCausets(10)})
    49  	node.Ranges = append(node.Ranges, &ranger.Range{LowVal: types.MakeCausets(13), HighVal: types.MakeCausets(13)})
    50  	node.Ranges = append(node.Ranges, &ranger.Range{LowVal: types.MakeCausets(25), HighVal: []types.Causet{types.MaxValueCauset()}})
    51  	intDefCausResult := `column:1 ndv:16 totDefCausSize:0
    52  num: 30 lower_bound: 0 upper_bound: 2 repeats: 10
    53  num: 11 lower_bound: 6 upper_bound: 8 repeats: 0
    54  num: 30 lower_bound: 9 upper_bound: 11 repeats: 0
    55  num: 1 lower_bound: 12 upper_bound: 14 repeats: 0
    56  num: 30 lower_bound: 27 upper_bound: 29 repeats: 0`
    57  
    58  	stringDefCaus := &DeferredCauset{}
    59  	stringDefCaus.Histogram = *NewHistogram(2, 15, 30, 0, types.NewFieldType(allegrosql.TypeString), chunk.InitialCapacity, 0)
    60  	stringDefCaus.Bounds.AppendString(0, "a")
    61  	stringDefCaus.Bounds.AppendString(0, "aaaabbbb")
    62  	stringDefCaus.Buckets = append(stringDefCaus.Buckets, Bucket{Repeat: 10, Count: 60})
    63  	stringDefCaus.Bounds.AppendString(0, "bbbb")
    64  	stringDefCaus.Bounds.AppendString(0, "fdsfdsfds")
    65  	stringDefCaus.Buckets = append(stringDefCaus.Buckets, Bucket{Repeat: 10, Count: 120})
    66  	stringDefCaus.Bounds.AppendString(0, "kkkkk")
    67  	stringDefCaus.Bounds.AppendString(0, "ooooo")
    68  	stringDefCaus.Buckets = append(stringDefCaus.Buckets, Bucket{Repeat: 10, Count: 180})
    69  	stringDefCaus.Bounds.AppendString(0, "oooooo")
    70  	stringDefCaus.Bounds.AppendString(0, "sssss")
    71  	stringDefCaus.Buckets = append(stringDefCaus.Buckets, Bucket{Repeat: 10, Count: 240})
    72  	stringDefCaus.Bounds.AppendString(0, "ssssssu")
    73  	stringDefCaus.Bounds.AppendString(0, "yyyyy")
    74  	stringDefCaus.Buckets = append(stringDefCaus.Buckets, Bucket{Repeat: 10, Count: 300})
    75  	stringDefCaus.PreCalculateScalar()
    76  	coll.DeferredCausets[2] = stringDefCaus
    77  	node2 := &StatsNode{ID: 2, Tp: DefCausType, Selectivity: 0.6}
    78  	node2.Ranges = append(node2.Ranges, &ranger.Range{LowVal: types.MakeCausets(nil), HighVal: types.MakeCausets(nil)})
    79  	node2.Ranges = append(node2.Ranges, &ranger.Range{LowVal: []types.Causet{types.MinNotNullCauset()}, HighVal: types.MakeCausets("aaa")})
    80  	node2.Ranges = append(node2.Ranges, &ranger.Range{LowVal: types.MakeCausets("aaaaaaaaaaa"), HighVal: types.MakeCausets("aaaaaaaaaaaaaa")})
    81  	node2.Ranges = append(node2.Ranges, &ranger.Range{LowVal: types.MakeCausets("bbb"), HighVal: types.MakeCausets("cccc")})
    82  	node2.Ranges = append(node2.Ranges, &ranger.Range{LowVal: types.MakeCausets("ddd"), HighVal: types.MakeCausets("fff")})
    83  	node2.Ranges = append(node2.Ranges, &ranger.Range{LowVal: types.MakeCausets("ggg"), HighVal: []types.Causet{types.MaxValueCauset()}})
    84  	stringDefCausResult := `column:2 ndv:9 totDefCausSize:0
    85  num: 60 lower_bound: a upper_bound: aaaabbbb repeats: 0
    86  num: 52 lower_bound: bbbb upper_bound: fdsfdsfds repeats: 0
    87  num: 54 lower_bound: kkkkk upper_bound: ooooo repeats: 0
    88  num: 60 lower_bound: oooooo upper_bound: sssss repeats: 0
    89  num: 60 lower_bound: ssssssu upper_bound: yyyyy repeats: 0`
    90  
    91  	newDefCausl := coll.NewHistDefCauslBySelectivity(sc, []*StatsNode{node, node2})
    92  	c.Assert(newDefCausl.DeferredCausets[1].String(), Equals, intDefCausResult)
    93  	c.Assert(newDefCausl.DeferredCausets[2].String(), Equals, stringDefCausResult)
    94  
    95  	idx := &Index{Info: &perceptron.IndexInfo{DeferredCausets: []*perceptron.IndexDeferredCauset{{Name: perceptron.NewCIStr("a"), Offset: 0}}}}
    96  	coll.Indices[0] = idx
    97  	idx.Histogram = *NewHistogram(0, 15, 0, 0, types.NewFieldType(allegrosql.TypeBlob), 0, 0)
    98  	for i := 0; i < 5; i++ {
    99  		low, err1 := codec.EncodeKey(sc, nil, types.NewIntCauset(int64(i*3)))
   100  		c.Assert(err1, IsNil, Commentf("Test failed: %v", err1))
   101  		high, err2 := codec.EncodeKey(sc, nil, types.NewIntCauset(int64(i*3+2)))
   102  		c.Assert(err2, IsNil, Commentf("Test failed: %v", err2))
   103  		idx.Bounds.AppendBytes(0, low)
   104  		idx.Bounds.AppendBytes(0, high)
   105  		idx.Buckets = append(idx.Buckets, Bucket{Repeat: 10, Count: int64(30*i + 30)})
   106  	}
   107  	idx.PreCalculateScalar()
   108  	node3 := &StatsNode{ID: 0, Tp: IndexType, Selectivity: 0.47}
   109  	node3.Ranges = append(node3.Ranges, &ranger.Range{LowVal: types.MakeCausets(2), HighVal: types.MakeCausets(3)})
   110  	node3.Ranges = append(node3.Ranges, &ranger.Range{LowVal: types.MakeCausets(10), HighVal: types.MakeCausets(13)})
   111  
   112  	idxResult := `index:0 ndv:7
   113  num: 30 lower_bound: 0 upper_bound: 2 repeats: 10
   114  num: 30 lower_bound: 3 upper_bound: 5 repeats: 10
   115  num: 30 lower_bound: 9 upper_bound: 11 repeats: 10
   116  num: 30 lower_bound: 12 upper_bound: 14 repeats: 10`
   117  
   118  	newDefCausl = coll.NewHistDefCauslBySelectivity(sc, []*StatsNode{node3})
   119  	c.Assert(newDefCausl.Indices[0].String(), Equals, idxResult)
   120  }
   121  
   122  func (s *testStatisticsSuite) TestTruncateHistogram(c *C) {
   123  	hist := NewHistogram(0, 0, 0, 0, types.NewFieldType(allegrosql.TypeLonglong), 1, 0)
   124  	low, high := types.NewIntCauset(0), types.NewIntCauset(1)
   125  	hist.AppendBucket(&low, &high, 0, 1)
   126  	newHist := hist.TruncateHistogram(1)
   127  	c.Assert(HistogramEqual(hist, newHist, true), IsTrue)
   128  	newHist = hist.TruncateHistogram(0)
   129  	c.Assert(newHist.Len(), Equals, 0)
   130  }
   131  
   132  func (s *testStatisticsSuite) TestValueToString4InvalidKey(c *C) {
   133  	bytes, err := codec.EncodeKey(nil, nil, types.NewCauset(1), types.NewCauset(0.5))
   134  	c.Assert(err, IsNil)
   135  	// Append invalid flag.
   136  	bytes = append(bytes, 20)
   137  	causet := types.NewCauset(bytes)
   138  	res, err := ValueToString(nil, &causet, 3, nil)
   139  	c.Assert(err, IsNil)
   140  	c.Assert(res, Equals, "(1, 0.5, \x14)")
   141  }