@@ -13,6 +13,7 @@ module Rel8.Aggregate.Legacy
1313 , aggregate
1414 , aggregateTabulation
1515 , groupBy
16+ , headAgg
1617 , listAgg
1718 , nonEmptyAgg
1819
@@ -33,14 +34,18 @@ import qualified Opaleye.Aggregate as Opaleye
3334-- rel8
3435import Rel8.Aggregate ( Aggregate, Aggregates, Col( A ) )
3536import Rel8.Expr ( Col( E ), Expr )
36−import Rel8.Expr.Aggregate ( groupByExpr, listAggExpr, nonEmptyAggExpr )
37+import Rel8.Expr.Aggregate
38+ ( groupByExpr
39+ , headAggExpr
40+ , listAggExpr, nonEmptyAggExpr
41+ )
3742import Rel8.Generic.Construction ( GGAggregate', ggaggregate' )
3843import Rel8.Generic.Table ( GAlgebra )
3944import Rel8.Kind.Algebra ( Algebra( Sum ) )
4045import Rel8.Query ( Query )
4146import Rel8.Query.Opaleye ( mapOpaleye )
4247import Rel8.Schema.Dict ( Dict( Dict ) )
43−import Rel8.Schema.HTable ( htabulate, hfield )
48+import Rel8.Schema.HTable ( hmap, hfield, htabulate )
4449import Rel8.Schema.HTable.Vectorize ( hvectorize )
4550import Rel8.Schema.Kind ( Rel8able )
4651import Rel8.Table ( toColumns, fromColumns )
@@ -76,6 +81,11 @@ groupBy (toColumns -> exprs) = fromColumns $ htabulate $ \field ->
7681 E expr -> A $ groupByExpr expr
7782
7883
84+-- | Keep only the first row's values.
85+headAgg :: Aggregates aggregates exprs => exprs -> aggregates
86+headAgg = fromColumns . hmap (\(E a) -> A $ headAggExpr a) . toColumns
87+
88+
7989-- | Aggregate rows into a single row containing an array of all aggregated
8090-- rows. This can be used to associate multiple rows with a single row, without
8191-- changing the over cardinality of the query. This allows you to essentially