{-# language FlexibleContexts #-}
{-# language MonoLocalBinds #-}

module Rel8.Internal.Aggregate.Function (
  aggregateFunction,
  rawAggregateFunction,
) where

-- base
import Prelude

-- opaleye
import qualified Opaleye.Internal.Aggregate as Opaleye
import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye

-- rel8
import Rel8.Internal.Aggregate (Aggregator1, unsafeMakeAggregator)
import Rel8.Internal.Aggregate.Fold (Fallback (Empty))
import Rel8.Internal.Expr (Expr)
import Rel8.Internal.Expr.Opaleye (castExpr, fromColumn, fromPrimExpr)
import Rel8.Internal.Schema.Null (Sql)
import Rel8.Internal.Schema.QualifiedName (QualifiedName, showQualifiedName)
import Rel8.Internal.Table (Table)
import Rel8.Internal.Table.Opaleye (unpackspec)
import Rel8.Internal.Type (DBType)


-- | 'aggregateFunction' allows the use use of custom aggregation functions
-- or PostgreSQL aggregation functions which are not otherwise supported by
-- Rel8.
aggregateFunction ::
  (Table Expr i, Sql DBType a) =>
  QualifiedName ->
  Aggregator1 i (Expr a)
aggregateFunction :: forall i a.
(Table Expr i, Sql DBType a) =>
QualifiedName -> Aggregator1 i (Expr a)
aggregateFunction QualifiedName
name = Expr a -> Expr a
forall a. Sql DBType a => Expr a -> Expr a
castExpr (Expr a -> Expr a)
-> Aggregator' 'Semi i (Expr a) -> Aggregator' 'Semi i (Expr a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QualifiedName -> Aggregator' 'Semi i (Expr a)
forall i a. Table Expr i => QualifiedName -> Aggregator1 i (Expr a)
rawAggregateFunction QualifiedName
name


rawAggregateFunction :: Table Expr i => QualifiedName -> Aggregator1 i (Expr a)
rawAggregateFunction :: forall i a. Table Expr i => QualifiedName -> Aggregator1 i (Expr a)
rawAggregateFunction QualifiedName
name =
  (i -> i)
-> (Field_ (ZonkAny 0) (ZonkAny 1) -> Expr a)
-> Fallback 'Semi (Expr a)
-> Aggregator i (Field_ (ZonkAny 0) (ZonkAny 1))
-> Aggregator' 'Semi i (Expr a)
forall i o (fold :: Fold) i' o'.
(i -> i')
-> (o' -> o)
-> Fallback fold o
-> Aggregator i' o'
-> Aggregator' fold i o
unsafeMakeAggregator
    i -> i
forall a. a -> a
id
    (PrimExpr -> Expr a
forall a. PrimExpr -> Expr a
fromPrimExpr (PrimExpr -> Expr a)
-> (Field_ (ZonkAny 0) (ZonkAny 1) -> PrimExpr)
-> Field_ (ZonkAny 0) (ZonkAny 1)
-> Expr a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Field_ (ZonkAny 0) (ZonkAny 1) -> PrimExpr
forall (n :: Nullability) b. Field_ n b -> PrimExpr
fromColumn)
    Fallback 'Semi (Expr a)
forall a. Fallback 'Semi a
Empty
    (Unpackspec i i
-> AggrOp -> Aggregator i (Field_ (ZonkAny 0) (ZonkAny 1))
forall a a' (n :: Nullability) b.
Unpackspec a a' -> AggrOp -> Aggregator a (Field_ n b)
Opaleye.makeAggrExplicit Unpackspec i i
forall a. Table Expr a => Unpackspec a a
unpackspec
      (String -> AggrOp
Opaleye.AggrOther (QualifiedName -> String
showQualifiedName QualifiedName
name)))