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
15 , listAggExpr, nonEmptyAggExpr
20import Data.Int ( Int64 )
21import Data.List.NonEmpty ( NonEmpty )
22import Prelude hiding ( and, max, min, null, or, sum )
25import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
28import Rel8.Aggregate ( Aggregate, Aggregator(..), unsafeMakeAggregate )
29import Rel8.Expr ( Expr )
30import Rel8.Expr.Bool ( caseExpr )
31import Rel8.Expr.Opaleye
37import Rel8.Expr.Null ( null )
38import Rel8.Expr.Serialize ( litExpr )
39import Rel8.Schema.Null ( Sql )
40import Rel8.Type.Array ( fromPrimArray )
41import Rel8.Type.Eq ( DBEq )
42import Rel8.Type.Num ( DBNum )
43import Rel8.Type.Ord ( DBMax, DBMin )
44import Rel8.Type.String ( DBString )
45import Rel8.Type.Sum ( DBSum )
48-- | Count the occurances of a single column. Corresponds to @COUNT(a)@
49count :: Expr a -> Aggregate (Expr Int64)
50count = unsafeMakeAggregate toPrimExpr fromPrimExpr $
52 { operation = Opaleye.AggrCount
54 , distinction = Opaleye.AggrAll
58-- | Count the number of distinct occurances of a single column. Corresponds to
60countDistinct :: Sql DBEq a => Expr a -> Aggregate (Expr Int64)
61countDistinct = unsafeMakeAggregate toPrimExpr fromPrimExpr $
63 { operation = Opaleye.AggrCount
65 , distinction = Opaleye.AggrDistinct
69-- | Corresponds to @COUNT(*)@.
70countStar :: Aggregate (Expr Int64)
71countStar = count (litExpr True)
74-- | A count of the number of times a given expression is @true@.
75countWhere :: Expr Bool -> Aggregate (Expr Int64)
76countWhere condition = count (caseExpr [(condition, litExpr (Just True))] null)
79-- | Corresponds to @bool_and@.
80and :: Expr Bool -> Aggregate (Expr Bool)
81and = unsafeMakeAggregate toPrimExpr fromPrimExpr $
83 { operation = Opaleye.AggrBoolAnd
85 , distinction = Opaleye.AggrAll
89-- | Corresponds to @bool_or@.
90or :: Expr Bool -> Aggregate (Expr Bool)
91or = unsafeMakeAggregate toPrimExpr fromPrimExpr $
93 { operation = Opaleye.AggrBoolOr
95 , distinction = Opaleye.AggrAll
99-- | Produce an aggregation for @Expr a@ using the @max@ function.
100max :: Sql DBMax a => Expr a -> Aggregate (Expr a)
101max = unsafeMakeAggregate toPrimExpr fromPrimExpr $
103 { operation = Opaleye.AggrMax
105 , distinction = Opaleye.AggrAll
109-- | Produce an aggregation for @Expr a@ using the @max@ function.
110min :: Sql DBMin a => Expr a -> Aggregate (Expr a)
111min = unsafeMakeAggregate toPrimExpr fromPrimExpr $
113 { operation = Opaleye.AggrMin
115 , distinction = Opaleye.AggrAll
118-- | Corresponds to @sum@. Note that in SQL, @sum@ is type changing - for
119-- example the @sum@ of @integer@ returns a @bigint@. Rel8 doesn't support
120-- this, and will add explicit cast back to the original input type. This can
121-- lead to overflows, and if you anticipate very large sums, you should upcast
123sum :: Sql DBSum a => Expr a -> Aggregate (Expr a)
124sum = unsafeMakeAggregate toPrimExpr (castExpr . fromPrimExpr) $
126 { operation = Opaleye.AggrSum
128 , distinction = Opaleye.AggrAll
132-- | Take the sum of all expressions that satisfy a predicate.
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)
142 unsafeMakeAggregate toPrimExpr (castExpr . fromPrimExpr) $
144 { operation = Opaleye.AggrStringAggr (toPrimExpr delimiter)
146 , distinction = Opaleye.AggrAll
150-- | Aggregate a value by grouping by it.
151groupByExpr :: Sql DBEq a => Expr a -> Aggregate (Expr a)
152groupByExpr = unsafeMakeAggregate toPrimExpr fromPrimExpr Nothing
155-- | Collect expressions values as a list.
156listAggExpr :: Expr a -> Aggregate (Expr [a])
157listAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
159 { operation = Opaleye.AggrArr
161 , distinction = Opaleye.AggrAll
164 from = fromPrimExpr . fromPrimArray
167-- | Collect expressions values as a non-empty list.
168nonEmptyAggExpr :: Expr a -> Aggregate (Expr (NonEmpty a))
169nonEmptyAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
171 { operation = Opaleye.AggrArr
173 , distinction = Opaleye.AggrAll
176 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