on 11bcbbb help sign in
rel8/src/Rel8/Table/Aggregate.hs
83 lines 2.8 kB history raw
1{-# language FlexibleContexts #-}
2{-# language NamedFieldPuns #-}
3{-# language ScopedTypeVariables #-}
4{-# language TypeApplications #-}
5{-# language TypeFamilies #-}
6{-# language ViewPatterns #-}
8module Rel8.Table.Aggregate
9 ( groupBy
10 , listAgg
11 , nonEmptyAgg
12 , sequenceAggregate
13 , distributeAggregate
14 )
15where
17-- base
18import Data.Functor.Identity ( Identity( Identity ) )
19import Prelude
21-- rel8
22import Rel8.Aggregate ( Aggregate, Aggregates, Col(..) )
23import Rel8.Expr ( Expr, Col(..) )
24import Rel8.Expr.Aggregate ( groupByExpr, listAggExpr, nonEmptyAggExpr )
25import Rel8.Schema.Dict ( Dict( Dict ) )
26import Rel8.Schema.HTable ( htabulate, hfield, hdicts )
27import Rel8.Schema.HTable.Vectorize ( hvectorize )
28import Rel8.Schema.Spec.ConstrainDBType ( ConstrainDBType )
29import Rel8.Table ( Table, Columns, toColumns, fromColumns )
30import Rel8.Table.Eq ( EqTable )
31import Rel8.Table.List ( ListTable )
32import Rel8.Table.NonEmpty ( NonEmptyTable )
33import Rel8.Type.Eq ( DBEq )
36-- | Group equal tables together. This works by aggregating each column in the
37-- given table with 'groupByExpr'.
38groupBy :: forall exprs. EqTable exprs => exprs -> Aggregate exprs
39groupBy (toColumns -> exprs) = fromColumns $ htabulate $ \field ->
40 case hfield dicts field of
41 Dict -> case hfield exprs field of
42 DB expr -> Aggregation $ groupByExpr expr
43 where
44 dicts = hdicts @(Columns exprs) @(ConstrainDBType DBEq)
47-- | Aggregate rows into a single row containing an array of all aggregated
48-- rows. This can be used to associate multiple rows with a single row, without
49-- changing the over cardinality of the query. This allows you to essentially
50-- return a tree-like structure from queries.
51--
52-- For example, if we have a table of orders and each orders contains multiple
53-- items, we could aggregate the table of orders, pairing each order with its
54-- items:
55--
56-- @
57-- ordersWithItems :: Query (Order Expr, ListTable (Item Expr))
58-- ordersWithItems = do
59-- order <- each orderSchema
60-- items <- aggregate $ listAgg <$> itemsFromOrder order
61-- return (order, items)
62-- @
63listAgg :: Table Expr exprs => exprs -> Aggregate (ListTable exprs)
64listAgg (toColumns -> exprs) = fromColumns $
65 hvectorize
66 (\_ (Identity (DB a)) -> Aggregation $ listAggExpr a)
67 (pure exprs)
70-- | Like 'listAgg', but the result is guaranteed to be a non-empty list.
71nonEmptyAgg :: Table Expr exprs => exprs -> Aggregate (NonEmptyTable exprs)
72nonEmptyAgg (toColumns -> exprs) = fromColumns $
73 hvectorize
74 (\_ (Identity (DB a)) -> Aggregation $ nonEmptyAggExpr a)
75 (pure exprs)
78sequenceAggregate :: Aggregates aggregates exprs => aggregates -> Aggregate exprs
79sequenceAggregate = fromColumns . toColumns
82distributeAggregate :: Aggregates aggregates exprs => Aggregate exprs -> aggregates
83distributeAggregate = fromColumns . toColumns