graphql-engine/server/src-lib/Hasura/Backends/BigQuery/Plan.hs
Antoine Leblanc 8a77386fcf 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-17 23:13:05 +00:00

88 lines
2.9 KiB
Haskell

-- | Planning T-SQL queries and subscriptions.
module Hasura.Backends.BigQuery.Plan
( planNoPlan
, planToForest
) where
import Hasura.Prelude
import qualified Data.Text.Lazy as LT
import Control.Monad.Validate
import Data.Aeson.Text
import Data.Text.Extended
import Data.Tree
import qualified Hasura.Backends.BigQuery.DataLoader.Plan as DataLoader
import qualified Hasura.Base.Error as E
import qualified Hasura.GraphQL.Parser as GraphQL
import qualified Hasura.RQL.Types.Column as RQL
import Hasura.Backends.BigQuery.FromIr
import Hasura.Backends.BigQuery.Types
import Hasura.RQL.IR
import Hasura.SQL.Backend
import Hasura.SQL.Types
import Hasura.Session
--------------------------------------------------------------------------------
-- Top-level planner
planToForest ::
MonadError E.QErr m
=> UserInfo
-> QueryDB 'BigQuery (Const Void) (GraphQL.UnpreparedValue 'BigQuery)
-> m (Forest DataLoader.PlannedAction)
planToForest userInfo qrf = do
select <- planNoPlan userInfo qrf
let (!_headAndTail, !plannedActionsList) =
DataLoader.runPlan
(DataLoader.planSelectHeadAndTail Nothing Nothing select)
!actionsForest = DataLoader.actionsForest id plannedActionsList
pure actionsForest
planNoPlan ::
MonadError E.QErr m
=> UserInfo
-> QueryDB 'BigQuery (Const Void) (GraphQL.UnpreparedValue 'BigQuery)
-> m Select
planNoPlan userInfo queryDB = do
rootField <- traverseQueryDB (prepareValueNoPlan (_uiSession userInfo)) queryDB
runValidate (runFromIr (fromRootField rootField))
`onLeft` (E.throw400 E.NotSupported . (tshow :: NonEmpty Error -> Text))
--------------------------------------------------------------------------------
-- Resolving values
-- | Prepare a value without any query planning; we just execute the
-- query with the values embedded.
prepareValueNoPlan ::
(MonadError E.QErr m)
=> SessionVariables
-> GraphQL.UnpreparedValue 'BigQuery
-> m Expression
prepareValueNoPlan sessionVariables =
\case
GraphQL.UVLiteral x -> pure x
GraphQL.UVSession -> pure globalSessionExpression
-- To be honest, I'm not sure if it's indeed the JSON_VALUE operator we need here...
GraphQL.UVSessionVar typ text ->
case typ of
CollectableTypeScalar scalarType ->
pure
(CastExpression
(JsonValueExpression
globalSessionExpression
(FieldPath RootPath (toTxt text)))
scalarType)
CollectableTypeArray {} ->
throwError $ E.internalError "Cannot currently prepare array types in BigQuery."
GraphQL.UVParameter _ RQL.ColumnValue {..} -> pure (ValueExpression cvValue)
where
globalSessionExpression =
ValueExpression
(StringValue (LT.toStrict (encodeToLazyText sessionVariables)))