Skip to content

Commit

Permalink
Fix #219 (#240)
Browse files Browse the repository at this point in the history
We need to `rebind` immediately after calling `UNNEST`, before we call `fromColumns`.
  • Loading branch information
shane-circuithub committed Jun 17, 2023
1 parent e2a77b7 commit baaabe7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Rel8/Query/List.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Rel8.Expr.Opaleye ( mapPrimExpr )
import Rel8.Query ( Query )
import Rel8.Query.Aggregate ( aggregate )
import Rel8.Query.Maybe ( optional )
import Rel8.Query.Rebind ( rebind )
import Rel8.Query.Rebind ( hrebind, rebind )
import Rel8.Schema.HTable.Vectorize ( hunvectorize )
import Rel8.Schema.Null ( Sql, Unnullify )
import Rel8.Schema.Spec ( Spec( Spec, info ) )
Expand Down Expand Up @@ -86,7 +86,7 @@ someExpr = aggregate . fmap nonEmptyAggExpr
-- @catListTable@ is an inverse to 'many'.
catListTable :: Table Expr a => ListTable Expr a -> Query a
catListTable (ListTable as) =
rebind "unnest" $ fromColumns $ runIdentity $
fmap fromColumns $ hrebind "unnest" $ runIdentity $
hunvectorize (\Spec {info} -> pure . sunnest info) as


Expand All @@ -96,7 +96,7 @@ catListTable (ListTable as) =
-- @catNonEmptyTable@ is an inverse to 'some'.
catNonEmptyTable :: Table Expr a => NonEmptyTable Expr a -> Query a
catNonEmptyTable (NonEmptyTable as) =
rebind "unnest" $ fromColumns $ runIdentity $
fmap fromColumns $ hrebind "unnest" $ runIdentity $
hunvectorize (\Spec {info} -> pure . sunnest info) as


Expand Down
7 changes: 7 additions & 0 deletions src/Rel8/Query/Rebind.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Rel8.Query.Rebind
( rebind
, hrebind
)
where

Expand All @@ -15,7 +16,9 @@ import qualified Opaleye.Internal.Rebind as Opaleye
-- rel8
import Rel8.Expr ( Expr )
import Rel8.Query ( Query )
import Rel8.Schema.HTable (HTable)
import Rel8.Table ( Table )
import Rel8.Table.Cols (Cols (Cols))
import Rel8.Table.Opaleye ( unpackspec )
import Rel8.Query.Opaleye (fromOpaleye)

Expand All @@ -25,3 +28,7 @@ import Rel8.Query.Opaleye (fromOpaleye)
-- variables. It's essentially a @let@ binding for Postgres expressions.
rebind :: Table Expr a => String -> a -> Query a
rebind prefix a = fromOpaleye (Opaleye.rebindExplicitPrefix prefix unpackspec <<< pure a)


hrebind :: HTable t => String -> t Expr -> Query (t Expr)
hrebind prefix = fmap (\(Cols a) -> a) . rebind prefix . Cols

0 comments on commit baaabe7

Please sign in to comment.