on da76712 help sign in
rel8/src/Rel8/Table/Window.hs
43 lines 1.3 kB history raw
1{-# language MonoLocalBinds #-}
3module Rel8.Table.Window
4 ( cumulative
5 , cumulative_
6 , currentRow
7 )
8where
10-- base
11import Prelude
13-- opaleye
14import qualified Opaleye.Window as Opaleye
16-- rel8
17import Rel8.Aggregate ( Aggregates )
18import qualified Rel8.Expr.Window as Expr
19import Rel8.Schema.HTable ( hfield, htabulateA )
20import Rel8.Table ( fromColumns, toColumns )
21import Rel8.Window ( Window( Window ) )
24-- | 'cumulative' allows the use of aggregation functions in 'Window'
25-- expressions. In particular, @'cumulative' 'Rel8.sum'@
26-- (when combined with 'Rel8.Window.orderPartitionBy') gives a running total,
27-- also known as a \"cumulative sum\", hence the name @cumulative@.
28cumulative :: Aggregates aggregates exprs => (a -> aggregates) -> Window a exprs
29cumulative f =
30 fmap fromColumns $
31 htabulateA $ \field ->
32 Expr.cumulative ((`hfield` field) . toColumns . f)
35-- | A version of 'cumulative' for use with nullary aggregators like
36-- 'Rel8.Expr.Aggregate.countStar'.
37cumulative_ :: Aggregates aggregates exprs => aggregates -> Window a exprs
38cumulative_ = cumulative . const
41-- | Return every column of the current row of a window query.
42currentRow :: Window a a
43currentRow = Window $ Opaleye.over (Opaleye.noWindowFunction id) mempty mempty