@@ -6,8 +6,8 @@
7module Rel8.Table.Aggregate
7module Rel8.Table.Aggregate
9− , listAgg
9+ , listAgg, nonEmptyAgg
10− , nonEmptyAgg
10+ , listAggWithOrder, nonEmptyAggWithOrder
@@ -18,7 +18,14 @@ import Prelude
19import Rel8.Aggregate ( Aggregate, Col(..) )
19import Rel8.Aggregate ( Aggregate, Col(..) )
20import Rel8.Expr ( Expr, Col(..) )
20import Rel8.Expr ( Expr, Col(..) )
21−import Rel8.Expr.Aggregate ( groupByExpr, listAggExpr, nonEmptyAggExpr )
21+import Rel8.Expr.Aggregate
24+ , listAggExprWithOrder
26+ , nonEmptyAggExprWithOrder
28+import Rel8.Order ( Order )
22import Rel8.Schema.Dict ( Dict( Dict ) )
29import Rel8.Schema.Dict ( Dict( Dict ) )
23import Rel8.Schema.HTable ( htabulate, hfield )
30import Rel8.Schema.HTable ( htabulate, hfield )
24import Rel8.Schema.HTable.Vectorize ( hvectorize )
31import Rel8.Schema.HTable.Vectorize ( hvectorize )
@@ -53,7 +60,7 @@ groupBy (toColumns -> exprs) = fromColumns $ htabulate $ \field ->
53-- items <- aggregate $ listAgg <$> itemsFromOrder order
60-- items <- aggregate $ listAgg <$> itemsFromOrder order
54-- return (order, items)
61-- return (order, items)
56−listAgg :: Table Expr exprs => exprs -> Aggregate (ListTable exprs)
63+listAgg :: Table Expr a => a -> Aggregate (ListTable a)
57listAgg (toColumns -> exprs) = fromColumns $
64listAgg (toColumns -> exprs) = fromColumns $
58 hvectorize
65 hvectorize
59 (\_ (Identity (DB a)) -> Aggregation $ listAggExpr a)
66 (\_ (Identity (DB a)) -> Aggregation $ listAggExpr a)
@@ -61,8 +68,24 @@ listAgg (toColumns -> exprs) = fromColumns $
63-- | Like 'listAgg', but the result is guaranteed to be a non-empty list.
70-- | 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)
65nonEmptyAgg (toColumns -> exprs) = fromColumns $
72nonEmptyAgg (toColumns -> exprs) = fromColumns $
66 hvectorize
73 hvectorize
67 (\_ (Identity (DB a)) -> Aggregation $ nonEmptyAggExpr a)
74 (\_ (Identity (DB a)) -> Aggregation $ nonEmptyAggExpr a)
68 (pure exprs)
75 (pure exprs)
78+listAggWithOrder :: Table Expr b
79+ => (a -> o) -> (a -> b) -> Order o -> a -> Aggregate (ListTable b)
80+listAggWithOrder o b order a = fromColumns $
82+ (\_ (Identity (DB x)) -> Aggregation $ listAggExprWithOrder (o a) order x)
83+ (pure (toColumns (b a)))
86+nonEmptyAggWithOrder :: Table Expr b
87+ => (a -> o) -> (a -> b) -> Order o -> a -> Aggregate (NonEmptyTable b)
88+nonEmptyAggWithOrder o b order a = fromColumns $
90+ (\_ (Identity (DB x)) -> Aggregation $ nonEmptyAggExprWithOrder (o a) order x)
91+ (pure (toColumns (b a)))