github.com/matrixorigin/matrixone@v0.7.0/pkg/testutil/util_make.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 testutil 16 17 import ( 18 "fmt" 19 "math/rand" 20 "time" 21 22 "github.com/matrixorigin/matrixone/pkg/common/mpool" 23 "github.com/matrixorigin/matrixone/pkg/container/nulls" 24 "github.com/matrixorigin/matrixone/pkg/container/types" 25 "github.com/matrixorigin/matrixone/pkg/container/vector" 26 ) 27 28 // All vectors generated by the Make Function, their memory is not allocated through the memory pool 29 // if you want to generate a vector in memory pool, use NewFunction to instead of MakeFunction. 30 var ( 31 TestUtilMp = mpool.MustNewZero() 32 33 MakeBoolVector = func(values []bool) *vector.Vector { 34 return makeVector(values, nil, boolType) 35 } 36 37 MakeBooleanlVector = func(values []bool, nsp []uint64) *vector.Vector { 38 return makeVector(values, nsp, boolType) 39 } 40 41 MakeInt64Vector = func(values []int64, nsp []uint64) *vector.Vector { 42 return makeVector(values, nsp, int64Type) 43 } 44 45 MakeInt32Vector = func(values []int32, nsp []uint64) *vector.Vector { 46 return makeVector(values, nsp, int32Type) 47 } 48 49 MakeInt16Vector = func(values []int16, nsp []uint64) *vector.Vector { 50 return makeVector(values, nsp, int16Type) 51 } 52 53 MakeInt8Vector = func(values []int8, nsp []uint64) *vector.Vector { 54 return makeVector(values, nsp, int8Type) 55 } 56 57 MakeUint64Vector = func(values []uint64, nsp []uint64) *vector.Vector { 58 return makeVector(values, nsp, uint64Type) 59 } 60 61 MakeUint32Vector = func(values []uint32, nsp []uint64) *vector.Vector { 62 return makeVector(values, nsp, uint32Type) 63 } 64 65 MakeUint16Vector = func(values []uint16, nsp []uint64) *vector.Vector { 66 return makeVector(values, nsp, uint16Type) 67 } 68 69 MakeUint8Vector = func(values []uint8, nsp []uint64) *vector.Vector { 70 return makeVector(values, nsp, uint8Type) 71 } 72 73 MakeFloat32Vector = func(values []float32, nsp []uint64) *vector.Vector { 74 return makeVector(values, nsp, float32Type) 75 } 76 77 MakeFloat64Vector = func(values []float64, nsp []uint64) *vector.Vector { 78 return makeVector(values, nsp, float64Type) 79 } 80 81 MakeCharVector = func(values []string, nsp []uint64) *vector.Vector { 82 return makeStringVector(values, nsp, charType) 83 } 84 85 MakeVarcharVector = func(values []string, nsp []uint64) *vector.Vector { 86 return makeStringVector(values, nsp, varcharType) 87 } 88 89 MakeBlobVector = func(values []string, nsp []uint64) *vector.Vector { 90 return makeStringVector(values, nsp, blobType) 91 } 92 MakeTextVector = func(values []string, nsp []uint64) *vector.Vector { 93 return makeStringVector(values, nsp, textType) 94 } 95 MakeDecimal64Vector = func(values []int64, nsp []uint64, _ types.Type) *vector.Vector { 96 cols := make([]types.Decimal64, len(values)) 97 for i, v := range values { 98 d, _ := types.InitDecimal64(v, 64, 0) 99 cols[i] = d 100 } 101 return makeVector(cols, nsp, decimal64Type) 102 } 103 104 MakeDecimal128Vector = func(values []int64, nsp []uint64, _ types.Type) *vector.Vector { 105 cols := make([]types.Decimal128, len(values)) 106 for i, v := range values { 107 d, _ := types.InitDecimal128(v, 64, 0) 108 cols[i] = d 109 } 110 return makeVector(cols, nsp, decimal128Type) 111 } 112 113 MakeDateVector = func(values []string, nsp []uint64) *vector.Vector { 114 ds := make([]types.Date, len(values)) 115 ns := nulls.Build(len(values), nsp...) 116 for i, s := range values { 117 if nulls.Contains(ns, uint64(i)) { 118 continue 119 } 120 d, err := types.ParseDateCast(s) 121 if err != nil { 122 panic(err) 123 } 124 ds[i] = d 125 } 126 return vector.NewWithFixed(types.T_date.ToType(), ds, ns, TestUtilMp) 127 } 128 129 MakeTimeVector = func(values []string, nsp []uint64) *vector.Vector { 130 ds := make([]types.Time, len(values)) 131 ns := nulls.Build(len(values), nsp...) 132 for i, s := range values { 133 if nulls.Contains(ns, uint64(i)) { 134 continue 135 } 136 d, err := types.ParseTime(s, 6) 137 if err != nil { 138 panic(err) 139 } 140 ds[i] = d 141 } 142 return vector.NewWithFixed(types.T_time.ToType(), ds, ns, TestUtilMp) 143 } 144 145 MakeDateTimeVector = func(values []string, nsp []uint64) *vector.Vector { 146 ds := make([]types.Datetime, len(values)) 147 ns := nulls.Build(len(values), nsp...) 148 for i, s := range values { 149 if nulls.Contains(ns, uint64(i)) { 150 continue 151 } 152 d, err := types.ParseDatetime(s, 6) 153 if err != nil { 154 panic(err) 155 } 156 ds[i] = d 157 } 158 return vector.NewWithFixed(types.T_datetime.ToType(), ds, ns, TestUtilMp) 159 } 160 161 MakeTimeStampVector = func(values []string, nsp []uint64) *vector.Vector { 162 ds := make([]types.Timestamp, len(values)) 163 ns := nulls.Build(len(values), nsp...) 164 for i, s := range values { 165 if nulls.Contains(ns, uint64(i)) { 166 continue 167 } 168 d, err := types.ParseTimestamp(time.Local, s, 6) 169 if err != nil { 170 panic(err) 171 } 172 ds[i] = d 173 } 174 return vector.NewWithFixed(types.T_timestamp.ToType(), ds, ns, TestUtilMp) 175 } 176 177 MakeUuidVector = func(values []types.Uuid, nsp []uint64) *vector.Vector { 178 ns := nulls.Build(len(values), nsp...) 179 return vector.NewWithFixed(uuidType, values, ns, TestUtilMp) 180 } 181 182 MakeUuidVectorByString = func(values []string, nsp []uint64) *vector.Vector { 183 ds := make([]types.Uuid, len(values)) 184 ns := nulls.Build(len(values), nsp...) 185 for i, s := range values { 186 if nulls.Contains(ns, uint64(i)) { 187 continue 188 } 189 d, err := types.ParseUuid(s) 190 if err != nil { 191 panic(err) 192 } 193 ds[i] = d 194 } 195 return vector.NewWithFixed(types.T_uuid.ToType(), ds, ns, TestUtilMp) 196 } 197 ) 198 199 // functions to make a scalar vector for test. 200 var ( 201 MakeScalarNull = func(typ types.T, length int) *vector.Vector { 202 vec := NewProc().AllocConstNullVector(typ.ToType(), length) 203 return vec 204 } 205 206 MakeScalarBool = func(v bool, length int) *vector.Vector { 207 return makeScalar(v, length, boolType) 208 } 209 210 MakeScalarInt64 = func(v int64, length int) *vector.Vector { 211 return makeScalar(v, length, int64Type) 212 } 213 214 MakeScalarInt32 = func(v int32, length int) *vector.Vector { 215 return makeScalar(v, length, int32Type) 216 } 217 218 MakeScalarInt16 = func(v int16, length int) *vector.Vector { 219 return makeScalar(v, length, int16Type) 220 } 221 222 MakeScalarInt8 = func(v int8, length int) *vector.Vector { 223 return makeScalar(v, length, int8Type) 224 } 225 226 MakeScalarUint64 = func(v uint64, length int) *vector.Vector { 227 return makeScalar(v, length, uint64Type) 228 } 229 230 MakeScalarUint3 = func(v uint32, length int) *vector.Vector { 231 return makeScalar(v, length, uint32Type) 232 } 233 234 MakeScalarUint16 = func(v uint16, length int) *vector.Vector { 235 return makeScalar(v, length, uint16Type) 236 } 237 238 MakeScalarUint8 = func(v uint8, length int) *vector.Vector { 239 return makeScalar(v, length, uint8Type) 240 } 241 242 MakeScalarFloat32 = func(v float32, length int) *vector.Vector { 243 return makeScalar(v, length, float32Type) 244 } 245 246 MakeScalarFloat64 = func(v float64, length int) *vector.Vector { 247 return makeScalar(v, length, float64Type) 248 } 249 250 MakeScalarChar = func(value string, length int) *vector.Vector { 251 return makeScalarString(value, length, charType) 252 } 253 254 MakeScalarVarchar = func(value string, length int) *vector.Vector { 255 return makeScalarString(value, length, varcharType) 256 } 257 258 MakeTextVarchar = func(value string, length int) *vector.Vector { 259 return makeScalarString(value, length, textType) 260 } 261 262 MakeScalarDate = func(value string, length int) *vector.Vector { 263 d, err := types.ParseDateCast(value) 264 if err != nil { 265 panic(err) 266 } 267 return vector.NewConstFixed(dateType, length, d, TestUtilMp) 268 } 269 270 MakeScalarTime = func(value string, length int) *vector.Vector { 271 d, err := types.ParseTime(value, 6) 272 if err != nil { 273 panic(err) 274 } 275 return vector.NewConstFixed(timeType, length, d, TestUtilMp) 276 } 277 278 MakeScalarDateTime = func(value string, length int) *vector.Vector { 279 d, err := types.ParseDatetime(value, 6) 280 if err != nil { 281 panic(err) 282 } 283 return vector.NewConstFixed(datetimeType, length, d, TestUtilMp) 284 } 285 286 MakeScalarTimeStamp = func(value string, length int) *vector.Vector { 287 d, err := types.ParseTimestamp(time.Local, value, 6) 288 if err != nil { 289 panic(err) 290 } 291 return vector.NewConstFixed(timestampType, length, d, TestUtilMp) 292 } 293 294 MakeScalarDecimal64 = func(v int64, length int, _ types.Type) *vector.Vector { 295 d, _ := types.InitDecimal64(v, 64, 0) 296 return vector.NewConstFixed(decimal64Type, length, d, TestUtilMp) 297 } 298 299 MakeScalarDecimal128 = func(v uint64, length int, _ types.Type) *vector.Vector { 300 d, _ := types.InitDecimal128UsingUint(v, 64, 0) 301 return vector.NewConstFixed(decimal64Type, length, d, TestUtilMp) 302 } 303 304 MakeScalarDecimal128ByFloat64 = func(v float64, length int, _ types.Type) *vector.Vector { 305 val := fmt.Sprintf("%f", v) 306 _, scale, err := types.ParseStringToDecimal128WithoutTable(val) 307 if err != nil { 308 panic(err) 309 } 310 dec128Val, err := types.ParseStringToDecimal128(val, 34, scale, false) 311 if err != nil { 312 panic(err) 313 } 314 return vector.NewConstFixed(decimal128Type, length, dec128Val, TestUtilMp) 315 } 316 ) 317 318 func makeVector[T types.FixedSizeT](values []T, nsp []uint64, typ types.Type) *vector.Vector { 319 ns := nulls.Build(len(values), nsp...) 320 return vector.NewWithFixed(typ, values, ns, TestUtilMp) 321 } 322 323 func makeStringVector(values []string, nsp []uint64, typ types.Type) *vector.Vector { 324 if nsp == nil { 325 return vector.NewWithStrings(typ, values, nil, TestUtilMp) 326 } else { 327 vnsp := nulls.Build(len(values), nsp...) 328 return vector.NewWithStrings(typ, values, vnsp, TestUtilMp) 329 } 330 } 331 332 func makeScalar[T types.FixedSizeT](value T, length int, typ types.Type) *vector.Vector { 333 return vector.NewConstFixed(typ, length, value, TestUtilMp) 334 } 335 336 func makeScalarString(value string, length int, typ types.Type) *vector.Vector { 337 return vector.NewConstString(typ, length, value, TestUtilMp) 338 } 339 340 func MakeDecimal64ArrByInt64Arr(input []int64) []types.Decimal64 { 341 ret := make([]types.Decimal64, len(input)) 342 for i, v := range input { 343 d, _ := types.InitDecimal64(v, 64, 0) 344 ret[i] = d 345 } 346 347 return ret 348 } 349 350 func MakeDecimal64ArrByFloat64Arr(input []float64) []types.Decimal64 { 351 ret := make([]types.Decimal64, len(input)) 352 for i, v := range input { 353 d, _ := types.Decimal64FromFloat64(v, 64, 10) 354 ret[i] = d 355 } 356 357 return ret 358 } 359 360 func MakeDecimal128ArrByInt64Arr(input []int64) []types.Decimal128 { 361 ret := make([]types.Decimal128, len(input)) 362 for i, v := range input { 363 d, _ := types.InitDecimal128(v, 64, 0) 364 ret[i] = d 365 } 366 367 return ret 368 } 369 370 func MakeDecimal128ArrByFloat64Arr(input []float64) []types.Decimal128 { 371 ret := make([]types.Decimal128, len(input)) 372 for i, v := range input { 373 d, _ := types.Decimal128FromFloat64(v, 64, 10) 374 ret[i] = d 375 } 376 377 return ret 378 } 379 380 func MakeRandomStrings(cardinality, targetRows int) []string { 381 chars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()" 382 charsLen := len(chars) 383 384 // mock random strings 385 dataset := make([][]byte, cardinality) 386 for i := range dataset { 387 randLen := rand.Intn(charsLen) + 1 388 dataset[i] = make([]byte, randLen) 389 for j := range dataset[i] { 390 dataset[i][j] = chars[rand.Intn(charsLen)] 391 } 392 } 393 394 data := make([]string, 0) 395 for { 396 for i := range dataset { 397 n := rand.Intn(10) + 1 398 for j := 0; j < n; j++ { 399 data = append(data, string(append([]byte{}, dataset[i]...))) 400 } 401 } 402 if len(data) >= targetRows { 403 break 404 } 405 } 406 return data 407 }