Skip to content

Commit

Permalink
Add ability to use custom aggregation functions with aggregateFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
shane-circuithub committed Oct 19, 2023
1 parent a94dd9a commit 068517e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelog.d/20231009_170238_shane.obrien_aggregateFunction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Added

- `aggregationFunction`, which allows custom aggregation functions to be used.
1 change: 1 addition & 0 deletions rel8.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ library
other-modules:
Rel8.Aggregate
Rel8.Aggregate.Fold
Rel8.Aggregate.Function

Rel8.Column
Rel8.Column.ADT
Expand Down
10 changes: 10 additions & 0 deletions src/Rel8.hs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,15 @@ module Rel8
, countWhere, countWhereOn
, and, andOn
, or, orOn
, aggregateFunction

, mode, modeOn
, percentile, percentileOn
, percentileContinuous, percentileContinuousOn
, hypotheticalRank
, hypotheticalDenseRank
, hypotheticalPercentRank
, hypotheticalCumeDist

, mode, modeOn
, percentile, percentileOn
Expand Down Expand Up @@ -388,6 +397,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
Expand Down
40 changes: 40 additions & 0 deletions src/Rel8/Aggregate/Function.hs
Original file line number Diff line number Diff line change
@@ -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)))

0 comments on commit 068517e

Please sign in to comment.