github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/util/composite_primary_key_util_test.go (about)

     1  // Copyright 2021 Matrix Origin
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package util
    16  
    17  import (
    18  	"math"
    19  	"math/rand"
    20  	"strconv"
    21  	"testing"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    24  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    25  	"github.com/matrixorigin/matrixone/pkg/container/types"
    26  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    27  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    28  	"github.com/matrixorigin/matrixone/pkg/testutil"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestSimpleCompositePrimaryKey(t *testing.T) {
    33  	tests := []struct {
    34  		args []string
    35  	}{
    36  		{
    37  			args: []string{"a", "b", "c"},
    38  		},
    39  	}
    40  	for _, test := range tests {
    41  		args := test.args
    42  		cKeyName := BuildCompositePrimaryKeyColumnName(args)
    43  		s := SplitCompositePrimaryKeyColumnName(cKeyName)
    44  		require.Equal(t, len(args), len(s))
    45  		for i := range s {
    46  			require.Equal(t, args[i], s[i])
    47  		}
    48  	}
    49  }
    50  
    51  func TestFillCompositePKeyBatch(t *testing.T) {
    52  	var proc = testutil.NewProc()
    53  	columnSi := 10
    54  	rowCount := 10
    55  	bat, col, valueCount := MakeBatch(columnSi, rowCount, proc.Mp())
    56  	err := FillCompositePKeyBatch(bat, col, proc)
    57  	require.Equal(t, err, nil)
    58  	bs := vector.GetBytesVectorValues(bat.Vecs[len(bat.Vecs)-1])
    59  	tuples := make([]types.Tuple, 0)
    60  	for i := 0; i < len(bs); i++ {
    61  		tuple, err := types.Unpack(bs[i])
    62  		require.Equal(t, err, nil)
    63  		tuples = append(tuples, tuple)
    64  	}
    65  	names := SplitCompositePrimaryKeyColumnName(col.Name)
    66  	for i := 0; i < rowCount*len(names); i++ {
    67  		num, _ := strconv.Atoi(names[i/rowCount])
    68  		require.Equal(t, tuples[i%rowCount][i/rowCount], valueCount[num*rowCount+i%rowCount])
    69  	}
    70  }
    71  
    72  func MakeBatch(columnSi int, rowCount int, mp *mpool.MPool) (*batch.Batch, *plan.ColDef, map[int]interface{}) {
    73  	idx := columnSi
    74  	attrs := make([]string, 0, idx)
    75  
    76  	for i := 0; i < idx; i++ {
    77  		attrs = append(attrs, strconv.Itoa(i))
    78  	}
    79  
    80  	var keys []string
    81  	for i := 0; i < idx; i++ {
    82  		x := rand.Intn(2)
    83  		if x == 0 {
    84  			keys = append(keys, strconv.Itoa(i))
    85  		}
    86  	}
    87  	cPkeyName := BuildCompositePrimaryKeyColumnName(keys)
    88  	attrs = append(attrs, cPkeyName)
    89  
    90  	bat := batch.New(true, attrs)
    91  
    92  	valueCount := make(map[int]interface{})
    93  
    94  	for i := 0; i < idx; i++ {
    95  		bat.Vecs[i] = vector.New(types.Type{Oid: randType()})
    96  		randInsertValues(bat.Vecs[i], bat.Vecs[i].Typ.Oid, rowCount, valueCount, i*rowCount, mp)
    97  	}
    98  
    99  	bat.Vecs[idx] = vector.New(types.Type{Oid: types.T_varchar, Width: types.MaxVarcharLen})
   100  
   101  	colDef := &plan.ColDef{
   102  		Name: cPkeyName,
   103  	}
   104  	return bat, colDef, valueCount
   105  }
   106  
   107  func randType() types.T {
   108  	t := rand.Intn(17)
   109  	var vt types.T
   110  	switch t {
   111  	case 0:
   112  		vt = types.T_bool
   113  	case 1:
   114  		vt = types.T_int8
   115  	case 2:
   116  		vt = types.T_int16
   117  	case 3:
   118  		vt = types.T_int32
   119  	case 4:
   120  		vt = types.T_int64
   121  	case 5:
   122  		vt = types.T_uint8
   123  	case 6:
   124  		vt = types.T_uint16
   125  	case 7:
   126  		vt = types.T_uint32
   127  	case 8:
   128  		vt = types.T_uint64
   129  	case 9:
   130  		vt = types.T_date
   131  	case 10:
   132  		vt = types.T_datetime
   133  	case 11:
   134  		vt = types.T_timestamp
   135  	case 12:
   136  		vt = types.T_float32
   137  	case 13:
   138  		vt = types.T_float64
   139  	case 14:
   140  		vt = types.T_decimal64
   141  	case 15:
   142  		vt = types.T_decimal128
   143  	case 16:
   144  		vt = types.T_varchar
   145  	}
   146  	return vt
   147  }
   148  
   149  func randInsertValues(v *vector.Vector, t types.T, rowCount int, valueCount map[int]interface{}, valueBegin int, mp *mpool.MPool) {
   150  	switch t {
   151  	case types.T_bool:
   152  		vs := make([]bool, rowCount)
   153  		for i := 0; i < rowCount; i++ {
   154  			if i < rowCount/2 {
   155  				vs[i] = true
   156  				valueCount[valueBegin+i] = true
   157  			} else {
   158  				vs[i] = false
   159  				valueCount[valueBegin+i] = false
   160  			}
   161  		}
   162  		vector.AppendFixed(v, vs, mp)
   163  	case types.T_int8:
   164  		vs := make([]int8, rowCount)
   165  		for i := 0; i < rowCount; i++ {
   166  			vs[i] = randPositiveInt8()
   167  			valueCount[valueBegin+i] = vs[i]
   168  		}
   169  		vector.AppendFixed(v, vs, mp)
   170  	case types.T_int16:
   171  		vs := make([]int16, rowCount)
   172  		for i := 0; i < rowCount; i++ {
   173  			vs[i] = randPositiveInt16()
   174  			valueCount[valueBegin+i] = vs[i]
   175  		}
   176  		vector.AppendFixed(v, vs, mp)
   177  	case types.T_int32:
   178  		vs := make([]int32, rowCount)
   179  		for i := 0; i < rowCount; i++ {
   180  			vs[i] = randPositiveInt32()
   181  			valueCount[valueBegin+i] = vs[i]
   182  		}
   183  		vector.AppendFixed(v, vs, mp)
   184  	case types.T_int64:
   185  		vs := make([]int64, rowCount)
   186  		for i := 0; i < rowCount; i++ {
   187  			vs[i] = randPositiveInt64()
   188  			valueCount[valueBegin+i] = vs[i]
   189  		}
   190  		vector.AppendFixed(v, vs, mp)
   191  	case types.T_uint8:
   192  		vs := make([]uint8, rowCount)
   193  		for i := 0; i < rowCount; i++ {
   194  			vs[i] = randUint8()
   195  			valueCount[valueBegin+i] = vs[i]
   196  		}
   197  		vector.AppendFixed(v, vs, mp)
   198  	case types.T_uint16:
   199  		vs := make([]uint16, rowCount)
   200  		for i := 0; i < rowCount; i++ {
   201  			vs[i] = randUint16()
   202  			valueCount[valueBegin+i] = vs[i]
   203  		}
   204  		vector.AppendFixed(v, vs, mp)
   205  	case types.T_uint32:
   206  		vs := make([]uint32, rowCount)
   207  		for i := 0; i < rowCount; i++ {
   208  			vs[i] = randUint32()
   209  			valueCount[valueBegin+i] = vs[i]
   210  		}
   211  		vector.AppendFixed(v, vs, mp)
   212  	case types.T_uint64:
   213  		vs := make([]uint64, rowCount)
   214  		for i := 0; i < rowCount; i++ {
   215  			vs[i] = randUint64()
   216  			valueCount[valueBegin+i] = vs[i]
   217  		}
   218  		vector.AppendFixed(v, vs, mp)
   219  	case types.T_date:
   220  		vs := make([]types.Date, rowCount)
   221  		for i := 0; i < rowCount; i++ {
   222  			vs[i] = randDate()
   223  			valueCount[valueBegin+i] = vs[i]
   224  		}
   225  		vector.AppendFixed(v, vs, mp)
   226  	case types.T_time:
   227  		vs := make([]types.Time, rowCount)
   228  		for i := 0; i < rowCount; i++ {
   229  			vs[i] = randTime()
   230  			valueCount[valueBegin+i] = vs[i]
   231  		}
   232  		vector.AppendFixed(v, vs, mp)
   233  	case types.T_datetime:
   234  		vs := make([]types.Datetime, rowCount)
   235  		for i := 0; i < rowCount; i++ {
   236  			vs[i] = randDatetime()
   237  			valueCount[valueBegin+i] = vs[i]
   238  		}
   239  		vector.AppendFixed(v, vs, mp)
   240  	case types.T_timestamp:
   241  		vs := make([]types.Timestamp, rowCount)
   242  		for i := 0; i < rowCount; i++ {
   243  			vs[i] = randTimestamp()
   244  			valueCount[valueBegin+i] = vs[i]
   245  		}
   246  		vector.AppendFixed(v, vs, mp)
   247  	case types.T_float32:
   248  		vs := make([]float32, rowCount)
   249  		for i := 0; i < rowCount; i++ {
   250  			vs[i] = rand.Float32()
   251  			valueCount[valueBegin+i] = vs[i]
   252  		}
   253  		vector.AppendFixed(v, vs, mp)
   254  	case types.T_float64:
   255  		vs := make([]float64, rowCount)
   256  		for i := 0; i < rowCount; i++ {
   257  			vs[i] = rand.Float64()
   258  			valueCount[valueBegin+i] = vs[i]
   259  		}
   260  		vector.AppendFixed(v, vs, mp)
   261  	case types.T_decimal64:
   262  		vs := make([]types.Decimal64, rowCount)
   263  		for i := 0; i < rowCount; i++ {
   264  			vs[i] = randDecimal64()
   265  			valueCount[valueBegin+i] = vs[i]
   266  		}
   267  		vector.AppendFixed(v, vs, mp)
   268  	case types.T_decimal128:
   269  		vs := make([]types.Decimal128, rowCount)
   270  		for i := 0; i < rowCount; i++ {
   271  			vs[i] = randDecimal128()
   272  			valueCount[valueBegin+i] = vs[i]
   273  		}
   274  		vector.AppendFixed(v, vs, mp)
   275  	case types.T_varchar:
   276  		vs := make([][]byte, rowCount)
   277  		for i := 0; i < rowCount; i++ {
   278  			vs[i] = randStringType()
   279  			valueCount[valueBegin+i] = vs[i]
   280  		}
   281  		vector.AppendBytes(v, vs, mp)
   282  	}
   283  
   284  }
   285  
   286  func randPositiveInt8() int8 {
   287  	return int8(rand.Int31n(math.MaxInt8 + 1))
   288  }
   289  
   290  func randPositiveInt16() int16 {
   291  	return int16(rand.Int31n(math.MaxInt16 + 1))
   292  }
   293  
   294  func randPositiveInt32() int32 {
   295  	return rand.Int31()
   296  }
   297  
   298  func randPositiveInt64() int64 {
   299  	return rand.Int63()
   300  }
   301  
   302  func randUint8() uint8 {
   303  	return uint8(rand.Int31n(math.MaxUint8 + 1))
   304  }
   305  
   306  func randUint16() uint16 {
   307  	return uint16(rand.Int31n(math.MaxUint16 + 1))
   308  }
   309  
   310  func randUint32() uint32 {
   311  	return rand.Uint32()
   312  }
   313  
   314  func randUint64() uint64 {
   315  	return rand.Uint64()
   316  }
   317  
   318  func randDate() types.Date {
   319  	year := rand.Intn(types.MaxDateYear) + types.MinDateYear
   320  	month := rand.Intn(12) + 1
   321  	day := rand.Intn(int(types.LastDay(int32(year), uint8(month)))) + 1
   322  	return types.DateFromCalendar(int32(year), uint8(month), uint8(day))
   323  }
   324  
   325  func randTime() types.Time {
   326  	isNeg := false
   327  	if tmp := rand.Intn(2); tmp == 0 {
   328  		isNeg = true
   329  	}
   330  	hour := rand.Intn(2562047788)
   331  	minute := rand.Intn(60)
   332  	second := rand.Intn(60)
   333  	microSecond := rand.Intn(1e6)
   334  	return types.TimeFromClock(isNeg, uint64(hour), uint8(minute), uint8(second), uint32(microSecond))
   335  }
   336  
   337  func randDatetime() types.Datetime {
   338  	year := rand.Intn(types.MaxDatetimeYear) + types.MinDatetimeYear
   339  	month := rand.Intn(12) + 1
   340  	day := rand.Intn(int(types.LastDay(int32(year), uint8(month)))) + 1
   341  	hour := rand.Intn(24)
   342  	minute := rand.Intn(60)
   343  	second := rand.Intn(60)
   344  	microSecond := rand.Intn(1e6)
   345  	return types.DatetimeFromClock(int32(year), uint8(month), uint8(day), uint8(hour), uint8(minute), uint8(second), uint32(microSecond))
   346  }
   347  
   348  func randTimestamp() types.Timestamp {
   349  	year := rand.Intn(types.MaxDatetimeYear) + types.MinDatetimeYear
   350  	month := rand.Intn(12) + 1
   351  	day := rand.Intn(int(types.LastDay(int32(year), uint8(month)))) + 1
   352  	hour := rand.Intn(24)
   353  	minute := rand.Intn(60)
   354  	second := rand.Intn(60)
   355  	microSecond := rand.Intn(1e6)
   356  	return types.FromClockUTC(int32(year), uint8(month), uint8(day), uint8(hour), uint8(minute), uint8(second), uint32(microSecond))
   357  }
   358  
   359  func randDecimal64() types.Decimal64 {
   360  	decimal, _ := types.Decimal64FromFloat64(rand.Float64(), 20, 10)
   361  	return decimal
   362  }
   363  
   364  func randDecimal128() types.Decimal128 {
   365  	decimal, _ := types.Decimal128FromFloat64(rand.Float64(), 20, 10)
   366  	return decimal
   367  }
   368  
   369  func randStringType() []byte {
   370  	b := make([]byte, 1024)
   371  	rand.Read(b)
   372  	return b
   373  }