on 28aaa76 help sign in
rel8/src/Rel8/Aggregate.hs
156 lines 5.1 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.Opaque ( Opaque )
36import Rel8.Schema.Context ( Interpretation(..) )
37import Rel8.Schema.Context.Label ( Labelable(..) )
38import Rel8.Schema.HTable ( hfield, htabulate, htabulateA, hspecs )
39import Rel8.Schema.Name ( Name )
40import Rel8.Schema.Nullability ( Sql )
41import Rel8.Schema.Spec ( Spec( Spec ), SSpec(..) )
42import Rel8.Table ( Table, Columns, Context, fromColumns, toColumns )
43import Rel8.Table.Recontextualize ( Recontextualize )
44import Rel8.Type ( DBType )
46-- semigroupoids
47import Data.Functor.Apply ( Apply, WrappedApplicative(..) )
50-- | An @Aggregate a@ describes how to aggregate @Table@s of type @a@. You can
51-- unpack an @Aggregate@ back to @a@ by running it with 'aggregate'. As
52-- @Aggregate@ is almost an 'Applicative' functor - but there is no 'pure'
53-- operation. This means 'Aggregate' is an instance of 'Apply', and you can
54-- combine @Aggregate@s using the @<.>@ combinator.
55type Aggregate :: Type -> Type
56newtype Aggregate a = Aggregate (Opaleye.Aggregator () a)
57 deriving newtype Functor
58 deriving Apply via (WrappedApplicative (Opaleye.Aggregator ()))
61instance Interpretation Aggregate where
62 data Col Aggregate _spec where
63 Aggregation :: ()
64 => Aggregate (Expr a)
65 -> Col Aggregate ('Spec labels necessity a)
68instance Table Expr a => Table Aggregate (Aggregate a) where
69 type Columns (Aggregate a) = Columns a
70 type Context (Aggregate a) = Aggregate
72 toColumns a = htabulate $ \field -> case hfield hspecs field of
73 SSpec {} -> Aggregation $ unDB . (`hfield` field) . toColumns <$> a
74 fromColumns as = fmap fromColumns $ htabulateA $ \field ->
75 case hfield as field of
76 Aggregation a -> DB <$> a
79instance Sql DBType a =>
80 Recontextualize Aggregate Aggregate (Aggregate (Expr a)) (Aggregate (Expr a))
83instance Sql DBType a =>
84 Recontextualize Aggregate Expr (Aggregate (Expr a)) (Expr a)
87instance Sql DBType a =>
88 Recontextualize Aggregate Identity (Aggregate (Expr a)) (Identity a)
91instance Sql DBType a =>
92 Recontextualize Aggregate Name (Aggregate (Expr a)) (Name a)
95instance Sql DBType a =>
96 Recontextualize Expr Aggregate (Expr a) (Aggregate (Expr a))
99instance Sql DBType a =>
100 Recontextualize Identity Aggregate (Identity a) (Aggregate (Expr a))
103instance Sql DBType a =>
104 Recontextualize Name Aggregate (Name a) (Aggregate (Expr a))
107instance Labelable Aggregate where
108 labeler (Aggregation aggregate) = Aggregation aggregate
109 unlabeler (Aggregation aggregate) = Aggregation aggregate
112-- | @Aggregates a b@ means that the columns in @a@ are all 'Aggregate' 'Expr's
113-- for the columns in @b@.
114type Aggregates :: Type -> Type -> Constraint
115class Recontextualize Aggregate Expr aggregates exprs => Aggregates aggregates exprs
116instance Recontextualize Aggregate Expr aggregates exprs => Aggregates aggregates exprs
117instance {-# OVERLAPPING #-} Aggregates (Opaque Aggregate Opaque) (Opaque Expr Opaque)
120foldInputs :: Monoid b
121 => (Maybe Aggregator -> Opaleye.PrimExpr -> b) -> Aggregate a -> b
122foldInputs f (Aggregate (Opaleye.Aggregator (Opaleye.PackMap agg))) =
123 getConst $ flip agg () $ \(aggregator, a) ->
124 Const $ f (detuplize <$> aggregator) a
125 where
126 detuplize (operation, ordering, distinction) =
127 Aggregator {operation, ordering, distinction}
130mapInputs :: ()
131 => (Opaleye.PrimExpr -> Opaleye.PrimExpr) -> Aggregate a -> Aggregate a
132mapInputs transform (Aggregate (Opaleye.Aggregator (Opaleye.PackMap agg))) =
133 Aggregate $ Opaleye.Aggregator $ Opaleye.PackMap $ agg . \f input ->
134 f (fmap transform input)
137type Aggregator :: Type
138data Aggregator = Aggregator
139 { operation :: Opaleye.AggrOp
140 , ordering :: [Opaleye.OrderExpr]
141 , distinction :: Opaleye.AggrDistinct
142 }
145unsafeMakeAggregate :: ()
146 => (input -> Opaleye.PrimExpr)
147 -> (Opaleye.PrimExpr -> output)
148 -> Maybe Aggregator
149 -> input
150 -> Aggregate output
151unsafeMakeAggregate input output aggregator expr =
152 Aggregate $ Opaleye.Aggregator $ Opaleye.PackMap $ \f _ ->
153 output <$> f (tuplize <$> aggregator, input expr)
154 where
155 tuplize Aggregator {operation, ordering, distinction} =
156 (operation, ordering, distinction)