github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/entity/schema.go (about)

     1  // Copyright (C) 2019-2021 Zilliz. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
     4  // with the License. You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software distributed under the License
     9  // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
    10  // or implied. See the License for the specific language governing permissions and limitations under the License.
    11  
    12  package entity
    13  
    14  import (
    15  	"strconv"
    16  
    17  	common "github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
    18  	schema "github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
    19  )
    20  
    21  const (
    22  	// TypeParamDim is the const for field type param dimension
    23  	TypeParamDim = "dim"
    24  
    25  	// TypeParamMaxLength is the const for varchar type maximal length
    26  	TypeParamMaxLength = "max_length"
    27  
    28  	// TypeParamMaxCapacity is the const for array type max capacity
    29  	TypeParamMaxCapacity = `max_capacity`
    30  
    31  	// ClStrong strong consistency level
    32  	ClStrong ConsistencyLevel = ConsistencyLevel(common.ConsistencyLevel_Strong)
    33  	// ClBounded bounded consistency level with default tolerance of 5 seconds
    34  	ClBounded ConsistencyLevel = ConsistencyLevel(common.ConsistencyLevel_Bounded)
    35  	// ClSession session consistency level
    36  	ClSession ConsistencyLevel = ConsistencyLevel(common.ConsistencyLevel_Session)
    37  	// ClEvenually eventually consistency level
    38  	ClEventually ConsistencyLevel = ConsistencyLevel(common.ConsistencyLevel_Eventually)
    39  	// ClCustomized customized consistency level and users pass their own `guarantee_timestamp`.
    40  	ClCustomized ConsistencyLevel = ConsistencyLevel(common.ConsistencyLevel_Customized)
    41  )
    42  
    43  // ConsistencyLevel enum type for collection Consistency Level
    44  type ConsistencyLevel common.ConsistencyLevel
    45  
    46  // CommonConsistencyLevel returns corresponding common.ConsistencyLevel
    47  func (cl ConsistencyLevel) CommonConsistencyLevel() common.ConsistencyLevel {
    48  	return common.ConsistencyLevel(cl)
    49  }
    50  
    51  // Schema represents schema info of collection in milvus
    52  type Schema struct {
    53  	CollectionName     string
    54  	Description        string
    55  	AutoID             bool
    56  	Fields             []*Field
    57  	EnableDynamicField bool
    58  	pkField            *Field
    59  }
    60  
    61  // NewSchema creates an empty schema object.
    62  func NewSchema() *Schema {
    63  	return &Schema{}
    64  }
    65  
    66  // WithName sets the name value of schema, returns schema itself.
    67  func (s *Schema) WithName(name string) *Schema {
    68  	s.CollectionName = name
    69  	return s
    70  }
    71  
    72  // WithDescription sets the description value of schema, returns schema itself.
    73  func (s *Schema) WithDescription(desc string) *Schema {
    74  	s.Description = desc
    75  	return s
    76  }
    77  
    78  func (s *Schema) WithAutoID(autoID bool) *Schema {
    79  	s.AutoID = autoID
    80  	return s
    81  }
    82  
    83  func (s *Schema) WithDynamicFieldEnabled(dynamicEnabled bool) *Schema {
    84  	s.EnableDynamicField = dynamicEnabled
    85  	return s
    86  }
    87  
    88  // WithField adds a field into schema and returns schema itself.
    89  func (s *Schema) WithField(f *Field) *Schema {
    90  	if f.PrimaryKey {
    91  		s.pkField = f
    92  	}
    93  	s.Fields = append(s.Fields, f)
    94  	return s
    95  }
    96  
    97  // ProtoMessage returns corresponding server.CollectionSchema
    98  func (s *Schema) ProtoMessage() *schema.CollectionSchema {
    99  	r := &schema.CollectionSchema{
   100  		Name:               s.CollectionName,
   101  		Description:        s.Description,
   102  		AutoID:             s.AutoID,
   103  		EnableDynamicField: s.EnableDynamicField,
   104  	}
   105  	r.Fields = make([]*schema.FieldSchema, 0, len(s.Fields))
   106  	for _, field := range s.Fields {
   107  		r.Fields = append(r.Fields, field.ProtoMessage())
   108  	}
   109  	return r
   110  }
   111  
   112  // ReadProto parses proto Collection Schema
   113  func (s *Schema) ReadProto(p *schema.CollectionSchema) *Schema {
   114  	s.AutoID = p.GetAutoID()
   115  	s.Description = p.GetDescription()
   116  	s.CollectionName = p.GetName()
   117  	s.Fields = make([]*Field, 0, len(p.GetFields()))
   118  	for _, fp := range p.GetFields() {
   119  		field := NewField().ReadProto(fp)
   120  		if field.PrimaryKey {
   121  			s.pkField = field
   122  		}
   123  		s.Fields = append(s.Fields, field)
   124  	}
   125  	s.EnableDynamicField = p.GetEnableDynamicField()
   126  	return s
   127  }
   128  
   129  // PKFieldName returns pk field name for this schema.
   130  func (s *Schema) PKFieldName() string {
   131  	if s.pkField == nil {
   132  		return ""
   133  	}
   134  	return s.pkField.Name
   135  }
   136  
   137  // PKField returns PK Field schema for this schema.
   138  func (s *Schema) PKField() *Field {
   139  	return s.pkField
   140  }
   141  
   142  // Field represent field schema in milvus
   143  type Field struct {
   144  	ID             int64  // field id, generated when collection is created, input value is ignored
   145  	Name           string // field name
   146  	PrimaryKey     bool   // is primary key
   147  	AutoID         bool   // is auto id
   148  	Description    string
   149  	DataType       FieldType
   150  	TypeParams     map[string]string
   151  	IndexParams    map[string]string
   152  	IsDynamic      bool
   153  	IsPartitionKey bool
   154  	ElementType    FieldType
   155  }
   156  
   157  // ProtoMessage generates corresponding FieldSchema
   158  func (f *Field) ProtoMessage() *schema.FieldSchema {
   159  	return &schema.FieldSchema{
   160  		FieldID:        f.ID,
   161  		Name:           f.Name,
   162  		Description:    f.Description,
   163  		IsPrimaryKey:   f.PrimaryKey,
   164  		AutoID:         f.AutoID,
   165  		DataType:       schema.DataType(f.DataType),
   166  		TypeParams:     MapKvPairs(f.TypeParams),
   167  		IndexParams:    MapKvPairs(f.IndexParams),
   168  		IsDynamic:      f.IsDynamic,
   169  		IsPartitionKey: f.IsPartitionKey,
   170  		ElementType:    schema.DataType(f.ElementType),
   171  	}
   172  }
   173  
   174  // NewField creates a new Field with map initialized.
   175  func NewField() *Field {
   176  	return &Field{
   177  		TypeParams:  make(map[string]string),
   178  		IndexParams: make(map[string]string),
   179  	}
   180  }
   181  
   182  func (f *Field) WithName(name string) *Field {
   183  	f.Name = name
   184  	return f
   185  }
   186  
   187  func (f *Field) WithDescription(desc string) *Field {
   188  	f.Description = desc
   189  	return f
   190  }
   191  
   192  func (f *Field) WithDataType(dataType FieldType) *Field {
   193  	f.DataType = dataType
   194  	return f
   195  }
   196  
   197  func (f *Field) WithIsPrimaryKey(isPrimaryKey bool) *Field {
   198  	f.PrimaryKey = isPrimaryKey
   199  	return f
   200  }
   201  
   202  func (f *Field) WithIsAutoID(isAutoID bool) *Field {
   203  	f.AutoID = isAutoID
   204  	return f
   205  }
   206  
   207  func (f *Field) WithIsDynamic(isDynamic bool) *Field {
   208  	f.IsDynamic = isDynamic
   209  	return f
   210  }
   211  
   212  func (f *Field) WithIsPartitionKey(isPartitionKey bool) *Field {
   213  	f.IsPartitionKey = isPartitionKey
   214  	return f
   215  }
   216  
   217  /*
   218  func (f *Field) WithDefaultValueBool(defaultValue bool) *Field {
   219  	f.DefaultValue = &schema.ValueField{
   220  		Data: &schema.ValueField_BoolData{
   221  			BoolData: defaultValue,
   222  		},
   223  	}
   224  	return f
   225  }
   226  
   227  func (f *Field) WithDefaultValueInt(defaultValue int32) *Field {
   228  	f.DefaultValue = &schema.ValueField{
   229  		Data: &schema.ValueField_IntData{
   230  			IntData: defaultValue,
   231  		},
   232  	}
   233  	return f
   234  }
   235  
   236  func (f *Field) WithDefaultValueLong(defaultValue int64) *Field {
   237  	f.DefaultValue = &schema.ValueField{
   238  		Data: &schema.ValueField_LongData{
   239  			LongData: defaultValue,
   240  		},
   241  	}
   242  	return f
   243  }
   244  
   245  func (f *Field) WithDefaultValueFloat(defaultValue float32) *Field {
   246  	f.DefaultValue = &schema.ValueField{
   247  		Data: &schema.ValueField_FloatData{
   248  			FloatData: defaultValue,
   249  		},
   250  	}
   251  	return f
   252  }
   253  
   254  func (f *Field) WithDefaultValueDouble(defaultValue float64) *Field {
   255  	f.DefaultValue = &schema.ValueField{
   256  		Data: &schema.ValueField_DoubleData{
   257  			DoubleData: defaultValue,
   258  		},
   259  	}
   260  	return f
   261  }
   262  
   263  func (f *Field) WithDefaultValueString(defaultValue string) *Field {
   264  	f.DefaultValue = &schema.ValueField{
   265  		Data: &schema.ValueField_StringData{
   266  			StringData: defaultValue,
   267  		},
   268  	}
   269  	return f
   270  }*/
   271  
   272  func (f *Field) WithTypeParams(key string, value string) *Field {
   273  	if f.TypeParams == nil {
   274  		f.TypeParams = make(map[string]string)
   275  	}
   276  	f.TypeParams[key] = value
   277  	return f
   278  }
   279  
   280  func (f *Field) WithDim(dim int64) *Field {
   281  	if f.TypeParams == nil {
   282  		f.TypeParams = make(map[string]string)
   283  	}
   284  	f.TypeParams[TypeParamDim] = strconv.FormatInt(dim, 10)
   285  	return f
   286  }
   287  
   288  func (f *Field) WithMaxLength(maxLen int64) *Field {
   289  	if f.TypeParams == nil {
   290  		f.TypeParams = make(map[string]string)
   291  	}
   292  	f.TypeParams[TypeParamMaxLength] = strconv.FormatInt(maxLen, 10)
   293  	return f
   294  }
   295  
   296  func (f *Field) WithElementType(eleType FieldType) *Field {
   297  	f.ElementType = eleType
   298  	return f
   299  }
   300  
   301  func (f *Field) WithMaxCapacity(maxCap int64) *Field {
   302  	if f.TypeParams == nil {
   303  		f.TypeParams = make(map[string]string)
   304  	}
   305  	f.TypeParams[TypeParamMaxCapacity] = strconv.FormatInt(maxCap, 10)
   306  	return f
   307  }
   308  
   309  // ReadProto parses FieldSchema
   310  func (f *Field) ReadProto(p *schema.FieldSchema) *Field {
   311  	f.ID = p.GetFieldID()
   312  	f.Name = p.GetName()
   313  	f.PrimaryKey = p.GetIsPrimaryKey()
   314  	f.AutoID = p.GetAutoID()
   315  	f.Description = p.GetDescription()
   316  	f.DataType = FieldType(p.GetDataType())
   317  	f.TypeParams = KvPairsMap(p.GetTypeParams())
   318  	f.IndexParams = KvPairsMap(p.GetIndexParams())
   319  	f.IsDynamic = p.GetIsDynamic()
   320  	f.IsPartitionKey = p.GetIsPartitionKey()
   321  	f.ElementType = FieldType(p.GetElementType())
   322  
   323  	return f
   324  }
   325  
   326  // MapKvPairs converts map into common.KeyValuePair slice
   327  func MapKvPairs(m map[string]string) []*common.KeyValuePair {
   328  	pairs := make([]*common.KeyValuePair, 0, len(m))
   329  	for k, v := range m {
   330  		pairs = append(pairs, &common.KeyValuePair{
   331  			Key:   k,
   332  			Value: v,
   333  		})
   334  	}
   335  	return pairs
   336  }
   337  
   338  // KvPairsMap converts common.KeyValuePair slices into map
   339  func KvPairsMap(kvps []*common.KeyValuePair) map[string]string {
   340  	m := make(map[string]string)
   341  	for _, kvp := range kvps {
   342  		m[kvp.Key] = kvp.Value
   343  	}
   344  	return m
   345  }
   346  
   347  // FieldType field data type alias type
   348  // used in go:generate trick, DO NOT modify names & string
   349  type FieldType int32
   350  
   351  // Name returns field type name
   352  func (t FieldType) Name() string {
   353  	switch t {
   354  	case FieldTypeBool:
   355  		return "Bool"
   356  	case FieldTypeInt8:
   357  		return "Int8"
   358  	case FieldTypeInt16:
   359  		return "Int16"
   360  	case FieldTypeInt32:
   361  		return "Int32"
   362  	case FieldTypeInt64:
   363  		return "Int64"
   364  	case FieldTypeFloat:
   365  		return "Float"
   366  	case FieldTypeDouble:
   367  		return "Double"
   368  	case FieldTypeString:
   369  		return "String"
   370  	case FieldTypeVarChar:
   371  		return "VarChar"
   372  	case FieldTypeArray:
   373  		return "Array"
   374  	case FieldTypeJSON:
   375  		return "JSON"
   376  	case FieldTypeBinaryVector:
   377  		return "BinaryVector"
   378  	case FieldTypeFloatVector:
   379  		return "FloatVector"
   380  	case FieldTypeFloat16Vector:
   381  		return "Float16Vector"
   382  	case FieldTypeBFloat16Vector:
   383  		return "BFloat16Vector"
   384  	default:
   385  		return "undefined"
   386  	}
   387  }
   388  
   389  // String returns field type
   390  func (t FieldType) String() string {
   391  	switch t {
   392  	case FieldTypeBool:
   393  		return "bool"
   394  	case FieldTypeInt8:
   395  		return "int8"
   396  	case FieldTypeInt16:
   397  		return "int16"
   398  	case FieldTypeInt32:
   399  		return "int32"
   400  	case FieldTypeInt64:
   401  		return "int64"
   402  	case FieldTypeFloat:
   403  		return "float32"
   404  	case FieldTypeDouble:
   405  		return "float64"
   406  	case FieldTypeString:
   407  		return "string"
   408  	case FieldTypeVarChar:
   409  		return "string"
   410  	case FieldTypeArray:
   411  		return "Array"
   412  	case FieldTypeJSON:
   413  		return "JSON"
   414  	case FieldTypeBinaryVector:
   415  		return "[]byte"
   416  	case FieldTypeFloatVector:
   417  		return "[]float32"
   418  	case FieldTypeFloat16Vector:
   419  		return "[]byte"
   420  	case FieldTypeBFloat16Vector:
   421  		return "[]byte"
   422  	case FieldTypeSparseVector:
   423  		return "[]SparseEmbedding"
   424  	default:
   425  		return "undefined"
   426  	}
   427  }
   428  
   429  // PbFieldType represents FieldType corresponding schema pb type
   430  func (t FieldType) PbFieldType() (string, string) {
   431  	switch t {
   432  	case FieldTypeBool:
   433  		return "Bool", "bool"
   434  	case FieldTypeInt8:
   435  		fallthrough
   436  	case FieldTypeInt16:
   437  		fallthrough
   438  	case FieldTypeInt32:
   439  		return "Int", "int32"
   440  	case FieldTypeInt64:
   441  		return "Long", "int64"
   442  	case FieldTypeFloat:
   443  		return "Float", "float32"
   444  	case FieldTypeDouble:
   445  		return "Double", "float64"
   446  	case FieldTypeString:
   447  		return "String", "string"
   448  	case FieldTypeVarChar:
   449  		return "VarChar", "string"
   450  	case FieldTypeJSON:
   451  		return "JSON", "JSON"
   452  	case FieldTypeBinaryVector:
   453  		return "[]byte", ""
   454  	case FieldTypeFloatVector:
   455  		return "[]float32", ""
   456  	case FieldTypeFloat16Vector:
   457  		return "[]byte", ""
   458  	case FieldTypeBFloat16Vector:
   459  		return "[]byte", ""
   460  	default:
   461  		return "undefined", ""
   462  	}
   463  }
   464  
   465  // Match schema definition
   466  const (
   467  	// FieldTypeNone zero value place holder
   468  	FieldTypeNone FieldType = 0 // zero value place holder
   469  	// FieldTypeBool field type boolean
   470  	FieldTypeBool FieldType = 1
   471  	// FieldTypeInt8 field type int8
   472  	FieldTypeInt8 FieldType = 2
   473  	// FieldTypeInt16 field type int16
   474  	FieldTypeInt16 FieldType = 3
   475  	// FieldTypeInt32 field type int32
   476  	FieldTypeInt32 FieldType = 4
   477  	// FieldTypeInt64 field type int64
   478  	FieldTypeInt64 FieldType = 5
   479  	// FieldTypeFloat field type float
   480  	FieldTypeFloat FieldType = 10
   481  	// FieldTypeDouble field type double
   482  	FieldTypeDouble FieldType = 11
   483  	// FieldTypeString field type string
   484  	FieldTypeString FieldType = 20
   485  	// FieldTypeVarChar field type varchar
   486  	FieldTypeVarChar FieldType = 21 // variable-length strings with a specified maximum length
   487  	// FieldTypeArray field type Array
   488  	FieldTypeArray FieldType = 22
   489  	// FieldTypeJSON field type JSON
   490  	FieldTypeJSON FieldType = 23
   491  	// FieldTypeBinaryVector field type binary vector
   492  	FieldTypeBinaryVector FieldType = 100
   493  	// FieldTypeFloatVector field type float vector
   494  	FieldTypeFloatVector FieldType = 101
   495  	// FieldTypeBinaryVector field type float16 vector
   496  	FieldTypeFloat16Vector FieldType = 102
   497  	// FieldTypeBinaryVector field type bf16 vector
   498  	FieldTypeBFloat16Vector FieldType = 103
   499  	// FieldTypeBinaryVector field type sparse vector
   500  	FieldTypeSparseVector FieldType = 104
   501  )