{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE TemplateHaskell #-}

module Hasura.GraphQL.Schema.BoolExp
  ( AggregationPredicatesSchema (..),
    boolExp,
    mkBoolOperator,
    equalityOperators,
    comparisonOperators,
  )
where

import Data.Has (getter)
import Data.Text.Casing (GQLNameIdentifier)
import Data.Text.Casing qualified as C
import Data.Text.Extended
import Hasura.GraphQL.Parser.Class
import Hasura.GraphQL.Schema.Backend
import Hasura.GraphQL.Schema.Common (MonadBuildSchemaBase, SchemaContext (..), askTableInfo, partialSQLExpToUnpreparedValue, retrieve)
import Hasura.GraphQL.Schema.NamingCase
import Hasura.GraphQL.Schema.Options qualified as Options
import Hasura.GraphQL.Schema.Parser
  ( InputFieldsParser,
    Kind (..),
    Parser,
  )
import Hasura.GraphQL.Schema.Parser qualified as P
import Hasura.GraphQL.Schema.Table
import Hasura.GraphQL.Schema.Typename (mkTypename)
import Hasura.Name qualified as Name
import Hasura.Prelude
import Hasura.RQL.IR.BoolExp
import Hasura.RQL.IR.Value
import Hasura.RQL.Types.Backend
import Hasura.RQL.Types.Column
import Hasura.RQL.Types.ComputedField
import Hasura.RQL.Types.Function
import Hasura.RQL.Types.Relationships.Local
import Hasura.RQL.Types.SchemaCache hiding (askTableInfo)
import Hasura.RQL.Types.Source
import Hasura.RQL.Types.SourceCustomization
import Hasura.RQL.Types.Table
import Hasura.SQL.Backend (BackendType)
import Language.GraphQL.Draft.Syntax qualified as G

-- | Backends implement this type class to specify the schema of
-- aggregation predicates.
--
-- The default implementation results in a parser that does not parse anything.
--
-- The scope of this class is local to the function 'boolExp'. In particular,
-- methods in `class BackendSchema` and `type MonadBuildSchema` should *NOT*
-- include this class as a constraint.
class AggregationPredicatesSchema (b :: BackendType) where
  aggregationPredicatesParser ::
    forall r m n.
    MonadBuildSchemaBase r m n =>
    SourceInfo b ->
    TableInfo b ->
    m (Maybe (InputFieldsParser n [AggregationPredicates b (UnpreparedValue b)]))

-- Overlapping instance for backends that do not implement Aggregation Predicates.
instance {-# OVERLAPPABLE #-} (AggregationPredicates b ~ Const Void) => AggregationPredicatesSchema (b :: BackendType) where
  aggregationPredicatesParser ::
    forall r m n.
    (MonadBuildSchemaBase r m n) =>
    SourceInfo b ->
    TableInfo b ->
    m (Maybe (InputFieldsParser n [AggregationPredicates b (UnpreparedValue b)]))
  aggregationPredicatesParser :: SourceInfo b
-> TableInfo b
-> m (Maybe
        (InputFieldsParser
           n [AggregationPredicates b (UnpreparedValue b)]))
aggregationPredicatesParser SourceInfo b
_ TableInfo b
_ = Maybe (InputFieldsParser n [Const Void (UnpreparedValue b)])
-> m (Maybe (InputFieldsParser n [Const Void (UnpreparedValue b)]))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (InputFieldsParser n [Const Void (UnpreparedValue b)])
forall a. Maybe a
Nothing

-- |
-- > input type_bool_exp {
-- >   _or: [type_bool_exp!]
-- >   _and: [type_bool_exp!]
-- >   _not: type_bool_exp
-- >   column: type_comparison_exp
-- >   ...
-- > }
boolExp ::
  forall b r m n.
  (MonadBuildSchema b r m n, AggregationPredicatesSchema b) =>
  SourceInfo b ->
  TableInfo b ->
  m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
boolExp :: SourceInfo b
-> TableInfo b
-> m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
boolExp SourceInfo b
sourceInfo TableInfo b
tableInfo = Name
-> (SourceName, TableName b)
-> m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
-> m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
forall (m :: * -> *) a (p :: (* -> *) -> * -> *) (n :: * -> *) b.
(MonadMemoize m, Ord a, Typeable a, Typeable p, MonadParse n,
 Typeable b) =>
Name -> a -> m (p n b) -> m (p n b)
P.memoizeOn 'boolExp (SourceInfo b -> SourceName
forall (b :: BackendType). SourceInfo b -> SourceName
_siName SourceInfo b
sourceInfo, TableName b
tableName) (m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
 -> m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b))))
-> m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
-> m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ do
  NamingCase
tCase <- (r -> NamingCase) -> m NamingCase
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks r -> NamingCase
forall a t. Has a t => t -> a
getter
  GQLNameIdentifier
tableGQLName <- TableInfo b -> m GQLNameIdentifier
forall (b :: BackendType) (m :: * -> *).
(Backend b, MonadError QErr m) =>
TableInfo b -> m GQLNameIdentifier
getTableIdentifierName TableInfo b
tableInfo
  Name
name <- Name -> m Name
forall r (m :: * -> *).
(MonadReader r m, Has MkTypename r) =>
Name -> m Name
mkTypename (Name -> m Name) -> Name -> m Name
forall a b. (a -> b) -> a -> b
$ NamingCase -> GQLNameIdentifier -> Name
applyTypeNameCaseIdentifier NamingCase
tCase (GQLNameIdentifier -> Name) -> GQLNameIdentifier -> Name
forall a b. (a -> b) -> a -> b
$ GQLNameIdentifier -> GQLNameIdentifier
mkTableBoolExpTypeName GQLNameIdentifier
tableGQLName
  let description :: Description
description =
        Text -> Description
G.Description (Text -> Description) -> Text -> Description
forall a b. (a -> b) -> a -> b
$
          Text
"Boolean expression to filter rows from the table " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TableName b
tableName
            TableName b -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
". All fields are combined with a logical 'AND'."

  [FieldInfo b]
fieldInfos <- SourceInfo b -> TableInfo b -> m [FieldInfo b]
forall (b :: BackendType) r (m :: * -> *).
(Backend b, MonadError QErr m, MonadReader r m,
 Has SchemaContext r) =>
SourceInfo b -> TableInfo b -> m [FieldInfo b]
tableSelectFields SourceInfo b
sourceInfo TableInfo b
tableInfo
  [InputFieldsParser n (Maybe (AnnBoolExpFld b (UnpreparedValue b)))]
tableFieldParsers <- [Maybe
   (InputFieldsParser
      n (Maybe (AnnBoolExpFld b (UnpreparedValue b))))]
-> [InputFieldsParser
      n (Maybe (AnnBoolExpFld b (UnpreparedValue b)))]
forall (f :: * -> *) a. Filterable f => f (Maybe a) -> f a
catMaybes ([Maybe
    (InputFieldsParser
       n (Maybe (AnnBoolExpFld b (UnpreparedValue b))))]
 -> [InputFieldsParser
       n (Maybe (AnnBoolExpFld b (UnpreparedValue b)))])
-> m [Maybe
        (InputFieldsParser
           n (Maybe (AnnBoolExpFld b (UnpreparedValue b))))]
-> m [InputFieldsParser
        n (Maybe (AnnBoolExpFld b (UnpreparedValue b)))]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (FieldInfo b
 -> m (Maybe
         (InputFieldsParser
            n (Maybe (AnnBoolExpFld b (UnpreparedValue b))))))
-> [FieldInfo b]
-> m [Maybe
        (InputFieldsParser
           n (Maybe (AnnBoolExpFld b (UnpreparedValue b))))]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse FieldInfo b
-> m (Maybe
        (InputFieldsParser
           n (Maybe (AnnBoolExpFld b (UnpreparedValue b)))))
mkField [FieldInfo b]
fieldInfos
  -- TODO: This naming is somewhat unsatifactory..
  InputFieldsParser
  MetadataObjId n [AggregationPredicates b (UnpreparedValue b)]
aggregationPredicatesParser' <- InputFieldsParser
  MetadataObjId n [AggregationPredicates b (UnpreparedValue b)]
-> Maybe
     (InputFieldsParser
        MetadataObjId n [AggregationPredicates b (UnpreparedValue b)])
-> InputFieldsParser
     MetadataObjId n [AggregationPredicates b (UnpreparedValue b)]
forall a. a -> Maybe a -> a
fromMaybe ([AggregationPredicates b (UnpreparedValue b)]
-> InputFieldsParser
     MetadataObjId n [AggregationPredicates b (UnpreparedValue b)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []) (Maybe
   (InputFieldsParser
      MetadataObjId n [AggregationPredicates b (UnpreparedValue b)])
 -> InputFieldsParser
      MetadataObjId n [AggregationPredicates b (UnpreparedValue b)])
-> m (Maybe
        (InputFieldsParser
           MetadataObjId n [AggregationPredicates b (UnpreparedValue b)]))
-> m (InputFieldsParser
        MetadataObjId n [AggregationPredicates b (UnpreparedValue b)])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SourceInfo b
-> TableInfo b
-> m (Maybe
        (InputFieldsParser
           MetadataObjId n [AggregationPredicates b (UnpreparedValue b)]))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(AggregationPredicatesSchema b, MonadBuildSchemaBase r m n) =>
SourceInfo b
-> TableInfo b
-> m (Maybe
        (InputFieldsParser
           n [AggregationPredicates b (UnpreparedValue b)]))
aggregationPredicatesParser SourceInfo b
sourceInfo TableInfo b
tableInfo
  Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
recur <- SourceInfo b
-> TableInfo b
-> m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(MonadBuildSchema b r m n, AggregationPredicatesSchema b) =>
SourceInfo b
-> TableInfo b
-> m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
boolExp SourceInfo b
sourceInfo TableInfo b
tableInfo
  -- Bafflingly, ApplicativeDo doesn’t work if we inline this definition (I
  -- think the TH splices throw it off), so we have to define it separately.
  let connectiveFieldParsers :: [InputFieldsParser
   MetadataObjId n (Maybe (AnnBoolExp b (UnpreparedValue b)))]
connectiveFieldParsers =
        [ Name
-> Maybe Description
-> Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
-> InputFieldsParser
     MetadataObjId n (Maybe (AnnBoolExp b (UnpreparedValue b)))
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m (Maybe a)
P.fieldOptional Name
Name.__or Maybe Description
forall a. Maybe a
Nothing ([AnnBoolExp b (UnpreparedValue b)]
-> AnnBoolExp b (UnpreparedValue b)
forall (backend :: BackendType) field.
[GBoolExp backend field] -> GBoolExp backend field
BoolOr ([AnnBoolExp b (UnpreparedValue b)]
 -> AnnBoolExp b (UnpreparedValue b))
-> Parser MetadataObjId 'Input n [AnnBoolExp b (UnpreparedValue b)]
-> Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
-> Parser MetadataObjId 'Input n [AnnBoolExp b (UnpreparedValue b)]
forall origin (k :: Kind) (m :: * -> *) a.
(MonadParse m, 'Input <: k) =>
Parser origin k m a -> Parser origin k m [a]
P.list Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
recur),
          Name
-> Maybe Description
-> Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
-> InputFieldsParser
     MetadataObjId n (Maybe (AnnBoolExp b (UnpreparedValue b)))
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m (Maybe a)
P.fieldOptional Name
Name.__and Maybe Description
forall a. Maybe a
Nothing ([AnnBoolExp b (UnpreparedValue b)]
-> AnnBoolExp b (UnpreparedValue b)
forall (backend :: BackendType) field.
[GBoolExp backend field] -> GBoolExp backend field
BoolAnd ([AnnBoolExp b (UnpreparedValue b)]
 -> AnnBoolExp b (UnpreparedValue b))
-> Parser MetadataObjId 'Input n [AnnBoolExp b (UnpreparedValue b)]
-> Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
-> Parser MetadataObjId 'Input n [AnnBoolExp b (UnpreparedValue b)]
forall origin (k :: Kind) (m :: * -> *) a.
(MonadParse m, 'Input <: k) =>
Parser origin k m a -> Parser origin k m [a]
P.list Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
recur),
          Name
-> Maybe Description
-> Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
-> InputFieldsParser
     MetadataObjId n (Maybe (AnnBoolExp b (UnpreparedValue b)))
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m (Maybe a)
P.fieldOptional Name
Name.__not Maybe Description
forall a. Maybe a
Nothing (AnnBoolExp b (UnpreparedValue b)
-> AnnBoolExp b (UnpreparedValue b)
forall (backend :: BackendType) field.
GBoolExp backend field -> GBoolExp backend field
BoolNot (AnnBoolExp b (UnpreparedValue b)
 -> AnnBoolExp b (UnpreparedValue b))
-> Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
-> Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
recur)
        ]

  pure $
    [AnnBoolExp b (UnpreparedValue b)]
-> AnnBoolExp b (UnpreparedValue b)
forall (backend :: BackendType) field.
[GBoolExp backend field] -> GBoolExp backend field
BoolAnd ([AnnBoolExp b (UnpreparedValue b)]
 -> AnnBoolExp b (UnpreparedValue b))
-> Parser MetadataObjId 'Input n [AnnBoolExp b (UnpreparedValue b)]
-> Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name
-> Maybe Description
-> InputFieldsParser
     MetadataObjId n [AnnBoolExp b (UnpreparedValue b)]
-> Parser MetadataObjId 'Input n [AnnBoolExp b (UnpreparedValue b)]
forall (m :: * -> *) origin a.
MonadParse m =>
Name
-> Maybe Description
-> InputFieldsParser origin m a
-> Parser origin 'Input m a
P.object Name
name (Description -> Maybe Description
forall a. a -> Maybe a
Just Description
description) do
      [AnnBoolExp b (UnpreparedValue b)]
tableFields <- (AnnBoolExpFld b (UnpreparedValue b)
 -> AnnBoolExp b (UnpreparedValue b))
-> [AnnBoolExpFld b (UnpreparedValue b)]
-> [AnnBoolExp b (UnpreparedValue b)]
forall a b. (a -> b) -> [a] -> [b]
map AnnBoolExpFld b (UnpreparedValue b)
-> AnnBoolExp b (UnpreparedValue b)
forall (backend :: BackendType) field.
field -> GBoolExp backend field
BoolField ([AnnBoolExpFld b (UnpreparedValue b)]
 -> [AnnBoolExp b (UnpreparedValue b)])
-> ([Maybe (AnnBoolExpFld b (UnpreparedValue b))]
    -> [AnnBoolExpFld b (UnpreparedValue b)])
-> [Maybe (AnnBoolExpFld b (UnpreparedValue b))]
-> [AnnBoolExp b (UnpreparedValue b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe (AnnBoolExpFld b (UnpreparedValue b))]
-> [AnnBoolExpFld b (UnpreparedValue b)]
forall (f :: * -> *) a. Filterable f => f (Maybe a) -> f a
catMaybes ([Maybe (AnnBoolExpFld b (UnpreparedValue b))]
 -> [AnnBoolExp b (UnpreparedValue b)])
-> InputFieldsParser
     MetadataObjId n [Maybe (AnnBoolExpFld b (UnpreparedValue b))]
-> InputFieldsParser
     MetadataObjId n [AnnBoolExp b (UnpreparedValue b)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [InputFieldsParser n (Maybe (AnnBoolExpFld b (UnpreparedValue b)))]
-> InputFieldsParser
     MetadataObjId n [Maybe (AnnBoolExpFld b (UnpreparedValue b))]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA [InputFieldsParser n (Maybe (AnnBoolExpFld b (UnpreparedValue b)))]
tableFieldParsers
      [AnnBoolExp b (UnpreparedValue b)]
specialFields <- [Maybe (AnnBoolExp b (UnpreparedValue b))]
-> [AnnBoolExp b (UnpreparedValue b)]
forall (f :: * -> *) a. Filterable f => f (Maybe a) -> f a
catMaybes ([Maybe (AnnBoolExp b (UnpreparedValue b))]
 -> [AnnBoolExp b (UnpreparedValue b)])
-> InputFieldsParser
     MetadataObjId n [Maybe (AnnBoolExp b (UnpreparedValue b))]
-> InputFieldsParser
     MetadataObjId n [AnnBoolExp b (UnpreparedValue b)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [InputFieldsParser
   MetadataObjId n (Maybe (AnnBoolExp b (UnpreparedValue b)))]
-> InputFieldsParser
     MetadataObjId n [Maybe (AnnBoolExp b (UnpreparedValue b))]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA [InputFieldsParser
   MetadataObjId n (Maybe (AnnBoolExp b (UnpreparedValue b)))]
connectiveFieldParsers
      [AnnBoolExp b (UnpreparedValue b)]
aggregationPredicateFields <- (AggregationPredicates b (UnpreparedValue b)
 -> AnnBoolExp b (UnpreparedValue b))
-> [AggregationPredicates b (UnpreparedValue b)]
-> [AnnBoolExp b (UnpreparedValue b)]
forall a b. (a -> b) -> [a] -> [b]
map (AnnBoolExpFld b (UnpreparedValue b)
-> AnnBoolExp b (UnpreparedValue b)
forall (backend :: BackendType) field.
field -> GBoolExp backend field
BoolField (AnnBoolExpFld b (UnpreparedValue b)
 -> AnnBoolExp b (UnpreparedValue b))
-> (AggregationPredicates b (UnpreparedValue b)
    -> AnnBoolExpFld b (UnpreparedValue b))
-> AggregationPredicates b (UnpreparedValue b)
-> AnnBoolExp b (UnpreparedValue b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AggregationPredicates b (UnpreparedValue b)
-> AnnBoolExpFld b (UnpreparedValue b)
forall (backend :: BackendType) leaf.
AggregationPredicates backend leaf -> AnnBoolExpFld backend leaf
AVAggregationPredicates) ([AggregationPredicates b (UnpreparedValue b)]
 -> [AnnBoolExp b (UnpreparedValue b)])
-> InputFieldsParser
     MetadataObjId n [AggregationPredicates b (UnpreparedValue b)]
-> InputFieldsParser
     MetadataObjId n [AnnBoolExp b (UnpreparedValue b)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> InputFieldsParser
  MetadataObjId n [AggregationPredicates b (UnpreparedValue b)]
aggregationPredicatesParser'
      pure ([AnnBoolExp b (UnpreparedValue b)]
tableFields [AnnBoolExp b (UnpreparedValue b)]
-> [AnnBoolExp b (UnpreparedValue b)]
-> [AnnBoolExp b (UnpreparedValue b)]
forall a. [a] -> [a] -> [a]
++ [AnnBoolExp b (UnpreparedValue b)]
specialFields [AnnBoolExp b (UnpreparedValue b)]
-> [AnnBoolExp b (UnpreparedValue b)]
-> [AnnBoolExp b (UnpreparedValue b)]
forall a. [a] -> [a] -> [a]
++ [AnnBoolExp b (UnpreparedValue b)]
aggregationPredicateFields)
  where
    tableName :: TableName b
tableName = TableInfo b -> TableName b
forall (b :: BackendType). TableInfo b -> TableName b
tableInfoName TableInfo b
tableInfo

    mkField ::
      FieldInfo b ->
      m (Maybe (InputFieldsParser n (Maybe (AnnBoolExpFld b (UnpreparedValue b)))))
    mkField :: FieldInfo b
-> m (Maybe
        (InputFieldsParser
           n (Maybe (AnnBoolExpFld b (UnpreparedValue b)))))
mkField FieldInfo b
fieldInfo = MaybeT
  m
  (InputFieldsParser n (Maybe (AnnBoolExpFld b (UnpreparedValue b))))
-> m (Maybe
        (InputFieldsParser
           n (Maybe (AnnBoolExpFld b (UnpreparedValue b)))))
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT do
      RoleName
roleName <- (SchemaContext -> RoleName) -> MaybeT m RoleName
forall r (m :: * -> *) a b.
(MonadReader r m, Has a r) =>
(a -> b) -> m b
retrieve SchemaContext -> RoleName
scRole
      Name
fieldName <- Maybe Name -> MaybeT m Name
forall (m :: * -> *) b. Applicative m => Maybe b -> MaybeT m b
hoistMaybe (Maybe Name -> MaybeT m Name) -> Maybe Name -> MaybeT m Name
forall a b. (a -> b) -> a -> b
$ FieldInfo b -> Maybe Name
forall (b :: BackendType). FieldInfo b -> Maybe Name
fieldInfoGraphQLName FieldInfo b
fieldInfo
      Name
-> Maybe Description
-> Parser
     MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b))
-> InputFieldsParser
     n (Maybe (AnnBoolExpFld b (UnpreparedValue b)))
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m (Maybe a)
P.fieldOptional Name
fieldName Maybe Description
forall a. Maybe a
Nothing (Parser
   MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b))
 -> InputFieldsParser
      n (Maybe (AnnBoolExpFld b (UnpreparedValue b))))
-> MaybeT
     m
     (Parser
        MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b)))
-> MaybeT
     m
     (InputFieldsParser n (Maybe (AnnBoolExpFld b (UnpreparedValue b))))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case FieldInfo b
fieldInfo of
        -- field_name: field_type_comparison_exp
        FIColumn ColumnInfo b
columnInfo ->
          m (Parser
     MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b)))
-> MaybeT
     m
     (Parser
        MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b)))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Parser
      MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b)))
 -> MaybeT
      m
      (Parser
         MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b))))
-> m (Parser
        MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b)))
-> MaybeT
     m
     (Parser
        MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ ([OpExpG b (UnpreparedValue b)]
 -> AnnBoolExpFld b (UnpreparedValue b))
-> Parser MetadataObjId 'Input n [OpExpG b (UnpreparedValue b)]
-> Parser
     MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ColumnInfo b
-> [OpExpG b (UnpreparedValue b)]
-> AnnBoolExpFld b (UnpreparedValue b)
forall (backend :: BackendType) leaf.
ColumnInfo backend
-> [OpExpG backend leaf] -> AnnBoolExpFld backend leaf
AVColumn ColumnInfo b
columnInfo) (Parser MetadataObjId 'Input n [OpExpG b (UnpreparedValue b)]
 -> Parser
      MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b)))
-> m (Parser MetadataObjId 'Input n [OpExpG b (UnpreparedValue b)])
-> m (Parser
        MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ColumnType b
-> m (Parser MetadataObjId 'Input n [OpExpG b (UnpreparedValue b)])
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(BackendSchema b, MonadBuildSchema b r m n) =>
ColumnType b -> m (Parser 'Input n [ComparisonExp b])
comparisonExps @b (ColumnInfo b -> ColumnType b
forall (b :: BackendType). ColumnInfo b -> ColumnType b
ciType ColumnInfo b
columnInfo)
        -- field_name: field_type_bool_exp
        FIRelationship RelInfo b
relationshipInfo -> do
          TableInfo b
remoteTableInfo <- SourceInfo b -> TableName b -> MaybeT m (TableInfo b)
forall (b :: BackendType) (m :: * -> *).
(Backend b, MonadError QErr m) =>
SourceInfo b -> TableName b -> m (TableInfo b)
askTableInfo SourceInfo b
sourceInfo (TableName b -> MaybeT m (TableInfo b))
-> TableName b -> MaybeT m (TableInfo b)
forall a b. (a -> b) -> a -> b
$ RelInfo b -> TableName b
forall (b :: BackendType). RelInfo b -> TableName b
riRTable RelInfo b
relationshipInfo
          let remoteTableFilter :: AnnBoolExp b (UnpreparedValue b)
remoteTableFilter =
                ((AnnBoolExpFld b (PartialSQLExp b)
 -> AnnBoolExpFld b (UnpreparedValue b))
-> GBoolExp b (AnnBoolExpFld b (PartialSQLExp b))
-> AnnBoolExp b (UnpreparedValue b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((AnnBoolExpFld b (PartialSQLExp b)
  -> AnnBoolExpFld b (UnpreparedValue b))
 -> GBoolExp b (AnnBoolExpFld b (PartialSQLExp b))
 -> AnnBoolExp b (UnpreparedValue b))
-> ((PartialSQLExp b -> UnpreparedValue b)
    -> AnnBoolExpFld b (PartialSQLExp b)
    -> AnnBoolExpFld b (UnpreparedValue b))
-> (PartialSQLExp b -> UnpreparedValue b)
-> GBoolExp b (AnnBoolExpFld b (PartialSQLExp b))
-> AnnBoolExp b (UnpreparedValue b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PartialSQLExp b -> UnpreparedValue b)
-> AnnBoolExpFld b (PartialSQLExp b)
-> AnnBoolExpFld b (UnpreparedValue b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) PartialSQLExp b -> UnpreparedValue b
forall (b :: BackendType). PartialSQLExp b -> UnpreparedValue b
partialSQLExpToUnpreparedValue (GBoolExp b (AnnBoolExpFld b (PartialSQLExp b))
 -> AnnBoolExp b (UnpreparedValue b))
-> GBoolExp b (AnnBoolExpFld b (PartialSQLExp b))
-> AnnBoolExp b (UnpreparedValue b)
forall a b. (a -> b) -> a -> b
$
                  GBoolExp b (AnnBoolExpFld b (PartialSQLExp b))
-> (SelPermInfo b
    -> GBoolExp b (AnnBoolExpFld b (PartialSQLExp b)))
-> Maybe (SelPermInfo b)
-> GBoolExp b (AnnBoolExpFld b (PartialSQLExp b))
forall b a. b -> (a -> b) -> Maybe a -> b
maybe GBoolExp b (AnnBoolExpFld b (PartialSQLExp b))
forall (backend :: BackendType) scalar. AnnBoolExp backend scalar
annBoolExpTrue SelPermInfo b -> GBoolExp b (AnnBoolExpFld b (PartialSQLExp b))
forall (b :: BackendType). SelPermInfo b -> AnnBoolExpPartialSQL b
spiFilter (Maybe (SelPermInfo b)
 -> GBoolExp b (AnnBoolExpFld b (PartialSQLExp b)))
-> Maybe (SelPermInfo b)
-> GBoolExp b (AnnBoolExpFld b (PartialSQLExp b))
forall a b. (a -> b) -> a -> b
$
                    RoleName -> TableInfo b -> Maybe (SelPermInfo b)
forall (b :: BackendType).
RoleName -> TableInfo b -> Maybe (SelPermInfo b)
tableSelectPermissions RoleName
roleName TableInfo b
remoteTableInfo
          Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
remoteBoolExp <- m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
-> MaybeT m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
 -> MaybeT m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b))))
-> m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
-> MaybeT m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ SourceInfo b
-> TableInfo b
-> m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(MonadBuildSchema b r m n, AggregationPredicatesSchema b) =>
SourceInfo b
-> TableInfo b
-> m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
boolExp SourceInfo b
sourceInfo TableInfo b
remoteTableInfo
          pure $ (AnnBoolExp b (UnpreparedValue b)
 -> AnnBoolExpFld b (UnpreparedValue b))
-> Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
-> Parser
     MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (RelInfo b
-> AnnBoolExp b (UnpreparedValue b)
-> AnnBoolExpFld b (UnpreparedValue b)
forall (backend :: BackendType) leaf.
RelInfo backend
-> AnnBoolExp backend leaf -> AnnBoolExpFld backend leaf
AVRelationship RelInfo b
relationshipInfo (AnnBoolExp b (UnpreparedValue b)
 -> AnnBoolExpFld b (UnpreparedValue b))
-> (AnnBoolExp b (UnpreparedValue b)
    -> AnnBoolExp b (UnpreparedValue b))
-> AnnBoolExp b (UnpreparedValue b)
-> AnnBoolExpFld b (UnpreparedValue b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnnBoolExp b (UnpreparedValue b)
-> AnnBoolExp b (UnpreparedValue b)
-> AnnBoolExp b (UnpreparedValue b)
forall (backend :: BackendType) scalar.
AnnBoolExp backend scalar
-> AnnBoolExp backend scalar -> AnnBoolExp backend scalar
andAnnBoolExps AnnBoolExp b (UnpreparedValue b)
remoteTableFilter) Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
remoteBoolExp
        FIComputedField ComputedFieldInfo {Maybe Text
ComputedFieldReturn b
XComputedField b
ComputedFieldFunction b
ComputedFieldName
_cfiDescription :: forall (b :: BackendType). ComputedFieldInfo b -> Maybe Text
_cfiReturnType :: forall (b :: BackendType).
ComputedFieldInfo b -> ComputedFieldReturn b
_cfiFunction :: forall (b :: BackendType).
ComputedFieldInfo b -> ComputedFieldFunction b
_cfiName :: forall (b :: BackendType). ComputedFieldInfo b -> ComputedFieldName
_cfiXComputedFieldInfo :: forall (b :: BackendType). ComputedFieldInfo b -> XComputedField b
_cfiDescription :: Maybe Text
_cfiReturnType :: ComputedFieldReturn b
_cfiFunction :: ComputedFieldFunction b
_cfiName :: ComputedFieldName
_cfiXComputedFieldInfo :: XComputedField b
..} -> do
          let ComputedFieldFunction {Maybe PGDescription
Seq (FunctionArgument b)
FunctionName b
ComputedFieldImplicitArguments b
_cffDescription :: forall (b :: BackendType).
ComputedFieldFunction b -> Maybe PGDescription
_cffComputedFieldImplicitArgs :: forall (b :: BackendType).
ComputedFieldFunction b -> ComputedFieldImplicitArguments b
_cffInputArgs :: forall (b :: BackendType).
ComputedFieldFunction b -> Seq (FunctionArgument b)
_cffName :: forall (b :: BackendType).
ComputedFieldFunction b -> FunctionName b
_cffDescription :: Maybe PGDescription
_cffComputedFieldImplicitArgs :: ComputedFieldImplicitArguments b
_cffInputArgs :: Seq (FunctionArgument b)
_cffName :: FunctionName b
..} = ComputedFieldFunction b
_cfiFunction
          -- For a computed field to qualify in boolean expression it shouldn't have any input arguments
          case Seq (FunctionArgument b) -> [FunctionArgument b]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Seq (FunctionArgument b)
_cffInputArgs of
            [] -> do
              let functionArgs :: FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b))
functionArgs =
                    ([FunctionArgumentExp b (UnpreparedValue b)]
 -> HashMap Text (FunctionArgumentExp b (UnpreparedValue b))
 -> FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b)))
-> HashMap Text (FunctionArgumentExp b (UnpreparedValue b))
-> [FunctionArgumentExp b (UnpreparedValue b)]
-> FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b))
forall a b c. (a -> b -> c) -> b -> a -> c
flip [FunctionArgumentExp b (UnpreparedValue b)]
-> HashMap Text (FunctionArgumentExp b (UnpreparedValue b))
-> FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b))
forall a. [a] -> HashMap Text a -> FunctionArgsExpG a
FunctionArgsExp HashMap Text (FunctionArgumentExp b (UnpreparedValue b))
forall a. Monoid a => a
mempty ([FunctionArgumentExp b (UnpreparedValue b)]
 -> FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b)))
-> [FunctionArgumentExp b (UnpreparedValue b)]
-> FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b))
forall a b. (a -> b) -> a -> b
$
                      UnpreparedValue b
-> ComputedFieldImplicitArguments b
-> [FunctionArgumentExp b (UnpreparedValue b)]
forall (b :: BackendType) v.
Backend b =>
v -> ComputedFieldImplicitArguments b -> [FunctionArgumentExp b v]
fromComputedFieldImplicitArguments @b UnpreparedValue b
forall (b :: BackendType). UnpreparedValue b
UVSession ComputedFieldImplicitArguments b
_cffComputedFieldImplicitArgs

              (ComputedFieldBoolExp b (UnpreparedValue b)
 -> AnnBoolExpFld b (UnpreparedValue b))
-> Parser
     MetadataObjId 'Input n (ComputedFieldBoolExp b (UnpreparedValue b))
-> Parser
     MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (AnnComputedFieldBoolExp b (UnpreparedValue b)
-> AnnBoolExpFld b (UnpreparedValue b)
forall (backend :: BackendType) leaf.
AnnComputedFieldBoolExp backend leaf -> AnnBoolExpFld backend leaf
AVComputedField (AnnComputedFieldBoolExp b (UnpreparedValue b)
 -> AnnBoolExpFld b (UnpreparedValue b))
-> (ComputedFieldBoolExp b (UnpreparedValue b)
    -> AnnComputedFieldBoolExp b (UnpreparedValue b))
-> ComputedFieldBoolExp b (UnpreparedValue b)
-> AnnBoolExpFld b (UnpreparedValue b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XComputedField b
-> ComputedFieldName
-> FunctionName b
-> FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b))
-> ComputedFieldBoolExp b (UnpreparedValue b)
-> AnnComputedFieldBoolExp b (UnpreparedValue b)
forall (backend :: BackendType) scalar.
XComputedField backend
-> ComputedFieldName
-> FunctionName backend
-> FunctionArgsExp backend scalar
-> ComputedFieldBoolExp backend scalar
-> AnnComputedFieldBoolExp backend scalar
AnnComputedFieldBoolExp XComputedField b
_cfiXComputedFieldInfo ComputedFieldName
_cfiName FunctionName b
_cffName FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b))
functionArgs)
                (Parser
   MetadataObjId 'Input n (ComputedFieldBoolExp b (UnpreparedValue b))
 -> Parser
      MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b)))
-> MaybeT
     m
     (Parser
        MetadataObjId
        'Input
        n
        (ComputedFieldBoolExp b (UnpreparedValue b)))
-> MaybeT
     m
     (Parser
        MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case ComputedFieldReturn b -> ComputedFieldReturnType b
forall (b :: BackendType).
Backend b =>
ComputedFieldReturn b -> ComputedFieldReturnType b
computedFieldReturnType @b ComputedFieldReturn b
_cfiReturnType of
                  ReturnsScalar ScalarType b
scalarType -> m (Parser
     MetadataObjId
     'Input
     n
     (ComputedFieldBoolExp b (UnpreparedValue b)))
-> MaybeT
     m
     (Parser
        MetadataObjId
        'Input
        n
        (ComputedFieldBoolExp b (UnpreparedValue b)))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Parser
      MetadataObjId
      'Input
      n
      (ComputedFieldBoolExp b (UnpreparedValue b)))
 -> MaybeT
      m
      (Parser
         MetadataObjId
         'Input
         n
         (ComputedFieldBoolExp b (UnpreparedValue b))))
-> m (Parser
        MetadataObjId
        'Input
        n
        (ComputedFieldBoolExp b (UnpreparedValue b)))
-> MaybeT
     m
     (Parser
        MetadataObjId
        'Input
        n
        (ComputedFieldBoolExp b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ ([OpExpG b (UnpreparedValue b)]
 -> ComputedFieldBoolExp b (UnpreparedValue b))
-> Parser MetadataObjId 'Input n [OpExpG b (UnpreparedValue b)]
-> Parser
     MetadataObjId 'Input n (ComputedFieldBoolExp b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [OpExpG b (UnpreparedValue b)]
-> ComputedFieldBoolExp b (UnpreparedValue b)
forall (backend :: BackendType) scalar.
[OpExpG backend scalar] -> ComputedFieldBoolExp backend scalar
CFBEScalar (Parser MetadataObjId 'Input n [OpExpG b (UnpreparedValue b)]
 -> Parser
      MetadataObjId
      'Input
      n
      (ComputedFieldBoolExp b (UnpreparedValue b)))
-> m (Parser MetadataObjId 'Input n [OpExpG b (UnpreparedValue b)])
-> m (Parser
        MetadataObjId
        'Input
        n
        (ComputedFieldBoolExp b (UnpreparedValue b)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ColumnType b
-> m (Parser MetadataObjId 'Input n [OpExpG b (UnpreparedValue b)])
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(BackendSchema b, MonadBuildSchema b r m n) =>
ColumnType b -> m (Parser 'Input n [ComparisonExp b])
comparisonExps @b (ScalarType b -> ColumnType b
forall (b :: BackendType). ScalarType b -> ColumnType b
ColumnScalar ScalarType b
scalarType)
                  ReturnsTable TableName b
table -> do
                    TableInfo b
info <- SourceInfo b -> TableName b -> MaybeT m (TableInfo b)
forall (b :: BackendType) (m :: * -> *).
(Backend b, MonadError QErr m) =>
SourceInfo b -> TableName b -> m (TableInfo b)
askTableInfo SourceInfo b
sourceInfo TableName b
table
                    m (Parser
     MetadataObjId
     'Input
     n
     (ComputedFieldBoolExp b (UnpreparedValue b)))
-> MaybeT
     m
     (Parser
        MetadataObjId
        'Input
        n
        (ComputedFieldBoolExp b (UnpreparedValue b)))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Parser
      MetadataObjId
      'Input
      n
      (ComputedFieldBoolExp b (UnpreparedValue b)))
 -> MaybeT
      m
      (Parser
         MetadataObjId
         'Input
         n
         (ComputedFieldBoolExp b (UnpreparedValue b))))
-> m (Parser
        MetadataObjId
        'Input
        n
        (ComputedFieldBoolExp b (UnpreparedValue b)))
-> MaybeT
     m
     (Parser
        MetadataObjId
        'Input
        n
        (ComputedFieldBoolExp b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ (AnnBoolExp b (UnpreparedValue b)
 -> ComputedFieldBoolExp b (UnpreparedValue b))
-> Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
-> Parser
     MetadataObjId 'Input n (ComputedFieldBoolExp b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (TableName b
-> AnnBoolExp b (UnpreparedValue b)
-> ComputedFieldBoolExp b (UnpreparedValue b)
forall (backend :: BackendType) scalar.
TableName backend
-> AnnBoolExp backend scalar -> ComputedFieldBoolExp backend scalar
CFBETable TableName b
table) (Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
 -> Parser
      MetadataObjId
      'Input
      n
      (ComputedFieldBoolExp b (UnpreparedValue b)))
-> m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
-> m (Parser
        MetadataObjId
        'Input
        n
        (ComputedFieldBoolExp b (UnpreparedValue b)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SourceInfo b
-> TableInfo b
-> m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(MonadBuildSchema b r m n, AggregationPredicatesSchema b) =>
SourceInfo b
-> TableInfo b
-> m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
boolExp SourceInfo b
sourceInfo TableInfo b
info
                  ComputedFieldReturnType b
ReturnsOthers -> Maybe
  (Parser
     MetadataObjId
     'Input
     n
     (ComputedFieldBoolExp b (UnpreparedValue b)))
-> MaybeT
     m
     (Parser
        MetadataObjId
        'Input
        n
        (ComputedFieldBoolExp b (UnpreparedValue b)))
forall (m :: * -> *) b. Applicative m => Maybe b -> MaybeT m b
hoistMaybe Maybe
  (Parser
     MetadataObjId
     'Input
     n
     (ComputedFieldBoolExp b (UnpreparedValue b)))
forall a. Maybe a
Nothing
            [FunctionArgument b]
_ -> Maybe
  (Parser
     MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b)))
-> MaybeT
     m
     (Parser
        MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b)))
forall (m :: * -> *) b. Applicative m => Maybe b -> MaybeT m b
hoistMaybe Maybe
  (Parser
     MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b)))
forall a. Maybe a
Nothing

        -- Using remote relationship fields in boolean expressions is not supported.
        FIRemoteRelationship RemoteFieldInfo (DBJoinField b)
_ -> MaybeT
  m
  (Parser
     MetadataObjId 'Input n (AnnBoolExpFld b (UnpreparedValue b)))
forall (f :: * -> *) a. Alternative f => f a
empty

{- Note [Nullability in comparison operators]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In comparisonExps, we hardcode most operators with `Nullability False` when
calling `column`, which might seem a bit sketchy. Shouldn’t the nullability
depend on the nullability of the underlying Postgres column?

No. If we did that, then we would allow boolean expressions like this:

    delete_users(where: {status: {eq: null}})

which in turn would generate a SQL query along the lines of:

    DELETE FROM users WHERE users.status = NULL

but `= NULL` might not do what they expect. For instance, on Postgres, it always
evaluates to False!

Even operators for which `null` is a valid value must be careful in their
implementation. An explicit `null` must always be handled explicitly! If,
instead, an explicit null is ignored:

    foo <- fmap join $ fieldOptional "_foo_level" $ nullable int

then

       delete_users(where: {_foo_level: null})
    => delete_users(where: {})
    => delete_users()

Now we’ve gone and deleted every user in the database. Whoops! Hopefully the
user had backups!

In most cases, as mentioned above, we avoid this problem by making the column
value non-nullable (which is correct, since we never treat a null value as a SQL
NULL), then creating the field using 'fieldOptional'. This creates a parser that
rejects nulls, but won’t be called at all if the field is not specified, which
is permitted by the GraphQL specification. See Note [The value of omitted
fields] in Hasura.GraphQL.Parser.Internal.Parser for more details.

Additionally, it is worth nothing that the `column` parser *does* handle
explicit nulls, by creating a Null column value.

But... the story doesn't end there. Some of our users WANT this peculiar
behaviour. For instance, they want to be able to express the following:

    query($isVerified: Boolean) {
      users(where: {_isVerified: {_eq: $isVerified}}) {
        name
      }
    }

    $isVerified is True  -> return users who are verified
    $isVerified is False -> return users who aren't
    $isVerified is null  -> return all users

In the future, we will likely introduce a separate group of operators that do
implement this particular behaviour explicitly; but for now we have an option that
reverts to the previous behaviour.

To do so, we have to treat explicit nulls as implicit one: this is what the
'nullable' combinator does: it treats an explicit null as if the field has never
been called at all.
-}

-- This is temporary, and should be removed as soon as possible.
mkBoolOperator ::
  (MonadParse n, 'Input P.<: k) =>
  -- | Naming convention for the field
  NamingCase ->
  -- | shall this be collapsed to True when null is given?
  Options.DangerouslyCollapseBooleans ->
  -- | name of this operator
  GQLNameIdentifier ->
  -- | optional description
  Maybe G.Description ->
  -- | parser for the underlying value
  Parser k n a ->
  InputFieldsParser n (Maybe a)
mkBoolOperator :: NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n a
-> InputFieldsParser n (Maybe a)
mkBoolOperator NamingCase
tCase DangerouslyCollapseBooleans
Options.DangerouslyCollapseBooleans GQLNameIdentifier
name Maybe Description
desc = (Maybe (Maybe a) -> Maybe a)
-> InputFieldsParser MetadataObjId n (Maybe (Maybe a))
-> InputFieldsParser n (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe (Maybe a) -> Maybe a
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (InputFieldsParser MetadataObjId n (Maybe (Maybe a))
 -> InputFieldsParser n (Maybe a))
-> (Parser k n a
    -> InputFieldsParser MetadataObjId n (Maybe (Maybe a)))
-> Parser k n a
-> InputFieldsParser n (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name
-> Maybe Description
-> Parser MetadataObjId k n (Maybe a)
-> InputFieldsParser MetadataObjId n (Maybe (Maybe a))
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m (Maybe a)
P.fieldOptional (NamingCase -> GQLNameIdentifier -> Name
applyFieldNameCaseIdentifier NamingCase
tCase GQLNameIdentifier
name) Maybe Description
desc (Parser MetadataObjId k n (Maybe a)
 -> InputFieldsParser MetadataObjId n (Maybe (Maybe a)))
-> (Parser k n a -> Parser MetadataObjId k n (Maybe a))
-> Parser k n a
-> InputFieldsParser MetadataObjId n (Maybe (Maybe a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser k n a -> Parser MetadataObjId k n (Maybe a)
forall origin (k :: Kind) (m :: * -> *) a.
(MonadParse m, 'Input <: k) =>
Parser origin k m a -> Parser origin k m (Maybe a)
P.nullable
mkBoolOperator NamingCase
tCase DangerouslyCollapseBooleans
Options.Don'tDangerouslyCollapseBooleans GQLNameIdentifier
name Maybe Description
desc = Name
-> Maybe Description
-> Parser k n a
-> InputFieldsParser n (Maybe a)
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m (Maybe a)
P.fieldOptional (NamingCase -> GQLNameIdentifier -> Name
applyFieldNameCaseIdentifier NamingCase
tCase GQLNameIdentifier
name) Maybe Description
desc

equalityOperators ::
  (MonadParse n, 'Input P.<: k) =>
  NamingCase ->
  -- | shall this be collapsed to True when null is given?
  Options.DangerouslyCollapseBooleans ->
  -- | parser for one column value
  Parser k n (UnpreparedValue b) ->
  -- | parser for a list of column values
  Parser k n (UnpreparedValue b) ->
  [InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))]
equalityOperators :: NamingCase
-> DangerouslyCollapseBooleans
-> Parser k n (UnpreparedValue b)
-> Parser k n (UnpreparedValue b)
-> [InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))]
equalityOperators NamingCase
tCase DangerouslyCollapseBooleans
collapseIfNull Parser k n (UnpreparedValue b)
valueParser Parser k n (UnpreparedValue b)
valueListParser =
  [ NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser 'Both n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall (n :: * -> *) (k :: Kind) a.
(MonadParse n, 'Input <: k) =>
NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n a
-> InputFieldsParser n (Maybe a)
mkBoolOperator NamingCase
tCase DangerouslyCollapseBooleans
collapseIfNull ((Name, [NameSuffix]) -> GQLNameIdentifier
C.fromAutogeneratedTuple $$(G.litGQLIdentifier ["_is", "null"])) Maybe Description
forall a. Maybe a
Nothing (Parser 'Both n (OpExpG b (UnpreparedValue b))
 -> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b))))
-> Parser 'Both n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ OpExpG b (UnpreparedValue b)
-> OpExpG b (UnpreparedValue b)
-> Bool
-> OpExpG b (UnpreparedValue b)
forall a. a -> a -> Bool -> a
bool OpExpG b (UnpreparedValue b)
forall (backend :: BackendType) field. OpExpG backend field
ANISNOTNULL OpExpG b (UnpreparedValue b)
forall (backend :: BackendType) field. OpExpG backend field
ANISNULL (Bool -> OpExpG b (UnpreparedValue b))
-> Parser MetadataObjId 'Both n Bool
-> Parser 'Both n (OpExpG b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser MetadataObjId 'Both n Bool
forall (m :: * -> *) origin.
MonadParse m =>
Parser origin 'Both m Bool
P.boolean,
    NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall (n :: * -> *) (k :: Kind) a.
(MonadParse n, 'Input <: k) =>
NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n a
-> InputFieldsParser n (Maybe a)
mkBoolOperator NamingCase
tCase DangerouslyCollapseBooleans
collapseIfNull (Name -> GQLNameIdentifier
C.fromAutogeneratedName Name
Name.__eq) Maybe Description
forall a. Maybe a
Nothing (Parser k n (OpExpG b (UnpreparedValue b))
 -> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b))))
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ Bool -> UnpreparedValue b -> OpExpG b (UnpreparedValue b)
forall (backend :: BackendType) field.
Bool -> field -> OpExpG backend field
AEQ Bool
True (UnpreparedValue b -> OpExpG b (UnpreparedValue b))
-> Parser k n (UnpreparedValue b)
-> Parser k n (OpExpG b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser k n (UnpreparedValue b)
valueParser,
    NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall (n :: * -> *) (k :: Kind) a.
(MonadParse n, 'Input <: k) =>
NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n a
-> InputFieldsParser n (Maybe a)
mkBoolOperator NamingCase
tCase DangerouslyCollapseBooleans
collapseIfNull (Name -> GQLNameIdentifier
C.fromAutogeneratedName Name
Name.__neq) Maybe Description
forall a. Maybe a
Nothing (Parser k n (OpExpG b (UnpreparedValue b))
 -> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b))))
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ Bool -> UnpreparedValue b -> OpExpG b (UnpreparedValue b)
forall (backend :: BackendType) field.
Bool -> field -> OpExpG backend field
ANE Bool
True (UnpreparedValue b -> OpExpG b (UnpreparedValue b))
-> Parser k n (UnpreparedValue b)
-> Parser k n (OpExpG b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser k n (UnpreparedValue b)
valueParser,
    NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall (n :: * -> *) (k :: Kind) a.
(MonadParse n, 'Input <: k) =>
NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n a
-> InputFieldsParser n (Maybe a)
mkBoolOperator NamingCase
tCase DangerouslyCollapseBooleans
collapseIfNull (Name -> GQLNameIdentifier
C.fromAutogeneratedName Name
Name.__in) Maybe Description
forall a. Maybe a
Nothing (Parser k n (OpExpG b (UnpreparedValue b))
 -> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b))))
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ UnpreparedValue b -> OpExpG b (UnpreparedValue b)
forall (backend :: BackendType) field.
field -> OpExpG backend field
AIN (UnpreparedValue b -> OpExpG b (UnpreparedValue b))
-> Parser k n (UnpreparedValue b)
-> Parser k n (OpExpG b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser k n (UnpreparedValue b)
valueListParser,
    NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall (n :: * -> *) (k :: Kind) a.
(MonadParse n, 'Input <: k) =>
NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n a
-> InputFieldsParser n (Maybe a)
mkBoolOperator NamingCase
tCase DangerouslyCollapseBooleans
collapseIfNull (Name -> GQLNameIdentifier
C.fromAutogeneratedName Name
Name.__nin) Maybe Description
forall a. Maybe a
Nothing (Parser k n (OpExpG b (UnpreparedValue b))
 -> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b))))
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ UnpreparedValue b -> OpExpG b (UnpreparedValue b)
forall (backend :: BackendType) field.
field -> OpExpG backend field
ANIN (UnpreparedValue b -> OpExpG b (UnpreparedValue b))
-> Parser k n (UnpreparedValue b)
-> Parser k n (OpExpG b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser k n (UnpreparedValue b)
valueListParser
  ]

comparisonOperators ::
  (MonadParse n, 'Input P.<: k) =>
  NamingCase ->
  -- | shall this be collapsed to True when null is given?
  Options.DangerouslyCollapseBooleans ->
  -- | parser for one column value
  Parser k n (UnpreparedValue b) ->
  [InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))]
comparisonOperators :: NamingCase
-> DangerouslyCollapseBooleans
-> Parser k n (UnpreparedValue b)
-> [InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))]
comparisonOperators NamingCase
tCase DangerouslyCollapseBooleans
collapseIfNull Parser k n (UnpreparedValue b)
valueParser =
  [ NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall (n :: * -> *) (k :: Kind) a.
(MonadParse n, 'Input <: k) =>
NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n a
-> InputFieldsParser n (Maybe a)
mkBoolOperator NamingCase
tCase DangerouslyCollapseBooleans
collapseIfNull (Name -> GQLNameIdentifier
C.fromAutogeneratedName Name
Name.__gt) Maybe Description
forall a. Maybe a
Nothing (Parser k n (OpExpG b (UnpreparedValue b))
 -> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b))))
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ UnpreparedValue b -> OpExpG b (UnpreparedValue b)
forall (backend :: BackendType) field.
field -> OpExpG backend field
AGT (UnpreparedValue b -> OpExpG b (UnpreparedValue b))
-> Parser k n (UnpreparedValue b)
-> Parser k n (OpExpG b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser k n (UnpreparedValue b)
valueParser,
    NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall (n :: * -> *) (k :: Kind) a.
(MonadParse n, 'Input <: k) =>
NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n a
-> InputFieldsParser n (Maybe a)
mkBoolOperator NamingCase
tCase DangerouslyCollapseBooleans
collapseIfNull (Name -> GQLNameIdentifier
C.fromAutogeneratedName Name
Name.__lt) Maybe Description
forall a. Maybe a
Nothing (Parser k n (OpExpG b (UnpreparedValue b))
 -> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b))))
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ UnpreparedValue b -> OpExpG b (UnpreparedValue b)
forall (backend :: BackendType) field.
field -> OpExpG backend field
ALT (UnpreparedValue b -> OpExpG b (UnpreparedValue b))
-> Parser k n (UnpreparedValue b)
-> Parser k n (OpExpG b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser k n (UnpreparedValue b)
valueParser,
    NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall (n :: * -> *) (k :: Kind) a.
(MonadParse n, 'Input <: k) =>
NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n a
-> InputFieldsParser n (Maybe a)
mkBoolOperator NamingCase
tCase DangerouslyCollapseBooleans
collapseIfNull (Name -> GQLNameIdentifier
C.fromAutogeneratedName Name
Name.__gte) Maybe Description
forall a. Maybe a
Nothing (Parser k n (OpExpG b (UnpreparedValue b))
 -> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b))))
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ UnpreparedValue b -> OpExpG b (UnpreparedValue b)
forall (backend :: BackendType) field.
field -> OpExpG backend field
AGTE (UnpreparedValue b -> OpExpG b (UnpreparedValue b))
-> Parser k n (UnpreparedValue b)
-> Parser k n (OpExpG b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser k n (UnpreparedValue b)
valueParser,
    NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall (n :: * -> *) (k :: Kind) a.
(MonadParse n, 'Input <: k) =>
NamingCase
-> DangerouslyCollapseBooleans
-> GQLNameIdentifier
-> Maybe Description
-> Parser k n a
-> InputFieldsParser n (Maybe a)
mkBoolOperator NamingCase
tCase DangerouslyCollapseBooleans
collapseIfNull (Name -> GQLNameIdentifier
C.fromAutogeneratedName Name
Name.__lte) Maybe Description
forall a. Maybe a
Nothing (Parser k n (OpExpG b (UnpreparedValue b))
 -> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b))))
-> Parser k n (OpExpG b (UnpreparedValue b))
-> InputFieldsParser n (Maybe (OpExpG b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ UnpreparedValue b -> OpExpG b (UnpreparedValue b)
forall (backend :: BackendType) field.
field -> OpExpG backend field
ALTE (UnpreparedValue b -> OpExpG b (UnpreparedValue b))
-> Parser k n (UnpreparedValue b)
-> Parser k n (OpExpG b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser k n (UnpreparedValue b)
valueParser
  ]