Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Rel8 to work with Opaleye's #586 #305

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <$)
Comment on lines -50 to +59
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious about why this change is needed. What's going on here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes me concerned that there's some implication of the corresponding Opaleye change that I'm not aware of.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, maybe it's just to get a Table Expr i instance? If so then that's fine. (I was worried that there was some subtlety about the SQL that we generate, and this change was to avoid a crashing query.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine it's to avoid propagating that constraint

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, that's reassuring. If so then there's nothing I don't understand and my PR is ready to go Opaleye-side. @duairc, could you please confirm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the delay, but yes I can confirm that this is just to get a Table Expr instance.

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
Loading