on master help sign in
Add array1DAggExpr
commitb0e605305a77c252f74cfd781eb7aa38114b16ce authorShane O'Brien <shane.obrien@circuithub.com> dateWed 14 Apr 2021, 19:49 (6 years ago) parente52dd75
3 files changed+30−9
src/Rel8.hs+1 src/Rel8/Expr/Aggregate.hs+14−1 src/Rel8/Type/Array1D.hs+15−8
src/Rel8.hs+1
@@ -172,6 +172,7 @@ module Rel8
172172 , Aggregate
173173 , aggregate
174174 , countRows
175+ , array1DAggExpr
175176 , listAgg, listAggExpr
176177 , nonEmptyAgg, nonEmptyAggExpr
177178 , groupBy
src/Rel8/Expr/Aggregate.hs+14−1
@@ -12,7 +12,7 @@ module Rel8.Expr.Aggregate
1212 , sum, sumWhere
1313 , stringAgg
1414 , groupByExpr
15− , listAggExpr, nonEmptyAggExpr
15+ , array1DAggExpr, listAggExpr, nonEmptyAggExpr
1616 )
1717where
1818
@@ -38,6 +38,7 @@ import Rel8.Expr.Null ( null )
3838import Rel8.Expr.Serialize ( litExpr )
3939import Rel8.Schema.Null ( Sql )
4040import Rel8.Type.Array ( fromPrimArray )
41+import Rel8.Type.Array1D ( Array1D, NotArray )
4142import Rel8.Type.Eq ( DBEq )
4243import Rel8.Type.Num ( DBNum )
4344import Rel8.Type.Ord ( DBMax, DBMin )
@@ -152,6 +153,18 @@ groupByExpr :: Sql DBEq a => Expr a -> Aggregate (Expr a)
152153groupByExpr = unsafeMakeAggregate toPrimExpr fromPrimExpr Nothing
153154
154155
156+-- | Collect expressions values as an array.
157+array1DAggExpr :: NotArray a => Expr a -> Aggregate (Expr (Array1D a))
158+array1DAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
159+ Aggregator
160+ { operation = Opaleye.AggrArr
161+ , ordering = []
162+ , distinction = Opaleye.AggrAll
163+ }
164+ where
165+ from = fromPrimExpr . fromPrimArray
166+
167+
155168-- | Collect expressions values as a list.
156169listAggExpr :: Expr a -> Aggregate (Expr [a])
157170listAggExpr = unsafeMakeAggregate toPrimExpr from $ Just
src/Rel8/Type/Array1D.hs+15−8
@@ -1,6 +1,8 @@
11{-# language DataKinds #-}
22{-# language DeriveTraversable #-}
33{-# language DerivingStrategies #-}
4+{-# language FlexibleContexts #-}
5+{-# language FlexibleInstances #-}
46{-# language GeneralizedNewtypeDeriving #-}
57{-# language NamedFieldPuns #-}
68{-# language StandaloneKindSignatures #-}
@@ -11,6 +13,7 @@
1113
1214module Rel8.Type.Array1D
1315 ( Array1D( Array1D )
16+ , NotArray
1417 , getArray1D
1518 )
1619where
@@ -89,7 +92,11 @@ type family IsArray1D a where
8992 IsArray1D _ = 'False
9093
9194
92−array1DTypeInformation :: IsArray1D (Unnullify a) ~ 'False
95+class IsArray1D a ~ 'False => NotArray a
96+instance IsArray1D a ~ 'False => NotArray a
97+
98+
99+array1DTypeInformation :: Sql NotArray a
93100 => Nullity a
94101 -> TypeInformation (Unnullify a)
95102 -> TypeInformation (Array1D a)
@@ -108,25 +115,25 @@ array1DTypeInformation nullity info =
108115 null = Opaleye.ConstExpr Opaleye.NullLit
109116
110117
111−instance (Sql DBType a, IsArray1D (Unnullify a) ~ 'False) => DBType (Array1D a) where
118+instance (Sql DBType a, Sql NotArray a) => DBType (Array1D a) where
112119 typeInformation = array1DTypeInformation nullable typeInformation
113120
114121
115−instance (Sql DBEq a, IsArray1D (Unnullify a) ~ 'False) => DBEq (Array1D a)
122+instance (Sql DBEq a, Sql NotArray a) => DBEq (Array1D a)
116123
117124
118−instance (Sql DBOrd a, IsArray1D (Unnullify a) ~ 'False) => DBOrd (Array1D a)
125+instance (Sql DBOrd a, Sql NotArray a) => DBOrd (Array1D a)
119126
120127
121−instance (Sql DBMax a, IsArray1D (Unnullify a) ~ 'False) => DBMax (Array1D a)
128+instance (Sql DBMax a, Sql NotArray a) => DBMax (Array1D a)
122129
123130
124−instance (Sql DBMin a, IsArray1D (Unnullify a) ~ 'False) => DBMin (Array1D a)
131+instance (Sql DBMin a, Sql NotArray a) => DBMin (Array1D a)
125132
126133
127−instance (Sql DBType a, IsArray1D (Unnullify a) ~ 'False) => DBSemigroup (Array1D a) where
134+instance (Sql DBType a, Sql NotArray a) => DBSemigroup (Array1D a) where
128135 (<>.) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:||))
129136
130137
131−instance (Sql DBType a, IsArray1D (Unnullify a) ~ 'False) => DBMonoid (Array1D a) where
138+instance (Sql DBType a, Sql NotArray a) => DBMonoid (Array1D a) where
132139 memptyExpr = litExpr mempty