github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/containers/mock.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 containers 16 17 import ( 18 "fmt" 19 "math" 20 "math/rand" 21 "strconv" 22 23 "github.com/matrixorigin/matrixone/pkg/container/types" 24 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 25 ) 26 27 type MockDataProvider struct { 28 providers map[int]Vector 29 } 30 31 func NewMockDataProvider() *MockDataProvider { 32 return &MockDataProvider{ 33 providers: make(map[int]Vector), 34 } 35 } 36 37 func (p *MockDataProvider) Reset() { 38 p.providers = make(map[int]Vector) 39 } 40 41 func (p *MockDataProvider) AddColumnProvider(colIdx int, provider Vector) { 42 p.providers[colIdx] = provider 43 } 44 45 func (p *MockDataProvider) GetColumnProvider(colIdx int) Vector { 46 if p == nil { 47 return nil 48 } 49 return p.providers[colIdx] 50 } 51 52 func MockVector(t types.Type, rows int, unique bool, provider Vector) (vec Vector) { 53 vec = MakeVector(t, common.DefaultAllocator) 54 if provider != nil { 55 vec.Extend(provider) 56 return 57 } 58 59 switch t.Oid { 60 case types.T_bool: 61 for i := 0; i < rows; i++ { 62 if i%2 == 0 { 63 vec.Append(true, false) 64 } else { 65 vec.Append(false, false) 66 } 67 } 68 case types.T_bit: 69 if unique { 70 for i := 0; i < rows; i++ { 71 vec.Append(uint64(i), false) 72 } 73 } else { 74 for i := 0; i < rows; i++ { 75 ival := rand.Int63() 76 vec.Append(uint64(ival), false) 77 } 78 } 79 case types.T_int8: 80 if unique { 81 if int(rows) >= math.MaxInt8-math.MinInt8 { 82 panic("overflow") 83 } 84 for i := 0; i < rows; i++ { 85 vec.Append(math.MinInt8+int8(i), false) 86 } 87 } else { 88 for i := 0; i < rows; i++ { 89 ival := rand.Intn(math.MaxInt8) 90 vec.Append(int8(ival), false) 91 } 92 } 93 case types.T_int16: 94 if unique { 95 for i := 0; i < rows; i++ { 96 vec.Append(int16(i), false) 97 } 98 } else { 99 for i := 0; i < rows; i++ { 100 ival := rand.Intn(math.MaxInt16) 101 vec.Append(int16(ival), false) 102 } 103 } 104 case types.T_int32: 105 if unique { 106 for i := 0; i < rows; i++ { 107 vec.Append(int32(i), false) 108 } 109 } else { 110 for i := 0; i < rows; i++ { 111 ival := rand.Intn(math.MaxInt32) 112 vec.Append(int32(ival), false) 113 } 114 } 115 case types.T_int64: 116 if unique { 117 for i := 0; i < rows; i++ { 118 vec.Append(int64(i), false) 119 } 120 } else { 121 for i := 0; i < rows; i++ { 122 ival := rand.Intn(math.MaxInt64) 123 vec.Append(int64(ival), false) 124 } 125 } 126 case types.T_uint8: 127 if unique { 128 if int(rows) >= math.MaxUint8 { 129 panic("overflow") 130 } 131 for i := 0; i < rows; i++ { 132 vec.Append(uint8(i), false) 133 } 134 } else { 135 for i := 0; i < rows; i++ { 136 ival := rand.Intn(math.MaxUint8) 137 vec.Append(uint8(ival), false) 138 } 139 } 140 case types.T_uint16: 141 if unique { 142 for i := 0; i < rows; i++ { 143 vec.Append(uint16(i), false) 144 } 145 } else { 146 for i := 0; i < rows; i++ { 147 ival := rand.Intn(int(math.MaxInt16)) 148 vec.Append(uint16(ival), false) 149 } 150 } 151 case types.T_uint32: 152 if unique { 153 for i := 0; i < rows; i++ { 154 vec.Append(uint32(i), false) 155 } 156 } else { 157 for i := 0; i < rows; i++ { 158 ival := rand.Int31() 159 vec.Append(uint32(ival), false) 160 } 161 } 162 case types.T_uint64: 163 if unique { 164 for i := 0; i < rows; i++ { 165 vec.Append(uint64(i), false) 166 } 167 } else { 168 for i := 0; i < rows; i++ { 169 ival := rand.Int63() 170 vec.Append(uint64(ival), false) 171 } 172 } 173 case types.T_float32: 174 for i := 0; i < rows; i++ { 175 v1 := rand.Intn(math.MaxInt32) 176 v2 := rand.Intn(math.MaxInt32) + 1 177 vec.Append(float32(v1)/float32(v2), false) 178 } 179 case types.T_float64: 180 for i := 0; i < rows; i++ { 181 v1 := rand.Intn(math.MaxInt32) 182 v2 := rand.Intn(math.MaxInt32) + 1 183 vec.Append(float64(v1)/float64(v2), false) 184 } 185 case types.T_varchar, types.T_char, types.T_binary, types.T_varbinary, types.T_blob, types.T_text: 186 if unique { 187 for i := 0; i < rows; i++ { 188 s := fmt.Sprintf("%d-%d", i, 0) 189 vec.Append([]byte(s), false) 190 } 191 } else { 192 for i := 0; i < rows; i++ { 193 s := fmt.Sprintf("%d%d", i, rand.Intn(10000000)) 194 vec.Append([]byte(s), false) 195 } 196 } 197 case types.T_datetime: 198 for i := 1; i <= rows; i++ { 199 vec.Append(types.DatetimeFromClock(int32(i*100), 1, 1, 1, 1, 1, 1), false) 200 } 201 case types.T_date: 202 for i := 1; i <= rows; i++ { 203 vec.Append(types.DateFromCalendar(int32(i)*100, 1, 1), false) 204 } 205 case types.T_time: 206 for i := 1; i <= rows; i++ { 207 vec.Append(types.TimeFromClock(false, 1, 1, 1, 1), false) 208 } 209 case types.T_timestamp: 210 for i := int32(1); i <= int32(rows); i++ { 211 vec.Append(types.Timestamp(common.NextGlobalSeqNum()), false) 212 } 213 case types.T_decimal64: 214 for i := int32(1); i <= int32(rows); i++ { 215 d := types.Decimal64(common.NextGlobalSeqNum()) 216 vec.Append(d, false) 217 } 218 case types.T_decimal128: 219 for i := int32(1); i <= int32(rows); i++ { 220 d := types.Decimal128{B0_63: common.NextGlobalSeqNum(), B64_127: 0} 221 vec.Append(d, false) 222 } 223 case types.T_TS: 224 for i := int32(1); i <= int32(rows); i++ { 225 vec.Append(types.BuildTS(int64(i+1), uint32(i%16)), false) 226 } 227 case types.T_Rowid: 228 for i := int32(1); i <= int32(rows); i++ { 229 vec.Append(types.BuildTestRowid(int64(i+1), int64(i)), false) 230 } 231 case types.T_Blockid: 232 for i := int32(1); i <= int32(rows); i++ { 233 vec.Append(types.BuildTestBlockid(int64(i+1), int64(i)), false) 234 } 235 case types.T_enum: 236 for i := int32(1); i <= int32(rows); i++ { 237 vec.Append(types.Enum(i), false) 238 } 239 case types.T_array_float32: 240 // Used for creating MockBatch in DN txn_test.go 241 for i := float32(1); i <= float32(rows); i++ { 242 vec.Append(types.ArrayToBytes([]float32{i + 1, i}), false) 243 } 244 case types.T_array_float64: 245 for i := float64(1); i <= float64(rows); i++ { 246 vec.Append(types.ArrayToBytes([]float64{i + 1, i}), false) 247 } 248 default: 249 panic(fmt.Sprintf("%v %s", t.Oid, "not supported")) 250 } 251 return 252 } 253 254 func MockVector2(typ types.Type, rows int, offset int) Vector { 255 vec := MakeVector(typ, common.DefaultAllocator) 256 switch typ.Oid { 257 case types.T_bool: 258 for i := 0; i < rows; i++ { 259 if i%2 == 0 { 260 vec.Append(true, false) 261 } else { 262 vec.Append(false, false) 263 } 264 } 265 case types.T_bit: 266 for i := 0; i < rows; i++ { 267 vec.Append(uint64(i+offset), false) 268 } 269 case types.T_int8: 270 for i := 0; i < rows; i++ { 271 vec.Append(int8(i+offset), false) 272 } 273 case types.T_int16: 274 for i := 0; i < rows; i++ { 275 vec.Append(int16(i+offset), false) 276 } 277 case types.T_int32: 278 for i := 0; i < rows; i++ { 279 vec.Append(int32(i+offset), false) 280 } 281 case types.T_int64: 282 for i := 0; i < rows; i++ { 283 vec.Append(int64(i+offset), false) 284 } 285 case types.T_uint8: 286 for i := 0; i < rows; i++ { 287 vec.Append(uint8(i+offset), false) 288 } 289 case types.T_uint16: 290 for i := 0; i < rows; i++ { 291 vec.Append(uint16(i+offset), false) 292 } 293 case types.T_uint32: 294 for i := 0; i < rows; i++ { 295 vec.Append(uint32(i+offset), false) 296 } 297 case types.T_uint64: 298 for i := 0; i < rows; i++ { 299 vec.Append(uint64(i+offset), false) 300 } 301 case types.T_float32: 302 for i := 0; i < rows; i++ { 303 vec.Append(float32(i+offset), false) 304 } 305 case types.T_float64: 306 for i := 0; i < rows; i++ { 307 vec.Append(float64(i+offset), false) 308 } 309 case types.T_decimal64: 310 for i := 0; i < rows; i++ { 311 d := types.Decimal64(int64(i + offset)) 312 vec.Append(d, false) 313 } 314 case types.T_decimal128: 315 for i := 0; i < rows; i++ { 316 d := types.Decimal128{B0_63: uint64(i + offset), B64_127: 0} 317 vec.Append(d, false) 318 } 319 case types.T_timestamp: 320 for i := 0; i < rows; i++ { 321 vec.Append(types.Timestamp(i+offset), false) 322 } 323 case types.T_date: 324 for i := 0; i < rows; i++ { 325 vec.Append(types.Date(i+offset), false) 326 } 327 case types.T_time: 328 for i := 0; i < rows; i++ { 329 vec.Append(types.Time(i+offset), false) 330 } 331 case types.T_datetime: 332 for i := 0; i < rows; i++ { 333 vec.Append(types.Datetime(i+offset), false) 334 } 335 case types.T_enum: 336 for i := 0; i < rows; i++ { 337 vec.Append(types.Enum(i+offset), false) 338 } 339 case types.T_char, types.T_varchar, types.T_binary, 340 types.T_varbinary, types.T_blob, types.T_text: 341 for i := 0; i < rows; i++ { 342 vec.Append([]byte(strconv.Itoa(i+offset)), false) 343 } 344 case types.T_array_float32: 345 for i := 0; i < rows; i++ { 346 arr := types.ArrayToBytes[float32]([]float32{float32(i + offset)}) // 1-D vector 347 vec.Append(arr, false) 348 } 349 case types.T_array_float64: 350 for i := 0; i < rows; i++ { 351 arr := types.ArrayToBytes[float64]([]float64{float64(i + offset)}) // 1-D vector 352 vec.Append(arr, false) 353 } 354 default: 355 panic("not support") 356 } 357 return vec 358 } 359 360 func MockBatchWithAttrs(vecTypes []types.Type, attrs []string, rows int, uniqueIdx int, provider *MockDataProvider) (bat *Batch) { 361 bat = MockNullableBatch(vecTypes, rows, uniqueIdx, provider) 362 bat.Attrs = attrs 363 return 364 } 365 366 func MockNullableBatch(vecTypes []types.Type, rows int, uniqueIdx int, provider *MockDataProvider) (bat *Batch) { 367 bat = NewEmptyBatch() 368 for idx := range vecTypes { 369 attr := "mock_" + strconv.Itoa(idx) 370 unique := uniqueIdx == idx 371 vec := MockVector(vecTypes[idx], rows, unique, provider.GetColumnProvider(idx)) 372 bat.AddVector(attr, vec) 373 } 374 return bat 375 } 376 377 func MockBatch(vecTypes []types.Type, rows int, uniqueIdx int, provider *MockDataProvider) (bat *Batch) { 378 bat = NewEmptyBatch() 379 for idx := range vecTypes { 380 attr := "mock_" + strconv.Itoa(idx) 381 unique := uniqueIdx == idx 382 vec := MockVector(vecTypes[idx], rows, unique, provider.GetColumnProvider(idx)) 383 bat.AddVector(attr, vec) 384 } 385 return bat 386 }