dylex / postgresql-typed

Haskell PostgreSQL library with compile-time type inference
http://hackage.haskell.org/package/postgresql-typed
Other
84 stars 12 forks source link

Cannot get int4range type to work #24

Closed anmolitor closed 3 years ago

anmolitor commented 3 years ago

Hi, first of all thanks for the great work, I really like working with typesafe SQL queries. Recently, I have added ranges to part of my domain model and was pleased to see the support. But I cannot get the "@>" operation to work. Given the following table

CREATE TABLE area (
    id SERIAL PRIMARY KEY,
    name VARCHAR NOT NULL UNIQUE,
    range_x int4range NOT NULL,
    range_y int4range NOT NULL
)

the following simplified query fails

[pgSQL|
      SELECT id, name, range_x, range_y
      FROM area
      WHERE range_x @> ${5 :: Int32}
 |],

complaining that " Could not deduce (Database.PostgreSQL.Typed.Types.PGParameter                           "int4range" Int32)         arising from a use of `Database.PostgreSQL.Typed.Types.pgEscapeParameter' "

I expected that I accidentally reversed the arguments of the function, but ${5 :: Int32} @> range_x yields the same result. Interestingly, the following query compiles without issues:

  [pgSQL|
    SELECT coord_x, coord_y, ground_tile, decoration_tile, overlay_tile
    FROM tile
    WHERE ${rangeX}::int4range @> coord_x
    AND ${rangeY}::int4range @> coord_y
   |]

where rangeX and rangeY are variables of type Range Int32 respectively.

Can anyone help me out here? :)

anmolitor commented 3 years ago

Wrapping the variable in "point" to make a singleton range works, since the @> operator in PostgreSQL works for subranges as well as single values. This should not be necessary though I think.

dylex commented 3 years ago

Does range_x @> ${5 :: Int32}::int4 work? My first guess is that since range @> range is valid, postgres is infering this by default for some reason. (Unfortunately we can't do much to influence postgres type inference beyond these explicit types.)

anmolitor commented 3 years ago

Wow yes it does! Then that just solves the issue, thanks :)