Skip to content

Commit

Permalink
Passing pure to withExplicit is invalid; materialize needs to t…
Browse files Browse the repository at this point in the history
…ake a function (circuithub#231)
  • Loading branch information
shane-circuithub authored Apr 24, 2023
1 parent ecf34ae commit e2a77b7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/Rel8/Query/Materialize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Opaleye.With ( withExplicit )
import Rel8.Expr ( Expr )
import Rel8.Query ( Query )
import Rel8.Query.Opaleye ( fromOpaleye, toOpaleye )
import Rel8.Query.Rebind ( rebind )
import Rel8.Table ( Table )
import Rel8.Table.Opaleye ( unpackspec )

Expand All @@ -32,6 +33,11 @@ import Rel8.Table.Opaleye ( unpackspec )
-- '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.
materialize :: Table Expr a => Query a -> Query (Query a)
materialize query = fromOpaleye $
withExplicit unpackspec (toOpaleye query) (pure . fromOpaleye)
materialize :: Table Expr a => Query a -> (Query a -> Query b) -> Query b
materialize query f =
fromOpaleye $
withExplicit unpackspec
(toOpaleye query')
(toOpaleye . f . fromOpaleye)
where
query' = query >>= rebind "with"
8 changes: 4 additions & 4 deletions src/Rel8/Tabulate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -636,13 +636,13 @@ difference a b = a <* absent b

-- | 'Q.materialize' for 'Tabulation's.
materialize :: (Table Expr k, Table Expr a)
=> Tabulation k a -> Query (Tabulation k a)
materialize tabulation = case peek tabulation of
=> Tabulation k a -> (Tabulation k a -> Query b) -> Query b
materialize tabulation f = case peek tabulation of
Tabulation query -> do
(_, equery) <- query mempty
case equery of
Left as -> liftQuery <$> Q.materialize as
Right kas -> fromQuery <$> Q.materialize kas
Left as -> Q.materialize as (f . liftQuery)
Right kas -> Q.materialize kas (f . fromQuery)


-- | 'Tabulation's can be produced with either 'fromQuery' or 'liftQuery', and
Expand Down

0 comments on commit e2a77b7

Please sign in to comment.