1{-# language FlexibleContexts #-}
2{-# language ScopedTypeVariables #-}
3{-# language TypeApplications #-}
4{-# language TypeFamilies #-}
5{-# language ViewPatterns #-}
7module Rel8.Table.Aggregate
10 , listAggWithOrder, nonEmptyAggWithOrder
15import Data.Functor.Identity ( Identity( Identity ) )
19import Rel8.Aggregate ( Aggregate, Col(..) )
20import Rel8.Expr ( Expr, Col(..) )
21import Rel8.Expr.Aggregate
24 , listAggExprWithOrder
26 , nonEmptyAggExprWithOrder
28import Rel8.Order ( Order )
29import Rel8.Schema.Dict ( Dict( Dict ) )
30import Rel8.Schema.HTable ( htabulate, hfield )
31import Rel8.Schema.HTable.Vectorize ( hvectorize )
32import Rel8.Table ( Table, toColumns, fromColumns )
33import Rel8.Table.Eq ( EqTable, eqTable )
34import Rel8.Table.List ( ListTable )
35import Rel8.Table.NonEmpty ( NonEmptyTable )
38-- | Group equal tables together. This works by aggregating each column in the
39-- given table with 'groupByExpr'.
40groupBy :: forall exprs. EqTable exprs => exprs -> Aggregate exprs
41groupBy (toColumns -> exprs) = fromColumns $ htabulate $ \field ->
42 case hfield (eqTable @exprs) field of
43 Dict -> case hfield exprs field of
44 DB expr -> Aggregation $ groupByExpr expr
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.
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
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)
63listAgg :: Table Expr a => a -> Aggregate (ListTable a)
64listAgg (toColumns -> exprs) = fromColumns $
66 (\_ (Identity (DB a)) -> Aggregation $ listAggExpr a)
70-- | Like 'listAgg', but the result is guaranteed to be a non-empty list.
71nonEmptyAgg :: Table Expr a => a -> Aggregate (NonEmptyTable a)
72nonEmptyAgg (toColumns -> exprs) = fromColumns $
74 (\_ (Identity (DB a)) -> Aggregation $ nonEmptyAggExpr a)
78listAggWithOrder :: Table Expr a
79 => Order o -> o -> a -> Aggregate (ListTable a)
80listAggWithOrder order o (toColumns -> exprs) = fromColumns $
82 (\_ (Identity (DB x)) -> Aggregation $ listAggExprWithOrder order o x)
86nonEmptyAggWithOrder :: Table Expr a
87 => Order o -> o -> a -> Aggregate (NonEmptyTable a)
88nonEmptyAggWithOrder order o (toColumns -> exprs) = fromColumns $
90 (\_ (Identity (DB x)) -> Aggregation $ nonEmptyAggExprWithOrder order o x)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list