2022-04-08 09:48:37 +03:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
|
2022-05-02 08:03:12 +03:00
|
|
|
module Hasura.Backends.DataConnector.Plan
|
2022-04-08 09:48:37 +03:00
|
|
|
( SourceConfig (..),
|
|
|
|
Plan (..),
|
|
|
|
mkPlan,
|
2022-04-14 05:06:07 +03:00
|
|
|
renderPlan,
|
2022-04-08 09:48:37 +03:00
|
|
|
queryHasRelations,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.Align
|
2022-04-14 05:06:07 +03:00
|
|
|
import Data.ByteString.Lazy qualified as BL
|
2022-04-08 09:48:37 +03:00
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
|
|
|
import Data.List.NonEmpty qualified as NE
|
|
|
|
import Data.Semigroup (Any (..), Min (..))
|
2022-04-28 04:51:58 +03:00
|
|
|
import Data.Text as T
|
2022-04-14 05:06:07 +03:00
|
|
|
import Data.Text.Encoding qualified as TE
|
2022-04-08 09:48:37 +03:00
|
|
|
import Data.These
|
2022-04-28 04:51:58 +03:00
|
|
|
import Data.Vector qualified as V
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.API qualified as API
|
|
|
|
import Hasura.Backends.DataConnector.Adapter.Types
|
|
|
|
import Hasura.Backends.DataConnector.IR.Export qualified as IR
|
|
|
|
import Hasura.Backends.DataConnector.IR.Expression qualified as IR.E
|
|
|
|
import Hasura.Backends.DataConnector.IR.OrderBy qualified as IR.O
|
|
|
|
import Hasura.Backends.DataConnector.IR.Query qualified as IR.Q
|
|
|
|
import Hasura.Backends.DataConnector.IR.Scalar.Value qualified as IR.S
|
2022-04-08 09:48:37 +03:00
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.GraphQL.Parser.Column
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR.BoolExp
|
|
|
|
import Hasura.RQL.IR.OrderBy
|
|
|
|
import Hasura.RQL.IR.Select
|
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.SQL.Backend
|
|
|
|
import Hasura.Session
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-04-28 04:51:58 +03:00
|
|
|
-- | A 'Plan' consists of an 'IR.Q' describing the query to be
|
2022-04-08 09:48:37 +03:00
|
|
|
-- performed by the Agent and a continuation for post processing the
|
|
|
|
-- response. See the 'postProcessResponseRow' haddock for more
|
|
|
|
-- information on why we need a post-processor.
|
|
|
|
data Plan = Plan
|
2022-04-28 04:51:58 +03:00
|
|
|
{ query :: IR.Q.Query,
|
|
|
|
postProcessor :: (API.QueryResponse -> Either ResponseError API.QueryResponse)
|
2022-04-08 09:48:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
-- | Error type for the postProcessor continuation. Failure can occur if the Agent
|
|
|
|
-- returns bad data.
|
|
|
|
data ResponseError
|
|
|
|
= RequiredFieldMissing
|
|
|
|
| UnexpectedFields
|
|
|
|
| ExpectedObject
|
|
|
|
| ExpectedArray
|
|
|
|
| UnexpectedResponseCardinality
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2022-04-28 04:51:58 +03:00
|
|
|
-- | Extract the 'IR.Q' from a 'Plan' and render it as 'Text'.
|
2022-04-14 05:06:07 +03:00
|
|
|
--
|
|
|
|
-- NOTE: This is for logging and debug purposes only.
|
|
|
|
renderPlan :: Plan -> Text
|
|
|
|
renderPlan =
|
2022-04-28 04:51:58 +03:00
|
|
|
TE.decodeUtf8 . BL.toStrict . J.encode . IR.queryToAPI . query
|
2022-04-14 05:06:07 +03:00
|
|
|
|
2022-05-02 08:03:12 +03:00
|
|
|
-- | Map a 'QueryDB 'DataConnector' term into a 'Plan'
|
2022-04-08 09:48:37 +03:00
|
|
|
mkPlan ::
|
|
|
|
forall m.
|
|
|
|
MonadError QErr m =>
|
|
|
|
SessionVariables ->
|
|
|
|
SourceConfig ->
|
2022-05-02 08:03:12 +03:00
|
|
|
QueryDB 'DataConnector Void (UnpreparedValue 'DataConnector) ->
|
2022-04-08 09:48:37 +03:00
|
|
|
m Plan
|
2022-05-02 02:01:11 +03:00
|
|
|
mkPlan session (SourceConfig {_scSchema = API.SchemaResponse {..}}) ir = translateQueryDB ir
|
2022-04-08 09:48:37 +03:00
|
|
|
where
|
|
|
|
translateQueryDB ::
|
2022-05-02 08:03:12 +03:00
|
|
|
QueryDB 'DataConnector Void (UnpreparedValue 'DataConnector) ->
|
2022-04-08 09:48:37 +03:00
|
|
|
m Plan
|
|
|
|
translateQueryDB =
|
|
|
|
traverse replaceSessionVariables >=> \case
|
|
|
|
QDBMultipleRows s -> do
|
2022-04-28 04:51:58 +03:00
|
|
|
query <- translateAnnSelect IR.Q.Many s
|
2022-04-08 09:48:37 +03:00
|
|
|
pure $
|
2022-04-28 04:51:58 +03:00
|
|
|
Plan query $ \API.QueryResponse {getQueryResponse = response} ->
|
|
|
|
fmap API.QueryResponse $ traverse (postProcessResponseRow srCapabilities query) response
|
2022-04-08 09:48:37 +03:00
|
|
|
QDBSingleRow s -> do
|
2022-04-28 04:51:58 +03:00
|
|
|
query <- translateAnnSelect IR.Q.OneOrZero s
|
2022-04-08 09:48:37 +03:00
|
|
|
pure $
|
2022-04-28 04:51:58 +03:00
|
|
|
Plan query $ \API.QueryResponse {getQueryResponse = response} ->
|
|
|
|
fmap API.QueryResponse $ traverse (postProcessResponseRow srCapabilities query) response
|
2022-04-08 09:48:37 +03:00
|
|
|
QDBAggregation {} -> throw400 NotSupported "QDBAggregation: not supported"
|
|
|
|
|
|
|
|
translateAnnSelect ::
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Cardinality ->
|
2022-05-02 08:03:12 +03:00
|
|
|
AnnSimpleSelectG 'DataConnector Void IR.E.Expression ->
|
2022-04-28 04:51:58 +03:00
|
|
|
m IR.Q.Query
|
2022-04-08 09:48:37 +03:00
|
|
|
translateAnnSelect card s = do
|
|
|
|
tableName <- case _asnFrom s of
|
|
|
|
FromTable tn -> pure tn
|
|
|
|
_ -> throw400 NotSupported "SelectFromG: not supported"
|
|
|
|
fields <- translateFields card (_asnFields s)
|
|
|
|
let whereClauseWithPermissions =
|
|
|
|
case _saWhere (_asnArgs s) of
|
|
|
|
Just expr -> BoolAnd [expr, _tpFilter (_asnPerm s)]
|
|
|
|
Nothing -> _tpFilter (_asnPerm s)
|
|
|
|
whereClause <- translateBoolExp whereClauseWithPermissions
|
|
|
|
orderBy <- translateOrderBy (_saOrderBy $ _asnArgs s)
|
|
|
|
pure
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Query
|
2022-04-08 09:48:37 +03:00
|
|
|
{ from = tableName,
|
|
|
|
fields = fields,
|
|
|
|
limit =
|
|
|
|
fmap getMin $
|
|
|
|
foldMap
|
|
|
|
(fmap Min)
|
|
|
|
[ _saLimit (_asnArgs s),
|
|
|
|
_tpLimit (_asnPerm s)
|
|
|
|
],
|
|
|
|
offset = fmap fromIntegral (_saOffset (_asnArgs s)),
|
|
|
|
where_ = Just whereClause,
|
|
|
|
orderBy = orderBy,
|
|
|
|
cardinality = card
|
|
|
|
}
|
|
|
|
|
|
|
|
translateOrderBy ::
|
2022-05-02 08:03:12 +03:00
|
|
|
Maybe (NE.NonEmpty (AnnotatedOrderByItemG 'DataConnector IR.E.Expression)) ->
|
2022-04-28 04:51:58 +03:00
|
|
|
m [IR.O.OrderBy]
|
2022-04-08 09:48:37 +03:00
|
|
|
translateOrderBy = \case
|
|
|
|
Nothing -> pure []
|
|
|
|
Just orderBys ->
|
|
|
|
do
|
|
|
|
NE.toList
|
|
|
|
<$> for orderBys \OrderByItemG {..} -> case obiColumn of
|
|
|
|
AOCColumn (ColumnInfo {ciColumn = dynColumnName}) ->
|
|
|
|
pure
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.O.OrderBy
|
2022-04-08 09:48:37 +03:00
|
|
|
{ column = dynColumnName,
|
|
|
|
-- NOTE: Picking a default ordering.
|
2022-04-28 04:51:58 +03:00
|
|
|
ordering = fromMaybe IR.O.Ascending obiType
|
2022-04-08 09:48:37 +03:00
|
|
|
}
|
2022-05-02 08:03:12 +03:00
|
|
|
_ -> throw400 NotSupported "translateOrderBy: references unsupported in the Data Connector backend"
|
2022-04-08 09:48:37 +03:00
|
|
|
|
|
|
|
translateFields ::
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Cardinality ->
|
2022-05-02 08:03:12 +03:00
|
|
|
AnnFieldsG 'DataConnector Void IR.E.Expression ->
|
2022-04-28 04:51:58 +03:00
|
|
|
m (HashMap Text IR.Q.Field)
|
2022-04-08 09:48:37 +03:00
|
|
|
translateFields card xs = do
|
|
|
|
fields <- traverse (traverse (translateField card)) xs
|
|
|
|
pure $
|
|
|
|
HashMap.fromList $
|
2022-04-14 05:06:07 +03:00
|
|
|
mapMaybe
|
|
|
|
sequence
|
|
|
|
( [ (getFieldNameTxt f, field)
|
2022-04-08 09:48:37 +03:00
|
|
|
| (f, field) <- fields
|
|
|
|
]
|
2022-04-14 05:06:07 +03:00
|
|
|
)
|
2022-04-08 09:48:37 +03:00
|
|
|
|
|
|
|
translateField ::
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Cardinality ->
|
2022-05-02 08:03:12 +03:00
|
|
|
AnnFieldG 'DataConnector Void IR.E.Expression ->
|
2022-04-28 04:51:58 +03:00
|
|
|
m (Maybe IR.Q.Field)
|
2022-04-08 09:48:37 +03:00
|
|
|
translateField _card (AFColumn colField) =
|
|
|
|
-- TODO: make sure certain fields in colField are not in use, since we don't
|
|
|
|
-- support them
|
2022-04-28 04:51:58 +03:00
|
|
|
pure $ Just $ IR.Q.Column (IR.Q.ColumnContents $ _acfColumn colField)
|
2022-04-08 09:48:37 +03:00
|
|
|
translateField card (AFObjectRelation objRel) = do
|
|
|
|
fields <- translateFields card (_aosFields (_aarAnnSelect objRel))
|
|
|
|
whereClause <- translateBoolExp (_aosTableFilter (_aarAnnSelect objRel))
|
|
|
|
pure $
|
|
|
|
Just $
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Relationship $
|
|
|
|
IR.Q.RelationshipContents
|
|
|
|
(HashMap.mapKeys IR.Q.PrimaryKey $ fmap IR.Q.ForeignKey $ _aarColumnMapping objRel)
|
|
|
|
( IR.Q.Query
|
2022-04-08 09:48:37 +03:00
|
|
|
{ fields = fields,
|
|
|
|
from = _aosTableFrom (_aarAnnSelect objRel),
|
|
|
|
where_ = Just whereClause,
|
|
|
|
limit = Nothing,
|
|
|
|
offset = Nothing,
|
|
|
|
orderBy = [],
|
|
|
|
cardinality = card
|
|
|
|
}
|
|
|
|
)
|
|
|
|
translateField _card (AFArrayRelation (ASSimple arrRel)) = do
|
2022-04-28 04:51:58 +03:00
|
|
|
query <- translateAnnSelect IR.Q.Many (_aarAnnSelect arrRel)
|
|
|
|
pure $ Just $ IR.Q.Relationship $ IR.Q.RelationshipContents (HashMap.mapKeys IR.Q.PrimaryKey $ fmap IR.Q.ForeignKey $ _aarColumnMapping arrRel) query
|
2022-04-08 09:48:37 +03:00
|
|
|
translateField _card (AFExpression _literal) =
|
|
|
|
pure Nothing
|
|
|
|
translateField _ _ = throw400 NotSupported "translateField: field type not supported"
|
|
|
|
|
|
|
|
replaceSessionVariables ::
|
2022-05-02 08:03:12 +03:00
|
|
|
UnpreparedValue 'DataConnector ->
|
2022-04-28 04:51:58 +03:00
|
|
|
m IR.E.Expression
|
2022-04-08 09:48:37 +03:00
|
|
|
replaceSessionVariables (UVLiteral e) = pure e
|
2022-04-28 04:51:58 +03:00
|
|
|
replaceSessionVariables (UVParameter _ e) = pure (IR.E.Literal (cvValue e))
|
2022-04-08 09:48:37 +03:00
|
|
|
replaceSessionVariables UVSession = throw400 NotSupported "replaceSessionVariables: UVSession"
|
|
|
|
replaceSessionVariables (UVSessionVar _ v) =
|
|
|
|
case getSessionVariableValue v session of
|
|
|
|
Nothing -> throw400 NotSupported "session var not found"
|
2022-04-28 04:51:58 +03:00
|
|
|
Just s -> pure (IR.E.Literal (IR.S.String s))
|
2022-04-08 09:48:37 +03:00
|
|
|
|
|
|
|
translateBoolExp ::
|
2022-05-02 08:03:12 +03:00
|
|
|
AnnBoolExp 'DataConnector IR.E.Expression ->
|
2022-04-28 04:51:58 +03:00
|
|
|
m IR.E.Expression
|
2022-04-08 09:48:37 +03:00
|
|
|
translateBoolExp (BoolAnd xs) =
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.E.And <$> traverse translateBoolExp xs
|
2022-04-08 09:48:37 +03:00
|
|
|
translateBoolExp (BoolOr xs) =
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.E.Or <$> traverse translateBoolExp xs
|
2022-04-08 09:48:37 +03:00
|
|
|
translateBoolExp (BoolNot x) =
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.E.Not <$> translateBoolExp x
|
2022-04-08 09:48:37 +03:00
|
|
|
translateBoolExp (BoolFld (AVColumn c xs)) =
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.E.And
|
2022-04-08 09:48:37 +03:00
|
|
|
<$> sequence
|
2022-04-28 04:51:58 +03:00
|
|
|
[translateOp (IR.E.Column (ciColumn c)) x | x <- xs]
|
2022-04-08 09:48:37 +03:00
|
|
|
translateBoolExp _ =
|
2022-05-02 08:03:12 +03:00
|
|
|
throw400 NotSupported "An expression type is not supported by the Data Connector backend"
|
2022-04-08 09:48:37 +03:00
|
|
|
|
|
|
|
translateOp ::
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.E.Expression ->
|
2022-05-02 08:03:12 +03:00
|
|
|
OpExpG 'DataConnector IR.E.Expression ->
|
2022-04-28 04:51:58 +03:00
|
|
|
m IR.E.Expression
|
2022-04-08 09:48:37 +03:00
|
|
|
translateOp lhs = \case
|
|
|
|
AEQ _ rhs ->
|
2022-04-28 04:51:58 +03:00
|
|
|
pure $ IR.E.ApplyOperator IR.E.Equal lhs rhs
|
2022-04-08 09:48:37 +03:00
|
|
|
ANE _ rhs ->
|
2022-04-28 04:51:58 +03:00
|
|
|
pure $ IR.E.Not (IR.E.ApplyOperator IR.E.Equal lhs rhs)
|
2022-04-08 09:48:37 +03:00
|
|
|
AGT rhs ->
|
2022-04-28 04:51:58 +03:00
|
|
|
pure $ IR.E.ApplyOperator IR.E.GreaterThan lhs rhs
|
2022-04-08 09:48:37 +03:00
|
|
|
ALT rhs ->
|
2022-04-28 04:51:58 +03:00
|
|
|
pure $ IR.E.ApplyOperator IR.E.LessThan lhs rhs
|
2022-04-08 09:48:37 +03:00
|
|
|
AGTE rhs ->
|
2022-04-28 04:51:58 +03:00
|
|
|
pure $ IR.E.ApplyOperator IR.E.GreaterThanOrEqual lhs rhs
|
2022-04-08 09:48:37 +03:00
|
|
|
ALTE rhs ->
|
2022-04-28 04:51:58 +03:00
|
|
|
pure $ IR.E.ApplyOperator IR.E.LessThanOrEqual lhs rhs
|
2022-04-08 09:48:37 +03:00
|
|
|
ANISNULL ->
|
2022-04-28 04:51:58 +03:00
|
|
|
pure $ IR.E.IsNull lhs
|
2022-04-08 09:48:37 +03:00
|
|
|
ANISNOTNULL ->
|
2022-04-28 04:51:58 +03:00
|
|
|
pure $ IR.E.Not (IR.E.IsNull lhs)
|
|
|
|
AIN (IR.E.Array rhs) -> pure $ IR.E.In lhs rhs
|
2022-05-11 09:14:25 +03:00
|
|
|
ANIN (IR.E.Array rhs) -> pure $ IR.E.Not $ IR.E.In lhs rhs
|
2022-04-08 09:48:37 +03:00
|
|
|
_ ->
|
2022-05-02 08:03:12 +03:00
|
|
|
throw400 NotSupported "An operator is not supported by the Data Connector backend"
|
2022-04-08 09:48:37 +03:00
|
|
|
|
|
|
|
-- | We need to modify any JSON substructures which appear as a result
|
|
|
|
-- of fetching object relationships, to peel off the outer array sent
|
|
|
|
-- by the backend.
|
|
|
|
--
|
|
|
|
-- This function takes a response object, and the 'Plan' used to
|
|
|
|
-- fetch it, and modifies any such arrays accordingly.
|
|
|
|
postProcessResponseRow ::
|
2022-04-28 04:51:58 +03:00
|
|
|
API.Capabilities ->
|
|
|
|
IR.Q.Query ->
|
2022-04-08 09:48:37 +03:00
|
|
|
J.Object ->
|
|
|
|
Either ResponseError J.Object
|
2022-04-28 04:51:58 +03:00
|
|
|
postProcessResponseRow capabilities IR.Q.Query {fields} row =
|
2022-04-08 09:48:37 +03:00
|
|
|
sequenceA $ alignWith go fields row
|
|
|
|
where
|
2022-04-28 04:51:58 +03:00
|
|
|
go :: These IR.Q.Field J.Value -> Either ResponseError J.Value
|
2022-04-08 09:48:37 +03:00
|
|
|
go (This field) =
|
|
|
|
case field of
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Literal literal ->
|
2022-04-08 09:48:37 +03:00
|
|
|
pure (J.String literal)
|
|
|
|
_ ->
|
|
|
|
Left RequiredFieldMissing
|
|
|
|
go That {} =
|
|
|
|
Left UnexpectedFields
|
|
|
|
go (These field value) =
|
|
|
|
case field of
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Literal {} ->
|
2022-04-08 09:48:37 +03:00
|
|
|
Left UnexpectedFields
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Column {} ->
|
2022-04-08 09:48:37 +03:00
|
|
|
pure value
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Relationship (IR.Q.RelationshipContents _ subquery@IR.Q.Query {cardinality}) ->
|
2022-04-08 09:48:37 +03:00
|
|
|
case value of
|
|
|
|
J.Array rows -> do
|
|
|
|
processed <- traverse (postProcessResponseRow capabilities subquery <=< parseObject) (toList rows)
|
|
|
|
applyCardinalityToResponse cardinality processed
|
|
|
|
other
|
2022-04-28 04:51:58 +03:00
|
|
|
| API.dcRelationships capabilities ->
|
2022-04-08 09:48:37 +03:00
|
|
|
Left ExpectedArray
|
|
|
|
| otherwise ->
|
|
|
|
pure other
|
|
|
|
|
|
|
|
parseObject :: J.Value -> Either ResponseError J.Object
|
|
|
|
parseObject = \case
|
|
|
|
J.Object obj -> pure obj
|
|
|
|
_ -> Left ExpectedObject
|
|
|
|
|
|
|
|
-- | If a fk-to-pk lookup comes from an object relationship then we
|
|
|
|
-- expect 0 or 1 items in the response and we should return it as an object.
|
|
|
|
-- if it's an array, we have to send back all of the results
|
2022-04-28 04:51:58 +03:00
|
|
|
applyCardinalityToResponse :: IR.Q.Cardinality -> [J.Object] -> Either ResponseError J.Value
|
|
|
|
applyCardinalityToResponse IR.Q.OneOrZero = \case
|
2022-04-08 09:48:37 +03:00
|
|
|
[] -> pure J.Null
|
|
|
|
[x] -> pure $ J.Object x
|
|
|
|
_ -> Left UnexpectedResponseCardinality
|
2022-04-28 04:51:58 +03:00
|
|
|
applyCardinalityToResponse IR.Q.Many =
|
|
|
|
pure . J.Array . V.fromList . fmap J.Object
|
2022-04-08 09:48:37 +03:00
|
|
|
|
2022-04-28 04:51:58 +03:00
|
|
|
-- | Validate if a 'IR.Q' contains any relationships.
|
|
|
|
queryHasRelations :: IR.Q.Query -> Bool
|
|
|
|
queryHasRelations IR.Q.Query {fields} = getAny $ flip foldMap fields \case
|
|
|
|
IR.Q.Relationship _ -> Any True
|
2022-04-08 09:48:37 +03:00
|
|
|
_ -> Any False
|