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
|
|
|
|
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 Database.PG.Query qualified as Q
|
|
|
|
import Hasura.Backends.Postgres.Connection (runTx)
|
|
|
|
import Hasura.Backends.Postgres.Execute.Insert (convertToSQLTransaction)
|
|
|
|
import Hasura.Backends.Postgres.Execute.LiveQuery qualified as PGL
|
|
|
|
import Hasura.Backends.Postgres.Execute.Mutation qualified as PGE
|
|
|
|
import Hasura.Backends.Postgres.Execute.Prepare
|
|
|
|
( PlanningSt (..),
|
|
|
|
PrepArgMap,
|
|
|
|
initPlanningSt,
|
|
|
|
prepareWithPlan,
|
|
|
|
prepareWithoutPlan,
|
|
|
|
resolveUnpreparedValue,
|
|
|
|
withUserVars,
|
|
|
|
)
|
|
|
|
import Hasura.Backends.Postgres.Execute.Types (PGSourceConfig (..))
|
|
|
|
import Hasura.Backends.Postgres.SQL.DML qualified as S
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types qualified as PG
|
|
|
|
import Hasura.Backends.Postgres.SQL.Value qualified as PG
|
|
|
|
import Hasura.Backends.Postgres.Translate.Select (PostgresAnnotatedFieldJSON)
|
|
|
|
import Hasura.Backends.Postgres.Translate.Select qualified as DS
|
2021-11-18 21:02:58 +03:00
|
|
|
import Hasura.Backends.Postgres.Types.Update
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Base.Error (QErr)
|
|
|
|
import Hasura.EncJSON (EncJSON, encJFromJValue)
|
|
|
|
import Hasura.GraphQL.Execute.Backend
|
|
|
|
( BackendExecute (..),
|
|
|
|
DBStepInfo (..),
|
|
|
|
ExplainPlan (..),
|
|
|
|
convertRemoteSourceRelationship,
|
|
|
|
)
|
|
|
|
import Hasura.GraphQL.Execute.LiveQuery.Plan
|
|
|
|
( LiveQueryPlan (..),
|
|
|
|
LiveQueryPlanExplanation (..),
|
|
|
|
ParameterizedLiveQueryPlan (..),
|
|
|
|
mkCohortVariables,
|
|
|
|
newCohortId,
|
|
|
|
)
|
2021-10-29 17:42:07 +03:00
|
|
|
import Hasura.GraphQL.Namespace
|
|
|
|
( RootFieldAlias (..),
|
|
|
|
RootFieldMap,
|
|
|
|
)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.GraphQL.Parser (UnpreparedValue (..))
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.QueryTags
|
|
|
|
( QueryTagsComment (..),
|
|
|
|
emptyQueryTagsComment,
|
|
|
|
)
|
|
|
|
import Hasura.RQL.DML.Internal (dmlTxErrorHandler)
|
|
|
|
import Hasura.RQL.IR (MutationDB (..), QueryDB (..))
|
|
|
|
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
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
( Backend (..),
|
|
|
|
BackendType (Postgres),
|
|
|
|
FieldName,
|
|
|
|
JsonAggSelect (..),
|
|
|
|
SourceName,
|
|
|
|
getFieldNameTxt,
|
|
|
|
liftTx,
|
|
|
|
)
|
|
|
|
import Hasura.RQL.Types.Column (ColumnType (..), ColumnValue (..))
|
|
|
|
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
|
|
|
|
|
|
|
|
data PreparedSql = PreparedSql
|
|
|
|
{ _psQuery :: !Q.Query,
|
|
|
|
_psPrepArgs :: !PrepArgMap
|
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
|
2021-09-24 01:56:37 +03:00
|
|
|
type ExecutionMonad ('Postgres pgKind) = Tracing.TraceT (Q.TxET 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-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)) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m (DBStepInfo ('Postgres pgKind))
|
2021-09-23 15:37:56 +03:00
|
|
|
pgDBQueryPlan userInfo sourceName sourceConfig qrf = do
|
2021-07-31 00:41:52 +03:00
|
|
|
(preparedQuery, PlanningSt _ _ planVals) <-
|
|
|
|
flip runStateT initPlanningSt $ traverse (prepareWithPlan userInfo) qrf
|
2021-09-23 15:37:56 +03:00
|
|
|
queryTagsComment <- ask
|
|
|
|
let preparedSQLWithQueryTags = appendPreparedSQLWithQueryTags (irToRootFieldPlan planVals preparedQuery) queryTagsComment
|
2021-07-29 11:29:12 +03:00
|
|
|
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-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)) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m (AB.AnyBackend DBStepInfo)
|
2021-09-22 13:43:05 +03:00
|
|
|
pgDBQueryExplain fieldName userInfo sourceName sourceConfig rootSelection = do
|
|
|
|
preparedQuery <- traverse (resolveUnpreparedValue userInfo) rootSelection
|
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
|
2021-09-24 01:56:37 +03:00
|
|
|
let action =
|
|
|
|
liftTx $
|
|
|
|
Q.listQE dmlTxErrorHandler (Q.fromText withExplain) () True <&> \planList ->
|
|
|
|
encJFromJValue $ ExplainPlan fieldName (Just textSQL) (Just $ map runIdentity planList)
|
|
|
|
pure $
|
|
|
|
AB.mkAnyBackend $
|
|
|
|
DBStepInfo @('Postgres pgKind) sourceName sourceConfig Nothing action
|
|
|
|
|
|
|
|
pgDBLiveQueryExplain ::
|
|
|
|
( MonadError QErr m,
|
|
|
|
MonadIO m,
|
|
|
|
MT.MonadBaseControl IO m
|
|
|
|
) =>
|
|
|
|
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
|
2021-09-24 01:56:37 +03:00
|
|
|
explanationLines <-
|
|
|
|
liftEitherM $
|
|
|
|
runExceptT $
|
|
|
|
runTx pgExecCtx Q.ReadOnly $
|
|
|
|
map runIdentity <$> PGL.executeQuery explainQuery [(cohortId, _lqpVariables plan)]
|
2021-04-13 14:10:08 +03:00
|
|
|
pure $ LiveQueryPlanExplanation queryText explanationLines $ _lqpVariables 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),
|
|
|
|
PostgresAnnotatedFieldJSON pgKind
|
|
|
|
) =>
|
|
|
|
UserInfo ->
|
2021-12-07 16:12:02 +03:00
|
|
|
IR.AnnDelG ('Postgres pgKind) Void (UnpreparedValue ('Postgres pgKind)) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
Bool ->
|
|
|
|
QueryTagsComment ->
|
|
|
|
m (Tracing.TraceT (Q.TxET QErr IO) EncJSON)
|
2021-07-29 11:29:12 +03:00
|
|
|
convertDelete userInfo deleteOperation stringifyNum queryTags = do
|
2021-07-31 00:41:52 +03:00
|
|
|
preparedDelete <- traverse (prepareWithoutPlan userInfo) deleteOperation
|
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
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
convertUpdate ::
|
|
|
|
forall pgKind m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
Backend ('Postgres pgKind),
|
|
|
|
PostgresAnnotatedFieldJSON pgKind
|
|
|
|
) =>
|
|
|
|
UserInfo ->
|
2021-12-07 16:12:02 +03:00
|
|
|
IR.AnnotatedUpdateG ('Postgres pgKind) Void (UnpreparedValue ('Postgres pgKind)) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
Bool ->
|
|
|
|
QueryTagsComment ->
|
|
|
|
m (Tracing.TraceT (Q.TxET QErr IO) EncJSON)
|
2021-07-29 11:29:12 +03:00
|
|
|
convertUpdate userInfo updateOperation stringifyNum queryTags = do
|
2021-07-31 00:41:52 +03:00
|
|
|
preparedUpdate <- traverse (prepareWithoutPlan userInfo) updateOperation
|
2021-11-25 00:39:42 +03:00
|
|
|
if null $ updateOperations . IR._auBackend $ updateOperation
|
|
|
|
then pure $ pure $ IR.buildEmptyMutResp $ IR._auOutput preparedUpdate
|
2021-09-24 01:56:37 +03:00
|
|
|
else
|
|
|
|
pure $
|
|
|
|
flip runReaderT queryTags $
|
|
|
|
PGE.execUpdateQuery stringifyNum userInfo (preparedUpdate, Seq.empty)
|
|
|
|
|
|
|
|
convertInsert ::
|
|
|
|
forall pgKind m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
Backend ('Postgres pgKind),
|
|
|
|
PostgresAnnotatedFieldJSON pgKind
|
|
|
|
) =>
|
|
|
|
UserInfo ->
|
2021-12-07 16:12:02 +03:00
|
|
|
IR.AnnInsert ('Postgres pgKind) Void (UnpreparedValue ('Postgres pgKind)) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
Bool ->
|
|
|
|
QueryTagsComment ->
|
|
|
|
m (Tracing.TraceT (Q.TxET QErr IO) EncJSON)
|
2021-07-29 11:29:12 +03:00
|
|
|
convertInsert userInfo insertOperation stringifyNum queryTags = do
|
2021-07-31 00:41:52 +03:00
|
|
|
preparedInsert <- traverse (prepareWithoutPlan userInfo) insertOperation
|
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').
|
2021-09-24 01:56:37 +03:00
|
|
|
convertFunction ::
|
|
|
|
forall pgKind m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
Backend ('Postgres pgKind),
|
|
|
|
PostgresAnnotatedFieldJSON pgKind
|
|
|
|
) =>
|
|
|
|
UserInfo ->
|
|
|
|
JsonAggSelect ->
|
|
|
|
-- | VOLATILE function as 'SelectExp'
|
2021-12-07 16:12:02 +03:00
|
|
|
IR.AnnSimpleSelectG ('Postgres pgKind) Void (UnpreparedValue ('Postgres pgKind)) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
-- | Query Tags
|
|
|
|
QueryTagsComment ->
|
|
|
|
m (Tracing.TraceT (Q.TxET 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
|
2021-09-24 01:56:37 +03:00
|
|
|
(preparedQuery, PlanningSt _ _ planVals) <-
|
|
|
|
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
|
2021-09-24 01:56:37 +03:00
|
|
|
pure
|
|
|
|
$! fst
|
|
|
|
$ mkCurPlanTx userInfo preparedSQLWithQueryTags -- forget (Maybe PreparedSql)
|
|
|
|
|
|
|
|
pgDBMutationPlan ::
|
|
|
|
forall pgKind m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
Backend ('Postgres pgKind),
|
|
|
|
PostgresAnnotatedFieldJSON pgKind,
|
|
|
|
MonadReader QueryTagsComment m
|
|
|
|
) =>
|
|
|
|
UserInfo ->
|
|
|
|
Bool ->
|
|
|
|
SourceName ->
|
|
|
|
SourceConfig ('Postgres pgKind) ->
|
2021-12-07 16:12:02 +03:00
|
|
|
MutationDB ('Postgres pgKind) Void (UnpreparedValue ('Postgres pgKind)) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m (DBStepInfo ('Postgres pgKind))
|
2021-09-23 15:37:56 +03:00
|
|
|
pgDBMutationPlan userInfo stringifyNum sourceName sourceConfig mrf = do
|
2021-09-24 01:56:37 +03:00
|
|
|
mutationQueryTagsComment <- ask
|
|
|
|
go <$> case mrf of
|
|
|
|
MDBInsert s -> convertInsert userInfo s stringifyNum mutationQueryTagsComment
|
|
|
|
MDBUpdate s -> convertUpdate userInfo s stringifyNum mutationQueryTagsComment
|
|
|
|
MDBDelete s -> convertDelete userInfo s stringifyNum mutationQueryTagsComment
|
|
|
|
MDBFunction returnsSet s -> convertFunction userInfo returnsSet s mutationQueryTagsComment
|
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
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
pgDBSubscriptionPlan ::
|
|
|
|
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))) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m (LiveQueryPlan ('Postgres pgKind) (MultiplexedQuery ('Postgres pgKind)))
|
2021-10-29 17:42:07 +03:00
|
|
|
pgDBSubscriptionPlan userInfo _sourceName sourceConfig namespace unpreparedAST = do
|
2021-09-24 01:56:37 +03:00
|
|
|
(preparedAST, PGL.QueryParametersInfo {..}) <-
|
|
|
|
flip runStateT mempty $
|
|
|
|
for unpreparedAST $ traverse (PGL.resolveMultiplexedValue $ _uiSession userInfo)
|
2021-09-23 15:37:56 +03:00
|
|
|
mutationQueryTagsComment <- 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 =
|
2021-09-24 01:56:37 +03:00
|
|
|
multiplexedQuery {PGL.unMultiplexedQuery = appendSQLWithQueryTags (PGL.unMultiplexedQuery multiplexedQuery) mutationQueryTagsComment}
|
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.
|
2021-09-24 01:56:37 +03:00
|
|
|
validatedQueryVars <- PGL.validateVariables (_pscExecCtx sourceConfig) _qpiReusableVariableValues
|
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
|
|
|
validatedSyntheticVars <- PGL.validateVariables (_pscExecCtx sourceConfig) $ toList _qpiSyntheticVariableValues
|
2021-02-20 16:45:49 +03:00
|
|
|
|
|
|
|
-- TODO validatedQueryVars validatedSyntheticVars
|
2021-09-24 01:56:37 +03:00
|
|
|
let cohortVariables =
|
|
|
|
mkCohortVariables
|
|
|
|
_qpiReferencedSessionVariables
|
|
|
|
(_uiSession userInfo)
|
|
|
|
validatedQueryVars
|
|
|
|
validatedSyntheticVars
|
2021-02-20 16:45:49 +03:00
|
|
|
|
2021-10-29 17:42:07 +03:00
|
|
|
pure $ LiveQueryPlan parameterizedPlan sourceConfig cohortVariables namespace
|
2021-04-22 00:44:37 +03:00
|
|
|
|
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 ->
|
|
|
|
(Tracing.TraceT (Q.TxET QErr IO) 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
|
2021-09-24 01:56:37 +03:00
|
|
|
in (,Just ps) $
|
|
|
|
Tracing.trace "Postgres" $ liftTx $ DS.asSingleRowJsonResp q prepArgs
|
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
|
2021-06-11 06:26:50 +03:00
|
|
|
where
|
2021-09-24 01:56:37 +03:00
|
|
|
mkPreparedSql :: (t -> Q.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
|
|
|
|
|
|
|
|
appendSQLWithQueryTags :: Q.Query -> QueryTagsComment -> Q.Query
|
2021-09-23 15:37:56 +03:00
|
|
|
appendSQLWithQueryTags query queryTags = query {Q.getQueryText = queryText <> _unQueryTagsComment queryTags}
|
2021-07-29 11:29:12 +03:00
|
|
|
where
|
|
|
|
queryText = Q.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) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m (DBStepInfo ('Postgres pgKind))
|
2021-09-22 13:43:05 +03:00
|
|
|
pgDBRemoteRelationshipPlan userInfo sourceName sourceConfig lhs lhsSchema argumentId relationship = do
|
|
|
|
-- 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.
|
2021-09-23 15:37:56 +03:00
|
|
|
flip runReaderT emptyQueryTagsComment $ pgDBQueryPlan userInfo sourceName sourceConfig rootSelection
|
2021-09-22 13:43:05 +03:00
|
|
|
where
|
|
|
|
coerceToColumn = PG.unsafePGCol . getFieldNameTxt
|
|
|
|
joinColumnMapping = mapKeys coerceToColumn lhsSchema
|
|
|
|
|
|
|
|
rowsArgument :: UnpreparedValue ('Postgres pgKind)
|
|
|
|
rowsArgument =
|
2021-09-24 01:56:37 +03:00
|
|
|
UVParameter Nothing $
|
|
|
|
ColumnValue (ColumnScalar PG.PGJSONB) $
|
|
|
|
PG.PGValJSONB $ Q.JSONB $ J.toJSON lhs
|
2021-09-22 13:43:05 +03:00
|
|
|
jsonToRecordSet :: IR.SelectFromG ('Postgres pgKind) (UnpreparedValue ('Postgres pgKind))
|
|
|
|
|
|
|
|
recordSetDefinitionList =
|
2021-09-24 01:56:37 +03:00
|
|
|
(coerceToColumn argumentId, PG.PGBigInt) : Map.toList (fmap snd joinColumnMapping)
|
2021-09-22 13:43:05 +03:00
|
|
|
jsonToRecordSet =
|
|
|
|
IR.FromFunction
|
|
|
|
(PG.QualifiedObject "pg_catalog" $ PG.FunctionName "jsonb_to_recordset")
|
|
|
|
(IR.FunctionArgsExp [IR.AEInput rowsArgument] mempty)
|
|
|
|
(Just recordSetDefinitionList)
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
rootSelection =
|
|
|
|
convertRemoteSourceRelationship
|
|
|
|
(fst <$> joinColumnMapping)
|
|
|
|
jsonToRecordSet
|
|
|
|
(PG.unsafePGCol $ getFieldNameTxt argumentId)
|
|
|
|
(ColumnScalar PG.PGBigInt)
|
|
|
|
relationship
|