github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/client/row_test.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/cockroachdb/errors"
    10  
    11  	"github.com/golang/protobuf/proto"
    12  	"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
    13  	"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
    14  	"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
    15  	"github.com/milvus-io/milvus-sdk-go/v2/entity"
    16  	"github.com/stretchr/testify/assert"
    17  	"github.com/stretchr/testify/mock"
    18  	"github.com/stretchr/testify/suite"
    19  )
    20  
    21  func TestCreateCollectionByRow(t *testing.T) {
    22  	ctx := context.Background()
    23  	c := testClient(ctx, t)
    24  	type ValidStruct struct {
    25  		entity.RowBase
    26  		ID     int64 `milvus:"primary_key"`
    27  		Attr1  int8
    28  		Attr2  int16
    29  		Attr3  int32
    30  		Attr4  float32
    31  		Attr5  float64
    32  		Attr6  string
    33  		Vector []float32 `milvus:"dim:4"`
    34  	}
    35  	t.Run("Test normal creation", func(t *testing.T) {
    36  		mockServer.DelInjection(MHasCollection)
    37  		shardsNum := int32(1)
    38  		mockServer.SetInjection(MCreateCollection, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
    39  			req, ok := raw.(*milvuspb.CreateCollectionRequest)
    40  			if !ok {
    41  				return &commonpb.Status{ErrorCode: commonpb.ErrorCode_IllegalArgument}, errors.New("illegal request type")
    42  			}
    43  			assert.Equal(t, "ValidStruct", req.GetCollectionName())
    44  			sschema := &schemapb.CollectionSchema{}
    45  			if !assert.Nil(t, proto.Unmarshal(req.GetSchema(), sschema)) {
    46  				assert.Equal(t, 8, len(sschema.Fields))
    47  				assert.Equal(t, shardsNum, req.GetShardsNum())
    48  			}
    49  
    50  			return &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success}, nil
    51  		})
    52  		assert.Nil(t, c.CreateCollectionByRow(ctx, &ValidStruct{}, shardsNum))
    53  	})
    54  
    55  	t.Run("Invalid cases", func(t *testing.T) {
    56  		//Duplicated
    57  		m := make(map[string]struct{})
    58  		mockServer.SetInjection(MCreateCollection, func(_ context.Context, raw proto.Message) (proto.Message, error) {
    59  			req, ok := raw.(*milvuspb.CreateCollectionRequest)
    60  			if !ok {
    61  				return BadRequestStatus()
    62  			}
    63  			m[req.GetCollectionName()] = struct{}{}
    64  
    65  			return SuccessStatus()
    66  		})
    67  		mockServer.SetInjection(MHasCollection, func(_ context.Context, raw proto.Message) (proto.Message, error) {
    68  			req, ok := raw.(*milvuspb.HasCollectionRequest)
    69  			resp := &milvuspb.BoolResponse{}
    70  			if !ok {
    71  				return BadRequestStatus()
    72  			}
    73  
    74  			_, has := m[req.GetCollectionName()]
    75  			resp.Value = has
    76  			s, err := SuccessStatus()
    77  			resp.Status = s
    78  			return resp, err
    79  		})
    80  		assert.Nil(t, c.CreateCollectionByRow(ctx, &ValidStruct{}, 1))
    81  		assert.NotNil(t, c.CreateCollectionByRow(ctx, &ValidStruct{}, 1))
    82  		// Invalid struct
    83  		anonymusStruct := struct {
    84  			entity.RowBase
    85  		}{}
    86  
    87  		assert.NotNil(t, c.CreateCollectionByRow(ctx, &anonymusStruct, 1))
    88  	})
    89  }
    90  
    91  type InsertByRowsSuite struct {
    92  	MockSuiteBase
    93  }
    94  
    95  func (s *InsertByRowsSuite) TestFails() {
    96  	ctx, cancel := context.WithCancel(context.Background())
    97  	defer cancel()
    98  
    99  	c := s.client
   100  
   101  	partName := "part_1"
   102  
   103  	s.Run("fail_empty_rows", func() {
   104  		defer s.resetMock()
   105  		_, err := c.InsertByRows(ctx, testCollectionName, partName, []entity.Row{})
   106  		s.Error(err)
   107  	})
   108  
   109  	s.Run("fail_collection_not_found", func() {
   110  		defer s.resetMock()
   111  		s.setupHasCollection()
   112  		_, err := c.InsertByRows(ctx, testCollectionName, partName, []entity.Row{entity.RowBase{}})
   113  		s.Error(err)
   114  	})
   115  
   116  	s.Run("fail_hascollection_errcode", func() {
   117  		defer s.resetMock()
   118  		s.setupHasCollectionError(commonpb.ErrorCode_UnexpectedError, nil)
   119  		_, err := c.InsertByRows(ctx, testCollectionName, partName, []entity.Row{entity.RowBase{}})
   120  		s.Error(err)
   121  	})
   122  
   123  	s.Run("fail_hascollection_error", func() {
   124  		defer s.resetMock()
   125  		s.setupHasCollectionError(commonpb.ErrorCode_Success, errors.New("mock error"))
   126  		_, err := c.InsertByRows(ctx, testCollectionName, partName, []entity.Row{entity.RowBase{}})
   127  		s.Error(err)
   128  	})
   129  
   130  	s.Run("fail_partition_not_found", func() {
   131  		defer s.resetMock()
   132  		s.setupHasCollection(testCollectionName)
   133  		s.setupHasPartition(testCollectionName)
   134  		_, err := c.InsertByRows(ctx, testCollectionName, partName, []entity.Row{entity.RowBase{}})
   135  		s.Error(err)
   136  	})
   137  
   138  	s.Run("fail_haspartition_error", func() {
   139  		defer s.resetMock()
   140  		s.setupHasCollection(testCollectionName)
   141  		s.setupHasPartitionError(commonpb.ErrorCode_Success, errors.New("mock error"))
   142  		_, err := c.InsertByRows(ctx, testCollectionName, partName, []entity.Row{entity.RowBase{}})
   143  		s.Error(err)
   144  	})
   145  
   146  	s.Run("fail_haspartition_errcode", func() {
   147  		defer s.resetMock()
   148  		s.setupHasCollection(testCollectionName)
   149  		s.setupHasPartitionError(commonpb.ErrorCode_UnexpectedError, nil)
   150  		_, err := c.InsertByRows(ctx, testCollectionName, partName, []entity.Row{entity.RowBase{}})
   151  		s.Error(err)
   152  	})
   153  
   154  	s.Run("fail_describecollection_error", func() {
   155  		defer s.resetMock()
   156  		s.setupHasCollection(testCollectionName)
   157  		s.setupHasPartition(testCollectionName, partName)
   158  		s.setupDescribeCollectionError(commonpb.ErrorCode_Success, errors.New("mock error"))
   159  		_, err := c.InsertByRows(ctx, testCollectionName, partName, []entity.Row{entity.RowBase{}})
   160  		s.Error(err)
   161  	})
   162  
   163  	s.Run("fail_describecollection_errcode", func() {
   164  		defer s.resetMock()
   165  		s.setupHasCollection(testCollectionName)
   166  		s.setupHasPartition(testCollectionName, partName)
   167  		s.setupDescribeCollectionError(commonpb.ErrorCode_UnexpectedError, nil)
   168  		_, err := c.InsertByRows(ctx, testCollectionName, partName, []entity.Row{entity.RowBase{}})
   169  		s.Error(err)
   170  	})
   171  
   172  	s.Run("fail_field_missing", func() {
   173  		defer s.resetMock()
   174  		s.setupHasCollection(testCollectionName)
   175  		s.setupHasPartition(testCollectionName, partName)
   176  		s.setupDescribeCollection(testCollectionName,
   177  			entity.NewSchema().
   178  				WithField(entity.NewField().WithName("ID").WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true).WithIsAutoID(true)).
   179  				WithField(entity.NewField().WithName("vector").WithDataType(entity.FieldTypeFloatVector).WithTypeParams(entity.TypeParamDim, "128")),
   180  		)
   181  		type row struct {
   182  			entity.RowBase
   183  			ID int64
   184  		}
   185  		_, err := c.InsertByRows(ctx, testCollectionName, partName, []entity.Row{row{ID: 100}})
   186  		s.Error(err)
   187  	})
   188  
   189  	s.Run("fail_field_type_not_match", func() {
   190  		defer s.resetMock()
   191  		s.setupHasCollection(testCollectionName)
   192  		s.setupHasPartition(testCollectionName, partName)
   193  		s.setupDescribeCollection(testCollectionName,
   194  			entity.NewSchema().
   195  				WithField(entity.NewField().WithName("ID").WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true).WithIsAutoID(true)).
   196  				WithField(entity.NewField().WithName("vector").WithDataType(entity.FieldTypeFloatVector).WithTypeParams(entity.TypeParamDim, "128")),
   197  		)
   198  		type row struct {
   199  			entity.RowBase
   200  			ID      string
   201  			Vectors []float32
   202  		}
   203  		_, err := c.InsertByRows(ctx, testCollectionName, partName, []entity.Row{row{ID: "100", Vectors: make([]float32, 128)}})
   204  		s.Error(err)
   205  	})
   206  
   207  	s.Run("fail_extra_field", func() {
   208  		defer s.resetMock()
   209  		s.setupHasCollection(testCollectionName)
   210  		s.setupHasPartition(testCollectionName, partName)
   211  		s.setupDescribeCollection(testCollectionName,
   212  			entity.NewSchema().
   213  				WithField(entity.NewField().WithName("ID").WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true).WithIsAutoID(true)).
   214  				WithField(entity.NewField().WithName("vector").WithDataType(entity.FieldTypeFloatVector).WithTypeParams(entity.TypeParamDim, "128")),
   215  		)
   216  		type row struct {
   217  			entity.RowBase
   218  			ID      int64
   219  			Extra   float64
   220  			Vectors []float32
   221  		}
   222  		_, err := c.InsertByRows(ctx, testCollectionName, partName, []entity.Row{row{ID: 100, Extra: 0.12, Vectors: make([]float32, 128)}})
   223  		s.Error(err)
   224  	})
   225  
   226  	s.Run("vector_dim_not_match", func() {
   227  		defer s.resetMock()
   228  		s.setupHasCollection(testCollectionName)
   229  		s.setupHasPartition(testCollectionName, partName)
   230  		s.setupDescribeCollection(testCollectionName,
   231  			entity.NewSchema().
   232  				WithField(entity.NewField().WithName("ID").WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true).WithIsAutoID(true)).
   233  				WithField(entity.NewField().WithName("vector").WithDataType(entity.FieldTypeFloatVector).WithTypeParams(entity.TypeParamDim, "128")),
   234  		)
   235  		type row struct {
   236  			entity.RowBase
   237  			ID      int64
   238  			Vectors []float32 `milvus:"dim:16"`
   239  		}
   240  		_, err := c.InsertByRows(ctx, testCollectionName, partName, []entity.Row{row{ID: 100, Vectors: make([]float32, 16)}})
   241  		s.Error(err)
   242  	})
   243  }
   244  
   245  func (s *InsertByRowsSuite) TestSuccess() {
   246  	partName := "part_1"
   247  	c := s.client
   248  	ctx, cancel := context.WithCancel(context.Background())
   249  	defer cancel()
   250  
   251  	s.Run("non_dynamic", func() {
   252  		defer s.resetMock()
   253  		s.setupHasCollection(testCollectionName)
   254  		s.setupHasPartition(testCollectionName, partName)
   255  		s.setupDescribeCollection(testCollectionName, entity.NewSchema().WithName(testCollectionName).
   256  			WithField(entity.NewField().WithName("ID").WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true)).
   257  			WithField(entity.NewField().WithName("Vector").WithDataType(entity.FieldTypeFloatVector).WithTypeParams(entity.TypeParamDim, "128")),
   258  		)
   259  
   260  		s.mock.EXPECT().Insert(mock.Anything, mock.AnythingOfType("*milvuspb.InsertRequest")).
   261  			Run(func(_ context.Context, req *milvuspb.InsertRequest) {
   262  				s.Equal(testCollectionName, req.GetCollectionName())
   263  				s.Equal(partName, req.GetPartitionName())
   264  				s.Equal(2, len(req.GetFieldsData()))
   265  			}).Return(&milvuspb.MutationResult{
   266  			Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success},
   267  			IDs:    &schemapb.IDs{IdField: &schemapb.IDs_IntId{IntId: &schemapb.LongArray{Data: []int64{100}}}},
   268  		}, nil)
   269  		type row struct {
   270  			entity.RowBase
   271  			ID     int64
   272  			Vector []float32
   273  		}
   274  		ids, err := c.InsertByRows(ctx, testCollectionName, partName, []entity.Row{
   275  			row{ID: 100, Vector: make([]float32, 128)},
   276  		})
   277  		s.NoError(err)
   278  		s.Equal(1, ids.Len())
   279  	})
   280  
   281  	s.Run("dynamic", func() {
   282  		defer s.resetMock()
   283  		s.setupHasCollection(testCollectionName)
   284  		s.setupHasPartition(testCollectionName, partName)
   285  		s.setupDescribeCollection(testCollectionName, entity.NewSchema().
   286  			WithName(testCollectionName).WithDynamicFieldEnabled(true).
   287  			WithField(entity.NewField().WithName("ID").WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true)).
   288  			WithField(entity.NewField().WithName("Vector").WithDataType(entity.FieldTypeFloatVector).WithTypeParams(entity.TypeParamDim, "128")),
   289  		)
   290  
   291  		s.mock.EXPECT().Insert(mock.Anything, mock.AnythingOfType("*milvuspb.InsertRequest")).
   292  			Run(func(_ context.Context, req *milvuspb.InsertRequest) {
   293  				s.Equal(testCollectionName, req.GetCollectionName())
   294  				s.Equal(partName, req.GetPartitionName())
   295  				s.Equal(3, len(req.GetFieldsData()))
   296  			}).Return(&milvuspb.MutationResult{
   297  			Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success},
   298  			IDs:    &schemapb.IDs{IdField: &schemapb.IDs_IntId{IntId: &schemapb.LongArray{Data: []int64{100}}}},
   299  		}, nil)
   300  		type row struct {
   301  			entity.RowBase
   302  			ID     int64
   303  			Vector []float32
   304  			Extra1 int32
   305  			Extra2 string
   306  		}
   307  		ids, err := c.InsertByRows(ctx, testCollectionName, partName, []entity.Row{
   308  			row{ID: 100, Vector: make([]float32, 128), Extra1: 1, Extra2: "aabb"},
   309  		})
   310  		s.NoError(err)
   311  		s.Equal(1, ids.Len())
   312  	})
   313  }
   314  
   315  func TestInsertByRows(t *testing.T) {
   316  	suite.Run(t, new(InsertByRowsSuite))
   317  }
   318  
   319  func TestSearchResultToRows(t *testing.T) {
   320  	t.Run("successful test cases", func(t *testing.T) {
   321  		sr := &schemapb.SearchResultData{
   322  			NumQueries: 1,
   323  			TopK:       3,
   324  			FieldsData: []*schemapb.FieldData{
   325  				longFieldData("ID", []int64{1, 2, 3}),
   326  				intFieldData("Attr1", []int32{1, 2, 3}),
   327  				intFieldData("Attr2", []int32{1, 2, 3}),
   328  				intFieldData("Attr3", []int32{1, 2, 3}),
   329  				floatFieldData("Attr4", []float32{0.1, 0.2, 0.3}),
   330  				doubleFieldData("Attr5", []float64{0.1, 0.2, 0.3}),
   331  				stringFieldData("Attr6", []string{"1", "2", "3"}),
   332  				floatVectorFieldData("Vector", 4, []float32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}),
   333  			},
   334  			Scores: []float32{0.1, 0.2, 0.3},
   335  			Ids: &schemapb.IDs{
   336  				IdField: &schemapb.IDs_IntId{
   337  					IntId: &schemapb.LongArray{
   338  						Data: []int64{1, 2, 3},
   339  					},
   340  				},
   341  			},
   342  			Topks: []int64{3},
   343  		}
   344  
   345  		type ValidStruct struct {
   346  			entity.RowBase
   347  			ID     int64 `milvus:"primary_key"`
   348  			Attr1  int8
   349  			Attr2  int16
   350  			Attr3  int32
   351  			Attr4  float32
   352  			Attr5  float64
   353  			Attr6  string
   354  			Vector []float32 `milvus:"dim:4"`
   355  		}
   356  		sch, err := entity.ParseSchema(&ValidStruct{})
   357  		assert.Nil(t, err)
   358  		results, err := SearchResultToRows(sch, sr, reflect.TypeOf(&ValidStruct{}), arrMap("ID", "Vector", "Attr1", "Attr2", "Attr3", "Attr4", "Attr5", "Attr6"))
   359  		assert.Nil(t, err)
   360  		assert.NotNil(t, results)
   361  		if assert.Equal(t, 1, len(results)) {
   362  			result := results[0]
   363  			assert.EqualValues(t, []float32{0.1, 0.2, 0.3}, result.Scores)
   364  			if assert.Nil(t, result.Err) {
   365  				if !assert.Equal(t, 3, len(result.Rows)) {
   366  					t.FailNow()
   367  				}
   368  				for i, row := range result.Rows {
   369  					s, ok := row.(*ValidStruct)
   370  					if assert.True(t, ok) {
   371  						assert.EqualValues(t, i+1, s.ID)
   372  						assert.EqualValues(t, i+1, s.Attr1)
   373  						assert.EqualValues(t, i+1, s.Attr2)
   374  						assert.EqualValues(t, i+1, s.Attr3)
   375  						assert.EqualValues(t, float32(i+1)/10.0, s.Attr4)
   376  						assert.EqualValues(t, float64(i+1)/10.0, s.Attr5)
   377  						assert.Equal(t, fmt.Sprintf("%d", i+1), s.Attr6)
   378  					}
   379  				}
   380  			}
   381  		}
   382  	})
   383  }
   384  
   385  func arrMap(items ...string) map[string]struct{} {
   386  	r := make(map[string]struct{})
   387  	for _, item := range items {
   388  		r[item] = struct{}{}
   389  	}
   390  	return r
   391  }
   392  
   393  func TestSetFieldValue(t *testing.T) {
   394  	type TestStruct struct {
   395  		Bool   bool
   396  		Int8   int8
   397  		Int16  int16
   398  		Int32  int32
   399  		Int64  int64
   400  		Float  float32
   401  		Double float64
   402  		String string
   403  		Arr    [8]float32
   404  		ArrBin [8]byte
   405  	}
   406  
   407  	t.Run("successful cases", func(t *testing.T) {
   408  		var err error
   409  		item := &TestStruct{}
   410  		// test bool field
   411  		b := reflect.ValueOf(item).Elem().FieldByName("Bool")
   412  		err = SetFieldValue(&entity.Field{
   413  			DataType: entity.FieldTypeBool,
   414  		}, b, boolFieldData("", []bool{true}), 0)
   415  		assert.Nil(t, err)
   416  		assert.EqualValues(t, true, item.Bool)
   417  
   418  		// test int 8 field
   419  		i8 := reflect.ValueOf(item).Elem().FieldByName("Int8")
   420  		err = SetFieldValue(&entity.Field{
   421  			DataType: entity.FieldTypeInt8,
   422  		}, i8, intFieldData("", []int32{10}), 0)
   423  		assert.Nil(t, err)
   424  		assert.EqualValues(t, 10, item.Int8)
   425  
   426  		i16 := reflect.ValueOf(item).Elem().FieldByName("Int16")
   427  		err = SetFieldValue(&entity.Field{
   428  			DataType: entity.FieldTypeInt16,
   429  		}, i16, intFieldData("", []int32{10}), 0)
   430  		assert.Nil(t, err)
   431  		assert.EqualValues(t, 10, item.Int16)
   432  
   433  		i32 := reflect.ValueOf(item).Elem().FieldByName("Int32")
   434  		err = SetFieldValue(&entity.Field{
   435  			DataType: entity.FieldTypeInt32,
   436  		}, i32, intFieldData("", []int32{10}), 0)
   437  		assert.Nil(t, err)
   438  		assert.EqualValues(t, 10, item.Int32)
   439  
   440  		i64 := reflect.ValueOf(item).Elem().FieldByName("Int64")
   441  		err = SetFieldValue(&entity.Field{
   442  			DataType: entity.FieldTypeInt64,
   443  		}, i64, longFieldData("", []int64{10}), 0)
   444  		assert.Nil(t, err)
   445  		assert.EqualValues(t, 10, item.Int64)
   446  
   447  		f32 := reflect.ValueOf(item).Elem().FieldByName("Float")
   448  		err = SetFieldValue(&entity.Field{
   449  			DataType: entity.FieldTypeFloat,
   450  		}, f32, floatFieldData("", []float32{0.618}), 0)
   451  		assert.Nil(t, err)
   452  		assert.InDelta(t, 0.618, item.Float, 1e-6)
   453  
   454  		f64 := reflect.ValueOf(item).Elem().FieldByName("Double")
   455  		err = SetFieldValue(&entity.Field{
   456  			DataType: entity.FieldTypeDouble,
   457  		}, f64, doubleFieldData("", []float64{0.618}), 0)
   458  		assert.Nil(t, err)
   459  		assert.InDelta(t, 0.618, item.Double, 1e-6)
   460  
   461  		str := reflect.ValueOf(item).Elem().FieldByName("String")
   462  		err = SetFieldValue(&entity.Field{
   463  			DataType: entity.FieldTypeString,
   464  		}, str, stringFieldData("", []string{"test"}), 0)
   465  		assert.Nil(t, err)
   466  		assert.EqualValues(t, "test", item.String)
   467  
   468  		// float32 array field
   469  		arr := reflect.ValueOf(item).Elem().FieldByName("Arr")
   470  		err = SetFieldValue(&entity.Field{
   471  			DataType: entity.FieldTypeFloatVector,
   472  		}, arr, floatVectorFieldData("", 8, []float32{0, 1, 2, 3, 4, 5, 6, 7}), 0)
   473  		assert.Nil(t, err)
   474  		assert.EqualValues(t, [8]float32{0, 1, 2, 3, 4, 5, 6, 7}, item.Arr)
   475  
   476  		binArr := reflect.ValueOf(item).Elem().FieldByName("ArrBin")
   477  		err = SetFieldValue(&entity.Field{
   478  			DataType: entity.FieldTypeBinaryVector,
   479  		}, binArr, binaryVectorFieldData("", []byte{'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'}), 0)
   480  		assert.Nil(t, err)
   481  		assert.EqualValues(t, [8]byte{'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'}, item.ArrBin)
   482  	})
   483  
   484  	t.Run("fail cases", func(t *testing.T) {
   485  		var err error
   486  		item := &TestStruct{}
   487  		// invalid data type
   488  		b := reflect.ValueOf(item).Elem().FieldByName("Bool")
   489  		i8 := reflect.ValueOf(item).Elem().FieldByName("Int8")
   490  		i16 := reflect.ValueOf(item).Elem().FieldByName("Int16")
   491  		i32 := reflect.ValueOf(item).Elem().FieldByName("Int32")
   492  		i64 := reflect.ValueOf(item).Elem().FieldByName("Int64")
   493  		f32 := reflect.ValueOf(item).Elem().FieldByName("Float")
   494  		f64 := reflect.ValueOf(item).Elem().FieldByName("Double")
   495  		str := reflect.ValueOf(item).Elem().FieldByName("String")
   496  		vf := reflect.ValueOf(item).Elem().FieldByName("Arr")
   497  		//vb := reflect.ValueOf(item).Elem().FieldByName("ArrBin")
   498  
   499  		err = SetFieldValue(&entity.Field{
   500  			DataType: entity.FieldTypeNone,
   501  		}, b, boolFieldData("", []bool{true}), 0)
   502  		assert.Equal(t, ErrFieldTypeNotMatch, err)
   503  		// field type not matched cases
   504  
   505  		// bool
   506  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeBool}, i8, boolFieldData("", []bool{true}), 0)
   507  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   508  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeBool}, b, emptyFieldData(), 0)
   509  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   510  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeBool}, b, emptyScalarFieldData(), 0)
   511  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   512  
   513  		// int8
   514  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeInt8}, b, intFieldData("", []int32{10}), 0)
   515  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   516  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeInt8}, i8, emptyFieldData(), 0)
   517  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   518  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeInt8}, i8, emptyScalarFieldData(), 0)
   519  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   520  
   521  		// int16
   522  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeInt16}, b, intFieldData("", []int32{10}), 0)
   523  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   524  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeInt16}, i16, emptyFieldData(), 0)
   525  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   526  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeInt16}, i16, emptyScalarFieldData(), 0)
   527  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   528  
   529  		// int32
   530  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeInt32}, b, intFieldData("", []int32{10}), 0)
   531  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   532  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeInt32}, i32, emptyFieldData(), 0)
   533  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   534  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeInt32}, i32, emptyScalarFieldData(), 0)
   535  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   536  
   537  		// int64
   538  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeInt64}, b, longFieldData("", []int64{10}), 0)
   539  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   540  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeInt64}, i64, emptyFieldData(), 0)
   541  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   542  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeInt64}, i64, emptyScalarFieldData(), 0)
   543  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   544  
   545  		// float
   546  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeFloat}, b, floatFieldData("", []float32{0.6}), 0)
   547  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   548  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeFloat}, f32, emptyFieldData(), 0)
   549  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   550  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeFloat}, f32, emptyScalarFieldData(), 0)
   551  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   552  
   553  		// double
   554  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeDouble}, b, doubleFieldData("", []float64{0.6}), 0)
   555  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   556  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeDouble}, f64, emptyFieldData(), 0)
   557  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   558  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeDouble}, f64, emptyScalarFieldData(), 0)
   559  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   560  
   561  		// string
   562  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeString}, b, stringFieldData("", []string{"test"}), 0)
   563  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   564  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeString}, str, emptyFieldData(), 0)
   565  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   566  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeString}, str, emptyScalarFieldData(), 0)
   567  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   568  
   569  		// float vector
   570  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeFloatVector}, b, floatVectorFieldData("", 4, []float32{1, 2, 3, 4}), 0)
   571  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   572  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeFloatVector}, vf, emptyFieldData(), 0)
   573  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   574  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeFloatVector}, vf, emptyVectorFieldData(), 0)
   575  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   576  
   577  		// binary vector
   578  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeBinaryVector}, b, binaryVectorFieldData("", []byte{1, 2, 3, 4}), 0)
   579  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   580  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeBinaryVector}, vf, emptyFieldData(), 0)
   581  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   582  		err = SetFieldValue(&entity.Field{DataType: entity.FieldTypeBinaryVector}, vf, emptyVectorFieldData(), 0)
   583  		assert.Equal(t, err, ErrFieldTypeNotMatch)
   584  	})
   585  }
   586  
   587  func emptyFieldData() *schemapb.FieldData {
   588  	return &schemapb.FieldData{}
   589  }
   590  func emptyScalarFieldData() *schemapb.FieldData {
   591  	return &schemapb.FieldData{
   592  		Field: &schemapb.FieldData_Scalars{
   593  			Scalars: &schemapb.ScalarField{},
   594  		},
   595  	}
   596  }
   597  
   598  func emptyVectorFieldData() *schemapb.FieldData {
   599  	return &schemapb.FieldData{
   600  		Field: &schemapb.FieldData_Vectors{
   601  			Vectors: &schemapb.VectorField{},
   602  		},
   603  	}
   604  }
   605  
   606  func boolFieldData(name string, data []bool) *schemapb.FieldData {
   607  	return &schemapb.FieldData{
   608  		FieldName: name,
   609  		Type:      schemapb.DataType_Bool,
   610  		Field: &schemapb.FieldData_Scalars{
   611  			Scalars: &schemapb.ScalarField{
   612  				Data: &schemapb.ScalarField_BoolData{
   613  					BoolData: &schemapb.BoolArray{
   614  						Data: data,
   615  					},
   616  				},
   617  			},
   618  		},
   619  	}
   620  }
   621  
   622  func intFieldData(name string, data []int32) *schemapb.FieldData {
   623  	return &schemapb.FieldData{
   624  		FieldName: name,
   625  		// Type not determined
   626  		Field: &schemapb.FieldData_Scalars{
   627  			Scalars: &schemapb.ScalarField{
   628  				Data: &schemapb.ScalarField_IntData{
   629  					IntData: &schemapb.IntArray{
   630  						Data: data,
   631  					},
   632  				},
   633  			},
   634  		},
   635  	}
   636  }
   637  
   638  func longFieldData(name string, data []int64) *schemapb.FieldData {
   639  	return &schemapb.FieldData{
   640  		FieldName: name,
   641  		Type:      schemapb.DataType_Int64,
   642  		Field: &schemapb.FieldData_Scalars{
   643  			Scalars: &schemapb.ScalarField{
   644  				Data: &schemapb.ScalarField_LongData{
   645  					LongData: &schemapb.LongArray{
   646  						Data: data,
   647  					},
   648  				},
   649  			},
   650  		},
   651  	}
   652  }
   653  
   654  func floatFieldData(name string, data []float32) *schemapb.FieldData {
   655  	return &schemapb.FieldData{
   656  		FieldName: name,
   657  		Type:      schemapb.DataType_Float,
   658  		Field: &schemapb.FieldData_Scalars{
   659  			Scalars: &schemapb.ScalarField{
   660  				Data: &schemapb.ScalarField_FloatData{
   661  					FloatData: &schemapb.FloatArray{
   662  						Data: data,
   663  					},
   664  				},
   665  			},
   666  		},
   667  	}
   668  }
   669  
   670  func doubleFieldData(name string, data []float64) *schemapb.FieldData {
   671  	return &schemapb.FieldData{
   672  		FieldName: name,
   673  		Type:      schemapb.DataType_Double,
   674  		Field: &schemapb.FieldData_Scalars{
   675  			Scalars: &schemapb.ScalarField{
   676  				Data: &schemapb.ScalarField_DoubleData{
   677  					DoubleData: &schemapb.DoubleArray{
   678  						Data: data,
   679  					},
   680  				},
   681  			},
   682  		},
   683  	}
   684  }
   685  
   686  func stringFieldData(name string, data []string) *schemapb.FieldData {
   687  	return &schemapb.FieldData{
   688  		FieldName: name,
   689  		Type:      schemapb.DataType_String,
   690  		Field: &schemapb.FieldData_Scalars{
   691  			Scalars: &schemapb.ScalarField{
   692  				Data: &schemapb.ScalarField_StringData{
   693  					StringData: &schemapb.StringArray{
   694  						Data: data,
   695  					},
   696  				},
   697  			},
   698  		},
   699  	}
   700  }
   701  
   702  func floatVectorFieldData(name string, dim int, data []float32) *schemapb.FieldData {
   703  	return &schemapb.FieldData{
   704  		FieldName: name,
   705  		Type:      schemapb.DataType_FloatVector,
   706  		Field: &schemapb.FieldData_Vectors{
   707  			Vectors: &schemapb.VectorField{
   708  				Dim: int64(dim),
   709  				Data: &schemapb.VectorField_FloatVector{
   710  					FloatVector: &schemapb.FloatArray{
   711  						Data: data,
   712  					},
   713  				},
   714  			},
   715  		},
   716  	}
   717  }
   718  
   719  func binaryVectorFieldData(name string, data []byte) *schemapb.FieldData {
   720  	return &schemapb.FieldData{
   721  		FieldName: name,
   722  		Type:      schemapb.DataType_BinaryVector,
   723  		Field: &schemapb.FieldData_Vectors{
   724  			Vectors: &schemapb.VectorField{
   725  				Dim: int64(8 * len(data)),
   726  				Data: &schemapb.VectorField_BinaryVector{
   727  					BinaryVector: data,
   728  				},
   729  			},
   730  		},
   731  	}
   732  }