{-# language FlexibleContexts #-}
{-# language NamedFieldPuns #-}
{-# language ScopedTypeVariables #-}
{-# language TypeApplications #-}
{-# language TypeFamilies #-}
{-# language ViewPatterns #-}
module Rel8.Table.Aggregate
( groupBy, hgroupBy
, listAgg, nonEmptyAgg
)
where
import Data.Functor.Identity ( Identity( Identity ) )
import Prelude
import Rel8.Aggregate ( Aggregate, Aggregates )
import Rel8.Expr ( Expr )
import Rel8.Expr.Aggregate
( groupByExpr
, slistAggExpr
, snonEmptyAggExpr
)
import Rel8.Schema.Dict ( Dict( Dict ) )
import Rel8.Schema.HTable ( HTable, hfield, htabulate )
import Rel8.Schema.HTable.Vectorize ( hvectorize )
import Rel8.Schema.Null ( Sql )
import Rel8.Schema.Spec ( Spec( Spec, info ) )
import Rel8.Table ( toColumns, fromColumns )
import Rel8.Table.Eq ( EqTable, eqTable )
import Rel8.Table.List ( ListTable )
import Rel8.Table.NonEmpty ( NonEmptyTable )
import Rel8.Type.Eq ( DBEq )
groupBy :: forall exprs aggregates. (EqTable exprs, Aggregates aggregates exprs)
=> exprs -> aggregates
groupBy :: forall exprs aggregates.
(EqTable exprs, Aggregates aggregates exprs) =>
exprs -> aggregates
groupBy = forall (context :: Context) a.
Table context a =>
Columns a context -> a
fromColumns forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: HTable).
HTable t =>
t (Dict (Sql DBEq)) -> t Expr -> t Aggregate
hgroupBy (forall a. EqTable a => Columns a (Dict (Sql DBEq))
eqTable @exprs) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (context :: Context) a.
Table context a =>
a -> Columns a context
toColumns
hgroupBy :: HTable t => t (Dict (Sql DBEq)) -> t Expr -> t Aggregate
hgroupBy :: forall (t :: HTable).
HTable t =>
t (Dict (Sql DBEq)) -> t Expr -> t Aggregate
hgroupBy t (Dict (Sql DBEq))
eqs t Expr
exprs = forall (t :: HTable) (context :: Context).
HTable t =>
(forall a. HField t a -> context a) -> t context
htabulate forall a b. (a -> b) -> a -> b
$ \HField t a
field ->
case forall (t :: HTable) (context :: Context) a.
HTable t =>
t context -> HField t a -> context a
hfield t (Dict (Sql DBEq))
eqs HField t a
field of
Dict (Sql DBEq) a
Dict -> case forall (t :: HTable) (context :: Context) a.
HTable t =>
t context -> HField t a -> context a
hfield t Expr
exprs HField t a
field of
Expr a
expr -> forall a. Sql DBEq a => Expr a -> Aggregate a
groupByExpr Expr a
expr
listAgg :: Aggregates aggregates exprs => exprs -> ListTable Aggregate aggregates
listAgg :: forall aggregates exprs.
Aggregates aggregates exprs =>
exprs -> ListTable Aggregate aggregates
listAgg (forall (context :: Context) a.
Table context a =>
a -> Columns a context
toColumns -> Columns exprs Expr
exprs) = forall (context :: Context) a.
Table context a =>
Columns a context -> a
fromColumns forall a b. (a -> b) -> a -> b
$
forall (t :: HTable) (f :: Context) (list :: Context)
(context :: Context) (context' :: Context).
(HTable t, Unzip f, Vector list) =>
(forall a. Spec a -> f (context a) -> context' (list a))
-> f (t context) -> HVectorize list t context'
hvectorize
(\Spec {TypeInformation (Unnullify a)
info :: TypeInformation (Unnullify a)
info :: forall a. Spec a -> TypeInformation (Unnullify a)
info} (Identity Expr a
a) -> forall a. TypeInformation (Unnullify a) -> Expr a -> Aggregate [a]
slistAggExpr TypeInformation (Unnullify a)
info Expr a
a)
(forall (f :: Context) a. Applicative f => a -> f a
pure Columns exprs Expr
exprs)
nonEmptyAgg :: Aggregates aggregates exprs => exprs -> NonEmptyTable Aggregate aggregates
nonEmptyAgg :: forall aggregates exprs.
Aggregates aggregates exprs =>
exprs -> NonEmptyTable Aggregate aggregates
nonEmptyAgg (forall (context :: Context) a.
Table context a =>
a -> Columns a context
toColumns -> Columns exprs Expr
exprs) = forall (context :: Context) a.
Table context a =>
Columns a context -> a
fromColumns forall a b. (a -> b) -> a -> b
$
forall (t :: HTable) (f :: Context) (list :: Context)
(context :: Context) (context' :: Context).
(HTable t, Unzip f, Vector list) =>
(forall a. Spec a -> f (context a) -> context' (list a))
-> f (t context) -> HVectorize list t context'
hvectorize
(\Spec {TypeInformation (Unnullify a)
info :: TypeInformation (Unnullify a)
info :: forall a. Spec a -> TypeInformation (Unnullify a)
info} (Identity Expr a
a) -> forall a.
TypeInformation (Unnullify a) -> Expr a -> Aggregate (NonEmpty a)
snonEmptyAggExpr TypeInformation (Unnullify a)
info Expr a
a)
(forall (f :: Context) a. Applicative f => a -> f a
pure Columns exprs Expr
exprs)