github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/codec/codec_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 codec
    15  
    16  import (
    17  	"bytes"
    18  	"hash"
    19  	"hash/crc32"
    20  	"hash/fnv"
    21  	"math"
    22  	"testing"
    23  	"time"
    24  
    25  	. "github.com/whtcorpsinc/check"
    26  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    27  	"github.com/whtcorpsinc/BerolinaSQL/terror"
    28  	"github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx"
    29  	"github.com/whtcorpsinc/milevadb/types"
    30  	"github.com/whtcorpsinc/milevadb/types/json"
    31  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    32  	"github.com/whtcorpsinc/milevadb/soliton/testleak"
    33  )
    34  
    35  func TestT(t *testing.T) {
    36  	CustomVerboseFlag = true
    37  	TestingT(t)
    38  }
    39  
    40  var _ = Suite(&testCodecSuite{})
    41  
    42  type testCodecSuite struct {
    43  }
    44  
    45  func (s *testCodecSuite) TestCodecKey(c *C) {
    46  	defer testleak.AfterTest(c)()
    47  	causet := []struct {
    48  		Input  []types.Causet
    49  		Expect []types.Causet
    50  	}{
    51  		{
    52  			types.MakeCausets(int64(1)),
    53  			types.MakeCausets(int64(1)),
    54  		},
    55  
    56  		{
    57  			types.MakeCausets(float32(1), float64(3.15), []byte("123"), "123"),
    58  			types.MakeCausets(float64(1), float64(3.15), []byte("123"), []byte("123")),
    59  		},
    60  		{
    61  			types.MakeCausets(uint64(1), float64(3.15), []byte("123"), int64(-1)),
    62  			types.MakeCausets(uint64(1), float64(3.15), []byte("123"), int64(-1)),
    63  		},
    64  
    65  		{
    66  			types.MakeCausets(true, false),
    67  			types.MakeCausets(int64(1), int64(0)),
    68  		},
    69  
    70  		{
    71  			types.MakeCausets(nil),
    72  			types.MakeCausets(nil),
    73  		},
    74  
    75  		{
    76  			types.MakeCausets(types.NewBinaryLiteralFromUint(100, -1), types.NewBinaryLiteralFromUint(100, 4)),
    77  			types.MakeCausets(uint64(100), uint64(100)),
    78  		},
    79  
    80  		{
    81  			types.MakeCausets(types.Enum{Name: "a", Value: 1}, types.Set{Name: "a", Value: 1}),
    82  			types.MakeCausets(uint64(1), uint64(1)),
    83  		},
    84  	}
    85  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
    86  	for i, t := range causet {
    87  		comment := Commentf("%d %v", i, t)
    88  		b, err := EncodeKey(sc, nil, t.Input...)
    89  		c.Assert(err, IsNil, comment)
    90  		args, err := Decode(b, 1)
    91  		c.Assert(err, IsNil)
    92  		c.Assert(args, DeepEquals, t.Expect)
    93  
    94  		b, err = EncodeValue(sc, nil, t.Input...)
    95  		c.Assert(err, IsNil)
    96  		size, err := estimateValuesSize(sc, t.Input)
    97  		c.Assert(err, IsNil)
    98  		c.Assert(len(b), Equals, size)
    99  		args, err = Decode(b, 1)
   100  		c.Assert(err, IsNil)
   101  		c.Assert(args, DeepEquals, t.Expect)
   102  	}
   103  	var raw types.Causet
   104  	raw.SetRaw([]byte("raw"))
   105  	_, err := EncodeKey(sc, nil, raw)
   106  	c.Assert(err, NotNil)
   107  }
   108  
   109  func estimateValuesSize(sc *stmtctx.StatementContext, vals []types.Causet) (int, error) {
   110  	size := 0
   111  	for _, val := range vals {
   112  		length, err := EstimateValueSize(sc, val)
   113  		if err != nil {
   114  			return 0, err
   115  		}
   116  		size += length
   117  	}
   118  	return size, nil
   119  }
   120  
   121  func (s *testCodecSuite) TestCodecKeyCompare(c *C) {
   122  	defer testleak.AfterTest(c)()
   123  	causet := []struct {
   124  		Left   []types.Causet
   125  		Right  []types.Causet
   126  		Expect int
   127  	}{
   128  		{
   129  			types.MakeCausets(1),
   130  			types.MakeCausets(1),
   131  			0,
   132  		},
   133  		{
   134  			types.MakeCausets(-1),
   135  			types.MakeCausets(1),
   136  			-1,
   137  		},
   138  		{
   139  			types.MakeCausets(3.15),
   140  			types.MakeCausets(3.12),
   141  			1,
   142  		},
   143  		{
   144  			types.MakeCausets("abc"),
   145  			types.MakeCausets("abcd"),
   146  			-1,
   147  		},
   148  		{
   149  			types.MakeCausets("abcdefgh"),
   150  			types.MakeCausets("abcdefghi"),
   151  			-1,
   152  		},
   153  		{
   154  			types.MakeCausets(1, "abc"),
   155  			types.MakeCausets(1, "abcd"),
   156  			-1,
   157  		},
   158  		{
   159  			types.MakeCausets(1, "abc", "def"),
   160  			types.MakeCausets(1, "abcd", "af"),
   161  			-1,
   162  		},
   163  		{
   164  			types.MakeCausets(3.12, "ebc", "def"),
   165  			types.MakeCausets(2.12, "abcd", "af"),
   166  			1,
   167  		},
   168  		{
   169  			types.MakeCausets([]byte{0x01, 0x00}, []byte{0xFF}),
   170  			types.MakeCausets([]byte{0x01, 0x00, 0xFF}),
   171  			-1,
   172  		},
   173  		{
   174  			types.MakeCausets([]byte{0x01}, uint64(0xFFFFFFFFFFFFFFF)),
   175  			types.MakeCausets([]byte{0x01, 0x10}, 0),
   176  			-1,
   177  		},
   178  		{
   179  			types.MakeCausets(0),
   180  			types.MakeCausets(nil),
   181  			1,
   182  		},
   183  		{
   184  			types.MakeCausets([]byte{0x00}),
   185  			types.MakeCausets(nil),
   186  			1,
   187  		},
   188  		{
   189  			types.MakeCausets(math.SmallestNonzeroFloat64),
   190  			types.MakeCausets(nil),
   191  			1,
   192  		},
   193  		{
   194  			types.MakeCausets(int64(math.MinInt64)),
   195  			types.MakeCausets(nil),
   196  			1,
   197  		},
   198  		{
   199  			types.MakeCausets(1, int64(math.MinInt64), nil),
   200  			types.MakeCausets(1, nil, uint64(math.MaxUint64)),
   201  			1,
   202  		},
   203  		{
   204  			types.MakeCausets(1, []byte{}, nil),
   205  			types.MakeCausets(1, nil, 123),
   206  			1,
   207  		},
   208  		{
   209  			types.MakeCausets(parseTime(c, "2011-11-11 00:00:00"), 1),
   210  			types.MakeCausets(parseTime(c, "2011-11-11 00:00:00"), 0),
   211  			1,
   212  		},
   213  		{
   214  			types.MakeCausets(parseDuration(c, "00:00:00"), 1),
   215  			types.MakeCausets(parseDuration(c, "00:00:01"), 0),
   216  			-1,
   217  		},
   218  		{
   219  			[]types.Causet{types.MinNotNullCauset()},
   220  			[]types.Causet{types.MaxValueCauset()},
   221  			-1,
   222  		},
   223  	}
   224  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
   225  	for _, t := range causet {
   226  		b1, err := EncodeKey(sc, nil, t.Left...)
   227  		c.Assert(err, IsNil)
   228  
   229  		b2, err := EncodeKey(sc, nil, t.Right...)
   230  		c.Assert(err, IsNil)
   231  
   232  		c.Assert(bytes.Compare(b1, b2), Equals, t.Expect, Commentf("%v - %v - %v - %v - %v", t.Left, t.Right, b1, b2, t.Expect))
   233  	}
   234  }
   235  
   236  func (s *testCodecSuite) TestNumberCodec(c *C) {
   237  	defer testleak.AfterTest(c)()
   238  	tblInt64 := []int64{
   239  		math.MinInt64,
   240  		math.MinInt32,
   241  		math.MinInt16,
   242  		math.MinInt8,
   243  		0,
   244  		math.MaxInt8,
   245  		math.MaxInt16,
   246  		math.MaxInt32,
   247  		math.MaxInt64,
   248  		1<<47 - 1,
   249  		-1 << 47,
   250  		1<<23 - 1,
   251  		-1 << 23,
   252  		1<<33 - 1,
   253  		-1 << 33,
   254  		1<<55 - 1,
   255  		-1 << 55,
   256  		1,
   257  		-1,
   258  	}
   259  
   260  	for _, t := range tblInt64 {
   261  		b := EncodeInt(nil, t)
   262  		_, v, err := DecodeInt(b)
   263  		c.Assert(err, IsNil)
   264  		c.Assert(v, Equals, t)
   265  
   266  		b = EncodeIntDesc(nil, t)
   267  		_, v, err = DecodeIntDesc(b)
   268  		c.Assert(err, IsNil)
   269  		c.Assert(v, Equals, t)
   270  
   271  		b = EncodeVarint(nil, t)
   272  		_, v, err = DecodeVarint(b)
   273  		c.Assert(err, IsNil)
   274  		c.Assert(v, Equals, t)
   275  
   276  		b = EncodeComparableVarint(nil, t)
   277  		_, v, err = DecodeComparableVarint(b)
   278  		c.Assert(err, IsNil)
   279  		c.Assert(v, Equals, t)
   280  	}
   281  
   282  	tblUint64 := []uint64{
   283  		0,
   284  		math.MaxUint8,
   285  		math.MaxUint16,
   286  		math.MaxUint32,
   287  		math.MaxUint64,
   288  		1<<24 - 1,
   289  		1<<48 - 1,
   290  		1<<56 - 1,
   291  		1,
   292  		math.MaxInt16,
   293  		math.MaxInt8,
   294  		math.MaxInt32,
   295  		math.MaxInt64,
   296  	}
   297  
   298  	for _, t := range tblUint64 {
   299  		b := EncodeUint(nil, t)
   300  		_, v, err := DecodeUint(b)
   301  		c.Assert(err, IsNil)
   302  		c.Assert(v, Equals, t)
   303  
   304  		b = EncodeUintDesc(nil, t)
   305  		_, v, err = DecodeUintDesc(b)
   306  		c.Assert(err, IsNil)
   307  		c.Assert(v, Equals, t)
   308  
   309  		b = EncodeUvarint(nil, t)
   310  		_, v, err = DecodeUvarint(b)
   311  		c.Assert(err, IsNil)
   312  		c.Assert(v, Equals, t)
   313  
   314  		b = EncodeComparableUvarint(nil, t)
   315  		_, v, err = DecodeComparableUvarint(b)
   316  		c.Assert(err, IsNil)
   317  		c.Assert(v, Equals, t)
   318  	}
   319  	var b []byte
   320  	b = EncodeComparableVarint(b, -1)
   321  	b = EncodeComparableUvarint(b, 1)
   322  	b = EncodeComparableVarint(b, 2)
   323  	b, i, err := DecodeComparableVarint(b)
   324  	c.Assert(err, IsNil)
   325  	c.Assert(i, Equals, int64(-1))
   326  	b, u, err := DecodeComparableUvarint(b)
   327  	c.Assert(err, IsNil)
   328  	c.Assert(u, Equals, uint64(1))
   329  	_, i, err = DecodeComparableVarint(b)
   330  	c.Assert(err, IsNil)
   331  	c.Assert(i, Equals, int64(2))
   332  }
   333  
   334  func (s *testCodecSuite) TestNumberOrder(c *C) {
   335  	defer testleak.AfterTest(c)()
   336  	tblInt64 := []struct {
   337  		Arg1 int64
   338  		Arg2 int64
   339  		Ret  int
   340  	}{
   341  		{-1, 1, -1},
   342  		{math.MaxInt64, math.MinInt64, 1},
   343  		{math.MaxInt64, math.MaxInt32, 1},
   344  		{math.MinInt32, math.MaxInt16, -1},
   345  		{math.MinInt64, math.MaxInt8, -1},
   346  		{0, math.MaxInt8, -1},
   347  		{math.MinInt8, 0, -1},
   348  		{math.MinInt16, math.MaxInt16, -1},
   349  		{1, -1, 1},
   350  		{1, 0, 1},
   351  		{-1, 0, -1},
   352  		{0, 0, 0},
   353  		{math.MaxInt16, math.MaxInt16, 0},
   354  	}
   355  
   356  	for _, t := range tblInt64 {
   357  		b1 := EncodeInt(nil, t.Arg1)
   358  		b2 := EncodeInt(nil, t.Arg2)
   359  
   360  		ret := bytes.Compare(b1, b2)
   361  		c.Assert(ret, Equals, t.Ret)
   362  
   363  		b1 = EncodeIntDesc(nil, t.Arg1)
   364  		b2 = EncodeIntDesc(nil, t.Arg2)
   365  
   366  		ret = bytes.Compare(b1, b2)
   367  		c.Assert(ret, Equals, -t.Ret)
   368  
   369  		b1 = EncodeComparableVarint(nil, t.Arg1)
   370  		b2 = EncodeComparableVarint(nil, t.Arg2)
   371  		ret = bytes.Compare(b1, b2)
   372  		c.Assert(ret, Equals, t.Ret)
   373  	}
   374  
   375  	tblUint64 := []struct {
   376  		Arg1 uint64
   377  		Arg2 uint64
   378  		Ret  int
   379  	}{
   380  		{0, 0, 0},
   381  		{1, 0, 1},
   382  		{0, 1, -1},
   383  		{math.MaxInt8, math.MaxInt16, -1},
   384  		{math.MaxUint32, math.MaxInt32, 1},
   385  		{math.MaxUint8, math.MaxInt8, 1},
   386  		{math.MaxUint16, math.MaxInt32, -1},
   387  		{math.MaxUint64, math.MaxInt64, 1},
   388  		{math.MaxInt64, math.MaxUint32, 1},
   389  		{math.MaxUint64, 0, 1},
   390  		{0, math.MaxUint64, -1},
   391  	}
   392  
   393  	for _, t := range tblUint64 {
   394  		b1 := EncodeUint(nil, t.Arg1)
   395  		b2 := EncodeUint(nil, t.Arg2)
   396  
   397  		ret := bytes.Compare(b1, b2)
   398  		c.Assert(ret, Equals, t.Ret)
   399  
   400  		b1 = EncodeUintDesc(nil, t.Arg1)
   401  		b2 = EncodeUintDesc(nil, t.Arg2)
   402  
   403  		ret = bytes.Compare(b1, b2)
   404  		c.Assert(ret, Equals, -t.Ret)
   405  
   406  		b1 = EncodeComparableUvarint(nil, t.Arg1)
   407  		b2 = EncodeComparableUvarint(nil, t.Arg2)
   408  		ret = bytes.Compare(b1, b2)
   409  		c.Assert(ret, Equals, t.Ret)
   410  	}
   411  }
   412  
   413  func (s *testCodecSuite) TestFloatCodec(c *C) {
   414  	defer testleak.AfterTest(c)()
   415  	tblFloat := []float64{
   416  		-1,
   417  		0,
   418  		1,
   419  		math.MaxFloat64,
   420  		math.MaxFloat32,
   421  		math.SmallestNonzeroFloat32,
   422  		math.SmallestNonzeroFloat64,
   423  		math.Inf(-1),
   424  		math.Inf(1),
   425  	}
   426  
   427  	for _, t := range tblFloat {
   428  		b := EncodeFloat(nil, t)
   429  		_, v, err := DecodeFloat(b)
   430  		c.Assert(err, IsNil)
   431  		c.Assert(v, Equals, t)
   432  
   433  		b = EncodeFloatDesc(nil, t)
   434  		_, v, err = DecodeFloatDesc(b)
   435  		c.Assert(err, IsNil)
   436  		c.Assert(v, Equals, t)
   437  	}
   438  
   439  	tblCmp := []struct {
   440  		Arg1 float64
   441  		Arg2 float64
   442  		Ret  int
   443  	}{
   444  		{1, -1, 1},
   445  		{1, 0, 1},
   446  		{0, -1, 1},
   447  		{0, 0, 0},
   448  		{math.MaxFloat64, 1, 1},
   449  		{math.MaxFloat32, math.MaxFloat64, -1},
   450  		{math.MaxFloat64, 0, 1},
   451  		{math.MaxFloat64, math.SmallestNonzeroFloat64, 1},
   452  		{math.Inf(-1), 0, -1},
   453  		{math.Inf(1), 0, 1},
   454  		{math.Inf(-1), math.Inf(1), -1},
   455  	}
   456  
   457  	for _, t := range tblCmp {
   458  		b1 := EncodeFloat(nil, t.Arg1)
   459  		b2 := EncodeFloat(nil, t.Arg2)
   460  
   461  		ret := bytes.Compare(b1, b2)
   462  		c.Assert(ret, Equals, t.Ret)
   463  
   464  		b1 = EncodeFloatDesc(nil, t.Arg1)
   465  		b2 = EncodeFloatDesc(nil, t.Arg2)
   466  
   467  		ret = bytes.Compare(b1, b2)
   468  		c.Assert(ret, Equals, -t.Ret)
   469  	}
   470  }
   471  
   472  func (s *testCodecSuite) TestBytes(c *C) {
   473  	defer testleak.AfterTest(c)()
   474  	tblBytes := [][]byte{
   475  		{},
   476  		{0x00, 0x01},
   477  		{0xff, 0xff},
   478  		{0x01, 0x00},
   479  		[]byte("abc"),
   480  		[]byte("hello world"),
   481  	}
   482  
   483  	for _, t := range tblBytes {
   484  		b := EncodeBytes(nil, t)
   485  		_, v, err := DecodeBytes(b, nil)
   486  		c.Assert(err, IsNil)
   487  		c.Assert(t, DeepEquals, v, Commentf("%v - %v - %v", t, b, v))
   488  
   489  		b = EncodeBytesDesc(nil, t)
   490  		_, v, err = DecodeBytesDesc(b, nil)
   491  		c.Assert(err, IsNil)
   492  		c.Assert(t, DeepEquals, v, Commentf("%v - %v - %v", t, b, v))
   493  
   494  		b = EncodeCompactBytes(nil, t)
   495  		_, v, err = DecodeCompactBytes(b)
   496  		c.Assert(err, IsNil)
   497  		c.Assert(t, DeepEquals, v, Commentf("%v - %v - %v", t, b, v))
   498  	}
   499  
   500  	tblCmp := []struct {
   501  		Arg1 []byte
   502  		Arg2 []byte
   503  		Ret  int
   504  	}{
   505  		{[]byte{}, []byte{0x00}, -1},
   506  		{[]byte{0x00}, []byte{0x00}, 0},
   507  		{[]byte{0xFF}, []byte{0x00}, 1},
   508  		{[]byte{0xFF}, []byte{0xFF, 0x00}, -1},
   509  		{[]byte("a"), []byte("b"), -1},
   510  		{[]byte("a"), []byte{0x00}, 1},
   511  		{[]byte{0x00}, []byte{0x01}, -1},
   512  		{[]byte{0x00, 0x01}, []byte{0x00, 0x00}, 1},
   513  		{[]byte{0x00, 0x00, 0x00}, []byte{0x00, 0x00}, 1},
   514  		{[]byte{0x00, 0x00, 0x00}, []byte{0x00, 0x00}, 1},
   515  		{[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, -1},
   516  		{[]byte{0x01, 0x02, 0x03, 0x00}, []byte{0x01, 0x02, 0x03}, 1},
   517  		{[]byte{0x01, 0x03, 0x03, 0x04}, []byte{0x01, 0x03, 0x03, 0x05}, -1},
   518  		{[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, -1},
   519  		{[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, 1},
   520  		{[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00}, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, 1},
   521  	}
   522  
   523  	for _, t := range tblCmp {
   524  		b1 := EncodeBytes(nil, t.Arg1)
   525  		b2 := EncodeBytes(nil, t.Arg2)
   526  
   527  		ret := bytes.Compare(b1, b2)
   528  		c.Assert(ret, Equals, t.Ret)
   529  
   530  		b1 = EncodeBytesDesc(nil, t.Arg1)
   531  		b2 = EncodeBytesDesc(nil, t.Arg2)
   532  
   533  		ret = bytes.Compare(b1, b2)
   534  		c.Assert(ret, Equals, -t.Ret)
   535  	}
   536  }
   537  
   538  func parseTime(c *C, s string) types.Time {
   539  	sc := &stmtctx.StatementContext{TimeZone: time.UTC}
   540  	m, err := types.ParseTime(sc, s, allegrosql.TypeDatetime, types.DefaultFsp)
   541  	c.Assert(err, IsNil)
   542  	return m
   543  }
   544  
   545  func parseDuration(c *C, s string) types.Duration {
   546  	m, err := types.ParseDuration(nil, s, types.DefaultFsp)
   547  	c.Assert(err, IsNil)
   548  	return m
   549  }
   550  
   551  func (s *testCodecSuite) TestTime(c *C) {
   552  	defer testleak.AfterTest(c)()
   553  	tbl := []string{
   554  		"2011-01-01 00:00:00",
   555  		"2011-01-01 00:00:00",
   556  		"0001-01-01 00:00:00",
   557  	}
   558  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
   559  	for _, t := range tbl {
   560  		m := types.NewCauset(parseTime(c, t))
   561  
   562  		b, err := EncodeKey(sc, nil, m)
   563  		c.Assert(err, IsNil)
   564  		v, err := Decode(b, 1)
   565  		c.Assert(err, IsNil)
   566  		var t types.Time
   567  		t.SetType(allegrosql.TypeDatetime)
   568  		t.FromPackedUint(v[0].GetUint64())
   569  		c.Assert(types.NewCauset(t), DeepEquals, m)
   570  	}
   571  
   572  	tblCmp := []struct {
   573  		Arg1 string
   574  		Arg2 string
   575  		Ret  int
   576  	}{
   577  		{"2011-10-10 00:00:00", "2000-12-12 11:11:11", 1},
   578  		{"2000-10-10 00:00:00", "2001-10-10 00:00:00", -1},
   579  		{"2000-10-10 00:00:00", "2000-10-10 00:00:00", 0},
   580  	}
   581  
   582  	for _, t := range tblCmp {
   583  		m1 := types.NewCauset(parseTime(c, t.Arg1))
   584  		m2 := types.NewCauset(parseTime(c, t.Arg2))
   585  
   586  		b1, err := EncodeKey(sc, nil, m1)
   587  		c.Assert(err, IsNil)
   588  		b2, err := EncodeKey(sc, nil, m2)
   589  		c.Assert(err, IsNil)
   590  
   591  		ret := bytes.Compare(b1, b2)
   592  		c.Assert(ret, Equals, t.Ret)
   593  	}
   594  }
   595  
   596  func (s *testCodecSuite) TestDuration(c *C) {
   597  	defer testleak.AfterTest(c)()
   598  	tbl := []string{
   599  		"11:11:11",
   600  		"00:00:00",
   601  		"1 11:11:11",
   602  	}
   603  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
   604  	for _, t := range tbl {
   605  		m := parseDuration(c, t)
   606  
   607  		b, err := EncodeKey(sc, nil, types.NewCauset(m))
   608  		c.Assert(err, IsNil)
   609  		v, err := Decode(b, 1)
   610  		c.Assert(err, IsNil)
   611  		m.Fsp = types.MaxFsp
   612  		c.Assert(v, DeepEquals, types.MakeCausets(m))
   613  	}
   614  
   615  	tblCmp := []struct {
   616  		Arg1 string
   617  		Arg2 string
   618  		Ret  int
   619  	}{
   620  		{"20:00:00", "11:11:11", 1},
   621  		{"00:00:00", "00:00:01", -1},
   622  		{"00:00:00", "00:00:00", 0},
   623  	}
   624  
   625  	for _, t := range tblCmp {
   626  		m1 := parseDuration(c, t.Arg1)
   627  		m2 := parseDuration(c, t.Arg2)
   628  
   629  		b1, err := EncodeKey(sc, nil, types.NewCauset(m1))
   630  		c.Assert(err, IsNil)
   631  		b2, err := EncodeKey(sc, nil, types.NewCauset(m2))
   632  		c.Assert(err, IsNil)
   633  
   634  		ret := bytes.Compare(b1, b2)
   635  		c.Assert(ret, Equals, t.Ret)
   636  	}
   637  }
   638  
   639  func (s *testCodecSuite) TestDecimal(c *C) {
   640  	defer testleak.AfterTest(c)()
   641  	tbl := []string{
   642  		"1234.00",
   643  		"1234",
   644  		"12.34",
   645  		"12.340",
   646  		"0.1234",
   647  		"0.0",
   648  		"0",
   649  		"-0.0",
   650  		"-0.0000",
   651  		"-1234.00",
   652  		"-1234",
   653  		"-12.34",
   654  		"-12.340",
   655  		"-0.1234",
   656  	}
   657  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
   658  	for _, t := range tbl {
   659  		dec := new(types.MyDecimal)
   660  		err := dec.FromString([]byte(t))
   661  		c.Assert(err, IsNil)
   662  		b, err := EncodeKey(sc, nil, types.NewCauset(dec))
   663  		c.Assert(err, IsNil)
   664  		v, err := Decode(b, 1)
   665  		c.Assert(err, IsNil)
   666  		c.Assert(v, HasLen, 1)
   667  		vv := v[0].GetMysqlDecimal()
   668  		c.Assert(vv.Compare(dec), Equals, 0)
   669  	}
   670  
   671  	tblCmp := []struct {
   672  		Arg1 interface{}
   673  		Arg2 interface{}
   674  		Ret  int
   675  	}{
   676  		// Test for float type decimal.
   677  		{"1234", "123400", -1},
   678  		{"12340", "123400", -1},
   679  		{"1234", "1234.5", -1},
   680  		{"1234", "1234.0000", 0},
   681  		{"1234", "12.34", 1},
   682  		{"12.34", "12.35", -1},
   683  		{"0.12", "0.1234", -1},
   684  		{"0.1234", "12.3400", -1},
   685  		{"0.1234", "0.1235", -1},
   686  		{"0.123400", "12.34", -1},
   687  		{"12.34000", "12.34", 0},
   688  		{"0.01234", "0.01235", -1},
   689  		{"0.1234", "0", 1},
   690  		{"0.0000", "0", 0},
   691  		{"0.0001", "0", 1},
   692  		{"0.0001", "0.0000", 1},
   693  		{"0", "-0.0000", 0},
   694  		{"-0.0001", "0", -1},
   695  		{"-0.1234", "0", -1},
   696  		{"-0.1234", "-0.12", -1},
   697  		{"-0.12", "-0.1234", 1},
   698  		{"-0.12", "-0.1200", 0},
   699  		{"-0.1234", "0.1234", -1},
   700  		{"-1.234", "-12.34", 1},
   701  		{"-0.1234", "-12.34", 1},
   702  		{"-12.34", "1234", -1},
   703  		{"-12.34", "-12.35", 1},
   704  		{"-0.01234", "-0.01235", 1},
   705  		{"-1234", "-123400", 1},
   706  		{"-12340", "-123400", 1},
   707  
   708  		// Test for int type decimal.
   709  		{int64(-1), int64(1), -1},
   710  		{int64(math.MaxInt64), int64(math.MinInt64), 1},
   711  		{int64(math.MaxInt64), int64(math.MaxInt32), 1},
   712  		{int64(math.MinInt32), int64(math.MaxInt16), -1},
   713  		{int64(math.MinInt64), int64(math.MaxInt8), -1},
   714  		{int64(0), int64(math.MaxInt8), -1},
   715  		{int64(math.MinInt8), int64(0), -1},
   716  		{int64(math.MinInt16), int64(math.MaxInt16), -1},
   717  		{int64(1), int64(-1), 1},
   718  		{int64(1), int64(0), 1},
   719  		{int64(-1), int64(0), -1},
   720  		{int64(0), int64(0), 0},
   721  		{int64(math.MaxInt16), int64(math.MaxInt16), 0},
   722  
   723  		// Test for uint type decimal.
   724  		{uint64(0), uint64(0), 0},
   725  		{uint64(1), uint64(0), 1},
   726  		{uint64(0), uint64(1), -1},
   727  		{uint64(math.MaxInt8), uint64(math.MaxInt16), -1},
   728  		{uint64(math.MaxUint32), uint64(math.MaxInt32), 1},
   729  		{uint64(math.MaxUint8), uint64(math.MaxInt8), 1},
   730  		{uint64(math.MaxUint16), uint64(math.MaxInt32), -1},
   731  		{uint64(math.MaxUint64), uint64(math.MaxInt64), 1},
   732  		{uint64(math.MaxInt64), uint64(math.MaxUint32), 1},
   733  		{uint64(math.MaxUint64), uint64(0), 1},
   734  		{uint64(0), uint64(math.MaxUint64), -1},
   735  	}
   736  	for _, t := range tblCmp {
   737  		d1 := types.NewCauset(t.Arg1)
   738  		dec1, err := d1.ToDecimal(sc)
   739  		c.Assert(err, IsNil)
   740  		d1.SetMysqlDecimal(dec1)
   741  		d2 := types.NewCauset(t.Arg2)
   742  		dec2, err := d2.ToDecimal(sc)
   743  		c.Assert(err, IsNil)
   744  		d2.SetMysqlDecimal(dec2)
   745  
   746  		d1.SetLength(30)
   747  		d1.SetFrac(6)
   748  		d2.SetLength(30)
   749  		d2.SetFrac(6)
   750  		b1, err := EncodeKey(sc, nil, d1)
   751  		c.Assert(err, IsNil)
   752  		b2, err := EncodeKey(sc, nil, d2)
   753  		c.Assert(err, IsNil)
   754  
   755  		ret := bytes.Compare(b1, b2)
   756  		c.Assert(ret, Equals, t.Ret, Commentf("%v %x %x", t, b1, b2))
   757  
   758  		b1, err = EncodeValue(sc, b1[:0], d1)
   759  		c.Assert(err, IsNil)
   760  		size, err := EstimateValueSize(sc, d1)
   761  		c.Assert(err, IsNil)
   762  		c.Assert(len(b1), Equals, size)
   763  
   764  	}
   765  
   766  	floats := []float64{-123.45, -123.40, -23.45, -1.43, -0.93, -0.4333, -0.068,
   767  		-0.0099, 0, 0.001, 0.0012, 0.12, 1.2, 1.23, 123.3, 2424.242424}
   768  	decs := make([][]byte, 0, len(floats))
   769  	for i := range floats {
   770  		dec := types.NewDecFromFloatForTest(floats[i])
   771  		var d types.Causet
   772  		d.SetLength(20)
   773  		d.SetFrac(6)
   774  		d.SetMysqlDecimal(dec)
   775  		b, err := EncodeDecimal(nil, d.GetMysqlDecimal(), d.Length(), d.Frac())
   776  		c.Assert(err, IsNil)
   777  		decs = append(decs, b)
   778  		size, err := EstimateValueSize(sc, d)
   779  		c.Assert(err, IsNil)
   780  		// size - 1 because the flag occupy 1 bit.
   781  		c.Assert(len(b), Equals, size-1)
   782  	}
   783  	for i := 0; i < len(decs)-1; i++ {
   784  		cmp := bytes.Compare(decs[i], decs[i+1])
   785  		c.Assert(cmp, LessEqual, 0)
   786  	}
   787  
   788  	d := types.NewDecFromStringForTest("-123.123456789")
   789  	_, err := EncodeDecimal(nil, d, 20, 5)
   790  	c.Assert(terror.ErrorEqual(err, types.ErrTruncated), IsTrue, Commentf("err %v", err))
   791  	_, err = EncodeDecimal(nil, d, 12, 10)
   792  	c.Assert(terror.ErrorEqual(err, types.ErrOverflow), IsTrue, Commentf("err %v", err))
   793  
   794  	sc.IgnoreTruncate = true
   795  	decimalCauset := types.NewCauset(d)
   796  	decimalCauset.SetLength(20)
   797  	decimalCauset.SetFrac(5)
   798  	_, err = EncodeValue(sc, nil, decimalCauset)
   799  	c.Assert(err, IsNil)
   800  
   801  	sc.OverflowAsWarning = true
   802  	decimalCauset.SetLength(12)
   803  	decimalCauset.SetFrac(10)
   804  	_, err = EncodeValue(sc, nil, decimalCauset)
   805  	c.Assert(err, IsNil)
   806  }
   807  
   808  func (s *testCodecSuite) TestJSON(c *C) {
   809  	defer testleak.AfterTest(c)()
   810  	tbl := []string{
   811  		"1234.00",
   812  		`{"a": "b"}`,
   813  	}
   814  
   815  	datums := make([]types.Causet, 0, len(tbl))
   816  	for _, t := range tbl {
   817  		var d types.Causet
   818  		j, err := json.ParseBinaryFromString(t)
   819  		c.Assert(err, IsNil)
   820  		d.SetMysqlJSON(j)
   821  		datums = append(datums, d)
   822  	}
   823  
   824  	buf := make([]byte, 0, 4096)
   825  	buf, err := encode(nil, buf, datums, false)
   826  	c.Assert(err, IsNil)
   827  
   828  	datums1, err := Decode(buf, 2)
   829  	c.Assert(err, IsNil)
   830  
   831  	for i := range datums1 {
   832  		lhs := datums[i].GetMysqlJSON().String()
   833  		rhs := datums1[i].GetMysqlJSON().String()
   834  		c.Assert(lhs, Equals, rhs)
   835  	}
   836  }
   837  
   838  func (s *testCodecSuite) TestCut(c *C) {
   839  	defer testleak.AfterTest(c)()
   840  	causet := []struct {
   841  		Input  []types.Causet
   842  		Expect []types.Causet
   843  	}{
   844  		{
   845  			types.MakeCausets(int64(1)),
   846  			types.MakeCausets(int64(1)),
   847  		},
   848  
   849  		{
   850  			types.MakeCausets(float32(1), float64(3.15), []byte("123"), "123"),
   851  			types.MakeCausets(float64(1), float64(3.15), []byte("123"), []byte("123")),
   852  		},
   853  		{
   854  			types.MakeCausets(uint64(1), float64(3.15), []byte("123"), int64(-1)),
   855  			types.MakeCausets(uint64(1), float64(3.15), []byte("123"), int64(-1)),
   856  		},
   857  
   858  		{
   859  			types.MakeCausets(true, false),
   860  			types.MakeCausets(int64(1), int64(0)),
   861  		},
   862  
   863  		{
   864  			types.MakeCausets(nil),
   865  			types.MakeCausets(nil),
   866  		},
   867  
   868  		{
   869  			types.MakeCausets(types.NewBinaryLiteralFromUint(100, -1), types.NewBinaryLiteralFromUint(100, 4)),
   870  			types.MakeCausets(uint64(100), uint64(100)),
   871  		},
   872  
   873  		{
   874  			types.MakeCausets(types.Enum{Name: "a", Value: 1}, types.Set{Name: "a", Value: 1}),
   875  			types.MakeCausets(uint64(1), uint64(1)),
   876  		},
   877  		{
   878  			types.MakeCausets(float32(1), float64(3.15), []byte("123456789012345")),
   879  			types.MakeCausets(float64(1), float64(3.15), []byte("123456789012345")),
   880  		},
   881  		{
   882  			types.MakeCausets(types.NewDecFromInt(0), types.NewDecFromFloatForTest(-1.3)),
   883  			types.MakeCausets(types.NewDecFromInt(0), types.NewDecFromFloatForTest(-1.3)),
   884  		},
   885  		{
   886  			types.MakeCausets(json.CreateBinary("abc")),
   887  			types.MakeCausets(json.CreateBinary("abc")),
   888  		},
   889  	}
   890  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
   891  	for i, t := range causet {
   892  		comment := Commentf("%d %v", i, t)
   893  		b, err := EncodeKey(sc, nil, t.Input...)
   894  		c.Assert(err, IsNil, comment)
   895  		var d []byte
   896  		for j, e := range t.Expect {
   897  			d, b, err = CutOne(b)
   898  			c.Assert(err, IsNil)
   899  			c.Assert(d, NotNil)
   900  			ed, err1 := EncodeKey(sc, nil, e)
   901  			c.Assert(err1, IsNil)
   902  			c.Assert(d, DeepEquals, ed, Commentf("%d:%d %#v", i, j, e))
   903  		}
   904  		c.Assert(b, HasLen, 0)
   905  	}
   906  	for i, t := range causet {
   907  		comment := Commentf("%d %v", i, t)
   908  		b, err := EncodeValue(sc, nil, t.Input...)
   909  		c.Assert(err, IsNil, comment)
   910  		var d []byte
   911  		for j, e := range t.Expect {
   912  			d, b, err = CutOne(b)
   913  			c.Assert(err, IsNil)
   914  			c.Assert(d, NotNil)
   915  			ed, err1 := EncodeValue(sc, nil, e)
   916  			c.Assert(err1, IsNil)
   917  			c.Assert(d, DeepEquals, ed, Commentf("%d:%d %#v", i, j, e))
   918  		}
   919  		c.Assert(b, HasLen, 0)
   920  	}
   921  
   922  	b, err := EncodeValue(sc, nil, types.NewCauset(42))
   923  	c.Assert(err, IsNil)
   924  	rem, n, err := CutDeferredCausetID(b)
   925  	c.Assert(err, IsNil)
   926  	c.Assert(rem, HasLen, 0)
   927  	c.Assert(n, Equals, int64(42))
   928  }
   929  
   930  func (s *testCodecSuite) TestSetRawValues(c *C) {
   931  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
   932  	datums := types.MakeCausets(1, "abc", 1.1, []byte("def"))
   933  	rowData, err := EncodeValue(sc, nil, datums...)
   934  	c.Assert(err, IsNil)
   935  	values := make([]types.Causet, 4)
   936  	err = SetRawValues(rowData, values)
   937  	c.Assert(err, IsNil)
   938  	for i, rawVal := range values {
   939  		c.Assert(rawVal.HoTT(), Equals, types.HoTTRaw)
   940  		encoded, err1 := EncodeValue(sc, nil, datums[i])
   941  		c.Assert(err1, IsNil)
   942  		c.Assert(encoded, BytesEquals, rawVal.GetBytes())
   943  	}
   944  }
   945  
   946  func (s *testCodecSuite) TestDecodeOneToChunk(c *C) {
   947  	defer testleak.AfterTest(c)()
   948  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
   949  	datums, tps := datumsForTest(sc)
   950  	rowCount := 3
   951  	chk := chunkForTest(c, sc, datums, tps, rowCount)
   952  	for defCausIdx, tp := range tps {
   953  		for rowIdx := 0; rowIdx < rowCount; rowIdx++ {
   954  			got := chk.GetRow(rowIdx).GetCauset(defCausIdx, tp)
   955  			expect := datums[defCausIdx]
   956  			if got.IsNull() {
   957  				c.Assert(expect.IsNull(), IsTrue)
   958  			} else {
   959  				if got.HoTT() != types.HoTTMysqlDecimal {
   960  					cmp, err := got.CompareCauset(sc, &expect)
   961  					c.Assert(err, IsNil)
   962  					c.Assert(cmp, Equals, 0, Commentf("expect: %v, got %v", expect, got))
   963  				} else {
   964  					c.Assert(got.GetString(), Equals, expect.GetString(), Commentf("expect: %v, got %v", expect, got))
   965  				}
   966  			}
   967  		}
   968  	}
   969  }
   970  
   971  func (s *testCodecSuite) TestHashGroup(c *C) {
   972  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
   973  	tp := types.NewFieldType(allegrosql.TypeNewDecimal)
   974  	tps := []*types.FieldType{tp}
   975  	chk1 := chunk.New(tps, 3, 3)
   976  	chk1.Reset()
   977  	chk1.DeferredCauset(0).AppendMyDecimal(types.NewDecFromStringForTest("-123.123456789"))
   978  	chk1.DeferredCauset(0).AppendMyDecimal(types.NewDecFromStringForTest("-123.123456789"))
   979  	chk1.DeferredCauset(0).AppendMyDecimal(types.NewDecFromStringForTest("-123.123456789"))
   980  
   981  	buf1 := make([][]byte, 3)
   982  	tp1 := tp
   983  	tp1.Flen = 20
   984  	tp1.Decimal = 5
   985  	_, err := HashGroupKey(sc, 3, chk1.DeferredCauset(0), buf1, tp1)
   986  	c.Assert(err, NotNil)
   987  	tp2 := tp
   988  	tp2.Flen = 12
   989  	tp2.Decimal = 10
   990  	_, err = HashGroupKey(sc, 3, chk1.DeferredCauset(0), buf1, tp2)
   991  	c.Assert(err, NotNil)
   992  }
   993  
   994  func datumsForTest(sc *stmtctx.StatementContext) ([]types.Causet, []*types.FieldType) {
   995  	decType := types.NewFieldType(allegrosql.TypeNewDecimal)
   996  	decType.Decimal = 2
   997  	causet := []struct {
   998  		value interface{}
   999  		tp    *types.FieldType
  1000  	}{
  1001  		{nil, types.NewFieldType(allegrosql.TypeNull)},
  1002  		{nil, types.NewFieldType(allegrosql.TypeLonglong)},
  1003  		{nil, types.NewFieldType(allegrosql.TypeFloat)},
  1004  		{nil, types.NewFieldType(allegrosql.TypeDate)},
  1005  		{nil, types.NewFieldType(allegrosql.TypeDuration)},
  1006  		{nil, types.NewFieldType(allegrosql.TypeNewDecimal)},
  1007  		{nil, types.NewFieldType(allegrosql.TypeEnum)},
  1008  		{nil, types.NewFieldType(allegrosql.TypeSet)},
  1009  		{nil, types.NewFieldType(allegrosql.TypeBit)},
  1010  		{nil, types.NewFieldType(allegrosql.TypeJSON)},
  1011  		{nil, types.NewFieldType(allegrosql.TypeVarchar)},
  1012  		{nil, types.NewFieldType(allegrosql.TypeDouble)},
  1013  		{int64(1), types.NewFieldType(allegrosql.TypeTiny)},
  1014  		{int64(1), types.NewFieldType(allegrosql.TypeShort)},
  1015  		{int64(1), types.NewFieldType(allegrosql.TypeInt24)},
  1016  		{int64(1), types.NewFieldType(allegrosql.TypeLong)},
  1017  		{int64(-1), types.NewFieldType(allegrosql.TypeLong)},
  1018  		{int64(1), types.NewFieldType(allegrosql.TypeLonglong)},
  1019  		{uint64(1), types.NewFieldType(allegrosql.TypeLonglong)},
  1020  		{float32(1), types.NewFieldType(allegrosql.TypeFloat)},
  1021  		{float64(1), types.NewFieldType(allegrosql.TypeDouble)},
  1022  		{types.NewDecFromInt(1), types.NewFieldType(allegrosql.TypeNewDecimal)},
  1023  		{types.NewDecFromStringForTest("1.123"), decType},
  1024  		{"abc", types.NewFieldType(allegrosql.TypeString)},
  1025  		{"def", types.NewFieldType(allegrosql.TypeVarchar)},
  1026  		{"ghi", types.NewFieldType(allegrosql.TypeVarString)},
  1027  		{[]byte("abc"), types.NewFieldType(allegrosql.TypeBlob)},
  1028  		{[]byte("abc"), types.NewFieldType(allegrosql.TypeTinyBlob)},
  1029  		{[]byte("abc"), types.NewFieldType(allegrosql.TypeMediumBlob)},
  1030  		{[]byte("abc"), types.NewFieldType(allegrosql.TypeLongBlob)},
  1031  		{types.CurrentTime(allegrosql.TypeDatetime), types.NewFieldType(allegrosql.TypeDatetime)},
  1032  		{types.CurrentTime(allegrosql.TypeDate), types.NewFieldType(allegrosql.TypeDate)},
  1033  		{types.NewTime(types.FromGoTime(time.Now()), allegrosql.TypeTimestamp, types.DefaultFsp), types.NewFieldType(allegrosql.TypeTimestamp)},
  1034  		{types.Duration{Duration: time.Second, Fsp: 1}, types.NewFieldType(allegrosql.TypeDuration)},
  1035  		{types.Enum{Name: "a", Value: 1}, &types.FieldType{Tp: allegrosql.TypeEnum, Elems: []string{"a"}}},
  1036  		{types.Set{Name: "a", Value: 1}, &types.FieldType{Tp: allegrosql.TypeSet, Elems: []string{"a"}}},
  1037  		{types.BinaryLiteral{100}, &types.FieldType{Tp: allegrosql.TypeBit, Flen: 8}},
  1038  		{json.CreateBinary("abc"), types.NewFieldType(allegrosql.TypeJSON)},
  1039  		{int64(1), types.NewFieldType(allegrosql.TypeYear)},
  1040  	}
  1041  
  1042  	datums := make([]types.Causet, 0, len(causet)+2)
  1043  	tps := make([]*types.FieldType, 0, len(causet)+2)
  1044  	for _, t := range causet {
  1045  		tps = append(tps, t.tp)
  1046  		d := types.NewCauset(t.value)
  1047  		datums = append(datums, d)
  1048  	}
  1049  	return datums, tps
  1050  }
  1051  
  1052  func chunkForTest(c *C, sc *stmtctx.StatementContext, datums []types.Causet, tps []*types.FieldType, rowCount int) *chunk.Chunk {
  1053  	causetDecoder := NewCausetDecoder(chunk.New(tps, 32, 32), sc.TimeZone)
  1054  	for rowIdx := 0; rowIdx < rowCount; rowIdx++ {
  1055  		encoded, err := EncodeValue(sc, nil, datums...)
  1056  		c.Assert(err, IsNil)
  1057  		causetDecoder.buf = make([]byte, 0, len(encoded))
  1058  		for defCausIdx, tp := range tps {
  1059  			encoded, err = causetDecoder.DecodeOne(encoded, defCausIdx, tp)
  1060  			c.Assert(err, IsNil)
  1061  		}
  1062  	}
  1063  	return causetDecoder.chk
  1064  }
  1065  
  1066  func (s *testCodecSuite) TestDecodeRange(c *C) {
  1067  	_, _, err := DecodeRange(nil, 0, nil, nil)
  1068  	c.Assert(err, NotNil)
  1069  
  1070  	datums := types.MakeCausets(1, "abc", 1.1, []byte("def"))
  1071  	rowData, err := EncodeValue(nil, nil, datums...)
  1072  	c.Assert(err, IsNil)
  1073  
  1074  	datums1, _, err := DecodeRange(rowData, len(datums), nil, nil)
  1075  	c.Assert(err, IsNil)
  1076  	for i, causet := range datums1 {
  1077  		cmp, err := causet.CompareCauset(nil, &datums[i])
  1078  		c.Assert(err, IsNil)
  1079  		c.Assert(cmp, Equals, 0)
  1080  	}
  1081  
  1082  	for _, b := range []byte{NilFlag, bytesFlag, maxFlag, maxFlag + 1} {
  1083  		newData := append(rowData, b)
  1084  		_, _, err := DecodeRange(newData, len(datums)+1, nil, nil)
  1085  		c.Assert(err, IsNil)
  1086  	}
  1087  }
  1088  
  1089  func testHashChunkRowEqual(c *C, a, b interface{}, equal bool) {
  1090  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
  1091  	buf1 := make([]byte, 1)
  1092  	buf2 := make([]byte, 1)
  1093  
  1094  	tp1 := new(types.FieldType)
  1095  	types.DefaultTypeForValue(a, tp1, allegrosql.DefaultCharset, allegrosql.DefaultDefCauslationName)
  1096  	chk1 := chunk.New([]*types.FieldType{tp1}, 1, 1)
  1097  	d := types.Causet{}
  1098  	d.SetValue(a, tp1)
  1099  	chk1.AppendCauset(0, &d)
  1100  
  1101  	tp2 := new(types.FieldType)
  1102  	types.DefaultTypeForValue(b, tp2, allegrosql.DefaultCharset, allegrosql.DefaultDefCauslationName)
  1103  	chk2 := chunk.New([]*types.FieldType{tp2}, 1, 1)
  1104  	d = types.Causet{}
  1105  	d.SetValue(b, tp2)
  1106  	chk2.AppendCauset(0, &d)
  1107  
  1108  	h := crc32.NewIEEE()
  1109  	err1 := HashChunkRow(sc, h, chk1.GetRow(0), []*types.FieldType{tp1}, []int{0}, buf1)
  1110  	sum1 := h.Sum32()
  1111  	h.Reset()
  1112  	err2 := HashChunkRow(sc, h, chk2.GetRow(0), []*types.FieldType{tp2}, []int{0}, buf2)
  1113  	sum2 := h.Sum32()
  1114  	c.Assert(err1, IsNil)
  1115  	c.Assert(err2, IsNil)
  1116  	if equal {
  1117  		c.Assert(sum1, Equals, sum2)
  1118  	} else {
  1119  		c.Assert(sum1, Not(Equals), sum2)
  1120  	}
  1121  	e, err := EqualChunkRow(sc,
  1122  		chk1.GetRow(0), []*types.FieldType{tp1}, []int{0},
  1123  		chk2.GetRow(0), []*types.FieldType{tp2}, []int{0})
  1124  	c.Assert(err, IsNil)
  1125  	if equal {
  1126  		c.Assert(e, IsTrue)
  1127  	} else {
  1128  		c.Assert(e, IsFalse)
  1129  	}
  1130  }
  1131  
  1132  func (s *testCodecSuite) TestHashChunkRow(c *C) {
  1133  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
  1134  	buf := make([]byte, 1)
  1135  	datums, tps := datumsForTest(sc)
  1136  	chk := chunkForTest(c, sc, datums, tps, 1)
  1137  
  1138  	defCausIdx := make([]int, len(tps))
  1139  	for i := 0; i < len(tps); i++ {
  1140  		defCausIdx[i] = i
  1141  	}
  1142  	h := crc32.NewIEEE()
  1143  	err1 := HashChunkRow(sc, h, chk.GetRow(0), tps, defCausIdx, buf)
  1144  	sum1 := h.Sum32()
  1145  	h.Reset()
  1146  	err2 := HashChunkRow(sc, h, chk.GetRow(0), tps, defCausIdx, buf)
  1147  	sum2 := h.Sum32()
  1148  
  1149  	c.Assert(err1, IsNil)
  1150  	c.Assert(err2, IsNil)
  1151  	c.Assert(sum1, Equals, sum2)
  1152  	e, err := EqualChunkRow(sc,
  1153  		chk.GetRow(0), tps, defCausIdx,
  1154  		chk.GetRow(0), tps, defCausIdx)
  1155  	c.Assert(err, IsNil)
  1156  	c.Assert(e, IsTrue)
  1157  
  1158  	testHashChunkRowEqual(c, nil, nil, true)
  1159  	testHashChunkRowEqual(c, uint64(1), int64(1), true)
  1160  	testHashChunkRowEqual(c, uint64(18446744073709551615), int64(-1), false)
  1161  
  1162  	dec1 := types.NewDecFromStringForTest("1.1")
  1163  	dec2 := types.NewDecFromStringForTest("01.100")
  1164  	testHashChunkRowEqual(c, dec1, dec2, true)
  1165  	dec1 = types.NewDecFromStringForTest("1.1")
  1166  	dec2 = types.NewDecFromStringForTest("01.200")
  1167  	testHashChunkRowEqual(c, dec1, dec2, false)
  1168  
  1169  	testHashChunkRowEqual(c, float32(1.0), float64(1.0), true)
  1170  	testHashChunkRowEqual(c, float32(1.0), float64(1.1), false)
  1171  
  1172  	testHashChunkRowEqual(c, "x", []byte("x"), true)
  1173  	testHashChunkRowEqual(c, "x", []byte("y"), false)
  1174  }
  1175  
  1176  func (s *testCodecSuite) TestValueSizeOfSignedInt(c *C) {
  1177  	testCase := []int64{64, 8192, 1048576, 134217728, 17179869184, 2199023255552, 281474976710656, 36028797018963968, 4611686018427387904}
  1178  	var b []byte
  1179  	for _, v := range testCase {
  1180  		b := encodeSignedInt(b[:0], v-10, false)
  1181  		c.Assert(len(b), Equals, valueSizeOfSignedInt(v-10))
  1182  
  1183  		b = encodeSignedInt(b[:0], v, false)
  1184  		c.Assert(len(b), Equals, valueSizeOfSignedInt(v))
  1185  
  1186  		b = encodeSignedInt(b[:0], v+10, false)
  1187  		c.Assert(len(b), Equals, valueSizeOfSignedInt(v+10))
  1188  
  1189  		// Test for negative value.
  1190  		b = encodeSignedInt(b[:0], 0-v, false)
  1191  		c.Assert(len(b), Equals, valueSizeOfSignedInt(0-v))
  1192  
  1193  		b = encodeSignedInt(b[:0], 0-v+10, false)
  1194  		c.Assert(len(b), Equals, valueSizeOfSignedInt(0-v+10))
  1195  
  1196  		b = encodeSignedInt(b[:0], 0-v-10, false)
  1197  		c.Assert(len(b), Equals, valueSizeOfSignedInt(0-v-10))
  1198  	}
  1199  }
  1200  
  1201  func (s *testCodecSuite) TestValueSizeOfUnsignedInt(c *C) {
  1202  	testCase := []uint64{128, 16384, 2097152, 268435456, 34359738368, 4398046511104, 562949953421312, 72057594037927936, 9223372036854775808}
  1203  	var b []byte
  1204  	for _, v := range testCase {
  1205  		b := encodeUnsignedInt(b[:0], v-10, false)
  1206  		c.Assert(len(b), Equals, valueSizeOfUnsignedInt(v-10))
  1207  
  1208  		b = encodeUnsignedInt(b[:0], v, false)
  1209  		c.Assert(len(b), Equals, valueSizeOfUnsignedInt(v))
  1210  
  1211  		b = encodeUnsignedInt(b[:0], v+10, false)
  1212  		c.Assert(len(b), Equals, valueSizeOfUnsignedInt(v+10))
  1213  	}
  1214  }
  1215  
  1216  func (s *testCodecSuite) TestHashChunkDeferredCausets(c *C) {
  1217  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
  1218  	buf := make([]byte, 1)
  1219  	datums, tps := datumsForTest(sc)
  1220  	chk := chunkForTest(c, sc, datums, tps, 4)
  1221  
  1222  	defCausIdx := make([]int, len(tps))
  1223  	for i := 0; i < len(tps); i++ {
  1224  		defCausIdx[i] = i
  1225  	}
  1226  	hasNull := []bool{false, false, false}
  1227  	vecHash := []hash.Hash64{fnv.New64(), fnv.New64(), fnv.New64()}
  1228  	rowHash := []hash.Hash64{fnv.New64(), fnv.New64(), fnv.New64()}
  1229  
  1230  	sel := make([]bool, len(datums))
  1231  	for i := 0; i < 3; i++ {
  1232  		sel[i] = true
  1233  	}
  1234  
  1235  	// Test hash value of the first 12 `Null` defCausumns
  1236  	for i := 0; i < 12; i++ {
  1237  		c.Assert(chk.GetRow(0).IsNull(i), Equals, true)
  1238  		err1 := HashChunkSelected(sc, vecHash, chk, tps[i], i, buf, hasNull, sel, false)
  1239  		err2 := HashChunkRow(sc, rowHash[0], chk.GetRow(0), tps, defCausIdx[i:i+1], buf)
  1240  		err3 := HashChunkRow(sc, rowHash[1], chk.GetRow(1), tps, defCausIdx[i:i+1], buf)
  1241  		err4 := HashChunkRow(sc, rowHash[2], chk.GetRow(2), tps, defCausIdx[i:i+1], buf)
  1242  		c.Assert(err1, IsNil)
  1243  		c.Assert(err2, IsNil)
  1244  		c.Assert(err3, IsNil)
  1245  		c.Assert(err4, IsNil)
  1246  
  1247  		c.Assert(hasNull[0], Equals, true)
  1248  		c.Assert(hasNull[1], Equals, true)
  1249  		c.Assert(hasNull[2], Equals, true)
  1250  		c.Assert(vecHash[0].Sum64(), Equals, rowHash[0].Sum64())
  1251  		c.Assert(vecHash[1].Sum64(), Equals, rowHash[1].Sum64())
  1252  		c.Assert(vecHash[2].Sum64(), Equals, rowHash[2].Sum64())
  1253  
  1254  	}
  1255  
  1256  	// Test hash value of every single defCausumn that is not `Null`
  1257  	for i := 12; i < len(tps); i++ {
  1258  		hasNull = []bool{false, false, false}
  1259  		vecHash = []hash.Hash64{fnv.New64(), fnv.New64(), fnv.New64()}
  1260  		rowHash = []hash.Hash64{fnv.New64(), fnv.New64(), fnv.New64()}
  1261  
  1262  		c.Assert(chk.GetRow(0).IsNull(i), Equals, false)
  1263  		err1 := HashChunkSelected(sc, vecHash, chk, tps[i], i, buf, hasNull, sel, false)
  1264  		err2 := HashChunkRow(sc, rowHash[0], chk.GetRow(0), tps, defCausIdx[i:i+1], buf)
  1265  		err3 := HashChunkRow(sc, rowHash[1], chk.GetRow(1), tps, defCausIdx[i:i+1], buf)
  1266  		err4 := HashChunkRow(sc, rowHash[2], chk.GetRow(2), tps, defCausIdx[i:i+1], buf)
  1267  		c.Assert(err1, IsNil)
  1268  		c.Assert(err2, IsNil)
  1269  		c.Assert(err3, IsNil)
  1270  		c.Assert(err4, IsNil)
  1271  
  1272  		c.Assert(hasNull[0], Equals, false)
  1273  		c.Assert(hasNull[1], Equals, false)
  1274  		c.Assert(hasNull[2], Equals, false)
  1275  		c.Assert(vecHash[0].Sum64(), Equals, rowHash[0].Sum64())
  1276  		c.Assert(vecHash[1].Sum64(), Equals, rowHash[1].Sum64())
  1277  		c.Assert(vecHash[2].Sum64(), Equals, rowHash[2].Sum64())
  1278  	}
  1279  }