Skip to content

Commit

Permalink
Ignore AutoID pk when converting rows to columns (#470) (#472)
Browse files Browse the repository at this point in the history
Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
  • Loading branch information
congqixia authored May 26, 2023
1 parent 02d910f commit cc1c52d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
11 changes: 11 additions & 0 deletions entity/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ func RowsToColumns(rows []Row, schemas ...*Schema) ([]Column, error) {

nameColumns := make(map[string]Column)
for _, field := range sch.Fields {
// skip auto id pk field
if field.PrimaryKey && field.AutoID {
continue
}
switch field.DataType {
case FieldTypeBool:
data := make([]bool, 0, rowsLen)
Expand Down Expand Up @@ -342,9 +346,16 @@ func RowsToColumns(rows []Row, schemas ...*Schema) ([]Column, error) {
}

for idx, field := range sch.Fields {
// skip dynamic field if visible
if isDynamic && field.IsDynamic {
continue
}
// skip auto id pk field
if field.PrimaryKey && field.AutoID {
// remove pk field from candidates set, avoid adding it into dynamic column
delete(set, field.Name)
continue
}
column := nameColumns[field.Name]

candi, ok := set[field.Name]
Expand Down
39 changes: 25 additions & 14 deletions entity/rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,27 +212,42 @@ type ValidStructWithNamedTag struct {
Vector [16]float32 `milvus:"name:vector"`
}

func TestRowsToColumns(t *testing.T) {
t.Run("valid cases", func(t *testing.T) {
type RowsSuite struct {
suite.Suite
}

func (s *RowsSuite) TestRowsToColumns() {
s.Run("valid_cases", func() {

columns, err := RowsToColumns([]Row{&ValidStruct{}})
assert.Nil(t, err)
assert.Equal(t, 10, len(columns))
s.Nil(err)
s.Equal(10, len(columns))

columns, err = RowsToColumns([]Row{&ValidStruct2{}})
assert.Nil(t, err)
assert.Equal(t, 3, len(columns))
s.Nil(err)
s.Equal(3, len(columns))
})

s.Run("auto_id_pk", func() {
type AutoPK struct {
RowBase
ID int64 `milvus:"primary_key;auto_id"`
Vector []float32 `milvus:"dim:32"`
}
columns, err := RowsToColumns([]Row{&AutoPK{}})
s.Nil(err)
s.Require().Equal(1, len(columns))
s.Equal("Vector", columns[0].Name())
})

t.Run("invalid cases", func(t *testing.T) {
s.Run("invalid_cases", func() {
// empty input
_, err := RowsToColumns([]Row{})
assert.NotNil(t, err)
s.NotNil(err)

// incompatible rows
_, err = RowsToColumns([]Row{&ValidStruct{}, &ValidStruct2{}})
assert.NotNil(t, err)
s.NotNil(err)

// schema & row not compatible
_, err = RowsToColumns([]Row{&ValidStruct{}}, &Schema{
Expand All @@ -243,14 +258,10 @@ func TestRowsToColumns(t *testing.T) {
},
},
})
assert.NotNil(t, err)
s.NotNil(err)
})
}

type RowsSuite struct {
suite.Suite
}

func (s *RowsSuite) TestDynamicSchema() {
s.Run("all_fallback_dynamic", func() {
columns, err := RowsToColumns([]Row{&ValidStruct{}},
Expand Down

0 comments on commit cc1c52d

Please sign in to comment.