{-# LANGUAGE UndecidableInstances #-}

-- | This module contains the default types and function that model aggregation
-- predicates.
module Hasura.RQL.IR.BoolExp.AggregationPredicates
  ( AggregationPredicatesImplementation (..),
    AggregationPredicate (..),
    AggregationPredicateArguments (..),
  )
where

import Data.Aeson
import Data.Aeson.Extended (ToJSONKeyValue (..))
import Data.Aeson.Key (fromText)
import Hasura.Incremental (Cacheable)
import Hasura.Prelude
import Hasura.RQL.IR.BoolExp (AnnBoolExp, OpExpG)
import Hasura.RQL.Types.Backend (Backend (Column))
import Hasura.RQL.Types.Backend qualified as B
import Hasura.RQL.Types.Relationships.Local (RelInfo)
import Hasura.SQL.Backend (BackendType)

-- | This type the default non-empty implementation of the
-- 'AggregationPredicates' type family of 'class Backend'.
--
-- This represents an _applied_ aggregation predicate, i.e. _not_ an aggegation
-- function in isolation.
--
-- In the default schema implementation, this type results from parsing graphql such as:
--
--    table(_where(
--             <relation>_aggregate:
--             {<functionname>: {
--                arguments: <arguments>,
--                predicate: <predicate>,
--                distinct: bool
--                }
--             }
--         ))
--      { ... }
--
-- Note that we make no attemt at modelling window functions or so-called
-- 'analytical' functions such as 'percentile_cont'.
data AggregationPredicatesImplementation (b :: BackendType) field = AggregationPredicatesImplementation
  { AggregationPredicatesImplementation b field -> RelInfo b
aggRelation :: RelInfo b,
    AggregationPredicatesImplementation b field
-> [AggregationPredicate b field]
aggPredicates :: [AggregationPredicate b field]
  }
  deriving stock (AggregationPredicatesImplementation b a -> Bool
(a -> m) -> AggregationPredicatesImplementation b a -> m
(a -> b -> b) -> b -> AggregationPredicatesImplementation b a -> b
(forall m.
 Monoid m =>
 AggregationPredicatesImplementation b m -> m)
-> (forall m a.
    Monoid m =>
    (a -> m) -> AggregationPredicatesImplementation b a -> m)
-> (forall m a.
    Monoid m =>
    (a -> m) -> AggregationPredicatesImplementation b a -> m)
-> (forall a b.
    (a -> b -> b) -> b -> AggregationPredicatesImplementation b a -> b)
-> (forall a b.
    (a -> b -> b) -> b -> AggregationPredicatesImplementation b a -> b)
-> (forall b a.
    (b -> a -> b) -> b -> AggregationPredicatesImplementation b a -> b)
-> (forall b a.
    (b -> a -> b) -> b -> AggregationPredicatesImplementation b a -> b)
-> (forall a.
    (a -> a -> a) -> AggregationPredicatesImplementation b a -> a)
-> (forall a.
    (a -> a -> a) -> AggregationPredicatesImplementation b a -> a)
-> (forall a. AggregationPredicatesImplementation b a -> [a])
-> (forall a. AggregationPredicatesImplementation b a -> Bool)
-> (forall a. AggregationPredicatesImplementation b a -> Int)
-> (forall a.
    Eq a =>
    a -> AggregationPredicatesImplementation b a -> Bool)
-> (forall a.
    Ord a =>
    AggregationPredicatesImplementation b a -> a)
-> (forall a.
    Ord a =>
    AggregationPredicatesImplementation b a -> a)
-> (forall a.
    Num a =>
    AggregationPredicatesImplementation b a -> a)
-> (forall a.
    Num a =>
    AggregationPredicatesImplementation b a -> a)
-> Foldable (AggregationPredicatesImplementation b)
forall a.
Eq a =>
a -> AggregationPredicatesImplementation b a -> Bool
forall a. Num a => AggregationPredicatesImplementation b a -> a
forall a. Ord a => AggregationPredicatesImplementation b a -> a
forall m. Monoid m => AggregationPredicatesImplementation b m -> m
forall a. AggregationPredicatesImplementation b a -> Bool
forall a. AggregationPredicatesImplementation b a -> Int
forall a. AggregationPredicatesImplementation b a -> [a]
forall a.
(a -> a -> a) -> AggregationPredicatesImplementation b a -> a
forall m a.
Monoid m =>
(a -> m) -> AggregationPredicatesImplementation b a -> m
forall b a.
(b -> a -> b) -> b -> AggregationPredicatesImplementation b a -> b
forall a b.
(a -> b -> b) -> b -> AggregationPredicatesImplementation b a -> b
forall (b :: BackendType) a.
(Backend b, Eq a) =>
a -> AggregationPredicatesImplementation b a -> Bool
forall (b :: BackendType) a.
(Backend b, Num a) =>
AggregationPredicatesImplementation b a -> a
forall (b :: BackendType) a.
(Backend b, Ord a) =>
AggregationPredicatesImplementation b a -> a
forall (b :: BackendType) m.
(Backend b, Monoid m) =>
AggregationPredicatesImplementation b m -> m
forall (b :: BackendType) a.
Backend b =>
AggregationPredicatesImplementation b a -> Bool
forall (b :: BackendType) a.
Backend b =>
AggregationPredicatesImplementation b a -> Int
forall (b :: BackendType) a.
Backend b =>
AggregationPredicatesImplementation b a -> [a]
forall (b :: BackendType) a.
Backend b =>
(a -> a -> a) -> AggregationPredicatesImplementation b a -> a
forall (b :: BackendType) m a.
(Backend b, Monoid m) =>
(a -> m) -> AggregationPredicatesImplementation b a -> m
forall (b :: BackendType) b a.
Backend b =>
(b -> a -> b) -> b -> AggregationPredicatesImplementation b a -> b
forall (b :: BackendType) a b.
Backend b =>
(a -> b -> b) -> b -> AggregationPredicatesImplementation b a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: AggregationPredicatesImplementation b a -> a
$cproduct :: forall (b :: BackendType) a.
(Backend b, Num a) =>
AggregationPredicatesImplementation b a -> a
sum :: AggregationPredicatesImplementation b a -> a
$csum :: forall (b :: BackendType) a.
(Backend b, Num a) =>
AggregationPredicatesImplementation b a -> a
minimum :: AggregationPredicatesImplementation b a -> a
$cminimum :: forall (b :: BackendType) a.
(Backend b, Ord a) =>
AggregationPredicatesImplementation b a -> a
maximum :: AggregationPredicatesImplementation b a -> a
$cmaximum :: forall (b :: BackendType) a.
(Backend b, Ord a) =>
AggregationPredicatesImplementation b a -> a
elem :: a -> AggregationPredicatesImplementation b a -> Bool
$celem :: forall (b :: BackendType) a.
(Backend b, Eq a) =>
a -> AggregationPredicatesImplementation b a -> Bool
length :: AggregationPredicatesImplementation b a -> Int
$clength :: forall (b :: BackendType) a.
Backend b =>
AggregationPredicatesImplementation b a -> Int
null :: AggregationPredicatesImplementation b a -> Bool
$cnull :: forall (b :: BackendType) a.
Backend b =>
AggregationPredicatesImplementation b a -> Bool
toList :: AggregationPredicatesImplementation b a -> [a]
$ctoList :: forall (b :: BackendType) a.
Backend b =>
AggregationPredicatesImplementation b a -> [a]
foldl1 :: (a -> a -> a) -> AggregationPredicatesImplementation b a -> a
$cfoldl1 :: forall (b :: BackendType) a.
Backend b =>
(a -> a -> a) -> AggregationPredicatesImplementation b a -> a
foldr1 :: (a -> a -> a) -> AggregationPredicatesImplementation b a -> a
$cfoldr1 :: forall (b :: BackendType) a.
Backend b =>
(a -> a -> a) -> AggregationPredicatesImplementation b a -> a
foldl' :: (b -> a -> b) -> b -> AggregationPredicatesImplementation b a -> b
$cfoldl' :: forall (b :: BackendType) b a.
Backend b =>
(b -> a -> b) -> b -> AggregationPredicatesImplementation b a -> b
foldl :: (b -> a -> b) -> b -> AggregationPredicatesImplementation b a -> b
$cfoldl :: forall (b :: BackendType) b a.
Backend b =>
(b -> a -> b) -> b -> AggregationPredicatesImplementation b a -> b
foldr' :: (a -> b -> b) -> b -> AggregationPredicatesImplementation b a -> b
$cfoldr' :: forall (b :: BackendType) a b.
Backend b =>
(a -> b -> b) -> b -> AggregationPredicatesImplementation b a -> b
foldr :: (a -> b -> b) -> b -> AggregationPredicatesImplementation b a -> b
$cfoldr :: forall (b :: BackendType) a b.
Backend b =>
(a -> b -> b) -> b -> AggregationPredicatesImplementation b a -> b
foldMap' :: (a -> m) -> AggregationPredicatesImplementation b a -> m
$cfoldMap' :: forall (b :: BackendType) m a.
(Backend b, Monoid m) =>
(a -> m) -> AggregationPredicatesImplementation b a -> m
foldMap :: (a -> m) -> AggregationPredicatesImplementation b a -> m
$cfoldMap :: forall (b :: BackendType) m a.
(Backend b, Monoid m) =>
(a -> m) -> AggregationPredicatesImplementation b a -> m
fold :: AggregationPredicatesImplementation b m -> m
$cfold :: forall (b :: BackendType) m.
(Backend b, Monoid m) =>
AggregationPredicatesImplementation b m -> m
Foldable, Functor (AggregationPredicatesImplementation b)
Foldable (AggregationPredicatesImplementation b)
Functor (AggregationPredicatesImplementation b)
-> Foldable (AggregationPredicatesImplementation b)
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b)
    -> AggregationPredicatesImplementation b a
    -> f (AggregationPredicatesImplementation b b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    AggregationPredicatesImplementation b (f a)
    -> f (AggregationPredicatesImplementation b a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b)
    -> AggregationPredicatesImplementation b a
    -> m (AggregationPredicatesImplementation b b))
-> (forall (m :: * -> *) a.
    Monad m =>
    AggregationPredicatesImplementation b (m a)
    -> m (AggregationPredicatesImplementation b a))
-> Traversable (AggregationPredicatesImplementation b)
(a -> f b)
-> AggregationPredicatesImplementation b a
-> f (AggregationPredicatesImplementation b b)
forall (b :: BackendType).
Backend b =>
Functor (AggregationPredicatesImplementation b)
forall (b :: BackendType).
Backend b =>
Foldable (AggregationPredicatesImplementation b)
forall (b :: BackendType) (m :: * -> *) a.
(Backend b, Monad m) =>
AggregationPredicatesImplementation b (m a)
-> m (AggregationPredicatesImplementation b a)
forall (b :: BackendType) (f :: * -> *) a.
(Backend b, Applicative f) =>
AggregationPredicatesImplementation b (f a)
-> f (AggregationPredicatesImplementation b a)
forall (b :: BackendType) (m :: * -> *) a b.
(Backend b, Monad m) =>
(a -> m b)
-> AggregationPredicatesImplementation b a
-> m (AggregationPredicatesImplementation b b)
forall (b :: BackendType) (f :: * -> *) a b.
(Backend b, Applicative f) =>
(a -> f b)
-> AggregationPredicatesImplementation b a
-> f (AggregationPredicatesImplementation b b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
AggregationPredicatesImplementation b (m a)
-> m (AggregationPredicatesImplementation b a)
forall (f :: * -> *) a.
Applicative f =>
AggregationPredicatesImplementation b (f a)
-> f (AggregationPredicatesImplementation b a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b)
-> AggregationPredicatesImplementation b a
-> m (AggregationPredicatesImplementation b b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b)
-> AggregationPredicatesImplementation b a
-> f (AggregationPredicatesImplementation b b)
sequence :: AggregationPredicatesImplementation b (m a)
-> m (AggregationPredicatesImplementation b a)
$csequence :: forall (b :: BackendType) (m :: * -> *) a.
(Backend b, Monad m) =>
AggregationPredicatesImplementation b (m a)
-> m (AggregationPredicatesImplementation b a)
mapM :: (a -> m b)
-> AggregationPredicatesImplementation b a
-> m (AggregationPredicatesImplementation b b)
$cmapM :: forall (b :: BackendType) (m :: * -> *) a b.
(Backend b, Monad m) =>
(a -> m b)
-> AggregationPredicatesImplementation b a
-> m (AggregationPredicatesImplementation b b)
sequenceA :: AggregationPredicatesImplementation b (f a)
-> f (AggregationPredicatesImplementation b a)
$csequenceA :: forall (b :: BackendType) (f :: * -> *) a.
(Backend b, Applicative f) =>
AggregationPredicatesImplementation b (f a)
-> f (AggregationPredicatesImplementation b a)
traverse :: (a -> f b)
-> AggregationPredicatesImplementation b a
-> f (AggregationPredicatesImplementation b b)
$ctraverse :: forall (b :: BackendType) (f :: * -> *) a b.
(Backend b, Applicative f) =>
(a -> f b)
-> AggregationPredicatesImplementation b a
-> f (AggregationPredicatesImplementation b b)
$cp2Traversable :: forall (b :: BackendType).
Backend b =>
Foldable (AggregationPredicatesImplementation b)
$cp1Traversable :: forall (b :: BackendType).
Backend b =>
Functor (AggregationPredicatesImplementation b)
Traversable, a
-> AggregationPredicatesImplementation b b
-> AggregationPredicatesImplementation b a
(a -> b)
-> AggregationPredicatesImplementation b a
-> AggregationPredicatesImplementation b b
(forall a b.
 (a -> b)
 -> AggregationPredicatesImplementation b a
 -> AggregationPredicatesImplementation b b)
-> (forall a b.
    a
    -> AggregationPredicatesImplementation b b
    -> AggregationPredicatesImplementation b a)
-> Functor (AggregationPredicatesImplementation b)
forall a b.
a
-> AggregationPredicatesImplementation b b
-> AggregationPredicatesImplementation b a
forall a b.
(a -> b)
-> AggregationPredicatesImplementation b a
-> AggregationPredicatesImplementation b b
forall (b :: BackendType) a b.
Backend b =>
a
-> AggregationPredicatesImplementation b b
-> AggregationPredicatesImplementation b a
forall (b :: BackendType) a b.
Backend b =>
(a -> b)
-> AggregationPredicatesImplementation b a
-> AggregationPredicatesImplementation b b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a
-> AggregationPredicatesImplementation b b
-> AggregationPredicatesImplementation b a
$c<$ :: forall (b :: BackendType) a b.
Backend b =>
a
-> AggregationPredicatesImplementation b b
-> AggregationPredicatesImplementation b a
fmap :: (a -> b)
-> AggregationPredicatesImplementation b a
-> AggregationPredicatesImplementation b b
$cfmap :: forall (b :: BackendType) a b.
Backend b =>
(a -> b)
-> AggregationPredicatesImplementation b a
-> AggregationPredicatesImplementation b b
Functor, (forall x.
 AggregationPredicatesImplementation b field
 -> Rep (AggregationPredicatesImplementation b field) x)
-> (forall x.
    Rep (AggregationPredicatesImplementation b field) x
    -> AggregationPredicatesImplementation b field)
-> Generic (AggregationPredicatesImplementation b field)
forall x.
Rep (AggregationPredicatesImplementation b field) x
-> AggregationPredicatesImplementation b field
forall x.
AggregationPredicatesImplementation b field
-> Rep (AggregationPredicatesImplementation b field) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (b :: BackendType) field x.
Rep (AggregationPredicatesImplementation b field) x
-> AggregationPredicatesImplementation b field
forall (b :: BackendType) field x.
AggregationPredicatesImplementation b field
-> Rep (AggregationPredicatesImplementation b field) x
$cto :: forall (b :: BackendType) field x.
Rep (AggregationPredicatesImplementation b field) x
-> AggregationPredicatesImplementation b field
$cfrom :: forall (b :: BackendType) field x.
AggregationPredicatesImplementation b field
-> Rep (AggregationPredicatesImplementation b field) x
Generic)

deriving instance
  ( B.Backend b,
    Eq (AggregationPredicate b field)
  ) =>
  Eq (AggregationPredicatesImplementation b field)

deriving instance
  ( B.Backend b,
    Show (AggregationPredicate b field)
  ) =>
  Show (AggregationPredicatesImplementation b field)

instance
  ( B.Backend b,
    Cacheable (AggregationPredicate b field)
  ) =>
  Cacheable (AggregationPredicatesImplementation b field)

instance
  ( B.Backend b,
    Hashable (AggregationPredicate b field)
  ) =>
  Hashable (AggregationPredicatesImplementation b field)

instance
  ( B.Backend b,
    NFData (AggregationPredicate b field)
  ) =>
  NFData (AggregationPredicatesImplementation b field)

instance
  ( ToJSON (AnnBoolExp b field),
    ToJSON (AggregationPredicate b field),
    ToJSON (OpExpG b field),
    B.Backend b
  ) =>
  ToJSON (AggregationPredicatesImplementation b field)

instance
  ( Backend b,
    ToJSONKeyValue (AggregationPredicate b field)
  ) =>
  ToJSONKeyValue (AggregationPredicatesImplementation b field)
  where
  toJSONKeyValue :: AggregationPredicatesImplementation b field -> (Key, Value)
toJSONKeyValue AggregationPredicatesImplementation {[AggregationPredicate b field]
RelInfo b
aggPredicates :: [AggregationPredicate b field]
aggRelation :: RelInfo b
aggPredicates :: forall (b :: BackendType) field.
AggregationPredicatesImplementation b field
-> [AggregationPredicate b field]
aggRelation :: forall (b :: BackendType) field.
AggregationPredicatesImplementation b field -> RelInfo b
..} =
    ( Text -> Key
fromText Text
"aggregation_predicates",
      [(Text, Value)] -> Value
forall a. ToJSON a => a -> Value
toJSON
        [ (Text
"predicates" :: Text, [(Key, Value)] -> Value
forall a. ToJSON a => a -> Value
toJSON ([(Key, Value)] -> Value) -> [(Key, Value)] -> Value
forall a b. (a -> b) -> a -> b
$ (AggregationPredicate b field -> (Key, Value))
-> [AggregationPredicate b field] -> [(Key, Value)]
forall a b. (a -> b) -> [a] -> [b]
map AggregationPredicate b field -> (Key, Value)
forall a. ToJSONKeyValue a => a -> (Key, Value)
toJSONKeyValue [AggregationPredicate b field]
aggPredicates),
          (Text
"relation", RelInfo b -> Value
forall a. ToJSON a => a -> Value
toJSON RelInfo b
aggRelation)
        ]
    )

data AggregationPredicate (b :: BackendType) field = AggregationPredicate
  { AggregationPredicate b field -> Text
aggPredFunctionName :: Text,
    AggregationPredicate b field -> Bool
aggPredDistinct :: Bool,
    AggregationPredicate b field -> Maybe (AnnBoolExp b field)
aggPredFilter :: Maybe (AnnBoolExp b field),
    AggregationPredicate b field -> AggregationPredicateArguments b
aggPredArguments :: AggregationPredicateArguments b,
    AggregationPredicate b field -> [OpExpG b field]
aggPredPredicate :: [OpExpG b field]
  }
  deriving stock (AggregationPredicate b a -> Bool
(a -> m) -> AggregationPredicate b a -> m
(a -> b -> b) -> b -> AggregationPredicate b a -> b
(forall m. Monoid m => AggregationPredicate b m -> m)
-> (forall m a.
    Monoid m =>
    (a -> m) -> AggregationPredicate b a -> m)
-> (forall m a.
    Monoid m =>
    (a -> m) -> AggregationPredicate b a -> m)
-> (forall a b.
    (a -> b -> b) -> b -> AggregationPredicate b a -> b)
-> (forall a b.
    (a -> b -> b) -> b -> AggregationPredicate b a -> b)
-> (forall b a.
    (b -> a -> b) -> b -> AggregationPredicate b a -> b)
-> (forall b a.
    (b -> a -> b) -> b -> AggregationPredicate b a -> b)
-> (forall a. (a -> a -> a) -> AggregationPredicate b a -> a)
-> (forall a. (a -> a -> a) -> AggregationPredicate b a -> a)
-> (forall a. AggregationPredicate b a -> [a])
-> (forall a. AggregationPredicate b a -> Bool)
-> (forall a. AggregationPredicate b a -> Int)
-> (forall a. Eq a => a -> AggregationPredicate b a -> Bool)
-> (forall a. Ord a => AggregationPredicate b a -> a)
-> (forall a. Ord a => AggregationPredicate b a -> a)
-> (forall a. Num a => AggregationPredicate b a -> a)
-> (forall a. Num a => AggregationPredicate b a -> a)
-> Foldable (AggregationPredicate b)
forall a. Eq a => a -> AggregationPredicate b a -> Bool
forall a. Num a => AggregationPredicate b a -> a
forall a. Ord a => AggregationPredicate b a -> a
forall m. Monoid m => AggregationPredicate b m -> m
forall a. AggregationPredicate b a -> Bool
forall a. AggregationPredicate b a -> Int
forall a. AggregationPredicate b a -> [a]
forall a. (a -> a -> a) -> AggregationPredicate b a -> a
forall m a. Monoid m => (a -> m) -> AggregationPredicate b a -> m
forall b a. (b -> a -> b) -> b -> AggregationPredicate b a -> b
forall a b. (a -> b -> b) -> b -> AggregationPredicate b a -> b
forall (b :: BackendType) a.
(Backend b, Eq a) =>
a -> AggregationPredicate b a -> Bool
forall (b :: BackendType) a.
(Backend b, Num a) =>
AggregationPredicate b a -> a
forall (b :: BackendType) a.
(Backend b, Ord a) =>
AggregationPredicate b a -> a
forall (b :: BackendType) m.
(Backend b, Monoid m) =>
AggregationPredicate b m -> m
forall (b :: BackendType) a.
Backend b =>
AggregationPredicate b a -> Bool
forall (b :: BackendType) a.
Backend b =>
AggregationPredicate b a -> Int
forall (b :: BackendType) a.
Backend b =>
AggregationPredicate b a -> [a]
forall (b :: BackendType) a.
Backend b =>
(a -> a -> a) -> AggregationPredicate b a -> a
forall (b :: BackendType) m a.
(Backend b, Monoid m) =>
(a -> m) -> AggregationPredicate b a -> m
forall (b :: BackendType) b a.
Backend b =>
(b -> a -> b) -> b -> AggregationPredicate b a -> b
forall (b :: BackendType) a b.
Backend b =>
(a -> b -> b) -> b -> AggregationPredicate b a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: AggregationPredicate b a -> a
$cproduct :: forall (b :: BackendType) a.
(Backend b, Num a) =>
AggregationPredicate b a -> a
sum :: AggregationPredicate b a -> a
$csum :: forall (b :: BackendType) a.
(Backend b, Num a) =>
AggregationPredicate b a -> a
minimum :: AggregationPredicate b a -> a
$cminimum :: forall (b :: BackendType) a.
(Backend b, Ord a) =>
AggregationPredicate b a -> a
maximum :: AggregationPredicate b a -> a
$cmaximum :: forall (b :: BackendType) a.
(Backend b, Ord a) =>
AggregationPredicate b a -> a
elem :: a -> AggregationPredicate b a -> Bool
$celem :: forall (b :: BackendType) a.
(Backend b, Eq a) =>
a -> AggregationPredicate b a -> Bool
length :: AggregationPredicate b a -> Int
$clength :: forall (b :: BackendType) a.
Backend b =>
AggregationPredicate b a -> Int
null :: AggregationPredicate b a -> Bool
$cnull :: forall (b :: BackendType) a.
Backend b =>
AggregationPredicate b a -> Bool
toList :: AggregationPredicate b a -> [a]
$ctoList :: forall (b :: BackendType) a.
Backend b =>
AggregationPredicate b a -> [a]
foldl1 :: (a -> a -> a) -> AggregationPredicate b a -> a
$cfoldl1 :: forall (b :: BackendType) a.
Backend b =>
(a -> a -> a) -> AggregationPredicate b a -> a
foldr1 :: (a -> a -> a) -> AggregationPredicate b a -> a
$cfoldr1 :: forall (b :: BackendType) a.
Backend b =>
(a -> a -> a) -> AggregationPredicate b a -> a
foldl' :: (b -> a -> b) -> b -> AggregationPredicate b a -> b
$cfoldl' :: forall (b :: BackendType) b a.
Backend b =>
(b -> a -> b) -> b -> AggregationPredicate b a -> b
foldl :: (b -> a -> b) -> b -> AggregationPredicate b a -> b
$cfoldl :: forall (b :: BackendType) b a.
Backend b =>
(b -> a -> b) -> b -> AggregationPredicate b a -> b
foldr' :: (a -> b -> b) -> b -> AggregationPredicate b a -> b
$cfoldr' :: forall (b :: BackendType) a b.
Backend b =>
(a -> b -> b) -> b -> AggregationPredicate b a -> b
foldr :: (a -> b -> b) -> b -> AggregationPredicate b a -> b
$cfoldr :: forall (b :: BackendType) a b.
Backend b =>
(a -> b -> b) -> b -> AggregationPredicate b a -> b
foldMap' :: (a -> m) -> AggregationPredicate b a -> m
$cfoldMap' :: forall (b :: BackendType) m a.
(Backend b, Monoid m) =>
(a -> m) -> AggregationPredicate b a -> m
foldMap :: (a -> m) -> AggregationPredicate b a -> m
$cfoldMap :: forall (b :: BackendType) m a.
(Backend b, Monoid m) =>
(a -> m) -> AggregationPredicate b a -> m
fold :: AggregationPredicate b m -> m
$cfold :: forall (b :: BackendType) m.
(Backend b, Monoid m) =>
AggregationPredicate b m -> m
Foldable, Functor (AggregationPredicate b)
Foldable (AggregationPredicate b)
Functor (AggregationPredicate b)
-> Foldable (AggregationPredicate b)
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b)
    -> AggregationPredicate b a -> f (AggregationPredicate b b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    AggregationPredicate b (f a) -> f (AggregationPredicate b a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b)
    -> AggregationPredicate b a -> m (AggregationPredicate b b))
-> (forall (m :: * -> *) a.
    Monad m =>
    AggregationPredicate b (m a) -> m (AggregationPredicate b a))
-> Traversable (AggregationPredicate b)
(a -> f b)
-> AggregationPredicate b a -> f (AggregationPredicate b b)
forall (b :: BackendType).
Backend b =>
Functor (AggregationPredicate b)
forall (b :: BackendType).
Backend b =>
Foldable (AggregationPredicate b)
forall (b :: BackendType) (m :: * -> *) a.
(Backend b, Monad m) =>
AggregationPredicate b (m a) -> m (AggregationPredicate b a)
forall (b :: BackendType) (f :: * -> *) a.
(Backend b, Applicative f) =>
AggregationPredicate b (f a) -> f (AggregationPredicate b a)
forall (b :: BackendType) (m :: * -> *) a b.
(Backend b, Monad m) =>
(a -> m b)
-> AggregationPredicate b a -> m (AggregationPredicate b b)
forall (b :: BackendType) (f :: * -> *) a b.
(Backend b, Applicative f) =>
(a -> f b)
-> AggregationPredicate b a -> f (AggregationPredicate b b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
AggregationPredicate b (m a) -> m (AggregationPredicate b a)
forall (f :: * -> *) a.
Applicative f =>
AggregationPredicate b (f a) -> f (AggregationPredicate b a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b)
-> AggregationPredicate b a -> m (AggregationPredicate b b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b)
-> AggregationPredicate b a -> f (AggregationPredicate b b)
sequence :: AggregationPredicate b (m a) -> m (AggregationPredicate b a)
$csequence :: forall (b :: BackendType) (m :: * -> *) a.
(Backend b, Monad m) =>
AggregationPredicate b (m a) -> m (AggregationPredicate b a)
mapM :: (a -> m b)
-> AggregationPredicate b a -> m (AggregationPredicate b b)
$cmapM :: forall (b :: BackendType) (m :: * -> *) a b.
(Backend b, Monad m) =>
(a -> m b)
-> AggregationPredicate b a -> m (AggregationPredicate b b)
sequenceA :: AggregationPredicate b (f a) -> f (AggregationPredicate b a)
$csequenceA :: forall (b :: BackendType) (f :: * -> *) a.
(Backend b, Applicative f) =>
AggregationPredicate b (f a) -> f (AggregationPredicate b a)
traverse :: (a -> f b)
-> AggregationPredicate b a -> f (AggregationPredicate b b)
$ctraverse :: forall (b :: BackendType) (f :: * -> *) a b.
(Backend b, Applicative f) =>
(a -> f b)
-> AggregationPredicate b a -> f (AggregationPredicate b b)
$cp2Traversable :: forall (b :: BackendType).
Backend b =>
Foldable (AggregationPredicate b)
$cp1Traversable :: forall (b :: BackendType).
Backend b =>
Functor (AggregationPredicate b)
Traversable, a -> AggregationPredicate b b -> AggregationPredicate b a
(a -> b) -> AggregationPredicate b a -> AggregationPredicate b b
(forall a b.
 (a -> b) -> AggregationPredicate b a -> AggregationPredicate b b)
-> (forall a b.
    a -> AggregationPredicate b b -> AggregationPredicate b a)
-> Functor (AggregationPredicate b)
forall a b.
a -> AggregationPredicate b b -> AggregationPredicate b a
forall a b.
(a -> b) -> AggregationPredicate b a -> AggregationPredicate b b
forall (b :: BackendType) a b.
Backend b =>
a -> AggregationPredicate b b -> AggregationPredicate b a
forall (b :: BackendType) a b.
Backend b =>
(a -> b) -> AggregationPredicate b a -> AggregationPredicate b b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> AggregationPredicate b b -> AggregationPredicate b a
$c<$ :: forall (b :: BackendType) a b.
Backend b =>
a -> AggregationPredicate b b -> AggregationPredicate b a
fmap :: (a -> b) -> AggregationPredicate b a -> AggregationPredicate b b
$cfmap :: forall (b :: BackendType) a b.
Backend b =>
(a -> b) -> AggregationPredicate b a -> AggregationPredicate b b
Functor, (forall x.
 AggregationPredicate b field
 -> Rep (AggregationPredicate b field) x)
-> (forall x.
    Rep (AggregationPredicate b field) x
    -> AggregationPredicate b field)
-> Generic (AggregationPredicate b field)
forall x.
Rep (AggregationPredicate b field) x
-> AggregationPredicate b field
forall x.
AggregationPredicate b field
-> Rep (AggregationPredicate b field) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (b :: BackendType) field x.
Rep (AggregationPredicate b field) x
-> AggregationPredicate b field
forall (b :: BackendType) field x.
AggregationPredicate b field
-> Rep (AggregationPredicate b field) x
$cto :: forall (b :: BackendType) field x.
Rep (AggregationPredicate b field) x
-> AggregationPredicate b field
$cfrom :: forall (b :: BackendType) field x.
AggregationPredicate b field
-> Rep (AggregationPredicate b field) x
Generic)

deriving instance
  ( B.Backend b,
    Eq (AnnBoolExp b field),
    Eq (OpExpG b field),
    Eq (AggregationPredicateArguments b)
  ) =>
  Eq (AggregationPredicate b field)

deriving instance
  ( B.Backend b,
    Show (AnnBoolExp b field),
    Show (OpExpG b field),
    Show (AggregationPredicateArguments b)
  ) =>
  Show (AggregationPredicate b field)

instance
  ( B.Backend b,
    Hashable (B.BooleanOperators b field),
    Hashable (AnnBoolExp b field),
    Hashable field
  ) =>
  Hashable (AggregationPredicate b field)

instance
  ( B.Backend b,
    Cacheable (AggregationPredicateArguments b),
    Cacheable (AnnBoolExp b field),
    Cacheable (OpExpG b field)
  ) =>
  Cacheable (AggregationPredicate b field)

instance
  ( B.Backend b,
    NFData (AggregationPredicateArguments b),
    NFData (AnnBoolExp b field),
    NFData (OpExpG b field)
  ) =>
  NFData (AggregationPredicate b field)

instance
  ( ToJSON (AggregationPredicateArguments b),
    ToJSON (AnnBoolExp b field),
    ToJSONKeyValue (OpExpG b field)
  ) =>
  ToJSONKeyValue (AggregationPredicate b field)
  where
  toJSONKeyValue :: AggregationPredicate b field -> (Key, Value)
toJSONKeyValue AggregationPredicate {Bool
[OpExpG b field]
Maybe (AnnBoolExp b field)
Text
AggregationPredicateArguments b
aggPredPredicate :: [OpExpG b field]
aggPredArguments :: AggregationPredicateArguments b
aggPredFilter :: Maybe (AnnBoolExp b field)
aggPredDistinct :: Bool
aggPredFunctionName :: Text
aggPredPredicate :: forall (b :: BackendType) field.
AggregationPredicate b field -> [OpExpG b field]
aggPredArguments :: forall (b :: BackendType) field.
AggregationPredicate b field -> AggregationPredicateArguments b
aggPredFilter :: forall (b :: BackendType) field.
AggregationPredicate b field -> Maybe (AnnBoolExp b field)
aggPredDistinct :: forall (b :: BackendType) field.
AggregationPredicate b field -> Bool
aggPredFunctionName :: forall (b :: BackendType) field.
AggregationPredicate b field -> Text
..} =
    ( Text -> Key
fromText Text
aggPredFunctionName,
      [(Text, Value)] -> Value
forall a. ToJSON a => a -> Value
toJSON
        [ (Text
"aggPredDistinct" :: Text, Bool -> Value
forall a. ToJSON a => a -> Value
toJSON Bool
aggPredDistinct),
          (Text
"aggPredFilter", Maybe (AnnBoolExp b field) -> Value
forall a. ToJSON a => a -> Value
toJSON Maybe (AnnBoolExp b field)
aggPredFilter),
          (Text
"aggPredArguments", AggregationPredicateArguments b -> Value
forall a. ToJSON a => a -> Value
toJSON AggregationPredicateArguments b
aggPredArguments),
          (Text
"aggPredPredicate", [(Key, Value)] -> Value
forall a. ToJSON a => a -> Value
toJSON ((OpExpG b field -> (Key, Value))
-> [OpExpG b field] -> [(Key, Value)]
forall a b. (a -> b) -> [a] -> [b]
map OpExpG b field -> (Key, Value)
forall a. ToJSONKeyValue a => a -> (Key, Value)
toJSONKeyValue [OpExpG b field]
aggPredPredicate))
        ]
    )

data AggregationPredicateArguments (b :: BackendType)
  = AggregationPredicateArgumentsStar
  | AggregationPredicateArguments (NonEmpty (Column b))
  deriving stock ((forall x.
 AggregationPredicateArguments b
 -> Rep (AggregationPredicateArguments b) x)
-> (forall x.
    Rep (AggregationPredicateArguments b) x
    -> AggregationPredicateArguments b)
-> Generic (AggregationPredicateArguments b)
forall x.
Rep (AggregationPredicateArguments b) x
-> AggregationPredicateArguments b
forall x.
AggregationPredicateArguments b
-> Rep (AggregationPredicateArguments b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (b :: BackendType) x.
Rep (AggregationPredicateArguments b) x
-> AggregationPredicateArguments b
forall (b :: BackendType) x.
AggregationPredicateArguments b
-> Rep (AggregationPredicateArguments b) x
$cto :: forall (b :: BackendType) x.
Rep (AggregationPredicateArguments b) x
-> AggregationPredicateArguments b
$cfrom :: forall (b :: BackendType) x.
AggregationPredicateArguments b
-> Rep (AggregationPredicateArguments b) x
Generic)

deriving instance B.Backend b => Eq (AggregationPredicateArguments b)

deriving instance B.Backend b => Show (AggregationPredicateArguments b)

instance Backend b => Hashable (AggregationPredicateArguments b)

instance Backend b => Cacheable (AggregationPredicateArguments b)

instance Backend b => NFData (AggregationPredicateArguments b)

instance (Backend b) => ToJSON (AggregationPredicateArguments b)