github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/milevadb-server/statistics/fmsketch_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/milevadb/stochastikctx/stmtctx"
    21  	"github.com/whtcorpsinc/milevadb/types"
    22  )
    23  
    24  // extractSampleItemsCausets is for test purpose only to extract Causet slice
    25  // from SampleItem slice.
    26  func extractSampleItemsCausets(items []*SampleItem) []types.Causet {
    27  	datums := make([]types.Causet, len(items))
    28  	for i, item := range items {
    29  		datums[i] = item.Value
    30  	}
    31  	return datums
    32  }
    33  
    34  func (s *testStatisticsSuite) TestSketch(c *C) {
    35  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
    36  	maxSize := 1000
    37  	sampleSketch, ndv, err := buildFMSketch(sc, extractSampleItemsCausets(s.samples), maxSize)
    38  	c.Check(err, IsNil)
    39  	c.Check(ndv, Equals, int64(6232))
    40  
    41  	rcSketch, ndv, err := buildFMSketch(sc, s.rc.(*recordSet).data, maxSize)
    42  	c.Check(err, IsNil)
    43  	c.Check(ndv, Equals, int64(73344))
    44  
    45  	pkSketch, ndv, err := buildFMSketch(sc, s.pk.(*recordSet).data, maxSize)
    46  	c.Check(err, IsNil)
    47  	c.Check(ndv, Equals, int64(100480))
    48  
    49  	sampleSketch.mergeFMSketch(pkSketch)
    50  	sampleSketch.mergeFMSketch(rcSketch)
    51  	c.Check(sampleSketch.NDV(), Equals, int64(100480))
    52  
    53  	maxSize = 2
    54  	sketch := NewFMSketch(maxSize)
    55  	sketch.insertHashValue(1)
    56  	sketch.insertHashValue(2)
    57  	c.Check(len(sketch.hashset), Equals, maxSize)
    58  	sketch.insertHashValue(4)
    59  	c.Check(len(sketch.hashset), LessEqual, maxSize)
    60  }
    61  
    62  func (s *testStatisticsSuite) TestSketchProtoConversion(c *C) {
    63  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
    64  	maxSize := 1000
    65  	sampleSketch, ndv, err := buildFMSketch(sc, extractSampleItemsCausets(s.samples), maxSize)
    66  	c.Check(err, IsNil)
    67  	c.Check(ndv, Equals, int64(6232))
    68  
    69  	p := FMSketchToProto(sampleSketch)
    70  	f := FMSketchFromProto(p)
    71  	c.Assert(sampleSketch.mask, Equals, f.mask)
    72  	c.Assert(len(sampleSketch.hashset), Equals, len(f.hashset))
    73  	for val := range sampleSketch.hashset {
    74  		c.Assert(f.hashset[val], IsTrue)
    75  	}
    76  }