aws / pg_tle

Framework for building trusted language extensions for PostgreSQL
Apache License 2.0
337 stars 31 forks source link

Cast from bytea to vector #238

Closed mgoldenbe closed 1 year ago

mgoldenbe commented 1 year ago

My table in AWS RDS Postgres has a column of type bytea, which encodes vectors of numbers in the range from -128 to 127. I need to convert these values of bytea type into vector in order to use the distances defined by the pgvector extension. I understand that I need to use pg_tle to create such a cast. Could you please provide me with a direction for doing this? If a direct cast is not possible, then a cast to int[] would do, because that can be casted to vector.

jim-mlodgenski commented 1 year ago

This is really independent of the pg_tle project, but could be packaged as a tle. It really comes done to how the numbers are encoded in the bytea column. You could write a simple function that loops though all the bytes in the byte array and then cast each one to an int. Then add each int to the int[].