bitemyapp / bitemyapp/esqueleto

`coalesceDefault [arrayAgg x] (val [])` causes a malformed array literal

Open
#355 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
399
Forks
107
Avg merge
1d 17h
Merged PRs (30d)
1

Description

I'm writing a query using `arrayAgg`, which can return a `NULL`/`Nothing` value, so I'm combining it with `coalesceDefault` to just get an empty array in that case. E.g.:

```hs
query =
select $ do
x <- from $ values [val (1 :: Int), val 2, val 3]

return $ coalesceDefault [arrayAgg x] (val [])
```

But executing this throws an exception saying the query has a malformed array literal:

```hs
*** Exception: SqlError {sqlState = "22P02", sqlExecStatus = FatalError, sqlErrorMsg = "malformed array literal: \"[]\"", sqlErrorDetail = "\"[\" must introduce explicitly-specified array dimensions.", sqlErrorHint = ""}
```

On the other hand, if I omit the coalesce:

```hs
query =
select $ do
x <- from $ values [val (1 :: Int), val 2, val 3]

return $ arrayAgg x
```

Then I get the expected result of `[Value {unValue = Just [1,2,3]}]`

My guess is that the error happens because the faulty query uses the `PersistField [a]` instance, which serialises the literal as a `PersistList` instead of a `PersistArray`. Perhaps `arrayAgg` shouldn't return a plain list but use the (currently private) [`PostgresArray` from `persistent-postgresql`](https://github.com/yesodweb/persistent/blob/a00b5b539e8e365e09f71280b7f4ec7d08784ffa/persistent-postgresql/Database/Persist/Postgresql/JSON.hs#L383).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.