github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/chunk/column_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 chunk
    15  
    16  import (
    17  	"fmt"
    18  	"math/rand"
    19  	"testing"
    20  	"time"
    21  	"unsafe"
    22  
    23  	"github.com/whtcorpsinc/check"
    24  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    25  	"github.com/whtcorpsinc/milevadb/types"
    26  	"github.com/whtcorpsinc/milevadb/types/json"
    27  )
    28  
    29  func equalDeferredCauset(c1, c2 *DeferredCauset) bool {
    30  	if c1.length != c2.length ||
    31  		c1.nullCount() != c2.nullCount() {
    32  		return false
    33  	}
    34  	if len(c1.nullBitmap) != len(c2.nullBitmap) ||
    35  		len(c1.offsets) != len(c2.offsets) ||
    36  		len(c1.data) != len(c2.data) ||
    37  		len(c1.elemBuf) != len(c2.elemBuf) {
    38  		return false
    39  	}
    40  	for i := range c1.nullBitmap {
    41  		if c1.nullBitmap[i] != c2.nullBitmap[i] {
    42  			return false
    43  		}
    44  	}
    45  	for i := range c1.offsets {
    46  		if c1.offsets[i] != c2.offsets[i] {
    47  			return false
    48  		}
    49  	}
    50  	for i := range c1.data {
    51  		if c1.data[i] != c2.data[i] {
    52  			return false
    53  		}
    54  	}
    55  	for i := range c1.elemBuf {
    56  		if c1.elemBuf[i] != c2.elemBuf[i] {
    57  			return false
    58  		}
    59  	}
    60  	return true
    61  }
    62  
    63  func (s *testChunkSuite) TestDeferredCausetCopy(c *check.C) {
    64  	defCaus := newFixedLenDeferredCauset(8, 10)
    65  	for i := 0; i < 10; i++ {
    66  		defCaus.AppendInt64(int64(i))
    67  	}
    68  
    69  	c1 := defCaus.CopyConstruct(nil)
    70  	c.Check(equalDeferredCauset(defCaus, c1), check.IsTrue)
    71  
    72  	c2 := newFixedLenDeferredCauset(8, 10)
    73  	c2 = defCaus.CopyConstruct(c2)
    74  	c.Check(equalDeferredCauset(defCaus, c2), check.IsTrue)
    75  }
    76  
    77  func (s *testChunkSuite) TestDeferredCausetCopyReconstructFixedLen(c *check.C) {
    78  	defCaus := NewDeferredCauset(types.NewFieldType(allegrosql.TypeLonglong), 1024)
    79  	results := make([]int64, 0, 1024)
    80  	nulls := make([]bool, 0, 1024)
    81  	sel := make([]int, 0, 1024)
    82  	for i := 0; i < 1024; i++ {
    83  		if rand.Intn(10) < 6 {
    84  			sel = append(sel, i)
    85  		}
    86  
    87  		if rand.Intn(10) < 2 {
    88  			defCaus.AppendNull()
    89  			nulls = append(nulls, true)
    90  			results = append(results, 0)
    91  			continue
    92  		}
    93  
    94  		v := rand.Int63()
    95  		defCaus.AppendInt64(v)
    96  		results = append(results, v)
    97  		nulls = append(nulls, false)
    98  	}
    99  
   100  	defCaus = defCaus.CopyReconstruct(sel, nil)
   101  	nullCnt := 0
   102  	for n, i := range sel {
   103  		if nulls[i] {
   104  			nullCnt++
   105  			c.Assert(defCaus.IsNull(n), check.Equals, true)
   106  		} else {
   107  			c.Assert(defCaus.GetInt64(n), check.Equals, results[i])
   108  		}
   109  	}
   110  	c.Assert(nullCnt, check.Equals, defCaus.nullCount())
   111  	c.Assert(defCaus.length, check.Equals, len(sel))
   112  
   113  	for i := 0; i < 128; i++ {
   114  		if i%2 == 0 {
   115  			defCaus.AppendNull()
   116  		} else {
   117  			defCaus.AppendInt64(int64(i * i * i))
   118  		}
   119  	}
   120  
   121  	c.Assert(defCaus.length, check.Equals, len(sel)+128)
   122  	c.Assert(defCaus.nullCount(), check.Equals, nullCnt+128/2)
   123  	for i := 0; i < 128; i++ {
   124  		if i%2 == 0 {
   125  			c.Assert(defCaus.IsNull(len(sel)+i), check.Equals, true)
   126  		} else {
   127  			c.Assert(defCaus.GetInt64(len(sel)+i), check.Equals, int64(i*i*i))
   128  			c.Assert(defCaus.IsNull(len(sel)+i), check.Equals, false)
   129  		}
   130  	}
   131  }
   132  
   133  func (s *testChunkSuite) TestDeferredCausetCopyReconstructVarLen(c *check.C) {
   134  	defCaus := NewDeferredCauset(types.NewFieldType(allegrosql.TypeVarString), 1024)
   135  	results := make([]string, 0, 1024)
   136  	nulls := make([]bool, 0, 1024)
   137  	sel := make([]int, 0, 1024)
   138  	for i := 0; i < 1024; i++ {
   139  		if rand.Intn(10) < 6 {
   140  			sel = append(sel, i)
   141  		}
   142  
   143  		if rand.Intn(10) < 2 {
   144  			defCaus.AppendNull()
   145  			nulls = append(nulls, true)
   146  			results = append(results, "")
   147  			continue
   148  		}
   149  
   150  		v := fmt.Sprintf("%v", rand.Int63())
   151  		defCaus.AppendString(v)
   152  		results = append(results, v)
   153  		nulls = append(nulls, false)
   154  	}
   155  
   156  	defCaus = defCaus.CopyReconstruct(sel, nil)
   157  	nullCnt := 0
   158  	for n, i := range sel {
   159  		if nulls[i] {
   160  			nullCnt++
   161  			c.Assert(defCaus.IsNull(n), check.Equals, true)
   162  		} else {
   163  			c.Assert(defCaus.GetString(n), check.Equals, results[i])
   164  		}
   165  	}
   166  	c.Assert(nullCnt, check.Equals, defCaus.nullCount())
   167  	c.Assert(defCaus.length, check.Equals, len(sel))
   168  
   169  	for i := 0; i < 128; i++ {
   170  		if i%2 == 0 {
   171  			defCaus.AppendNull()
   172  		} else {
   173  			defCaus.AppendString(fmt.Sprintf("%v", i*i*i))
   174  		}
   175  	}
   176  
   177  	c.Assert(defCaus.length, check.Equals, len(sel)+128)
   178  	c.Assert(defCaus.nullCount(), check.Equals, nullCnt+128/2)
   179  	for i := 0; i < 128; i++ {
   180  		if i%2 == 0 {
   181  			c.Assert(defCaus.IsNull(len(sel)+i), check.Equals, true)
   182  		} else {
   183  			c.Assert(defCaus.GetString(len(sel)+i), check.Equals, fmt.Sprintf("%v", i*i*i))
   184  			c.Assert(defCaus.IsNull(len(sel)+i), check.Equals, false)
   185  		}
   186  	}
   187  }
   188  
   189  func (s *testChunkSuite) TestLargeStringDeferredCausetOffset(c *check.C) {
   190  	numRows := 1
   191  	defCaus := newVarLenDeferredCauset(numRows, nil)
   192  	defCaus.offsets[0] = 6 << 30
   193  	c.Check(defCaus.offsets[0], check.Equals, int64(6<<30)) // test no overflow.
   194  }
   195  
   196  func (s *testChunkSuite) TestI64DeferredCauset(c *check.C) {
   197  	chk := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeLonglong)}, 1024)
   198  	defCaus := chk.DeferredCauset(0)
   199  	for i := 0; i < 1024; i++ {
   200  		defCaus.AppendInt64(int64(i))
   201  	}
   202  
   203  	i64s := defCaus.Int64s()
   204  	for i := 0; i < 1024; i++ {
   205  		c.Assert(i64s[i], check.Equals, int64(i))
   206  		i64s[i]++
   207  	}
   208  
   209  	it := NewIterator4Chunk(chk)
   210  	var i int
   211  	for event := it.Begin(); event != it.End(); event = it.Next() {
   212  		c.Assert(event.GetInt64(0), check.Equals, int64(i+1))
   213  		c.Assert(defCaus.GetInt64(i), check.Equals, int64(i+1))
   214  		i++
   215  	}
   216  }
   217  
   218  func (s *testChunkSuite) TestF64DeferredCauset(c *check.C) {
   219  	chk := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeDouble)}, 1024)
   220  	defCaus := chk.DeferredCauset(0)
   221  	for i := 0; i < 1024; i++ {
   222  		defCaus.AppendFloat64(float64(i))
   223  	}
   224  
   225  	f64s := defCaus.Float64s()
   226  	for i := 0; i < 1024; i++ {
   227  		c.Assert(f64s[i], check.Equals, float64(i))
   228  		f64s[i] /= 2
   229  	}
   230  
   231  	it := NewIterator4Chunk(chk)
   232  	var i int64
   233  	for event := it.Begin(); event != it.End(); event = it.Next() {
   234  		c.Assert(event.GetFloat64(0), check.Equals, float64(i)/2)
   235  		c.Assert(defCaus.GetFloat64(int(i)), check.Equals, float64(i)/2)
   236  		i++
   237  	}
   238  }
   239  
   240  func (s *testChunkSuite) TestF32DeferredCauset(c *check.C) {
   241  	chk := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeFloat)}, 1024)
   242  	defCaus := chk.DeferredCauset(0)
   243  	for i := 0; i < 1024; i++ {
   244  		defCaus.AppendFloat32(float32(i))
   245  	}
   246  
   247  	f32s := defCaus.Float32s()
   248  	for i := 0; i < 1024; i++ {
   249  		c.Assert(f32s[i], check.Equals, float32(i))
   250  		f32s[i] /= 2
   251  	}
   252  
   253  	it := NewIterator4Chunk(chk)
   254  	var i int64
   255  	for event := it.Begin(); event != it.End(); event = it.Next() {
   256  		c.Assert(event.GetFloat32(0), check.Equals, float32(i)/2)
   257  		c.Assert(defCaus.GetFloat32(int(i)), check.Equals, float32(i)/2)
   258  		i++
   259  	}
   260  }
   261  
   262  func (s *testChunkSuite) TestDurationSliceDeferredCauset(c *check.C) {
   263  	chk := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeDuration)}, 1024)
   264  	defCaus := chk.DeferredCauset(0)
   265  	for i := 0; i < 1024; i++ {
   266  		defCaus.AppendDuration(types.Duration{Duration: time.Duration(i)})
   267  	}
   268  
   269  	ds := defCaus.GoDurations()
   270  	for i := 0; i < 1024; i++ {
   271  		c.Assert(ds[i], check.Equals, time.Duration(i))
   272  		d := types.Duration{Duration: ds[i]}
   273  		d, _ = d.Add(d)
   274  		ds[i] = d.Duration
   275  	}
   276  
   277  	it := NewIterator4Chunk(chk)
   278  	var i int64
   279  	for event := it.Begin(); event != it.End(); event = it.Next() {
   280  		c.Assert(event.GetDuration(0, 0).Duration, check.Equals, time.Duration(i)*2)
   281  		c.Assert(defCaus.GetDuration(int(i), 0).Duration, check.Equals, time.Duration(i)*2)
   282  		i++
   283  	}
   284  }
   285  
   286  func (s *testChunkSuite) TestMyDecimal(c *check.C) {
   287  	chk := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeNewDecimal)}, 1024)
   288  	defCaus := chk.DeferredCauset(0)
   289  	for i := 0; i < 1024; i++ {
   290  		d := new(types.MyDecimal)
   291  		if err := d.FromFloat64(float64(i) * 1.1); err != nil {
   292  			c.Fatal(err)
   293  		}
   294  		defCaus.AppendMyDecimal(d)
   295  	}
   296  
   297  	ds := defCaus.Decimals()
   298  	for i := 0; i < 1024; i++ {
   299  		d := new(types.MyDecimal)
   300  		if err := d.FromFloat64(float64(i) * 1.1); err != nil {
   301  			c.Fatal(err)
   302  		}
   303  		c.Assert(d.Compare(&ds[i]), check.Equals, 0)
   304  
   305  		if err := types.DecimalAdd(&ds[i], d, &ds[i]); err != nil {
   306  			c.Fatal(err)
   307  		}
   308  	}
   309  
   310  	it := NewIterator4Chunk(chk)
   311  	var i int64
   312  	for event := it.Begin(); event != it.End(); event = it.Next() {
   313  		d := new(types.MyDecimal)
   314  		if err := d.FromFloat64(float64(i) * 1.1 * 2); err != nil {
   315  			c.Fatal(err)
   316  		}
   317  
   318  		delta := new(types.MyDecimal)
   319  		if err := types.DecimalSub(d, event.GetMyDecimal(0), delta); err != nil {
   320  			c.Fatal(err)
   321  		}
   322  
   323  		fDelta, err := delta.ToFloat64()
   324  		if err != nil {
   325  			c.Fatal(err)
   326  		}
   327  		if fDelta > 0.0001 || fDelta < -0.0001 {
   328  			c.Fatal()
   329  		}
   330  
   331  		i++
   332  	}
   333  }
   334  
   335  func (s *testChunkSuite) TestStringDeferredCauset(c *check.C) {
   336  	chk := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeVarString)}, 1024)
   337  	defCaus := chk.DeferredCauset(0)
   338  	for i := 0; i < 1024; i++ {
   339  		defCaus.AppendString(fmt.Sprintf("%v", i*i))
   340  	}
   341  
   342  	it := NewIterator4Chunk(chk)
   343  	var i int
   344  	for event := it.Begin(); event != it.End(); event = it.Next() {
   345  		c.Assert(event.GetString(0), check.Equals, fmt.Sprintf("%v", i*i))
   346  		c.Assert(defCaus.GetString(i), check.Equals, fmt.Sprintf("%v", i*i))
   347  		i++
   348  	}
   349  }
   350  
   351  func (s *testChunkSuite) TestSetDeferredCauset(c *check.C) {
   352  	chk := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeSet)}, 1024)
   353  	defCaus := chk.DeferredCauset(0)
   354  	for i := 0; i < 1024; i++ {
   355  		defCaus.AppendSet(types.Set{Name: fmt.Sprintf("%v", i), Value: uint64(i)})
   356  	}
   357  
   358  	it := NewIterator4Chunk(chk)
   359  	var i int
   360  	for event := it.Begin(); event != it.End(); event = it.Next() {
   361  		s1 := defCaus.GetSet(i)
   362  		s2 := event.GetSet(0)
   363  		c.Assert(s1.Name, check.Equals, s2.Name)
   364  		c.Assert(s1.Value, check.Equals, s2.Value)
   365  		c.Assert(s1.Name, check.Equals, fmt.Sprintf("%v", i))
   366  		c.Assert(s1.Value, check.Equals, uint64(i))
   367  		i++
   368  	}
   369  }
   370  
   371  func (s *testChunkSuite) TestJSONDeferredCauset(c *check.C) {
   372  	chk := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeJSON)}, 1024)
   373  	defCaus := chk.DeferredCauset(0)
   374  	for i := 0; i < 1024; i++ {
   375  		j := new(json.BinaryJSON)
   376  		if err := j.UnmarshalJSON([]byte(fmt.Sprintf(`{"%v":%v}`, i, i))); err != nil {
   377  			c.Fatal(err)
   378  		}
   379  		defCaus.AppendJSON(*j)
   380  	}
   381  
   382  	it := NewIterator4Chunk(chk)
   383  	var i int
   384  	for event := it.Begin(); event != it.End(); event = it.Next() {
   385  		j1 := defCaus.GetJSON(i)
   386  		j2 := event.GetJSON(0)
   387  		c.Assert(j1.String(), check.Equals, j2.String())
   388  		i++
   389  	}
   390  }
   391  
   392  func (s *testChunkSuite) TestTimeDeferredCauset(c *check.C) {
   393  	chk := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeDatetime)}, 1024)
   394  	defCaus := chk.DeferredCauset(0)
   395  	for i := 0; i < 1024; i++ {
   396  		defCaus.AppendTime(types.CurrentTime(allegrosql.TypeDatetime))
   397  		time.Sleep(time.Millisecond / 10)
   398  	}
   399  
   400  	it := NewIterator4Chunk(chk)
   401  	ts := defCaus.Times()
   402  	var i int
   403  	for event := it.Begin(); event != it.End(); event = it.Next() {
   404  		j1 := defCaus.GetTime(i)
   405  		j2 := event.GetTime(0)
   406  		j3 := ts[i]
   407  		c.Assert(j1.Compare(j2), check.Equals, 0)
   408  		c.Assert(j1.Compare(j3), check.Equals, 0)
   409  		i++
   410  	}
   411  }
   412  
   413  func (s *testChunkSuite) TestDurationDeferredCauset(c *check.C) {
   414  	chk := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeDuration)}, 1024)
   415  	defCaus := chk.DeferredCauset(0)
   416  	for i := 0; i < 1024; i++ {
   417  		defCaus.AppendDuration(types.Duration{Duration: time.Second * time.Duration(i)})
   418  	}
   419  
   420  	it := NewIterator4Chunk(chk)
   421  	var i int
   422  	for event := it.Begin(); event != it.End(); event = it.Next() {
   423  		j1 := defCaus.GetDuration(i, 0)
   424  		j2 := event.GetDuration(0, 0)
   425  		c.Assert(j1.Compare(j2), check.Equals, 0)
   426  		i++
   427  	}
   428  }
   429  
   430  func (s *testChunkSuite) TestEnumDeferredCauset(c *check.C) {
   431  	chk := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeEnum)}, 1024)
   432  	defCaus := chk.DeferredCauset(0)
   433  	for i := 0; i < 1024; i++ {
   434  		defCaus.AppendEnum(types.Enum{Name: fmt.Sprintf("%v", i), Value: uint64(i)})
   435  	}
   436  
   437  	it := NewIterator4Chunk(chk)
   438  	var i int
   439  	for event := it.Begin(); event != it.End(); event = it.Next() {
   440  		s1 := defCaus.GetEnum(i)
   441  		s2 := event.GetEnum(0)
   442  		c.Assert(s1.Name, check.Equals, s2.Name)
   443  		c.Assert(s1.Value, check.Equals, s2.Value)
   444  		c.Assert(s1.Name, check.Equals, fmt.Sprintf("%v", i))
   445  		c.Assert(s1.Value, check.Equals, uint64(i))
   446  		i++
   447  	}
   448  }
   449  
   450  func (s *testChunkSuite) TestNullsDeferredCauset(c *check.C) {
   451  	chk := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeLonglong)}, 1024)
   452  	defCaus := chk.DeferredCauset(0)
   453  	for i := 0; i < 1024; i++ {
   454  		if i%2 == 0 {
   455  			defCaus.AppendNull()
   456  			continue
   457  		}
   458  		defCaus.AppendInt64(int64(i))
   459  	}
   460  
   461  	it := NewIterator4Chunk(chk)
   462  	var i int
   463  	for event := it.Begin(); event != it.End(); event = it.Next() {
   464  		if i%2 == 0 {
   465  			c.Assert(event.IsNull(0), check.Equals, true)
   466  			c.Assert(defCaus.IsNull(i), check.Equals, true)
   467  		} else {
   468  			c.Assert(event.GetInt64(0), check.Equals, int64(i))
   469  		}
   470  		i++
   471  	}
   472  }
   473  
   474  func (s *testChunkSuite) TestReconstructFixedLen(c *check.C) {
   475  	defCaus := NewDeferredCauset(types.NewFieldType(allegrosql.TypeLonglong), 1024)
   476  	results := make([]int64, 0, 1024)
   477  	nulls := make([]bool, 0, 1024)
   478  	sel := make([]int, 0, 1024)
   479  	for i := 0; i < 1024; i++ {
   480  		if rand.Intn(10) < 6 {
   481  			sel = append(sel, i)
   482  		}
   483  
   484  		if rand.Intn(10) < 2 {
   485  			defCaus.AppendNull()
   486  			nulls = append(nulls, true)
   487  			results = append(results, 0)
   488  			continue
   489  		}
   490  
   491  		v := rand.Int63()
   492  		defCaus.AppendInt64(v)
   493  		results = append(results, v)
   494  		nulls = append(nulls, false)
   495  	}
   496  
   497  	defCaus.reconstruct(sel)
   498  	nullCnt := 0
   499  	for n, i := range sel {
   500  		if nulls[i] {
   501  			nullCnt++
   502  			c.Assert(defCaus.IsNull(n), check.Equals, true)
   503  		} else {
   504  			c.Assert(defCaus.GetInt64(n), check.Equals, results[i])
   505  		}
   506  	}
   507  	c.Assert(nullCnt, check.Equals, defCaus.nullCount())
   508  	c.Assert(defCaus.length, check.Equals, len(sel))
   509  
   510  	for i := 0; i < 128; i++ {
   511  		if i%2 == 0 {
   512  			defCaus.AppendNull()
   513  		} else {
   514  			defCaus.AppendInt64(int64(i * i * i))
   515  		}
   516  	}
   517  
   518  	c.Assert(defCaus.length, check.Equals, len(sel)+128)
   519  	c.Assert(defCaus.nullCount(), check.Equals, nullCnt+128/2)
   520  	for i := 0; i < 128; i++ {
   521  		if i%2 == 0 {
   522  			c.Assert(defCaus.IsNull(len(sel)+i), check.Equals, true)
   523  		} else {
   524  			c.Assert(defCaus.GetInt64(len(sel)+i), check.Equals, int64(i*i*i))
   525  			c.Assert(defCaus.IsNull(len(sel)+i), check.Equals, false)
   526  		}
   527  	}
   528  }
   529  
   530  func (s *testChunkSuite) TestReconstructVarLen(c *check.C) {
   531  	defCaus := NewDeferredCauset(types.NewFieldType(allegrosql.TypeVarString), 1024)
   532  	results := make([]string, 0, 1024)
   533  	nulls := make([]bool, 0, 1024)
   534  	sel := make([]int, 0, 1024)
   535  	for i := 0; i < 1024; i++ {
   536  		if rand.Intn(10) < 6 {
   537  			sel = append(sel, i)
   538  		}
   539  
   540  		if rand.Intn(10) < 2 {
   541  			defCaus.AppendNull()
   542  			nulls = append(nulls, true)
   543  			results = append(results, "")
   544  			continue
   545  		}
   546  
   547  		v := fmt.Sprintf("%v", rand.Int63())
   548  		defCaus.AppendString(v)
   549  		results = append(results, v)
   550  		nulls = append(nulls, false)
   551  	}
   552  
   553  	defCaus.reconstruct(sel)
   554  	nullCnt := 0
   555  	for n, i := range sel {
   556  		if nulls[i] {
   557  			nullCnt++
   558  			c.Assert(defCaus.IsNull(n), check.Equals, true)
   559  		} else {
   560  			c.Assert(defCaus.GetString(n), check.Equals, results[i])
   561  		}
   562  	}
   563  	c.Assert(nullCnt, check.Equals, defCaus.nullCount())
   564  	c.Assert(defCaus.length, check.Equals, len(sel))
   565  
   566  	for i := 0; i < 128; i++ {
   567  		if i%2 == 0 {
   568  			defCaus.AppendNull()
   569  		} else {
   570  			defCaus.AppendString(fmt.Sprintf("%v", i*i*i))
   571  		}
   572  	}
   573  
   574  	c.Assert(defCaus.length, check.Equals, len(sel)+128)
   575  	c.Assert(defCaus.nullCount(), check.Equals, nullCnt+128/2)
   576  	for i := 0; i < 128; i++ {
   577  		if i%2 == 0 {
   578  			c.Assert(defCaus.IsNull(len(sel)+i), check.Equals, true)
   579  		} else {
   580  			c.Assert(defCaus.GetString(len(sel)+i), check.Equals, fmt.Sprintf("%v", i*i*i))
   581  			c.Assert(defCaus.IsNull(len(sel)+i), check.Equals, false)
   582  		}
   583  	}
   584  }
   585  
   586  func (s *testChunkSuite) TestPreAllocInt64(c *check.C) {
   587  	defCaus := NewDeferredCauset(types.NewFieldType(allegrosql.TypeLonglong), 128)
   588  	defCaus.ResizeInt64(256, true)
   589  	i64s := defCaus.Int64s()
   590  	c.Assert(len(i64s), check.Equals, 256)
   591  	for i := 0; i < 256; i++ {
   592  		c.Assert(defCaus.IsNull(i), check.Equals, true)
   593  	}
   594  	defCaus.AppendInt64(2333)
   595  	c.Assert(defCaus.IsNull(256), check.Equals, false)
   596  	c.Assert(len(defCaus.Int64s()), check.Equals, 257)
   597  	c.Assert(defCaus.Int64s()[256], check.Equals, int64(2333))
   598  }
   599  
   600  func (s *testChunkSuite) TestPreAllocUint64(c *check.C) {
   601  	tll := types.NewFieldType(allegrosql.TypeLonglong)
   602  	tll.Flag |= allegrosql.UnsignedFlag
   603  	defCaus := NewDeferredCauset(tll, 128)
   604  	defCaus.ResizeUint64(256, true)
   605  	u64s := defCaus.Uint64s()
   606  	c.Assert(len(u64s), check.Equals, 256)
   607  	for i := 0; i < 256; i++ {
   608  		c.Assert(defCaus.IsNull(i), check.Equals, true)
   609  	}
   610  	defCaus.AppendUint64(2333)
   611  	c.Assert(defCaus.IsNull(256), check.Equals, false)
   612  	c.Assert(len(defCaus.Uint64s()), check.Equals, 257)
   613  	c.Assert(defCaus.Uint64s()[256], check.Equals, uint64(2333))
   614  }
   615  
   616  func (s *testChunkSuite) TestPreAllocFloat32(c *check.C) {
   617  	defCaus := newFixedLenDeferredCauset(sizeFloat32, 128)
   618  	defCaus.ResizeFloat32(256, true)
   619  	f32s := defCaus.Float32s()
   620  	c.Assert(len(f32s), check.Equals, 256)
   621  	for i := 0; i < 256; i++ {
   622  		c.Assert(defCaus.IsNull(i), check.Equals, true)
   623  	}
   624  	defCaus.AppendFloat32(2333)
   625  	c.Assert(defCaus.IsNull(256), check.Equals, false)
   626  	c.Assert(len(defCaus.Float32s()), check.Equals, 257)
   627  	c.Assert(defCaus.Float32s()[256], check.Equals, float32(2333))
   628  }
   629  
   630  func (s *testChunkSuite) TestPreAllocFloat64(c *check.C) {
   631  	defCaus := newFixedLenDeferredCauset(sizeFloat64, 128)
   632  	defCaus.ResizeFloat64(256, true)
   633  	f64s := defCaus.Float64s()
   634  	c.Assert(len(f64s), check.Equals, 256)
   635  	for i := 0; i < 256; i++ {
   636  		c.Assert(defCaus.IsNull(i), check.Equals, true)
   637  	}
   638  	defCaus.AppendFloat64(2333)
   639  	c.Assert(defCaus.IsNull(256), check.Equals, false)
   640  	c.Assert(len(defCaus.Float64s()), check.Equals, 257)
   641  	c.Assert(defCaus.Float64s()[256], check.Equals, float64(2333))
   642  }
   643  
   644  func (s *testChunkSuite) TestPreAllocDecimal(c *check.C) {
   645  	defCaus := newFixedLenDeferredCauset(sizeMyDecimal, 128)
   646  	defCaus.ResizeDecimal(256, true)
   647  	ds := defCaus.Decimals()
   648  	c.Assert(len(ds), check.Equals, 256)
   649  	for i := 0; i < 256; i++ {
   650  		c.Assert(defCaus.IsNull(i), check.Equals, true)
   651  	}
   652  	defCaus.AppendMyDecimal(new(types.MyDecimal))
   653  	c.Assert(defCaus.IsNull(256), check.Equals, false)
   654  	c.Assert(len(defCaus.Float64s()), check.Equals, 257)
   655  }
   656  
   657  func (s *testChunkSuite) TestPreAllocTime(c *check.C) {
   658  	defCaus := newFixedLenDeferredCauset(sizeTime, 128)
   659  	defCaus.ResizeTime(256, true)
   660  	ds := defCaus.Times()
   661  	c.Assert(len(ds), check.Equals, 256)
   662  	for i := 0; i < 256; i++ {
   663  		c.Assert(defCaus.IsNull(i), check.Equals, true)
   664  	}
   665  	defCaus.AppendTime(types.ZeroDatetime)
   666  	c.Assert(defCaus.IsNull(256), check.Equals, false)
   667  	c.Assert(len(defCaus.Times()), check.Equals, 257)
   668  }
   669  
   670  func (s *testChunkSuite) TestNull(c *check.C) {
   671  	defCaus := newFixedLenDeferredCauset(sizeFloat64, 32)
   672  	defCaus.ResizeFloat64(1024, true)
   673  	c.Assert(defCaus.nullCount(), check.Equals, 1024)
   674  
   675  	notNulls := make(map[int]struct{})
   676  	for i := 0; i < 512; i++ {
   677  		idx := rand.Intn(1024)
   678  		notNulls[idx] = struct{}{}
   679  		defCaus.SetNull(idx, false)
   680  	}
   681  
   682  	c.Assert(defCaus.nullCount(), check.Equals, 1024-len(notNulls))
   683  	for idx := range notNulls {
   684  		c.Assert(defCaus.IsNull(idx), check.Equals, false)
   685  	}
   686  
   687  	defCaus.ResizeFloat64(8, true)
   688  	defCaus.SetNulls(0, 8, true)
   689  	defCaus.SetNull(7, false)
   690  	c.Assert(defCaus.nullCount(), check.Equals, 7)
   691  
   692  	defCaus.ResizeFloat64(8, true)
   693  	defCaus.SetNulls(0, 8, true)
   694  	c.Assert(defCaus.nullCount(), check.Equals, 8)
   695  
   696  	defCaus.ResizeFloat64(9, true)
   697  	defCaus.SetNulls(0, 9, true)
   698  	defCaus.SetNull(8, false)
   699  	c.Assert(defCaus.nullCount(), check.Equals, 8)
   700  }
   701  
   702  func (s *testChunkSuite) TestSetNulls(c *check.C) {
   703  	defCaus := newFixedLenDeferredCauset(sizeFloat64, 32)
   704  	defCaus.ResizeFloat64(1024, true)
   705  	c.Assert(defCaus.nullCount(), check.Equals, 1024)
   706  
   707  	defCaus.SetNulls(0, 1024, false)
   708  	c.Assert(defCaus.nullCount(), check.Equals, 0)
   709  
   710  	nullMap := make(map[int]struct{})
   711  	for i := 0; i < 100; i++ {
   712  		begin := rand.Intn(1024)
   713  		l := rand.Intn(37)
   714  		end := begin + l
   715  		if end > 1024 {
   716  			end = 1024
   717  		}
   718  		for i := begin; i < end; i++ {
   719  			nullMap[i] = struct{}{}
   720  		}
   721  		defCaus.SetNulls(begin, end, true)
   722  
   723  		c.Assert(defCaus.nullCount(), check.Equals, len(nullMap))
   724  		for k := range nullMap {
   725  			c.Assert(defCaus.IsNull(k), check.Equals, true)
   726  		}
   727  	}
   728  }
   729  
   730  func (s *testChunkSuite) TestResizeReserve(c *check.C) {
   731  	cI64s := newFixedLenDeferredCauset(sizeInt64, 0)
   732  	c.Assert(cI64s.length, check.Equals, 0)
   733  	for i := 0; i < 100; i++ {
   734  		t := rand.Intn(1024)
   735  		cI64s.ResizeInt64(t, true)
   736  		c.Assert(cI64s.length, check.Equals, t)
   737  		c.Assert(len(cI64s.Int64s()), check.Equals, t)
   738  	}
   739  	cI64s.ResizeInt64(0, true)
   740  	c.Assert(cI64s.length, check.Equals, 0)
   741  	c.Assert(len(cI64s.Int64s()), check.Equals, 0)
   742  
   743  	cStrs := newVarLenDeferredCauset(0, nil)
   744  	for i := 0; i < 100; i++ {
   745  		t := rand.Intn(1024)
   746  		cStrs.ReserveString(t)
   747  		c.Assert(cStrs.length, check.Equals, 0)
   748  	}
   749  	cStrs.ReserveString(0)
   750  	c.Assert(cStrs.length, check.Equals, 0)
   751  }
   752  
   753  func (s *testChunkSuite) TestGetRaw(c *check.C) {
   754  	chk := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeFloat)}, 1024)
   755  	defCaus := chk.DeferredCauset(0)
   756  	for i := 0; i < 1024; i++ {
   757  		defCaus.AppendFloat32(float32(i))
   758  	}
   759  	it := NewIterator4Chunk(chk)
   760  	var i int
   761  	for event := it.Begin(); event != it.End(); event = it.Next() {
   762  		f := float32(i)
   763  		b := (*[unsafe.Sizeof(f)]byte)(unsafe.Pointer(&f))[:]
   764  		c.Assert(event.GetRaw(0), check.DeepEquals, b)
   765  		c.Assert(defCaus.GetRaw(i), check.DeepEquals, b)
   766  		i++
   767  	}
   768  
   769  	chk = NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeVarString)}, 1024)
   770  	defCaus = chk.DeferredCauset(0)
   771  	for i := 0; i < 1024; i++ {
   772  		defCaus.AppendString(fmt.Sprint(i))
   773  	}
   774  	it = NewIterator4Chunk(chk)
   775  	i = 0
   776  	for event := it.Begin(); event != it.End(); event = it.Next() {
   777  		c.Assert(event.GetRaw(0), check.DeepEquals, []byte(fmt.Sprint(i)))
   778  		c.Assert(defCaus.GetRaw(i), check.DeepEquals, []byte(fmt.Sprint(i)))
   779  		i++
   780  	}
   781  }
   782  
   783  func (s *testChunkSuite) TestResize(c *check.C) {
   784  	defCaus := NewDeferredCauset(types.NewFieldType(allegrosql.TypeLonglong), 1024)
   785  	for i := 0; i < 1024; i++ {
   786  		defCaus.AppendInt64(int64(i))
   787  	}
   788  	defCaus.ResizeInt64(1024, false)
   789  	for i := 0; i < 1024; i++ {
   790  		c.Assert(defCaus.Int64s()[i], check.Equals, int64(0))
   791  	}
   792  
   793  	defCaus = NewDeferredCauset(types.NewFieldType(allegrosql.TypeFloat), 1024)
   794  	for i := 0; i < 1024; i++ {
   795  		defCaus.AppendFloat32(float32(i))
   796  	}
   797  	defCaus.ResizeFloat32(1024, false)
   798  	for i := 0; i < 1024; i++ {
   799  		c.Assert(defCaus.Float32s()[i], check.Equals, float32(0))
   800  	}
   801  
   802  	defCaus = NewDeferredCauset(types.NewFieldType(allegrosql.TypeDouble), 1024)
   803  	for i := 0; i < 1024; i++ {
   804  		defCaus.AppendFloat64(float64(i))
   805  	}
   806  	defCaus.ResizeFloat64(1024, false)
   807  	for i := 0; i < 1024; i++ {
   808  		c.Assert(defCaus.Float64s()[i], check.Equals, float64(0))
   809  	}
   810  
   811  	defCaus = NewDeferredCauset(types.NewFieldType(allegrosql.TypeNewDecimal), 1024)
   812  	for i := 0; i < 1024; i++ {
   813  		defCaus.AppendMyDecimal(new(types.MyDecimal).FromInt(int64(i)))
   814  	}
   815  	defCaus.ResizeDecimal(1024, false)
   816  	for i := 0; i < 1024; i++ {
   817  		var d types.MyDecimal
   818  		c.Assert(defCaus.Decimals()[i], check.Equals, d)
   819  	}
   820  
   821  	defCaus = NewDeferredCauset(types.NewFieldType(allegrosql.TypeDuration), 1024)
   822  	for i := 0; i < 1024; i++ {
   823  		defCaus.AppendDuration(types.Duration{Duration: time.Duration(i), Fsp: int8(i)})
   824  	}
   825  	defCaus.ResizeGoDuration(1024, false)
   826  	for i := 0; i < 1024; i++ {
   827  		c.Assert(defCaus.GoDurations()[i], check.Equals, time.Duration(0))
   828  	}
   829  
   830  	defCaus = NewDeferredCauset(types.NewFieldType(allegrosql.TypeDatetime), 1024)
   831  	for i := 0; i < 1024; i++ {
   832  		gt := types.FromDate(rand.Intn(2200), rand.Intn(10)+1, rand.Intn(20)+1, rand.Intn(12), rand.Intn(60), rand.Intn(60), rand.Intn(1000000))
   833  		t := types.NewTime(gt, 0, 0)
   834  		defCaus.AppendTime(t)
   835  	}
   836  	defCaus.ResizeTime(1024, false)
   837  	for i := 0; i < 1024; i++ {
   838  		var t types.Time
   839  		c.Assert(defCaus.Times()[i], check.Equals, t)
   840  	}
   841  }
   842  
   843  func BenchmarkDurationRow(b *testing.B) {
   844  	chk1 := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeDuration)}, 1024)
   845  	defCaus1 := chk1.DeferredCauset(0)
   846  	for i := 0; i < 1024; i++ {
   847  		defCaus1.AppendDuration(types.Duration{Duration: time.Second * time.Duration(i)})
   848  	}
   849  	chk2 := chk1.CopyConstruct()
   850  	result := chk1.CopyConstruct()
   851  
   852  	b.ResetTimer()
   853  	for k := 0; k < b.N; k++ {
   854  		result.Reset()
   855  		it1 := NewIterator4Chunk(chk1)
   856  		it2 := NewIterator4Chunk(chk2)
   857  		for r1, r2 := it1.Begin(), it2.Begin(); r1 != it1.End() && r2 != it2.End(); r1, r2 = it1.Next(), it2.Next() {
   858  			d1 := r1.GetDuration(0, 0)
   859  			d2 := r2.GetDuration(0, 0)
   860  			r, err := d1.Add(d2)
   861  			if err != nil {
   862  				b.Fatal(err)
   863  			}
   864  			result.AppendDuration(0, r)
   865  		}
   866  	}
   867  }
   868  
   869  func BenchmarkDurationVec(b *testing.B) {
   870  	chk := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeDuration)}, 1024)
   871  	defCaus1 := chk.DeferredCauset(0)
   872  	for i := 0; i < 1024; i++ {
   873  		defCaus1.AppendDuration(types.Duration{Duration: time.Second * time.Duration(i)})
   874  	}
   875  	defCaus2 := defCaus1.CopyConstruct(nil)
   876  	result := defCaus1.CopyConstruct(nil)
   877  
   878  	ds1 := defCaus1.GoDurations()
   879  	ds2 := defCaus2.GoDurations()
   880  	rs := result.GoDurations()
   881  
   882  	b.ResetTimer()
   883  	for k := 0; k < b.N; k++ {
   884  		result.ResizeGoDuration(1024, true)
   885  		for i := 0; i < 1024; i++ {
   886  			d1 := types.Duration{Duration: ds1[i]}
   887  			d2 := types.Duration{Duration: ds2[i]}
   888  			r, err := d1.Add(d2)
   889  			if err != nil {
   890  				b.Fatal(err)
   891  			}
   892  			rs[i] = r.Duration
   893  		}
   894  	}
   895  }
   896  
   897  func BenchmarkTimeRow(b *testing.B) {
   898  	chk1 := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeDate)}, 1024)
   899  	defCaus1 := chk1.DeferredCauset(0)
   900  	for i := 0; i < 1024; i++ {
   901  		defCaus1.AppendTime(types.ZeroDate)
   902  	}
   903  	chk2 := chk1.CopyConstruct()
   904  	result := chk1.CopyConstruct()
   905  
   906  	b.ResetTimer()
   907  	for k := 0; k < b.N; k++ {
   908  		result.Reset()
   909  		it1 := NewIterator4Chunk(chk1)
   910  		it2 := NewIterator4Chunk(chk2)
   911  		for r1, r2 := it1.Begin(), it2.Begin(); r1 != it1.End() && r2 != it2.End(); r1, r2 = it1.Next(), it2.Next() {
   912  			d1 := r1.GetTime(0)
   913  			d2 := r2.GetTime(0)
   914  			if r := d1.Compare(d2); r > 0 {
   915  				result.AppendTime(0, d1)
   916  			} else {
   917  				result.AppendTime(0, d2)
   918  			}
   919  		}
   920  	}
   921  }
   922  
   923  func BenchmarkTimeVec(b *testing.B) {
   924  	chk := NewChunkWithCapacity([]*types.FieldType{types.NewFieldType(allegrosql.TypeDate)}, 1024)
   925  	defCaus1 := chk.DeferredCauset(0)
   926  	for i := 0; i < 1024; i++ {
   927  		defCaus1.AppendTime(types.ZeroDate)
   928  	}
   929  	defCaus2 := defCaus1.CopyConstruct(nil)
   930  	result := defCaus1.CopyConstruct(nil)
   931  
   932  	ds1 := defCaus1.Times()
   933  	ds2 := defCaus2.Times()
   934  	rs := result.Times()
   935  
   936  	b.ResetTimer()
   937  	for k := 0; k < b.N; k++ {
   938  		result.ResizeTime(1024, true)
   939  		for i := 0; i < 1024; i++ {
   940  			if r := ds1[i].Compare(ds2[i]); r > 0 {
   941  				rs[i] = ds1[i]
   942  			} else {
   943  				rs[i] = ds2[i]
   944  			}
   945  		}
   946  	}
   947  }
   948  
   949  func genNullDefCauss(n int) []*DeferredCauset {
   950  	defcaus := make([]*DeferredCauset, n)
   951  	for i := range defcaus {
   952  		defcaus[i] = NewDeferredCauset(types.NewFieldType(allegrosql.TypeLonglong), 1024)
   953  		defcaus[i].ResizeInt64(1024, false)
   954  		for j := 0; j < 1024; j++ {
   955  			if rand.Intn(10) < 5 {
   956  				defcaus[i].SetNull(j, true)
   957  			}
   958  		}
   959  	}
   960  	return defcaus
   961  }
   962  
   963  func (s *testChunkSuite) TestVectorizedNulls(c *check.C) {
   964  	for i := 0; i < 256; i++ {
   965  		defcaus := genNullDefCauss(4)
   966  		lDefCaus, rDefCaus := defcaus[0], defcaus[1]
   967  		vecResult, rowResult := defcaus[2], defcaus[3]
   968  		vecResult.SetNulls(0, 1024, false)
   969  		rowResult.SetNulls(0, 1024, false)
   970  		vecResult.MergeNulls(lDefCaus, rDefCaus)
   971  		for i := 0; i < 1024; i++ {
   972  			rowResult.SetNull(i, lDefCaus.IsNull(i) || rDefCaus.IsNull(i))
   973  		}
   974  
   975  		for i := 0; i < 1024; i++ {
   976  			c.Assert(rowResult.IsNull(i), check.Equals, vecResult.IsNull(i))
   977  		}
   978  	}
   979  }
   980  
   981  func (s *testChunkSuite) TestResetDeferredCauset(c *check.C) {
   982  	defCaus0 := NewDeferredCauset(types.NewFieldType(allegrosql.TypeVarString), 0)
   983  	defCaus1 := NewDeferredCauset(types.NewFieldType(allegrosql.TypeLonglong), 0)
   984  
   985  	// using defCaus0.reset() here will cause panic since it doesn't reset the elemBuf field which
   986  	// is used by MergeNulls.
   987  	defCaus0.Reset(types.ETInt)
   988  	defCaus0.MergeNulls(defCaus1)
   989  
   990  	defCaus := NewDeferredCauset(types.NewFieldType(allegrosql.TypeDatetime), 0)
   991  	defCaus.Reset(types.ETDuration)
   992  	defCaus.AppendDuration(types.Duration{})
   993  	// using defCaus.reset() above will let this assertion fail since the length of initialized elemBuf
   994  	// is sizeTime.
   995  	c.Assert(len(defCaus.data), check.Equals, sizeGoDuration)
   996  }
   997  
   998  func BenchmarkMergeNullsVectorized(b *testing.B) {
   999  	defcaus := genNullDefCauss(3)
  1000  	b.ResetTimer()
  1001  	for i := 0; i < b.N; i++ {
  1002  		defcaus[0].MergeNulls(defcaus[1:]...)
  1003  	}
  1004  }
  1005  
  1006  func BenchmarkMergeNullsNonVectorized(b *testing.B) {
  1007  	defcaus := genNullDefCauss(3)
  1008  	b.ResetTimer()
  1009  	for i := 0; i < b.N; i++ {
  1010  		for i := 0; i < 1024; i++ {
  1011  			defcaus[0].SetNull(i, defcaus[1].IsNull(i) || defcaus[2].IsNull(i))
  1012  		}
  1013  	}
  1014  }