github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/test/common/utils.go (about) 1 package common 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "encoding/json" 7 "fmt" 8 "log" 9 "math" 10 "math/rand" 11 "strconv" 12 "strings" 13 "time" 14 15 "github.com/x448/float16" 16 17 "github.com/milvus-io/milvus-sdk-go/v2/entity" 18 ) 19 20 // const default value for test 21 const ( 22 DefaultIntFieldName = "int64" 23 DefaultInt8FieldName = "int8" 24 DefaultInt16FieldName = "int16" 25 DefaultInt32FieldName = "int32" 26 DefaultBoolFieldName = "bool" 27 DefaultFloatFieldName = "float" 28 DefaultDoubleFieldName = "double" 29 DefaultVarcharFieldName = "varchar" 30 DefaultJSONFieldName = "json" 31 DefaultArrayFieldName = "array" 32 DefaultFloatVecFieldName = "floatVec" 33 DefaultBinaryVecFieldName = "binaryVec" 34 DefaultFloat16VecFieldName = "fp16Vec" 35 DefaultBFloat16VecFieldName = "bf16Vec" 36 DefaultSparseVecFieldName = "sparseVec" 37 DefaultDynamicNumberField = "dynamicNumber" 38 DefaultDynamicStringField = "dynamicString" 39 DefaultDynamicBoolField = "dynamicBool" 40 DefaultDynamicListField = "dynamicList" 41 DefaultBoolArrayField = "boolArray" 42 DefaultInt8ArrayField = "int8Array" 43 DefaultInt16ArrayField = "int16Array" 44 DefaultInt32ArrayField = "int32Array" 45 DefaultInt64ArrayField = "int64Array" 46 DefaultFloatArrayField = "floatArray" 47 DefaultDoubleArrayField = "doubleArray" 48 DefaultVarcharArrayField = "varcharArray" 49 RowCount = "row_count" 50 DefaultTimeout = 120 51 DefaultDim = int64(128) 52 DefaultShards = int32(2) 53 DefaultNb = 3000 54 DefaultNq = 5 55 //DefaultNq = 1 56 DefaultTopK = 10 57 TestCapacity = 100 // default array field capacity 58 TestMaxLen = 100 // default varchar field max length 59 ) 60 61 // const default value from milvus 62 const ( 63 MaxPartitionNum = 4096 64 DefaultDynamicFieldName = "$meta" 65 QueryCountFieldName = "count(*)" 66 DefaultPartition = "_default" 67 DefaultIndexName = "_default_idx_102" 68 DefaultIndexNameBinary = "_default_idx_100" 69 DefaultRgName = "__default_resource_group" 70 DefaultDb = "default" 71 DefaultConsistencyLevel = entity.ClBounded 72 MaxDim = 32768 73 MaxLength = int64(65535) 74 MaxCollectionNameLen = 255 75 DefaultRgCapacity = 1000000 76 RetentionDuration = 40 // common.retentionDuration 77 MaxCapacity = 4096 // max array capacity 78 DefaultPartitionNum = 16 // default num_partitions 79 MaxTopK = 16384 80 MaxVectorFieldNum = 4 81 DefaultBatchSize = 1000 82 ) 83 84 var IndexStateValue = map[string]int32{ 85 "IndexStateNone": 0, 86 "Unissued": 1, 87 "InProgress": 2, 88 "Finished": 3, 89 "Failed": 4, 90 "Retry": 5, 91 } 92 93 var ArrayFieldType = []entity.FieldType{ 94 entity.FieldTypeBool, 95 entity.FieldTypeInt8, 96 entity.FieldTypeInt16, 97 entity.FieldTypeInt32, 98 entity.FieldTypeInt64, 99 entity.FieldTypeFloat, 100 entity.FieldTypeDouble, 101 //entity.FieldTypeVarChar, //t.Skip("Waiting for varchar bytes array fixed") 102 } 103 104 var AllArrayFieldsName = []string{ 105 DefaultBoolArrayField, 106 DefaultInt8ArrayField, 107 DefaultInt16ArrayField, 108 DefaultInt32ArrayField, 109 DefaultInt64ArrayField, 110 DefaultFloatArrayField, 111 DefaultDoubleArrayField, 112 DefaultVarcharArrayField, 113 } 114 115 var AllVectorsFieldsName = []string{ 116 DefaultFloatVecFieldName, 117 DefaultBinaryVecFieldName, 118 DefaultFloat16VecFieldName, 119 DefaultBFloat16VecFieldName, 120 } 121 122 var AllFloatVectorsFieldNames = []string{ 123 DefaultFloatVecFieldName, 124 DefaultFloat16VecFieldName, 125 DefaultBFloat16VecFieldName, 126 } 127 128 // return all fields name 129 func GetAllFieldsName(enableDynamicField bool, onlyScalar bool) []string { 130 allFieldName := []string{ 131 DefaultIntFieldName, 132 DefaultBoolFieldName, 133 DefaultInt8FieldName, 134 DefaultInt16FieldName, 135 DefaultInt32FieldName, 136 DefaultFloatFieldName, 137 DefaultDoubleFieldName, 138 DefaultVarcharFieldName, 139 DefaultJSONFieldName, 140 } 141 allFieldName = append(allFieldName, AllVectorsFieldsName...) 142 if enableDynamicField { 143 allFieldName = append(allFieldName, DefaultDynamicFieldName) 144 } 145 if onlyScalar { 146 return allFieldName 147 } 148 allFieldName = append(allFieldName, AllArrayFieldsName...) 149 return allFieldName 150 } 151 152 var r *rand.Rand 153 154 func init() { 155 r = rand.New(rand.NewSource(time.Now().UnixNano())) 156 } 157 158 // --- common utils --- 159 var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") 160 161 // GenRandomString gen random string 162 func GenRandomString(n int) string { 163 b := make([]rune, n) 164 for i := range b { 165 b[i] = letterRunes[r.Intn(len(letterRunes))] 166 } 167 return string(b) 168 } 169 170 // GenLongString gen invalid long string 171 func GenLongString(n int) string { 172 var builder strings.Builder 173 longString := "a" 174 for i := 0; i < n; i++ { 175 builder.WriteString(longString) 176 } 177 return builder.String() 178 } 179 180 // ColumnIndexFunc generate column index 181 func ColumnIndexFunc(data []entity.Column, fieldName string) int { 182 for index, column := range data { 183 if column.Name() == fieldName { 184 return index 185 } 186 } 187 return -1 188 } 189 190 func GenFloatVector(dim int64) []float32 { 191 vector := make([]float32, 0, dim) 192 for j := 0; j < int(dim); j++ { 193 vector = append(vector, rand.Float32()) 194 } 195 return vector 196 } 197 198 func GenFloat16Vector(dim int64) []byte { 199 ret := make([]byte, dim*2) 200 for i := 0; i < int(dim); i++ { 201 v := float16.Fromfloat32(rand.Float32()).Bits() 202 binary.LittleEndian.PutUint16(ret[i*2:], v) 203 } 204 return ret 205 } 206 207 func GenBFloat16Vector(dim int64) []byte { 208 ret16 := make([]uint16, 0, dim) 209 for i := 0; i < int(dim); i++ { 210 f := rand.Float32() 211 bits := math.Float32bits(f) 212 bits >>= 16 213 bits &= 0x7FFF 214 ret16 = append(ret16, uint16(bits)) 215 } 216 ret := make([]byte, len(ret16)*2) 217 for i, value := range ret16 { 218 binary.LittleEndian.PutUint16(ret[i*2:], value) 219 } 220 return ret 221 } 222 223 func GenBinaryVector(dim int64) []byte { 224 vector := make([]byte, dim/8) 225 rand.Read(vector) 226 return vector 227 } 228 229 func GenSparseVector(maxLen int) entity.SparseEmbedding { 230 length := 1 + rand.Intn(1+maxLen) 231 positions := make([]uint32, length) 232 values := make([]float32, length) 233 for i := 0; i < length; i++ { 234 positions[i] = uint32(2*i + 1) 235 values[i] = rand.Float32() 236 } 237 vector, err := entity.NewSliceSparseEmbedding(positions, values) 238 if err != nil { 239 log.Fatalf("Generate vector failed %s", err) 240 } 241 return vector 242 } 243 244 // --- common utils --- 245 246 // --- gen fields --- 247 248 // GenDefaultFields gen default fields with int64, float, floatVector field 249 func GenDefaultFields(autoID bool) []*entity.Field { 250 intField := GenField(DefaultIntFieldName, entity.FieldTypeInt64, WithIsPrimaryKey(true), WithAutoID(autoID)) 251 floatField := GenField(DefaultFloatFieldName, entity.FieldTypeFloat) 252 floatVecField := GenField(DefaultFloatVecFieldName, entity.FieldTypeFloatVector, WithDim(DefaultDim)) 253 fields := []*entity.Field{ 254 intField, floatField, floatVecField, 255 } 256 return fields 257 } 258 259 // GenDefaultBinaryFields gen default binary fields with int64, float, binaryVector field 260 func GenDefaultBinaryFields(autoID bool, dim int64) []*entity.Field { 261 intField := GenField(DefaultIntFieldName, entity.FieldTypeInt64, WithIsPrimaryKey(true), WithAutoID(autoID)) 262 floatField := GenField(DefaultFloatFieldName, entity.FieldTypeFloat) 263 binaryVecField := GenField(DefaultBinaryVecFieldName, entity.FieldTypeBinaryVector, WithDim(dim)) 264 265 fields := []*entity.Field{ 266 intField, floatField, binaryVecField, 267 } 268 return fields 269 } 270 271 // GenDefaultVarcharFields gen default fields with varchar, floatVector field 272 func GenDefaultVarcharFields(autoID bool) []*entity.Field { 273 varcharField := GenField(DefaultVarcharFieldName, entity.FieldTypeVarChar, WithIsPrimaryKey(true), WithAutoID(autoID), WithMaxLength(MaxLength)) 274 binaryVecField := GenField(DefaultBinaryVecFieldName, entity.FieldTypeBinaryVector, WithDim(DefaultDim)) 275 fields := []*entity.Field{ 276 varcharField, binaryVecField, 277 } 278 return fields 279 } 280 281 func GenAllArrayFields() []*entity.Field { 282 return GenAllArrayFieldsWithCapacity(TestCapacity) 283 } 284 285 // GenAllArrayFieldsWithCapacity GenAllArrayFields gen all array fields 286 func GenAllArrayFieldsWithCapacity(capacity int64) []*entity.Field { 287 fields := []*entity.Field{ 288 GenField(DefaultBoolArrayField, entity.FieldTypeArray, WithElementType(entity.FieldTypeBool), WithMaxCapacity(capacity)), 289 GenField(DefaultInt8ArrayField, entity.FieldTypeArray, WithElementType(entity.FieldTypeInt8), WithMaxCapacity(capacity)), 290 GenField(DefaultInt16ArrayField, entity.FieldTypeArray, WithElementType(entity.FieldTypeInt16), WithMaxCapacity(capacity)), 291 GenField(DefaultInt32ArrayField, entity.FieldTypeArray, WithElementType(entity.FieldTypeInt32), WithMaxCapacity(capacity)), 292 GenField(DefaultInt64ArrayField, entity.FieldTypeArray, WithElementType(entity.FieldTypeInt64), WithMaxCapacity(capacity)), 293 GenField(DefaultFloatArrayField, entity.FieldTypeArray, WithElementType(entity.FieldTypeFloat), WithMaxCapacity(capacity)), 294 GenField(DefaultDoubleArrayField, entity.FieldTypeArray, WithElementType(entity.FieldTypeDouble), WithMaxCapacity(capacity)), 295 GenField(DefaultVarcharArrayField, entity.FieldTypeArray, WithElementType(entity.FieldTypeVarChar), WithMaxLength(TestMaxLen), WithMaxCapacity(capacity)), 296 } 297 return fields 298 } 299 300 // GenAllFields gen fields with all scala field types 301 func GenAllFields() []*entity.Field { 302 allFields := []*entity.Field{ 303 GenField(DefaultIntFieldName, entity.FieldTypeInt64, WithIsPrimaryKey(true)), // int64 304 GenField(DefaultBoolFieldName, entity.FieldTypeBool), // bool 305 GenField(DefaultInt8FieldName, entity.FieldTypeInt8), // int8 306 GenField(DefaultInt16FieldName, entity.FieldTypeInt16), // int16 307 GenField(DefaultInt32FieldName, entity.FieldTypeInt32), // int32 308 GenField(DefaultFloatFieldName, entity.FieldTypeFloat), // float 309 GenField(DefaultDoubleFieldName, entity.FieldTypeDouble), // double 310 GenField(DefaultVarcharFieldName, entity.FieldTypeVarChar, WithMaxLength(MaxLength)), // varchar 311 GenField(DefaultJSONFieldName, entity.FieldTypeJSON), // json 312 GenField(DefaultFloatVecFieldName, entity.FieldTypeFloatVector, WithDim(DefaultDim)), // float vector 313 GenField(DefaultFloat16VecFieldName, entity.FieldTypeFloat16Vector, WithDim(DefaultDim)), // float16 vector 314 GenField(DefaultBFloat16VecFieldName, entity.FieldTypeBFloat16Vector, WithDim(DefaultDim)), // bf16 vector 315 GenField(DefaultBinaryVecFieldName, entity.FieldTypeBinaryVector, WithDim(DefaultDim)), // binary vector 316 } 317 allFields = append(allFields, GenAllArrayFields()...) 318 return allFields 319 } 320 321 // --- gen fields --- 322 323 // --- gen column data --- 324 325 // GenDefaultColumnData gen default column with data 326 func GenDefaultColumnData(start int, nb int, dim int64) (entity.Column, entity.Column, entity.Column) { 327 return GenColumnData(start, nb, entity.FieldTypeInt64, DefaultIntFieldName), 328 GenColumnData(start, nb, entity.FieldTypeFloat, DefaultFloatFieldName), 329 GenColumnData(start, nb, entity.FieldTypeFloatVector, DefaultFloatVecFieldName, WithVectorDim(dim)) 330 } 331 332 // GenColumnData GenColumnDataOption 333 func GenColumnData(start int, nb int, fieldType entity.FieldType, fieldName string, opts ...GenColumnDataOption) entity.Column { 334 opt := &genDataOpt{} 335 for _, o := range opts { 336 o(opt) 337 } 338 switch fieldType { 339 case entity.FieldTypeInt64: 340 int64Values := make([]int64, 0, nb) 341 for i := start; i < start+nb; i++ { 342 int64Values = append(int64Values, int64(i)) 343 } 344 return entity.NewColumnInt64(fieldName, int64Values) 345 346 case entity.FieldTypeInt8: 347 int8Values := make([]int8, 0, nb) 348 for i := start; i < start+nb; i++ { 349 int8Values = append(int8Values, int8(i)) 350 } 351 return entity.NewColumnInt8(fieldName, int8Values) 352 353 case entity.FieldTypeInt16: 354 int16Values := make([]int16, 0, nb) 355 for i := start; i < start+nb; i++ { 356 int16Values = append(int16Values, int16(i)) 357 } 358 return entity.NewColumnInt16(fieldName, int16Values) 359 360 case entity.FieldTypeInt32: 361 int32Values := make([]int32, 0, nb) 362 for i := start; i < start+nb; i++ { 363 int32Values = append(int32Values, int32(i)) 364 } 365 return entity.NewColumnInt32(fieldName, int32Values) 366 367 case entity.FieldTypeBool: 368 boolValues := make([]bool, 0, nb) 369 for i := start; i < start+nb; i++ { 370 boolValues = append(boolValues, i/2 == 0) 371 } 372 return entity.NewColumnBool(fieldName, boolValues) 373 374 case entity.FieldTypeFloat: 375 floatValues := make([]float32, 0, nb) 376 for i := start; i < start+nb; i++ { 377 floatValues = append(floatValues, float32(i)) 378 } 379 return entity.NewColumnFloat(fieldName, floatValues) 380 381 case entity.FieldTypeDouble: 382 floatValues := make([]float64, 0, nb) 383 for i := start; i < start+nb; i++ { 384 floatValues = append(floatValues, float64(i)) 385 } 386 return entity.NewColumnDouble(fieldName, floatValues) 387 388 case entity.FieldTypeVarChar: 389 varcharValues := make([]string, 0, nb) 390 for i := start; i < start+nb; i++ { 391 varcharValues = append(varcharValues, strconv.Itoa(i)) 392 } 393 return entity.NewColumnVarChar(fieldName, varcharValues) 394 395 case entity.FieldTypeArray: 396 return GenArrayColumnData(start, nb, fieldName, opts...) 397 398 case entity.FieldTypeFloatVector: 399 vecFloatValues := make([][]float32, 0, nb) 400 for i := start; i < start+nb; i++ { 401 vec := GenFloatVector(opt.dim) 402 vecFloatValues = append(vecFloatValues, vec) 403 } 404 return entity.NewColumnFloatVector(fieldName, int(opt.dim), vecFloatValues) 405 case entity.FieldTypeBinaryVector: 406 binaryVectors := make([][]byte, 0, nb) 407 for i := 0; i < nb; i++ { 408 vec := GenBinaryVector(opt.dim) 409 binaryVectors = append(binaryVectors, vec) 410 } 411 return entity.NewColumnBinaryVector(fieldName, int(opt.dim), binaryVectors) 412 case entity.FieldTypeFloat16Vector: 413 fp16Vectors := make([][]byte, 0, nb) 414 for i := start; i < start+nb; i++ { 415 vec := GenFloat16Vector(opt.dim) 416 fp16Vectors = append(fp16Vectors, vec) 417 } 418 return entity.NewColumnFloat16Vector(fieldName, int(opt.dim), fp16Vectors) 419 case entity.FieldTypeBFloat16Vector: 420 bf16Vectors := make([][]byte, 0, nb) 421 for i := start; i < start+nb; i++ { 422 vec := GenBFloat16Vector(opt.dim) 423 bf16Vectors = append(bf16Vectors, vec) 424 } 425 return entity.NewColumnBFloat16Vector(fieldName, int(opt.dim), bf16Vectors) 426 case entity.FieldTypeSparseVector: 427 vectors := make([]entity.SparseEmbedding, 0, nb) 428 for i := start; i < start+nb; i++ { 429 vec := GenSparseVector(opt.maxLenSparse) 430 vectors = append(vectors, vec) 431 } 432 return entity.NewColumnSparseVectors(fieldName, vectors) 433 default: 434 return nil 435 } 436 } 437 438 func GenArrayColumnData(start int, nb int, fieldName string, opts ...GenColumnDataOption) entity.Column { 439 opt := &genDataOpt{} 440 for _, o := range opts { 441 o(opt) 442 } 443 eleType := opt.ElementType 444 capacity := int(opt.capacity) 445 switch eleType { 446 case entity.FieldTypeBool: 447 boolValues := make([][]bool, 0, nb) 448 for i := start; i < start+nb; i++ { 449 boolArray := make([]bool, 0, capacity) 450 for j := 0; j < capacity; j++ { 451 boolArray = append(boolArray, i%2 == 0) 452 } 453 boolValues = append(boolValues, boolArray) 454 } 455 return entity.NewColumnBoolArray(fieldName, boolValues) 456 case entity.FieldTypeInt8: 457 int8Values := make([][]int8, 0, nb) 458 for i := start; i < start+nb; i++ { 459 int8Array := make([]int8, 0, capacity) 460 for j := 0; j < capacity; j++ { 461 int8Array = append(int8Array, int8(i+j)) 462 } 463 int8Values = append(int8Values, int8Array) 464 } 465 return entity.NewColumnInt8Array(fieldName, int8Values) 466 case entity.FieldTypeInt16: 467 int16Values := make([][]int16, 0, nb) 468 for i := start; i < start+nb; i++ { 469 int16Array := make([]int16, 0, capacity) 470 for j := 0; j < capacity; j++ { 471 int16Array = append(int16Array, int16(i+j)) 472 } 473 int16Values = append(int16Values, int16Array) 474 } 475 return entity.NewColumnInt16Array(fieldName, int16Values) 476 case entity.FieldTypeInt32: 477 int32Values := make([][]int32, 0, nb) 478 for i := start; i < start+nb; i++ { 479 int32Array := make([]int32, 0, capacity) 480 for j := 0; j < capacity; j++ { 481 int32Array = append(int32Array, int32(i+j)) 482 } 483 int32Values = append(int32Values, int32Array) 484 } 485 return entity.NewColumnInt32Array(fieldName, int32Values) 486 case entity.FieldTypeInt64: 487 int64Values := make([][]int64, 0, nb) 488 for i := start; i < start+nb; i++ { 489 int64Array := make([]int64, 0, capacity) 490 for j := 0; j < capacity; j++ { 491 int64Array = append(int64Array, int64(i+j)) 492 } 493 int64Values = append(int64Values, int64Array) 494 } 495 return entity.NewColumnInt64Array(fieldName, int64Values) 496 case entity.FieldTypeFloat: 497 floatValues := make([][]float32, 0, nb) 498 for i := start; i < start+nb; i++ { 499 floatArray := make([]float32, 0, capacity) 500 for j := 0; j < capacity; j++ { 501 floatArray = append(floatArray, float32(i+j)) 502 } 503 floatValues = append(floatValues, floatArray) 504 } 505 return entity.NewColumnFloatArray(fieldName, floatValues) 506 case entity.FieldTypeDouble: 507 doubleValues := make([][]float64, 0, nb) 508 for i := start; i < start+nb; i++ { 509 doubleArray := make([]float64, 0, capacity) 510 for j := 0; j < capacity; j++ { 511 doubleArray = append(doubleArray, float64(i+j)) 512 } 513 doubleValues = append(doubleValues, doubleArray) 514 } 515 return entity.NewColumnDoubleArray(fieldName, doubleValues) 516 case entity.FieldTypeVarChar: 517 varcharValues := make([][][]byte, 0, nb) 518 for i := start; i < start+nb; i++ { 519 varcharArray := make([][]byte, 0, capacity) 520 for j := 0; j < capacity; j++ { 521 var buf bytes.Buffer 522 buf.WriteString(strconv.Itoa(i + j)) 523 varcharArray = append(varcharArray, buf.Bytes()) 524 } 525 varcharValues = append(varcharValues, varcharArray) 526 } 527 return entity.NewColumnVarCharArray(fieldName, varcharValues) 528 default: 529 return nil 530 } 531 } 532 533 type JSONStruct struct { 534 Number int32 `json:"number" milvus:"name:number"` 535 String string `json:"string" milvus:"name:string"` 536 Bool bool `json:"bool" milvus:"name:bool"` 537 List []int64 `json:"list" milvus:"name:list"` 538 } 539 540 // GenDefaultJSONData gen default column with data 541 func GenDefaultJSONData(columnName string, start int, nb int) *entity.ColumnJSONBytes { 542 jsonValues := make([][]byte, 0, nb) 543 var m interface{} 544 for i := start; i < start+nb; i++ { 545 // kv value 546 if i < (start+nb)/2 { 547 if i%2 == 0 { 548 m = JSONStruct{ 549 String: strconv.Itoa(i), 550 Bool: i%2 == 0, 551 } 552 } else { 553 m = JSONStruct{ 554 Number: int32(i), 555 String: strconv.Itoa(i), 556 Bool: i%2 == 0, 557 List: []int64{int64(i), int64(i + 1)}, 558 } 559 } 560 } else { 561 // int, float, string, list 562 switch i % 4 { 563 case 0: 564 m = i 565 case 1: 566 m = float32(i) 567 case 2: 568 m = strconv.Itoa(i) 569 case 3: 570 m = []int64{int64(i), int64(i + 1)} 571 } 572 } 573 bs, err := json.Marshal(&m) 574 if err != nil { 575 log.Fatalf("Marshal json field failed: %s", err) 576 } 577 jsonValues = append(jsonValues, bs) 578 } 579 jsonColumn := entity.NewColumnJSONBytes(columnName, jsonValues) 580 return jsonColumn 581 } 582 583 // GenDefaultBinaryData gen default binary collection data 584 func GenDefaultBinaryData(start int, nb int, dim int64) (entity.Column, entity.Column, entity.Column) { 585 return GenColumnData(start, nb, entity.FieldTypeInt64, DefaultIntFieldName), 586 GenColumnData(start, nb, entity.FieldTypeFloat, DefaultFloatFieldName), 587 GenColumnData(start, nb, entity.FieldTypeBinaryVector, DefaultBinaryVecFieldName, WithVectorDim(dim)) 588 } 589 590 func GenDefaultVarcharData(start int, nb int, dim int64) (entity.Column, entity.Column) { 591 varColumn := GenColumnData(start, nb, entity.FieldTypeVarChar, DefaultVarcharFieldName) 592 binaryColumn := GenColumnData(start, nb, entity.FieldTypeBinaryVector, DefaultBinaryVecFieldName, WithVectorDim(dim)) 593 return varColumn, binaryColumn 594 } 595 596 func GenAllArrayData(start int, nb int, opts ...GenColumnDataOption) []entity.Column { 597 // how to pass different capacity for different column 598 opt := &genDataOpt{} 599 for _, o := range opts { 600 o(opt) 601 } 602 data := []entity.Column{ 603 GenArrayColumnData(start, nb, DefaultBoolArrayField, WithArrayElementType(entity.FieldTypeBool), WithArrayCapacity(opt.capacity)), 604 GenArrayColumnData(start, nb, DefaultInt8ArrayField, WithArrayElementType(entity.FieldTypeInt8), WithArrayCapacity(opt.capacity)), 605 GenArrayColumnData(start, nb, DefaultInt16ArrayField, WithArrayElementType(entity.FieldTypeInt16), WithArrayCapacity(opt.capacity)), 606 GenArrayColumnData(start, nb, DefaultInt32ArrayField, WithArrayElementType(entity.FieldTypeInt32), WithArrayCapacity(opt.capacity)), 607 GenArrayColumnData(start, nb, DefaultInt64ArrayField, WithArrayElementType(entity.FieldTypeInt64), WithArrayCapacity(opt.capacity)), 608 GenArrayColumnData(start, nb, DefaultFloatArrayField, WithArrayElementType(entity.FieldTypeFloat), WithArrayCapacity(opt.capacity)), 609 GenArrayColumnData(start, nb, DefaultDoubleArrayField, WithArrayElementType(entity.FieldTypeDouble), WithArrayCapacity(opt.capacity)), 610 GenArrayColumnData(start, nb, DefaultVarcharArrayField, WithArrayElementType(entity.FieldTypeVarChar), WithArrayCapacity(opt.capacity)), 611 } 612 return data 613 } 614 615 func GenAllVectorsData(start int, nb int, dim int64, opts ...GenColumnDataOption) []entity.Column { 616 opt := &genDataOpt{} 617 for _, o := range opts { 618 o(opt) 619 } 620 621 // prepare data 622 data := []entity.Column{ 623 GenColumnData(start, nb, entity.FieldTypeInt64, "int64"), 624 GenColumnData(start, nb, entity.FieldTypeFloatVector, "floatVec", WithVectorDim(dim)), 625 GenColumnData(start, nb, entity.FieldTypeFloat16Vector, "fp16Vec", WithVectorDim(dim)), 626 GenColumnData(start, nb, entity.FieldTypeBFloat16Vector, "bf16Vec", WithVectorDim(dim)), 627 GenColumnData(start, nb, entity.FieldTypeBinaryVector, "binaryVec", WithVectorDim(dim)), 628 } 629 return data 630 } 631 632 func GenAllFieldsData(start int, nb int, dim int64, opts ...GenColumnDataOption) []entity.Column { 633 opt := &genDataOpt{} 634 for _, o := range opts { 635 o(opt) 636 } 637 // prepare data 638 data := []entity.Column{ 639 GenColumnData(start, nb, entity.FieldTypeInt64, DefaultIntFieldName), 640 GenColumnData(start, nb, entity.FieldTypeBool, DefaultBoolFieldName), 641 GenColumnData(start, nb, entity.FieldTypeInt8, DefaultInt8FieldName), 642 GenColumnData(start, nb, entity.FieldTypeInt16, DefaultInt16FieldName), 643 GenColumnData(start, nb, entity.FieldTypeInt32, DefaultInt32FieldName), 644 GenColumnData(start, nb, entity.FieldTypeFloat, DefaultFloatFieldName), 645 GenColumnData(start, nb, entity.FieldTypeDouble, DefaultDoubleFieldName), 646 GenColumnData(start, nb, entity.FieldTypeVarChar, DefaultVarcharFieldName), 647 GenDefaultJSONData(DefaultJSONFieldName, start, nb), 648 GenColumnData(start, nb, entity.FieldTypeFloatVector, DefaultFloatVecFieldName, WithVectorDim(dim)), 649 GenColumnData(start, nb, entity.FieldTypeFloat16Vector, DefaultFloat16VecFieldName, WithVectorDim(dim)), 650 GenColumnData(start, nb, entity.FieldTypeBFloat16Vector, DefaultBFloat16VecFieldName, WithVectorDim(dim)), 651 GenColumnData(start, nb, entity.FieldTypeBinaryVector, DefaultBinaryVecFieldName, WithVectorDim(dim)), 652 } 653 data = append(data, GenAllArrayData(start, nb, opts...)...) 654 return data 655 } 656 657 // --- gen column data --- 658 659 // --- gen row data --- 660 661 type Dynamic struct { 662 Number int32 `json:"dynamicNumber" milvus:"name:dynamicNumber"` 663 String string `json:"dynamicString" milvus:"name:dynamicString"` 664 Bool bool `json:"dynamicBool" milvus:"name:dynamicBool"` 665 List []int64 `json:"dynamicList" milvus:"name:dynamicList"` 666 } 667 668 type Array struct { 669 BoolArray []bool `json:"boolArray" milvus:"name:boolArray"` 670 Int8Array []int8 `json:"int8Array" milvus:"name:int8Array"` 671 Int16Array []int16 `json:"int16Array" milvus:"name:int16Array"` 672 Int32Array []int32 `json:"int32Array" milvus:"name:int32Array"` 673 Int64Array []int64 `json:"int64Array" milvus:"name:int64Array"` 674 FloatArray []float32 `json:"floatArray" milvus:"name:floatArray"` 675 DoubleArray []float64 `json:"doubleArray" milvus:"name:doubleArray"` 676 VarcharArray [][]byte `json:"varcharArray" milvus:"name:varcharArray"` 677 } 678 679 func GenDefaultRows(start int, nb int, dim int64, enableDynamicField bool) []interface{} { 680 rows := make([]interface{}, 0, nb) 681 682 // BaseRow generate insert rows 683 type BaseRow struct { 684 Int64 int64 `json:"int64" milvus:"name:int64"` 685 Float float32 `json:"float" milvus:"name:float"` 686 FloatVec []float32 `json:"floatVec" milvus:"name:floatVec"` 687 } 688 689 type DynamicRow struct { 690 Int64 int64 `json:"int64" milvus:"name:int64"` 691 Float float32 `json:"float" milvus:"name:float"` 692 FloatVec []float32 `json:"floatVec" milvus:"name:floatVec"` 693 Dynamic Dynamic `json:"dynamic" milvus:"name:dynamic"` 694 } 695 696 for i := start; i < start+nb; i++ { 697 baseRow := BaseRow{ 698 Int64: int64(i), 699 Float: float32(i), 700 FloatVec: GenFloatVector(dim), 701 } 702 if enableDynamicField { 703 var dynamic Dynamic 704 if i%2 == 0 { 705 dynamic = Dynamic{ 706 Number: int32(i), 707 String: strconv.Itoa(i), 708 Bool: i%2 == 0, 709 } 710 } else { 711 dynamic = Dynamic{ 712 Number: int32(i), 713 String: strconv.Itoa(i), 714 Bool: i%2 == 0, 715 List: []int64{int64(i), int64(i + 1)}, 716 } 717 } 718 719 dynamicRow := DynamicRow{ 720 Int64: baseRow.Int64, 721 Float: baseRow.Float, 722 FloatVec: baseRow.FloatVec, 723 Dynamic: dynamic, 724 } 725 726 rows = append(rows, dynamicRow) 727 } else { 728 rows = append(rows, &baseRow) 729 } 730 } 731 return rows 732 } 733 734 func GenDefaultBinaryRows(start int, nb int, dim int64, enableDynamicField bool) []interface{} { 735 rows := make([]interface{}, 0, nb) 736 737 // BaseRow generate insert rows 738 type BaseRow struct { 739 Int64 int64 `json:"int64" milvus:"name:int64"` 740 Float float32 `json:"float" milvus:"name:float"` 741 BinaryVec []byte `json:"binaryVec" milvus:"name:binaryVec"` 742 } 743 744 type DynamicRow struct { 745 Int64 int64 `json:"int64" milvus:"name:int64"` 746 Float float32 `json:"float" milvus:"name:float"` 747 BinaryVec []byte `json:"binaryVec" milvus:"name:binaryVec"` 748 Dynamic Dynamic `json:"dynamic" milvus:"name:dynamic"` 749 } 750 751 for i := start; i < start+nb; i++ { 752 baseRow := BaseRow{ 753 Int64: int64(i), 754 Float: float32(i), 755 BinaryVec: GenBinaryVector(dim), 756 } 757 if enableDynamicField { 758 dynamic := Dynamic{ 759 Number: int32(i), 760 String: strconv.Itoa(i), 761 Bool: i%2 == 0, 762 List: []int64{int64(i), int64(i + 1)}, 763 } 764 765 dynamicRow := DynamicRow{ 766 Int64: baseRow.Int64, 767 Float: baseRow.Float, 768 BinaryVec: baseRow.BinaryVec, 769 Dynamic: dynamic, 770 } 771 772 rows = append(rows, dynamicRow) 773 } else { 774 rows = append(rows, &baseRow) 775 } 776 } 777 return rows 778 } 779 780 func GenDefaultVarcharRows(start int, nb int, dim int64, enableDynamicField bool) []interface{} { 781 rows := make([]interface{}, 0, nb) 782 783 // BaseRow generate insert rows 784 type BaseRow struct { 785 Varchar string `json:"varchar" milvus:"name:varchar"` 786 BinaryVec []byte `json:"binaryVec" milvus:"name:binaryVec"` 787 } 788 789 type DynamicRow struct { 790 Varchar string `json:"varchar" milvus:"name:varchar"` 791 BinaryVec []byte `json:"binaryVec" milvus:"name:binaryVec"` 792 Dynamic Dynamic `json:"dynamic" milvus:"name:dynamic"` 793 } 794 795 for i := start; i < start+nb; i++ { 796 baseRow := BaseRow{ 797 Varchar: strconv.Itoa(i), 798 BinaryVec: GenBinaryVector(dim), 799 } 800 801 if enableDynamicField { 802 dynamic := Dynamic{ 803 Number: int32(i), 804 String: strconv.Itoa(i), 805 Bool: i%2 == 0, 806 List: []int64{int64(i), int64(i + 1)}, 807 } 808 809 dynamicRow := DynamicRow{ 810 Varchar: baseRow.Varchar, 811 BinaryVec: baseRow.BinaryVec, 812 Dynamic: dynamic, 813 } 814 815 rows = append(rows, dynamicRow) 816 } else { 817 rows = append(rows, &baseRow) 818 } 819 } 820 return rows 821 } 822 823 func GenDefaultJSONRows(start int, nb int, dim int64, enableDynamicField bool) []interface{} { 824 rows := make([]interface{}, 0, nb) 825 826 // BaseRow generate insert rows 827 type BaseRow struct { 828 Int64 int64 `json:"int64" milvus:"name:int64"` 829 Float float32 `json:"float" milvus:"name:float"` 830 FloatVec []float32 `json:"floatVec" milvus:"name:floatVec"` 831 JSON JSONStruct `json:"json" milvus:"name:json"` 832 } 833 834 type BaseDynamicRow struct { 835 Int64 int64 `json:"int64" milvus:"name:int64"` 836 Float float32 `json:"float" milvus:"name:float"` 837 FloatVec []float32 `json:"floatVec" milvus:"name:floatVec"` 838 JSON JSONStruct `json:"json" milvus:"name:json"` 839 Number int32 `json:"dynamicNumber" milvus:"name:dynamicNumber"` 840 String string `json:"dynamicString" milvus:"name:dynamicString"` 841 Bool bool `json:"dynamicBool" milvus:"name:dynamicBool"` 842 //List []int64 `json:"dynamicList" milvus:"name:dynamicList"` 843 } 844 845 for i := start; i < start+nb; i++ { 846 //jsonStruct row and dynamic row 847 var jsonStruct JSONStruct 848 if i%2 == 0 { 849 jsonStruct = JSONStruct{ 850 String: strconv.Itoa(i), 851 Bool: i%2 == 0, 852 } 853 } else { 854 jsonStruct = JSONStruct{ 855 Number: int32(i), 856 String: strconv.Itoa(i), 857 Bool: i%2 == 0, 858 List: []int64{int64(i), int64(i + 1)}, 859 } 860 } 861 // base row 862 baseRow := BaseRow{ 863 Int64: int64(i), 864 Float: float32(i), 865 FloatVec: GenFloatVector(dim), 866 JSON: jsonStruct, 867 } 868 if enableDynamicField { 869 baseDynamicRow := BaseDynamicRow{ 870 Int64: baseRow.Int64, 871 Float: baseRow.Float, 872 FloatVec: baseRow.FloatVec, 873 JSON: baseRow.JSON, 874 Number: int32(i), 875 String: strconv.Itoa(i), 876 Bool: i%2 == 0, 877 //List: []int64{int64(i), int64(i + 1)}, 878 } 879 880 rows = append(rows, &baseDynamicRow) 881 } else { 882 rows = append(rows, &baseRow) 883 } 884 } 885 return rows 886 } 887 888 func GenAllArrayRow(index int, opts ...GenColumnDataOption) Array { 889 opt := &genDataOpt{} 890 for _, o := range opts { 891 o(opt) 892 } 893 var capacity int 894 if opt.capacity != 0 { 895 capacity = int(opt.capacity) 896 } else { 897 capacity = TestCapacity 898 } 899 900 boolRow := make([]bool, 0, capacity) 901 int8Row := make([]int8, 0, capacity) 902 int16Row := make([]int16, 0, capacity) 903 int32Row := make([]int32, 0, capacity) 904 int64Row := make([]int64, 0, capacity) 905 floatRow := make([]float32, 0, capacity) 906 doubleRow := make([]float64, 0, capacity) 907 varcharRow := make([][]byte, 0, capacity) 908 for j := 0; j < capacity; j++ { 909 boolRow = append(boolRow, index%2 == 0) 910 int8Row = append(int8Row, int8(index+j)) 911 int16Row = append(int16Row, int16(index+j)) 912 int32Row = append(int32Row, int32(index+j)) 913 int64Row = append(int64Row, int64(index+j)) 914 floatRow = append(floatRow, float32(index+j)) 915 doubleRow = append(doubleRow, float64(index+j)) 916 var buf bytes.Buffer 917 buf.WriteString(strconv.Itoa(index + j)) 918 varcharRow = append(varcharRow, buf.Bytes()) 919 } 920 arrayRow := Array{ 921 BoolArray: boolRow, 922 Int8Array: int8Row, 923 Int16Array: int16Row, 924 Int32Array: int32Row, 925 Int64Array: int64Row, 926 FloatArray: floatRow, 927 DoubleArray: doubleRow, 928 VarcharArray: varcharRow, 929 } 930 return arrayRow 931 } 932 933 func GenDefaultArrayRows(start int, nb int, dim int64, enableDynamicField bool, opts ...GenColumnDataOption) []interface{} { 934 rows := make([]interface{}, 0, nb) 935 936 // BaseRow generate insert rows 937 type BaseRow struct { 938 Int64 int64 `json:"int64" milvus:"name:int64"` 939 Float float32 `json:"float" milvus:"name:float"` 940 FloatVec []float32 `json:"floatVec" milvus:"name:floatVec"` 941 BoolArray []bool `json:"boolArray" milvus:"name:boolArray"` 942 Int8Array []int8 `json:"int8Array" milvus:"name:int8Array"` 943 Int16Array []int16 `json:"int16Array" milvus:"name:int16Array"` 944 Int32Array []int32 `json:"int32Array" milvus:"name:int32Array"` 945 Int64Array []int64 `json:"int64Array" milvus:"name:int64Array"` 946 FloatArray []float32 `json:"floatArray" milvus:"name:floatArray"` 947 DoubleArray []float64 `json:"doubleArray" milvus:"name:doubleArray"` 948 VarcharArray [][]byte `json:"varcharArray" milvus:"name:varcharArray"` 949 } 950 951 type DynamicRow struct { 952 Int64 int64 `json:"int64" milvus:"name:int64"` 953 Float float32 `json:"float" milvus:"name:float"` 954 FloatVec []float32 `json:"floatVec" milvus:"name:floatVec"` 955 BoolArray []bool `json:"boolArray" milvus:"name:boolArray"` 956 Int8Array []int8 `json:"int8Array" milvus:"name:int8Array"` 957 Int16Array []int16 `json:"int16Array" milvus:"name:int16Array"` 958 Int32Array []int32 `json:"int32Array" milvus:"name:int32Array"` 959 Int64Array []int64 `json:"int64Array" milvus:"name:int64Array"` 960 FloatArray []float32 `json:"floatArray" milvus:"name:floatArray"` 961 DoubleArray []float64 `json:"doubleArray" milvus:"name:doubleArray"` 962 VarcharArray [][]byte `json:"varcharArray" milvus:"name:varcharArray"` 963 Dynamic Dynamic `json:"dynamic" milvus:"name:dynamic"` 964 } 965 966 for i := start; i < start+nb; i++ { 967 // json and dynamic field 968 dynamicJSON := Dynamic{ 969 Number: int32(i), 970 String: strconv.Itoa(i), 971 Bool: i%2 == 0, 972 List: []int64{int64(i), int64(i + 1)}, 973 } 974 arrayRow := GenAllArrayRow(i, opts...) 975 baseRow := BaseRow{ 976 Int64: int64(i), 977 Float: float32(i), 978 FloatVec: GenFloatVector(dim), 979 BoolArray: arrayRow.BoolArray, 980 Int8Array: arrayRow.Int8Array, 981 Int16Array: arrayRow.Int16Array, 982 Int32Array: arrayRow.Int32Array, 983 Int64Array: arrayRow.Int64Array, 984 FloatArray: arrayRow.FloatArray, 985 DoubleArray: arrayRow.DoubleArray, 986 VarcharArray: arrayRow.VarcharArray, 987 } 988 if enableDynamicField { 989 dynamicRow := DynamicRow{ 990 Int64: baseRow.Int64, 991 Float: baseRow.Float, 992 FloatVec: baseRow.FloatVec, 993 BoolArray: arrayRow.BoolArray, 994 Int8Array: arrayRow.Int8Array, 995 Int16Array: arrayRow.Int16Array, 996 Int32Array: arrayRow.Int32Array, 997 Int64Array: arrayRow.Int64Array, 998 FloatArray: arrayRow.FloatArray, 999 DoubleArray: arrayRow.DoubleArray, 1000 VarcharArray: arrayRow.VarcharArray, 1001 Dynamic: dynamicJSON, 1002 } 1003 1004 rows = append(rows, dynamicRow) 1005 } else { 1006 rows = append(rows, &baseRow) 1007 } 1008 } 1009 return rows 1010 } 1011 1012 func GenDefaultSparseRows(start int, nb int, dim int64, maxLenSparse int, enableDynamicField bool) []interface{} { 1013 rows := make([]interface{}, 0, nb) 1014 type BaseRow struct { 1015 Int64 int64 `json:"int64" milvus:"name:int64"` 1016 Varchar string `json:"varchar" milvus:"name:varchar"` 1017 FloatVec []float32 `json:"floatVec" milvus:"name:floatVec"` 1018 SparseVec entity.SparseEmbedding `json:"sparseVec" milvus:"name:sparseVec"` 1019 } 1020 1021 type DynamicRow struct { 1022 Int64 int64 `json:"int64" milvus:"name:int64"` 1023 Varchar string `json:"varchar" milvus:"name:varchar"` 1024 FloatVec []float32 `json:"floatVec" milvus:"name:floatVec"` 1025 SparseVec entity.SparseEmbedding `json:"sparseVec" milvus:"name:sparseVec"` 1026 Dynamic Dynamic `json:"dynamic" milvus:"name:dynamic"` 1027 } 1028 1029 for i := start; i < start+nb; i++ { 1030 baseRow := BaseRow{ 1031 Int64: int64(i), 1032 Varchar: strconv.Itoa(i), 1033 FloatVec: GenFloatVector(dim), 1034 SparseVec: GenSparseVector(maxLenSparse), 1035 } 1036 // json and dynamic field 1037 dynamicJSON := Dynamic{ 1038 Number: int32(i), 1039 String: strconv.Itoa(i), 1040 Bool: i%2 == 0, 1041 List: []int64{int64(i), int64(i + 1)}, 1042 } 1043 if enableDynamicField { 1044 dynamicRow := DynamicRow{ 1045 Int64: baseRow.Int64, 1046 Varchar: baseRow.Varchar, 1047 FloatVec: baseRow.FloatVec, 1048 SparseVec: baseRow.SparseVec, 1049 Dynamic: dynamicJSON, 1050 } 1051 rows = append(rows, dynamicRow) 1052 } else { 1053 rows = append(rows, &baseRow) 1054 } 1055 } 1056 return rows 1057 } 1058 1059 func GenAllVectorsRows(start int, nb int, dim int64, enableDynamicField bool) []interface{} { 1060 rows := make([]interface{}, 0, nb) 1061 type BaseRow struct { 1062 Int64 int64 `json:"int64" milvus:"name:int64"` 1063 FloatVec []float32 `json:"floatVec" milvus:"name:floatVec"` 1064 Float16Vec []byte `json:"fp16Vec" milvus:"name:fp16Vec"` 1065 BFloat16Vec []byte `json:"bf16Vec" milvus:"name:bf16Vec"` 1066 BinaryVec []byte `json:"binaryVec" milvus:"name:binaryVec"` 1067 } 1068 1069 type DynamicRow struct { 1070 Int64 int64 `json:"int64" milvus:"name:int64"` 1071 FloatVec []float32 `json:"floatVec" milvus:"name:floatVec"` 1072 Float16Vec []byte `json:"fp16Vec" milvus:"name:fp16Vec"` 1073 BFloat16Vec []byte `json:"bf16Vec" milvus:"name:bf16Vec"` 1074 BinaryVec []byte `json:"binaryVec" milvus:"name:binaryVec"` 1075 Dynamic Dynamic `json:"dynamic" milvus:"name:dynamic"` 1076 } 1077 1078 for i := start; i < start+nb; i++ { 1079 baseRow := BaseRow{ 1080 Int64: int64(i), 1081 FloatVec: GenFloatVector(dim), 1082 Float16Vec: GenFloat16Vector(dim), 1083 BFloat16Vec: GenBFloat16Vector(dim), 1084 BinaryVec: GenBinaryVector(dim), 1085 } 1086 // json and dynamic field 1087 dynamicJSON := Dynamic{ 1088 Number: int32(i), 1089 String: strconv.Itoa(i), 1090 Bool: i%2 == 0, 1091 List: []int64{int64(i), int64(i + 1)}, 1092 } 1093 if enableDynamicField { 1094 dynamicRow := DynamicRow{ 1095 Int64: baseRow.Int64, 1096 FloatVec: baseRow.FloatVec, 1097 Float16Vec: baseRow.Float16Vec, 1098 BFloat16Vec: baseRow.BFloat16Vec, 1099 BinaryVec: baseRow.BinaryVec, 1100 Dynamic: dynamicJSON, 1101 } 1102 rows = append(rows, dynamicRow) 1103 } else { 1104 rows = append(rows, &baseRow) 1105 } 1106 } 1107 return rows 1108 } 1109 1110 func GenAllFieldsRows(start int, nb int, dim int64, enableDynamicField bool, opts ...GenColumnDataOption) []interface{} { 1111 rows := make([]interface{}, 0, nb) 1112 1113 // BaseRow generate insert rows 1114 type BaseRow struct { 1115 Int64 int64 `json:"int64" milvus:"name:int64"` 1116 Bool bool `json:"bool" milvus:"name:bool"` 1117 Int8 int8 `json:"int8" milvus:"name:int8"` 1118 Int16 int16 `json:"int16" milvus:"name:int16"` 1119 Int32 int32 `json:"int32" milvus:"name:int32"` 1120 Float float32 `json:"float" milvus:"name:float"` 1121 Double float64 `json:"double" milvus:"name:double"` 1122 Varchar string `json:"varchar" milvus:"name:varchar"` 1123 JSON Dynamic `json:"json" milvus:"name:json"` 1124 FloatVec []float32 `json:"floatVec" milvus:"name:floatVec"` 1125 Float16Vec []byte `json:"fp16Vec" milvus:"name:fp16Vec"` 1126 BFloat16Vec []byte `json:"bf16Vec" milvus:"name:bf16Vec"` 1127 BinaryVec []byte `json:"binaryVec" milvus:"name:binaryVec"` 1128 BoolArray []bool `json:"boolArray" milvus:"name:boolArray"` 1129 Int8Array []int8 `json:"int8Array" milvus:"name:int8Array"` 1130 Int16Array []int16 `json:"int16Array" milvus:"name:int16Array"` 1131 Int32Array []int32 `json:"int32Array" milvus:"name:int32Array"` 1132 Int64Array []int64 `json:"int64Array" milvus:"name:int64Array"` 1133 FloatArray []float32 `json:"floatArray" milvus:"name:floatArray"` 1134 DoubleArray []float64 `json:"doubleArray" milvus:"name:doubleArray"` 1135 VarcharArray [][]byte `json:"varcharArray" milvus:"name:varcharArray"` 1136 } 1137 1138 type DynamicRow struct { 1139 Int64 int64 `json:"int64" milvus:"name:int64"` 1140 Bool bool `json:"bool" milvus:"name:bool"` 1141 Int8 int8 `json:"int8" milvus:"name:int8"` 1142 Int16 int16 `json:"int16" milvus:"name:int16"` 1143 Int32 int32 `json:"int32" milvus:"name:int32"` 1144 Float float32 `json:"float" milvus:"name:float"` 1145 Double float64 `json:"double" milvus:"name:double"` 1146 Varchar string `json:"varchar" milvus:"name:varchar"` 1147 JSON Dynamic `json:"json" milvus:"name:json"` 1148 FloatVec []float32 `json:"floatVec" milvus:"name:floatVec"` 1149 Float16Vec []byte `json:"fp16Vec" milvus:"name:fp16Vec"` 1150 BFloat16Vec []byte `json:"bf16Vec" milvus:"name:bf16Vec"` 1151 BinaryVec []byte `json:"binaryVec" milvus:"name:binaryVec"` 1152 BoolArray []bool `json:"boolArray" milvus:"name:boolArray"` 1153 Int8Array []int8 `json:"int8Array" milvus:"name:int8Array"` 1154 Int16Array []int16 `json:"int16Array" milvus:"name:int16Array"` 1155 Int32Array []int32 `json:"int32Array" milvus:"name:int32Array"` 1156 Int64Array []int64 `json:"int64Array" milvus:"name:int64Array"` 1157 FloatArray []float32 `json:"floatArray" milvus:"name:floatArray"` 1158 DoubleArray []float64 `json:"doubleArray" milvus:"name:doubleArray"` 1159 VarcharArray [][]byte `json:"varcharArray" milvus:"name:varcharArray"` 1160 Dynamic Dynamic `json:"dynamic" milvus:"name:dynamic"` 1161 } 1162 1163 for i := start; i < start+nb; i++ { 1164 arrayRow := GenAllArrayRow(i, opts...) 1165 baseRow := BaseRow{ 1166 Int64: int64(i), 1167 Bool: i%2 == 0, 1168 Int8: int8(i), 1169 Int16: int16(i), 1170 Int32: int32(i), 1171 Float: float32(i), 1172 Double: float64(i), 1173 Varchar: strconv.Itoa(i), 1174 FloatVec: GenFloatVector(dim), 1175 Float16Vec: GenFloat16Vector(dim), 1176 BFloat16Vec: GenBFloat16Vector(dim), 1177 BinaryVec: GenBinaryVector(dim), 1178 BoolArray: arrayRow.BoolArray, 1179 Int8Array: arrayRow.Int8Array, 1180 Int16Array: arrayRow.Int16Array, 1181 Int32Array: arrayRow.Int32Array, 1182 Int64Array: arrayRow.Int64Array, 1183 FloatArray: arrayRow.FloatArray, 1184 DoubleArray: arrayRow.DoubleArray, 1185 VarcharArray: arrayRow.VarcharArray, 1186 } 1187 1188 // json and dynamic field 1189 dynamicJSON := Dynamic{ 1190 Number: int32(i), 1191 String: strconv.Itoa(i), 1192 Bool: i%2 == 0, 1193 List: []int64{int64(i), int64(i + 1)}, 1194 } 1195 if enableDynamicField { 1196 dynamicRow := DynamicRow{ 1197 Int64: baseRow.Int64, 1198 Bool: baseRow.Bool, 1199 Int8: baseRow.Int8, 1200 Int16: baseRow.Int16, 1201 Int32: baseRow.Int32, 1202 Float: baseRow.Float, 1203 Double: baseRow.Double, 1204 Varchar: baseRow.Varchar, 1205 FloatVec: baseRow.FloatVec, 1206 Float16Vec: baseRow.Float16Vec, 1207 BFloat16Vec: baseRow.BFloat16Vec, 1208 BinaryVec: baseRow.BinaryVec, 1209 BoolArray: arrayRow.BoolArray, 1210 Int8Array: arrayRow.Int8Array, 1211 Int16Array: arrayRow.Int16Array, 1212 Int32Array: arrayRow.Int32Array, 1213 Int64Array: arrayRow.Int64Array, 1214 FloatArray: arrayRow.FloatArray, 1215 DoubleArray: arrayRow.DoubleArray, 1216 VarcharArray: arrayRow.VarcharArray, 1217 Dynamic: dynamicJSON, 1218 } 1219 rows = append(rows, dynamicRow) 1220 } else { 1221 rows = append(rows, &baseRow) 1222 } 1223 } 1224 return rows 1225 } 1226 1227 func GenDynamicFieldData(start int, nb int) []entity.Column { 1228 type ListStruct struct { 1229 List []int64 `json:"list" milvus:"name:list"` 1230 } 1231 1232 // gen number, string bool list data column 1233 numberValues := make([]int32, 0, nb) 1234 stringValues := make([]string, 0, nb) 1235 boolValues := make([]bool, 0, nb) 1236 //listValues := make([][]byte, 0, nb) 1237 //m := make(map[string]interface{}) 1238 for i := start; i < start+nb; i++ { 1239 numberValues = append(numberValues, int32(i)) 1240 stringValues = append(stringValues, strconv.Itoa(i)) 1241 boolValues = append(boolValues, i%3 == 0) 1242 //m["list"] = ListStruct{ 1243 // List: []int64{int64(i), int64(i + 1)}, 1244 //} 1245 //bs, err := json.Marshal(m) 1246 //if err != nil { 1247 // log.Fatalf("Marshal json field failed: %s", err) 1248 //} 1249 //listValues = append(listValues, bs) 1250 } 1251 data := []entity.Column{ 1252 entity.NewColumnInt32(DefaultDynamicNumberField, numberValues), 1253 entity.NewColumnString(DefaultDynamicStringField, stringValues), 1254 entity.NewColumnBool(DefaultDynamicBoolField, boolValues), 1255 //entity.NewColumnJSONBytes(DefaultDynamicListField, listValues), 1256 } 1257 return data 1258 } 1259 1260 func MergeColumnsToDynamic(nb int, columns []entity.Column) *entity.ColumnJSONBytes { 1261 values := make([][]byte, 0, nb) 1262 for i := 0; i < nb; i++ { 1263 m := make(map[string]interface{}) 1264 for _, column := range columns { 1265 // range guaranteed 1266 m[column.Name()], _ = column.Get(i) 1267 } 1268 bs, err := json.Marshal(&m) 1269 if err != nil { 1270 log.Fatal(err) 1271 } 1272 values = append(values, bs) 1273 } 1274 jsonColumn := entity.NewColumnJSONBytes(DefaultDynamicFieldName, values) 1275 1276 var jsonData []string 1277 for i := 0; i < jsonColumn.Len(); i++ { 1278 line, err := jsonColumn.GetAsString(i) 1279 if err != nil { 1280 fmt.Println(err) 1281 } 1282 jsonData = append(jsonData, line) 1283 } 1284 1285 return jsonColumn 1286 } 1287 1288 // --- gen row data --- 1289 1290 // --- index utils --- 1291 var SupportFloatMetricType = []entity.MetricType{ 1292 entity.L2, 1293 entity.IP, 1294 entity.COSINE, 1295 } 1296 1297 var SupportBinFlatMetricType = []entity.MetricType{ 1298 entity.JACCARD, 1299 entity.HAMMING, 1300 entity.SUBSTRUCTURE, 1301 entity.SUPERSTRUCTURE, 1302 } 1303 1304 var SupportBinIvfFlatMetricType = []entity.MetricType{ 1305 entity.JACCARD, 1306 entity.HAMMING, 1307 } 1308 1309 var UnsupportedSparseVecMetricsType = []entity.MetricType{ 1310 entity.L2, 1311 entity.COSINE, 1312 entity.JACCARD, 1313 entity.HAMMING, 1314 entity.SUBSTRUCTURE, 1315 entity.SUPERSTRUCTURE, 1316 } 1317 1318 // GenAllFloatIndex gen all float vector index 1319 func GenAllFloatIndex(metricTypes ...entity.MetricType) []entity.Index { 1320 nlist := 128 1321 var allFloatIndex []entity.Index 1322 var allMetricTypes []entity.MetricType 1323 log.Println(metricTypes) 1324 if len(metricTypes) == 0 { 1325 allMetricTypes = SupportFloatMetricType 1326 } else { 1327 allMetricTypes = metricTypes 1328 } 1329 for _, metricType := range allMetricTypes { 1330 log.Println(metricType) 1331 idxFlat, _ := entity.NewIndexFlat(metricType) 1332 idxIvfFlat, _ := entity.NewIndexIvfFlat(metricType, nlist) 1333 idxIvfSq8, _ := entity.NewIndexIvfSQ8(metricType, nlist) 1334 idxIvfPq, _ := entity.NewIndexIvfPQ(metricType, nlist, 16, 8) 1335 idxHnsw, _ := entity.NewIndexHNSW(metricType, 8, 96) 1336 idxScann, _ := entity.NewIndexSCANN(metricType, 16, false) 1337 idxDiskAnn, _ := entity.NewIndexDISKANN(metricType) 1338 allFloatIndex = append(allFloatIndex, idxFlat, idxIvfFlat, idxIvfSq8, idxIvfPq, idxHnsw, idxScann, idxDiskAnn) 1339 } 1340 return allFloatIndex 1341 } 1342 1343 // --- index utils --- 1344 1345 // --- search utils --- 1346 1347 // GenSearchVectors gen search vectors 1348 func GenSearchVectors(nq int, dim int64, dataType entity.FieldType) []entity.Vector { 1349 vectors := make([]entity.Vector, 0, nq) 1350 switch dataType { 1351 case entity.FieldTypeFloatVector: 1352 for i := 0; i < nq; i++ { 1353 vector := GenFloatVector(dim) 1354 vectors = append(vectors, entity.FloatVector(vector)) 1355 } 1356 case entity.FieldTypeBinaryVector: 1357 for i := 0; i < nq; i++ { 1358 vector := GenBinaryVector(dim) 1359 vectors = append(vectors, entity.BinaryVector(vector)) 1360 } 1361 case entity.FieldTypeFloat16Vector: 1362 for i := 0; i < nq; i++ { 1363 vector := GenFloat16Vector(dim) 1364 vectors = append(vectors, entity.Float16Vector(vector)) 1365 } 1366 case entity.FieldTypeBFloat16Vector: 1367 for i := 0; i < nq; i++ { 1368 vector := GenBFloat16Vector(dim) 1369 vectors = append(vectors, entity.BFloat16Vector(vector)) 1370 } 1371 case entity.FieldTypeSparseVector: 1372 for i := 0; i < nq; i++ { 1373 vec := GenSparseVector(int(dim)) 1374 vectors = append(vectors, vec) 1375 } 1376 } 1377 return vectors 1378 } 1379 1380 // InvalidExprStruct invalid expr 1381 type InvalidExprStruct struct { 1382 Expr string 1383 ErrNil bool 1384 ErrMsg string 1385 } 1386 1387 var InvalidExpressions = []InvalidExprStruct{ 1388 {Expr: "id in [0]", ErrNil: true, ErrMsg: "fieldName(id) not found"}, // not exist field but no error 1389 {Expr: "int64 in not [0]", ErrNil: false, ErrMsg: "cannot parse expression"}, // wrong term expr keyword 1390 {Expr: "int64 > 10 AND int64 < 100", ErrNil: false, ErrMsg: "cannot parse expression"}, // AND isn't supported 1391 {Expr: "int64 < 10 OR int64 > 100", ErrNil: false, ErrMsg: "cannot parse expression"}, // OR isn't supported 1392 {Expr: "int64 < floatVec", ErrNil: false, ErrMsg: "not supported"}, // unsupported compare field 1393 {Expr: "floatVec in [0]", ErrNil: false, ErrMsg: "cannot be casted to FloatVector"}, // value and field type mismatch 1394 {Expr: fmt.Sprintf("%s == 1", DefaultJSONFieldName), ErrNil: true, ErrMsg: ""}, // hist empty 1395 {Expr: fmt.Sprintf("%s like 'a%%' ", DefaultJSONFieldName), ErrNil: true, ErrMsg: ""}, // hist empty 1396 {Expr: fmt.Sprintf("%s like `a%%` ", DefaultJSONFieldName), ErrNil: false, ErrMsg: "cannot parse expression"}, // `` 1397 {Expr: fmt.Sprintf("%s > 1", DefaultDynamicFieldName), ErrNil: true, ErrMsg: ""}, // hits empty 1398 {Expr: fmt.Sprintf("%s[\"dynamicList\"] == [2, 3]", DefaultDynamicFieldName), ErrNil: true, ErrMsg: ""}, 1399 {Expr: fmt.Sprintf("%s['a'] == [2, 3]", DefaultJSONFieldName), ErrNil: true, ErrMsg: ""}, // json field not exist 1400 {Expr: fmt.Sprintf("%s['number'] == [2, 3]", DefaultJSONFieldName), ErrNil: true, ErrMsg: ""}, // field exist but type not match 1401 {Expr: fmt.Sprintf("%s[0] == [2, 3]", DefaultJSONFieldName), ErrNil: true, ErrMsg: ""}, // field exist but type not match 1402 {Expr: fmt.Sprintf("json_contains (%s['number'], 2)", DefaultJSONFieldName), ErrNil: true, ErrMsg: ""}, 1403 {Expr: fmt.Sprintf("json_contains (%s['list'], [2])", DefaultJSONFieldName), ErrNil: true, ErrMsg: ""}, 1404 {Expr: fmt.Sprintf("json_contains_all (%s['list'], 2)", DefaultJSONFieldName), ErrNil: false, ErrMsg: "contains_all operation element must be an array"}, 1405 {Expr: fmt.Sprintf("JSON_CONTAINS_ANY (%s['list'], 2)", DefaultJSONFieldName), ErrNil: false, ErrMsg: "contains_any operation element must be an array"}, 1406 {Expr: fmt.Sprintf("json_contains_aby (%s['list'], 2)", DefaultJSONFieldName), ErrNil: false, ErrMsg: "invalid expression: json_contains_aby"}, 1407 {Expr: fmt.Sprintf("json_contains_aby (%s['list'], 2)", DefaultJSONFieldName), ErrNil: false, ErrMsg: "invalid expression: json_contains_aby"}, 1408 {Expr: fmt.Sprintf("%s[-1] > %d", DefaultInt8ArrayField, TestCapacity), ErrNil: false, ErrMsg: "cannot parse expression"}, // array[-1] > 1409 {Expr: fmt.Sprintf(fmt.Sprintf("%s[-1] > 1", DefaultJSONFieldName)), ErrNil: false, ErrMsg: "invalid expression"}, // json[-1] > 1410 } 1411 1412 func GenBatchSizes(limit int, batch int) []int { 1413 if batch == 0 { 1414 log.Fatal("Batch should be larger than 0") 1415 } 1416 if limit == 0 { 1417 return []int{} 1418 } 1419 _loop := limit / batch 1420 _last := limit % batch 1421 batchSizes := make([]int, 0, _loop+1) 1422 for i := 0; i < _loop; i++ { 1423 batchSizes = append(batchSizes, batch) 1424 } 1425 if _last > 0 { 1426 batchSizes = append(batchSizes, _last) 1427 } 1428 return batchSizes 1429 } 1430 1431 // --- search utils ---