{-# language DataKinds #-}
{-# language FlexibleContexts #-}
{-# language ScopedTypeVariables #-}
{-# language TypeFamilies #-}
{-# options_ghc -fno-warn-redundant-constraints #-}
module Rel8.Expr.Aggregate
( count, countDistinct, countStar, countWhere
, and, or
, min, max
, sum, sumWhere
, avg
, stringAgg
, groupByExpr
, listAggExpr, nonEmptyAggExpr
, slistAggExpr, snonEmptyAggExpr
)
where
import Data.Int ( Int64 )
import Data.List.NonEmpty ( NonEmpty )
import Prelude hiding ( and, max, min, null, or, sum )
import qualified Opaleye.Internal.Aggregate as Opaleye
import Opaleye.Internal.Column ( Field_( Column ) )
import qualified Opaleye.Aggregate as Opaleye
import Rel8.Aggregate ( Aggregate, unsafeMakeAggregate )
import Rel8.Expr ( Expr )
import Rel8.Expr.Bool ( caseExpr )
import Rel8.Expr.Opaleye
( castExpr
, fromPrimExpr
, fromPrimExpr
, toPrimExpr
)
import Rel8.Expr.Null ( null )
import Rel8.Expr.Serialize ( litExpr )
import Rel8.Schema.Null ( Sql, Unnullify )
import Rel8.Type ( DBType, typeInformation )
import Rel8.Type.Array ( encodeArrayElement )
import Rel8.Type.Eq ( DBEq )
import Rel8.Type.Information ( TypeInformation )
import Rel8.Type.Num ( DBNum )
import Rel8.Type.Ord ( DBMax, DBMin )
import Rel8.Type.String ( DBString )
import Rel8.Type.Sum ( DBSum )
count :: Expr a -> Aggregate Int64
count :: forall a. Expr a -> Aggregate Int64
count = forall input output (n :: Nullability) (n' :: Nullability) a a'.
(Expr input -> PrimExpr)
-> (PrimExpr -> Expr output)
-> Aggregator (Field_ n a) (Field_ n' a')
-> Expr input
-> Aggregate output
unsafeMakeAggregate forall a. Expr a -> PrimExpr
toPrimExpr forall a. PrimExpr -> Expr a
fromPrimExpr forall a. Aggregator (Field a) (Field SqlInt8)
Opaleye.count
countDistinct :: Sql DBEq a => Expr a -> Aggregate Int64
countDistinct :: forall a. Sql DBEq a => Expr a -> Aggregate Int64
countDistinct = forall input output (n :: Nullability) (n' :: Nullability) a a'.
(Expr input -> PrimExpr)
-> (PrimExpr -> Expr output)
-> Aggregator (Field_ n a) (Field_ n' a')
-> Expr input
-> Aggregate output
unsafeMakeAggregate forall a. Expr a -> PrimExpr
toPrimExpr forall a. PrimExpr -> Expr a
fromPrimExpr forall a b. (a -> b) -> a -> b
$
forall a b. Aggregator a b -> Aggregator a b
Opaleye.distinctAggregator forall a. Aggregator (Field a) (Field SqlInt8)
Opaleye.count
countStar :: Aggregate Int64
countStar :: Aggregate Int64
countStar = forall a. Expr a -> Aggregate Int64
count (forall a. Sql DBType a => a -> Expr a
litExpr Bool
True)
countWhere :: Expr Bool -> Aggregate Int64
countWhere :: Expr Bool -> Aggregate Int64
countWhere Expr Bool
condition = forall a. Expr a -> Aggregate Int64
count (forall a. [(Expr Bool, Expr a)] -> Expr a -> Expr a
caseExpr [(Expr Bool
condition, forall a. Sql DBType a => a -> Expr a
litExpr (forall a. a -> Maybe a
Just Bool
True))] forall a. DBType a => Expr (Maybe a)
null)
and :: Expr Bool -> Aggregate Bool
and :: Expr Bool -> Aggregate Bool
and = forall input output (n :: Nullability) (n' :: Nullability) a a'.
(Expr input -> PrimExpr)
-> (PrimExpr -> Expr output)
-> Aggregator (Field_ n a) (Field_ n' a')
-> Expr input
-> Aggregate output
unsafeMakeAggregate forall a. Expr a -> PrimExpr
toPrimExpr forall a. PrimExpr -> Expr a
fromPrimExpr Aggregator (Field SqlBool) (Field SqlBool)
Opaleye.boolAnd
or :: Expr Bool -> Aggregate Bool
or :: Expr Bool -> Aggregate Bool
or = forall input output (n :: Nullability) (n' :: Nullability) a a'.
(Expr input -> PrimExpr)
-> (PrimExpr -> Expr output)
-> Aggregator (Field_ n a) (Field_ n' a')
-> Expr input
-> Aggregate output
unsafeMakeAggregate forall a. Expr a -> PrimExpr
toPrimExpr forall a. PrimExpr -> Expr a
fromPrimExpr Aggregator (Field SqlBool) (Field SqlBool)
Opaleye.boolOr
max :: Sql DBMax a => Expr a -> Aggregate a
max :: forall a. Sql DBMax a => Expr a -> Aggregate a
max = forall input output (n :: Nullability) (n' :: Nullability) a a'.
(Expr input -> PrimExpr)
-> (PrimExpr -> Expr output)
-> Aggregator (Field_ n a) (Field_ n' a')
-> Expr input
-> Aggregate output
unsafeMakeAggregate forall a. Expr a -> PrimExpr
toPrimExpr forall a. PrimExpr -> Expr a
fromPrimExpr forall a. Aggregator (Field a) (Field a)
Opaleye.unsafeMax
min :: Sql DBMin a => Expr a -> Aggregate a
min :: forall a. Sql DBMin a => Expr a -> Aggregate a
min = forall input output (n :: Nullability) (n' :: Nullability) a a'.
(Expr input -> PrimExpr)
-> (PrimExpr -> Expr output)
-> Aggregator (Field_ n a) (Field_ n' a')
-> Expr input
-> Aggregate output
unsafeMakeAggregate forall a. Expr a -> PrimExpr
toPrimExpr forall a. PrimExpr -> Expr a
fromPrimExpr forall a. Aggregator (Field a) (Field a)
Opaleye.unsafeMin
sum :: Sql DBSum a => Expr a -> Aggregate a
sum :: forall a. Sql DBSum a => Expr a -> Aggregate a
sum = forall input output (n :: Nullability) (n' :: Nullability) a a'.
(Expr input -> PrimExpr)
-> (PrimExpr -> Expr output)
-> Aggregator (Field_ n a) (Field_ n' a')
-> Expr input
-> Aggregate output
unsafeMakeAggregate forall a. Expr a -> PrimExpr
toPrimExpr (forall a. Sql DBType a => Expr a -> Expr a
castExpr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. PrimExpr -> Expr a
fromPrimExpr) forall a. Aggregator (Field a) (Field a)
Opaleye.unsafeSum
avg :: Sql DBSum a => Expr a -> Aggregate a
avg :: forall a. Sql DBSum a => Expr a -> Aggregate a
avg = forall input output (n :: Nullability) (n' :: Nullability) a a'.
(Expr input -> PrimExpr)
-> (PrimExpr -> Expr output)
-> Aggregator (Field_ n a) (Field_ n' a')
-> Expr input
-> Aggregate output
unsafeMakeAggregate forall a. Expr a -> PrimExpr
toPrimExpr (forall a. Sql DBType a => Expr a -> Expr a
castExpr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. PrimExpr -> Expr a
fromPrimExpr) forall a. Aggregator (Field a) (Field a)
Opaleye.unsafeAvg
sumWhere :: (Sql DBNum a, Sql DBSum a)
=> Expr Bool -> Expr a -> Aggregate a
sumWhere :: forall a.
(Sql DBNum a, Sql DBSum a) =>
Expr Bool -> Expr a -> Aggregate a
sumWhere Expr Bool
condition Expr a
a = forall a. Sql DBSum a => Expr a -> Aggregate a
sum (forall a. [(Expr Bool, Expr a)] -> Expr a -> Expr a
caseExpr [(Expr Bool
condition, Expr a
a)] Expr a
0)
stringAgg :: Sql DBString a
=> Expr db -> Expr a -> Aggregate a
stringAgg :: forall a db. Sql DBString a => Expr db -> Expr a -> Aggregate a
stringAgg Expr db
delimiter =
forall input output (n :: Nullability) (n' :: Nullability) a a'.
(Expr input -> PrimExpr)
-> (PrimExpr -> Expr output)
-> Aggregator (Field_ n a) (Field_ n' a')
-> Expr input
-> Aggregate output
unsafeMakeAggregate forall a. Expr a -> PrimExpr
toPrimExpr (forall a. Sql DBType a => Expr a -> Expr a
castExpr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. PrimExpr -> Expr a
fromPrimExpr) (Field SqlText -> Aggregator (Field SqlText) (Field SqlText)
Opaleye.stringAgg (forall (n :: Nullability) sqlType. PrimExpr -> Field_ n sqlType
Column (forall a. Expr a -> PrimExpr
toPrimExpr Expr db
delimiter)))
groupByExpr :: Sql DBEq a => Expr a -> Aggregate a
groupByExpr :: forall a. Sql DBEq a => Expr a -> Aggregate a
groupByExpr = forall input output (n :: Nullability) (n' :: Nullability) a a'.
(Expr input -> PrimExpr)
-> (PrimExpr -> Expr output)
-> Aggregator (Field_ n a) (Field_ n' a')
-> Expr input
-> Aggregate output
unsafeMakeAggregate forall a. Expr a -> PrimExpr
toPrimExpr forall a. PrimExpr -> Expr a
fromPrimExpr forall (n :: Nullability) a. Aggregator (Field_ n a) (Field_ n a)
Opaleye.groupBy
listAggExpr :: Sql DBType a => Expr a -> Aggregate [a]
listAggExpr :: forall a. Sql DBType a => Expr a -> Aggregate [a]
listAggExpr = forall a. TypeInformation (Unnullify a) -> Expr a -> Aggregate [a]
slistAggExpr forall a. DBType a => TypeInformation a
typeInformation
nonEmptyAggExpr :: Sql DBType a => Expr a -> Aggregate (NonEmpty a)
nonEmptyAggExpr :: forall a. Sql DBType a => Expr a -> Aggregate (NonEmpty a)
nonEmptyAggExpr = forall a.
TypeInformation (Unnullify a) -> Expr a -> Aggregate (NonEmpty a)
snonEmptyAggExpr forall a. DBType a => TypeInformation a
typeInformation
slistAggExpr :: ()
=> TypeInformation (Unnullify a) -> Expr a -> Aggregate [a]
slistAggExpr :: forall a. TypeInformation (Unnullify a) -> Expr a -> Aggregate [a]
slistAggExpr TypeInformation (Unnullify a)
info = forall input output (n :: Nullability) (n' :: Nullability) a a'.
(Expr input -> PrimExpr)
-> (PrimExpr -> Expr output)
-> Aggregator (Field_ n a) (Field_ n' a')
-> Expr input
-> Aggregate output
unsafeMakeAggregate Expr a -> PrimExpr
to forall a. PrimExpr -> Expr a
fromPrimExpr forall a. Aggregator (Field a) (Field (SqlArray a))
Opaleye.arrayAgg
where
to :: Expr a -> PrimExpr
to = forall a. TypeInformation a -> PrimExpr -> PrimExpr
encodeArrayElement TypeInformation (Unnullify a)
info forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Expr a -> PrimExpr
toPrimExpr
snonEmptyAggExpr :: ()
=> TypeInformation (Unnullify a) -> Expr a -> Aggregate (NonEmpty a)
snonEmptyAggExpr :: forall a.
TypeInformation (Unnullify a) -> Expr a -> Aggregate (NonEmpty a)
snonEmptyAggExpr TypeInformation (Unnullify a)
info = forall input output (n :: Nullability) (n' :: Nullability) a a'.
(Expr input -> PrimExpr)
-> (PrimExpr -> Expr output)
-> Aggregator (Field_ n a) (Field_ n' a')
-> Expr input
-> Aggregate output
unsafeMakeAggregate Expr a -> PrimExpr
to forall a. PrimExpr -> Expr a
fromPrimExpr forall a. Aggregator (Field a) (Field (SqlArray a))
Opaleye.arrayAgg
where
to :: Expr a -> PrimExpr
to = forall a. TypeInformation a -> PrimExpr -> PrimExpr
encodeArrayElement TypeInformation (Unnullify a)
info forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Expr a -> PrimExpr
toPrimExpr