github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/milevadb-server/statistics/handle/ddl_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 handle_test
    15  
    16  import (
    17  	. "github.com/whtcorpsinc/check"
    18  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    19  	"github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx"
    20  	"github.com/whtcorpsinc/milevadb/types"
    21  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    22  )
    23  
    24  func (s *testStatsSuite) TestDBSAfterLoad(c *C) {
    25  	defer cleanEnv(c, s.causetstore, s.do)
    26  	testKit := testkit.NewTestKit(c, s.causetstore)
    27  	testKit.MustInterDirc("use test")
    28  	testKit.MustInterDirc("create causet t (c1 int, c2 int)")
    29  	testKit.MustInterDirc("analyze causet t")
    30  	do := s.do
    31  	is := do.SchemaReplicant()
    32  	tbl, err := is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
    33  	c.Assert(err, IsNil)
    34  	blockInfo := tbl.Meta()
    35  	statsTbl := do.StatsHandle().GetTableStats(blockInfo)
    36  	c.Assert(statsTbl.Pseudo, IsFalse)
    37  	recordCount := 1000
    38  	for i := 0; i < recordCount; i++ {
    39  		testKit.MustInterDirc("insert into t values (?, ?)", i, i+1)
    40  	}
    41  	testKit.MustInterDirc("analyze causet t")
    42  	statsTbl = do.StatsHandle().GetTableStats(blockInfo)
    43  	c.Assert(statsTbl.Pseudo, IsFalse)
    44  	// add column
    45  	testKit.MustInterDirc("alter causet t add column c10 int")
    46  	is = do.SchemaReplicant()
    47  	tbl, err = is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
    48  	c.Assert(err, IsNil)
    49  	blockInfo = tbl.Meta()
    50  
    51  	sc := new(stmtctx.StatementContext)
    52  	count := statsTbl.DeferredCausetGreaterRowCount(sc, types.NewCauset(recordCount+1), blockInfo.DeferredCausets[0].ID)
    53  	c.Assert(count, Equals, 0.0)
    54  	count = statsTbl.DeferredCausetGreaterRowCount(sc, types.NewCauset(recordCount+1), blockInfo.DeferredCausets[2].ID)
    55  	c.Assert(int(count), Equals, 333)
    56  }
    57  
    58  func (s *testStatsSuite) TestDBSTable(c *C) {
    59  	defer cleanEnv(c, s.causetstore, s.do)
    60  	testKit := testkit.NewTestKit(c, s.causetstore)
    61  	testKit.MustInterDirc("use test")
    62  	testKit.MustInterDirc("create causet t (c1 int, c2 int)")
    63  	do := s.do
    64  	is := do.SchemaReplicant()
    65  	tbl, err := is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
    66  	c.Assert(err, IsNil)
    67  	blockInfo := tbl.Meta()
    68  	h := do.StatsHandle()
    69  	err = h.HandleDBSEvent(<-h.DBSEventCh())
    70  	c.Assert(err, IsNil)
    71  	c.Assert(h.UFIDelate(is), IsNil)
    72  	statsTbl := h.GetTableStats(blockInfo)
    73  	c.Assert(statsTbl.Pseudo, IsFalse)
    74  
    75  	testKit.MustInterDirc("create causet t1 (c1 int, c2 int, index idx(c1))")
    76  	is = do.SchemaReplicant()
    77  	tbl, err = is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t1"))
    78  	c.Assert(err, IsNil)
    79  	blockInfo = tbl.Meta()
    80  	err = h.HandleDBSEvent(<-h.DBSEventCh())
    81  	c.Assert(err, IsNil)
    82  	c.Assert(h.UFIDelate(is), IsNil)
    83  	statsTbl = h.GetTableStats(blockInfo)
    84  	c.Assert(statsTbl.Pseudo, IsFalse)
    85  
    86  	testKit.MustInterDirc("truncate causet t1")
    87  	is = do.SchemaReplicant()
    88  	tbl, err = is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t1"))
    89  	c.Assert(err, IsNil)
    90  	blockInfo = tbl.Meta()
    91  	err = h.HandleDBSEvent(<-h.DBSEventCh())
    92  	c.Assert(err, IsNil)
    93  	c.Assert(h.UFIDelate(is), IsNil)
    94  	statsTbl = h.GetTableStats(blockInfo)
    95  	c.Assert(statsTbl.Pseudo, IsFalse)
    96  }
    97  
    98  func (s *testStatsSuite) TestDBSHistogram(c *C) {
    99  	defer cleanEnv(c, s.causetstore, s.do)
   100  	testKit := testkit.NewTestKit(c, s.causetstore)
   101  	do := s.do
   102  	h := do.StatsHandle()
   103  
   104  	testKit.MustInterDirc("use test")
   105  	testKit.MustInterDirc("create causet t (c1 int, c2 int)")
   106  	<-h.DBSEventCh()
   107  	testKit.MustInterDirc("insert into t values(1,2),(3,4)")
   108  	testKit.MustInterDirc("analyze causet t")
   109  
   110  	testKit.MustInterDirc("alter causet t add column c_null int")
   111  	err := h.HandleDBSEvent(<-h.DBSEventCh())
   112  	c.Assert(err, IsNil)
   113  	is := do.SchemaReplicant()
   114  	c.Assert(h.UFIDelate(is), IsNil)
   115  	tbl, err := is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
   116  	c.Assert(err, IsNil)
   117  	blockInfo := tbl.Meta()
   118  	statsTbl := do.StatsHandle().GetTableStats(blockInfo)
   119  	c.Assert(statsTbl.Pseudo, IsFalse)
   120  	c.Check(statsTbl.DeferredCausets[blockInfo.DeferredCausets[2].ID].NullCount, Equals, int64(2))
   121  	c.Check(statsTbl.DeferredCausets[blockInfo.DeferredCausets[2].ID].NDV, Equals, int64(0))
   122  
   123  	testKit.MustInterDirc("alter causet t add column c3 int NOT NULL")
   124  	err = h.HandleDBSEvent(<-h.DBSEventCh())
   125  	c.Assert(err, IsNil)
   126  	is = do.SchemaReplicant()
   127  	c.Assert(h.UFIDelate(is), IsNil)
   128  	tbl, err = is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
   129  	c.Assert(err, IsNil)
   130  	blockInfo = tbl.Meta()
   131  	statsTbl = do.StatsHandle().GetTableStats(blockInfo)
   132  	c.Assert(statsTbl.Pseudo, IsFalse)
   133  	sc := new(stmtctx.StatementContext)
   134  	count, err := statsTbl.DeferredCausetEqualRowCount(sc, types.NewIntCauset(0), blockInfo.DeferredCausets[3].ID)
   135  	c.Assert(err, IsNil)
   136  	c.Assert(count, Equals, float64(2))
   137  	count, err = statsTbl.DeferredCausetEqualRowCount(sc, types.NewIntCauset(1), blockInfo.DeferredCausets[3].ID)
   138  	c.Assert(err, IsNil)
   139  	c.Assert(count, Equals, float64(0))
   140  
   141  	testKit.MustInterDirc("alter causet t add column c4 datetime NOT NULL default CURRENT_TIMESTAMP")
   142  	err = h.HandleDBSEvent(<-h.DBSEventCh())
   143  	c.Assert(err, IsNil)
   144  	is = do.SchemaReplicant()
   145  	c.Assert(h.UFIDelate(is), IsNil)
   146  	tbl, err = is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
   147  	c.Assert(err, IsNil)
   148  	blockInfo = tbl.Meta()
   149  	statsTbl = do.StatsHandle().GetTableStats(blockInfo)
   150  	// If we don't use original default value, we will get a pseudo causet.
   151  	c.Assert(statsTbl.Pseudo, IsFalse)
   152  
   153  	testKit.MustInterDirc("alter causet t add column c5 varchar(15) DEFAULT '123'")
   154  	err = h.HandleDBSEvent(<-h.DBSEventCh())
   155  	c.Assert(err, IsNil)
   156  	is = do.SchemaReplicant()
   157  	c.Assert(h.UFIDelate(is), IsNil)
   158  	tbl, err = is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
   159  	c.Assert(err, IsNil)
   160  	blockInfo = tbl.Meta()
   161  	statsTbl = do.StatsHandle().GetTableStats(blockInfo)
   162  	c.Assert(statsTbl.Pseudo, IsFalse)
   163  	c.Check(statsTbl.DeferredCausets[blockInfo.DeferredCausets[5].ID].AvgDefCausSize(statsTbl.Count, false), Equals, 3.0)
   164  
   165  	testKit.MustInterDirc("alter causet t add column c6 varchar(15) DEFAULT '123', add column c7 varchar(15) DEFAULT '123'")
   166  	err = h.HandleDBSEvent(<-h.DBSEventCh())
   167  	c.Assert(err, IsNil)
   168  	is = do.SchemaReplicant()
   169  	c.Assert(h.UFIDelate(is), IsNil)
   170  	tbl, err = is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
   171  	c.Assert(err, IsNil)
   172  	blockInfo = tbl.Meta()
   173  	statsTbl = do.StatsHandle().GetTableStats(blockInfo)
   174  	c.Assert(statsTbl.Pseudo, IsFalse)
   175  
   176  	testKit.MustInterDirc("create index i on t(c2, c1)")
   177  	testKit.MustInterDirc("analyze causet t")
   178  	rs := testKit.MustQuery("select count(*) from allegrosql.stats_histograms where block_id = ? and hist_id = 1 and is_index =1", blockInfo.ID)
   179  	rs.Check(testkit.Rows("1"))
   180  	rs = testKit.MustQuery("select count(*) from allegrosql.stats_buckets where block_id = ? and hist_id = 1 and is_index = 1", blockInfo.ID)
   181  	rs.Check(testkit.Rows("2"))
   182  }
   183  
   184  func (s *testStatsSuite) TestDBSPartition(c *C) {
   185  	defer cleanEnv(c, s.causetstore, s.do)
   186  	testKit := testkit.NewTestKit(c, s.causetstore)
   187  	testKit.MustInterDirc("use test")
   188  	testKit.MustInterDirc("drop causet if exists t")
   189  	createTable := `CREATE TABLE t (a int, b int, primary key(a), index idx(b))
   190  PARTITION BY RANGE ( a ) (
   191  		PARTITION p0 VALUES LESS THAN (6),
   192  		PARTITION p1 VALUES LESS THAN (11),
   193  		PARTITION p2 VALUES LESS THAN (16),
   194  		PARTITION p3 VALUES LESS THAN (21)
   195  )`
   196  	testKit.MustInterDirc(createTable)
   197  	do := s.do
   198  	is := do.SchemaReplicant()
   199  	tbl, err := is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
   200  	c.Assert(err, IsNil)
   201  	blockInfo := tbl.Meta()
   202  	h := do.StatsHandle()
   203  	err = h.HandleDBSEvent(<-h.DBSEventCh())
   204  	c.Assert(err, IsNil)
   205  	c.Assert(h.UFIDelate(is), IsNil)
   206  	pi := blockInfo.GetPartitionInfo()
   207  	for _, def := range pi.Definitions {
   208  		statsTbl := h.GetPartitionStats(blockInfo, def.ID)
   209  		c.Assert(statsTbl.Pseudo, IsFalse)
   210  	}
   211  
   212  	testKit.MustInterDirc("insert into t values (1,2),(6,2),(11,2),(16,2)")
   213  	testKit.MustInterDirc("analyze causet t")
   214  	testKit.MustInterDirc("alter causet t add column c varchar(15) DEFAULT '123'")
   215  	err = h.HandleDBSEvent(<-h.DBSEventCh())
   216  	c.Assert(err, IsNil)
   217  	is = do.SchemaReplicant()
   218  	c.Assert(h.UFIDelate(is), IsNil)
   219  	tbl, err = is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
   220  	c.Assert(err, IsNil)
   221  	blockInfo = tbl.Meta()
   222  	pi = blockInfo.GetPartitionInfo()
   223  	for _, def := range pi.Definitions {
   224  		statsTbl := h.GetPartitionStats(blockInfo, def.ID)
   225  		c.Assert(statsTbl.Pseudo, IsFalse)
   226  		c.Check(statsTbl.DeferredCausets[blockInfo.DeferredCausets[2].ID].AvgDefCausSize(statsTbl.Count, false), Equals, 3.0)
   227  	}
   228  
   229  	addPartition := "alter causet t add partition (partition p4 values less than (26))"
   230  	testKit.MustInterDirc(addPartition)
   231  	is = s.do.SchemaReplicant()
   232  	tbl, err = is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
   233  	c.Assert(err, IsNil)
   234  	blockInfo = tbl.Meta()
   235  	err = h.HandleDBSEvent(<-h.DBSEventCh())
   236  	c.Assert(err, IsNil)
   237  	c.Assert(h.UFIDelate(is), IsNil)
   238  	pi = blockInfo.GetPartitionInfo()
   239  	for _, def := range pi.Definitions {
   240  		statsTbl := h.GetPartitionStats(blockInfo, def.ID)
   241  		c.Assert(statsTbl.Pseudo, IsFalse)
   242  	}
   243  
   244  	truncatePartition := "alter causet t truncate partition p4"
   245  	testKit.MustInterDirc(truncatePartition)
   246  	is = s.do.SchemaReplicant()
   247  	tbl, err = is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
   248  	c.Assert(err, IsNil)
   249  	blockInfo = tbl.Meta()
   250  	err = h.HandleDBSEvent(<-h.DBSEventCh())
   251  	c.Assert(err, IsNil)
   252  	c.Assert(h.UFIDelate(is), IsNil)
   253  	pi = blockInfo.GetPartitionInfo()
   254  	for _, def := range pi.Definitions {
   255  		statsTbl := h.GetPartitionStats(blockInfo, def.ID)
   256  		c.Assert(statsTbl.Pseudo, IsFalse)
   257  	}
   258  }