module Database.Beam.Query.Aggregate
  ( -- * Aggregates
    -- | See the corresponding
    --   <https://haskell-beam.github.io/beam/user-guide/queries/aggregates.md manual section>
    --   for more detail

    aggregate_
  , filterWhere_, filterWhere_'

  , QGroupable(..)

    -- ** General-purpose aggregate functions #gp-agg-funcs#
  , sum_, avg_, min_, max_, count_, countAll_
  , rank_, cumeDist_, percentRank_, denseRank_
  , rowNumber_

  , every_, any_, some_

    -- ** Quantified aggregate functions
    -- | These functions correspond one-to-one with the <#gp-agg-funcs
    --   general-purpose aggregate functions>. However, they each take a
    --   mandatory "set quantifier", which is any of the
    --   <#set-quantifiers set quantifier> values.
  , sumOver_, avgOver_, minOver_, maxOver_, countOver_
  , everyOver_, anyOver_, someOver_

    -- *** Set quantifiers #set-quantifiers#
  , distinctInGroup_, allInGroup_, allInGroupExplicitly_
  ) where

import Database.Beam.Query.Internal
import Database.Beam.Query.Operator
import Database.Beam.Query.Ord

import Database.Beam.Backend.SQL
import Database.Beam.Schema.Tables

import Control.Applicative
import Control.Monad.Writer
import Control.Monad.Free

import Data.Typeable

type Aggregable be a =
  ProjectibleWithPredicate AggregateContext be (WithExprContext (BeamSqlBackendExpressionSyntax' be)) a

-- | Compute an aggregate over a query.
--
--   The supplied aggregate projection should return an aggregate expression (an
--   expression containing an aggregate function such as 'count_', 'sum_',
--   'countAll_', etc), a grouping key (specified with the 'group_' function),
--   or a combination of tuples of the above.
--
--   Appropriate instances are provided up to 8-tuples.
--
--   Semantically, all grouping expressions in the projection will be added to a
--   SQL @GROUP BY@ clause and all aggregate expressions will be computed.
--
--   The return value will be the type of the aggregate projection, but
--   transformed to be in the normal value context (i.e., everything will become
--   'QExpr's).
--
--   For usage examples, see
--   <https://haskell-beam.github.io/beam/user-guide/queries/aggregates/ the manual>.
aggregate_ :: forall be a r db s.
              ( BeamSqlBackend be
              , Aggregable be a, Projectible be r, Projectible be a

              , ContextRewritable a
              , ThreadRewritable (QNested s) (WithRewrittenContext a QValueContext)
              )
           => (r -> a)                  -- ^ Aggregate projection
           -> Q be db (QNested s) r -- ^ Query to aggregate over
           -> Q be db s (WithRewrittenThread (QNested s) s (WithRewrittenContext a QValueContext))
aggregate_ :: forall be a r (db :: (* -> *) -> *) s.
(BeamSqlBackend be, Aggregable be a, Projectible be r,
 Projectible be a, ContextRewritable a,
 ThreadRewritable
   (QNested s) (WithRewrittenContext a QValueContext)) =>
(r -> a)
-> Q be db (QNested s) r
-> Q be
     db
     s
     (WithRewrittenThread
        (QNested s) s (WithRewrittenContext a QValueContext))
aggregate_ r -> a
mkAggregation (Q QM be db (QNested s) r
aggregating) =
  forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q (forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be grouping a (db :: (* -> *) -> *) s next.
(Projectible be grouping, Projectible be a) =>
(a
 -> TablePrefix
 -> (Maybe (BeamSqlBackendGroupingSyntax be), grouping))
-> QM be db (QNested s) a -> (grouping -> next) -> QF be db s next
QAggregate r
-> TablePrefix
-> (Maybe
      (Sql92SelectTableGroupingSyntax
         (Sql92SelectSelectTableSyntax
            (Sql92SelectSyntax (BeamSqlBackendSyntax be)))),
    a)
mkAggregation' QM be db (QNested s) r
aggregating (forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a ctxt.
ContextRewritable a =>
Proxy ctxt -> a -> WithRewrittenContext a ctxt
rewriteContext (forall {k} (t :: k). Proxy t
Proxy @QValueContext))))
  where
    mkAggregation' :: r
-> TablePrefix
-> (Maybe
      (Sql92SelectTableGroupingSyntax
         (Sql92SelectSelectTableSyntax
            (Sql92SelectSyntax (BeamSqlBackendSyntax be)))),
    a)
mkAggregation' r
x TablePrefix
tblPfx =
      let agg :: a
agg = r -> a
mkAggregation r
x
          doProject :: AggregateContext c
                    => Proxy c -> Proxy be
                    -> WithExprContext (BeamSqlBackendExpressionSyntax' be)
                    -> Writer [WithExprContext (BeamSqlBackendExpressionSyntax' be)]
                              (WithExprContext (BeamSqlBackendExpressionSyntax' be))
          doProject :: forall c.
AggregateContext c =>
Proxy c
-> Proxy be
-> (TablePrefix -> BeamSqlBackendExpressionSyntax' be)
-> Writer
     [TablePrefix -> BeamSqlBackendExpressionSyntax' be]
     (TablePrefix -> BeamSqlBackendExpressionSyntax' be)
doProject Proxy c
p Proxy be
_ TablePrefix -> BeamSqlBackendExpressionSyntax' be
expr =
            case forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast Proxy c
p of
              Just (Proxy QGroupingContext
Proxy :: Proxy QGroupingContext) ->
                forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [ TablePrefix -> BeamSqlBackendExpressionSyntax' be
expr ] forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Applicative f => a -> f a
pure TablePrefix -> BeamSqlBackendExpressionSyntax' be
expr
              Maybe (Proxy QGroupingContext)
Nothing ->
                case forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast Proxy c
p of
                  Just (Proxy QAggregateContext
Proxy :: Proxy QAggregateContext) ->
                    forall (f :: * -> *) a. Applicative f => a -> f a
pure TablePrefix -> BeamSqlBackendExpressionSyntax' be
expr
                  Maybe (Proxy QAggregateContext)
Nothing -> forall a. HasCallStack => [Char] -> a
error [Char]
"aggregate_: impossible"

          groupingExprs :: [TablePrefix
 -> Sql92SelectTableExpressionSyntax
      (Sql92SelectSelectTableSyntax
         (Sql92SelectSyntax (BeamSqlBackendSyntax be)))]
groupingExprs =
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall be.
BeamSqlBackendExpressionSyntax' be
-> BeamSqlBackendExpressionSyntax be
fromBeamSqlBackendExpressionSyntax) forall a b. (a -> b) -> a -> b
$
            forall w a. Writer w a -> w
execWriter (forall (contextPredicate :: * -> Constraint) be res a
       (m :: * -> *).
(ProjectibleWithPredicate contextPredicate be res a, Monad m) =>
Proxy contextPredicate
-> Proxy (be, res)
-> (forall context.
    contextPredicate context =>
    Proxy context -> Proxy be -> res -> m res)
-> a
-> m a
project' (forall {k} (t :: k). Proxy t
Proxy @AggregateContext) (forall {k} (t :: k). Proxy t
Proxy @(be, WithExprContext (BeamSqlBackendExpressionSyntax' be))) forall c.
AggregateContext c =>
Proxy c
-> Proxy be
-> (TablePrefix -> BeamSqlBackendExpressionSyntax' be)
-> Writer
     [TablePrefix -> BeamSqlBackendExpressionSyntax' be]
     (TablePrefix -> BeamSqlBackendExpressionSyntax' be)
doProject a
agg)
      in case [TablePrefix
 -> Sql92SelectTableExpressionSyntax
      (Sql92SelectSelectTableSyntax
         (Sql92SelectSyntax (BeamSqlBackendSyntax be)))]
groupingExprs of
           [] -> (forall a. Maybe a
Nothing, a
agg)
           [TablePrefix
 -> Sql92SelectTableExpressionSyntax
      (Sql92SelectSelectTableSyntax
         (Sql92SelectSyntax (BeamSqlBackendSyntax be)))]
_ -> (forall a. a -> Maybe a
Just (forall grouping.
IsSql92GroupingSyntax grouping =>
[Sql92GroupingExpressionSyntax grouping] -> grouping
groupByExpressions (forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA [TablePrefix
 -> Sql92SelectTableExpressionSyntax
      (Sql92SelectSelectTableSyntax
         (Sql92SelectSyntax (BeamSqlBackendSyntax be)))]
groupingExprs TablePrefix
tblPfx)), a
agg)

-- | Type class for grouping keys. 'expr' is the type of the grouping key after
--   projection. 'grouped' is the type of the grouping key in the aggregate
--   expression (usually something that contains 'QGenExpr's in the
--   'QGroupingContext').
class QGroupable expr grouped | expr -> grouped, grouped -> expr where
  group_ :: expr -> grouped

-- | 'group_' for simple value expressions.
instance QGroupable (QExpr be s a) (QGroupExpr be s a) where
  group_ :: QExpr be s a -> QGroupExpr be s a
group_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax be
a) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax be
a

-- | 'group_' for any 'Beamable' type. Adds every field in the type to the
--   grouping key. This is the equivalent of including the grouping expression
--   of each field in the type as part of the aggregate projection
instance Beamable tbl =>
  QGroupable (tbl (QExpr be s)) (tbl (QGroupExpr be s)) where
  group_ :: tbl (QExpr be s) -> tbl (QGroupExpr be s)
group_ = forall (table :: (* -> *) -> *) (f :: * -> *) (g :: * -> *).
Beamable table =>
(forall a. Columnar' f a -> Columnar' g a) -> table f -> table g
changeBeamRep (\(Columnar' (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax be
x)) -> forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax be
x))

-- | 'group_' for any 'Beamable' type. Adds every field in the type to the
--   grouping key. This is the equivalent of including the grouping expression
--   of each field in the type as part of the aggregate projection
instance Beamable tbl =>
  QGroupable (tbl (Nullable (QExpr be s))) (tbl (Nullable (QGroupExpr be s))) where
  group_ :: tbl (Nullable (QExpr be s)) -> tbl (Nullable (QGroupExpr be s))
group_ = forall (table :: (* -> *) -> *) (f :: * -> *) (g :: * -> *).
Beamable table =>
(forall a. Columnar' f a -> Columnar' g a) -> table f -> table g
changeBeamRep (\(Columnar' (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax be
x)) -> forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax be
x))

-- | Compute an aggregate over all values in a group. Corresponds semantically
--   to the @AGG(ALL ..)@ syntax, but doesn't produce an explicit @ALL@. To
--   produce @ALL@ expicitly, see 'allInGroupExplicitly_'.
allInGroup_ :: IsSql92AggregationSetQuantifierSyntax s
            => Maybe s
allInGroup_ :: forall s. IsSql92AggregationSetQuantifierSyntax s => Maybe s
allInGroup_ = forall a. Maybe a
Nothing

-- | Compute an aggregate only over distinct values in a group. Corresponds to
--   the @AGG(DISTINCT ..)@ syntax.
distinctInGroup_ :: IsSql92AggregationSetQuantifierSyntax s
                 => Maybe s
distinctInGroup_ :: forall s. IsSql92AggregationSetQuantifierSyntax s => Maybe s
distinctInGroup_ = forall a. a -> Maybe a
Just forall q. IsSql92AggregationSetQuantifierSyntax q => q
setQuantifierDistinct

-- | Compute an aggregate over all values in a group. Corresponds to the
--   @AGG(ALL ..)@ syntax. Note that @ALL@ is the default for most aggregations,
--   so you don't normally explicitly specify @ALL@. However, if you need to,
--   you can use this function. To be explicit about quantification in the beam
--   query DSL, but not produce an explicit @ALL@, use 'allInGroup_'.
--   'allInGroup_' has the same semantic meaning, but does not produce an
--   explicit @ALL@.
allInGroupExplicitly_ :: IsSql92AggregationSetQuantifierSyntax s
                      => Maybe s
allInGroupExplicitly_ :: forall s. IsSql92AggregationSetQuantifierSyntax s => Maybe s
allInGroupExplicitly_ = forall a. a -> Maybe a
Just forall q. IsSql92AggregationSetQuantifierSyntax q => q
setQuantifierAll

-- ** Aggregations
--
--    These functions all return 'Maybe' (except for `count_` and
--    `countAll_`) because empty aggregates return SQL @NULL@ values.

-- | SQL @MIN(ALL ..)@ function (but without the explicit ALL)
min_ :: BeamSqlBackend be
     => QExpr be s a -> QAgg be s (Maybe a)
min_ :: forall be s a.
BeamSqlBackend be =>
QExpr be s a -> QAgg be s (Maybe a)
min_ = forall be s a.
BeamSqlBackend be =>
Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
-> QExpr be s a -> QAgg be s (Maybe a)
minOver_ forall s. IsSql92AggregationSetQuantifierSyntax s => Maybe s
allInGroup_

-- | SQL @MAX(ALL ..)@ function (but without the explicit ALL)
max_ :: BeamSqlBackend be
     => QExpr be s a -> QAgg be s (Maybe a)
max_ :: forall be s a.
BeamSqlBackend be =>
QExpr be s a -> QAgg be s (Maybe a)
max_ = forall be s a.
BeamSqlBackend be =>
Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
-> QExpr be s a -> QAgg be s (Maybe a)
maxOver_ forall s. IsSql92AggregationSetQuantifierSyntax s => Maybe s
allInGroup_

-- | SQL @AVG(ALL ..)@ function (but without the explicit ALL)
avg_ :: ( BeamSqlBackend be, Num a )
     => QExpr be s a -> QAgg be s (Maybe a)
avg_ :: forall be a s.
(BeamSqlBackend be, Num a) =>
QExpr be s a -> QAgg be s (Maybe a)
avg_ = forall be a s.
(BeamSqlBackend be, Num a) =>
Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
-> QExpr be s a -> QAgg be s (Maybe a)
avgOver_ forall s. IsSql92AggregationSetQuantifierSyntax s => Maybe s
allInGroup_

-- | SQL @SUM(ALL ..)@ function (but without the explicit ALL)
sum_ :: ( BeamSqlBackend be, Num a )
     => QExpr be s a -> QAgg be s (Maybe a)
sum_ :: forall be a s.
(BeamSqlBackend be, Num a) =>
QExpr be s a -> QAgg be s (Maybe a)
sum_ = forall be a s.
(BeamSqlBackend be, Num a) =>
Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
-> QExpr be s a -> QAgg be s (Maybe a)
sumOver_ forall s. IsSql92AggregationSetQuantifierSyntax s => Maybe s
allInGroup_

-- | SQL @COUNT(*)@ function
countAll_ :: ( BeamSqlBackend be, Integral a ) => QAgg be s a
countAll_ :: forall be a s. (BeamSqlBackend be, Integral a) => QAgg be s a
countAll_ = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall expr. IsSql92AggregationExpressionSyntax expr => expr
countAllE)

-- | SQL @COUNT(ALL ..)@ function (but without the explicit ALL)
count_ :: ( BeamSqlBackend be, Integral b )
       => QExpr be s a -> QAgg be s b
count_ :: forall be b s a.
(BeamSqlBackend be, Integral b) =>
QExpr be s a -> QAgg be s b
count_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
over) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr.
IsSql92AggregationExpressionSyntax expr =>
Maybe (Sql92AggregationSetQuantifierSyntax expr) -> expr -> expr
countE forall a. Maybe a
Nothing forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
over)

-- | SQL2003 @CUME_DIST@ function (Requires T612 Advanced OLAP operations support)
cumeDist_ :: BeamSqlT612Backend be
          => QAgg be s Double
cumeDist_ :: forall be s. BeamSqlT612Backend be => QAgg be s Double
cumeDist_ = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall expr.
IsSql2003ExpressionAdvancedOLAPOperationsSyntax expr =>
expr
cumeDistAggE)

-- | SQL2003 @PERCENT_RANK@ function (Requires T612 Advanced OLAP operations support)
percentRank_ :: BeamSqlT612Backend be
             => QAgg be s Double
percentRank_ :: forall be s. BeamSqlT612Backend be => QAgg be s Double
percentRank_ = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall expr.
IsSql2003ExpressionAdvancedOLAPOperationsSyntax expr =>
expr
percentRankAggE)

-- | SQL2003 @DENSE_RANK@ function (Requires T612 Advanced OLAP operations support)
denseRank_ :: ( BeamSqlT612Backend be, Integral a )
           => QAgg be s a
denseRank_ :: forall be a s. (BeamSqlT612Backend be, Integral a) => QAgg be s a
denseRank_ = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall expr.
IsSql2003ExpressionAdvancedOLAPOperationsSyntax expr =>
expr
denseRankAggE)

-- | SQL2003 @ROW_NUMBER@ function
rowNumber_ :: ( BeamSql2003ExpressionBackend be, Integral a )
           =>  QAgg be s a
rowNumber_ :: forall be a s.
(BeamSql2003ExpressionBackend be, Integral a) =>
QAgg be s a
rowNumber_ = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall expr. IsSql2003ExpressionSyntax expr => expr
rowNumberE)

-- | SQL2003 @RANK@ function (Requires T611 Elementary OLAP operations support)
rank_ :: ( BeamSqlT611Backend be, Integral a )
      => QAgg be s a
rank_ :: forall be a s. (BeamSqlT611Backend be, Integral a) => QAgg be s a
rank_ = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall expr.
IsSql2003ExpressionElementaryOLAPOperationsSyntax expr =>
expr
rankAggE)

minOver_, maxOver_
  :: BeamSqlBackend be
  => Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
  -> QExpr be s a -> QAgg be s (Maybe a)
minOver_ :: forall be s a.
BeamSqlBackend be =>
Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
-> QExpr be s a -> QAgg be s (Maybe a)
minOver_ Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
a) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr.
IsSql92AggregationExpressionSyntax expr =>
Maybe (Sql92AggregationSetQuantifierSyntax expr) -> expr -> expr
minE Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
a)
maxOver_ :: forall be s a.
BeamSqlBackend be =>
Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
-> QExpr be s a -> QAgg be s (Maybe a)
maxOver_ Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
a) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr.
IsSql92AggregationExpressionSyntax expr =>
Maybe (Sql92AggregationSetQuantifierSyntax expr) -> expr -> expr
maxE Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
a)

avgOver_, sumOver_
  :: ( BeamSqlBackend be, Num a )
  => Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
  -> QExpr be s a -> QAgg be s (Maybe a)
avgOver_ :: forall be a s.
(BeamSqlBackend be, Num a) =>
Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
-> QExpr be s a -> QAgg be s (Maybe a)
avgOver_ Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
a) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr.
IsSql92AggregationExpressionSyntax expr =>
Maybe (Sql92AggregationSetQuantifierSyntax expr) -> expr -> expr
avgE Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
a)
sumOver_ :: forall be a s.
(BeamSqlBackend be, Num a) =>
Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
-> QExpr be s a -> QAgg be s (Maybe a)
sumOver_ Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
a) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr.
IsSql92AggregationExpressionSyntax expr =>
Maybe (Sql92AggregationSetQuantifierSyntax expr) -> expr -> expr
sumE Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
a)

countOver_
  :: ( BeamSqlBackend be, Integral b )
  => Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
  -> QExpr be s a -> QAgg be s b
countOver_ :: forall be b s a.
(BeamSqlBackend be, Integral b) =>
Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
-> QExpr be s a -> QAgg be s b
countOver_ Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
a) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr.
IsSql92AggregationExpressionSyntax expr =>
Maybe (Sql92AggregationSetQuantifierSyntax expr) -> expr -> expr
countE Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
a)

-- | SQL @EVERY@, @SOME@, and @ANY@ aggregates. Operates over
-- 'SqlBool' only, as the result can be @NULL@, even if all inputs are
-- known (no input rows).
everyOver_, someOver_, anyOver_
  :: BeamSql99AggregationBackend be
  => Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
  -> QExpr be s SqlBool -> QAgg be s SqlBool
everyOver_ :: forall be s.
BeamSql99AggregationBackend be =>
Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
-> QExpr be s SqlBool -> QAgg be s SqlBool
everyOver_ Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax be
a) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr.
IsSql99AggregationExpressionSyntax expr =>
Maybe (Sql92AggregationSetQuantifierSyntax expr) -> expr -> expr
everyE Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax be
a)
someOver_ :: forall be s.
BeamSql99AggregationBackend be =>
Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
-> QExpr be s SqlBool -> QAgg be s SqlBool
someOver_  Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax be
a) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr.
IsSql99AggregationExpressionSyntax expr =>
Maybe (Sql92AggregationSetQuantifierSyntax expr) -> expr -> expr
someE  Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax be
a)
anyOver_ :: forall be s.
BeamSql99AggregationBackend be =>
Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
-> QExpr be s SqlBool -> QAgg be s SqlBool
anyOver_   Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax be
a) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr.
IsSql99AggregationExpressionSyntax expr =>
Maybe (Sql92AggregationSetQuantifierSyntax expr) -> expr -> expr
anyE   Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
q forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax be
a)

-- | Support for FILTER (WHERE ...) syntax for aggregates.
--   Part of SQL2003 Elementary OLAP operations feature (T611).
--
-- See 'filterWhere_'' for a version that accepts 'SqlBool'.
filterWhere_ :: BeamSqlT611Backend be
             => QAgg be s a -> QExpr be s Bool -> QAgg be s a
filterWhere_ :: forall be s a.
BeamSqlT611Backend be =>
QAgg be s a -> QExpr be s Bool -> QAgg be s a
filterWhere_ QAgg be s a
agg QExpr be s Bool
cond = forall be s a.
BeamSqlT611Backend be =>
QAgg be s a -> QExpr be s SqlBool -> QAgg be s a
filterWhere_' QAgg be s a
agg (forall context syntax s.
QGenExpr context syntax s Bool -> QGenExpr context syntax s SqlBool
sqlBool_ QExpr be s Bool
cond)

-- | Like 'filterWhere_' but accepting 'SqlBool'.
filterWhere_' :: BeamSqlT611Backend be
              => QAgg be s a -> QExpr be s SqlBool -> QAgg be s a
filterWhere_' :: forall be s a.
BeamSqlT611Backend be =>
QAgg be s a -> QExpr be s SqlBool -> QAgg be s a
filterWhere_' (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax be
agg) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax be
cond) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall expr.
IsSql2003ExpressionElementaryOLAPOperationsSyntax expr =>
expr -> expr -> expr
filterAggE TablePrefix -> BeamSqlBackendExpressionSyntax be
agg TablePrefix -> BeamSqlBackendExpressionSyntax be
cond)

-- | SQL99 @EVERY(ALL ..)@ function (but without the explicit ALL)
every_ :: BeamSql99AggregationBackend be
       => QExpr be s SqlBool -> QAgg be s SqlBool
every_ :: forall be s.
BeamSql99AggregationBackend be =>
QExpr be s SqlBool -> QAgg be s SqlBool
every_ = forall be s.
BeamSql99AggregationBackend be =>
Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
-> QExpr be s SqlBool -> QAgg be s SqlBool
everyOver_ forall s. IsSql92AggregationSetQuantifierSyntax s => Maybe s
allInGroup_

-- | SQL99 @SOME(ALL ..)@ function (but without the explicit ALL)
some_ :: BeamSql99AggregationBackend be
      => QExpr be s SqlBool -> QAgg be s SqlBool
some_ :: forall be s.
BeamSql99AggregationBackend be =>
QExpr be s SqlBool -> QAgg be s SqlBool
some_  = forall be s.
BeamSql99AggregationBackend be =>
Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
-> QExpr be s SqlBool -> QAgg be s SqlBool
someOver_  forall s. IsSql92AggregationSetQuantifierSyntax s => Maybe s
allInGroup_

-- | SQL99 @ANY(ALL ..)@ function (but without the explicit ALL)
any_ :: BeamSql99AggregationBackend be
     => QExpr be s SqlBool -> QAgg be s SqlBool
any_ :: forall be s.
BeamSql99AggregationBackend be =>
QExpr be s SqlBool -> QAgg be s SqlBool
any_   = forall be s.
BeamSql99AggregationBackend be =>
Maybe (BeamSqlBackendAggregationQuantifierSyntax be)
-> QExpr be s SqlBool -> QAgg be s SqlBool
anyOver_   forall s. IsSql92AggregationSetQuantifierSyntax s => Maybe s
allInGroup_