Skip to content

Commit

Permalink
Update Rel8 to work with Opaleye's #586
Browse files Browse the repository at this point in the history
  • Loading branch information
shane-circuithub committed Feb 9, 2024
1 parent 2cbf663 commit b6e8126
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 25 deletions.
78 changes: 66 additions & 12 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rel8.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ library
, data-textual
, hasql ^>= 1.6.1.2
, network-ip
, opaleye ^>= 0.10.2.0
, opaleye ^>= 0.10.2.1
, pretty
, profunctors
, product-profunctors
Expand Down
2 changes: 1 addition & 1 deletion src/Rel8/Expr/List.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Prelude

-- rel8
import Rel8.Expr (Expr)
import Rel8.Expr.Opaleye (fromPrimExpr, mapPrimExpr, toPrimExpr)
import Rel8.Expr.Opaleye (mapPrimExpr, toPrimExpr)
import Rel8.Schema.Null (Nullify, Sql, Unnullify)
import Rel8.Type (DBType, typeInformation)
import Rel8.Type.Information (TypeInformation)
Expand Down
2 changes: 1 addition & 1 deletion src/Rel8/Expr/NonEmpty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Prelude

-- rel8
import Rel8.Expr (Expr)
import Rel8.Expr.Opaleye (fromPrimExpr, mapPrimExpr, toPrimExpr)
import Rel8.Expr.Opaleye (mapPrimExpr, toPrimExpr)
import Rel8.Schema.Null (Nullify, Sql, Unnullify)
import Rel8.Type (DBType, typeInformation)
import Rel8.Type.Information (TypeInformation)
Expand Down
17 changes: 13 additions & 4 deletions src/Rel8/Query/Aggregate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module Rel8.Query.Aggregate
( aggregate
, aggregate1
, aggregateU
, countRows
)
where
Expand All @@ -15,36 +16,44 @@ import Data.Int ( Int64 )
import Prelude

-- opaleye
import qualified Opaleye.Adaptors as Opaleye
import qualified Opaleye.Aggregate as Opaleye

-- rel8
import Rel8.Aggregate (Aggregator' (Aggregator), Aggregator)
import Rel8.Aggregate.Fold (Fallback (Fallback))
import Rel8.Expr ( Expr )
import Rel8.Expr.Aggregate ( countStar )
import Rel8.Expr.Bool (true)
import Rel8.Query ( Query )
import Rel8.Query.Maybe ( optional )
import Rel8.Query.Opaleye ( mapOpaleye )
import Rel8.Table (Table)
import Rel8.Table.Maybe (fromMaybeTable)
import Rel8.Table.Opaleye (unpackspec)


-- | Apply an 'Aggregator' to all rows returned by a 'Query'. If the 'Query'
-- is empty, then a single \"fallback\" row is returned, composed of the
-- identity elements of the constituent aggregation functions.
aggregate :: Table Expr a => Aggregator i a -> Query i -> Query a
aggregate :: (Table Expr i, Table Expr a) => Aggregator i a -> Query i -> Query a
aggregate aggregator@(Aggregator (Fallback fallback) _) =
fmap (fromMaybeTable fallback) . optional . aggregate1 aggregator


-- | Apply an 'Rel8.Aggregator1' to all rows returned by a 'Query'. If
-- the 'Query' is empty, then zero rows are returned.
aggregate1 :: Aggregator' fold i a -> Query i -> Query a
aggregate1 (Aggregator _ aggregator) = mapOpaleye (Opaleye.aggregate aggregator)
aggregate1 :: Table Expr i => Aggregator' fold i a -> Query i -> Query a
aggregate1 = aggregateU unpackspec


aggregateU :: Opaleye.Unpackspec i i -> Aggregator' fold i a -> Query i -> Query a
aggregateU unpack (Aggregator _ aggregator) =
mapOpaleye (Opaleye.aggregateExplicit unpack aggregator)


-- | Count the number of rows returned by a query. Note that this is different
-- from @countStar@, as even if the given query yields no rows, @countRows@
-- will return @0@.
countRows :: Query a -> Query (Expr Int64)
countRows = aggregate countStar
countRows = aggregate countStar . (true <$)
4 changes: 2 additions & 2 deletions src/Rel8/Query/Distinct.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import Rel8.Order ( Order( Order ) )
import Rel8.Query ( Query )
import Rel8.Query.Opaleye ( mapOpaleye )
import Rel8.Table.Eq ( EqTable )
import Rel8.Table.Opaleye ( distinctspec, unpackspec )
import Rel8.Table.Opaleye (distinctspec, unpackspec)


-- | Select all distinct rows from a query, removing duplicates. @distinct q@
-- is equivalent to the SQL statement @SELECT DISTINCT q@.
distinct :: EqTable a => Query a -> Query a
distinct = mapOpaleye (Opaleye.distinctExplicit distinctspec)
distinct = mapOpaleye (Opaleye.distinctExplicit unpackspec distinctspec)


-- | Select all distinct rows from a query, where rows are equivalent according
Expand Down
8 changes: 4 additions & 4 deletions src/Rel8/Tabulate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -338,18 +338,18 @@ lookup k (Tabulation f) = do
-- the given aggregator, and every other possible key contains a single
-- \"fallback\" row is returned, composed of the identity elements of the
-- constituent aggregation functions.
aggregate :: (EqTable k, Table Expr a)
aggregate :: (EqTable k, Table Expr i, Table Expr a)
=> Aggregator i a -> Tabulation k i -> Tabulation k a
aggregate aggregator@(Aggregator (Fallback fallback) _) =
fmap (fromMaybeTable fallback) . optional . aggregate1 aggregator


-- | 'aggregate1' aggregates the values within each key of a
-- 'Tabulation'. There is an implicit @GROUP BY@ on all the key columns.
aggregate1 :: EqTable k
aggregate1 :: (EqTable k, Table Expr i)
=> Aggregator' fold i a -> Tabulation k i -> Tabulation k a
aggregate1 aggregator (Tabulation f) =
Tabulation $ Q.aggregate1 (keyed groupBy (toAggregator1 aggregator)) . f
Tabulation $ Q.aggregateU (keyed unpackspec unpackspec) (keyed groupBy (toAggregator1 aggregator)) . f


-- | 'distinct' ensures a 'Tabulation' has at most one value for
Expand Down Expand Up @@ -416,7 +416,7 @@ order ordering (Tabulation f) =
-- The resulting 'Tabulation' is \"magic\" in that the value @0@ exists at
-- every possible key that wasn't in the given 'Tabulation'.
count :: EqTable k => Tabulation k a -> Tabulation k (Expr Int64)
count = aggregate countStar
count = aggregate countStar . (true <$)


-- | 'optional' produces a \"magic\" 'Tabulation' whereby each
Expand Down

0 comments on commit b6e8126

Please sign in to comment.