Skip to content

Conversation

nicktobey
Copy link
Contributor

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.)

@nicktobey nicktobey force-pushed the nicktobey/vector2 branch 2 times, most recently from 90102d4 to d955334 Compare August 19, 2025 17:26
Copy link
Contributor

@macneale4 macneale4 left a 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))
Copy link
Contributor

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants