on ba684c0 help sign in
rel8/src/Rel8/Aggregate.hs
154 lines 5.0 kB history raw
1{-# language DataKinds #-}
2{-# language DerivingVia #-}
3{-# language FlexibleContexts #-}
4{-# language FlexibleInstances #-}
5{-# language GADTs #-}
6{-# language GeneralizedNewtypeDeriving #-}
7{-# language MultiParamTypeClasses #-}
8{-# language NamedFieldPuns #-}
9{-# language StandaloneKindSignatures #-}
10{-# language TypeFamilies #-}
11{-# language UndecidableInstances #-}
12{-# language UndecidableSuperClasses #-}
14module Rel8.Aggregate
15 ( Aggregate(..), foldInputs, mapInputs
16 , Aggregator(..), unsafeMakeAggregate
17 , Aggregates
18 , Col( Aggregation )
19 )
20where
22-- base
23import Data.Functor.Const ( Const( Const ), getConst )
24import Data.Functor.Identity ( Identity )
25import Data.Kind ( Constraint, Type )
26import Prelude
28-- opaleye
29import qualified Opaleye.Internal.Aggregate as Opaleye
30import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
31import qualified Opaleye.Internal.PackMap as Opaleye
33-- rel8
34import Rel8.Expr ( Expr, Col(..) )
35import Rel8.Schema.Context ( Interpretation(..) )
36import Rel8.Schema.Context.Label ( Labelable(..) )
37import Rel8.Schema.HTable ( hfield, htabulate, htabulateA, hspecs )
38import Rel8.Schema.Name ( Name )
39import Rel8.Schema.Null ( Sql )
40import Rel8.Schema.Spec ( Spec( Spec ), SSpec(..) )
41import Rel8.Table ( Table, Columns, Context, fromColumns, toColumns )
42import Rel8.Table.Recontextualize ( Recontextualize )
43import Rel8.Type ( DBType )
45-- semigroupoids
46import Data.Functor.Apply ( Apply, WrappedApplicative(..) )
49-- | An @Aggregate a@ describes how to aggregate @Table@s of type @a@. You can
50-- unpack an @Aggregate@ back to @a@ by running it with 'Rel8.aggregate'. As
51-- @Aggregate@ is almost an 'Applicative' functor - but there is no 'pure'
52-- operation. This means 'Aggregate' is an instance of 'Apply', and you can
53-- combine @Aggregate@s using the @<.>@ combinator.
54type Aggregate :: Type -> Type
55newtype Aggregate a = Aggregate (Opaleye.Aggregator () a)
56 deriving newtype Functor
57 deriving Apply via (WrappedApplicative (Opaleye.Aggregator ()))
60instance Interpretation Aggregate where
61 data Col Aggregate _spec where
62 Aggregation :: ()
63 => Aggregate (Expr a)
64 -> Col Aggregate ('Spec labels necessity a)
67instance Table Expr a => Table Aggregate (Aggregate a) where
68 type Columns (Aggregate a) = Columns a
69 type Context (Aggregate a) = Aggregate
71 toColumns a = htabulate $ \field -> case hfield hspecs field of
72 SSpec {} -> Aggregation $ unDB . (`hfield` field) . toColumns <$> a
73 fromColumns as = fmap fromColumns $ htabulateA $ \field ->
74 case hfield as field of
75 Aggregation a -> DB <$> a
78instance Sql DBType a =>
79 Recontextualize Aggregate Aggregate (Aggregate (Expr a)) (Aggregate (Expr a))
82instance Sql DBType a =>
83 Recontextualize Aggregate Expr (Aggregate (Expr a)) (Expr a)
86instance Sql DBType a =>
87 Recontextualize Aggregate Identity (Aggregate (Expr a)) (Identity a)
90instance Sql DBType a =>
91 Recontextualize Aggregate Name (Aggregate (Expr a)) (Name a)
94instance Sql DBType a =>
95 Recontextualize Expr Aggregate (Expr a) (Aggregate (Expr a))
98instance Sql DBType a =>
99 Recontextualize Identity Aggregate (Identity a) (Aggregate (Expr a))
102instance Sql DBType a =>
103 Recontextualize Name Aggregate (Name a) (Aggregate (Expr a))
106instance Labelable Aggregate where
107 labeler (Aggregation aggregate) = Aggregation aggregate
108 unlabeler (Aggregation aggregate) = Aggregation aggregate
111-- | @Aggregates a b@ means that the columns in @a@ are all 'Aggregate' 'Expr's
112-- for the columns in @b@.
113type Aggregates :: Type -> Type -> Constraint
114class Recontextualize Aggregate Expr aggregates exprs => Aggregates aggregates exprs
115instance Recontextualize Aggregate Expr aggregates exprs => Aggregates aggregates exprs
118foldInputs :: Monoid b
119 => (Maybe Aggregator -> Opaleye.PrimExpr -> b) -> Aggregate a -> b
120foldInputs f (Aggregate (Opaleye.Aggregator (Opaleye.PackMap agg))) =
121 getConst $ flip agg () $ \(aggregator, a) ->
122 Const $ f (detuplize <$> aggregator) a
123 where
124 detuplize (operation, ordering, distinction) =
125 Aggregator {operation, ordering, distinction}
128mapInputs :: ()
129 => (Opaleye.PrimExpr -> Opaleye.PrimExpr) -> Aggregate a -> Aggregate a
130mapInputs transform (Aggregate (Opaleye.Aggregator (Opaleye.PackMap agg))) =
131 Aggregate $ Opaleye.Aggregator $ Opaleye.PackMap $ agg . \f input ->
132 f (fmap transform input)
135type Aggregator :: Type
136data Aggregator = Aggregator
137 { operation :: Opaleye.AggrOp
138 , ordering :: [Opaleye.OrderExpr]
139 , distinction :: Opaleye.AggrDistinct
140 }
143unsafeMakeAggregate :: ()
144 => (input -> Opaleye.PrimExpr)
145 -> (Opaleye.PrimExpr -> output)
146 -> Maybe Aggregator
147 -> input
148 -> Aggregate output
149unsafeMakeAggregate input output aggregator expr =
150 Aggregate $ Opaleye.Aggregator $ Opaleye.PackMap $ \f _ ->
151 output <$> f (tuplize <$> aggregator, input expr)
152 where
153 tuplize Aggregator {operation, ordering, distinction} =
154 (operation, ordering, distinction)