You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by "rcosnita (via GitHub)" <gi...@apache.org> on 2024/09/03 20:53:42 UTC
Re: [I] Feature Request: Support Vector Type [cassandra-gocql-driver]
rcosnita commented on issue #1734:
URL: https://github.com/apache/cassandra-gocql-driver/issues/1734#issuecomment-2327419047
Hello everyone,
For people who are using version 4 of the protocol but need to be able to write vectors from their code base here is a possible idea:
```go
package warm
import (
"fmt"
"github.com/gocql/gocql"
)
func encInt(v int32) []byte {
return []byte{byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)}
}
func encFloat(v float32) []byte {
return encInt(int32(math.Float32bits(v)))
}
type Float32Vector struct {
value []float32
dimensions int
}
func (m *Float32Vector) MarshalCQL(info gocql.TypeInfo) ([]byte, error) {
if len(m.value) != m.dimensions {
return nil, fmt.Errorf("float32vector expects size %d but received size %d",
m.dimensions, len(m.value))
}
var results []byte
for _, part := range m.value {
results = append(results, encFloat(part)...)
}
return results, nil
}
func (m *Float32Vector) UnmarshalCQL(info gocql.TypeInfo, data []byte) error {
panic("unmarshalling vector is not fully implemented")
}
func (m *Float32Vector) Value() []float32 {
return m.value
}
func ensureVectorDimension(values []float32, dimensions int) []float32 {
if len(values) == dimensions {
return values
}
delta := dimensions - len(values)
result := make([]float32, dimensions)
for idx, v := range values {
if idx < dimensions {
result[idx] = v
} else {
break
}
}
for idx := len(values); idx < delta+len(values); idx++ {
result[idx] = 0.0
}
return result
}
func FromVectorFloat32(value []float32, dimensions int) *Float32Vector {
return &Float32Vector{
value: ensureVectorDimension(value, dimensions),
dimensions: dimensions,
}
}
```
You can opt to skip the padding completely and return an error if that feels more natural for your use case. The encoding functions are extracted from the existing gocql codebase.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org