on 293706d help sign in
rel8/src/Rel8/Expr/Aggregate.hs
179 lines 5.1 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 , sgroupByExpr
18 )
19where
21-- base
22import Data.Int ( Int64 )
23import Data.List.NonEmpty ( NonEmpty )
24import Prelude hiding ( and, max, min, null, or, sum )
26-- opaleye
27import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
29-- rel8
30import Rel8.Aggregate ( Aggregate, Aggregator(..), unsafeMakeAggregate )
31import Rel8.Expr ( Expr )
32import Rel8.Expr.Bool ( caseExpr )
33import Rel8.Expr.Opaleye
34 ( castExpr
35 , fromPrimExpr
36 , fromPrimExpr
37 , toPrimExpr
38 )
39import Rel8.Expr.Null ( null )
40import Rel8.Expr.Serialize ( litExpr )
41import Rel8.Schema.Nullability ( Nullability, Sql, nullabilization )
42import Rel8.Type.Array ( fromPrimArray )
43import Rel8.Type.Eq ( DBEq )
44import Rel8.Type.Num ( DBNum )
45import Rel8.Type.Ord ( DBMax, DBMin )
46import Rel8.Type.String ( DBString )
47import Rel8.Type.Sum ( DBSum )
50-- | Count the occurances of a single column. Corresponds to @COUNT(a)@
51count :: Expr a -> Aggregate (Expr Int64)
52count = unsafeMakeAggregate toPrimExpr fromPrimExpr $
53 Just Aggregator
54 { operation = Opaleye.AggrCount
55 , ordering = []
56 , distinction = Opaleye.AggrAll
57 }
60-- | Count the number of distinct occurances of a single column. Corresponds to
61-- @COUNT(DISTINCT a)@
62countDistinct :: DBEq a => Expr a -> Aggregate (Expr Int64)
63countDistinct = unsafeMakeAggregate toPrimExpr fromPrimExpr $
64 Just Aggregator
65 { operation = Opaleye.AggrCount
66 , ordering = []
67 , distinction = Opaleye.AggrDistinct
68 }
71-- | Corresponds to @COUNT(*)@.
72countStar :: Aggregate (Expr Int64)
73countStar = count (litExpr True)
76-- | A count of the number of times a given expression is @true@.
77countWhere :: Expr Bool -> Aggregate (Expr Int64)
78countWhere condition = count (caseExpr [(condition, litExpr (Just True))] null)
81-- | Corresponds to @bool_and@.
82and :: Expr Bool -> Aggregate (Expr Bool)
83and = unsafeMakeAggregate toPrimExpr fromPrimExpr $
84 Just Aggregator
85 { operation = Opaleye.AggrBoolAnd
86 , ordering = []
87 , distinction = Opaleye.AggrAll
88 }
91-- | Corresponds to @bool_or@.
92or :: Expr Bool -> Aggregate (Expr Bool)
93or = unsafeMakeAggregate toPrimExpr fromPrimExpr $
94 Just Aggregator
95 { operation = Opaleye.AggrBoolOr
96 , ordering = []
97 , distinction = Opaleye.AggrAll
98 }
101-- | Produce an aggregation for @Expr a@ using the @max@ function.
102max :: Sql DBMax a => Expr a -> Aggregate (Expr a)
103max = unsafeMakeAggregate toPrimExpr fromPrimExpr $
104 Just Aggregator
105 { operation = Opaleye.AggrSum
106 , ordering = []
107 , distinction = Opaleye.AggrAll
108 }
111-- | Produce an aggregation for @Expr a@ using the @max@ function.
112min :: Sql DBMin a => Expr a -> Aggregate (Expr a)
113min = unsafeMakeAggregate toPrimExpr fromPrimExpr $
114 Just Aggregator
115 { operation = Opaleye.AggrSum
116 , ordering = []
117 , distinction = Opaleye.AggrAll
118 }
120-- | Corresponds to @sum@. Note that in SQL, @sum@ is type changing - for
121-- example the @sum@ of @integer@ returns a @bigint@. Rel8 doesn't support
122-- this, and will add explicit cast back to the original input type. This can
123-- lead to overflows, and if you anticipate very large sums, you should upcast
124-- your input.
125sum :: Sql DBSum a => Expr a -> Aggregate (Expr a)
126sum = unsafeMakeAggregate toPrimExpr (castExpr . fromPrimExpr) $
127 Just Aggregator
128 { operation = Opaleye.AggrSum
129 , ordering = []
130 , distinction = Opaleye.AggrAll
131 }
134sumWhere :: (Sql DBNum a, Sql DBSum a)
135 => Expr Bool -> Expr a -> Aggregate (Expr a)
136sumWhere condition a = sum (caseExpr [(condition, a)] 0)
139-- | Corresponds to @string_agg()@.
140stringAgg :: Sql DBString a
141 => Expr db -> Expr a -> Aggregate (Expr a)
142stringAgg delimiter =
143 unsafeMakeAggregate toPrimExpr (castExpr . fromPrimExpr) $
144 Just Aggregator
145 { operation = Opaleye.AggrStringAggr (toPrimExpr delimiter)
146 , ordering = []
147 , distinction = Opaleye.AggrAll
148 }
151-- | Aggregate a value by grouping by it.
152groupByExpr :: Sql DBEq a => Expr a -> Aggregate (Expr a)
153groupByExpr = sgroupByExpr nullabilization
156listAggExpr :: Expr a -> Aggregate (Expr [a])
157listAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
158 Aggregator
159 { operation = Opaleye.AggrArr
160 , ordering = []
161 , distinction = Opaleye.AggrAll
162 }
163 where
164 from = fromPrimExpr . fromPrimArray
167nonEmptyAggExpr :: Expr a -> Aggregate (Expr (NonEmpty a))
168nonEmptyAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
169 Aggregator
170 { operation = Opaleye.AggrArr
171 , ordering = []
172 , distinction = Opaleye.AggrAll
173 }
174 where
175 from = fromPrimExpr . fromPrimArray
178sgroupByExpr :: DBEq db => Nullability db a -> Expr a -> Aggregate (Expr a)
179sgroupByExpr _ = unsafeMakeAggregate toPrimExpr fromPrimExpr Nothing