@@ -6,8 +6,8 @@
66
77module Rel8.Table.Aggregate
88 ( groupBy
9− , listAgg
10− , nonEmptyAgg
9+ , listAgg, nonEmptyAgg
10+ , listAggWithOrder, nonEmptyAggWithOrder
1111 )
1212where
1313
@@ -18,7 +18,14 @@ import Prelude
1818-- rel8
1919import Rel8.Aggregate ( Aggregate, Col(..) )
2020import Rel8.Expr ( Expr, Col(..) )
21−import Rel8.Expr.Aggregate ( groupByExpr, listAggExpr, nonEmptyAggExpr )
21+import Rel8.Expr.Aggregate
22+ ( groupByExpr
23+ , listAggExpr
24+ , listAggExprWithOrder
25+ , nonEmptyAggExpr
26+ , nonEmptyAggExprWithOrder
27+ )
28+import Rel8.Order ( Order )
2229import Rel8.Schema.Dict ( Dict( Dict ) )
2330import Rel8.Schema.HTable ( htabulate, hfield )
2431import Rel8.Schema.HTable.Vectorize ( hvectorize )
@@ -53,7 +60,7 @@ groupBy (toColumns -> exprs) = fromColumns $ htabulate $ \field ->
5360-- items <- aggregate $ listAgg <$> itemsFromOrder order
5461-- return (order, items)
5562-- @
56−listAgg :: Table Expr exprs => exprs -> Aggregate (ListTable exprs)
63+listAgg :: Table Expr a => a -> Aggregate (ListTable a)
5764listAgg (toColumns -> exprs) = fromColumns $
5865 hvectorize
5966 (\_ (Identity (DB a)) -> Aggregation $ listAggExpr a)
@@ -61,8 +68,24 @@ listAgg (toColumns -> exprs) = fromColumns $
6168
6269
6370-- | Like 'listAgg', but the result is guaranteed to be a non-empty list.
64−nonEmptyAgg :: Table Expr exprs => exprs -> Aggregate (NonEmptyTable exprs)
71+nonEmptyAgg :: Table Expr a => a -> Aggregate (NonEmptyTable a)
6572nonEmptyAgg (toColumns -> exprs) = fromColumns $
6673 hvectorize
6774 (\_ (Identity (DB a)) -> Aggregation $ nonEmptyAggExpr a)
6875 (pure exprs)
76+
77+
78+listAggWithOrder :: Table Expr b
79+ => (a -> o) -> (a -> b) -> Order o -> a -> Aggregate (ListTable b)
80+listAggWithOrder o b order a = fromColumns $
81+ hvectorize
82+ (\_ (Identity (DB x)) -> Aggregation $ listAggExprWithOrder (o a) order x)
83+ (pure (toColumns (b a)))
84+
85+
86+nonEmptyAggWithOrder :: Table Expr b
87+ => (a -> o) -> (a -> b) -> Order o -> a -> Aggregate (NonEmptyTable b)
88+nonEmptyAggWithOrder o b order a = fromColumns $
89+ hvectorize
90+ (\_ (Identity (DB x)) -> Aggregation $ nonEmptyAggExprWithOrder (o a) order x)
91+ (pure (toColumns (b a)))