2021-02-12 06:04:09 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2021-04-22 00:44:37 +03:00
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2021-05-21 05:46:58 +03:00
|
|
|
module Hasura.Backends.Postgres.Instances.Execute
|
2021-06-11 06:26:50 +03:00
|
|
|
( PreparedSql(..)
|
2021-05-21 05:46:58 +03:00
|
|
|
) where
|
2021-02-12 06:04:09 +03:00
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
2021-04-13 14:10:08 +03:00
|
|
|
import qualified Control.Monad.Trans.Control as MT
|
2021-02-20 16:45:49 +03:00
|
|
|
import qualified Data.HashSet as Set
|
2021-06-11 06:26:50 +03:00
|
|
|
import qualified Data.IntMap as IntMap
|
2021-02-20 16:45:49 +03:00
|
|
|
import qualified Data.Sequence as Seq
|
2021-04-13 14:10:08 +03:00
|
|
|
import qualified Database.PG.Query as Q
|
2021-02-20 16:45:49 +03:00
|
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
|
|
|
|
|
|
|
import qualified Hasura.Backends.Postgres.Execute.LiveQuery as PGL
|
|
|
|
import qualified Hasura.Backends.Postgres.Execute.Mutation as PGE
|
2021-06-11 06:26:50 +03:00
|
|
|
import qualified Hasura.Backends.Postgres.SQL.DML as S
|
|
|
|
import qualified Hasura.Backends.Postgres.Translate.Select as DS
|
2021-02-20 16:45:49 +03:00
|
|
|
import qualified Hasura.RQL.IR.Delete as IR
|
|
|
|
import qualified Hasura.RQL.IR.Insert as IR
|
|
|
|
import qualified Hasura.RQL.IR.Returning as IR
|
|
|
|
import qualified Hasura.RQL.IR.Select as IR
|
|
|
|
import qualified Hasura.RQL.IR.Update as IR
|
2021-03-15 16:02:58 +03:00
|
|
|
import qualified Hasura.SQL.AnyBackend as AB
|
2021-02-20 16:45:49 +03:00
|
|
|
import qualified Hasura.Tracing as Tracing
|
2021-02-12 06:04:09 +03:00
|
|
|
|
|
|
|
import Hasura.Backends.Postgres.Connection
|
2021-06-11 06:26:50 +03:00
|
|
|
import Hasura.Backends.Postgres.Execute.Insert
|
|
|
|
import Hasura.Backends.Postgres.Execute.Prepare
|
2021-05-21 05:46:58 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.Select (PostgresAnnotatedFieldJSON)
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Base.Error
|
2021-02-12 06:04:09 +03:00
|
|
|
import Hasura.EncJSON
|
|
|
|
import Hasura.GraphQL.Execute.Backend
|
2021-02-20 16:45:49 +03:00
|
|
|
import Hasura.GraphQL.Execute.LiveQuery.Plan
|
2021-02-12 06:04:09 +03:00
|
|
|
import Hasura.GraphQL.Parser
|
2021-07-29 11:29:12 +03:00
|
|
|
import Hasura.QueryTags
|
2021-04-13 14:10:08 +03:00
|
|
|
import Hasura.RQL.DML.Internal (dmlTxErrorHandler)
|
2021-06-11 06:26:50 +03:00
|
|
|
import Hasura.RQL.IR
|
2021-02-12 06:04:09 +03:00
|
|
|
import Hasura.RQL.Types
|
2021-02-20 16:45:49 +03:00
|
|
|
import Hasura.Server.Version (HasVersion)
|
2021-02-12 06:04:09 +03:00
|
|
|
import Hasura.Session
|
|
|
|
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
|
2021-06-11 06:26:50 +03:00
|
|
|
data PreparedSql
|
|
|
|
= PreparedSql
|
|
|
|
{ _psQuery :: !Q.Query
|
|
|
|
, _psPrepArgs :: !PrepArgMap
|
|
|
|
}
|
|
|
|
|
2021-05-21 05:46:58 +03:00
|
|
|
instance
|
|
|
|
( Backend ('Postgres pgKind)
|
|
|
|
, PostgresAnnotatedFieldJSON pgKind
|
|
|
|
) => BackendExecute ('Postgres pgKind) where
|
|
|
|
|
2021-06-11 06:26:50 +03:00
|
|
|
type PreparedQuery ('Postgres pgKind) = PreparedSql
|
2021-04-22 00:44:37 +03:00
|
|
|
type MultiplexedQuery ('Postgres pgKind) = PGL.MultiplexedQuery
|
|
|
|
type ExecutionMonad ('Postgres pgKind) = Tracing.TraceT (LazyTxT QErr IO)
|
2021-02-12 06:04:09 +03:00
|
|
|
|
|
|
|
mkDBQueryPlan = pgDBQueryPlan
|
|
|
|
mkDBMutationPlan = pgDBMutationPlan
|
2021-02-20 16:45:49 +03:00
|
|
|
mkDBSubscriptionPlan = pgDBSubscriptionPlan
|
2021-04-13 14:10:08 +03:00
|
|
|
mkDBQueryExplain = pgDBQueryExplain
|
|
|
|
mkLiveQueryExplain = pgDBLiveQueryExplain
|
2021-02-12 06:04:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- query
|
|
|
|
|
|
|
|
pgDBQueryPlan
|
2021-04-22 00:44:37 +03:00
|
|
|
:: forall pgKind m
|
|
|
|
. ( MonadError QErr m
|
|
|
|
, Backend ('Postgres pgKind)
|
2021-05-21 05:46:58 +03:00
|
|
|
, PostgresAnnotatedFieldJSON pgKind
|
2021-02-12 06:04:09 +03:00
|
|
|
)
|
2021-06-11 06:26:50 +03:00
|
|
|
=> UserInfo
|
2021-04-01 23:40:31 +03:00
|
|
|
-> SourceName
|
2021-04-22 00:44:37 +03:00
|
|
|
-> SourceConfig ('Postgres pgKind)
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> QueryDB ('Postgres pgKind) (Const Void) (UnpreparedValue ('Postgres pgKind))
|
2021-07-29 11:29:12 +03:00
|
|
|
-> QueryTagsComment
|
2021-06-11 06:26:50 +03:00
|
|
|
-> m (DBStepInfo ('Postgres pgKind))
|
2021-07-29 11:29:12 +03:00
|
|
|
pgDBQueryPlan userInfo sourceName sourceConfig qrf queryTags = do
|
2021-07-08 18:41:59 +03:00
|
|
|
(preparedQuery, PlanningSt _ _ planVals expectedVariables) <-
|
|
|
|
flip runStateT initPlanningSt $ traverse prepareWithPlan qrf
|
2021-02-12 06:04:09 +03:00
|
|
|
validateSessionVariables expectedVariables $ _uiSession userInfo
|
2021-07-29 11:29:12 +03:00
|
|
|
let preparedSQLWithQueryTags = appendPreparedSQLWithQueryTags (irToRootFieldPlan planVals preparedQuery) queryTags
|
|
|
|
let (action, preparedSQL) = mkCurPlanTx userInfo preparedSQLWithQueryTags
|
2021-06-11 06:26:50 +03:00
|
|
|
pure $ DBStepInfo @('Postgres pgKind) sourceName sourceConfig preparedSQL action
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2021-04-13 14:10:08 +03:00
|
|
|
pgDBQueryExplain
|
2021-04-22 00:44:37 +03:00
|
|
|
:: forall pgKind m
|
|
|
|
. ( MonadError QErr m
|
|
|
|
, Backend ('Postgres pgKind)
|
2021-05-21 05:46:58 +03:00
|
|
|
, PostgresAnnotatedFieldJSON pgKind
|
2021-04-22 00:44:37 +03:00
|
|
|
)
|
2021-04-13 14:10:08 +03:00
|
|
|
=> G.Name
|
|
|
|
-> UserInfo
|
|
|
|
-> SourceName
|
2021-04-22 00:44:37 +03:00
|
|
|
-> SourceConfig ('Postgres pgKind)
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> QueryDB ('Postgres pgKind) (Const Void) (UnpreparedValue ('Postgres pgKind))
|
2021-04-13 14:10:08 +03:00
|
|
|
-> m (AB.AnyBackend DBStepInfo)
|
|
|
|
pgDBQueryExplain fieldName userInfo sourceName sourceConfig qrf = do
|
2021-07-08 18:41:59 +03:00
|
|
|
preparedQuery <- traverse (resolveUnpreparedValue userInfo) qrf
|
2021-06-11 06:26:50 +03:00
|
|
|
let PreparedSql querySQL _ = irToRootFieldPlan mempty preparedQuery
|
2021-04-13 14:10:08 +03:00
|
|
|
textSQL = Q.getQueryText querySQL
|
|
|
|
-- CAREFUL!: an `EXPLAIN ANALYZE` here would actually *execute* this
|
|
|
|
-- query, maybe resulting in privilege escalation:
|
|
|
|
withExplain = "EXPLAIN (FORMAT TEXT) " <> textSQL
|
|
|
|
let action = liftTx $
|
|
|
|
Q.listQE dmlTxErrorHandler (Q.fromText withExplain) () True <&> \planList ->
|
|
|
|
encJFromJValue $ ExplainPlan fieldName (Just textSQL) (Just $ map runIdentity planList)
|
|
|
|
pure
|
|
|
|
$ AB.mkAnyBackend
|
2021-04-22 00:44:37 +03:00
|
|
|
$ DBStepInfo @('Postgres pgKind) sourceName sourceConfig Nothing action
|
2021-04-13 14:10:08 +03:00
|
|
|
|
|
|
|
pgDBLiveQueryExplain
|
|
|
|
:: ( MonadError QErr m
|
|
|
|
, MonadIO m
|
|
|
|
, MT.MonadBaseControl IO m
|
|
|
|
)
|
2021-04-22 00:44:37 +03:00
|
|
|
=> LiveQueryPlan ('Postgres pgKind) (MultiplexedQuery ('Postgres pgKind)) -> m LiveQueryPlanExplanation
|
2021-04-13 14:10:08 +03:00
|
|
|
pgDBLiveQueryExplain plan = do
|
|
|
|
let parameterizedPlan = _lqpParameterizedPlan plan
|
|
|
|
pgExecCtx = _pscExecCtx $ _lqpSourceConfig plan
|
|
|
|
queryText = Q.getQueryText . PGL.unMultiplexedQuery $ _plqpQuery parameterizedPlan
|
|
|
|
-- CAREFUL!: an `EXPLAIN ANALYZE` here would actually *execute* this
|
|
|
|
-- query, maybe resulting in privilege escalation:
|
|
|
|
explainQuery = Q.fromText $ "EXPLAIN (FORMAT TEXT) " <> queryText
|
|
|
|
cohortId <- newCohortId
|
|
|
|
explanationLines <- liftEitherM $ runExceptT $ runLazyTx pgExecCtx Q.ReadOnly $
|
|
|
|
map runIdentity <$> PGL.executeQuery explainQuery [(cohortId, _lqpVariables plan)]
|
|
|
|
pure $ LiveQueryPlanExplanation queryText explanationLines $ _lqpVariables plan
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
|
2021-02-12 06:04:09 +03:00
|
|
|
-- mutation
|
|
|
|
|
|
|
|
convertDelete
|
2021-04-22 00:44:37 +03:00
|
|
|
:: forall pgKind m
|
|
|
|
. ( MonadError QErr m
|
|
|
|
, Backend ('Postgres pgKind)
|
2021-05-21 05:46:58 +03:00
|
|
|
, PostgresAnnotatedFieldJSON pgKind
|
2021-02-12 06:04:09 +03:00
|
|
|
)
|
2021-06-11 06:26:50 +03:00
|
|
|
=> UserInfo
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> IR.AnnDelG ('Postgres pgKind) (Const Void) (UnpreparedValue ('Postgres pgKind))
|
2021-02-12 06:04:09 +03:00
|
|
|
-> Bool
|
2021-07-29 11:29:12 +03:00
|
|
|
-> QueryTagsComment
|
2021-02-20 16:45:49 +03:00
|
|
|
-> m (Tracing.TraceT (LazyTxT QErr IO) EncJSON)
|
2021-07-29 11:29:12 +03:00
|
|
|
convertDelete userInfo deleteOperation stringifyNum queryTags = do
|
2021-04-22 00:44:37 +03:00
|
|
|
let (preparedDelete, expectedVariables) =
|
2021-07-08 18:41:59 +03:00
|
|
|
flip runState Set.empty $ traverse prepareWithoutPlan deleteOperation
|
2021-06-11 06:26:50 +03:00
|
|
|
validateSessionVariables expectedVariables $ _uiSession userInfo
|
2021-07-29 11:29:12 +03:00
|
|
|
pure $ flip runReaderT queryTags $ PGE.execDeleteQuery stringifyNum userInfo (preparedDelete, Seq.empty)
|
2021-02-12 06:04:09 +03:00
|
|
|
|
|
|
|
convertUpdate
|
2021-04-22 00:44:37 +03:00
|
|
|
:: forall pgKind m
|
|
|
|
. ( MonadError QErr m
|
|
|
|
, Backend ('Postgres pgKind)
|
2021-05-21 05:46:58 +03:00
|
|
|
, PostgresAnnotatedFieldJSON pgKind
|
2021-02-12 06:04:09 +03:00
|
|
|
)
|
2021-06-11 06:26:50 +03:00
|
|
|
=> UserInfo
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> IR.AnnUpdG ('Postgres pgKind) (Const Void) (UnpreparedValue ('Postgres pgKind))
|
2021-02-12 06:04:09 +03:00
|
|
|
-> Bool
|
2021-07-29 11:29:12 +03:00
|
|
|
-> QueryTagsComment
|
2021-02-20 16:45:49 +03:00
|
|
|
-> m (Tracing.TraceT (LazyTxT QErr IO) EncJSON)
|
2021-07-29 11:29:12 +03:00
|
|
|
convertUpdate userInfo updateOperation stringifyNum queryTags = do
|
2021-07-08 18:41:59 +03:00
|
|
|
let (preparedUpdate, expectedVariables) = flip runState Set.empty $ traverse prepareWithoutPlan updateOperation
|
2021-02-12 06:04:09 +03:00
|
|
|
if null $ IR.uqp1OpExps updateOperation
|
|
|
|
then pure $ pure $ IR.buildEmptyMutResp $ IR.uqp1Output preparedUpdate
|
|
|
|
else do
|
2021-06-11 06:26:50 +03:00
|
|
|
validateSessionVariables expectedVariables $ _uiSession userInfo
|
2021-07-29 11:29:12 +03:00
|
|
|
pure $ flip runReaderT queryTags $ PGE.execUpdateQuery stringifyNum userInfo (preparedUpdate, Seq.empty)
|
2021-02-12 06:04:09 +03:00
|
|
|
|
|
|
|
convertInsert
|
2021-04-22 00:44:37 +03:00
|
|
|
:: forall pgKind m
|
|
|
|
. ( MonadError QErr m
|
2021-02-12 06:04:09 +03:00
|
|
|
, HasVersion
|
2021-04-22 00:44:37 +03:00
|
|
|
, Backend ('Postgres pgKind)
|
2021-05-21 05:46:58 +03:00
|
|
|
, PostgresAnnotatedFieldJSON pgKind
|
2021-02-12 06:04:09 +03:00
|
|
|
)
|
2021-06-11 06:26:50 +03:00
|
|
|
=> UserInfo
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> IR.AnnInsert ('Postgres pgKind) (Const Void) (UnpreparedValue ('Postgres pgKind))
|
2021-02-12 06:04:09 +03:00
|
|
|
-> Bool
|
2021-07-29 11:29:12 +03:00
|
|
|
-> QueryTagsComment
|
2021-02-20 16:45:49 +03:00
|
|
|
-> m (Tracing.TraceT (LazyTxT QErr IO) EncJSON)
|
2021-07-29 11:29:12 +03:00
|
|
|
convertInsert userInfo insertOperation stringifyNum queryTags = do
|
2021-07-08 18:41:59 +03:00
|
|
|
let (preparedInsert, expectedVariables) = flip runState Set.empty $ traverse prepareWithoutPlan insertOperation
|
2021-06-11 06:26:50 +03:00
|
|
|
validateSessionVariables expectedVariables $ _uiSession userInfo
|
2021-07-29 11:29:12 +03:00
|
|
|
pure $ flip runReaderT queryTags $ convertToSQLTransaction preparedInsert userInfo Seq.empty stringifyNum
|
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').
|
|
|
|
convertFunction
|
2021-04-22 00:44:37 +03:00
|
|
|
:: forall pgKind m
|
|
|
|
. ( MonadError QErr m
|
|
|
|
, Backend ('Postgres pgKind)
|
2021-05-21 05:46:58 +03:00
|
|
|
, PostgresAnnotatedFieldJSON pgKind
|
2021-02-12 06:04:09 +03:00
|
|
|
)
|
2021-06-11 06:26:50 +03:00
|
|
|
=> UserInfo
|
2021-02-12 06:04:09 +03:00
|
|
|
-> JsonAggSelect
|
2021-07-08 18:41:59 +03:00
|
|
|
-> IR.AnnSimpleSelectG ('Postgres pgKind) (Const Void) (UnpreparedValue ('Postgres pgKind))
|
2021-02-12 06:04:09 +03:00
|
|
|
-- ^ VOLATILE function as 'SelectExp'
|
2021-07-29 11:29:12 +03:00
|
|
|
-> QueryTagsComment
|
|
|
|
-- ^ Query Tags
|
2021-02-20 16:45:49 +03:00
|
|
|
-> m (Tracing.TraceT (LazyTxT QErr IO) EncJSON)
|
2021-07-29 11:29:12 +03:00
|
|
|
convertFunction userInfo jsonAggSelect unpreparedQuery queryTags = do
|
2021-02-12 06:04:09 +03:00
|
|
|
-- Transform the RQL AST into a prepared SQL query
|
|
|
|
(preparedQuery, PlanningSt _ _ planVals expectedVariables)
|
|
|
|
<- flip runStateT initPlanningSt
|
2021-07-08 18:41:59 +03:00
|
|
|
$ traverse prepareWithPlan unpreparedQuery
|
2021-02-12 06:04:09 +03:00
|
|
|
validateSessionVariables expectedVariables $ _uiSession userInfo
|
|
|
|
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
|
2021-02-12 06:04:09 +03:00
|
|
|
pure $!
|
|
|
|
fst $ -- forget (Maybe PreparedSql)
|
2021-07-29 11:29:12 +03:00
|
|
|
mkCurPlanTx userInfo preparedSQLWithQueryTags
|
2021-02-12 06:04:09 +03:00
|
|
|
|
|
|
|
pgDBMutationPlan
|
2021-04-22 00:44:37 +03:00
|
|
|
:: forall pgKind m
|
|
|
|
. ( MonadError QErr m
|
2021-02-12 06:04:09 +03:00
|
|
|
, HasVersion
|
2021-04-22 00:44:37 +03:00
|
|
|
, Backend ('Postgres pgKind)
|
2021-05-21 05:46:58 +03:00
|
|
|
, PostgresAnnotatedFieldJSON pgKind
|
2021-02-12 06:04:09 +03:00
|
|
|
)
|
2021-06-11 06:26:50 +03:00
|
|
|
=> UserInfo
|
2021-02-12 06:04:09 +03:00
|
|
|
-> Bool
|
2021-04-01 23:40:31 +03:00
|
|
|
-> SourceName
|
2021-04-22 00:44:37 +03:00
|
|
|
-> SourceConfig ('Postgres pgKind)
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> MutationDB ('Postgres pgKind) (Const Void) (UnpreparedValue ('Postgres pgKind))
|
2021-07-29 11:29:12 +03:00
|
|
|
-> QueryTagsComment
|
2021-06-11 06:26:50 +03:00
|
|
|
-> m (DBStepInfo ('Postgres pgKind))
|
2021-07-29 11:29:12 +03:00
|
|
|
pgDBMutationPlan userInfo stringifyNum sourceName sourceConfig mrf queryTags =
|
|
|
|
go <$> case mrf of
|
|
|
|
MDBInsert s -> convertInsert userInfo s stringifyNum queryTags
|
|
|
|
MDBUpdate s -> convertUpdate userInfo s stringifyNum queryTags
|
|
|
|
MDBDelete s -> convertDelete userInfo s stringifyNum queryTags
|
|
|
|
MDBFunction returnsSet s -> convertFunction userInfo returnsSet s queryTags
|
2021-02-12 06:04:09 +03:00
|
|
|
where
|
2021-06-11 06:26:50 +03:00
|
|
|
go v = DBStepInfo @('Postgres pgKind) sourceName sourceConfig Nothing v
|
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
|
|
|
|
|
|
|
pgDBSubscriptionPlan
|
2021-04-22 00:44:37 +03:00
|
|
|
:: forall pgKind m
|
|
|
|
. ( MonadError QErr m
|
2021-02-23 20:37:27 +03:00
|
|
|
, MonadIO m
|
2021-04-22 00:44:37 +03:00
|
|
|
, Backend ('Postgres pgKind)
|
2021-05-21 05:46:58 +03:00
|
|
|
, PostgresAnnotatedFieldJSON pgKind
|
2021-02-23 20:37:27 +03:00
|
|
|
)
|
2021-02-20 16:45:49 +03:00
|
|
|
=> UserInfo
|
2021-04-01 23:40:31 +03:00
|
|
|
-> SourceName
|
2021-04-22 00:44:37 +03:00
|
|
|
-> SourceConfig ('Postgres pgKind)
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> InsOrdHashMap G.Name (QueryDB ('Postgres pgKind) (Const Void) (UnpreparedValue ('Postgres pgKind)))
|
2021-07-29 11:29:12 +03:00
|
|
|
-> QueryTagsComment
|
2021-04-22 00:44:37 +03:00
|
|
|
-> m (LiveQueryPlan ('Postgres pgKind) (MultiplexedQuery ('Postgres pgKind)))
|
2021-07-29 11:29:12 +03:00
|
|
|
pgDBSubscriptionPlan userInfo _sourceName sourceConfig unpreparedAST queryTags = do
|
2021-02-20 16:45:49 +03:00
|
|
|
(preparedAST, PGL.QueryParametersInfo{..}) <- flip runStateT mempty $
|
2021-07-08 18:41:59 +03:00
|
|
|
for unpreparedAST $ traverse (PGL.resolveMultiplexedValue $ _uiSession userInfo)
|
2021-02-20 16:45:49 +03:00
|
|
|
let multiplexedQuery = PGL.mkMultiplexedQuery preparedAST
|
2021-07-29 11:29:12 +03:00
|
|
|
multiplexedQueryWithQueryTags = multiplexedQuery { PGL.unMultiplexedQuery = appendSQLWithQueryTags (PGL.unMultiplexedQuery multiplexedQuery) queryTags}
|
2021-02-20 16:45:49 +03:00
|
|
|
roleName = _uiRole userInfo
|
2021-07-29 11:29:12 +03:00
|
|
|
parameterizedPlan = ParameterizedLiveQueryPlan roleName multiplexedQueryWithQueryTags
|
2021-02-20 16:45:49 +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.
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
validatedQueryVars <- PGL.validateVariables (_pscExecCtx sourceConfig) _qpiReusableVariableValues
|
|
|
|
validatedSyntheticVars <- PGL.validateVariables (_pscExecCtx sourceConfig) $ toList _qpiSyntheticVariableValues
|
2021-02-20 16:45:49 +03:00
|
|
|
|
|
|
|
-- TODO validatedQueryVars validatedSyntheticVars
|
|
|
|
let cohortVariables = mkCohortVariables
|
|
|
|
_qpiReferencedSessionVariables
|
|
|
|
(_uiSession userInfo)
|
|
|
|
validatedQueryVars
|
|
|
|
validatedSyntheticVars
|
|
|
|
|
|
|
|
pure $ LiveQueryPlan parameterizedPlan sourceConfig cohortVariables
|
2021-04-22 00:44:37 +03:00
|
|
|
|
2021-06-11 06:26:50 +03:00
|
|
|
-- turn the current plan into a transaction
|
|
|
|
mkCurPlanTx
|
|
|
|
:: UserInfo
|
|
|
|
-> PreparedSql
|
|
|
|
-> (Tracing.TraceT (LazyTxT QErr IO) EncJSON, Maybe PreparedSql)
|
|
|
|
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
|
|
|
|
in (, Just ps) $
|
|
|
|
Tracing.trace "Postgres" $ liftTx $ DS.asSingleRowJsonResp q prepArgs
|
|
|
|
|
|
|
|
-- convert a query from an intermediate representation to... another
|
|
|
|
irToRootFieldPlan
|
|
|
|
:: ( Backend ('Postgres pgKind)
|
|
|
|
, DS.PostgresAnnotatedFieldJSON pgKind
|
|
|
|
)
|
|
|
|
=> PrepArgMap
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> QueryDB ('Postgres pgKind) (Const Void) S.SQLExp
|
2021-06-11 06:26:50 +03:00
|
|
|
-> PreparedSql
|
|
|
|
irToRootFieldPlan prepped = \case
|
|
|
|
QDBMultipleRows s -> mkPreparedSql (DS.selectQuerySQL JASMultipleRows) s
|
|
|
|
QDBSingleRow s -> mkPreparedSql (DS.selectQuerySQL JASSingleObject) s
|
|
|
|
QDBAggregation s -> mkPreparedSql DS.selectAggregateQuerySQL s
|
|
|
|
QDBConnection s -> mkPreparedSql DS.connectionSelectQuerySQL s
|
|
|
|
where
|
|
|
|
mkPreparedSql :: (t -> Q.Query) -> t -> PreparedSql
|
|
|
|
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
|
|
|
|
|
|
|
|
appendSQLWithQueryTags :: Q.Query -> QueryTagsComment -> Q.Query
|
|
|
|
appendSQLWithQueryTags query queryTags = query {Q.getQueryText = queryText <> (_unQueryTagsComment queryTags)}
|
|
|
|
where
|
|
|
|
queryText = Q.getQueryText query
|