dan
dan

Reputation: 45632

count(*) type compatibility error with Database.PostgreSQL.Simple?

The error is

*** Exception: Incompatible {errSQLType = "int8", errHaskellType = "Int", errMessage = "types incompatible"}

It looks like any value returned by count(*) in the query must be converted into Integer rather than Int. If I change those specific variables to type Integer, the queries work.

But this error wasn't being raised on another machine with the same exact code. The first machine was 32 bit and this other one 64-bit. That's the only difference I could discern.

Does anyone have any insight into what is going on?

Upvotes: 4

Views: 477

Answers (2)

lpsmith
lpsmith

Reputation: 707

As documented in the FromField module, postgresql-simple will only do client-side conversions between numerical types when there isn't any possibility of overflow or loss of precision. Note especially the list of types in the haddocks for the instance FromField Int: "int2, int4, and if compiled as 64-bit code, int8 as well. This library was compiled as 32-bit code." The latter part of that comment is of course specific to the build that hackage itself performs.

On 32-bit platforms, Int is a 32-bit integer, and on 64-bit platforms, Int is a 64-bit integer. If you use Int32 you'll get the same exception. You can use Int64 or the arbitrary-precision Integer type to avoid this problem on both kinds of platform.

Upvotes: 1

Tim Child
Tim Child

Reputation: 3012

The PostgreSQL count() functions returns a Bigint type, see

http://www.postgresql.org/docs/9.2/static/functions-aggregate.html

Bigint are 8 bytes see http://www.postgresql.org/docs/9.2/static/datatype-numeric.html

Haskell int is ~ 2**29 which implies it a 4 byte integer.

http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Int.html

Then its normal that PostgreSQL or its API will not do an implicit downwards conversion in precision.

So use a Haskell int64 type or cast count(*) to integer.

Upvotes: 2

Related Questions