on master help sign in
Add headAgg aggregation function
43bf3a4Shane O'Brien
src/Rel8.hs+2−1
@@ -215,10 +215,11 @@ module Rel8
215 , Aggregate
215 , Aggregate
216 , aggregate
216 , aggregate
217 , countRows
217 , countRows
218+ , groupBy
219+ , headAgg
218 , array1DAggExpr
220 , array1DAggExpr
219 , listAgg, listAggWithOrder, listAggExpr
221 , listAgg, listAggWithOrder, listAggExpr
220 , nonEmptyAgg, nonEmptyAggWithOrder, nonEmptyAggExpr
222 , nonEmptyAgg, nonEmptyAggWithOrder, nonEmptyAggExpr
221− , groupBy
222 , DBMax, max
223 , DBMax, max
223 , DBMin, min
224 , DBMin, min
224 , DBSum, sum, sumWhere
225 , DBSum, sum, sumWhere
src/Rel8/Aggregate/Legacy.hs+12−2
@@ -13,6 +13,7 @@ module Rel8.Aggregate.Legacy
13 , aggregate
13 , aggregate
14 , aggregateTabulation
14 , aggregateTabulation
15 , groupBy
15 , groupBy
16+ , headAgg
16 , listAgg
17 , listAgg
17 , nonEmptyAgg
18 , nonEmptyAgg
18
19
@@ -33,14 +34,18 @@ import qualified Opaleye.Aggregate as Opaleye
33-- rel8
34-- rel8
34import Rel8.Aggregate ( Aggregate, Aggregates, Col( A ) )
35import Rel8.Aggregate ( Aggregate, Aggregates, Col( A ) )
35import Rel8.Expr ( Col( E ), Expr )
36import Rel8.Expr ( Col( E ), Expr )
37+import Rel8.Expr.Aggregate
38+ ( groupByExpr
39+ , headAggExpr
36−import Rel8.Expr.Aggregate ( groupByExpr, listAggExpr, nonEmptyAggExpr )
40+ , listAggExpr, nonEmptyAggExpr
41+ )
37import Rel8.Generic.Construction ( GGAggregate', ggaggregate' )
42import Rel8.Generic.Construction ( GGAggregate', ggaggregate' )
38import Rel8.Generic.Table ( GAlgebra )
43import Rel8.Generic.Table ( GAlgebra )
39import Rel8.Kind.Algebra ( Algebra( Sum ) )
44import Rel8.Kind.Algebra ( Algebra( Sum ) )
40import Rel8.Query ( Query )
45import Rel8.Query ( Query )
41import Rel8.Query.Opaleye ( mapOpaleye )
46import Rel8.Query.Opaleye ( mapOpaleye )
42import Rel8.Schema.Dict ( Dict( Dict ) )
47import Rel8.Schema.Dict ( Dict( Dict ) )
43−import Rel8.Schema.HTable ( htabulate, hfield )
48+import Rel8.Schema.HTable ( hmap, hfield, htabulate )
44import Rel8.Schema.HTable.Vectorize ( hvectorize )
49import Rel8.Schema.HTable.Vectorize ( hvectorize )
45import Rel8.Schema.Kind ( Rel8able )
50import Rel8.Schema.Kind ( Rel8able )
46import Rel8.Table ( toColumns, fromColumns )
51import Rel8.Table ( toColumns, fromColumns )
@@ -76,6 +81,11 @@ groupBy (toColumns -> exprs) = fromColumns $ htabulate $ \field ->
76 E expr -> A $ groupByExpr expr
81 E expr -> A $ groupByExpr expr
77
82
78
83
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+
79-- | Aggregate rows into a single row containing an array of all aggregated
89-- | Aggregate rows into a single row containing an array of all aggregated
80-- rows. This can be used to associate multiple rows with a single row, without
90-- rows. This can be used to associate multiple rows with a single row, without
81-- changing the over cardinality of the query. This allows you to essentially
91-- 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
12 , sum, sumWhere
12 , sum, sumWhere
13 , stringAgg
13 , stringAgg
14 , groupByExpr
14 , groupByExpr
15+ , headAggExpr
15 , array1DAggExpr, listAggExpr, nonEmptyAggExpr
16 , array1DAggExpr, listAggExpr, nonEmptyAggExpr
16 , listAggExprWithOrder, nonEmptyAggExprWithOrder
17 , listAggExprWithOrder, nonEmptyAggExprWithOrder
17 )
18 )
@@ -155,6 +156,20 @@ groupByExpr :: Sql DBEq a => Expr a -> Aggregate (Expr a)
155groupByExpr = unsafeMakeAggregate toPrimExpr fromPrimExpr Nothing
156groupByExpr = unsafeMakeAggregate toPrimExpr fromPrimExpr Nothing
156
157
157
158
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+
158-- | Collect expressions values as an array.
173-- | Collect expressions values as an array.
159array1DAggExpr :: NotArray a => Expr a -> Aggregate (Expr (Array1D a))
174array1DAggExpr :: NotArray a => Expr a -> Aggregate (Expr (Array1D a))
160array1DAggExpr = unsafeMakeAggregate toPrimExpr fromPrimExpr $ Just
175array1DAggExpr = unsafeMakeAggregate toPrimExpr fromPrimExpr $ Just
src/Rel8/Table/Aggregate.hs+8−1
@@ -6,6 +6,7 @@
6
6
7module Rel8.Table.Aggregate
7module Rel8.Table.Aggregate
8 ( groupBy
8 ( groupBy
9+ , headAgg
9 , listAgg, nonEmptyAgg
10 , listAgg, nonEmptyAgg
10 , listAggWithOrder, nonEmptyAggWithOrder
11 , listAggWithOrder, nonEmptyAggWithOrder
11 )
12 )
@@ -20,6 +21,7 @@ import Rel8.Aggregate ( Aggregate, Col( A ) )
20import Rel8.Expr ( Expr, Col( E ) )
21import Rel8.Expr ( Expr, Col( E ) )
21import Rel8.Expr.Aggregate
22import Rel8.Expr.Aggregate
22 ( groupByExpr
23 ( groupByExpr
24+ , headAggExpr
23 , listAggExpr
25 , listAggExpr
24 , listAggExprWithOrder
26 , listAggExprWithOrder
25 , nonEmptyAggExpr
27 , nonEmptyAggExpr
@@ -27,7 +29,7 @@ import Rel8.Expr.Aggregate
27 )
29 )
28import Rel8.Order ( Order )
30import Rel8.Order ( Order )
29import Rel8.Schema.Dict ( Dict( Dict ) )
31import Rel8.Schema.Dict ( Dict( Dict ) )
30−import Rel8.Schema.HTable ( htabulate, hfield )
32+import Rel8.Schema.HTable ( hfield, hmap, htabulate )
31import Rel8.Schema.HTable.Vectorize ( hvectorize )
33import Rel8.Schema.HTable.Vectorize ( hvectorize )
32import Rel8.Table ( Table, toColumns, fromColumns )
34import Rel8.Table ( Table, toColumns, fromColumns )
33import Rel8.Table.Eq ( EqTable, eqTable )
35import Rel8.Table.Eq ( EqTable, eqTable )
@@ -44,6 +46,11 @@ groupBy (toColumns -> exprs) = fromColumns $ htabulate $ \field ->
44 E expr -> A $ groupByExpr expr
46 E expr -> A $ groupByExpr expr
45
47
46
48
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+
47-- | Aggregate rows into a single row containing an array of all aggregated
54-- | Aggregate rows into a single row containing an array of all aggregated
48-- rows. This can be used to associate multiple rows with a single row, without
55-- rows. This can be used to associate multiple rows with a single row, without
49-- changing the over cardinality of the query. This allows you to essentially
56-- changing the over cardinality of the query. This allows you to essentially