graphql-engine/server/src-lib/Hasura/Backends/BigQuery/Plan.hs
Antoine Leblanc d91029ad51 [gardening] remove all traverse functions from RQL.IR
### Description

This PR removes all `fmapX` and `traverseX` functions from RQL.IR, favouring instead `Functor` and `Traversable` instances throughout the code. This was a relatively straightforward change, except for two small pain points: `AnnSelectG` and `AnnInsert`. Both were parametric over two types `a` and `v`, making it impossible to make them traversable functors... But it turns out that in every single use case, `a ~ f v`. By changing those types to take such an `f :: Type -> Type` as an argument instead of `a :: Type` makes it possible to make them functors.

The only small difference is for `AnnIns`, I had to introduce one `Identity` transformation for one of the `f` parameters. This is relatively straightforward.

### Notes

This PR fixes the most verbose BigQuery hint (`let` instead of `<- pure`).

https://github.com/hasura/graphql-engine-mono/pull/1668

GitOrigin-RevId: e632263a8c559aa04aeae10dcaec915b4a81ad1a
2021-07-08 15:42:53 +00:00

73 lines
2.4 KiB
Haskell

-- | Planning T-SQL queries and subscriptions.
module Hasura.Backends.BigQuery.Plan
( planNoPlan
) where
import Hasura.Prelude
import qualified Data.Text.Lazy as LT
import Control.Monad.Validate
import Data.Aeson.Text
import Data.Text.Extended
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 as BigQuery
import Hasura.Backends.BigQuery.Types
import Hasura.RQL.IR
import Hasura.SQL.Backend
import Hasura.SQL.Types
import Hasura.Session
--------------------------------------------------------------------------------
-- Top-level planner
planNoPlan ::
MonadError E.QErr m
=> FromIrConfig
-> UserInfo
-> QueryDB 'BigQuery (Const Void) (GraphQL.UnpreparedValue 'BigQuery)
-> m Select
planNoPlan fromIrConfig userInfo queryDB = do
rootField <- traverse (prepareValueNoPlan (_uiSession userInfo)) queryDB
runValidate (BigQuery.runFromIr fromIrConfig (BigQuery.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)))