on ca7aaef help sign in
rel8/src/Rel8/Expr/Aggregate.hs
174 lines 4.9 kB history raw
1{-# language DataKinds #-}
2{-# language FlexibleContexts #-}
3{-# language ScopedTypeVariables #-}
4{-# language TypeApplications #-}
5{-# language TypeFamilies #-}
7{-# options_ghc -fno-warn-redundant-constraints #-}
9module Rel8.Expr.Aggregate
10 ( count, countDistinct, countStar, countWhere
11 , and, or
12 , min, max
13 , sum, sumWhere
14 , stringAgg
15 , groupByExpr
16 , listAggExpr, nonEmptyAggExpr
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 ( Sql )
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 )
49-- | Count the occurances of a single column. Corresponds to @COUNT(a)@
50count :: Expr a -> Aggregate (Expr Int64)
51count = unsafeMakeAggregate toPrimExpr fromPrimExpr $
52 Just Aggregator
53 { operation = Opaleye.AggrCount
54 , ordering = []
55 , distinction = Opaleye.AggrAll
56 }
59-- | Count the number of distinct occurances of a single column. Corresponds to
60-- @COUNT(DISTINCT a)@
61countDistinct :: Sql DBEq a => Expr a -> Aggregate (Expr Int64)
62countDistinct = unsafeMakeAggregate toPrimExpr fromPrimExpr $
63 Just Aggregator
64 { operation = Opaleye.AggrCount
65 , ordering = []
66 , distinction = Opaleye.AggrDistinct
67 }
70-- | Corresponds to @COUNT(*)@.
71countStar :: Aggregate (Expr Int64)
72countStar = count (litExpr True)
75-- | A count of the number of times a given expression is @true@.
76countWhere :: Expr Bool -> Aggregate (Expr Int64)
77countWhere condition = count (caseExpr [(condition, litExpr (Just True))] null)
80-- | Corresponds to @bool_and@.
81and :: Expr Bool -> Aggregate (Expr Bool)
82and = unsafeMakeAggregate toPrimExpr fromPrimExpr $
83 Just Aggregator
84 { operation = Opaleye.AggrBoolAnd
85 , ordering = []
86 , distinction = Opaleye.AggrAll
87 }
90-- | Corresponds to @bool_or@.
91or :: Expr Bool -> Aggregate (Expr Bool)
92or = unsafeMakeAggregate toPrimExpr fromPrimExpr $
93 Just Aggregator
94 { operation = Opaleye.AggrBoolOr
95 , ordering = []
96 , distinction = Opaleye.AggrAll
97 }
100-- | Produce an aggregation for @Expr a@ using the @max@ function.
101max :: Sql DBMax a => Expr a -> Aggregate (Expr a)
102max = unsafeMakeAggregate toPrimExpr fromPrimExpr $
103 Just Aggregator
104 { operation = Opaleye.AggrMax
105 , ordering = []
106 , distinction = Opaleye.AggrAll
107 }
110-- | Produce an aggregation for @Expr a@ using the @max@ function.
111min :: Sql DBMin a => Expr a -> Aggregate (Expr a)
112min = unsafeMakeAggregate toPrimExpr fromPrimExpr $
113 Just Aggregator
114 { operation = Opaleye.AggrMin
115 , ordering = []
116 , distinction = Opaleye.AggrAll
117 }
119-- | Corresponds to @sum@. Note that in SQL, @sum@ is type changing - for
120-- example the @sum@ of @integer@ returns a @bigint@. Rel8 doesn't support
121-- this, and will add explicit cast back to the original input type. This can
122-- lead to overflows, and if you anticipate very large sums, you should upcast
123-- your input.
124sum :: Sql DBSum a => Expr a -> Aggregate (Expr a)
125sum = unsafeMakeAggregate toPrimExpr (castExpr . fromPrimExpr) $
126 Just Aggregator
127 { operation = Opaleye.AggrSum
128 , ordering = []
129 , distinction = Opaleye.AggrAll
130 }
133sumWhere :: (Sql DBNum a, Sql DBSum a)
134 => Expr Bool -> Expr a -> Aggregate (Expr a)
135sumWhere condition a = sum (caseExpr [(condition, a)] 0)
138-- | Corresponds to @string_agg()@.
139stringAgg :: Sql DBString a
140 => Expr db -> Expr a -> Aggregate (Expr a)
141stringAgg delimiter =
142 unsafeMakeAggregate toPrimExpr (castExpr . fromPrimExpr) $
143 Just Aggregator
144 { operation = Opaleye.AggrStringAggr (toPrimExpr delimiter)
145 , ordering = []
146 , distinction = Opaleye.AggrAll
147 }
150-- | Aggregate a value by grouping by it.
151groupByExpr :: Sql DBEq a => Expr a -> Aggregate (Expr a)
152groupByExpr = unsafeMakeAggregate toPrimExpr fromPrimExpr Nothing
155listAggExpr :: Expr a -> Aggregate (Expr [a])
156listAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
157 Aggregator
158 { operation = Opaleye.AggrArr
159 , ordering = []
160 , distinction = Opaleye.AggrAll
161 }
162 where
163 from = fromPrimExpr . fromPrimArray
166nonEmptyAggExpr :: Expr a -> Aggregate (Expr (NonEmpty a))
167nonEmptyAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
168 Aggregator
169 { operation = Opaleye.AggrArr
170 , ordering = []
171 , distinction = Opaleye.AggrAll
172 }
173 where
174 from = fromPrimExpr . fromPrimArray