github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/codec/collation_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  	"hash"
    18  	"hash/crc32"
    19  	"hash/fnv"
    20  	"time"
    21  
    22  	. "github.com/whtcorpsinc/check"
    23  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    24  	"github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx"
    25  	"github.com/whtcorpsinc/milevadb/types"
    26  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    27  	"github.com/whtcorpsinc/milevadb/soliton/defCauslate"
    28  )
    29  
    30  var _ = SerialSuites(&testDefCauslationSuite{})
    31  
    32  type testDefCauslationSuite struct {
    33  }
    34  
    35  func prepareDefCauslationData() (int, *chunk.Chunk, *chunk.Chunk) {
    36  	tp := types.NewFieldType(allegrosql.TypeString)
    37  	tps := []*types.FieldType{tp}
    38  	chk1 := chunk.New(tps, 3, 3)
    39  	chk2 := chunk.New(tps, 3, 3)
    40  	chk1.Reset()
    41  	chk2.Reset()
    42  	chk1.DeferredCauset(0).AppendString("aaa")
    43  	chk2.DeferredCauset(0).AppendString("AAA")
    44  	chk1.DeferredCauset(0).AppendString("😜")
    45  	chk2.DeferredCauset(0).AppendString("😃")
    46  	chk1.DeferredCauset(0).AppendString("À")
    47  	chk2.DeferredCauset(0).AppendString("A")
    48  	return 3, chk1, chk2
    49  }
    50  
    51  func (s *testDefCauslationSuite) TestHashGroupKeyDefCauslation(c *C) {
    52  	defCauslate.SetNewDefCauslationEnabledForTest(true)
    53  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
    54  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
    55  	tp := types.NewFieldType(allegrosql.TypeString)
    56  	n, chk1, chk2 := prepareDefCauslationData()
    57  
    58  	tp.DefCauslate = "utf8_general_ci"
    59  	buf1 := make([][]byte, n)
    60  	buf2 := make([][]byte, n)
    61  	buf1, err := HashGroupKey(sc, n, chk1.DeferredCauset(0), buf1, tp)
    62  	c.Assert(err, IsNil)
    63  	buf2, err = HashGroupKey(sc, n, chk2.DeferredCauset(0), buf2, tp)
    64  	c.Assert(err, IsNil)
    65  	for i := 0; i < n; i++ {
    66  		c.Assert(len(buf1[i]), Equals, len(buf2[i]))
    67  		for j := range buf1 {
    68  			c.Assert(buf1[i][j], Equals, buf2[i][j])
    69  		}
    70  	}
    71  
    72  	tp.DefCauslate = "utf8_unicode_ci"
    73  	buf1 = make([][]byte, n)
    74  	buf2 = make([][]byte, n)
    75  	buf1, err = HashGroupKey(sc, n, chk1.DeferredCauset(0), buf1, tp)
    76  	c.Assert(err, IsNil)
    77  	buf2, err = HashGroupKey(sc, n, chk2.DeferredCauset(0), buf2, tp)
    78  	c.Assert(err, IsNil)
    79  	for i := 0; i < n; i++ {
    80  		c.Assert(len(buf1[i]), Equals, len(buf2[i]))
    81  		for j := range buf1 {
    82  			c.Assert(buf1[i][j], Equals, buf2[i][j])
    83  		}
    84  	}
    85  }
    86  
    87  func (s *testDefCauslationSuite) TestHashChunkRowDefCauslation(c *C) {
    88  	defCauslate.SetNewDefCauslationEnabledForTest(true)
    89  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
    90  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
    91  	tp := types.NewFieldType(allegrosql.TypeString)
    92  	tps := []*types.FieldType{tp}
    93  	n, chk1, chk2 := prepareDefCauslationData()
    94  	defcaus := []int{0}
    95  	buf := make([]byte, 1)
    96  
    97  	tp.DefCauslate = "bin"
    98  	for i := 0; i < n; i++ {
    99  		h1 := crc32.NewIEEE()
   100  		h2 := crc32.NewIEEE()
   101  		c.Assert(HashChunkRow(sc, h1, chk1.GetRow(i), tps, defcaus, buf), IsNil)
   102  		c.Assert(HashChunkRow(sc, h2, chk2.GetRow(i), tps, defcaus, buf), IsNil)
   103  		c.Assert(h1.Sum32(), Not(Equals), h2.Sum32())
   104  		h1.Reset()
   105  		h2.Reset()
   106  	}
   107  
   108  	tp.DefCauslate = "utf8_general_ci"
   109  	for i := 0; i < n; i++ {
   110  		h1 := crc32.NewIEEE()
   111  		h2 := crc32.NewIEEE()
   112  		c.Assert(HashChunkRow(sc, h1, chk1.GetRow(i), tps, defcaus, buf), IsNil)
   113  		c.Assert(HashChunkRow(sc, h2, chk2.GetRow(i), tps, defcaus, buf), IsNil)
   114  		c.Assert(h1.Sum32(), Equals, h2.Sum32())
   115  		h1.Reset()
   116  		h2.Reset()
   117  	}
   118  
   119  	tp.DefCauslate = "utf8_unicode_ci"
   120  	for i := 0; i < n; i++ {
   121  		h1 := crc32.NewIEEE()
   122  		h2 := crc32.NewIEEE()
   123  		c.Assert(HashChunkRow(sc, h1, chk1.GetRow(i), tps, defcaus, buf), IsNil)
   124  		c.Assert(HashChunkRow(sc, h2, chk2.GetRow(i), tps, defcaus, buf), IsNil)
   125  		c.Assert(h1.Sum32(), Equals, h2.Sum32())
   126  		h1.Reset()
   127  		h2.Reset()
   128  	}
   129  }
   130  
   131  func (s *testDefCauslationSuite) TestHashChunkDeferredCausetsDefCauslation(c *C) {
   132  	defCauslate.SetNewDefCauslationEnabledForTest(true)
   133  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
   134  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
   135  	tp := types.NewFieldType(allegrosql.TypeString)
   136  	n, chk1, chk2 := prepareDefCauslationData()
   137  	buf := make([]byte, 1)
   138  	hasNull := []bool{false, false, false}
   139  	h1s := []hash.Hash64{fnv.New64(), fnv.New64(), fnv.New64()}
   140  	h2s := []hash.Hash64{fnv.New64(), fnv.New64(), fnv.New64()}
   141  
   142  	tp.DefCauslate = "bin"
   143  	c.Assert(HashChunkDeferredCausets(sc, h1s, chk1, tp, 0, buf, hasNull), IsNil)
   144  	c.Assert(HashChunkDeferredCausets(sc, h2s, chk2, tp, 0, buf, hasNull), IsNil)
   145  	for i := 0; i < n; i++ {
   146  		c.Assert(h1s[i].Sum64(), Not(Equals), h2s[i].Sum64())
   147  		h1s[i].Reset()
   148  		h2s[i].Reset()
   149  	}
   150  
   151  	tp.DefCauslate = "utf8_general_ci"
   152  	c.Assert(HashChunkDeferredCausets(sc, h1s, chk1, tp, 0, buf, hasNull), IsNil)
   153  	c.Assert(HashChunkDeferredCausets(sc, h2s, chk2, tp, 0, buf, hasNull), IsNil)
   154  	for i := 0; i < n; i++ {
   155  		c.Assert(h1s[i].Sum64(), Equals, h2s[i].Sum64())
   156  	}
   157  
   158  	tp.DefCauslate = "utf8_unicode_ci"
   159  	c.Assert(HashChunkDeferredCausets(sc, h1s, chk1, tp, 0, buf, hasNull), IsNil)
   160  	c.Assert(HashChunkDeferredCausets(sc, h2s, chk2, tp, 0, buf, hasNull), IsNil)
   161  	for i := 0; i < n; i++ {
   162  		c.Assert(h1s[i].Sum64(), Equals, h2s[i].Sum64())
   163  	}
   164  }