2021-10-12 14:33:33 +03:00
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
|
2021-10-12 19:13:15 +03:00
|
|
|
-- |
|
|
|
|
--
|
|
|
|
-- Execute the plan given from .Plan.
|
2021-11-04 19:08:33 +03:00
|
|
|
module Hasura.Backends.MySQL.DataLoader.Execute
|
|
|
|
( OutputValue (..),
|
|
|
|
RecordSet (..),
|
2021-12-01 15:49:30 +03:00
|
|
|
ExecuteProblem (..),
|
2021-11-04 19:08:33 +03:00
|
|
|
execute,
|
|
|
|
runExecute,
|
2021-12-01 15:49:30 +03:00
|
|
|
-- for testing
|
|
|
|
joinObjectRows,
|
|
|
|
leftObjectJoin,
|
2021-11-04 19:08:33 +03:00
|
|
|
)
|
|
|
|
where
|
2021-10-12 14:33:33 +03:00
|
|
|
|
2021-10-12 19:13:15 +03:00
|
|
|
import Control.Monad.IO.Class
|
|
|
|
import Data.Aeson hiding (Value)
|
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.Bifunctor
|
|
|
|
import Data.Foldable
|
|
|
|
import Data.Graph
|
|
|
|
import Data.HashMap.Strict.InsOrd qualified as OMap
|
|
|
|
import Data.IORef
|
|
|
|
import Data.Vector (Vector)
|
|
|
|
import Data.Vector qualified as V
|
|
|
|
import GHC.TypeLits qualified
|
2021-10-22 02:50:18 +03:00
|
|
|
import Hasura.Backends.MySQL.Connection (runQueryYieldingRows)
|
2021-12-01 15:49:30 +03:00
|
|
|
import Hasura.Backends.MySQL.DataLoader.Plan
|
|
|
|
( Action (..),
|
|
|
|
FieldName (..),
|
|
|
|
HeadAndTail (..),
|
|
|
|
Join
|
|
|
|
( joinFieldName,
|
|
|
|
joinRhsOffset,
|
|
|
|
joinRhsTop,
|
|
|
|
joinType,
|
|
|
|
leftRecordSet,
|
|
|
|
rightRecordSet
|
|
|
|
),
|
|
|
|
PlannedAction (..),
|
|
|
|
Ref,
|
|
|
|
selectQuery,
|
|
|
|
toFieldName,
|
2021-10-12 19:13:15 +03:00
|
|
|
)
|
|
|
|
import Hasura.Backends.MySQL.DataLoader.Plan qualified as DataLoaderPlan
|
|
|
|
import Hasura.Backends.MySQL.DataLoader.Plan qualified as Plan
|
2021-10-22 02:50:18 +03:00
|
|
|
import Hasura.Backends.MySQL.ToQuery (fromSelect, toQueryFlat)
|
2021-10-12 19:13:15 +03:00
|
|
|
import Hasura.Backends.MySQL.Types hiding
|
|
|
|
( FieldName,
|
|
|
|
ScalarValue,
|
|
|
|
selectWhere,
|
|
|
|
)
|
2021-11-04 19:08:33 +03:00
|
|
|
-- import Hasura.Backends.MySQL.Types qualified as MySQL
|
2021-10-12 19:13:15 +03:00
|
|
|
import Hasura.GraphQL.Parser ()
|
|
|
|
-- Brings an instance for Hashable (Vector a)...
|
|
|
|
import Hasura.Prelude hiding
|
|
|
|
( concatMap,
|
|
|
|
elem,
|
|
|
|
head,
|
|
|
|
map,
|
|
|
|
mapMaybe,
|
|
|
|
tail,
|
|
|
|
toList,
|
|
|
|
)
|
2021-10-12 14:33:33 +03:00
|
|
|
|
|
|
|
-- | A set of records produced by the database. These are joined
|
|
|
|
-- together. There are all sorts of optimizations possible here, from
|
|
|
|
-- using a matrix/flat vector, unboxed sums for Value, etc. Presently
|
|
|
|
-- we choose a naive implementation in the interest of getting other
|
|
|
|
-- work done.
|
|
|
|
data RecordSet = RecordSet
|
2022-07-29 17:05:03 +03:00
|
|
|
{ origin :: Maybe PlannedAction,
|
|
|
|
rows :: Vector (InsOrdHashMap FieldName OutputValue),
|
|
|
|
wantedFields :: Maybe [Text]
|
2021-10-12 19:13:15 +03:00
|
|
|
}
|
|
|
|
deriving (Show)
|
|
|
|
|
2021-10-12 14:33:33 +03:00
|
|
|
instance GHC.TypeLits.TypeError ('GHC.TypeLits.Text "Aeson loses key order, so you can't use this instance.") => ToJSON RecordSet where
|
|
|
|
toJSON RecordSet {} = error "RecordSet.toJSON: do not use."
|
|
|
|
|
|
|
|
-- | The read-only info. used by the Execute monad. Later, this IORef
|
|
|
|
-- may become either atomically modified or in an STM or MVar so that
|
|
|
|
-- jobs can be executed in parallel.
|
|
|
|
data ExecuteReader = ExecuteReader
|
2021-10-12 19:13:15 +03:00
|
|
|
{ recordSets :: IORef (InsOrdHashMap Ref RecordSet),
|
2022-07-29 17:05:03 +03:00
|
|
|
credentials :: SourceConfig
|
2021-10-12 14:33:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
-- | Any problem encountered while executing the plan.
|
|
|
|
data ExecuteProblem
|
2021-10-23 14:42:20 +03:00
|
|
|
= GetJobDecodeProblem String
|
|
|
|
| CreateQueryJobDecodeProblem String
|
|
|
|
| JoinProblem ExecuteProblem
|
2021-10-12 14:33:33 +03:00
|
|
|
| UnsupportedJoinBug JoinType
|
|
|
|
| MissingRecordSetBug Ref
|
2021-12-01 15:49:30 +03:00
|
|
|
| BrokenJoinInvariant [DataLoaderPlan.FieldName]
|
2021-10-12 14:33:33 +03:00
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
-- | Execute monad; as queries are performed, the record sets are
|
|
|
|
-- stored in the map.
|
2021-10-12 19:13:15 +03:00
|
|
|
newtype Execute a = Execute
|
|
|
|
{unExecute :: ReaderT ExecuteReader (ExceptT ExecuteProblem IO) a}
|
2021-10-12 14:33:33 +03:00
|
|
|
deriving
|
2021-10-12 19:13:15 +03:00
|
|
|
( Functor,
|
|
|
|
Applicative,
|
|
|
|
Monad,
|
|
|
|
MonadReader ExecuteReader,
|
|
|
|
MonadIO,
|
|
|
|
MonadError ExecuteProblem
|
2021-10-12 14:33:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
-- | A value outputted by this execute module in a record set.
|
|
|
|
data OutputValue
|
2022-07-29 17:05:03 +03:00
|
|
|
= ArrayOutputValue (Vector OutputValue)
|
|
|
|
| RecordOutputValue (InsOrdHashMap DataLoaderPlan.FieldName OutputValue)
|
|
|
|
| ScalarOutputValue J.Value -- TODO: switch to 'MySQL.Scalar...'?
|
2021-10-12 14:33:33 +03:00
|
|
|
| NullOutputValue
|
|
|
|
deriving (Show, Eq, Generic)
|
2021-10-12 19:13:15 +03:00
|
|
|
|
2021-10-12 14:33:33 +03:00
|
|
|
instance Hashable OutputValue
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Main entry points
|
|
|
|
|
|
|
|
-- | Using the config, run the execute action. Finally, resolve the
|
|
|
|
-- head-and-tail to a record set.
|
2021-10-12 19:13:15 +03:00
|
|
|
runExecute ::
|
|
|
|
MonadIO m =>
|
|
|
|
SourceConfig ->
|
|
|
|
HeadAndTail ->
|
|
|
|
Execute a ->
|
|
|
|
m (Either ExecuteProblem RecordSet)
|
2021-10-12 14:33:33 +03:00
|
|
|
runExecute credentials headAndTail action = do
|
|
|
|
recordSets <- liftIO (newIORef mempty)
|
|
|
|
liftIO $
|
|
|
|
runExceptT $
|
|
|
|
runReaderT
|
|
|
|
(unExecute (action >> getFinalRecordSet headAndTail))
|
|
|
|
(ExecuteReader {credentials, recordSets})
|
|
|
|
|
|
|
|
-- | Execute the forest of actions.
|
|
|
|
execute :: Forest PlannedAction -> Execute ()
|
|
|
|
execute = traverse_ (traverse_ executePlannedAction)
|
|
|
|
|
|
|
|
-- | Execute an action, then store its result in the ref assigned to it.
|
|
|
|
executePlannedAction :: PlannedAction -> Execute ()
|
|
|
|
executePlannedAction PlannedAction {ref, action} =
|
|
|
|
fetchRecordSetForAction action >>= saveRecordSet ref
|
|
|
|
|
|
|
|
-- | Fetch the record set for the given action.
|
|
|
|
fetchRecordSetForAction :: Action -> Execute RecordSet
|
|
|
|
fetchRecordSetForAction =
|
|
|
|
\case
|
|
|
|
SelectAction select -> do
|
2021-10-22 02:50:18 +03:00
|
|
|
recordSet <- do
|
|
|
|
SourceConfig {scConnectionPool} <- asks credentials
|
|
|
|
result <-
|
|
|
|
liftIO $
|
|
|
|
runExceptT $
|
|
|
|
runQueryYieldingRows
|
|
|
|
scConnectionPool
|
|
|
|
(toQueryFlat (fromSelect (selectQuery select)))
|
|
|
|
case result of
|
|
|
|
Left problem -> throwError (JoinProblem problem)
|
|
|
|
Right rows -> pure (makeRecordSet rows)
|
2021-10-12 14:33:33 +03:00
|
|
|
-- Update the wanted fields from the original select. This lets
|
|
|
|
-- the executor know which fields to include after performing a
|
|
|
|
-- join.
|
|
|
|
pure recordSet {wantedFields = Plan.selectWantedFields select}
|
2021-10-14 20:06:55 +03:00
|
|
|
JoinAction Plan.Join {joinType = joinType', joinFieldName = fieldName, ..} -> do
|
2021-10-12 14:33:33 +03:00
|
|
|
left <- getRecordSet leftRecordSet
|
|
|
|
right <- getRecordSet rightRecordSet
|
2021-10-14 20:06:55 +03:00
|
|
|
case joinType' of
|
2021-10-12 14:33:33 +03:00
|
|
|
ArrayJoin fields ->
|
2021-12-01 15:49:30 +03:00
|
|
|
leftArrayJoin
|
2021-10-12 19:13:15 +03:00
|
|
|
wantedFields
|
2021-10-14 20:06:55 +03:00
|
|
|
fieldName
|
2021-10-12 19:13:15 +03:00
|
|
|
(toFieldNames fields)
|
|
|
|
joinRhsTop
|
|
|
|
joinRhsOffset
|
|
|
|
left
|
2021-12-01 15:49:30 +03:00
|
|
|
right
|
|
|
|
`onLeft` (throwError . JoinProblem)
|
2021-10-12 14:33:33 +03:00
|
|
|
ObjectJoin fields ->
|
2021-12-01 15:49:30 +03:00
|
|
|
leftObjectJoin
|
2021-10-12 19:13:15 +03:00
|
|
|
wantedFields
|
2021-10-14 20:06:55 +03:00
|
|
|
fieldName
|
2021-10-12 19:13:15 +03:00
|
|
|
(toFieldNames fields)
|
|
|
|
left
|
2021-12-01 15:49:30 +03:00
|
|
|
right
|
|
|
|
`onLeft` (throwError . JoinProblem)
|
2021-10-14 20:06:55 +03:00
|
|
|
_ -> throwError (UnsupportedJoinBug joinType')
|
2021-10-12 14:33:33 +03:00
|
|
|
where
|
|
|
|
toFieldNames = fmap (bimap toFieldName toFieldName)
|
|
|
|
|
|
|
|
-- | Make a record set from a flat record from the DB.
|
|
|
|
makeRecordSet :: Vector (InsOrdHashMap FieldName J.Value) -> RecordSet
|
|
|
|
makeRecordSet rows =
|
|
|
|
RecordSet
|
2021-10-12 19:13:15 +03:00
|
|
|
{ origin = Nothing, -- No information for this yet, but will follow
|
|
|
|
-- up with a change for this later.
|
|
|
|
rows = fmap (fmap ScalarOutputValue) rows,
|
|
|
|
wantedFields = Nothing
|
2021-10-12 14:33:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
saveRecordSet :: Ref -> RecordSet -> Execute ()
|
|
|
|
saveRecordSet ref recordSet = do
|
|
|
|
recordSetsRef <- asks recordSets
|
|
|
|
liftIO (modifyIORef' recordSetsRef (OMap.insert ref recordSet))
|
|
|
|
|
|
|
|
getRecordSet :: Ref -> Execute RecordSet
|
|
|
|
getRecordSet ref = do
|
|
|
|
recordSetsRef <- asks recordSets
|
|
|
|
hash <- liftIO (readIORef recordSetsRef)
|
2021-12-01 15:49:30 +03:00
|
|
|
OMap.lookup ref hash `onNothing` throwError (MissingRecordSetBug ref)
|
2021-10-12 14:33:33 +03:00
|
|
|
|
|
|
|
-- | See documentation for 'HeadAndTail'.
|
|
|
|
getFinalRecordSet :: HeadAndTail -> Execute RecordSet
|
|
|
|
getFinalRecordSet HeadAndTail {..} = do
|
|
|
|
headSet <- getRecordSet head
|
|
|
|
tailSet <-
|
|
|
|
if tail /= head
|
|
|
|
then getRecordSet tail
|
|
|
|
else pure headSet
|
|
|
|
pure
|
|
|
|
tailSet
|
|
|
|
{ rows =
|
|
|
|
fmap
|
2021-12-01 15:49:30 +03:00
|
|
|
( OMap.filterWithKey
|
|
|
|
( \(FieldName k) _ ->
|
2022-10-04 00:49:32 +03:00
|
|
|
all (elem k) (wantedFields headSet)
|
2021-12-01 15:49:30 +03:00
|
|
|
)
|
2021-10-12 19:13:15 +03:00
|
|
|
)
|
2021-10-12 14:33:33 +03:00
|
|
|
(rows tailSet)
|
|
|
|
}
|
|
|
|
|
2021-11-04 19:08:33 +03:00
|
|
|
{- WIP, it seems:
|
2021-10-12 14:33:33 +03:00
|
|
|
-- | Make an lhs_fk IN (rhs_fk1, rhs_fk2, ..) expression list.
|
|
|
|
makeRelationshipIn :: DataLoaderPlan.Relationship -> Execute [Expression]
|
2021-10-12 19:13:15 +03:00
|
|
|
makeRelationshipIn
|
|
|
|
DataLoaderPlan.Relationship
|
|
|
|
{ leftRecordSet,
|
|
|
|
joinType = _,
|
|
|
|
rightTable = _rightTable
|
|
|
|
} = do
|
|
|
|
RecordSet {rows = _rows} <- getRecordSet leftRecordSet
|
|
|
|
-- TODO: A follow-up PR will add IN(..) and will join on the join
|
|
|
|
-- fields for the left/right tables. It needs support from Types.hs.
|
|
|
|
pure []
|
|
|
|
where
|
|
|
|
_lookupField' k row =
|
|
|
|
case OMap.lookup k row of
|
|
|
|
Nothing -> Nothing
|
|
|
|
Just x -> Just x
|
2021-10-12 14:33:33 +03:00
|
|
|
|
|
|
|
-- | Will be used by makeRelationshipIn for forming lhs_fk IN (rhs_fk1, rhs_fk2, ..)
|
|
|
|
planFieldNameToQueryFieldName :: EntityAlias -> FieldName -> MySQL.FieldName
|
|
|
|
planFieldNameToQueryFieldName (EntityAlias fieldNameEntity) (FieldName fieldName) =
|
|
|
|
MySQL.FieldName {fNameEntity = fieldNameEntity, fName = fieldName}
|
2021-11-04 19:08:33 +03:00
|
|
|
-}
|
2021-10-12 14:33:33 +03:00
|
|
|
|
|
|
|
-- | Inefficient but clean left object join.
|
|
|
|
leftObjectJoin ::
|
2021-10-12 19:13:15 +03:00
|
|
|
Maybe [Text] ->
|
|
|
|
Text ->
|
|
|
|
[(DataLoaderPlan.FieldName, DataLoaderPlan.FieldName)] ->
|
|
|
|
RecordSet ->
|
|
|
|
RecordSet ->
|
|
|
|
Either ExecuteProblem RecordSet
|
2021-12-01 15:49:30 +03:00
|
|
|
leftObjectJoin wantedFields joinAlias joinFields left right = do
|
|
|
|
rows' <- fmap V.fromList . traverse makeRows . toList $ rows left
|
2021-10-12 14:33:33 +03:00
|
|
|
pure
|
|
|
|
RecordSet
|
2021-10-12 19:13:15 +03:00
|
|
|
{ origin = Nothing,
|
|
|
|
wantedFields = Nothing,
|
2021-12-01 15:49:30 +03:00
|
|
|
rows = rows'
|
2021-10-12 14:33:33 +03:00
|
|
|
}
|
2021-12-01 15:49:30 +03:00
|
|
|
where
|
|
|
|
makeRows :: InsOrdHashMap FieldName OutputValue -> Either ExecuteProblem (InsOrdHashMap FieldName OutputValue)
|
|
|
|
makeRows leftRow =
|
|
|
|
let rightRows =
|
|
|
|
V.fromList
|
|
|
|
[ rightRow
|
|
|
|
| not (null joinFields),
|
|
|
|
rightRow <- toList (rows right),
|
|
|
|
all
|
|
|
|
( \(rightField, leftField) ->
|
|
|
|
Just True
|
|
|
|
== ( do
|
|
|
|
leftValue <- OMap.lookup leftField leftRow
|
|
|
|
rightValue <- OMap.lookup rightField rightRow
|
|
|
|
pure (leftValue == rightValue)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
joinFields
|
|
|
|
]
|
|
|
|
in -- The line below will return Left is rightRows has more than one element.
|
|
|
|
-- Consider moving the check here if it makes sense in the future.
|
|
|
|
joinObjectRows wantedFields joinAlias leftRow rightRows
|
2021-10-12 14:33:33 +03:00
|
|
|
|
|
|
|
-- | A naive, exponential reference implementation of a left join. It
|
|
|
|
-- serves as a trivial sample implementation for correctness checking
|
|
|
|
-- of more efficient ones.
|
|
|
|
leftArrayJoin ::
|
2021-10-12 19:13:15 +03:00
|
|
|
Maybe [Text] ->
|
|
|
|
Text ->
|
|
|
|
[(DataLoaderPlan.FieldName, DataLoaderPlan.FieldName)] ->
|
|
|
|
Top ->
|
|
|
|
Maybe Int ->
|
|
|
|
RecordSet ->
|
|
|
|
RecordSet ->
|
|
|
|
Either ExecuteProblem RecordSet
|
2021-10-12 14:33:33 +03:00
|
|
|
leftArrayJoin wantedFields joinAlias joinFields rhsTop rhsOffset left right =
|
|
|
|
pure
|
|
|
|
RecordSet
|
2021-10-12 19:13:15 +03:00
|
|
|
{ origin = Nothing,
|
|
|
|
wantedFields = Nothing,
|
|
|
|
rows =
|
2021-10-12 14:33:33 +03:00
|
|
|
V.fromList
|
|
|
|
[ joinArrayRows wantedFields joinAlias leftRow rightRows
|
2021-10-12 19:13:15 +03:00
|
|
|
| leftRow <- toList (rows left),
|
|
|
|
let rightRows =
|
|
|
|
V.fromList
|
|
|
|
( limit
|
|
|
|
( offset
|
|
|
|
[ rightRow
|
2021-12-01 15:49:30 +03:00
|
|
|
| not (null joinFields),
|
|
|
|
rightRow <- toList (rows right),
|
2021-10-12 19:13:15 +03:00
|
|
|
all
|
|
|
|
( \(rightField, leftField) ->
|
2021-12-01 15:49:30 +03:00
|
|
|
Just True
|
|
|
|
== ( do
|
|
|
|
leftValue <- OMap.lookup leftField leftRow
|
|
|
|
rightValue <- OMap.lookup rightField rightRow
|
|
|
|
pure (leftValue == rightValue)
|
|
|
|
)
|
2021-10-12 19:13:15 +03:00
|
|
|
)
|
|
|
|
joinFields
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2021-10-12 14:33:33 +03:00
|
|
|
]
|
|
|
|
}
|
|
|
|
where
|
|
|
|
offset = maybe id drop rhsOffset
|
|
|
|
limit =
|
|
|
|
case rhsTop of
|
|
|
|
NoTop -> id
|
|
|
|
Top n -> take n
|
|
|
|
|
|
|
|
-- | Join a row with another as an array join.
|
|
|
|
joinArrayRows ::
|
2021-10-12 19:13:15 +03:00
|
|
|
Maybe [Text] ->
|
|
|
|
Text ->
|
|
|
|
InsOrdHashMap DataLoaderPlan.FieldName OutputValue ->
|
|
|
|
Vector (InsOrdHashMap DataLoaderPlan.FieldName OutputValue) ->
|
|
|
|
InsOrdHashMap DataLoaderPlan.FieldName OutputValue
|
2021-10-12 14:33:33 +03:00
|
|
|
joinArrayRows wantedFields fieldName leftRow rightRow =
|
|
|
|
OMap.insert
|
|
|
|
(DataLoaderPlan.FieldName fieldName)
|
2021-10-12 19:13:15 +03:00
|
|
|
( ArrayOutputValue
|
|
|
|
( fmap
|
|
|
|
( RecordOutputValue
|
|
|
|
. OMap.filterWithKey
|
|
|
|
( \(DataLoaderPlan.FieldName k) _ ->
|
2022-10-04 00:49:32 +03:00
|
|
|
all (elem k) wantedFields
|
2021-10-12 19:13:15 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
rightRow
|
|
|
|
)
|
|
|
|
)
|
2021-10-12 14:33:33 +03:00
|
|
|
leftRow
|
|
|
|
|
|
|
|
-- | Join a row with another as an object join.
|
|
|
|
--
|
2021-12-01 15:49:30 +03:00
|
|
|
-- If rightRow is not a single row, we throw 'BrokenJoinInvariant'.
|
2021-10-12 14:33:33 +03:00
|
|
|
joinObjectRows ::
|
2021-10-12 19:13:15 +03:00
|
|
|
Maybe [Text] ->
|
|
|
|
Text ->
|
|
|
|
InsOrdHashMap DataLoaderPlan.FieldName OutputValue ->
|
|
|
|
Vector (InsOrdHashMap DataLoaderPlan.FieldName OutputValue) ->
|
2021-12-01 15:49:30 +03:00
|
|
|
Either ExecuteProblem (InsOrdHashMap DataLoaderPlan.FieldName OutputValue)
|
|
|
|
joinObjectRows wantedFields fieldName leftRow rightRows
|
|
|
|
| V.length rightRows /= 1 = Left . BrokenJoinInvariant . foldMap OMap.keys $ rightRows
|
|
|
|
| otherwise =
|
|
|
|
let row = V.head rightRows
|
|
|
|
in pure $
|
|
|
|
OMap.insert
|
|
|
|
(DataLoaderPlan.FieldName fieldName)
|
|
|
|
( RecordOutputValue
|
|
|
|
( OMap.filterWithKey
|
2022-10-04 00:49:32 +03:00
|
|
|
(\(DataLoaderPlan.FieldName k) _ -> all (elem k) wantedFields)
|
2021-12-01 15:49:30 +03:00
|
|
|
row
|
|
|
|
)
|
|
|
|
)
|
|
|
|
leftRow
|