util_schema.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Licensed to the LF AI & Data foundation under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package integration
  17. import (
  18. "fmt"
  19. "github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
  20. "github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
  21. "github.com/milvus-io/milvus/pkg/common"
  22. )
  23. const (
  24. BoolField = "boolField"
  25. Int8Field = "int8Field"
  26. Int16Field = "int16Field"
  27. Int32Field = "int32Field"
  28. Int64Field = "int64Field"
  29. FloatField = "floatField"
  30. DoubleField = "doubleField"
  31. VarCharField = "varCharField"
  32. JSONField = "jsonField"
  33. FloatVecField = "floatVecField"
  34. BinVecField = "binVecField"
  35. Float16VecField = "float16VecField"
  36. BFloat16VecField = "bfloat16VecField"
  37. SparseFloatVecField = "sparseFloatVecField"
  38. )
  39. func ConstructSchema(collection string, dim int, autoID bool, fields ...*schemapb.FieldSchema) *schemapb.CollectionSchema {
  40. // if fields are specified, construct it
  41. if len(fields) > 0 {
  42. return &schemapb.CollectionSchema{
  43. Name: collection,
  44. AutoID: autoID,
  45. Fields: fields,
  46. }
  47. }
  48. // if no field is specified, use default
  49. pk := &schemapb.FieldSchema{
  50. FieldID: 100,
  51. Name: Int64Field,
  52. IsPrimaryKey: true,
  53. Description: "",
  54. DataType: schemapb.DataType_Int64,
  55. TypeParams: nil,
  56. IndexParams: nil,
  57. AutoID: autoID,
  58. }
  59. fVec := &schemapb.FieldSchema{
  60. FieldID: 101,
  61. Name: FloatVecField,
  62. IsPrimaryKey: false,
  63. Description: "",
  64. DataType: schemapb.DataType_FloatVector,
  65. TypeParams: []*commonpb.KeyValuePair{
  66. {
  67. Key: common.DimKey,
  68. Value: fmt.Sprintf("%d", dim),
  69. },
  70. },
  71. IndexParams: nil,
  72. }
  73. return &schemapb.CollectionSchema{
  74. Name: collection,
  75. AutoID: autoID,
  76. Fields: []*schemapb.FieldSchema{pk, fVec},
  77. }
  78. }
  79. func ConstructSchemaOfVecDataType(collection string, dim int, autoID bool, dataType schemapb.DataType) *schemapb.CollectionSchema {
  80. pk := &schemapb.FieldSchema{
  81. FieldID: 100,
  82. Name: Int64Field,
  83. IsPrimaryKey: true,
  84. Description: "",
  85. DataType: schemapb.DataType_Int64,
  86. TypeParams: nil,
  87. IndexParams: nil,
  88. AutoID: autoID,
  89. }
  90. var name string
  91. var typeParams []*commonpb.KeyValuePair
  92. switch dataType {
  93. case schemapb.DataType_FloatVector:
  94. name = FloatVecField
  95. typeParams = []*commonpb.KeyValuePair{
  96. {
  97. Key: common.DimKey,
  98. Value: fmt.Sprintf("%d", dim),
  99. },
  100. }
  101. case schemapb.DataType_SparseFloatVector:
  102. name = SparseFloatVecField
  103. typeParams = nil
  104. default:
  105. panic("unsupported data type")
  106. }
  107. fVec := &schemapb.FieldSchema{
  108. FieldID: 101,
  109. Name: name,
  110. IsPrimaryKey: false,
  111. Description: "",
  112. DataType: dataType,
  113. TypeParams: typeParams,
  114. IndexParams: nil,
  115. }
  116. return &schemapb.CollectionSchema{
  117. Name: collection,
  118. AutoID: autoID,
  119. Fields: []*schemapb.FieldSchema{pk, fVec},
  120. }
  121. }