Count (*) compatibility error with Database.PostgreSQL.Simple?

Error

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

It looks like any value returned by count(*) in the request should be converted to Integer , not Int . If I change these specific variables for Integer input, the queries are executed.

But this error did not occur on another machine with the same exact code. The first machine was 32 bits, and the other was 64-bit. This is the only difference I could make out.

Does anyone have any idea what is going on?

+4
source share
2 answers

PostgreSQL's countg () functions return a Bigint type, see

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

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

Haskell int is ~ 2 ** 29, which means it is 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 the implicit conversion down exactly.

Thus, use the Haskell type int64 or the number of instances (*) for an integer.

+2
source

As described in the FromField module, postgresql-simple will only do client-side conversions between numerical types when there is no possibility of overflow or loss of precision. Pay attention, especially to the list of types in the peaks for instance FromField Int : "int2, int4, as well as compiled as 64-bit code, int8. This library was compiled as 32-bit code." The last part of this comment, of course, is specific to the assembly that the hacker himself 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 will get the same exception. You can use Int64 or the Integer arbitrary precision type to avoid this problem on both platforms.

+1
source

Source: https://habr.com/ru/post/1485104/


All Articles