Skip to content

Commit

Permalink
Added EncodeText method to Vector and SparseVector
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Aug 6, 2024
1 parent 1406c17 commit e5cd30d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pgx/sparsevec.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type encodePlanSparseVectorCodecText struct{}

func (encodePlanSparseVectorCodecText) Encode(value any, buf []byte) (newBuf []byte, err error) {
v := value.(pgvector.SparseVector)
return append(buf, v.String()...), nil
return v.EncodeText(buf)
}

func (SparseVectorCodec) PlanScan(m *pgtype.Map, oid uint32, format int16, target any) pgtype.ScanPlan {
Expand Down
2 changes: 1 addition & 1 deletion pgx/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type encodePlanVectorCodecText struct{}

func (encodePlanVectorCodecText) Encode(value any, buf []byte) (newBuf []byte, err error) {
v := value.(pgvector.Vector)
return append(buf, v.String()...), nil
return v.EncodeText(buf)
}

func (VectorCodec) PlanScan(m *pgtype.Map, oid uint32, format int16, target any) pgtype.ScanPlan {
Expand Down
38 changes: 23 additions & 15 deletions sparsevec.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,9 @@ func (v SparseVector) Slice() []float32 {

// String returns a string representation of the sparse vector.
func (v SparseVector) String() string {
buf := make([]byte, 0, 13+27*len(v.indices))
buf = append(buf, '{')

for i := 0; i < len(v.indices); i++ {
if i > 0 {
buf = append(buf, ',')
}
buf = strconv.AppendInt(buf, int64(v.indices[i])+1, 10)
buf = append(buf, ':')
buf = strconv.AppendFloat(buf, float64(v.values[i]), 'f', -1, 32)
}

buf = append(buf, '}')
buf = append(buf, '/')
buf = strconv.AppendInt(buf, int64(v.dim), 10)
// should never throw an error
// but returning an empty string is fine if it does
buf, _ := v.EncodeText(nil)
return string(buf)
}

Expand Down Expand Up @@ -168,6 +156,26 @@ func (v *SparseVector) DecodeBinary(buf []byte) error {
return nil
}

// EncodeText encodes a text representation of the sparse vector.
func (v SparseVector) EncodeText(buf []byte) (newBuf []byte, err error) {
buf = slices.Grow(buf, 13+27*len(v.indices))
buf = append(buf, '{')

for i := 0; i < len(v.indices); i++ {
if i > 0 {
buf = append(buf, ',')
}
buf = strconv.AppendInt(buf, int64(v.indices[i])+1, 10)
buf = append(buf, ':')
buf = strconv.AppendFloat(buf, float64(v.values[i]), 'f', -1, 32)
}

buf = append(buf, '}')
buf = append(buf, '/')
buf = strconv.AppendInt(buf, int64(v.dim), 10)
return buf, nil
}

// statically assert that SparseVector implements sql.Scanner.
var _ sql.Scanner = (*SparseVector)(nil)

Expand Down
28 changes: 17 additions & 11 deletions vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,9 @@ func (v Vector) Slice() []float32 {

// String returns a string representation of the vector.
func (v Vector) String() string {
buf := make([]byte, 0, 2+16*len(v.vec))
buf = append(buf, '[')

for i := 0; i < len(v.vec); i++ {
if i > 0 {
buf = append(buf, ',')
}
buf = strconv.AppendFloat(buf, float64(v.vec[i]), 'f', -1, 32)
}

buf = append(buf, ']')
// should never throw an error
// but returning an empty string is fine if it does
buf, _ := v.EncodeText(nil)
return string(buf)
}

Expand Down Expand Up @@ -86,6 +78,20 @@ func (v *Vector) DecodeBinary(buf []byte) error {
return nil
}

// EncodeText encodes a text representation of the vector.
func (v Vector) EncodeText(buf []byte) (newBuf []byte, err error) {
buf = slices.Grow(buf, 2+16*len(v.vec))
buf = append(buf, '[')
for i := 0; i < len(v.vec); i++ {
if i > 0 {
buf = append(buf, ',')
}
buf = strconv.AppendFloat(buf, float64(v.vec[i]), 'f', -1, 32)
}
buf = append(buf, ']')
return buf, nil
}

// statically assert that Vector implements sql.Scanner.
var _ sql.Scanner = (*Vector)(nil)

Expand Down

0 comments on commit e5cd30d

Please sign in to comment.