Add ability to use custom aggregation functions with aggregateFunction (#283)

This commit is contained in:
Shane 2024-01-09 01:28:04 +00:00 committed by GitHub
parent 91e7a1bfab
commit b5789a692e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,3 @@
### Added
- `aggregationFunction`, which allows custom aggregation functions to be used.

View File

@ -70,6 +70,7 @@ library
other-modules:
Rel8.Aggregate
Rel8.Aggregate.Fold
Rel8.Aggregate.Function
Rel8.Column
Rel8.Column.ADT

View File

@ -284,6 +284,7 @@ module Rel8
, countWhere, countWhereOn
, and, andOn
, or, orOn
, aggregateFunction
, mode, modeOn
, percentile, percentileOn
@ -383,6 +384,7 @@ import Prelude ()
-- rel8
import Rel8.Aggregate
import Rel8.Aggregate.Fold
import Rel8.Aggregate.Function
import Rel8.Column
import Rel8.Column.ADT
import Rel8.Column.Either

View File

@ -0,0 +1,40 @@
{-# language FlexibleContexts #-}
{-# language MonoLocalBinds #-}
module Rel8.Aggregate.Function (
aggregateFunction,
) where
-- base
import Prelude
-- opaleye
import qualified Opaleye.Internal.Aggregate as Opaleye
import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
-- rel8
import Rel8.Aggregate (Aggregator1, unsafeMakeAggregator)
import Rel8.Aggregate.Fold (Fallback (Empty))
import Rel8.Expr (Expr)
import Rel8.Expr.Opaleye (castExpr, fromColumn, fromPrimExpr)
import Rel8.Schema.Null (Sql)
import Rel8.Schema.QualifiedName (QualifiedName, showQualifiedName)
import Rel8.Table (Table)
import Rel8.Table.Opaleye (unpackspec)
import Rel8.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 name =
unsafeMakeAggregator
id
(castExpr . fromPrimExpr . fromColumn)
Empty
(Opaleye.makeAggrExplicit unpackspec
(Opaleye.AggrOther (showQualifiedName name)))