Skip to content

Commit

Permalink
Switch to WITH _ AS MATERIALIZED (_) for materialize
Browse files Browse the repository at this point in the history
  • Loading branch information
shane-circuithub committed Dec 15, 2023
1 parent 6df1bd2 commit 8afc8a9
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/Rel8/Query/Materialize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

module Rel8.Query.Materialize
( materialize
) where
)
where

-- base
import Prelude

-- opaleye
import Opaleye.With ( withExplicit )
import Opaleye.With ( withMaterializedExplicit )

-- rel8
import Rel8.Expr ( Expr )
Expand All @@ -26,19 +27,16 @@ import Rel8.Table.Opaleye ( unpackspec )
-- you're doing this can sometimes help to nudge it in a particular direction.
--
-- 'materialize' is currently implemented in terms of Postgres'
-- [@WITH](https://www.postgresql.org/docs/current/queries-with.html) syntax.
-- Note that on newer versions of PostgreSQL starting with version 12, @WITH@
-- doesn't always automatically materialize if the results of the query aren't
-- used more than once. We reserve the right to change the implementation of
-- 'materialize' to use the newer @WITH foo AS MATERIALIZED bar@ syntax
-- introduced in PostgreSQL 12 in the future. Currently Rel8 does not use
-- @AS MATERIALIZED@ to support earlier PostgreSQL versions.
-- [@WITH](https://www.postgresql.org/docs/current/queries-with.html) syntax,
-- specifically the @WITH _ AS MATERIALIZED (_)@ form introduced in PostgreSQL
-- 12. This means that 'materialize' can only be used with PostgreSQL 12 or
-- newer.
materialize :: (Table Expr a, Table Expr b)
=> Query a -> (Query a -> Query b) -> Query b
materialize query f =
(>>= rebind "with") . fromOpaleye $
withExplicit unpackspec
withMaterializedExplicit unpackspec
(toOpaleye query')
(toOpaleye . f . fromOpaleye)
where
query' = query >>= rebind "materialize"
query' = query

0 comments on commit 8afc8a9

Please sign in to comment.