postgrest-js icon indicating copy to clipboard operation
postgrest-js copied to clipboard

support type casts in query typings

Open mosnicholas opened this issue 1 year ago • 11 comments

Describe the bug Embeddings type definition has been updated to string -- it is represented internally as a vector, and in code often as a number[] -- I think number[] would be a more apt definition of the type in typescript.

Screenshot 2023-06-01 at 2 03 54 PM

mosnicholas avatar Jun 01 '23 11:06 mosnicholas

This used to work... Trying to track down the change but can't seem to find it... Pretty frustrating that you haven't even got a response on a breaking change like this...

@codesnik any thoughts?

ChuckJonas avatar Jun 13 '23 02:06 ChuckJonas

The change should be in postgres-meta repo. @soedirgo do we need a new patch release for pgmeta?

sweatybridge avatar Jun 13 '23 02:06 sweatybridge

here is the PR: https://github.com/supabase/postgres-meta/pull/559

I've been creating records and calling rpc using number[], but did just check the result of a query and it's a string so I don't really get what's going on.

ChuckJonas avatar Jun 13 '23 02:06 ChuckJonas

As mentioned in the PR, vector is represented as a JSON string in the response, so we type it as string. You'd need to do a cast like embedding::_float4, though the query typings don't support this atm.

soedirgo avatar Jun 13 '23 04:06 soedirgo

Converting this issue to supporting casts in query typings

soedirgo avatar Jun 13 '23 06:06 soedirgo

@codesnik any thoughts?

@ChuckJonas you probably wanted to mention someone else?

codesnik avatar Jun 13 '23 07:06 codesnik

@codesnik ya sorry!

For some reason, when I typed @soedirgo github shows your username as the first option 🤷

ChuckJonas avatar Jun 13 '23 15:06 ChuckJonas

@soedirgo do you have any idea why it actually still works to pass an array of numbers to the supabase client? I'm guessing maybe the supabase client calls JSON.stringify if it receives a javascript object?

It's fairly annoying to have to convert, when the results from any vector operation is always going to be an number[].

ChuckJonas avatar Jun 13 '23 15:06 ChuckJonas

@steve-chavez does PostgREST convert the JSON array into real[] when inserting into a vector column? I noticed that with plain SQL array[1,2,3] works, but '[1,2,3]'::jsonb doesn't.

soedirgo avatar Jun 14 '23 03:06 soedirgo

@soedirgo No. All it does is pass the json body to json_populate_recordset.

steve-chavez avatar Jun 14 '23 05:06 steve-chavez

OK, so it seems like some numeric types can be converted to vector, which is why the RPC using number[] works. But vector doesn't have a cast to json, so the text representation is used instead [0].

As an alternative to casting, you can add create a computed column like so:

alter table mytable add embedding_arr float4[] generated always as (embedding) stored;

When you query that column it'll be typed as number[].

[0] from the to_json[b] function description

soedirgo avatar Jun 14 '23 07:06 soedirgo