mirror of
https://github.com/milvus-io/milvus.git
synced 2026-07-21 10:15:43 +00:00
feat: impl StructArray -- support csv/json import on more types of vector array (#50753)
issue: https://github.com/milvus-io/milvus/issues/42148 design doc: docs/design-docs/design_docs/20260306-struct.md Signed-off-by: SpadeA <tangchenjie1210@gmail.com>
This commit is contained in:
@@ -0,0 +1,238 @@
|
||||
// Licensed to the LF AI & Data foundation under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/milvus-io/milvus-proto/go-api/v3/schemapb"
|
||||
"github.com/milvus-io/milvus/internal/json"
|
||||
"github.com/milvus-io/milvus/pkg/v3/util/merr"
|
||||
"github.com/milvus-io/milvus/pkg/v3/util/typeutil"
|
||||
)
|
||||
|
||||
// ArrayOfVectorToFieldData converts a user-facing list-of-vectors value into
|
||||
// the per-row VectorField representation used by VectorArrayFieldData.
|
||||
func ArrayOfVectorToFieldData(vectors []any, field *schemapb.FieldSchema, dim int) (*schemapb.VectorField, error) {
|
||||
switch field.GetElementType() {
|
||||
case schemapb.DataType_FloatVector:
|
||||
values, err := parseFloatVectorArray(vectors, field, dim)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := typeutil.VerifyFloats32(values); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &schemapb.VectorField{
|
||||
Dim: int64(dim),
|
||||
Data: &schemapb.VectorField_FloatVector{
|
||||
FloatVector: &schemapb.FloatArray{Data: values},
|
||||
},
|
||||
}, nil
|
||||
case schemapb.DataType_BinaryVector:
|
||||
values, err := parseByteVectorArray(vectors, field, binaryBytesPerVector(dim))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &schemapb.VectorField{
|
||||
Dim: int64(dim),
|
||||
Data: &schemapb.VectorField_BinaryVector{BinaryVector: values},
|
||||
}, nil
|
||||
case schemapb.DataType_Float16Vector:
|
||||
values, err := parseHalfVectorArray(vectors, field, dim, typeutil.Float32ToFloat16Bytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := typeutil.VerifyFloats16(values); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &schemapb.VectorField{
|
||||
Dim: int64(dim),
|
||||
Data: &schemapb.VectorField_Float16Vector{Float16Vector: values},
|
||||
}, nil
|
||||
case schemapb.DataType_BFloat16Vector:
|
||||
values, err := parseHalfVectorArray(vectors, field, dim, typeutil.Float32ToBFloat16Bytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := typeutil.VerifyBFloats16(values); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &schemapb.VectorField{
|
||||
Dim: int64(dim),
|
||||
Data: &schemapb.VectorField_Bfloat16Vector{Bfloat16Vector: values},
|
||||
}, nil
|
||||
case schemapb.DataType_Int8Vector:
|
||||
values, err := parseInt8VectorArray(vectors, field, dim)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &schemapb.VectorField{
|
||||
Dim: int64(dim),
|
||||
Data: &schemapb.VectorField_Int8Vector{Int8Vector: values},
|
||||
}, nil
|
||||
case schemapb.DataType_SparseFloatVector:
|
||||
return nil, merr.WrapErrImportFailed("ArrayOfVector with SparseFloatVector element type is not implemented yet")
|
||||
default:
|
||||
return nil, merr.WrapErrImportFailedMsg("unsupported ArrayOfVector element type: %s", field.GetElementType().String())
|
||||
}
|
||||
}
|
||||
|
||||
func parseFloatVectorArray(vectors []any, field *schemapb.FieldSchema, dim int) ([]float32, error) {
|
||||
values := make([]float32, 0, len(vectors)*dim)
|
||||
for i, rawVector := range vectors {
|
||||
vector, err := asVector(rawVector)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(vector) != dim {
|
||||
return nil, vectorDimErr(field, i, len(vector), dim)
|
||||
}
|
||||
for _, rawValue := range vector {
|
||||
value, err := asFloat32(rawValue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
values = append(values, value)
|
||||
}
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func parseByteVectorArray(vectors []any, field *schemapb.FieldSchema, bytesPerVector int) ([]byte, error) {
|
||||
values := make([]byte, 0, len(vectors)*bytesPerVector)
|
||||
for i, rawVector := range vectors {
|
||||
vector, err := asVector(rawVector)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(vector) != bytesPerVector {
|
||||
return nil, vectorByteSizeErr(field, i, len(vector), bytesPerVector)
|
||||
}
|
||||
for _, rawValue := range vector {
|
||||
value, err := asUint8(rawValue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
values = append(values, value)
|
||||
}
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func parseHalfVectorArray(
|
||||
vectors []any,
|
||||
field *schemapb.FieldSchema,
|
||||
dim int,
|
||||
convert func(float32) []byte,
|
||||
) ([]byte, error) {
|
||||
values := make([]byte, 0, len(vectors)*dim*2)
|
||||
for i, rawVector := range vectors {
|
||||
vector, err := asVector(rawVector)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(vector) != dim {
|
||||
return nil, vectorDimErr(field, i, len(vector), dim)
|
||||
}
|
||||
for _, rawValue := range vector {
|
||||
value, err := asFloat32(rawValue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
values = append(values, convert(value)...)
|
||||
}
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func parseInt8VectorArray(vectors []any, field *schemapb.FieldSchema, dim int) ([]byte, error) {
|
||||
values := make([]byte, 0, len(vectors)*dim)
|
||||
for i, rawVector := range vectors {
|
||||
vector, err := asVector(rawVector)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(vector) != dim {
|
||||
return nil, vectorDimErr(field, i, len(vector), dim)
|
||||
}
|
||||
for _, rawValue := range vector {
|
||||
value, err := asInt8(rawValue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
values = append(values, byte(value))
|
||||
}
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func binaryBytesPerVector(dim int) int {
|
||||
return (dim + 7) / 8
|
||||
}
|
||||
|
||||
func asVector(value any) ([]any, error) {
|
||||
vector, ok := value.([]any)
|
||||
if !ok {
|
||||
return nil, merr.WrapErrImportFailedMsg("expected slice as vector, but got %T", value)
|
||||
}
|
||||
return vector, nil
|
||||
}
|
||||
|
||||
func asFloat32(value any) (float32, error) {
|
||||
v, ok := value.(json.Number)
|
||||
if !ok {
|
||||
return 0, numericTypeErr(value)
|
||||
}
|
||||
num, err := strconv.ParseFloat(v.String(), 32)
|
||||
return float32(num), err
|
||||
}
|
||||
|
||||
func asUint8(value any) (byte, error) {
|
||||
v, ok := value.(json.Number)
|
||||
if !ok {
|
||||
return 0, numericTypeErr(value)
|
||||
}
|
||||
num, err := strconv.ParseUint(v.String(), 0, 8)
|
||||
return byte(num), err
|
||||
}
|
||||
|
||||
func asInt8(value any) (int8, error) {
|
||||
v, ok := value.(json.Number)
|
||||
if !ok {
|
||||
return 0, numericTypeErr(value)
|
||||
}
|
||||
num, err := strconv.ParseInt(v.String(), 10, 8)
|
||||
return int8(num), err
|
||||
}
|
||||
|
||||
func vectorDimErr(field *schemapb.FieldSchema, position int, actual int, expected int) error {
|
||||
return merr.WrapErrImportFailed(
|
||||
fmt.Sprintf("vector dimension mismatch for field '%s': position=%d, actual=%d, expected=%d",
|
||||
field.GetName(), position, actual, expected))
|
||||
}
|
||||
|
||||
func vectorByteSizeErr(field *schemapb.FieldSchema, position int, actual int, expected int) error {
|
||||
return merr.WrapErrImportFailed(
|
||||
fmt.Sprintf("vector byte size mismatch for field '%s': position=%d, actual=%d, expected=%d",
|
||||
field.GetName(), position, actual, expected))
|
||||
}
|
||||
|
||||
func numericTypeErr(value any) error {
|
||||
return merr.WrapErrImportFailedMsg("expected numeric vector element, got type %T with value %v", value, value)
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
// Licensed to the LF AI & Data foundation under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/milvus-io/milvus-proto/go-api/v3/commonpb"
|
||||
"github.com/milvus-io/milvus-proto/go-api/v3/schemapb"
|
||||
"github.com/milvus-io/milvus/internal/json"
|
||||
pkgcommon "github.com/milvus-io/milvus/pkg/v3/common"
|
||||
"github.com/milvus-io/milvus/pkg/v3/util/typeutil"
|
||||
)
|
||||
|
||||
func vectorArrayField(elementType schemapb.DataType, dim int) *schemapb.FieldSchema {
|
||||
return &schemapb.FieldSchema{
|
||||
FieldID: 100,
|
||||
Name: "items[vec]",
|
||||
DataType: schemapb.DataType_ArrayOfVector,
|
||||
ElementType: elementType,
|
||||
TypeParams: []*commonpb.KeyValuePair{
|
||||
{Key: pkgcommon.DimKey, Value: strconv.Itoa(dim)},
|
||||
{Key: pkgcommon.MaxCapacityKey, Value: "8"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func numbers(values ...string) []any {
|
||||
out := make([]any, 0, len(values))
|
||||
for _, value := range values {
|
||||
out = append(out, json.Number(value))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestArrayOfVectorToFieldData_FixedDimElementTypes(t *testing.T) {
|
||||
float16Expected := append(typeutil.Float32ToFloat16Bytes(0.25), typeutil.Float32ToFloat16Bytes(0.5)...)
|
||||
bfloat16Expected := append(typeutil.Float32ToBFloat16Bytes(0.25), typeutil.Float32ToBFloat16Bytes(0.5)...)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
elementType schemapb.DataType
|
||||
dim int
|
||||
vectors []any
|
||||
verify func(*testing.T, *schemapb.VectorField)
|
||||
}{
|
||||
{
|
||||
name: "float",
|
||||
elementType: schemapb.DataType_FloatVector,
|
||||
dim: 2,
|
||||
vectors: []any{
|
||||
numbers("0.25", "0.5"),
|
||||
numbers("0.75", "1.0"),
|
||||
},
|
||||
verify: func(t *testing.T, field *schemapb.VectorField) {
|
||||
require.Equal(t, []float32{0.25, 0.5, 0.75, 1.0}, field.GetFloatVector().GetData())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "binary",
|
||||
elementType: schemapb.DataType_BinaryVector,
|
||||
dim: 16,
|
||||
vectors: []any{
|
||||
numbers("3", "11"),
|
||||
numbers("5", "7"),
|
||||
},
|
||||
verify: func(t *testing.T, field *schemapb.VectorField) {
|
||||
require.Equal(t, []byte{3, 11, 5, 7}, field.GetBinaryVector())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "float16",
|
||||
elementType: schemapb.DataType_Float16Vector,
|
||||
dim: 2,
|
||||
vectors: []any{
|
||||
numbers("0.25", "0.5"),
|
||||
},
|
||||
verify: func(t *testing.T, field *schemapb.VectorField) {
|
||||
require.Equal(t, float16Expected, field.GetFloat16Vector())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bfloat16",
|
||||
elementType: schemapb.DataType_BFloat16Vector,
|
||||
dim: 2,
|
||||
vectors: []any{
|
||||
numbers("0.25", "0.5"),
|
||||
},
|
||||
verify: func(t *testing.T, field *schemapb.VectorField) {
|
||||
require.Equal(t, bfloat16Expected, field.GetBfloat16Vector())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "int8",
|
||||
elementType: schemapb.DataType_Int8Vector,
|
||||
dim: 2,
|
||||
vectors: []any{
|
||||
numbers("1", "-2"),
|
||||
numbers("3", "-4"),
|
||||
},
|
||||
verify: func(t *testing.T, field *schemapb.VectorField) {
|
||||
require.Equal(t, typeutil.Int8ArrayToBytes([]int8{1, -2, 3, -4}), field.GetInt8Vector())
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
field, err := ArrayOfVectorToFieldData(tt.vectors, vectorArrayField(tt.elementType, tt.dim), tt.dim)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(tt.dim), field.GetDim())
|
||||
tt.verify(t, field)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestArrayOfVectorToFieldData_RejectsUnsupportedOrInvalidInput(t *testing.T) {
|
||||
_, err := ArrayOfVectorToFieldData(
|
||||
[]any{numbers("1", "2")},
|
||||
vectorArrayField(schemapb.DataType_SparseFloatVector, 2),
|
||||
2,
|
||||
)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "SparseFloatVector")
|
||||
|
||||
_, err = ArrayOfVectorToFieldData(
|
||||
[]any{numbers("1", "2", "3")},
|
||||
vectorArrayField(schemapb.DataType_Int8Vector, 2),
|
||||
2,
|
||||
)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "dimension")
|
||||
|
||||
_, err = ArrayOfVectorToFieldData(
|
||||
[]any{numbers("1", "2", "3", "4")},
|
||||
vectorArrayField(schemapb.DataType_Float16Vector, 2),
|
||||
2,
|
||||
)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "dimension")
|
||||
|
||||
_, err = ArrayOfVectorToFieldData(
|
||||
[]any{[]any{float64(1), float64(2)}},
|
||||
vectorArrayField(schemapb.DataType_FloatVector, 2),
|
||||
2,
|
||||
)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "expected numeric vector element")
|
||||
|
||||
_, err = ArrayOfVectorToFieldData(
|
||||
[]any{numbers("1", "256")},
|
||||
vectorArrayField(schemapb.DataType_BinaryVector, 16),
|
||||
16,
|
||||
)
|
||||
require.Error(t, err)
|
||||
}
|
||||
@@ -755,55 +755,11 @@ func (r *rowParser) arrayToFieldData(arr []interface{}, field *schemapb.FieldSch
|
||||
}
|
||||
|
||||
func (r *rowParser) arrayOfVectorToFieldData(vectors []any, field *schemapb.FieldSchema) (*schemapb.VectorField, error) {
|
||||
elementType := field.GetElementType()
|
||||
switch elementType {
|
||||
case schemapb.DataType_FloatVector:
|
||||
dim, err := typeutil.GetDim(field)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
values := make([]float32, 0, len(vectors)*int(dim))
|
||||
|
||||
for _, vectorAny := range vectors {
|
||||
var vector []float32
|
||||
v, ok := vectorAny.([]interface{})
|
||||
if !ok {
|
||||
return nil, r.wrapTypeError(vectorAny, field)
|
||||
}
|
||||
vector = make([]float32, len(v))
|
||||
for i, elem := range v {
|
||||
value, ok := elem.(json.Number)
|
||||
if !ok {
|
||||
return nil, r.wrapArrayValueTypeError(elem, elementType)
|
||||
}
|
||||
num, err := strconv.ParseFloat(value.String(), 32)
|
||||
if err != nil {
|
||||
return nil, merr.Wrap(err, "failed to parse float")
|
||||
}
|
||||
vector[i] = float32(num)
|
||||
}
|
||||
|
||||
if len(vector) != int(dim) {
|
||||
return nil, r.wrapDimError(len(vector), field)
|
||||
}
|
||||
values = append(values, vector...)
|
||||
}
|
||||
|
||||
return &schemapb.VectorField{
|
||||
Dim: dim,
|
||||
Data: &schemapb.VectorField_FloatVector{
|
||||
FloatVector: &schemapb.FloatArray{
|
||||
Data: values,
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
|
||||
case schemapb.DataType_Float16Vector, schemapb.DataType_BFloat16Vector, schemapb.DataType_BinaryVector,
|
||||
schemapb.DataType_Int8Vector, schemapb.DataType_SparseFloatVector:
|
||||
return nil, merr.WrapErrImportFailedMsg("not implemented element type for CSV: %s", elementType.String())
|
||||
default:
|
||||
return nil, merr.WrapErrImportFailedMsg("unsupported element type: %s", elementType.String())
|
||||
dim, err := typeutil.GetDim(field)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return common.ArrayOfVectorToFieldData(vectors, field, int(dim))
|
||||
}
|
||||
|
||||
func (r *rowParser) wrapTypeError(v any, field *schemapb.FieldSchema) error {
|
||||
|
||||
@@ -953,6 +953,175 @@ func TestCsvRowParser(t *testing.T) {
|
||||
suite.Run(t, new(RowParserSuite))
|
||||
}
|
||||
|
||||
func TestArrayOfVectorToFieldData_NonFloatElementTypes(t *testing.T) {
|
||||
makeField := func(elementType schemapb.DataType, dim string) *schemapb.FieldSchema {
|
||||
return &schemapb.FieldSchema{
|
||||
FieldID: 100,
|
||||
Name: "test_vec",
|
||||
DataType: schemapb.DataType_ArrayOfVector,
|
||||
ElementType: elementType,
|
||||
TypeParams: []*commonpb.KeyValuePair{
|
||||
{Key: common.DimKey, Value: dim},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
field *schemapb.FieldSchema
|
||||
dim int
|
||||
vectors []any
|
||||
verify func(*schemapb.VectorField)
|
||||
}{
|
||||
{
|
||||
name: "binary",
|
||||
field: makeField(schemapb.DataType_BinaryVector, "16"),
|
||||
dim: 16,
|
||||
vectors: []any{[]any{json.Number("3"), json.Number("11")}},
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
assert.Equal(t, []byte{3, 11}, field.GetBinaryVector())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "float16",
|
||||
field: makeField(schemapb.DataType_Float16Vector, "2"),
|
||||
dim: 2,
|
||||
vectors: []any{[]any{json.Number("0.25"), json.Number("0.5")}},
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
expected := append(typeutil.Float32ToFloat16Bytes(0.25), typeutil.Float32ToFloat16Bytes(0.5)...)
|
||||
assert.Equal(t, expected, field.GetFloat16Vector())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bfloat16",
|
||||
field: makeField(schemapb.DataType_BFloat16Vector, "2"),
|
||||
dim: 2,
|
||||
vectors: []any{[]any{json.Number("0.25"), json.Number("0.5")}},
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
expected := append(typeutil.Float32ToBFloat16Bytes(0.25), typeutil.Float32ToBFloat16Bytes(0.5)...)
|
||||
assert.Equal(t, expected, field.GetBfloat16Vector())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "int8",
|
||||
field: makeField(schemapb.DataType_Int8Vector, "2"),
|
||||
dim: 2,
|
||||
vectors: []any{[]any{json.Number("1"), json.Number("-2")}},
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
assert.Equal(t, typeutil.Int8ArrayToBytes([]int8{1, -2}), field.GetInt8Vector())
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
parser := &rowParser{name2Dim: map[string]int{tt.field.GetName(): tt.dim}}
|
||||
result, err := parser.arrayOfVectorToFieldData(tt.vectors, tt.field)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, int64(tt.dim), result.GetDim())
|
||||
tt.verify(result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseStructArrayArrayOfVector_NonFloatElementTypes(t *testing.T) {
|
||||
makeSchema := func(elementType schemapb.DataType, dim string) *schemapb.CollectionSchema {
|
||||
return &schemapb.CollectionSchema{
|
||||
Fields: []*schemapb.FieldSchema{
|
||||
{
|
||||
FieldID: 1,
|
||||
Name: "id",
|
||||
IsPrimaryKey: true,
|
||||
DataType: schemapb.DataType_Int64,
|
||||
},
|
||||
},
|
||||
StructArrayFields: []*schemapb.StructArrayFieldSchema{
|
||||
{
|
||||
FieldID: 200,
|
||||
Name: "struct_array",
|
||||
Fields: []*schemapb.FieldSchema{
|
||||
{
|
||||
FieldID: 201,
|
||||
Name: "struct_array[vec]",
|
||||
DataType: schemapb.DataType_ArrayOfVector,
|
||||
ElementType: elementType,
|
||||
TypeParams: []*commonpb.KeyValuePair{
|
||||
{Key: common.DimKey, Value: dim},
|
||||
{Key: common.MaxCapacityKey, Value: "4"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
elementType schemapb.DataType
|
||||
dim string
|
||||
raw string
|
||||
verify func(*schemapb.VectorField)
|
||||
}{
|
||||
{
|
||||
name: "binary",
|
||||
elementType: schemapb.DataType_BinaryVector,
|
||||
dim: "16",
|
||||
raw: `[{"vec":[3,11]},{"vec":[5,7]}]`,
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
assert.Equal(t, []byte{3, 11, 5, 7}, field.GetBinaryVector())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "float16",
|
||||
elementType: schemapb.DataType_Float16Vector,
|
||||
dim: "2",
|
||||
raw: `[{"vec":[0.25,0.5]},{"vec":[0.75,1.0]}]`,
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
expected := append(typeutil.Float32ToFloat16Bytes(0.25), typeutil.Float32ToFloat16Bytes(0.5)...)
|
||||
expected = append(expected, typeutil.Float32ToFloat16Bytes(0.75)...)
|
||||
expected = append(expected, typeutil.Float32ToFloat16Bytes(1.0)...)
|
||||
assert.Equal(t, expected, field.GetFloat16Vector())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bfloat16",
|
||||
elementType: schemapb.DataType_BFloat16Vector,
|
||||
dim: "2",
|
||||
raw: `[{"vec":[0.25,0.5]},{"vec":[0.75,1.0]}]`,
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
expected := append(typeutil.Float32ToBFloat16Bytes(0.25), typeutil.Float32ToBFloat16Bytes(0.5)...)
|
||||
expected = append(expected, typeutil.Float32ToBFloat16Bytes(0.75)...)
|
||||
expected = append(expected, typeutil.Float32ToBFloat16Bytes(1.0)...)
|
||||
assert.Equal(t, expected, field.GetBfloat16Vector())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "int8",
|
||||
elementType: schemapb.DataType_Int8Vector,
|
||||
dim: "2",
|
||||
raw: `[{"vec":[1,-2]},{"vec":[3,-4]}]`,
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
assert.Equal(t, typeutil.Int8ArrayToBytes([]int8{1, -2, 3, -4}), field.GetInt8Vector())
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
parser, err := NewRowParser(makeSchema(tt.elementType, tt.dim), []string{"id", "struct_array"}, "")
|
||||
assert.NoError(t, err)
|
||||
|
||||
row, err := parser.Parse([]string{"1", tt.raw})
|
||||
assert.NoError(t, err)
|
||||
vectorField, ok := row[201].(*schemapb.VectorField)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, tt.dim, strconv.FormatInt(vectorField.GetDim(), 10))
|
||||
tt.verify(vectorField)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTextFieldValue(t *testing.T) {
|
||||
// TEXT fields should be parsed as strings without maxLength validation.
|
||||
schema := &schemapb.CollectionSchema{
|
||||
|
||||
@@ -834,46 +834,6 @@ func (r *rowParser) arrayToFieldData(arr []interface{}, field *schemapb.FieldSch
|
||||
}
|
||||
|
||||
func (r *rowParser) arrayOfVectorToFieldData(vectors []any, field *schemapb.FieldSchema) (*schemapb.VectorField, error) {
|
||||
elementType := field.GetElementType()
|
||||
fieldID := field.GetFieldID()
|
||||
switch elementType {
|
||||
case schemapb.DataType_FloatVector:
|
||||
values := make([]float32, 0, len(vectors)*10)
|
||||
dim := r.id2Dim[fieldID]
|
||||
for i, vectorAny := range vectors {
|
||||
vector, ok := vectorAny.([]any)
|
||||
if !ok {
|
||||
return nil, merr.WrapErrImportFailedMsg("expected slice as vector, but got %T", vectorAny)
|
||||
}
|
||||
if len(vector) != dim {
|
||||
return nil, merr.WrapErrImportFailed(
|
||||
fmt.Sprintf("vector at index %d has dimension %d, expected %d", i, len(vector), dim))
|
||||
}
|
||||
for _, v := range vector {
|
||||
value, ok := v.(json.Number)
|
||||
if !ok {
|
||||
return nil, r.wrapTypeError(value, fieldID)
|
||||
}
|
||||
num, err := strconv.ParseFloat(value.String(), 32)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
values = append(values, float32(num))
|
||||
}
|
||||
}
|
||||
return &schemapb.VectorField{
|
||||
Dim: int64(dim),
|
||||
Data: &schemapb.VectorField_FloatVector{
|
||||
FloatVector: &schemapb.FloatArray{
|
||||
Data: values,
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
|
||||
case schemapb.DataType_Float16Vector, schemapb.DataType_BFloat16Vector, schemapb.DataType_BinaryVector,
|
||||
schemapb.DataType_Int8Vector, schemapb.DataType_SparseFloatVector:
|
||||
return nil, merr.WrapErrImportFailedMsg("not implemented element type: %s", elementType.String())
|
||||
default:
|
||||
return nil, merr.WrapErrImportFailedMsg("unsupported element type: %s", elementType.String())
|
||||
}
|
||||
return common.ArrayOfVectorToFieldData(vectors, field, r.id2Dim[fieldID])
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -867,6 +868,201 @@ func TestArrayOfVectorToFieldData_DimensionMismatch(t *testing.T) {
|
||||
assert.Equal(t, int64(dim), result.Dim)
|
||||
}
|
||||
|
||||
func TestArrayOfVectorToFieldData_NonFloatElementTypes(t *testing.T) {
|
||||
fieldID := int64(100)
|
||||
makeField := func(elementType schemapb.DataType, dim string) *schemapb.FieldSchema {
|
||||
return &schemapb.FieldSchema{
|
||||
FieldID: fieldID,
|
||||
Name: "test_vec",
|
||||
DataType: schemapb.DataType_ArrayOfVector,
|
||||
ElementType: elementType,
|
||||
TypeParams: []*commonpb.KeyValuePair{
|
||||
{Key: common.DimKey, Value: dim},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
field *schemapb.FieldSchema
|
||||
dim int
|
||||
vectors []any
|
||||
verify func(*schemapb.VectorField)
|
||||
}{
|
||||
{
|
||||
name: "binary",
|
||||
field: makeField(schemapb.DataType_BinaryVector, "16"),
|
||||
dim: 16,
|
||||
vectors: []any{[]any{json.Number("3"), json.Number("11")}},
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
assert.Equal(t, []byte{3, 11}, field.GetBinaryVector())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "float16",
|
||||
field: makeField(schemapb.DataType_Float16Vector, "2"),
|
||||
dim: 2,
|
||||
vectors: []any{[]any{json.Number("0.25"), json.Number("0.5")}},
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
expected := append(typeutil.Float32ToFloat16Bytes(0.25), typeutil.Float32ToFloat16Bytes(0.5)...)
|
||||
assert.Equal(t, expected, field.GetFloat16Vector())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bfloat16",
|
||||
field: makeField(schemapb.DataType_BFloat16Vector, "2"),
|
||||
dim: 2,
|
||||
vectors: []any{[]any{json.Number("0.25"), json.Number("0.5")}},
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
expected := append(typeutil.Float32ToBFloat16Bytes(0.25), typeutil.Float32ToBFloat16Bytes(0.5)...)
|
||||
assert.Equal(t, expected, field.GetBfloat16Vector())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "int8",
|
||||
field: makeField(schemapb.DataType_Int8Vector, "2"),
|
||||
dim: 2,
|
||||
vectors: []any{[]any{json.Number("1"), json.Number("-2")}},
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
assert.Equal(t, typeutil.Int8ArrayToBytes([]int8{1, -2}), field.GetInt8Vector())
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
parser := &rowParser{
|
||||
id2Dim: map[int64]int{fieldID: tt.dim},
|
||||
id2Field: map[int64]*schemapb.FieldSchema{fieldID: tt.field},
|
||||
}
|
||||
result, err := parser.arrayOfVectorToFieldData(tt.vectors, tt.field)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, int64(tt.dim), result.GetDim())
|
||||
tt.verify(result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseStructArrayArrayOfVector_NonFloatElementTypes(t *testing.T) {
|
||||
makeSchema := func(elementType schemapb.DataType, dim string) *schemapb.CollectionSchema {
|
||||
return &schemapb.CollectionSchema{
|
||||
Fields: []*schemapb.FieldSchema{
|
||||
{
|
||||
FieldID: 1,
|
||||
Name: "id",
|
||||
IsPrimaryKey: true,
|
||||
DataType: schemapb.DataType_Int64,
|
||||
},
|
||||
},
|
||||
StructArrayFields: []*schemapb.StructArrayFieldSchema{
|
||||
{
|
||||
FieldID: 200,
|
||||
Name: "struct_array",
|
||||
Fields: []*schemapb.FieldSchema{
|
||||
{
|
||||
FieldID: 201,
|
||||
Name: "struct_array[vec]",
|
||||
DataType: schemapb.DataType_ArrayOfVector,
|
||||
ElementType: elementType,
|
||||
TypeParams: []*commonpb.KeyValuePair{
|
||||
{Key: common.DimKey, Value: dim},
|
||||
{Key: common.MaxCapacityKey, Value: "4"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
makeStructRows := func(vectors ...[]any) []any {
|
||||
rows := make([]any, 0, len(vectors))
|
||||
for _, vector := range vectors {
|
||||
rows = append(rows, map[string]any{"vec": vector})
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
elementType schemapb.DataType
|
||||
dim string
|
||||
rows []any
|
||||
verify func(*schemapb.VectorField)
|
||||
}{
|
||||
{
|
||||
name: "binary",
|
||||
elementType: schemapb.DataType_BinaryVector,
|
||||
dim: "16",
|
||||
rows: makeStructRows(
|
||||
[]any{json.Number("3"), json.Number("11")},
|
||||
[]any{json.Number("5"), json.Number("7")},
|
||||
),
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
assert.Equal(t, []byte{3, 11, 5, 7}, field.GetBinaryVector())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "float16",
|
||||
elementType: schemapb.DataType_Float16Vector,
|
||||
dim: "2",
|
||||
rows: makeStructRows(
|
||||
[]any{json.Number("0.25"), json.Number("0.5")},
|
||||
[]any{json.Number("0.75"), json.Number("1.0")},
|
||||
),
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
expected := append(typeutil.Float32ToFloat16Bytes(0.25), typeutil.Float32ToFloat16Bytes(0.5)...)
|
||||
expected = append(expected, typeutil.Float32ToFloat16Bytes(0.75)...)
|
||||
expected = append(expected, typeutil.Float32ToFloat16Bytes(1.0)...)
|
||||
assert.Equal(t, expected, field.GetFloat16Vector())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bfloat16",
|
||||
elementType: schemapb.DataType_BFloat16Vector,
|
||||
dim: "2",
|
||||
rows: makeStructRows(
|
||||
[]any{json.Number("0.25"), json.Number("0.5")},
|
||||
[]any{json.Number("0.75"), json.Number("1.0")},
|
||||
),
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
expected := append(typeutil.Float32ToBFloat16Bytes(0.25), typeutil.Float32ToBFloat16Bytes(0.5)...)
|
||||
expected = append(expected, typeutil.Float32ToBFloat16Bytes(0.75)...)
|
||||
expected = append(expected, typeutil.Float32ToBFloat16Bytes(1.0)...)
|
||||
assert.Equal(t, expected, field.GetBfloat16Vector())
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "int8",
|
||||
elementType: schemapb.DataType_Int8Vector,
|
||||
dim: "2",
|
||||
rows: makeStructRows(
|
||||
[]any{json.Number("1"), json.Number("-2")},
|
||||
[]any{json.Number("3"), json.Number("-4")},
|
||||
),
|
||||
verify: func(field *schemapb.VectorField) {
|
||||
assert.Equal(t, typeutil.Int8ArrayToBytes([]int8{1, -2, 3, -4}), field.GetInt8Vector())
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
parser, err := NewRowParser(makeSchema(tt.elementType, tt.dim))
|
||||
assert.NoError(t, err)
|
||||
|
||||
row, err := parser.Parse(map[string]any{
|
||||
"id": json.Number("1"),
|
||||
"struct_array": tt.rows,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
vectorField, ok := row[201].(*schemapb.VectorField)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, tt.dim, strconv.FormatInt(vectorField.GetDim(), 10))
|
||||
tt.verify(vectorField)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJsonRowParser(t *testing.T) {
|
||||
suite.Run(t, new(RowParserSuite))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user