2021-04-22 00:44:37 +03:00
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
2021-09-24 01:56:37 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2022-02-08 12:24:34 +03:00
|
|
|
-- | Postgres Instances Execute
|
|
|
|
--
|
|
|
|
-- This module implements the needed functionality for implementing a 'BackendExecute'
|
|
|
|
-- instance for Postgres, which defines an interface for translating a root field into
|
|
|
|
-- an execution plan and interacting with a database.
|
|
|
|
--
|
|
|
|
-- This module includes the Postgres implementation of queries, mutations, and more.
|
2021-05-21 05:46:58 +03:00
|
|
|
module Hasura.Backends.Postgres.Instances.Execute
|
2021-09-24 01:56:37 +03:00
|
|
|
( PreparedSql (..),
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Control.Monad.Trans.Control qualified as MT
|
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.HashMap.Strict qualified as Map
|
2021-10-29 17:42:07 +03:00
|
|
|
import Data.HashMap.Strict.InsOrd qualified as OMap
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.IntMap qualified as IntMap
|
|
|
|
import Data.Sequence qualified as Seq
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
import Database.PG.Query qualified as PG
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.Backends.Postgres.Connection.MonadTx
|
2023-01-25 10:12:53 +03:00
|
|
|
import Hasura.Backends.Postgres.Execute.ConnectionTemplate (QueryContext (..), QueryOperationType (..))
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Backends.Postgres.Execute.Insert (convertToSQLTransaction)
|
|
|
|
import Hasura.Backends.Postgres.Execute.Mutation qualified as PGE
|
|
|
|
import Hasura.Backends.Postgres.Execute.Prepare
|
|
|
|
( PlanningSt (..),
|
|
|
|
PrepArgMap,
|
|
|
|
initPlanningSt,
|
|
|
|
prepareWithPlan,
|
|
|
|
prepareWithoutPlan,
|
|
|
|
withUserVars,
|
|
|
|
)
|
2022-04-07 17:41:43 +03:00
|
|
|
import Hasura.Backends.Postgres.Execute.Subscription qualified as PGL
|
2023-01-25 10:12:53 +03:00
|
|
|
import Hasura.Backends.Postgres.Execute.Types
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.DML qualified as S
|
2022-09-21 14:34:39 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.Types qualified as Postgres
|
|
|
|
import Hasura.Backends.Postgres.SQL.Value qualified as Postgres
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.Select (PostgresAnnotatedFieldJSON)
|
|
|
|
import Hasura.Backends.Postgres.Translate.Select qualified as DS
|
2022-09-21 14:34:39 +03:00
|
|
|
import Hasura.Backends.Postgres.Types.Function qualified as Postgres
|
2023-01-10 04:54:40 +03:00
|
|
|
import Hasura.Backends.Postgres.Types.Update qualified as Postgres
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Base.Error (QErr)
|
2022-09-05 18:03:18 +03:00
|
|
|
import Hasura.EncJSON (EncJSON, encJFromJValue)
|
2023-04-03 13:18:54 +03:00
|
|
|
import Hasura.Function.Cache
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.GraphQL.Execute.Backend
|
|
|
|
( BackendExecute (..),
|
|
|
|
DBStepInfo (..),
|
|
|
|
ExplainPlan (..),
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
OnBaseMonad (..),
|
2021-09-24 01:56:37 +03:00
|
|
|
convertRemoteSourceRelationship,
|
2023-03-14 14:32:20 +03:00
|
|
|
withNoStatistics,
|
2021-09-24 01:56:37 +03:00
|
|
|
)
|
2022-03-21 13:39:49 +03:00
|
|
|
import Hasura.GraphQL.Execute.Subscription.Plan
|
2022-12-22 20:08:04 +03:00
|
|
|
( CohortId,
|
|
|
|
CohortVariables,
|
|
|
|
ParameterizedSubscriptionQueryPlan (..),
|
2022-03-21 13:39:49 +03:00
|
|
|
SubscriptionQueryPlan (..),
|
|
|
|
SubscriptionQueryPlanExplanation (..),
|
2021-09-24 01:56:37 +03:00
|
|
|
mkCohortVariables,
|
|
|
|
newCohortId,
|
|
|
|
)
|
2021-10-29 17:42:07 +03:00
|
|
|
import Hasura.GraphQL.Namespace
|
|
|
|
( RootFieldAlias (..),
|
|
|
|
RootFieldMap,
|
|
|
|
)
|
2022-04-22 22:53:12 +03:00
|
|
|
import Hasura.GraphQL.Namespace qualified as G
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.QueryTags
|
|
|
|
( QueryTagsComment (..),
|
|
|
|
emptyQueryTagsComment,
|
|
|
|
)
|
2022-05-31 01:07:02 +03:00
|
|
|
import Hasura.RQL.IR
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.RQL.IR.Delete qualified as IR
|
|
|
|
import Hasura.RQL.IR.Insert qualified as IR
|
|
|
|
import Hasura.RQL.IR.Returning qualified as IR
|
|
|
|
import Hasura.RQL.IR.Select qualified as IR
|
|
|
|
import Hasura.RQL.IR.Update qualified as IR
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Backend
|
2023-04-24 21:35:48 +03:00
|
|
|
import Hasura.RQL.Types.BackendType
|
2022-04-07 17:41:43 +03:00
|
|
|
import Hasura.RQL.Types.Column
|
2022-04-22 22:53:12 +03:00
|
|
|
( ColumnType (..),
|
2022-04-07 17:41:43 +03:00
|
|
|
ColumnValue (..),
|
2022-04-27 16:57:28 +03:00
|
|
|
ciName,
|
2022-04-07 17:41:43 +03:00
|
|
|
)
|
2022-04-22 22:53:12 +03:00
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
( FieldName (..),
|
|
|
|
JsonAggSelect (..),
|
|
|
|
SourceName,
|
|
|
|
)
|
2023-04-24 18:17:15 +03:00
|
|
|
import Hasura.RQL.Types.Schema.Options qualified as Options
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.SQL.AnyBackend qualified as AB
|
|
|
|
import Hasura.Session (UserInfo (..))
|
|
|
|
import Hasura.Tracing qualified as Tracing
|
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
2023-01-25 10:12:53 +03:00
|
|
|
import Network.HTTP.Types qualified as HTTP
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|
|
|
data PreparedSql = PreparedSql
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
{ _psQuery :: PG.Query,
|
2022-07-29 17:05:03 +03:00
|
|
|
_psPrepArgs :: PrepArgMap
|
2021-06-11 06:26:50 +03:00
|
|
|
}
|
2023-01-04 11:28:19 +03:00
|
|
|
deriving (Show)
|
2021-06-11 06:26:50 +03:00
|
|
|
|
2021-05-21 05:46:58 +03:00
|
|
|
instance
|
2021-09-24 01:56:37 +03:00
|
|
|
( Backend ('Postgres pgKind),
|
|
|
|
PostgresAnnotatedFieldJSON pgKind
|
|
|
|
) =>
|
|
|
|
BackendExecute ('Postgres pgKind)
|
|
|
|
where
|
|
|
|
type PreparedQuery ('Postgres pgKind) = PreparedSql
|
2021-04-22 00:44:37 +03:00
|
|
|
type MultiplexedQuery ('Postgres pgKind) = PGL.MultiplexedQuery
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
type ExecutionMonad ('Postgres pgKind) = PG.TxET QErr
|
2021-02-12 06:04:09 +03:00
|
|
|
|
|
|
|
mkDBQueryPlan = pgDBQueryPlan
|
|
|
|
mkDBMutationPlan = pgDBMutationPlan
|
2022-04-07 17:41:43 +03:00
|
|
|
mkLiveQuerySubscriptionPlan = pgDBLiveQuerySubscriptionPlan
|
|
|
|
mkDBStreamingSubscriptionPlan = pgDBStreamingSubscriptionPlan
|
2021-04-13 14:10:08 +03:00
|
|
|
mkDBQueryExplain = pgDBQueryExplain
|
2022-03-21 13:39:49 +03:00
|
|
|
mkSubscriptionExplain = pgDBSubscriptionExplain
|
2021-09-22 13:43:05 +03:00
|
|
|
mkDBRemoteRelationshipPlan = pgDBRemoteRelationshipPlan
|
2021-02-12 06:04:09 +03:00
|
|
|
|
|
|
|
-- query
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
pgDBQueryPlan ::
|
|
|
|
forall pgKind m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
Backend ('Postgres pgKind),
|
|
|
|
PostgresAnnotatedFieldJSON pgKind,
|
|
|
|
MonadReader QueryTagsComment m
|
|
|
|
) =>
|
|
|
|
UserInfo ->
|
|
|
|
SourceName ->
|
|
|
|
SourceConfig ('Postgres pgKind) ->
|
2021-12-07 16:12:02 +03:00
|
|
|
QueryDB ('Postgres pgKind) Void (UnpreparedValue ('Postgres pgKind)) ->
|
2023-01-25 10:12:53 +03:00
|
|
|
[HTTP.Header] ->
|
|
|
|
Maybe G.Name ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m (DBStepInfo ('Postgres pgKind))
|
2023-04-13 04:29:15 +03:00
|
|
|
pgDBQueryPlan userInfo sourceName sourceConfig qrf reqHeaders operationName = do
|
2023-02-03 14:15:08 +03:00
|
|
|
(preparedQuery, PlanningSt {_psPrepped = planVals}) <-
|
2021-07-31 00:41:52 +03:00
|
|
|
flip runStateT initPlanningSt $ traverse (prepareWithPlan userInfo) qrf
|
2023-02-01 11:44:50 +03:00
|
|
|
|
2021-09-23 15:37:56 +03:00
|
|
|
queryTagsComment <- ask
|
2023-01-25 10:12:53 +03:00
|
|
|
resolvedConnectionTemplate <-
|
2023-03-21 00:54:55 +03:00
|
|
|
let connectionTemplateResolver =
|
|
|
|
connectionTemplateConfigResolver (_pscConnectionTemplateConfig sourceConfig)
|
|
|
|
queryContext =
|
|
|
|
Just $
|
|
|
|
QueryContext operationName $
|
|
|
|
QueryOperationType G.OperationTypeQuery
|
|
|
|
in applyConnectionTemplateResolverNonAdmin connectionTemplateResolver userInfo reqHeaders queryContext
|
2021-09-23 15:37:56 +03:00
|
|
|
let preparedSQLWithQueryTags = appendPreparedSQLWithQueryTags (irToRootFieldPlan planVals preparedQuery) queryTagsComment
|
2021-07-29 11:29:12 +03:00
|
|
|
let (action, preparedSQL) = mkCurPlanTx userInfo preparedSQLWithQueryTags
|
2023-03-14 14:32:20 +03:00
|
|
|
|
|
|
|
pure $ DBStepInfo @('Postgres pgKind) sourceName sourceConfig preparedSQL (fmap withNoStatistics action) resolvedConnectionTemplate
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
pgDBQueryExplain ::
|
|
|
|
forall pgKind m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
Backend ('Postgres pgKind),
|
|
|
|
PostgresAnnotatedFieldJSON pgKind
|
|
|
|
) =>
|
2021-10-29 17:42:07 +03:00
|
|
|
RootFieldAlias ->
|
2021-09-24 01:56:37 +03:00
|
|
|
UserInfo ->
|
|
|
|
SourceName ->
|
|
|
|
SourceConfig ('Postgres pgKind) ->
|
2021-12-07 16:12:02 +03:00
|
|
|
QueryDB ('Postgres pgKind) Void (UnpreparedValue ('Postgres pgKind)) ->
|
2023-01-25 10:12:53 +03:00
|
|
|
[HTTP.Header] ->
|
|
|
|
Maybe G.Name ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m (AB.AnyBackend DBStepInfo)
|
2023-01-25 10:12:53 +03:00
|
|
|
pgDBQueryExplain fieldName userInfo sourceName sourceConfig rootSelection reqHeaders operationName = do
|
2022-06-15 21:15:51 +03:00
|
|
|
preparedQuery <- traverse (prepareWithoutPlan userInfo) rootSelection
|
2021-06-11 06:26:50 +03:00
|
|
|
let PreparedSql querySQL _ = irToRootFieldPlan mempty preparedQuery
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
textSQL = PG.getQueryText querySQL
|
2021-04-13 14:10:08 +03:00
|
|
|
-- CAREFUL!: an `EXPLAIN ANALYZE` here would actually *execute* this
|
|
|
|
-- query, maybe resulting in privilege escalation:
|
2022-10-18 19:47:41 +03:00
|
|
|
withExplain = "EXPLAIN " <> textSQL
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
let action = OnBaseMonad do
|
|
|
|
PG.withQE dmlTxErrorHandler (PG.fromText withExplain) () True <&> \planList ->
|
2023-03-14 14:32:20 +03:00
|
|
|
withNoStatistics $ encJFromJValue $ ExplainPlan fieldName (Just textSQL) (Just $ map runIdentity planList)
|
2023-01-25 10:12:53 +03:00
|
|
|
resolvedConnectionTemplate <-
|
2023-03-21 00:54:55 +03:00
|
|
|
let connectionTemplateResolver =
|
|
|
|
connectionTemplateConfigResolver (_pscConnectionTemplateConfig sourceConfig)
|
|
|
|
queryContext =
|
|
|
|
Just $
|
|
|
|
QueryContext operationName $
|
|
|
|
QueryOperationType G.OperationTypeQuery
|
|
|
|
in applyConnectionTemplateResolverNonAdmin connectionTemplateResolver userInfo reqHeaders queryContext
|
2021-09-24 01:56:37 +03:00
|
|
|
pure $
|
|
|
|
AB.mkAnyBackend $
|
2023-01-25 10:12:53 +03:00
|
|
|
DBStepInfo @('Postgres pgKind) sourceName sourceConfig Nothing action resolvedConnectionTemplate
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2022-03-21 13:39:49 +03:00
|
|
|
pgDBSubscriptionExplain ::
|
2021-09-24 01:56:37 +03:00
|
|
|
( MonadError QErr m,
|
|
|
|
MonadIO m,
|
|
|
|
MT.MonadBaseControl IO m
|
|
|
|
) =>
|
2022-03-21 13:39:49 +03:00
|
|
|
SubscriptionQueryPlan ('Postgres pgKind) (MultiplexedQuery ('Postgres pgKind)) ->
|
|
|
|
m SubscriptionQueryPlanExplanation
|
|
|
|
pgDBSubscriptionExplain plan = do
|
|
|
|
let parameterizedPlan = _sqpParameterizedPlan plan
|
|
|
|
pgExecCtx = _pscExecCtx $ _sqpSourceConfig plan
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
queryText = PG.getQueryText . PGL.unMultiplexedQuery $ _plqpQuery parameterizedPlan
|
2021-04-13 14:10:08 +03:00
|
|
|
-- CAREFUL!: an `EXPLAIN ANALYZE` here would actually *execute* this
|
|
|
|
-- query, maybe resulting in privilege escalation:
|
2022-10-18 19:47:41 +03:00
|
|
|
explainQuery = PG.fromText $ "EXPLAIN " <> queryText
|
2023-01-25 10:12:53 +03:00
|
|
|
resolvedConnectionTemplate = _sqpResolvedConnectionTemplate plan
|
2021-04-13 14:10:08 +03:00
|
|
|
cohortId <- newCohortId
|
2021-09-24 01:56:37 +03:00
|
|
|
explanationLines <-
|
|
|
|
liftEitherM $
|
|
|
|
runExceptT $
|
2023-01-25 10:12:53 +03:00
|
|
|
_pecRunTx pgExecCtx (PGExecCtxInfo (Tx PG.ReadOnly Nothing) (GraphQLQuery resolvedConnectionTemplate)) $
|
2022-03-21 13:39:49 +03:00
|
|
|
map runIdentity <$> PGL.executeQuery explainQuery [(cohortId, _sqpVariables plan)]
|
|
|
|
pure $ SubscriptionQueryPlanExplanation queryText explanationLines $ _sqpVariables plan
|
2021-02-12 06:04:09 +03:00
|
|
|
|
|
|
|
-- mutation
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
convertDelete ::
|
|
|
|
forall pgKind m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
Backend ('Postgres pgKind),
|
2022-04-28 22:33:33 +03:00
|
|
|
PostgresAnnotatedFieldJSON pgKind,
|
|
|
|
MonadReader QueryTagsComment m
|
2021-09-24 01:56:37 +03:00
|
|
|
) =>
|
|
|
|
UserInfo ->
|
2021-12-07 16:12:02 +03:00
|
|
|
IR.AnnDelG ('Postgres pgKind) Void (UnpreparedValue ('Postgres pgKind)) ->
|
2022-07-14 20:57:28 +03:00
|
|
|
Options.StringifyNumbers ->
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
m (OnBaseMonad (PG.TxET QErr) EncJSON)
|
2022-04-28 22:33:33 +03:00
|
|
|
convertDelete userInfo deleteOperation stringifyNum = do
|
|
|
|
queryTags <- ask
|
2021-07-31 00:41:52 +03:00
|
|
|
preparedDelete <- traverse (prepareWithoutPlan userInfo) deleteOperation
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
pure $
|
|
|
|
OnBaseMonad $
|
|
|
|
flip runReaderT queryTags $
|
|
|
|
PGE.execDeleteQuery stringifyNum (_adNamingConvention deleteOperation) userInfo (preparedDelete, Seq.empty)
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
convertUpdate ::
|
|
|
|
forall pgKind m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
Backend ('Postgres pgKind),
|
2022-04-28 22:33:33 +03:00
|
|
|
PostgresAnnotatedFieldJSON pgKind,
|
|
|
|
MonadReader QueryTagsComment m
|
2021-09-24 01:56:37 +03:00
|
|
|
) =>
|
|
|
|
UserInfo ->
|
2021-12-07 16:12:02 +03:00
|
|
|
IR.AnnotatedUpdateG ('Postgres pgKind) Void (UnpreparedValue ('Postgres pgKind)) ->
|
2022-07-14 20:57:28 +03:00
|
|
|
Options.StringifyNumbers ->
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
m (OnBaseMonad (PG.TxET QErr) EncJSON)
|
2022-04-28 22:33:33 +03:00
|
|
|
convertUpdate userInfo updateOperation stringifyNum = do
|
|
|
|
queryTags <- ask
|
2021-07-31 00:41:52 +03:00
|
|
|
preparedUpdate <- traverse (prepareWithoutPlan userInfo) updateOperation
|
2023-01-10 04:54:40 +03:00
|
|
|
if Postgres.updateVariantIsEmpty $ IR._auUpdateVariant updateOperation
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
then pure $ OnBaseMonad $ pure $ IR.buildEmptyMutResp $ IR._auOutput preparedUpdate
|
2021-09-24 01:56:37 +03:00
|
|
|
else
|
|
|
|
pure $
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
OnBaseMonad $
|
|
|
|
flip runReaderT queryTags $
|
|
|
|
PGE.execUpdateQuery stringifyNum (_auNamingConvention updateOperation) userInfo (preparedUpdate, Seq.empty)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|
|
|
convertInsert ::
|
|
|
|
forall pgKind m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
Backend ('Postgres pgKind),
|
2022-04-28 22:33:33 +03:00
|
|
|
PostgresAnnotatedFieldJSON pgKind,
|
|
|
|
MonadReader QueryTagsComment m
|
2021-09-24 01:56:37 +03:00
|
|
|
) =>
|
|
|
|
UserInfo ->
|
2022-04-01 09:43:05 +03:00
|
|
|
IR.AnnotatedInsert ('Postgres pgKind) Void (UnpreparedValue ('Postgres pgKind)) ->
|
2022-07-14 20:57:28 +03:00
|
|
|
Options.StringifyNumbers ->
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
m (OnBaseMonad (PG.TxET QErr) EncJSON)
|
2022-04-28 22:33:33 +03:00
|
|
|
convertInsert userInfo insertOperation stringifyNum = do
|
|
|
|
queryTags <- ask
|
2021-07-31 00:41:52 +03:00
|
|
|
preparedInsert <- traverse (prepareWithoutPlan userInfo) insertOperation
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
pure $
|
|
|
|
OnBaseMonad $
|
|
|
|
flip runReaderT queryTags $
|
|
|
|
convertToSQLTransaction preparedInsert userInfo Seq.empty stringifyNum (_aiNamingConvention insertOperation)
|
2021-02-12 06:04:09 +03:00
|
|
|
|
|
|
|
-- | A pared-down version of 'Query.convertQuerySelSet', for use in execution of
|
|
|
|
-- special case of SQL function mutations (see 'MDBFunction').
|
2021-09-24 01:56:37 +03:00
|
|
|
convertFunction ::
|
|
|
|
forall pgKind m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
Backend ('Postgres pgKind),
|
2022-04-28 22:33:33 +03:00
|
|
|
PostgresAnnotatedFieldJSON pgKind,
|
|
|
|
MonadReader QueryTagsComment m
|
2021-09-24 01:56:37 +03:00
|
|
|
) =>
|
|
|
|
UserInfo ->
|
|
|
|
JsonAggSelect ->
|
|
|
|
-- | VOLATILE function as 'SelectExp'
|
2021-12-07 16:12:02 +03:00
|
|
|
IR.AnnSimpleSelectG ('Postgres pgKind) Void (UnpreparedValue ('Postgres pgKind)) ->
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
m (OnBaseMonad (PG.TxET QErr) EncJSON)
|
2022-04-28 22:33:33 +03:00
|
|
|
convertFunction userInfo jsonAggSelect unpreparedQuery = do
|
|
|
|
queryTags <- ask
|
2021-02-12 06:04:09 +03:00
|
|
|
-- Transform the RQL AST into a prepared SQL query
|
2023-02-03 14:15:08 +03:00
|
|
|
(preparedQuery, PlanningSt {_psPrepped = planVals}) <-
|
2021-09-24 01:56:37 +03:00
|
|
|
flip runStateT initPlanningSt $
|
|
|
|
traverse (prepareWithPlan userInfo) unpreparedQuery
|
2021-02-12 06:04:09 +03:00
|
|
|
let queryResultFn =
|
|
|
|
case jsonAggSelect of
|
|
|
|
JASMultipleRows -> QDBMultipleRows
|
|
|
|
JASSingleObject -> QDBSingleRow
|
2021-07-29 11:29:12 +03:00
|
|
|
let preparedSQLWithQueryTags = appendPreparedSQLWithQueryTags (irToRootFieldPlan planVals $ queryResultFn preparedQuery) queryTags
|
2022-11-02 23:53:23 +03:00
|
|
|
pure $!
|
|
|
|
fst $
|
|
|
|
mkCurPlanTx userInfo preparedSQLWithQueryTags -- forget (Maybe PreparedSql)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|
|
|
pgDBMutationPlan ::
|
|
|
|
forall pgKind m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
Backend ('Postgres pgKind),
|
|
|
|
PostgresAnnotatedFieldJSON pgKind,
|
|
|
|
MonadReader QueryTagsComment m
|
|
|
|
) =>
|
|
|
|
UserInfo ->
|
2022-07-14 20:57:28 +03:00
|
|
|
Options.StringifyNumbers ->
|
2021-09-24 01:56:37 +03:00
|
|
|
SourceName ->
|
|
|
|
SourceConfig ('Postgres pgKind) ->
|
2021-12-07 16:12:02 +03:00
|
|
|
MutationDB ('Postgres pgKind) Void (UnpreparedValue ('Postgres pgKind)) ->
|
2023-01-25 10:12:53 +03:00
|
|
|
[HTTP.Header] ->
|
|
|
|
Maybe G.Name ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m (DBStepInfo ('Postgres pgKind))
|
2023-04-13 04:29:15 +03:00
|
|
|
pgDBMutationPlan userInfo stringifyNum sourceName sourceConfig mrf reqHeaders operationName = do
|
2023-01-25 10:12:53 +03:00
|
|
|
resolvedConnectionTemplate <-
|
2023-03-21 00:54:55 +03:00
|
|
|
let connectionTemplateResolver =
|
|
|
|
connectionTemplateConfigResolver (_pscConnectionTemplateConfig sourceConfig)
|
|
|
|
queryContext =
|
|
|
|
Just $
|
|
|
|
QueryContext operationName $
|
|
|
|
QueryOperationType G.OperationTypeMutation
|
|
|
|
in applyConnectionTemplateResolverNonAdmin connectionTemplateResolver userInfo reqHeaders queryContext
|
2023-01-25 10:12:53 +03:00
|
|
|
go resolvedConnectionTemplate <$> case mrf of
|
2022-04-28 22:33:33 +03:00
|
|
|
MDBInsert s -> convertInsert userInfo s stringifyNum
|
|
|
|
MDBUpdate s -> convertUpdate userInfo s stringifyNum
|
|
|
|
MDBDelete s -> convertDelete userInfo s stringifyNum
|
|
|
|
MDBFunction returnsSet s -> convertFunction userInfo returnsSet s
|
2021-02-12 06:04:09 +03:00
|
|
|
where
|
2023-03-14 14:32:20 +03:00
|
|
|
go resolvedConnectionTemplate v =
|
|
|
|
DBStepInfo
|
|
|
|
{ dbsiSourceName = sourceName,
|
|
|
|
dbsiSourceConfig = sourceConfig,
|
|
|
|
dbsiPreparedQuery = Nothing,
|
|
|
|
dbsiAction = fmap withNoStatistics v,
|
|
|
|
dbsiResolvedConnectionTemplate = resolvedConnectionTemplate
|
|
|
|
}
|
2021-04-01 23:40:31 +03:00
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
-- subscription
|
2021-02-20 16:45:49 +03:00
|
|
|
|
2022-04-07 17:41:43 +03:00
|
|
|
pgDBLiveQuerySubscriptionPlan ::
|
2021-09-24 01:56:37 +03:00
|
|
|
forall pgKind m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
MonadIO m,
|
|
|
|
Backend ('Postgres pgKind),
|
|
|
|
PostgresAnnotatedFieldJSON pgKind,
|
|
|
|
MonadReader QueryTagsComment m
|
|
|
|
) =>
|
|
|
|
UserInfo ->
|
|
|
|
SourceName ->
|
|
|
|
SourceConfig ('Postgres pgKind) ->
|
2021-10-29 17:42:07 +03:00
|
|
|
Maybe G.Name ->
|
2021-12-07 16:12:02 +03:00
|
|
|
RootFieldMap (QueryDB ('Postgres pgKind) Void (UnpreparedValue ('Postgres pgKind))) ->
|
2023-01-25 10:12:53 +03:00
|
|
|
[HTTP.Header] ->
|
|
|
|
Maybe G.Name ->
|
2022-03-21 13:39:49 +03:00
|
|
|
m (SubscriptionQueryPlan ('Postgres pgKind) (MultiplexedQuery ('Postgres pgKind)))
|
2023-01-25 10:12:53 +03:00
|
|
|
pgDBLiveQuerySubscriptionPlan userInfo _sourceName sourceConfig namespace unpreparedAST reqHeaders operationName = do
|
2021-09-24 01:56:37 +03:00
|
|
|
(preparedAST, PGL.QueryParametersInfo {..}) <-
|
|
|
|
flip runStateT mempty $
|
2022-11-02 23:53:23 +03:00
|
|
|
for unpreparedAST $
|
|
|
|
traverse (PGL.resolveMultiplexedValue (_uiSession userInfo))
|
2022-04-07 17:41:43 +03:00
|
|
|
subscriptionQueryTagsComment <- ask
|
2021-10-29 17:42:07 +03:00
|
|
|
let multiplexedQuery = PGL.mkMultiplexedQuery $ OMap.mapKeys _rfaAlias preparedAST
|
2021-09-23 15:37:56 +03:00
|
|
|
multiplexedQueryWithQueryTags =
|
2022-04-07 17:41:43 +03:00
|
|
|
multiplexedQuery {PGL.unMultiplexedQuery = appendSQLWithQueryTags (PGL.unMultiplexedQuery multiplexedQuery) subscriptionQueryTagsComment}
|
2021-02-20 16:45:49 +03:00
|
|
|
roleName = _uiRole userInfo
|
2022-03-21 13:39:49 +03:00
|
|
|
parameterizedPlan = ParameterizedSubscriptionQueryPlan roleName multiplexedQueryWithQueryTags
|
2021-02-20 16:45:49 +03:00
|
|
|
|
2023-01-25 10:12:53 +03:00
|
|
|
resolvedConnectionTemplate <-
|
2023-03-21 00:54:55 +03:00
|
|
|
let connectionTemplateResolver =
|
|
|
|
connectionTemplateConfigResolver (_pscConnectionTemplateConfig sourceConfig)
|
|
|
|
queryContext =
|
|
|
|
Just $
|
|
|
|
QueryContext operationName $
|
|
|
|
QueryOperationType G.OperationTypeSubscription
|
|
|
|
in applyConnectionTemplateResolverNonAdmin connectionTemplateResolver userInfo reqHeaders queryContext
|
2023-01-25 10:12:53 +03:00
|
|
|
|
2022-12-22 20:08:04 +03:00
|
|
|
-- Cohort Id: Used for validating the multiplexed query. See @'testMultiplexedQueryTx'.
|
|
|
|
-- It is disposed when the subscriber is added to existing cohort.
|
|
|
|
cohortId <- newCohortId
|
|
|
|
|
2023-01-25 10:12:53 +03:00
|
|
|
let pgExecCtxInfo = PGExecCtxInfo (Tx PG.ReadOnly Nothing) (GraphQLQuery resolvedConnectionTemplate)
|
|
|
|
cohortVariables <- liftEitherM $ liftIO $ runExceptT $ _pecRunTx (_pscExecCtx sourceConfig) pgExecCtxInfo do
|
2022-12-22 20:08:04 +03:00
|
|
|
-- We need to ensure that the values provided for variables are correct according to Postgres.
|
|
|
|
-- Without this check an invalid value for a variable for one instance of the subscription will
|
|
|
|
-- take down the entire multiplexed query.
|
|
|
|
validatedQueryVars <- PGL.validateVariablesTx _qpiReusableVariableValues
|
|
|
|
validatedSyntheticVars <- PGL.validateVariablesTx $ toList _qpiSyntheticVariableValues
|
|
|
|
let cohortVariables =
|
|
|
|
mkCohortVariables
|
|
|
|
_qpiReferencedSessionVariables
|
|
|
|
(_uiSession userInfo)
|
|
|
|
validatedQueryVars
|
|
|
|
validatedSyntheticVars
|
|
|
|
mempty -- live query subscriptions don't use the streaming cursor variables
|
|
|
|
|
|
|
|
-- Test the multiplexed query. Without this test if the query fails, the subscription will
|
|
|
|
-- take down the entier multiplexed query affecting all subscribers.
|
|
|
|
testMultiplexedQueryTx multiplexedQueryWithQueryTags cohortId cohortVariables
|
|
|
|
pure cohortVariables
|
2021-02-20 16:45:49 +03:00
|
|
|
|
2023-01-25 10:12:53 +03:00
|
|
|
pure $ SubscriptionQueryPlan parameterizedPlan sourceConfig cohortId resolvedConnectionTemplate cohortVariables namespace
|
2021-04-22 00:44:37 +03:00
|
|
|
|
2022-04-07 17:41:43 +03:00
|
|
|
pgDBStreamingSubscriptionPlan ::
|
|
|
|
forall pgKind m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
MonadIO m,
|
|
|
|
Backend ('Postgres pgKind),
|
|
|
|
PostgresAnnotatedFieldJSON pgKind,
|
|
|
|
MonadReader QueryTagsComment m
|
|
|
|
) =>
|
|
|
|
UserInfo ->
|
|
|
|
SourceName ->
|
|
|
|
SourceConfig ('Postgres pgKind) ->
|
|
|
|
(RootFieldAlias, (QueryDB ('Postgres pgKind) Void (UnpreparedValue ('Postgres pgKind)))) ->
|
2023-01-25 10:12:53 +03:00
|
|
|
[HTTP.Header] ->
|
|
|
|
Maybe G.Name ->
|
2022-04-07 17:41:43 +03:00
|
|
|
m (SubscriptionQueryPlan ('Postgres pgKind) (MultiplexedQuery ('Postgres pgKind)))
|
2023-01-25 10:12:53 +03:00
|
|
|
pgDBStreamingSubscriptionPlan userInfo _sourceName sourceConfig (rootFieldAlias, unpreparedAST) reqHeaders operationName = do
|
2022-04-07 17:41:43 +03:00
|
|
|
(preparedAST, PGL.QueryParametersInfo {..}) <-
|
|
|
|
flip runStateT mempty $
|
|
|
|
traverse (PGL.resolveMultiplexedValue (_uiSession userInfo)) unpreparedAST
|
|
|
|
subscriptionQueryTagsComment <- ask
|
2022-04-22 22:53:12 +03:00
|
|
|
let multiplexedQuery = PGL.mkStreamingMultiplexedQuery (G._rfaAlias rootFieldAlias, preparedAST)
|
2022-04-07 17:41:43 +03:00
|
|
|
multiplexedQueryWithQueryTags =
|
|
|
|
multiplexedQuery {PGL.unMultiplexedQuery = appendSQLWithQueryTags (PGL.unMultiplexedQuery multiplexedQuery) subscriptionQueryTagsComment}
|
|
|
|
roleName = _uiRole userInfo
|
|
|
|
parameterizedPlan = ParameterizedSubscriptionQueryPlan roleName multiplexedQueryWithQueryTags
|
|
|
|
|
2023-01-25 10:12:53 +03:00
|
|
|
resolvedConnectionTemplate <-
|
2023-03-21 00:54:55 +03:00
|
|
|
let connectionTemplateResolver =
|
|
|
|
connectionTemplateConfigResolver (_pscConnectionTemplateConfig sourceConfig)
|
|
|
|
queryContext =
|
|
|
|
Just $
|
|
|
|
QueryContext operationName $
|
|
|
|
QueryOperationType G.OperationTypeSubscription
|
|
|
|
in applyConnectionTemplateResolverNonAdmin connectionTemplateResolver userInfo reqHeaders queryContext
|
2023-01-25 10:12:53 +03:00
|
|
|
|
2022-12-22 20:08:04 +03:00
|
|
|
-- Cohort Id: Used for validating the multiplexed query. See @'testMultiplexedQueryTx'.
|
|
|
|
-- It is disposed when the subscriber is added to existing cohort.
|
|
|
|
cohortId <- newCohortId
|
|
|
|
|
2023-01-25 10:12:53 +03:00
|
|
|
let pgExecCtxInfo = PGExecCtxInfo (Tx PG.ReadOnly Nothing) (GraphQLQuery resolvedConnectionTemplate)
|
|
|
|
cohortVariables <- liftEitherM $ liftIO $ runExceptT $ _pecRunTx (_pscExecCtx sourceConfig) pgExecCtxInfo do
|
2022-12-22 20:08:04 +03:00
|
|
|
-- We need to ensure that the values provided for variables are correct according to Postgres.
|
|
|
|
-- Without this check an invalid value for a variable for one instance of the subscription will
|
|
|
|
-- take down the entire multiplexed query.
|
|
|
|
validatedQueryVars <- PGL.validateVariablesTx _qpiReusableVariableValues
|
|
|
|
validatedSyntheticVars <- PGL.validateVariablesTx $ toList _qpiSyntheticVariableValues
|
|
|
|
validatedCursorVars <- PGL.validateVariablesTx $ getCursorVars unpreparedAST
|
|
|
|
let cohortVariables =
|
|
|
|
mkCohortVariables
|
|
|
|
_qpiReferencedSessionVariables
|
|
|
|
(_uiSession userInfo)
|
|
|
|
validatedQueryVars
|
|
|
|
validatedSyntheticVars
|
|
|
|
validatedCursorVars
|
2022-04-07 17:41:43 +03:00
|
|
|
|
2022-12-22 20:08:04 +03:00
|
|
|
-- Test the multiplexed query. Without this test if the query fails, the subscription will
|
|
|
|
-- take down the entier multiplexed query affecting all subscribers.
|
|
|
|
testMultiplexedQueryTx multiplexedQueryWithQueryTags cohortId cohortVariables
|
|
|
|
pure cohortVariables
|
2022-04-07 17:41:43 +03:00
|
|
|
|
2023-01-25 10:12:53 +03:00
|
|
|
pure $ SubscriptionQueryPlan parameterizedPlan sourceConfig cohortId resolvedConnectionTemplate cohortVariables $ _rfaNamespace rootFieldAlias
|
2022-04-07 17:41:43 +03:00
|
|
|
where
|
|
|
|
getCursorVars qdb =
|
|
|
|
case qdb of
|
|
|
|
QDBStreamMultipleRows (IR.AnnSelectStreamG () _ _ _ args _) ->
|
|
|
|
let cursorArg = IR._ssaCursorArg args
|
|
|
|
colInfo = IR._sciColInfo cursorArg
|
|
|
|
in Map.singleton (ciName colInfo) (IR._sciInitialValue cursorArg)
|
|
|
|
_ -> mempty
|
|
|
|
|
2022-12-22 20:08:04 +03:00
|
|
|
-- | Test a multiplexed query in a transaction.
|
|
|
|
testMultiplexedQueryTx ::
|
|
|
|
(MonadTx m) =>
|
|
|
|
PGL.MultiplexedQuery ->
|
|
|
|
CohortId ->
|
|
|
|
CohortVariables ->
|
|
|
|
m ()
|
|
|
|
testMultiplexedQueryTx (PGL.MultiplexedQuery query) cohortId cohortVariables = do
|
|
|
|
-- Run the query and discard the results
|
|
|
|
-- NOTE: Adding `LIMIT 1` to the root selection of the query would make
|
|
|
|
-- executing the query faster. However, it is not preferred due to the following
|
|
|
|
-- reasons:
|
|
|
|
-- Multiplex query validation is required for queries involving any SQL functions,
|
|
|
|
-- computed fields and SQL functions as root fields, as the functions are bound to
|
|
|
|
-- raise run-time SQL exception resulting in error response for all subscribers in a cohort.
|
|
|
|
-- a. In case of computed fields, applying `LIMIT 1` to the base table selection will
|
|
|
|
-- enforce SQL function to evaluate only on one row. There's a possibility of SQL exception
|
|
|
|
-- on evaluating function on other rows.
|
|
|
|
-- b. In case of SQL functions as root fields, applying `LIMIT 1` to the base SQL function selection
|
|
|
|
-- don't have any performance impact as the limit is applied on the function result.
|
|
|
|
PG.Discard () <- PGL.executeQuery query [(cohortId, cohortVariables)]
|
|
|
|
pure ()
|
|
|
|
|
2021-06-11 06:26:50 +03:00
|
|
|
-- turn the current plan into a transaction
|
2021-09-24 01:56:37 +03:00
|
|
|
mkCurPlanTx ::
|
|
|
|
UserInfo ->
|
|
|
|
PreparedSql ->
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
(OnBaseMonad (PG.TxET QErr) EncJSON, Maybe PreparedSql)
|
2021-06-11 06:26:50 +03:00
|
|
|
mkCurPlanTx userInfo ps@(PreparedSql q prepMap) =
|
|
|
|
-- generate the SQL and prepared vars or the bytestring
|
|
|
|
let args = withUserVars (_uiSession userInfo) prepMap
|
|
|
|
-- WARNING: this quietly assumes the intmap keys are contiguous
|
|
|
|
prepArgs = fst <$> IntMap.elems args
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
in (,Just ps) $ OnBaseMonad do
|
Rewrite `Tracing` to allow for only one `TraceT` in the entire stack.
This PR is on top of #7789.
### Description
This PR entirely rewrites the API of the Tracing library, to make `interpTraceT` a thing of the past. Before this change, we ran traces by sticking a `TraceT` on top of whatever we were doing. This had several major drawbacks:
- we were carrying a bunch of `TraceT` across the codebase, and the entire codebase had to know about it
- we needed to carry a second class constraint around (`HasReporterM`) to be able to run all of those traces
- we kept having to do stack rewriting with `interpTraceT`, which went from inconvenient to horrible
- we had to declare several behavioral instances on `TraceT m`
This PR rewrite all of `Tracing` using a more conventional model: there is ONE `TraceT` at the bottom of the stack, and there is an associated class constraint `MonadTrace`: any part of the code that happens to satisfy `MonadTrace` is able to create new traces. We NEVER have to do stack rewriting, `interpTraceT` is gone, and `TraceT` and `Reporter` become implementation details that 99% of the code is blissfully unaware of: code that needs to do tracing only needs to declare that the monad in which it operates implements `MonadTrace`.
In doing so, this PR revealed **several bugs in the codebase**: places where we were expecting to trace something, but due to the default instance of `HasReporterM IO` we would actually not do anything. This PR also splits the code of `Tracing` in more byte-sized modules, with the goal of potentially moving to `server/lib` down the line.
### Remaining work
This PR is a draft; what's left to do is:
- [x] make Pro compile; i haven't updated `HasuraPro/Main` yet
- [x] document Tracing by writing a note that explains how to use the library, and the meaning of "reporter", "trace" and "span", as well as the pitfalls
- [x] discuss some of the trade-offs in the implementation, which is why i'm opening this PR already despite it not fully building yet
- [x] it depends on #7789 being merged first
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7791
GitOrigin-RevId: cadd32d039134c93ddbf364599a2f4dd988adea8
2023-03-13 20:37:16 +03:00
|
|
|
Tracing.newSpan "Postgres" $
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
runIdentity . PG.getRow
|
|
|
|
<$> PG.rawQE dmlTxErrorHandler q prepArgs True
|
2021-06-11 06:26:50 +03:00
|
|
|
|
|
|
|
-- convert a query from an intermediate representation to... another
|
2021-09-24 01:56:37 +03:00
|
|
|
irToRootFieldPlan ::
|
|
|
|
( Backend ('Postgres pgKind),
|
|
|
|
DS.PostgresAnnotatedFieldJSON pgKind
|
|
|
|
) =>
|
|
|
|
PrepArgMap ->
|
2021-12-07 16:12:02 +03:00
|
|
|
QueryDB ('Postgres pgKind) Void S.SQLExp ->
|
2021-09-24 01:56:37 +03:00
|
|
|
PreparedSql
|
2021-06-11 06:26:50 +03:00
|
|
|
irToRootFieldPlan prepped = \case
|
|
|
|
QDBMultipleRows s -> mkPreparedSql (DS.selectQuerySQL JASMultipleRows) s
|
2021-09-24 01:56:37 +03:00
|
|
|
QDBSingleRow s -> mkPreparedSql (DS.selectQuerySQL JASSingleObject) s
|
|
|
|
QDBAggregation s -> mkPreparedSql DS.selectAggregateQuerySQL s
|
|
|
|
QDBConnection s -> mkPreparedSql DS.connectionSelectQuerySQL s
|
2022-04-07 17:41:43 +03:00
|
|
|
QDBStreamMultipleRows s -> mkPreparedSql DS.selectStreamQuerySQL s
|
2021-06-11 06:26:50 +03:00
|
|
|
where
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
mkPreparedSql :: (t -> PG.Query) -> t -> PreparedSql
|
2021-06-11 06:26:50 +03:00
|
|
|
mkPreparedSql f simpleSel =
|
|
|
|
PreparedSql (f simpleSel) prepped
|
2021-07-29 11:29:12 +03:00
|
|
|
|
|
|
|
-- Append Query Tags to the Prepared SQL
|
|
|
|
appendPreparedSQLWithQueryTags :: PreparedSql -> QueryTagsComment -> PreparedSql
|
|
|
|
appendPreparedSQLWithQueryTags preparedSQL queryTags =
|
|
|
|
preparedSQL {_psQuery = appendSQLWithQueryTags query queryTags}
|
|
|
|
where
|
|
|
|
query = _psQuery preparedSQL
|
|
|
|
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
appendSQLWithQueryTags :: PG.Query -> QueryTagsComment -> PG.Query
|
|
|
|
appendSQLWithQueryTags query queryTags = query {PG.getQueryText = queryText <> _unQueryTagsComment queryTags}
|
2021-07-29 11:29:12 +03:00
|
|
|
where
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
queryText = PG.getQueryText query
|
2021-09-22 13:43:05 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Remote Relationships (e.g. DB-to-DB Joins, remote schema joins, etc.)
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | Construct an action (i.e. 'DBStepInfo') which can marshal some remote
|
|
|
|
-- relationship information into a form that Postgres can query against.
|
2021-09-24 01:56:37 +03:00
|
|
|
pgDBRemoteRelationshipPlan ::
|
|
|
|
forall pgKind m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
Backend ('Postgres pgKind),
|
|
|
|
PostgresAnnotatedFieldJSON pgKind
|
|
|
|
) =>
|
|
|
|
UserInfo ->
|
|
|
|
SourceName ->
|
|
|
|
SourceConfig ('Postgres pgKind) ->
|
2021-09-22 13:43:05 +03:00
|
|
|
-- | List of json objects, each of which becomes a row of the table.
|
2021-09-24 01:56:37 +03:00
|
|
|
NonEmpty J.Object ->
|
2021-09-22 13:43:05 +03:00
|
|
|
-- | The above objects have this schema
|
|
|
|
--
|
|
|
|
-- XXX: What is this for/what does this mean?
|
2021-09-24 01:56:37 +03:00
|
|
|
HashMap FieldName (Column ('Postgres pgKind), ScalarType ('Postgres pgKind)) ->
|
2021-09-22 13:43:05 +03:00
|
|
|
-- | This is a field name from the lhs that *has* to be selected in the
|
|
|
|
-- response along with the relationship.
|
2021-09-24 01:56:37 +03:00
|
|
|
FieldName ->
|
2021-12-07 16:12:02 +03:00
|
|
|
(FieldName, IR.SourceRelationshipSelection ('Postgres pgKind) Void UnpreparedValue) ->
|
2023-01-25 10:12:53 +03:00
|
|
|
[HTTP.Header] ->
|
|
|
|
Maybe G.Name ->
|
2022-12-19 17:03:13 +03:00
|
|
|
Options.StringifyNumbers ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m (DBStepInfo ('Postgres pgKind))
|
2023-04-13 04:29:15 +03:00
|
|
|
pgDBRemoteRelationshipPlan userInfo sourceName sourceConfig lhs lhsSchema argumentId relationship reqHeaders operationName stringifyNumbers = do
|
2021-09-22 13:43:05 +03:00
|
|
|
-- NOTE: 'QueryTags' currently cannot support remote relationship queries.
|
|
|
|
--
|
|
|
|
-- In the future if we want to add support we'll need to add a new type of
|
|
|
|
-- metadata (e.g. 'ParameterizedQueryHash' doesn't make sense here) and find
|
|
|
|
-- a root field name that makes sense to attach to it.
|
2023-04-13 04:29:15 +03:00
|
|
|
flip runReaderT emptyQueryTagsComment $ pgDBQueryPlan userInfo sourceName sourceConfig rootSelection reqHeaders operationName
|
2021-09-22 13:43:05 +03:00
|
|
|
where
|
2022-09-21 14:34:39 +03:00
|
|
|
coerceToColumn = Postgres.unsafePGCol . getFieldNameTxt
|
2021-09-22 13:43:05 +03:00
|
|
|
joinColumnMapping = mapKeys coerceToColumn lhsSchema
|
|
|
|
|
|
|
|
rowsArgument :: UnpreparedValue ('Postgres pgKind)
|
|
|
|
rowsArgument =
|
2023-04-12 12:04:16 +03:00
|
|
|
UVParameter Unknown $
|
2022-09-21 14:34:39 +03:00
|
|
|
ColumnValue (ColumnScalar Postgres.PGJSONB) $
|
2022-11-02 23:53:23 +03:00
|
|
|
Postgres.PGValJSONB $
|
|
|
|
PG.JSONB $
|
|
|
|
J.toJSON lhs
|
2021-09-22 13:43:05 +03:00
|
|
|
jsonToRecordSet :: IR.SelectFromG ('Postgres pgKind) (UnpreparedValue ('Postgres pgKind))
|
|
|
|
|
|
|
|
recordSetDefinitionList =
|
2022-09-21 14:34:39 +03:00
|
|
|
(coerceToColumn argumentId, Postgres.PGBigInt) : Map.toList (fmap snd joinColumnMapping)
|
2021-09-22 13:43:05 +03:00
|
|
|
jsonToRecordSet =
|
|
|
|
IR.FromFunction
|
2022-09-21 14:34:39 +03:00
|
|
|
(Postgres.QualifiedObject "pg_catalog" $ Postgres.FunctionName "jsonb_to_recordset")
|
|
|
|
(FunctionArgsExp [Postgres.AEInput rowsArgument] mempty)
|
2021-09-22 13:43:05 +03:00
|
|
|
(Just recordSetDefinitionList)
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
rootSelection =
|
|
|
|
convertRemoteSourceRelationship
|
|
|
|
(fst <$> joinColumnMapping)
|
|
|
|
jsonToRecordSet
|
2022-09-21 14:34:39 +03:00
|
|
|
(Postgres.unsafePGCol $ getFieldNameTxt argumentId)
|
|
|
|
(ColumnScalar Postgres.PGBigInt)
|
2021-09-24 01:56:37 +03:00
|
|
|
relationship
|
2022-12-19 17:03:13 +03:00
|
|
|
stringifyNumbers
|