on master help sign in
Add headAgg aggregation function
commit43bf3a404bbdd424dd90037b11d2dc7baae8dcc0 authorShane O'Brien <shane.obrien@circuithub.com> dateSun 13 Jun 2021, 23:52 (5 years ago) parent5a45f67
4 files changed+37−4
src/Rel8.hs+2−1 src/Rel8/Aggregate/Legacy.hs+12−2 src/Rel8/Expr/Aggregate.hs+15 src/Rel8/Table/Aggregate.hs+8−1
src/Rel8.hs+2−1
@@ -215,10 +215,11 @@ module Rel8
215215 , Aggregate
216216 , aggregate
217217 , countRows
218+ , groupBy
219+ , headAgg
218220 , array1DAggExpr
219221 , listAgg, listAggWithOrder, listAggExpr
220222 , nonEmptyAgg, nonEmptyAggWithOrder, nonEmptyAggExpr
221− , groupBy
222223 , DBMax, max
223224 , DBMin, min
224225 , DBSum, sum, sumWhere
src/Rel8/Aggregate/Legacy.hs+12−2
@@ -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
src/Rel8/Expr/Aggregate.hs+15
@@ -12,6 +12,7 @@ module Rel8.Expr.Aggregate
1212 , sum, sumWhere
1313 , stringAgg
1414 , groupByExpr
15+ , headAggExpr
1516 , array1DAggExpr, listAggExpr, nonEmptyAggExpr
1617 , listAggExprWithOrder, nonEmptyAggExprWithOrder
1718 )
@@ -155,6 +156,20 @@ groupByExpr :: Sql DBEq a => Expr a -> Aggregate (Expr a)
155156groupByExpr = unsafeMakeAggregate toPrimExpr fromPrimExpr Nothing
156157
157158
159+-- | Keep only the first value.
160+headAggExpr :: Expr a -> Aggregate (Expr a)
161+headAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
162+ Aggregator
163+ { operation = Opaleye.AggrArr
164+ , ordering = []
165+ , distinction = Opaleye.AggrAll
166+ }
167+ where
168+ from = fromPrimExpr . flip Opaleye.ArrayIndex one
169+ where
170+ one = Opaleye.ConstExpr (Opaleye.NumericLit 1)
171+
172+
158173-- | Collect expressions values as an array.
159174array1DAggExpr :: NotArray a => Expr a -> Aggregate (Expr (Array1D a))
160175array1DAggExpr = unsafeMakeAggregate toPrimExpr fromPrimExpr $ Just
src/Rel8/Table/Aggregate.hs+8−1
@@ -6,6 +6,7 @@
66
77module Rel8.Table.Aggregate
88 ( groupBy
9+ , headAgg
910 , listAgg, nonEmptyAgg
1011 , listAggWithOrder, nonEmptyAggWithOrder
1112 )
@@ -20,6 +21,7 @@ import Rel8.Aggregate ( Aggregate, Col( A ) )
2021import Rel8.Expr ( Expr, Col( E ) )
2122import Rel8.Expr.Aggregate
2223 ( groupByExpr
24+ , headAggExpr
2325 , listAggExpr
2426 , listAggExprWithOrder
2527 , nonEmptyAggExpr
@@ -27,7 +29,7 @@ import Rel8.Expr.Aggregate
2729 )
2830import Rel8.Order ( Order )
2931import Rel8.Schema.Dict ( Dict( Dict ) )
30−import Rel8.Schema.HTable ( htabulate, hfield )
32+import Rel8.Schema.HTable ( hfield, hmap, htabulate )
3133import Rel8.Schema.HTable.Vectorize ( hvectorize )
3234import Rel8.Table ( Table, toColumns, fromColumns )
3335import Rel8.Table.Eq ( EqTable, eqTable )
@@ -44,6 +46,11 @@ groupBy (toColumns -> exprs) = fromColumns $ htabulate $ \field ->
4446 E expr -> A $ groupByExpr expr
4547
4648
49+-- | Keep only the first row.
50+headAgg :: Table Expr a => a -> Aggregate a
51+headAgg = fromColumns . hmap (\(E a) -> A $ headAggExpr a) . toColumns
52+
53+
4754-- | Aggregate rows into a single row containing an array of all aggregated
4855-- rows. This can be used to associate multiple rows with a single row, without
4956-- changing the over cardinality of the query. This allows you to essentially