2021-04-12 13:18:29 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
|
|
|
module Hasura.Backends.BigQuery.Instances.Transport () where
|
|
|
|
|
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
|
|
|
import Control.Monad.Trans.Control
|
2021-04-13 14:10:08 +03:00
|
|
|
import Data.Aeson qualified as J
|
2021-04-12 13:18:29 +03:00
|
|
|
import Hasura.Backends.BigQuery.Instances.Execute ()
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Base.Error
|
2021-04-12 13:18:29 +03:00
|
|
|
import Hasura.EncJSON
|
2021-05-05 19:20:04 +03:00
|
|
|
import Hasura.GraphQL.Execute.Backend
|
2021-04-13 14:10:08 +03:00
|
|
|
import Hasura.GraphQL.Logging
|
2023-03-15 16:05:17 +03:00
|
|
|
( ExecutionLog (..),
|
|
|
|
ExecutionStats (..),
|
2023-03-14 14:32:20 +03:00
|
|
|
GeneratedQuery (..),
|
2023-03-15 16:05:17 +03:00
|
|
|
MonadExecutionLog (..),
|
2021-04-19 04:21:34 +03:00
|
|
|
MonadQueryLog (..),
|
|
|
|
QueryLog (..),
|
2023-03-15 16:05:17 +03:00
|
|
|
QueryLogKind (QueryLogKindDatabase),
|
2021-09-24 01:56:37 +03:00
|
|
|
)
|
2021-10-29 17:42:07 +03:00
|
|
|
import Hasura.GraphQL.Namespace (RootFieldAlias)
|
2021-04-12 13:18:29 +03:00
|
|
|
import Hasura.GraphQL.Transport.Backend
|
|
|
|
import Hasura.GraphQL.Transport.HTTP.Protocol
|
2021-05-05 19:20:04 +03:00
|
|
|
import Hasura.Logging qualified as L
|
|
|
|
import Hasura.Prelude
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Backend
|
2023-03-14 14:32:20 +03:00
|
|
|
import Hasura.SQL.AnyBackend (AnyBackend)
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.SQL.Backend
|
2021-04-13 14:10:08 +03:00
|
|
|
import Hasura.Server.Types (RequestId)
|
2021-04-12 13:18:29 +03:00
|
|
|
import Hasura.Session
|
|
|
|
import Hasura.Tracing
|
2021-05-05 19:20:04 +03:00
|
|
|
|
2021-04-12 13:18:29 +03:00
|
|
|
instance BackendTransport 'BigQuery where
|
|
|
|
runDBQuery = runQuery
|
2021-05-05 19:20:04 +03:00
|
|
|
runDBQueryExplain = runQueryExplain
|
2021-04-12 13:18:29 +03:00
|
|
|
runDBMutation = runMutation
|
|
|
|
runDBSubscription = error "Not supported."
|
2022-04-07 17:41:43 +03:00
|
|
|
runDBStreamingSubscription = error "Not supported"
|
2021-04-12 13:18:29 +03:00
|
|
|
|
|
|
|
runQuery ::
|
|
|
|
( MonadIO m,
|
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
|
|
|
MonadBaseControl IO m,
|
2021-04-12 13:18:29 +03:00
|
|
|
MonadQueryLog m,
|
2023-03-15 16:05:17 +03:00
|
|
|
MonadExecutionLog m,
|
2021-04-12 13:18:29 +03:00
|
|
|
MonadTrace m,
|
|
|
|
MonadError QErr m
|
|
|
|
) =>
|
|
|
|
RequestId ->
|
|
|
|
GQLReqUnparsed ->
|
2021-10-29 17:42:07 +03:00
|
|
|
RootFieldAlias ->
|
2021-04-12 13:18:29 +03:00
|
|
|
UserInfo ->
|
|
|
|
L.Logger L.Hasura ->
|
|
|
|
SourceConfig 'BigQuery ->
|
2023-03-14 14:32:20 +03:00
|
|
|
OnBaseMonad IdentityT (Maybe (AnyBackend ExecutionStats), EncJSON) ->
|
2021-04-12 13:18:29 +03:00
|
|
|
Maybe Text ->
|
2023-01-25 10:12:53 +03:00
|
|
|
ResolvedConnectionTemplate 'BigQuery ->
|
2021-04-12 13:18:29 +03:00
|
|
|
-- | Also return the time spent in the PG query; for telemetry.
|
|
|
|
m (DiffTime, EncJSON)
|
2023-01-25 10:12:53 +03:00
|
|
|
runQuery reqId query fieldName _userInfo logger _sourceConfig tx genSql _ = do
|
2021-04-12 13:18:29 +03:00
|
|
|
-- log the generated SQL and the graphql query
|
|
|
|
-- FIXME: fix logging by making logQueryLog expect something backend agnostic!
|
2023-03-15 16:05:17 +03:00
|
|
|
logQueryLog logger $ mkQueryLog (QueryLogKindDatabase Nothing) query fieldName genSql reqId
|
2023-03-14 14:32:20 +03:00
|
|
|
(diffTime, (stats, result)) <- withElapsedTime $ run tx
|
|
|
|
|
2023-03-15 16:05:17 +03:00
|
|
|
logExecutionLog logger $ mkExecutionLog reqId stats
|
2023-03-14 14:32:20 +03:00
|
|
|
|
|
|
|
pure (diffTime, result)
|
2021-04-12 13:18:29 +03:00
|
|
|
|
2021-05-05 19:20:04 +03:00
|
|
|
runQueryExplain ::
|
|
|
|
( MonadIO m,
|
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
|
|
|
MonadBaseControl IO m,
|
|
|
|
MonadError QErr m,
|
|
|
|
MonadTrace m
|
2021-05-05 19:20:04 +03:00
|
|
|
) =>
|
|
|
|
DBStepInfo 'BigQuery ->
|
|
|
|
m EncJSON
|
2023-03-14 14:32:20 +03:00
|
|
|
runQueryExplain (DBStepInfo _ _ _ action _) = fmap arResult (run action)
|
2021-04-12 13:18:29 +03:00
|
|
|
|
|
|
|
runMutation ::
|
|
|
|
( MonadError QErr m
|
|
|
|
) =>
|
|
|
|
RequestId ->
|
|
|
|
GQLReqUnparsed ->
|
2021-10-29 17:42:07 +03:00
|
|
|
RootFieldAlias ->
|
2021-04-12 13:18:29 +03:00
|
|
|
UserInfo ->
|
|
|
|
L.Logger L.Hasura ->
|
|
|
|
SourceConfig 'BigQuery ->
|
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 IdentityT EncJSON ->
|
2021-04-12 13:18:29 +03:00
|
|
|
Maybe Text ->
|
2023-01-25 10:12:53 +03:00
|
|
|
ResolvedConnectionTemplate 'BigQuery ->
|
2021-04-12 13:18:29 +03:00
|
|
|
-- | Also return 'Mutation' when the operation was a mutation, and the time
|
|
|
|
-- spent in the PG query; for telemetry.
|
|
|
|
m (DiffTime, EncJSON)
|
2023-01-25 10:12:53 +03:00
|
|
|
runMutation _reqId _query _fieldName _userInfo _logger _sourceConfig _tx _genSql _ =
|
2021-04-12 13:18:29 +03:00
|
|
|
-- do
|
|
|
|
throw500 "BigQuery does not support mutations!"
|
|
|
|
|
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
|
|
|
run ::
|
|
|
|
( MonadIO m,
|
|
|
|
MonadBaseControl IO m,
|
|
|
|
MonadError QErr m,
|
|
|
|
MonadTrace m
|
|
|
|
) =>
|
|
|
|
OnBaseMonad IdentityT a ->
|
|
|
|
m a
|
|
|
|
run = runIdentityT . runOnBaseMonad
|
2021-05-05 19:20:04 +03:00
|
|
|
|
2023-03-14 14:32:20 +03:00
|
|
|
-- @QueryLogKindDatabase Nothing@ means that the backend doesn't support connection templates
|
2021-04-12 13:18:29 +03:00
|
|
|
mkQueryLog ::
|
2023-03-14 14:32:20 +03:00
|
|
|
QueryLogKind ->
|
2021-04-12 13:18:29 +03:00
|
|
|
GQLReqUnparsed ->
|
2021-10-29 17:42:07 +03:00
|
|
|
RootFieldAlias ->
|
2021-04-12 13:18:29 +03:00
|
|
|
Maybe Text ->
|
|
|
|
RequestId ->
|
|
|
|
QueryLog
|
2023-03-15 16:05:17 +03:00
|
|
|
mkQueryLog _qlKind _qlQuery fieldName preparedSql _qlRequestId = QueryLog {..}
|
2021-04-12 13:18:29 +03:00
|
|
|
where
|
2023-03-14 14:32:20 +03:00
|
|
|
_qlGeneratedSql = preparedSql <&> \query -> (fieldName, GeneratedQuery query J.Null)
|
2023-03-15 16:05:17 +03:00
|
|
|
|
|
|
|
mkExecutionLog ::
|
|
|
|
RequestId ->
|
|
|
|
Maybe (AnyBackend ExecutionStats) ->
|
|
|
|
ExecutionLog
|
|
|
|
mkExecutionLog _elRequestId _elStatistics = ExecutionLog {..}
|