Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- newtype Aggregator a b = Aggregator (PackMap Aggregate PrimExpr a b)
- makeAggr' :: Maybe AggrOp -> Aggregator (Field_ n a) (Field_ n' b)
- makeAggr :: AggrOp -> Aggregator (Field_ n a) (Field_ n' b)
- makeAggrExplicit :: Unpackspec a a' -> AggrOp -> Aggregator a (Field_ n b)
- orderAggregate :: Order a -> Aggregator a b -> Aggregator a b
- runAggregator :: Applicative f => Aggregator a b -> (Aggregate -> f PrimExpr) -> a -> f b
- aggregatorApply :: Aggregator (Aggregator a b, a) b
- aggregateU :: Aggregator a b -> (a, Tag) -> (b, PrimQuery -> PrimQuery)
- extractAggregateFields :: Tag -> Aggregate -> PM (Bindings Aggregate) PrimExpr
- unsafeMax :: Aggregator (Field a) (Field a)
- unsafeMin :: Aggregator (Field a) (Field a)
- unsafeAvg :: Aggregator (Field a) (Field a)
- unsafeSum :: Aggregator (Field a) (Field a)
- filterWhereInternal :: (FieldNullable SqlBool -> b -> mb) -> (a -> Field SqlBool) -> Aggregator a b -> Aggregator a mb
- withinGroup :: Order a -> Aggregator a b -> Aggregator a b
Documentation
newtype Aggregator a b Source #
An Aggregator
takes a collection of rows of type a
, groups
them, and transforms each group into a single row of type b
. This
corresponds to aggregators using GROUP BY
in SQL.
You should combine basic Aggregator
s into Aggregator
s on compound
types by using the operations in Data.Profunctor.Product.
An Aggregator
corresponds closely to a Fold
from the
foldl
package. Whereas an Aggregator
a
b
takes each group of
type a
to a single row of type b
, a Fold
a
b
takes a list of a
and returns a single value of type b
.
Aggregator (PackMap Aggregate PrimExpr a b) |
Instances
makeAggrExplicit :: Unpackspec a a' -> AggrOp -> Aggregator a (Field_ n b) Source #
orderAggregate :: Order a -> Aggregator a b -> Aggregator a b Source #
Order the values within each aggregation in Aggregator
using
the given ordering. This is only relevant for aggregations that
depend on the order they get their elements, like
arrayAgg
and stringAgg
.
You can either apply it to an aggregation of multiple columns, in which case it will apply to all aggregation functions in there
Example:
x :: Aggregator (Column a, Column b) (Column (PGArray a), Column (PGArray b)) x = orderAggregate (asc snd) $ p2 (arrayAgg, arrayAgg)
This will generate:
SELECT array_agg(a ORDER BY b ASC), array_agg(b ORDER BY b ASC) FROM (SELECT a, b FROM ...)
Or you can apply it to a single column, and then compose the aggregations afterwards.
Example:
x :: Aggregator (Column a, Column b) (Column (PGArray a), Column (PGArray a)) x = (,) <$> orderAggregate (asc snd) (lmap fst arrayAgg) <*> orderAggregate (desc snd) (lmap fst arrayAgg)
This will generate:
SELECT array_agg(a ORDER BY b ASC), array_agg(a ORDER BY b DESC) FROM (SELECT a, b FROM ...)
runAggregator :: Applicative f => Aggregator a b -> (Aggregate -> f PrimExpr) -> a -> f b Source #
aggregatorApply :: Aggregator (Aggregator a b, a) b Source #
aggregateU :: Aggregator a b -> (a, Tag) -> (b, PrimQuery -> PrimQuery) Source #
filterWhereInternal :: (FieldNullable SqlBool -> b -> mb) -> (a -> Field SqlBool) -> Aggregator a b -> Aggregator a mb Source #
Aggregate only rows matching the given predicate
withinGroup :: Order a -> Aggregator a b -> Aggregator a b Source #