1{-# language DataKinds #-}
2{-# language FlexibleContexts #-}
3{-# language ScopedTypeVariables #-}
4{-# language TypeFamilies #-}
6{-# options_ghc -fno-warn-redundant-constraints #-}
8module Rel8.Expr.Aggregate
9 ( count, countDistinct, countStar, countWhere
16 , array1DAggExpr, listAggExpr, nonEmptyAggExpr
17 , listAggExprWithOrder, nonEmptyAggExprWithOrder
22import Data.Int ( Int64 )
23import Data.List.NonEmpty ( NonEmpty )
24import Prelude hiding ( and, max, min, null, or, sum )
27import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
30import Rel8.Aggregate ( Aggregate, Aggregator(..), unsafeMakeAggregate )
31import Rel8.Expr ( Expr )
32import Rel8.Expr.Bool ( caseExpr )
33import Rel8.Expr.Opaleye
39import Rel8.Expr.Null ( null )
40import Rel8.Expr.Serialize ( litExpr )
41import Rel8.Order ( Order, toOrderExprs )
42import Rel8.Schema.Null ( Sql )
43import Rel8.Type.Array ( fromPrimArray )
44import Rel8.Type.Array1D ( Array1D, NotArray )
45import Rel8.Type.Eq ( DBEq )
46import Rel8.Type.Num ( DBNum )
47import Rel8.Type.Ord ( DBMax, DBMin )
48import Rel8.Type.String ( DBString )
49import Rel8.Type.Sum ( DBSum )
52-- | Count the occurances of a single column. Corresponds to @COUNT(a)@
53count :: Expr a -> Aggregate (Expr Int64)
54count = unsafeMakeAggregate toPrimExpr fromPrimExpr $
56 { operation = Opaleye.AggrCount
58 , distinction = Opaleye.AggrAll
62-- | Count the number of distinct occurances of a single column. Corresponds to
64countDistinct :: Sql DBEq a => Expr a -> Aggregate (Expr Int64)
65countDistinct = unsafeMakeAggregate toPrimExpr fromPrimExpr $
67 { operation = Opaleye.AggrCount
69 , distinction = Opaleye.AggrDistinct
73-- | Corresponds to @COUNT(*)@.
74countStar :: Aggregate (Expr Int64)
75countStar = count (litExpr True)
78-- | A count of the number of times a given expression is @true@.
79countWhere :: Expr Bool -> Aggregate (Expr Int64)
80countWhere condition = count (caseExpr [(condition, litExpr (Just True))] null)
83-- | Corresponds to @bool_and@.
84and :: Expr Bool -> Aggregate (Expr Bool)
85and = unsafeMakeAggregate toPrimExpr fromPrimExpr $
87 { operation = Opaleye.AggrBoolAnd
89 , distinction = Opaleye.AggrAll
93-- | Corresponds to @bool_or@.
94or :: Expr Bool -> Aggregate (Expr Bool)
95or = unsafeMakeAggregate toPrimExpr fromPrimExpr $
97 { operation = Opaleye.AggrBoolOr
99 , distinction = Opaleye.AggrAll
103-- | Produce an aggregation for @Expr a@ using the @max@ function.
104max :: Sql DBMax a => Expr a -> Aggregate (Expr a)
105max = unsafeMakeAggregate toPrimExpr fromPrimExpr $
107 { operation = Opaleye.AggrMax
109 , distinction = Opaleye.AggrAll
113-- | Produce an aggregation for @Expr a@ using the @max@ function.
114min :: Sql DBMin a => Expr a -> Aggregate (Expr a)
115min = unsafeMakeAggregate toPrimExpr fromPrimExpr $
117 { operation = Opaleye.AggrMin
119 , distinction = Opaleye.AggrAll
122-- | Corresponds to @sum@. Note that in SQL, @sum@ is type changing - for
123-- example the @sum@ of @integer@ returns a @bigint@. Rel8 doesn't support
124-- this, and will add explicit cast back to the original input type. This can
125-- lead to overflows, and if you anticipate very large sums, you should upcast
127sum :: Sql DBSum a => Expr a -> Aggregate (Expr a)
128sum = unsafeMakeAggregate toPrimExpr (castExpr . fromPrimExpr) $
130 { operation = Opaleye.AggrSum
132 , distinction = Opaleye.AggrAll
136-- | Take the sum of all expressions that satisfy a predicate.
137sumWhere :: (Sql DBNum a, Sql DBSum a)
138 => Expr Bool -> Expr a -> Aggregate (Expr a)
139sumWhere condition a = sum (caseExpr [(condition, a)] 0)
142-- | Corresponds to @string_agg()@.
143stringAgg :: Sql DBString a
144 => Expr db -> Expr a -> Aggregate (Expr a)
146 unsafeMakeAggregate toPrimExpr (castExpr . fromPrimExpr) $
148 { operation = Opaleye.AggrStringAggr (toPrimExpr delimiter)
150 , distinction = Opaleye.AggrAll
154-- | Aggregate a value by grouping by it.
155groupByExpr :: Sql DBEq a => Expr a -> Aggregate (Expr a)
156groupByExpr = unsafeMakeAggregate toPrimExpr fromPrimExpr Nothing
159-- | Keep only the first value.
160headAggExpr :: Expr a -> Aggregate (Expr a)
161headAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
163 { operation = Opaleye.AggrArr
165 , distinction = Opaleye.AggrAll
168 from = fromPrimExpr . flip Opaleye.ArrayIndex one
170 one = Opaleye.ConstExpr (Opaleye.NumericLit 1)
173-- | Collect expressions values as an array.
174array1DAggExpr :: NotArray a => Expr a -> Aggregate (Expr (Array1D a))
175array1DAggExpr = unsafeMakeAggregate toPrimExpr fromPrimExpr $ Just
177 { operation = Opaleye.AggrArr
179 , distinction = Opaleye.AggrAll
183-- | Collect expressions values as a list.
184listAggExpr :: Expr a -> Aggregate (Expr [a])
185listAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
187 { operation = Opaleye.AggrArr
189 , distinction = Opaleye.AggrAll
192 from = fromPrimExpr . fromPrimArray
195-- | Collect expressions values as a non-empty list.
196nonEmptyAggExpr :: Expr a -> Aggregate (Expr (NonEmpty a))
197nonEmptyAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
199 { operation = Opaleye.AggrArr
201 , distinction = Opaleye.AggrAll
204 from = fromPrimExpr . fromPrimArray
207listAggExprWithOrder :: Order o -> o -> Expr a -> Aggregate (Expr [a])
208listAggExprWithOrder order o = unsafeMakeAggregate toPrimExpr from $ Just
210 { operation = Opaleye.AggrArr
211 , ordering = toOrderExprs order o
212 , distinction = Opaleye.AggrAll
215 from = fromPrimExpr . fromPrimArray
218nonEmptyAggExprWithOrder :: ()
219 => Order o -> o -> Expr a -> Aggregate (Expr (NonEmpty a))
220nonEmptyAggExprWithOrder order o = unsafeMakeAggregate toPrimExpr from $ Just
222 { operation = Opaleye.AggrArr
223 , ordering = toOrderExprs order o
224 , distinction = Opaleye.AggrAll
227 from = fromPrimExpr . fromPrimArray
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list