haskell-beam / haskell-beam/beam

Postgres returns "numeric" type for "sum_" over Int64 column causing "ColumnTypeMismatch"-error

Open
#545 8 comments 2 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
635
Forks
193
PR merge metrics
No merged PRs in 30d

Description

I have [a table](https://github.com/runeksvendsen/crypto-liquidity-db/blob/d5fb777cf14265b7ca0a284b7586a4431de3c019/src/Schema/PathQty.hs#L25-L32) whose Beam table definition contains an `Int64` field, which is a `BIGINT` in the Postgres database. The field in question is called `qty`:

```
orderbooks=> SELECT pg_typeof(qty) FROM path_qtys LIMIT 1;
pg_typeof
-----------
bigint
(1 row)
```

However, when I do a `sum_` over this field, Postgres returns a `numeric` for this column:

```
orderbooks=> SELECT SUM(qty), pg_typeof(SUM(qty)) FROM path_qtys GROUP BY path__id LIMIT 1;
sum | pg_typeof
-----+-----------
0 | numeric
(1 row)
```

In the [Beam/Haskell code for the `sum_` expression](https://github.com/runeksvendsen/crypto-liquidity-db/blob/d5fb777cf14265b7ca0a284b7586a4431de3c019/src/Query/Liquidity.hs#L181), the return type is `Int64`, which results in this query failing with the error:

```
BeamRowReadError {
brreColumn = Just 6,
brreError = ColumnTypeMismatch {
ctmHaskellType = "Integer",
ctmSQLType = "numeric",
ctmMessage = "types incompatible"
}
}
```

A temporary workaround is to `cast_` the result of the `sum_` operation to `bigint`.

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.