Skip to content
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

postgresql pair function #400

Open
marcosh opened this issue Aug 23, 2024 · 2 comments
Open

postgresql pair function #400

marcosh opened this issue Aug 23, 2024 · 2 comments

Comments

@marcosh
Copy link

marcosh commented Aug 23, 2024

Sometimes in postgresql you need to pass a pair of value to a function.

The example I have in mind is something like array_agg ((foo.bar, foo.baz)) (the double parenthesis is required).

It would be nice to have a pair function with signature SqlExpr (Value a) -> SqlExpr (Value b) -> SqlExpr (Value (a, b)) to construct such values

@belevy
Copy link
Collaborator

belevy commented Sep 15, 2024

This would require full composite type support, especially the way you are describing with your array agg. Composite types are difficult and would likely require work in persistent to support.

@fintara
Copy link

fintara commented Sep 18, 2024

If using JSON is fine, you could do this:

import Database.Esqueleto.Internal.Internal (unsafeSqlFunction)
import Database.Esqueleto.PostgreSQL.JSON (JSONBExpr)

pair :: SqlExpr (Value a) -> SqlExpr (Value b) -> JSONBExpr (a, b)
pair = unsafeSqlFunction "jsonb_build_array"

With array_agg and nullable fields:

arrayRemoveNull $ maybeArray $ arrayAgg $ pair (a ?. Field) (b ?. Field)

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

No branches or pull requests

3 participants