1{-# language FlexibleContexts #-}
2{-# language NamedFieldPuns #-}
3{-# language ScopedTypeVariables #-}
4{-# language TypeApplications #-}
5{-# language TypeFamilies #-}
6{-# language ViewPatterns #-}
8module Rel8.Table.Aggregate
10 , listAgg, nonEmptyAgg
15import Data.Functor.Identity ( Identity( Identity ) )
19import Rel8.Aggregate ( Aggregate, Col( A ) )
20import Rel8.Expr ( Expr, Col( E ) )
21import Rel8.Expr.Aggregate
26import Rel8.Schema.Dict ( Dict( Dict ) )
27import Rel8.Schema.HTable ( hfield, htabulate )
28import Rel8.Schema.HTable.Vectorize ( hvectorize )
29import Rel8.Schema.Spec ( SSpec( SSpec, info ) )
30import Rel8.Table ( Table, toColumns, fromColumns )
31import Rel8.Table.Eq ( EqTable, eqTable )
32import Rel8.Table.List ( ListTable )
33import Rel8.Table.NonEmpty ( NonEmptyTable )
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 (eqTable @exprs) field of
41 Dict -> case hfield exprs field of
42 E expr -> A $ groupByExpr expr
45-- | Aggregate rows into a single row containing an array of all aggregated
46-- rows. This can be used to associate multiple rows with a single row, without
47-- changing the over cardinality of the query. This allows you to essentially
48-- return a tree-like structure from queries.
50-- For example, if we have a table of orders and each orders contains multiple
51-- items, we could aggregate the table of orders, pairing each order with its
55-- ordersWithItems :: Query (Order Expr, ListTable (Item Expr))
56-- ordersWithItems = do
57-- order <- each orderSchema
58-- items <- aggregate $ listAgg <$> itemsFromOrder order
59-- return (order, items)
61listAgg :: Table Expr a => a -> Aggregate (ListTable a)
62listAgg (toColumns -> exprs) = fromColumns $
64 (\SSpec {info} (Identity (E a)) -> A $ slistAggExpr info a)
68-- | Like 'listAgg', but the result is guaranteed to be a non-empty list.
69nonEmptyAgg :: Table Expr a => a -> Aggregate (NonEmptyTable a)
70nonEmptyAgg (toColumns -> exprs) = fromColumns $
72 (\SSpec {info} (Identity (E a)) -> A $ snonEmptyAggExpr info a)
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