2023-03-24 18:15:24 +03:00
|
|
|
{-# LANGUAGE MonadComprehensions #-}
|
|
|
|
|
2021-04-12 13:18:29 +03:00
|
|
|
-- | Planning T-SQL queries and subscriptions.
|
|
|
|
module Hasura.Backends.BigQuery.Plan
|
2021-09-24 01:56:37 +03:00
|
|
|
( planNoPlan,
|
|
|
|
)
|
|
|
|
where
|
2021-05-11 18:18:31 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Control.Monad.Validate
|
|
|
|
import Data.Aeson.Text
|
2023-03-24 18:15:24 +03:00
|
|
|
import Data.List.NonEmpty qualified as NE
|
|
|
|
import Data.Map.Strict qualified as Map
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Text.Extended
|
|
|
|
import Data.Text.Lazy qualified as LT
|
|
|
|
import Hasura.Backends.BigQuery.FromIr as BigQuery
|
|
|
|
import Hasura.Backends.BigQuery.Types
|
|
|
|
import Hasura.Base.Error qualified as E
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR
|
2023-04-24 21:35:48 +03:00
|
|
|
import Hasura.RQL.Types.BackendType
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.RQL.Types.Column qualified as RQL
|
|
|
|
import Hasura.SQL.Types
|
|
|
|
import Hasura.Session
|
2021-04-12 13:18:29 +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-04-12 13:18:29 +03:00
|
|
|
|
|
|
|
planNoPlan ::
|
2023-05-24 16:51:56 +03:00
|
|
|
(MonadError E.QErr m) =>
|
2021-09-24 01:56:37 +03:00
|
|
|
FromIrConfig ->
|
|
|
|
UserInfo ->
|
2022-05-31 01:07:02 +03:00
|
|
|
QueryDB 'BigQuery Void (UnpreparedValue 'BigQuery) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m Select
|
2021-06-25 16:35:39 +03:00
|
|
|
planNoPlan fromIrConfig userInfo queryDB = do
|
2021-07-08 18:41:59 +03:00
|
|
|
rootField <- traverse (prepareValueNoPlan (_uiSession userInfo)) queryDB
|
2023-03-24 18:15:24 +03:00
|
|
|
|
2023-04-13 19:10:38 +03:00
|
|
|
(select, FromIrWriter {fromIrWriterNativeQueries}) <-
|
2023-03-24 18:15:24 +03:00
|
|
|
runValidate (BigQuery.runFromIr fromIrConfig (BigQuery.fromRootField rootField))
|
|
|
|
`onLeft` (E.throw400 E.NotSupported . (tshow :: NonEmpty Error -> Text))
|
|
|
|
|
2023-04-13 19:10:38 +03:00
|
|
|
-- Native queries used within this query need to be converted into CTEs.
|
2023-03-24 18:15:24 +03:00
|
|
|
-- These need to come before any other CTEs in case those CTEs also depend on
|
2023-04-13 19:10:38 +03:00
|
|
|
-- the native queries.
|
|
|
|
let nativeQueries :: Maybe With
|
|
|
|
nativeQueries = do
|
|
|
|
ctes <- NE.nonEmpty (Map.toList fromIrWriterNativeQueries)
|
2023-03-24 18:15:24 +03:00
|
|
|
pure (With [Aliased query (toTxt name) | (name, query) <- ctes])
|
|
|
|
|
2023-04-13 19:10:38 +03:00
|
|
|
pure select {selectWith = nativeQueries <> selectWith select}
|
2021-04-12 13:18:29 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Resolving values
|
|
|
|
|
|
|
|
-- | Prepare a value without any query planning; we just execute the
|
|
|
|
-- query with the values embedded.
|
|
|
|
prepareValueNoPlan ::
|
2021-09-24 01:56:37 +03:00
|
|
|
(MonadError E.QErr m) =>
|
|
|
|
SessionVariables ->
|
2022-05-31 01:07:02 +03:00
|
|
|
UnpreparedValue 'BigQuery ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m Expression
|
2021-04-12 13:18:29 +03:00
|
|
|
prepareValueNoPlan sessionVariables =
|
|
|
|
\case
|
2022-05-31 01:07:02 +03:00
|
|
|
UVLiteral x -> pure x
|
|
|
|
UVSession -> pure globalSessionExpression
|
2021-04-12 13:18:29 +03:00
|
|
|
-- To be honest, I'm not sure if it's indeed the JSON_VALUE operator we need here...
|
2022-05-31 01:07:02 +03:00
|
|
|
UVSessionVar typ text ->
|
2021-04-12 13:18:29 +03:00
|
|
|
case typ of
|
|
|
|
CollectableTypeScalar scalarType ->
|
|
|
|
pure
|
2021-09-24 01:56:37 +03:00
|
|
|
( CastExpression
|
|
|
|
( JsonValueExpression
|
|
|
|
globalSessionExpression
|
|
|
|
(FieldPath RootPath (toTxt text))
|
|
|
|
)
|
|
|
|
scalarType
|
|
|
|
)
|
2021-04-12 13:18:29 +03:00
|
|
|
CollectableTypeArray {} ->
|
2021-05-11 18:18:31 +03:00
|
|
|
throwError $ E.internalError "Cannot currently prepare array types in BigQuery."
|
2022-05-31 01:07:02 +03:00
|
|
|
UVParameter _ RQL.ColumnValue {..} -> pure (ValueExpression cvValue)
|
2021-04-12 13:18:29 +03:00
|
|
|
where
|
|
|
|
globalSessionExpression =
|
|
|
|
ValueExpression
|
|
|
|
(StringValue (LT.toStrict (encodeToLazyText sessionVariables)))
|