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 Jan 9, 2024
1 parent 2d9b6ab commit a3fa873
Show file tree
Hide file tree
Showing 4 changed files with 46 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 @@ -70,6 +70,7 @@ library
other-modules:
Rel8.Aggregate
Rel8.Aggregate.Fold
Rel8.Aggregate.Function

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

, mode, modeOn
, percentile, percentileOn
Expand Down Expand Up @@ -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
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 a3fa873

Please sign in to comment.