github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/serial_test.go (about)

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