github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/test/testcases/insert_test.go (about)

     1  //go:build L0
     2  
     3  package testcases
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"log"
     9  	"math"
    10  	"math/rand"
    11  	"strconv"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/milvus-io/milvus-sdk-go/v2/client"
    16  
    17  	"github.com/milvus-io/milvus-sdk-go/v2/entity"
    18  
    19  	"github.com/milvus-io/milvus-sdk-go/v2/test/common"
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  // test insert
    24  func TestInsert(t *testing.T) {
    25  	t.Parallel()
    26  	ctx := createContext(t, time.Second*common.DefaultTimeout)
    27  	// connect
    28  	mc := createMilvusClient(ctx, t)
    29  
    30  	for _, enableDynamicField := range []bool{true, false} {
    31  		// create default collection
    32  		collName := createDefaultCollection(ctx, t, mc, false, common.DefaultShards, client.WithEnableDynamicSchema(enableDynamicField))
    33  
    34  		// insert
    35  		intColumn, floatColumn, vecColumn := common.GenDefaultColumnData(0, common.DefaultNb, common.DefaultDim)
    36  		ids, errInsert := mc.Insert(ctx, collName, "", intColumn, floatColumn, vecColumn)
    37  		common.CheckErr(t, errInsert, true)
    38  		common.CheckInsertResult(t, ids, intColumn)
    39  
    40  		// flush and check row count
    41  		errFlush := mc.Flush(ctx, collName, false)
    42  		common.CheckErr(t, errFlush, true)
    43  		stats, _ := mc.GetCollectionStatistics(ctx, collName)
    44  		require.Equal(t, strconv.Itoa(common.DefaultNb), stats[common.RowCount])
    45  	}
    46  }
    47  
    48  // test insert with autoID collection
    49  func TestInsertAutoId(t *testing.T) {
    50  	ctx := createContext(t, time.Second*common.DefaultTimeout)
    51  
    52  	// connect
    53  	mc := createMilvusClient(ctx, t)
    54  
    55  	// create default collection with autoID true
    56  	collName := createDefaultCollection(ctx, t, mc, true, common.DefaultShards)
    57  
    58  	// insert
    59  	_, floatColumn, vecColumn := common.GenDefaultColumnData(0, common.DefaultNb, common.DefaultDim)
    60  	ids, errInsert := mc.Insert(ctx, collName, "", floatColumn, vecColumn)
    61  	common.CheckErr(t, errInsert, true)
    62  	require.Equal(t, common.DefaultNb, ids.Len())
    63  
    64  	// list partition
    65  	partitions, _ := mc.ShowPartitions(ctx, collName)
    66  	require.Equal(t, partitions[0].Name, common.DefaultPartition)
    67  
    68  	// flush and check row count
    69  	errFlush := mc.Flush(ctx, collName, false)
    70  	common.CheckErr(t, errFlush, true)
    71  	stats, _ := mc.GetCollectionStatistics(ctx, collName)
    72  	require.Equal(t, strconv.Itoa(common.DefaultNb), stats[common.RowCount])
    73  }
    74  
    75  func TestInsertAutoIdPkData(t *testing.T) {
    76  	ctx := createContext(t, time.Second*common.DefaultTimeout)
    77  
    78  	// connect
    79  	mc := createMilvusClient(ctx, t)
    80  
    81  	// create default collection with autoID true
    82  	collName := createDefaultCollection(ctx, t, mc, true, common.DefaultShards)
    83  
    84  	// insert
    85  	pkColumn, floatColumn, vecColumn := common.GenDefaultColumnData(0, common.DefaultNb, common.DefaultDim)
    86  	_, errInsert := mc.Insert(ctx, collName, "", pkColumn, floatColumn, vecColumn)
    87  	common.CheckErr(t, errInsert, false, "can not assign primary field data when auto id enabled")
    88  
    89  	// flush and check row count
    90  	errFlush := mc.Flush(ctx, collName, false)
    91  	common.CheckErr(t, errFlush, true)
    92  	stats, _ := mc.GetCollectionStatistics(ctx, collName)
    93  	require.Equal(t, "0", stats[common.RowCount])
    94  }
    95  
    96  // test insert binary vectors
    97  func TestInsertBinaryCollection(t *testing.T) {
    98  	ctx := createContext(t, time.Second*common.DefaultTimeout)
    99  
   100  	// connect
   101  	mc := createMilvusClient(ctx, t)
   102  
   103  	// create binary collection with autoID true
   104  	collName := common.GenRandomString(6)
   105  	binaryFields := common.GenDefaultBinaryFields(true, common.DefaultDim)
   106  	schema := common.GenSchema(collName, true, binaryFields)
   107  	mc.CreateCollection(ctx, schema, common.DefaultShards)
   108  
   109  	// insert
   110  	_, floatColumn, vecColumn := common.GenDefaultBinaryData(0, common.DefaultNb, common.DefaultDim)
   111  	ids, errInsert := mc.Insert(ctx, collName, "", floatColumn, vecColumn)
   112  	common.CheckErr(t, errInsert, true)
   113  	require.Equal(t, common.DefaultNb, ids.Len())
   114  
   115  	// list partition
   116  	partitions, _ := mc.ShowPartitions(ctx, collName)
   117  	require.Equal(t, partitions[0].Name, common.DefaultPartition)
   118  
   119  	// flush and check row count
   120  	errFlush := mc.Flush(ctx, collName, false)
   121  	common.CheckErr(t, errFlush, true)
   122  	stats, _ := mc.GetCollectionStatistics(ctx, collName)
   123  	require.Equal(t, strconv.Itoa(common.DefaultNb), stats[common.RowCount])
   124  }
   125  
   126  // test insert not exist collection
   127  func TestInsertNotExistCollection(t *testing.T) {
   128  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   129  
   130  	// connect
   131  	mc := createMilvusClient(ctx, t)
   132  
   133  	// insert
   134  	intColumn, floatColumn, vecColumn := common.GenDefaultColumnData(0, common.DefaultNb, common.DefaultDim)
   135  	_, errInsert := mc.Insert(ctx, "notExist", "", intColumn, floatColumn, vecColumn)
   136  	common.CheckErr(t, errInsert, false, "can't find collection")
   137  }
   138  
   139  // test insert into an not existed partition
   140  func TestInsertNotExistPartition(t *testing.T) {
   141  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   142  
   143  	// connect
   144  	mc := createMilvusClient(ctx, t)
   145  
   146  	// create default collection with autoID true
   147  	collName := createDefaultCollection(ctx, t, mc, true, common.DefaultShards)
   148  
   149  	// insert
   150  	_, floatColumn, vecColumn := common.GenDefaultColumnData(0, common.DefaultNb, common.DefaultDim)
   151  	_, errInsert := mc.Insert(ctx, collName, "aaa", floatColumn, vecColumn)
   152  	common.CheckErr(t, errInsert, false, "partition not found")
   153  }
   154  
   155  // test insert data into collection that has all scala fields
   156  func TestInsertAllFieldsData(t *testing.T) {
   157  	t.Parallel()
   158  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   159  	mc := createMilvusClient(ctx, t)
   160  
   161  	// prepare fields, name, schema
   162  	allFields := common.GenAllFields()
   163  	collName := common.GenRandomString(6)
   164  	schema := common.GenSchema(collName, false, allFields, common.WithEnableDynamicField(true))
   165  
   166  	// create collection
   167  	errCreateCollection := mc.CreateCollection(ctx, schema, common.DefaultShards)
   168  	common.CheckErr(t, errCreateCollection, true)
   169  
   170  	// prepare and insert data
   171  	data := common.GenAllFieldsData(0, common.DefaultNb, common.DefaultDim)
   172  	data = append(data, common.GenDynamicFieldData(0, common.DefaultNb)...)
   173  	// insert data
   174  	ids, errInsert := mc.Insert(
   175  		ctx,
   176  		collName,
   177  		"",
   178  		data...,
   179  	)
   180  	common.CheckErr(t, errInsert, true)
   181  	require.Equal(t, common.DefaultNb, ids.Len())
   182  
   183  	// flush and check row count
   184  	errFlush := mc.Flush(ctx, collName, false)
   185  	common.CheckErr(t, errFlush, true)
   186  	stats, _ := mc.GetCollectionStatistics(ctx, collName)
   187  	require.Equal(t, strconv.Itoa(common.DefaultNb), stats[common.RowCount])
   188  }
   189  
   190  // test insert data columns len, order mismatch fields
   191  func TestInsertColumnsMismatchFields(t *testing.T) {
   192  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   193  
   194  	// connect
   195  	mc := createMilvusClient(ctx, t)
   196  
   197  	// create default collection
   198  	collName := createDefaultCollection(ctx, t, mc, false, common.DefaultShards)
   199  	intColumn, floatColumn, vecColumn := common.GenDefaultColumnData(0, common.DefaultNb, common.DefaultDim)
   200  
   201  	// len(column) < len(fields)
   202  	_, errInsert := mc.Insert(ctx, collName, "", intColumn, floatColumn)
   203  	common.CheckErr(t, errInsert, false, "not passed")
   204  
   205  	// len(column) > len(fields)
   206  	_, errInsert2 := mc.Insert(ctx, collName, "", intColumn, floatColumn, vecColumn, floatColumn)
   207  	common.CheckErr(t, errInsert2, false, "duplicated column")
   208  
   209  	//
   210  	binaryColumn := common.GenColumnData(0, common.DefaultNb, entity.FieldTypeBinaryVector, "binaryVec", common.WithVectorDim(common.DefaultDim))
   211  	_, errInsert4 := mc.Insert(ctx, collName, "", intColumn, floatColumn, vecColumn, binaryColumn)
   212  	common.CheckErr(t, errInsert4, false, "does not exist")
   213  
   214  	// order(column) != order(fields)
   215  	_, errInsert3 := mc.Insert(ctx, collName, "", floatColumn, vecColumn, intColumn)
   216  	common.CheckErr(t, errInsert3, true)
   217  }
   218  
   219  // test insert with columns which has different len
   220  func TestInsertColumnsDifferentLen(t *testing.T) {
   221  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   222  
   223  	// connect
   224  	mc := createMilvusClient(ctx, t)
   225  
   226  	// create default collection
   227  	collName := createDefaultCollection(ctx, t, mc, false, common.DefaultShards)
   228  
   229  	// data, different column has different len
   230  	int64Values := make([]int64, 0, 100)
   231  	floatValues := make([]float32, 0, 200)
   232  	vecFloatValues := make([][]float32, 0, 200)
   233  	for i := 0; i < 100; i++ {
   234  		int64Values = append(int64Values, int64(i))
   235  	}
   236  	for i := 0; i < 200; i++ {
   237  		floatValues = append(floatValues, float32(i))
   238  		vec := make([]float32, 0, common.DefaultDim)
   239  		for j := 0; j < int(common.DefaultDim); j++ {
   240  			vec = append(vec, rand.Float32())
   241  		}
   242  		vecFloatValues = append(vecFloatValues, vec)
   243  	}
   244  
   245  	// insert
   246  	_, errInsert := mc.Insert(ctx, collName, "",
   247  		entity.NewColumnInt64(common.DefaultIntFieldName, int64Values),
   248  		entity.NewColumnFloat(common.DefaultFloatFieldName, floatValues),
   249  		entity.NewColumnFloatVector(common.DefaultFloatVecFieldName, int(common.DefaultDim), vecFloatValues))
   250  	common.CheckErr(t, errInsert, false, "column size not match")
   251  }
   252  
   253  // test insert rows enable or disable dynamic field
   254  func TestInsertRows(t *testing.T) {
   255  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   256  
   257  	// connect
   258  	mc := createMilvusClient(ctx, t)
   259  	for _, enableDynamicField := range []bool{true, false} {
   260  		// create collection enable dynamic field
   261  		schema := common.GenSchema(common.GenRandomString(6), false, common.GenDefaultFields(false), common.WithEnableDynamicField(enableDynamicField))
   262  		createCustomerCollection(ctx, t, mc, schema, common.DefaultShards)
   263  
   264  		// insert rows
   265  		rows := common.GenDefaultRows(0, common.DefaultNb, common.DefaultDim, enableDynamicField)
   266  		ids, err := mc.InsertRows(ctx, schema.CollectionName, "", rows)
   267  		common.CheckErr(t, err, true)
   268  
   269  		int64Values := make([]int64, 0, common.DefaultNb)
   270  		for i := 0; i < common.DefaultNb; i++ {
   271  			int64Values = append(int64Values, int64(i))
   272  		}
   273  		common.CheckInsertResult(t, ids, entity.NewColumnInt64(common.DefaultIntFieldName, int64Values))
   274  
   275  		// flush and check row count
   276  		errFlush := mc.Flush(ctx, schema.CollectionName, false)
   277  		common.CheckErr(t, errFlush, true)
   278  		stats, _ := mc.GetCollectionStatistics(ctx, schema.CollectionName)
   279  		require.Equal(t, strconv.Itoa(common.DefaultNb), stats[common.RowCount])
   280  	}
   281  }
   282  
   283  // test insert json rows field name not match
   284  func TestInsertJsonCollectionFieldNotMatch(t *testing.T) {
   285  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   286  
   287  	// connect
   288  	mc := createMilvusClient(ctx, t)
   289  	nb := 1000
   290  
   291  	// create json collection with random json field name
   292  	collName := common.GenRandomString(6)
   293  	jsonRandomField := common.GenField("", entity.FieldTypeJSON)
   294  	fields := common.GenDefaultFields(true)
   295  	fields = append(fields, jsonRandomField)
   296  	schema := common.GenSchema(collName, true, fields)
   297  
   298  	// create collection
   299  	err := mc.CreateCollection(ctx, schema, common.DefaultShards)
   300  	common.CheckErr(t, err, true)
   301  
   302  	// insert data without "json" field
   303  	rows := common.GenDefaultRows(0, nb, common.DefaultDim, true)
   304  	_, errInsert := mc.InsertRows(ctx, collName, "", rows)
   305  	common.CheckErr(t, errInsert, false, "does not has field")
   306  }
   307  
   308  // test insert json collection
   309  func TestInsertJsonCollection(t *testing.T) {
   310  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   311  
   312  	// connect
   313  	mc := createMilvusClient(ctx, t)
   314  	nb := 1000
   315  
   316  	// create collection with json field named "json"
   317  	jsonField := common.GenField(common.DefaultJSONFieldName, entity.FieldTypeJSON)
   318  	fields1 := common.GenDefaultFields(true)
   319  	fields1 = append(fields1, jsonField)
   320  	collName := common.GenRandomString(4)
   321  	err := mc.CreateCollection(ctx, common.GenSchema(collName, true, fields1), common.DefaultShards)
   322  	common.CheckErr(t, err, true)
   323  
   324  	// insert rows to json collection
   325  	rows := common.GenDefaultJSONRows(0, nb, common.DefaultDim, true)
   326  	_, ok := rows[0].([]byte)
   327  	if !ok {
   328  		log.Printf("invalid type, expected []byte, got %T", rows)
   329  	}
   330  	_, errInsert := mc.InsertRows(ctx, collName, "", rows)
   331  	common.CheckErr(t, errInsert, true)
   332  
   333  	// insert json data column
   334  	_, floatColumn, vecColumn := common.GenDefaultColumnData(0, nb, common.DefaultDim)
   335  	jsonColumn := common.GenDefaultJSONData(common.DefaultJSONFieldName, 0, nb)
   336  	ids, errInsert := mc.Insert(ctx, collName, "", floatColumn, vecColumn, jsonColumn)
   337  	common.CheckErr(t, errInsert, true)
   338  	require.Equal(t, nb, ids.Len())
   339  
   340  	// flush and check row count
   341  	errFlush := mc.Flush(ctx, collName, false)
   342  	common.CheckErr(t, errFlush, true)
   343  	stats, _ := mc.GetCollectionStatistics(ctx, collName)
   344  	require.Equal(t, strconv.Itoa(nb*2), stats[common.RowCount])
   345  
   346  	// insert json data column less than other column
   347  	_, floatColumn, vecColumn = common.GenDefaultColumnData(0, nb, common.DefaultDim)
   348  	jsonColumn = common.GenDefaultJSONData(common.DefaultJSONFieldName, 0, nb/2)
   349  	ids, errInsert = mc.Insert(ctx, collName, "", floatColumn, vecColumn, jsonColumn)
   350  	common.CheckErr(t, errInsert, false, "column size not match")
   351  }
   352  
   353  // Test insert with dynamic field
   354  func TestInsertDynamicFieldData(t *testing.T) {
   355  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   356  
   357  	// connect
   358  	mc := createMilvusClient(ctx, t)
   359  	nb := 1000
   360  
   361  	// create collection enable dynamic field
   362  	schema := common.GenSchema(common.GenRandomString(6), false, common.GenDefaultFields(false), common.WithEnableDynamicField(true))
   363  	createCustomerCollection(ctx, t, mc, schema, common.DefaultShards)
   364  
   365  	// insert without dynamic field data
   366  	intColumn, floatColumn, vecColumn := common.GenDefaultColumnData(0, nb, common.DefaultDim)
   367  	ids, errInsert := mc.Insert(ctx, schema.CollectionName, "", intColumn, floatColumn, vecColumn)
   368  	common.CheckErr(t, errInsert, true)
   369  	require.Equal(t, nb, ids.Len())
   370  
   371  	// insert with extra varchar column data
   372  	intColumn, floatColumn, vecColumn = common.GenDefaultColumnData(nb, nb, common.DefaultDim)
   373  	varcharColumn, _ := common.GenDefaultVarcharData(nb, nb, 0)
   374  	ids, errInsert = mc.Insert(ctx, schema.CollectionName, "", intColumn, floatColumn, vecColumn, varcharColumn)
   375  	common.CheckErr(t, errInsert, true)
   376  	require.Equal(t, nb, ids.Len())
   377  
   378  	// insert with extra int64 column data
   379  	intColumn, floatColumn, vecColumn = common.GenDefaultColumnData(nb, nb, common.DefaultDim)
   380  	int64Values := make([]int64, 0, nb)
   381  	for i := 0; i < nb; i++ {
   382  		int64Values = append(int64Values, int64(i*10))
   383  	}
   384  	ids, errInsert = mc.Insert(ctx, schema.CollectionName, "", intColumn, floatColumn, vecColumn, entity.NewColumnInt64("aa", int64Values))
   385  	common.CheckErr(t, errInsert, true)
   386  	require.Equal(t, nb, ids.Len())
   387  
   388  	// flush and check row count
   389  	errFlush := mc.Flush(ctx, schema.CollectionName, false)
   390  	common.CheckErr(t, errFlush, true)
   391  	stats, _ := mc.GetCollectionStatistics(ctx, schema.CollectionName)
   392  	require.Equal(t, strconv.Itoa(nb*3), stats[common.RowCount])
   393  
   394  	// insert dynamic by rows struct
   395  	rows := common.GenDefaultRows(nb*2, nb, common.DefaultDim, false)
   396  	_, err := mc.InsertRows(context.Background(), schema.CollectionName, "", rows)
   397  	common.CheckErr(t, err, true)
   398  
   399  	// flush and check row count
   400  	errFlush = mc.Flush(ctx, schema.CollectionName, false)
   401  	common.CheckErr(t, errFlush, true)
   402  	stats, _ = mc.GetCollectionStatistics(ctx, schema.CollectionName)
   403  	require.Equalf(t, strconv.Itoa(nb*4), stats[common.RowCount], fmt.Sprintf("Expected row_count: %d, actual: %s", common.DefaultNb, stats[common.RowCount]))
   404  
   405  	// create index
   406  	idx, _ := entity.NewIndexHNSW(entity.L2, 8, 96)
   407  	err = mc.CreateIndex(ctx, schema.CollectionName, common.DefaultFloatVecFieldName, idx, false)
   408  	common.CheckErr(t, err, true)
   409  
   410  	// insert column data less other column
   411  	intColumn, floatColumn, vecColumn = common.GenDefaultColumnData(nb, nb, common.DefaultDim)
   412  	varcharColumn, _ = common.GenDefaultVarcharData(nb, nb/2, 0)
   413  	ids, errInsert = mc.Insert(ctx, schema.CollectionName, "", intColumn, floatColumn, vecColumn, varcharColumn)
   414  	common.CheckErr(t, errInsert, false, "column size not match")
   415  
   416  }
   417  
   418  // test insert array column with empty data
   419  func TestInsertEmptyArray(t *testing.T) {
   420  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   421  	// connect
   422  	mc := createMilvusClient(ctx, t)
   423  
   424  	// create collection
   425  	cp := CollectionParams{CollectionFieldsType: Int64FloatVecArray, AutoID: false, EnableDynamicField: true,
   426  		ShardsNum: common.DefaultShards, Dim: common.DefaultDim, MaxCapacity: common.TestCapacity}
   427  	collName := createCollection(ctx, t, mc, cp)
   428  
   429  	// prepare and insert data
   430  	var capacity int64
   431  	dp := DataParams{CollectionName: collName, PartitionName: "", CollectionFieldsType: Int64FloatVecArray,
   432  		start: 0, nb: common.DefaultNb, dim: common.DefaultDim, EnableDynamicField: true, WithRows: false}
   433  	_, _ = insertData(ctx, t, mc, dp, common.WithArrayCapacity(capacity))
   434  
   435  	// flush and check row count
   436  	errFlush := mc.Flush(ctx, collName, false)
   437  	common.CheckErr(t, errFlush, true)
   438  	stats, _ := mc.GetCollectionStatistics(ctx, collName)
   439  	require.Equal(t, strconv.Itoa(common.DefaultNb), stats[common.RowCount])
   440  }
   441  
   442  // test insert array type rows data
   443  func TestInsertArrayRows(t *testing.T) {
   444  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   445  	// connect
   446  	mc := createMilvusClient(ctx, t)
   447  
   448  	// create collection
   449  	for _, dynamic := range []bool{true, false} {
   450  		cp := CollectionParams{CollectionFieldsType: AllFields, AutoID: false, EnableDynamicField: dynamic,
   451  			ShardsNum: common.DefaultShards, Dim: common.DefaultDim, MaxCapacity: common.TestCapacity}
   452  		collName := createCollection(ctx, t, mc, cp)
   453  
   454  		// prepare and insert array rows data
   455  		dp := DataParams{CollectionName: collName, PartitionName: "", CollectionFieldsType: AllFields,
   456  			start: 0, nb: common.DefaultNb, dim: common.DefaultDim, EnableDynamicField: dynamic, WithRows: true}
   457  		_, _ = insertData(ctx, t, mc, dp, common.WithArrayCapacity(common.TestCapacity))
   458  
   459  		// flush and check row count
   460  		errFlush := mc.Flush(ctx, collName, false)
   461  		common.CheckErr(t, errFlush, true)
   462  		stats, _ := mc.GetCollectionStatistics(ctx, collName)
   463  		require.Equal(t, strconv.Itoa(common.DefaultNb), stats[common.RowCount])
   464  	}
   465  }
   466  
   467  // test insert array data type not match array field element type
   468  func TestInsertArrayDataTypeNotMatch(t *testing.T) {
   469  	t.Parallel()
   470  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   471  	// connect
   472  	mc := createMilvusClient(ctx, t)
   473  	for _, arrayType := range common.ArrayFieldType {
   474  
   475  		// fields: int64 + float + vector + array with TestCapacity
   476  		defaultFields := common.GenDefaultFields(false)
   477  		arrayField := common.GenField(common.DefaultArrayFieldName, entity.FieldTypeArray,
   478  			common.WithElementType(arrayType), common.WithMaxCapacity(100), common.WithMaxLength(100))
   479  		fields := append(defaultFields, arrayField)
   480  
   481  		// create collection
   482  		collName := common.GenRandomString(6)
   483  		schema := common.GenSchema(collName, false, fields, common.WithEnableDynamicField(true))
   484  		err := mc.CreateCollection(ctx, schema, common.DefaultShards)
   485  		common.CheckErr(t, err, true)
   486  
   487  		// insert data type does not match schema array element type
   488  		var columnType entity.FieldType
   489  		if arrayType == entity.FieldTypeInt64 {
   490  			columnType = entity.FieldTypeBool
   491  		} else {
   492  			columnType = entity.FieldTypeInt64
   493  		}
   494  		intColumn, floatColumn, vectorColumn := common.GenDefaultColumnData(0, common.DefaultNb, common.DefaultDim)
   495  		arrayColumn := common.GenArrayColumnData(0, common.DefaultNb, common.DefaultArrayFieldName,
   496  			common.WithArrayElementType(columnType))
   497  		_, err = mc.Insert(ctx, collName, "", intColumn, floatColumn, vectorColumn, arrayColumn)
   498  		common.CheckErr(t, err, false, "insert data does not match")
   499  	}
   500  }
   501  
   502  // test insert array column data that capacity exceeds max capacity
   503  func TestInsertArrayDataCapacityExceed(t *testing.T) {
   504  	t.Parallel()
   505  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   506  	// connect
   507  	mc := createMilvusClient(ctx, t)
   508  	for _, arrayType := range common.ArrayFieldType {
   509  		// fields: int64 + float + vector + array with TestCapacity
   510  		defaultFields := common.GenDefaultFields(false)
   511  		arrayField := common.GenField(common.DefaultArrayFieldName, entity.FieldTypeArray,
   512  			common.WithElementType(arrayType), common.WithMaxCapacity(common.TestCapacity), common.WithMaxLength(100))
   513  		fields := append(defaultFields, arrayField)
   514  
   515  		// create collection
   516  		collName := common.GenRandomString(6)
   517  		schema := common.GenSchema(collName, false, fields, common.WithEnableDynamicField(true))
   518  		err := mc.CreateCollection(ctx, schema, common.DefaultShards)
   519  		common.CheckErr(t, err, true)
   520  
   521  		// insert data capacity larger than TestCapacity
   522  		intColumn, floatColumn, vectorColumn := common.GenDefaultColumnData(0, common.DefaultNb, common.DefaultDim)
   523  		arrayColumn := common.GenArrayColumnData(0, common.DefaultNb, common.DefaultArrayFieldName,
   524  			common.WithArrayElementType(arrayType), common.WithArrayCapacity(common.TestCapacity+1))
   525  		_, err = mc.Insert(ctx, collName, "", intColumn, floatColumn, vectorColumn, arrayColumn)
   526  		common.CheckErr(t, err, false, "array length exceeds max capacity")
   527  	}
   528  }
   529  
   530  // test insert sparse vector column and rows
   531  func TestInsertSparseData(t *testing.T) {
   532  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   533  	// connect
   534  	mc := createMilvusClient(ctx, t)
   535  
   536  	cp := CollectionParams{CollectionFieldsType: Int64VarcharSparseVec, AutoID: false, EnableDynamicField: true,
   537  		ShardsNum: common.DefaultShards, Dim: common.DefaultDim, MaxLength: common.TestMaxLen}
   538  	collName := createCollection(ctx, t, mc, cp)
   539  
   540  	// insert data column
   541  	intColumn1 := common.GenColumnData(0, common.DefaultNb, entity.FieldTypeInt64, common.DefaultIntFieldName)
   542  	data := []entity.Column{
   543  		intColumn1,
   544  		common.GenColumnData(0, common.DefaultNb, entity.FieldTypeVarChar, common.DefaultVarcharFieldName),
   545  		common.GenColumnData(0, common.DefaultNb, entity.FieldTypeFloatVector, common.DefaultFloatVecFieldName, common.WithVectorDim(common.DefaultDim)),
   546  		common.GenColumnData(0, common.DefaultNb, entity.FieldTypeSparseVector, common.DefaultSparseVecFieldName, common.WithSparseVectorLen(20)),
   547  	}
   548  	ids, err := mc.Insert(ctx, collName, "", data...)
   549  	common.CheckErr(t, err, true)
   550  	common.CheckInsertResult(t, ids, intColumn1)
   551  
   552  	// insert rows
   553  	rows := common.GenDefaultSparseRows(common.DefaultNb, common.DefaultNb, common.DefaultDim, 50, true)
   554  	ids2, err := mc.InsertRows(ctx, collName, "", rows)
   555  	common.CheckErr(t, err, true)
   556  	require.Equal(t, ids2.Len(), common.DefaultNb)
   557  
   558  	// flush and verify
   559  	err = mc.Flush(ctx, collName, false)
   560  	common.CheckErr(t, err, true)
   561  	stats, _ := mc.GetCollectionStatistics(ctx, collName)
   562  	require.Equal(t, strconv.Itoa(common.DefaultNb*2), stats[common.RowCount])
   563  }
   564  
   565  // the dimension of a sparse embedding can be any value from 0 to (maximum of uint32 - 1)
   566  func TestInsertSparseDataMaxDim(t *testing.T) {
   567  	// invalid sparse vector: positions >= uint32
   568  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   569  	// connect
   570  	mc := createMilvusClient(ctx, t)
   571  
   572  	cp := CollectionParams{CollectionFieldsType: Int64VarcharSparseVec, AutoID: false, EnableDynamicField: true,
   573  		ShardsNum: common.DefaultShards, Dim: common.DefaultDim, MaxLength: common.TestMaxLen}
   574  	collName := createCollection(ctx, t, mc, cp)
   575  
   576  	// insert data column
   577  	pkColumn := common.GenColumnData(0, 1, entity.FieldTypeInt64, common.DefaultIntFieldName)
   578  	data := []entity.Column{
   579  		pkColumn,
   580  		common.GenColumnData(0, 1, entity.FieldTypeVarChar, common.DefaultVarcharFieldName),
   581  		common.GenColumnData(0, 1, entity.FieldTypeFloatVector, common.DefaultFloatVecFieldName, common.WithVectorDim(common.DefaultDim)),
   582  	}
   583  	// sparse vector with max dim
   584  	positions := []uint32{0, math.MaxUint32 - 10, math.MaxUint32 - 1}
   585  	values := []float32{0.453, 5.0776, 100.098}
   586  	sparseVec, err := entity.NewSliceSparseEmbedding(positions, values)
   587  	common.CheckErr(t, err, true)
   588  	data = append(data, entity.NewColumnSparseVectors(common.DefaultSparseVecFieldName, []entity.SparseEmbedding{sparseVec}))
   589  	ids, err := mc.Insert(ctx, collName, "", data...)
   590  	common.CheckErr(t, err, true)
   591  	common.CheckInsertResult(t, ids, pkColumn)
   592  }
   593  
   594  func TestInsertSparseInvalidVector(t *testing.T) {
   595  	// invalid sparse vector: len(positions) != len(values)
   596  	positions := []uint32{1, 10}
   597  	values := []float32{0.4, 5.0, 0.34}
   598  	_, err := entity.NewSliceSparseEmbedding(positions, values)
   599  	common.CheckErr(t, err, false, "invalid sparse embedding input, positions shall have same number of values")
   600  
   601  	// invalid sparse vector: positions >= uint32
   602  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   603  	// connect
   604  	mc := createMilvusClient(ctx, t)
   605  
   606  	cp := CollectionParams{CollectionFieldsType: Int64VarcharSparseVec, AutoID: false, EnableDynamicField: true,
   607  		ShardsNum: common.DefaultShards, Dim: common.DefaultDim, MaxLength: common.TestMaxLen}
   608  	collName := createCollection(ctx, t, mc, cp)
   609  
   610  	// insert data column
   611  	data := []entity.Column{
   612  		common.GenColumnData(0, 1, entity.FieldTypeInt64, common.DefaultIntFieldName),
   613  		common.GenColumnData(0, 1, entity.FieldTypeVarChar, common.DefaultVarcharFieldName),
   614  		common.GenColumnData(0, 1, entity.FieldTypeFloatVector, common.DefaultFloatVecFieldName, common.WithVectorDim(common.DefaultDim)),
   615  	}
   616  	// invalid sparse vector: position > (maximum of uint32 - 1)
   617  	positions = []uint32{math.MaxUint32}
   618  	values = []float32{0.4}
   619  	sparseVec, err := entity.NewSliceSparseEmbedding(positions, values)
   620  	common.CheckErr(t, err, true)
   621  	data1 := append(data, entity.NewColumnSparseVectors(common.DefaultSparseVecFieldName, []entity.SparseEmbedding{sparseVec}))
   622  	_, err = mc.Insert(ctx, collName, "", data1...)
   623  	common.CheckErr(t, err, false, "invalid index in sparse float vector: must be less than 2^32-1")
   624  
   625  	// invalid sparse vector: empty position and values
   626  	positions = []uint32{}
   627  	values = []float32{}
   628  	sparseVec, err = entity.NewSliceSparseEmbedding(positions, values)
   629  	common.CheckErr(t, err, true)
   630  	data2 := append(data, entity.NewColumnSparseVectors(common.DefaultSparseVecFieldName, []entity.SparseEmbedding{sparseVec}))
   631  	_, err = mc.Insert(ctx, collName, "", data2...)
   632  	common.CheckErr(t, err, false, "empty sparse float vector row")
   633  }
   634  
   635  func TestInsertSparseVectorSamePosition(t *testing.T) {
   636  	// invalid sparse vector: positions >= uint32
   637  	ctx := createContext(t, time.Second*common.DefaultTimeout)
   638  	// connect
   639  	mc := createMilvusClient(ctx, t)
   640  
   641  	cp := CollectionParams{CollectionFieldsType: Int64VarcharSparseVec, AutoID: false, EnableDynamicField: true,
   642  		ShardsNum: common.DefaultShards, Dim: common.DefaultDim, MaxLength: common.TestMaxLen}
   643  	collName := createCollection(ctx, t, mc, cp)
   644  
   645  	//insert data column
   646  	data := []entity.Column{
   647  		common.GenColumnData(0, 1, entity.FieldTypeInt64, common.DefaultIntFieldName),
   648  		common.GenColumnData(0, 1, entity.FieldTypeVarChar, common.DefaultVarcharFieldName),
   649  		common.GenColumnData(0, 1, entity.FieldTypeFloatVector, common.DefaultFloatVecFieldName, common.WithVectorDim(common.DefaultDim)),
   650  	}
   651  	//invalid sparse vector: position > (maximum of uint32 - 1)
   652  	sparseVec, err := entity.NewSliceSparseEmbedding([]uint32{2, 10, 2}, []float32{0.4, 0.5, 0.6})
   653  	common.CheckErr(t, err, true)
   654  	data = append(data, entity.NewColumnSparseVectors(common.DefaultSparseVecFieldName, []entity.SparseEmbedding{sparseVec}))
   655  	_, err = mc.Insert(ctx, collName, "", data...)
   656  	common.CheckErr(t, err, false, "unsorted or same indices in sparse float vector")
   657  }