on 8a441e6 help sign in
rel8/src/Rel8/Expr/Aggregate.hs
163 lines 4.3 kB history raw
1{-# language DataKinds #-}
2{-# language ScopedTypeVariables #-}
3{-# language TypeApplications #-}
4{-# language TypeFamilies #-}
6{-# options_ghc -fno-warn-redundant-constraints #-}
8module Rel8.Expr.Aggregate
9 ( count, countDistinct, countStar, countWhere
10 , and, or
11 , min, max
12 , sum, sumWhere
13 , stringAgg
14 , groupByExpr
15 , listAggExpr, nonEmptyAggExpr
16 , sgroupByExpr
17 )
18where
20-- base
21import Data.Int ( Int64 )
22import Data.List.NonEmpty ( NonEmpty )
23import Prelude hiding ( and, max, min, null, or, sum )
25-- opaleye
26import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
28-- rel8
29import Rel8.Aggregate ( Aggregate, Aggregator(..), unsafeMakeAggregate )
30import Rel8.Expr ( Expr )
31import Rel8.Expr.Bool ( caseExpr )
32import Rel8.Expr.Opaleye
33 ( castExpr
34 , fromPrimExpr
35 , fromPrimExpr
36 , toPrimExpr
37 )
38import Rel8.Expr.Null ( null )
39import Rel8.Expr.Serialize ( litExpr )
40import Rel8.Schema.Nullability ( Nullability, Nullabilizes, nullabilization )
41import Rel8.Type.Array ( fromPrimArray )
42import Rel8.Type.Eq ( DBEq )
43import Rel8.Type.Num ( DBNum )
44import Rel8.Type.Ord ( DBMax, DBMin )
45import Rel8.Type.String ( DBString )
46import Rel8.Type.Sum ( DBSum )
49count :: Expr a -> Aggregate (Expr Int64)
50count = unsafeMakeAggregate toPrimExpr fromPrimExpr $
51 Just Aggregator
52 { operation = Opaleye.AggrCount
53 , ordering = []
54 , distinction = Opaleye.AggrAll
55 }
58countDistinct :: DBEq a => Expr a -> Aggregate (Expr Int64)
59countDistinct = unsafeMakeAggregate toPrimExpr fromPrimExpr $
60 Just Aggregator
61 { operation = Opaleye.AggrCount
62 , ordering = []
63 , distinction = Opaleye.AggrDistinct
64 }
67countStar :: Aggregate (Expr Int64)
68countStar = count (litExpr True)
71countWhere :: Expr Bool -> Aggregate (Expr Int64)
72countWhere condition = count (caseExpr [(condition, litExpr (Just True))] null)
75and :: Expr Bool -> Aggregate (Expr Bool)
76and = unsafeMakeAggregate toPrimExpr fromPrimExpr $
77 Just Aggregator
78 { operation = Opaleye.AggrBoolAnd
79 , ordering = []
80 , distinction = Opaleye.AggrAll
81 }
84or :: Expr Bool -> Aggregate (Expr Bool)
85or = unsafeMakeAggregate toPrimExpr fromPrimExpr $
86 Just Aggregator
87 { operation = Opaleye.AggrBoolOr
88 , ordering = []
89 , distinction = Opaleye.AggrAll
90 }
93max :: (DBMax db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
94max = unsafeMakeAggregate toPrimExpr fromPrimExpr $
95 Just Aggregator
96 { operation = Opaleye.AggrSum
97 , ordering = []
98 , distinction = Opaleye.AggrAll
99 }
102min :: (DBMin db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
103min = unsafeMakeAggregate toPrimExpr fromPrimExpr $
104 Just Aggregator
105 { operation = Opaleye.AggrSum
106 , ordering = []
107 , distinction = Opaleye.AggrAll
108 }
111sum :: (DBSum db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
112sum = unsafeMakeAggregate toPrimExpr (castExpr . fromPrimExpr) $
113 Just Aggregator
114 { operation = Opaleye.AggrSum
115 , ordering = []
116 , distinction = Opaleye.AggrAll
117 }
120sumWhere :: (DBNum db, DBSum db, Nullabilizes db a)
121 => Expr Bool -> Expr a -> Aggregate (Expr a)
122sumWhere condition a = sum (caseExpr [(condition, a)] 0)
125stringAgg :: (DBString db, Nullabilizes db a)
126 => Expr db -> Expr a -> Aggregate (Expr a)
127stringAgg delimiter =
128 unsafeMakeAggregate toPrimExpr (castExpr . fromPrimExpr) $
129 Just Aggregator
130 { operation = Opaleye.AggrStringAggr (toPrimExpr delimiter)
131 , ordering = []
132 , distinction = Opaleye.AggrAll
133 }
136groupByExpr :: (DBEq db, Nullabilizes db a) => Expr a -> Aggregate (Expr a)
137groupByExpr = sgroupByExpr nullabilization
140listAggExpr :: Expr a -> Aggregate (Expr [a])
141listAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
142 Aggregator
143 { operation = Opaleye.AggrArr
144 , ordering = []
145 , distinction = Opaleye.AggrAll
146 }
147 where
148 from = fromPrimExpr . fromPrimArray
151nonEmptyAggExpr :: Expr a -> Aggregate (Expr (NonEmpty a))
152nonEmptyAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
153 Aggregator
154 { operation = Opaleye.AggrArr
155 , ordering = []
156 , distinction = Opaleye.AggrAll
157 }
158 where
159 from = fromPrimExpr . fromPrimArray
162sgroupByExpr :: DBEq db => Nullability db a -> Expr a -> Aggregate (Expr a)
163sgroupByExpr _ = unsafeMakeAggregate toPrimExpr fromPrimExpr Nothing