{-# 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

-- base
import Data.Int ( Int64 )
import Data.List.NonEmpty ( NonEmpty )
import Prelude hiding ( and, max, min, null, or, sum )

-- opaleye
import qualified Opaleye.Internal.Aggregate as Opaleye
import Opaleye.Internal.Column ( Field_( Column ) )
import qualified Opaleye.Aggregate as Opaleye

-- rel8
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 the occurances of a single column. Corresponds to @COUNT(a)@
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

-- | Count the number of distinct occurances of a single column. Corresponds to
-- @COUNT(DISTINCT a)@
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


-- | Corresponds to @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)


-- | A count of the number of times a given expression is @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)


-- | Corresponds to @bool_and@.
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


-- | Corresponds to @bool_or@.
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


-- | Produce an aggregation for @Expr a@ using the @max@ function.
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


-- | Produce an aggregation for @Expr a@ using the @max@ function.
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

-- | Corresponds to @sum@. Note that in SQL, @sum@ is type changing - for
-- example the @sum@ of @integer@ returns a @bigint@. Rel8 doesn't support
-- this, and will add explicit casts back to the original input type. This can
-- lead to overflows, and if you anticipate very large sums, you should upcast
-- your input.
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


-- | Corresponds to @avg@. Note that in SQL, @avg@ is type changing - for
-- example, the @avg@ of @integer@ returns a @numeric@. Rel8 doesn't support
-- this, and will add explicit casts back to the original input type. If you
-- need a fractional result on an integral column, you should cast your input
-- to 'Double' or 'Data.Scientific.Scientific' before calling 'avg'.
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

-- | Take the sum of all expressions that satisfy a predicate.
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)


-- | Corresponds to @string_agg()@.
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)))


-- | Aggregate a value by grouping by it.
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


-- | Collect expressions values as a list.
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


-- | Collect expressions values as a non-empty list.
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