-
-
Notifications
You must be signed in to change notification settings - Fork 232
Add support for VECTOR type #3162
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
base: main
Are you sure you want to change the base?
Conversation
33bc757
to
0bae5a0
Compare
90102d4
to
d955334
Compare
…simplifies the logic a lot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small stuff. I think the use of unsafe is fine with one additional validation on your input.
@@ -325,10 +329,22 @@ func ConvertToBool(ctx *Context, v interface{}) (bool, error) { | |||
} | |||
} | |||
|
|||
// DecodeVector decodes a byte slice that represents a vector. This is needed for distance functions. | |||
func DecodeVector(buf []byte) []float32 { | |||
return unsafe.Slice((*float32)(unsafe.Pointer(&buf[0])), len(buf)/int(values.Float32Size)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like a check to ensure that len(buf) % int(values.Float32Size) == 0
would be prudent.
Is there some additional safety we have on the input which I'm not seeing here?
return nil, sql.OutOfRange, fmt.Errorf("VECTOR dimension mismatch: expected %d, got %d", t.Dimensions, len(val)) | ||
if t.Dimensions != 0 && len(val) != int(values.Float32Size)*t.Dimensions { | ||
if len(val)%int(values.Float32Size) != 0 { | ||
return nil, sql.OutOfRange, fmt.Errorf("cannot convert BINARY(%d) to VECTOR(%d), need BINARY(%d)", len(val), t.Dimensions, 4*t.Dimensions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return nil, sql.OutOfRange, fmt.Errorf("cannot convert BINARY(%d) to VECTOR(%d), need BINARY(%d)", len(val), t.Dimensions, 4*t.Dimensions) | |
return nil, sql.OutOfRange, fmt.Errorf("cannot convert BINARY(%d) to VECTOR(%d), need BINARY(%d)", len(val), t.Dimensions, int(values.Float32Size)*t.Dimensions) |
if len(val)%int(values.Float32Size) != 0 { | ||
return nil, sql.OutOfRange, fmt.Errorf("cannot convert BINARY(%d) to VECTOR(%d), need BINARY(%d)", len(val), t.Dimensions, 4*t.Dimensions) | ||
} | ||
return nil, sql.OutOfRange, fmt.Errorf("VECTOR dimension mismatch: expected %d, got %d", t.Dimensions, len(val)/4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return nil, sql.OutOfRange, fmt.Errorf("VECTOR dimension mismatch: expected %d, got %d", t.Dimensions, len(val)/4) | |
return nil, sql.OutOfRange, fmt.Errorf("VECTOR dimension mismatch: expected %d, got %d", t.Dimensions, len(val)/int(values.Float32Size)) |
2953ab3
to
817084a
Compare
This PR adds a VECTOR type to GMS. Vectors are arrays of 32-bit floats.
It also adds several functions that take vectors as arguments, including converting vectors to and from strings, and functions for computing distances between vectors.
Finally, it ensures that vector types work correctly when passed to existing functions (such as BIT_LENGTH, MD5, etc.)