2021-02-23 20:37:27 +03:00
|
|
|
-- | Planning T-SQL queries and subscriptions.
|
2021-11-04 19:08:33 +03:00
|
|
|
module Hasura.Backends.MSSQL.Plan
|
|
|
|
( PrepareState (..),
|
|
|
|
planQuery,
|
|
|
|
planSubscription,
|
|
|
|
prepareValueQuery,
|
|
|
|
resultAlias,
|
|
|
|
resultIdAlias,
|
|
|
|
resultVarsAlias,
|
|
|
|
rowAlias,
|
|
|
|
)
|
|
|
|
where
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
-- TODO: Re-add the export list after cleaning up the module
|
|
|
|
-- ( planQuery
|
|
|
|
-- , planSubscription
|
|
|
|
-- ) where
|
|
|
|
|
|
|
|
import Control.Monad.Validate
|
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.ByteString.Lazy (toStrict)
|
|
|
|
import Data.HashMap.Strict qualified as HM
|
|
|
|
import Data.HashMap.Strict.InsOrd qualified as OMap
|
|
|
|
import Data.HashSet qualified as Set
|
|
|
|
import Data.Text qualified as T
|
|
|
|
import Data.Text.Extended
|
|
|
|
import Database.ODBC.SQLServer qualified as ODBC
|
|
|
|
import Hasura.Backends.MSSQL.FromIr
|
2021-11-26 16:47:12 +03:00
|
|
|
import Hasura.Backends.MSSQL.Types.Internal
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.GraphQL.Parser qualified as GraphQL
|
|
|
|
import Hasura.Prelude hiding (first)
|
|
|
|
import Hasura.RQL.IR
|
|
|
|
import Hasura.RQL.Types.Column qualified as RQL
|
|
|
|
import Hasura.SQL.Backend
|
2021-11-20 12:02:49 +03:00
|
|
|
import Hasura.SQL.Types
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Session
|
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
2021-02-23 20:37:27 +03:00
|
|
|
|
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
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Top-level planner
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
planQuery ::
|
|
|
|
MonadError QErr m =>
|
|
|
|
SessionVariables ->
|
|
|
|
QueryDB 'MSSQL (Const Void) (GraphQL.UnpreparedValue 'MSSQL) ->
|
|
|
|
m Select
|
2021-05-21 14:37:34 +03:00
|
|
|
planQuery sessionVariables queryDB = do
|
2021-07-08 18:41:59 +03:00
|
|
|
rootField <- traverse (prepareValueQuery sessionVariables) queryDB
|
2021-07-31 00:41:52 +03:00
|
|
|
runValidate (runFromIr (fromRootField rootField))
|
2021-02-23 20:37:27 +03:00
|
|
|
`onLeft` (throw400 NotSupported . tshow)
|
|
|
|
|
2021-06-01 13:04:29 +03:00
|
|
|
-- | Prepare a value without any query planning; we just execute the
|
|
|
|
-- query with the values embedded.
|
2021-09-24 01:56:37 +03:00
|
|
|
prepareValueQuery ::
|
|
|
|
MonadError QErr m =>
|
|
|
|
SessionVariables ->
|
|
|
|
GraphQL.UnpreparedValue 'MSSQL ->
|
|
|
|
m Expression
|
2021-06-01 13:04:29 +03:00
|
|
|
prepareValueQuery sessionVariables =
|
|
|
|
{- History note:
|
|
|
|
This function used to be called 'planNoPlan', and was used for building sql
|
|
|
|
expressions for queries. That evolved differently, but this function is now
|
|
|
|
left as a *suggestion* for implementing support for mutations.
|
|
|
|
-}
|
|
|
|
\case
|
|
|
|
GraphQL.UVLiteral x -> pure x
|
|
|
|
GraphQL.UVSession -> pure $ ValueExpression $ ODBC.ByteStringValue $ toStrict $ J.encode sessionVariables
|
2021-09-24 01:56:37 +03:00
|
|
|
GraphQL.UVParameter _ RQL.ColumnValue {..} -> pure $ ValueExpression cvValue
|
2021-11-20 12:02:49 +03:00
|
|
|
GraphQL.UVSessionVar typ sessionVariable -> do
|
2021-09-24 01:56:37 +03:00
|
|
|
value <-
|
|
|
|
getSessionVariableValue sessionVariable sessionVariables
|
|
|
|
`onNothing` throw400 NotFound ("missing session variable: " <>> sessionVariable)
|
2021-11-20 12:02:49 +03:00
|
|
|
-- See https://github.com/fpco/odbc/pull/34#issuecomment-812223147
|
|
|
|
-- We first cast to nvarchar(max) because casting from ntext is not supported
|
|
|
|
CastExpression (CastExpression (ValueExpression $ ODBC.TextValue value) "nvarchar(max)")
|
|
|
|
<$> case typ of
|
|
|
|
CollectableTypeScalar baseTy ->
|
|
|
|
pure (scalarTypeDBName baseTy)
|
|
|
|
CollectableTypeArray {} ->
|
|
|
|
throw400 NotSupported "Array types are currently not supported in MS SQL Server"
|
2021-06-01 13:04:29 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
planSubscription ::
|
|
|
|
MonadError QErr m =>
|
|
|
|
OMap.InsOrdHashMap G.Name (QueryDB 'MSSQL (Const Void) (GraphQL.UnpreparedValue 'MSSQL)) ->
|
|
|
|
SessionVariables ->
|
|
|
|
m (Reselect, PrepareState)
|
2021-05-21 14:37:34 +03:00
|
|
|
planSubscription unpreparedMap sessionVariables = do
|
2021-07-31 00:41:52 +03:00
|
|
|
(rootFieldMap, prepareState) <-
|
2021-09-24 01:56:37 +03:00
|
|
|
runStateT
|
|
|
|
( traverse
|
|
|
|
(traverse (prepareValueSubscription (getSessionVariablesSet sessionVariables)))
|
|
|
|
unpreparedMap
|
|
|
|
)
|
|
|
|
emptyPrepareState
|
2021-04-20 19:57:14 +03:00
|
|
|
selectMap <-
|
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
|
|
|
runValidate (runFromIr (traverse fromRootField rootFieldMap))
|
2021-09-24 01:56:37 +03:00
|
|
|
`onLeft` (throw400 NotSupported . tshow)
|
2021-04-20 19:57:14 +03:00
|
|
|
pure (collapseMap selectMap, prepareState)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
-- Plan a query without prepare/exec.
|
|
|
|
-- planNoPlanMap ::
|
|
|
|
-- OMap.InsOrdHashMap G.Name (SubscriptionRootFieldMSSQL (GraphQL.UnpreparedValue 'MSSQL))
|
|
|
|
-- -> Either PrepareError Reselect
|
|
|
|
-- planNoPlanMap _unpreparedMap =
|
|
|
|
-- let rootFieldMap = runIdentity $
|
|
|
|
-- traverse (traverseQueryRootField (pure . prepareValueNoPlan)) unpreparedMap
|
|
|
|
-- selectMap <-
|
|
|
|
-- first
|
|
|
|
-- FromIrError
|
|
|
|
-- (runValidate (runFromIr (traverse fromRootField rootFieldMap)))
|
|
|
|
-- pure (collapseMap selectMap)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Converting a root field into a T-SQL select statement
|
|
|
|
|
|
|
|
-- | Collapse a set of selects into a single select that projects
|
|
|
|
-- these as subselects.
|
2021-09-24 01:56:37 +03:00
|
|
|
collapseMap ::
|
|
|
|
OMap.InsOrdHashMap G.Name Select ->
|
|
|
|
Reselect
|
2021-02-23 20:37:27 +03:00
|
|
|
collapseMap selects =
|
|
|
|
Reselect
|
|
|
|
{ reselectFor =
|
2021-09-24 01:56:37 +03:00
|
|
|
JsonFor ForJson {jsonCardinality = JsonSingleton, jsonRoot = NoRoot},
|
|
|
|
reselectWhere = Where mempty,
|
|
|
|
reselectProjections =
|
2021-02-23 20:37:27 +03:00
|
|
|
map projectSelect (OMap.toList selects)
|
|
|
|
}
|
|
|
|
where
|
|
|
|
projectSelect :: (G.Name, Select) -> Projection
|
2021-06-08 06:50:24 +03:00
|
|
|
projectSelect (name, sel) =
|
2021-02-23 20:37:27 +03:00
|
|
|
ExpressionProjection
|
2021-09-24 01:56:37 +03:00
|
|
|
( Aliased
|
|
|
|
{ aliasedThing = SelectExpression sel,
|
|
|
|
aliasedAlias = G.unName name
|
|
|
|
}
|
|
|
|
)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Session variables
|
|
|
|
|
2021-11-04 19:08:33 +03:00
|
|
|
-- globalSessionExpression :: Expression
|
|
|
|
-- globalSessionExpression =
|
|
|
|
-- ValueExpression (ODBC.TextValue "current_setting('hasura.user')::json")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Resolving values
|
|
|
|
|
2021-11-04 19:08:33 +03:00
|
|
|
-- data PrepareError
|
|
|
|
-- = FromIrError (NonEmpty Error)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
data PrepareState = PrepareState
|
2021-09-24 01:56:37 +03:00
|
|
|
{ positionalArguments :: ![RQL.ColumnValue 'MSSQL],
|
|
|
|
namedArguments :: !(HashMap G.Name (RQL.ColumnValue 'MSSQL)),
|
|
|
|
sessionVariables :: !(Set.HashSet SessionVariable)
|
2021-02-23 20:37:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
emptyPrepareState :: PrepareState
|
2021-09-24 01:56:37 +03:00
|
|
|
emptyPrepareState =
|
|
|
|
PrepareState
|
|
|
|
{ positionalArguments = mempty,
|
|
|
|
namedArguments = mempty,
|
|
|
|
sessionVariables = mempty
|
|
|
|
}
|
2021-05-21 14:37:34 +03:00
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
-- | Prepare a value for multiplexed queries.
|
2021-09-24 01:56:37 +03:00
|
|
|
prepareValueSubscription ::
|
|
|
|
(MonadState PrepareState m, MonadError QErr m) =>
|
|
|
|
Set.HashSet SessionVariable ->
|
|
|
|
GraphQL.UnpreparedValue 'MSSQL ->
|
|
|
|
m Expression
|
2021-05-21 14:37:34 +03:00
|
|
|
prepareValueSubscription globalVariables =
|
2021-02-23 20:37:27 +03:00
|
|
|
\case
|
|
|
|
GraphQL.UVLiteral x -> pure x
|
2021-04-20 19:57:14 +03:00
|
|
|
GraphQL.UVSession -> do
|
|
|
|
modify' (\s -> s {sessionVariables = sessionVariables s <> globalVariables})
|
2021-05-21 14:37:34 +03:00
|
|
|
pure $ resultVarExp (RootPath `FieldPath` "session")
|
2021-02-23 20:37:27 +03:00
|
|
|
GraphQL.UVSessionVar _typ text -> do
|
2021-07-31 00:41:52 +03:00
|
|
|
if Set.member text globalVariables
|
|
|
|
then pure ()
|
2021-09-24 01:56:37 +03:00
|
|
|
else
|
|
|
|
throw400
|
|
|
|
NotFound
|
|
|
|
("missing session variable: " <>> sessionVariableToText text)
|
2021-02-23 20:37:27 +03:00
|
|
|
modify' (\s -> s {sessionVariables = text `Set.insert` sessionVariables s})
|
2021-05-21 14:37:34 +03:00
|
|
|
pure $ resultVarExp (sessionDot $ toTxt text)
|
2021-04-20 19:57:14 +03:00
|
|
|
GraphQL.UVParameter mVariableInfo columnValue ->
|
2021-02-23 20:37:27 +03:00
|
|
|
case fmap GraphQL.getName mVariableInfo of
|
|
|
|
Nothing -> do
|
2021-07-31 00:41:52 +03:00
|
|
|
currentIndex <- toInteger . length <$> gets positionalArguments
|
2021-09-24 01:56:37 +03:00
|
|
|
modify'
|
|
|
|
( \s ->
|
|
|
|
s
|
|
|
|
{ positionalArguments = positionalArguments s <> [columnValue]
|
|
|
|
}
|
|
|
|
)
|
2021-05-21 14:37:34 +03:00
|
|
|
pure (resultVarExp (syntheticIx currentIndex))
|
2021-02-23 20:37:27 +03:00
|
|
|
Just name -> do
|
|
|
|
modify
|
2021-09-24 01:56:37 +03:00
|
|
|
( \s ->
|
|
|
|
s
|
|
|
|
{ namedArguments =
|
|
|
|
HM.insert name columnValue (namedArguments s)
|
|
|
|
}
|
|
|
|
)
|
2021-05-21 14:37:34 +03:00
|
|
|
pure $ resultVarExp (queryDot $ G.unName name)
|
2021-09-24 01:56:37 +03:00
|
|
|
where
|
|
|
|
resultVarExp :: JsonPath -> Expression
|
|
|
|
resultVarExp =
|
|
|
|
JsonValueExpression $
|
|
|
|
ColumnExpression $
|
|
|
|
FieldName
|
|
|
|
{ fieldNameEntity = rowAlias,
|
|
|
|
fieldName = resultVarsAlias
|
|
|
|
}
|
|
|
|
|
|
|
|
queryDot :: Text -> JsonPath
|
|
|
|
queryDot name = RootPath `FieldPath` "query" `FieldPath` name
|
|
|
|
|
|
|
|
syntheticIx :: Integer -> JsonPath
|
|
|
|
syntheticIx i = RootPath `FieldPath` "synthetic" `IndexPath` i
|
|
|
|
|
|
|
|
sessionDot :: Text -> JsonPath
|
|
|
|
sessionDot name = RootPath `FieldPath` "session" `FieldPath` name
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
resultIdAlias :: T.Text
|
|
|
|
resultIdAlias = "result_id"
|
|
|
|
|
|
|
|
resultVarsAlias :: T.Text
|
|
|
|
resultVarsAlias = "result_vars"
|
|
|
|
|
|
|
|
resultAlias :: T.Text
|
|
|
|
resultAlias = "result"
|
|
|
|
|
|
|
|
rowAlias :: T.Text
|
|
|
|
rowAlias = "row"
|