Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: Support GetAsVector from Column and SparseEmbedding support return as map #793

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions entity/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ type Column interface {
GetAsString(int) (string, error)
GetAsDouble(int) (float64, error)
GetAsBool(int) (bool, error)
GetAsBFloat16Vector(int) (BFloat16Vector, error)
GetAsFloatVector(int) (FloatVector, error)
GetAsFloat16Vector(int) (Float16Vector, error)
GetAsBinaryVector(int) (BinaryVector, error)
GetAsSparseFloatVector(int) (SparseEmbedding, error)
}

// ColumnBase adds conversion methods support for fixed-type columns.
Expand All @@ -57,6 +62,26 @@ func (b ColumnBase) GetAsBool(_ int) (bool, error) {
return false, errors.New("conversion between fixed-type column not support")
}

func (b ColumnBase) GetAsBFloat16Vector(int) (BFloat16Vector, error) {
return nil, errors.New("conversion between fixed-type column not support")
}

func (b ColumnBase) GetAsFloatVector(int) (FloatVector, error) {
return nil, errors.New("conversion between fixed-type column not support")
}

func (b ColumnBase) GetAsFloat16Vector(int) (Float16Vector, error) {
return nil, errors.New("conversion between fixed-type column not support")
}

func (b ColumnBase) GetAsBinaryVector(int) (BinaryVector, error) {
return nil, errors.New("conversion between fixed-type column not support")
}

func (b ColumnBase) GetAsSparseFloatVector(int) (SparseEmbedding, error) {
return nil, errors.New("conversion between fixed-type column not support")
}

// Vector interface vector used int search
type Vector interface {
Dim() int
Expand Down
14 changes: 14 additions & 0 deletions entity/columns_sparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type SparseEmbedding interface {
Dim() int // the dimension
Len() int // the actual items in this vector
Get(idx int) (pos uint32, value float32, ok bool)
Map() map[uint32]float32
Serialize() []byte
FieldType() FieldType
}
Expand Down Expand Up @@ -68,6 +69,15 @@ func (e sliceSparseEmbedding) Serialize() []byte {
return row
}

// Return sparse embedding as map
func (e sliceSparseEmbedding) Map() map[uint32]float32 {
result := make(map[uint32]float32, 0)
for i := 0; i < e.len; i++ {
result[e.positions[i]] = e.values[i]
}
return result
}

// Less implements sort.Interce
func (e sliceSparseEmbedding) Less(i, j int) bool {
return e.positions[i] < e.positions[j]
Expand Down Expand Up @@ -166,6 +176,10 @@ func (c *ColumnSparseFloatVector) Get(idx int) (interface{}, error) {
return c.vectors[idx], nil
}

func (c *ColumnSparseFloatVector) GetAsSparseFloatVector(idx int) (SparseEmbedding, error) {
return c.ValueByIdx(idx)
}

// ValueByIdx returns value of the provided index
// error occurs when index out of range
func (c *ColumnSparseFloatVector) ValueByIdx(idx int) (SparseEmbedding, error) {
Expand Down
33 changes: 33 additions & 0 deletions entity/columns_vector_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading