circuithub / rel8

Hey! Hey! Can u rel8?
https://rel8.readthedocs.io
Other
150 stars 38 forks source link

How do I check whether a row is contained in a `ListTable`? #171

Closed marcosh closed 2 years ago

marcosh commented 2 years ago

I would like to have a function with the following signature

ListTable Expr (f Expr) -> f Expr -> Expr Bool

which checks whether the second argument is contained in the list table.

Is this something which is achievable with the provided API? How could I do that?

ocharles commented 2 years ago

Your best option is probably going to be using catListTable to convert a ListTable to a Query of all of its rows, and then using exists to test if the given row is in the list. This won't quite be the type you want, as this will pull you into the Query monad. I'd also warn that this is likely to have quite poor performance (I doubt PostgreSQL will be able to use an index, for example), but I may be wrong!

marcosh commented 2 years ago

Thanks @ocharles for the answer.

I hoped there would be a way without ending in the Query monad

ocharles commented 2 years ago

Unfortunately it will probably be quite difficult, due to what ListTable actual means in terms of a query. If you have a table T with n columns, then a ListTable T is a table with n columns, but each column is a PostgreSQL array. To see if a row is in a list table means you have to check each of these n arrays contains the respective column from the row you're looking up and that all elements have the same index. It's certainly doable, but I'm not sure it can be done with the Rel8 API at the moment.

marcosh commented 2 years ago

I see, thanks for the clarification. For the moment I'm doing it at the Haskell level instead of at the database level. Not super performant, but it makes things easier at least