refactor: Enable Aggregation Predicates IR for Postgres

This does not yet enable Aggregation Predicates to users, but enables building the execution backend and tests of the schema.

This is a prerequisite for:
* #5174
* #5261

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5607
GitOrigin-RevId: e07beb01949724545131629c111d41a7ec4636f2
This commit is contained in:
Philip Lykke Carlsen 2022-08-24 11:58:53 +00:00 committed by hasura-bot
parent 51ca899ddd
commit 29b6ea54c0
5 changed files with 63 additions and 2 deletions

View File

@ -1,4 +1,5 @@
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
@ -41,6 +42,7 @@ import Hasura.GraphQL.Schema.Backend
)
import Hasura.GraphQL.Schema.Backend qualified as BS
import Hasura.GraphQL.Schema.BoolExp
import Hasura.GraphQL.Schema.BoolExp.AggregationPredicates as Agg
import Hasura.GraphQL.Schema.Build qualified as GSB
import Hasura.GraphQL.Schema.Common
import Hasura.GraphQL.Schema.Mutation qualified as GSB
@ -83,6 +85,7 @@ import Hasura.SQL.Backend (BackendType (Postgres), PostgresKind (Citus, Cockroac
import Hasura.SQL.Tag (HasTag)
import Hasura.SQL.Types
import Language.GraphQL.Draft.Syntax qualified as G
import Language.GraphQL.Draft.Syntax.QQ qualified as G
----------------------------------------------------------------
-- BackendSchema instance
@ -137,6 +140,57 @@ instance PostgresSchema 'Cockroach where
-- postgres schema
-- Not implemented yet: Pending https://github.com/hasura/graphql-engine-mono/issues/5174"
instance AggregationPredicatesSchema ('Postgres pgKind) where
aggregationPredicatesParser _ _ = return Nothing
-- instance (BackendSchema ('Postgres pgKind)) => AggregationPredicatesSchema ('Postgres pgKind) where
-- aggregationPredicatesParser = Agg.defaultAggregationPredicatesParser aggregationFunctions
-- | The aggregation functions that are supported by postgres variants.
-- TODO: Add more.
_aggregationFunctions :: [Agg.FunctionSignature ('Postgres pgKind)]
_aggregationFunctions =
[ Agg.FunctionSignature
{ fnName = "count",
fnGQLName = [G.name|count|],
fnReturnType = PGInteger,
fnArguments = Agg.ArgumentsStar
},
Agg.FunctionSignature
{ fnName = "bool_and",
fnGQLName = [G.name|bool_and|],
fnReturnType = PGBoolean,
fnArguments =
Agg.Arguments
( NE.fromList
[ Agg.ArgumentSignature
{ argType = PGBoolean,
argName = [G.name|arg0|]
}
]
)
},
Agg.FunctionSignature
{ fnName = "corr",
fnGQLName = [G.name|corr|],
fnReturnType = PGDouble,
fnArguments =
Agg.Arguments
( NE.fromList
[ Agg.ArgumentSignature
{ argType = PGDouble,
argName = [G.name|Y|]
},
Agg.ArgumentSignature
{ argType = PGDouble,
argName = [G.name|X|]
}
]
)
}
]
instance
( PostgresSchema pgKind,
Backend ('Postgres pgKind),

View File

@ -6,7 +6,9 @@
module Hasura.Backends.Postgres.Instances.SchemaCache () where
import Hasura.RQL.Types.Backend
import Hasura.RQL.Types.SchemaCacheTypes (GetAggregationPredicatesDeps)
import Hasura.RQL.Types.SchemaCache.AggregationPredicates (defaultGetAggregationPredicateDeps)
import Hasura.RQL.Types.SchemaCacheTypes (GetAggregationPredicatesDeps (..))
import Hasura.SQL.Backend (BackendType (Postgres))
instance (Backend ('Postgres pgKind)) => GetAggregationPredicatesDeps ('Postgres pgKind)
instance (Backend ('Postgres pgKind)) => GetAggregationPredicatesDeps ('Postgres pgKind) where
getAggregationPredicateDeps = defaultGetAggregationPredicateDeps

View File

@ -25,6 +25,7 @@ import Hasura.Backends.Postgres.Types.Insert qualified as PG (BackendInsert)
import Hasura.Backends.Postgres.Types.Update qualified as PG
import Hasura.Base.Error
import Hasura.Prelude
import Hasura.RQL.IR.BoolExp.AggregationPredicates qualified as Agg
import Hasura.RQL.Types.Backend
import Hasura.SQL.Backend
import Hasura.SQL.Tag
@ -93,6 +94,8 @@ instance
type BackendUpdate ('Postgres pgKind) = PG.BackendUpdate pgKind
type AggregationPredicates ('Postgres pgKind) = Agg.AggregationPredicatesImplementation ('Postgres pgKind)
type ExtraTableMetadata ('Postgres pgKind) = PgExtraTableMetadata pgKind
type BackendInsert ('Postgres pgKind) = PG.BackendInsert pgKind

View File

@ -191,6 +191,7 @@ translateBoolExp = \case
mkComputedFieldFunctionExp currTableReference function sessionArgPresence $
Just $ S.toTableAlias aliasFunction
S.mkExists (S.FIFunc functionExp) <$> recCurrentTable (S.QualifiedIdentifier aliasFunction Nothing) be
AVAggregationPredicates _aggPreds -> error "Not implemented yet: Pending https://github.com/hasura/graphql-engine-mono/issues/5174"
where
mkQCol :: forall a. IsIdentifier a => S.Qual -> a -> S.SQLExp
mkQCol q = S.SEQIdentifier . S.QIdentifier q . toIdentifier

View File

@ -295,6 +295,7 @@ checkOnColExp spi sessVarBldr annFld = case annFld of
-- Including table permission filter; "input condition" AND "permission filter condition"
let finalBoolExp = andAnnBoolExps modBoolExp resolvedFltr
pure $ AVComputedField cfBoolExp {_acfbBoolExp = CFBETable table finalBoolExp}
AVAggregationPredicates {} -> throw400 NotExists "Aggregation Predicates cannot appear in permission checks"
convAnnBoolExpPartialSQL ::
(Applicative f) =>