@@ -18,7 +18,7 @@ import qualified ProjectM36.Attribute as A
18import qualified Data.Map as M
18import qualified Data.Map as M
19import qualified Data.HashSet as HS
19import qualified Data.HashSet as HS
20import qualified Data.Set as S
20import qualified Data.Set as S
21−import Control.Monad.RWS.Strict as RWS
21+import Control.Monad.RWS.Strict as RWS hiding (join)
22import Control.Monad.Trans.Class (lift)
22import Control.Monad.Trans.Class (lift)
23import Control.Exception
23import Control.Exception
24import Data.Maybe
24import Data.Maybe
@@ -27,10 +27,11 @@ import Data.Char (isUpper)
27import qualified Data.Text as T
27import qualified Data.Text as T
28import qualified Data.Vector as V
28import qualified Data.Vector as V
29import qualified ProjectM36.TypeConstructorDef as TCD
29import qualified ProjectM36.TypeConstructorDef as TCD
30−import Control.Monad.Except
30+import Control.Monad.Except hiding (join)
31import Control.Monad.Trans.Except (except)
31import Control.Monad.Trans.Except (except)
32−import Control.Monad.Reader as R
32+import Control.Monad.Reader as R hiding (join)
33import ProjectM36.NormalizeExpr
33import ProjectM36.NormalizeExpr
34+import ProjectM36.WithNameExpr
34import Test.QuickCheck
35import Test.QuickCheck
36import GHC.Paths
37import GHC.Paths
@@ -38,11 +39,6 @@ import Data.Time.Clock
38import Data.Time.Calendar
39import Data.Time.Calendar
39import qualified Data.List.NonEmpty as NE
40import qualified Data.List.NonEmpty as NE
40import Data.Functor.Identity
41import Data.Functor.Identity
42−import Control.Monad.IO.Class
43−#if __GLASGOW_HASKELL__ <= 802
47data DatabaseContextExprDetails = CountUpdatedTuples
43data DatabaseContextExprDetails = CountUpdatedTuples
@@ -86,14 +82,12 @@ instance Show RelationalExprEnv where
86--used to eval relationalexpr
82--used to eval relationalexpr
87type RelationalExprM a = ReaderT RelationalExprEnv (ExceptT RelationalError Identity) a
83type RelationalExprM a = ReaderT RelationalExprEnv (ExceptT RelationalError Identity) a
85+runRelationalExprM :: RelationalExprEnv -> RelationalExprM a -> Either RelationalError a
86+runRelationalExprM env m = runIdentity (runExceptT (runReaderT m env))
89reGraph :: RelationalExprM TransactionGraph
88reGraph :: RelationalExprM TransactionGraph
90reGraph = re_graph <$> R.ask
89reGraph = re_graph <$> R.ask
92−gfTransForId :: TransactionId -> GraphRefRelationalExprM Transaction
95− lift $ except $ transactionForId tid graph
97currentTrans :: RelationalExprM Transaction
91currentTrans :: RelationalExprM Transaction
98currentTrans = do
92currentTrans = do
99 tid <- re_currentTransId <$> R.ask
93 tid <- re_currentTransId <$> R.ask
@@ -183,6 +177,11 @@ data GraphRefRelationalExprEnv =
184type GraphRefRelationalExprM a = ReaderT GraphRefRelationalExprEnv (ExceptT RelationalError Identity) a
178type GraphRefRelationalExprM a = ReaderT GraphRefRelationalExprEnv (ExceptT RelationalError Identity) a
180+gfTransForId :: TransactionId -> GraphRefRelationalExprM Transaction
181+gfTransForId tid = do
183+ lift $ except $ transactionForId tid graph
186runGraphRefRelationalExprM :: GraphRefRelationalExprEnv -> GraphRefRelationalExprM a -> Either RelationalError a
185runGraphRefRelationalExprM :: GraphRefRelationalExprEnv -> GraphRefRelationalExprM a -> Either RelationalError a
187runGraphRefRelationalExprM env m = runIdentity (runExceptT (runReaderT m env))
186runGraphRefRelationalExprM env m = runIdentity (runExceptT (runReaderT m env))
@@ -388,7 +387,6 @@ evalGraphRefDatabaseContextExpr NoOperation = pure ()
388evalGraphRefDatabaseContextExpr (Define relVarName attrExprs) = do
387evalGraphRefDatabaseContextExpr (Define relVarName attrExprs) = do
389 relvars <- fmap relationVariables getStateContext
388 relvars <- fmap relationVariables getStateContext
390 tConss <- fmap typeConstructorMapping getStateContext
389 tConss <- fmap typeConstructorMapping getStateContext
391− relEnv <- dbcRelationalExprEnv
392 graph <- dbcGraph
390 graph <- dbcGraph
393 let eAttrs = runGraphRefRelationalExprM gfEnv (mapM evalGraphRefAttrExpr attrExprs)
391 let eAttrs = runGraphRefRelationalExprM gfEnv (mapM evalGraphRefAttrExpr attrExprs)
394 gfEnv = freshGraphRefRelationalExprEnv graph
392 gfEnv = freshGraphRefRelationalExprEnv graph
@@ -456,7 +454,7 @@ evalGraphRefDatabaseContextExpr (Update relVarName atomExprMap pred') = do
456 tmpAttr attr = "_tmp_" <> attr --this could certainly be improved to verify that there is no attribute name conflict
454 tmpAttr attr = "_tmp_" <> attr --this could certainly be improved to verify that there is no attribute name conflict
457 updateAttr nam atomExpr = Extend (AttributeExtendTupleExpr (tmpAttr nam) atomExpr)
455 updateAttr nam atomExpr = Extend (AttributeExtendTupleExpr (tmpAttr nam) atomExpr)
458 projectAndRename attr expr = Rename (tmpAttr attr) attr (Project (InvertedAttributeNames (S.singleton attr)) expr)
456 projectAndRename attr expr = Rename (tmpAttr attr) attr (Project (InvertedAttributeNames (S.singleton attr)) expr)
459− restrictedPortion = Restrict pred'
457+ --TODO restrictedPortion = Restrict pred'
460 updated = foldr (\(oldname, atomExpr) accum ->
458 updated = foldr (\(oldname, atomExpr) accum ->
461 let procAtomExpr = runProcessExprM transId graph (processAtomExpr atomExpr) in
459 let procAtomExpr = runProcessExprM transId graph (processAtomExpr atomExpr) in
462 updateAttr oldname procAtomExpr accum
460 updateAttr oldname procAtomExpr accum
@@ -572,7 +570,6 @@ evalGraphRefDatabaseContextExpr (RemoveDatabaseContextFunction funcName) = do
573evalGraphRefDatabaseContextExpr (ExecuteDatabaseContextFunction funcName atomArgExprs) = do
571evalGraphRefDatabaseContextExpr (ExecuteDatabaseContextFunction funcName atomArgExprs) = do
574 context <- getStateContext
572 context <- getStateContext
575− relExprEnv <- dbcRelationalExprEnv
576 graph <- dbcGraph
573 graph <- dbcGraph
577 --resolve atom arguments
574 --resolve atom arguments
578 let eAtomTypes = mapM (\atomExpr -> runGraphRefRelationalExprM gfEnv (typeForGraphRefAtomExpr emptyAttributes atomExpr)) atomArgExprs
575 let eAtomTypes = mapM (\atomExpr -> runGraphRefRelationalExprM gfEnv (typeForGraphRefAtomExpr emptyAttributes atomExpr)) atomArgExprs
@@ -744,7 +741,6 @@ evalGraphRefDatabaseContextIOExpr (CreateArbitraryRelation relVarName attrExprs
744 --create graph ref expr
741 --create graph ref expr
745 let gfExpr = Define relVarName attrExprs
742 let gfExpr = Define relVarName attrExprs
746 evalEnv = mkDatabaseContextEvalEnv (dbcio_transId env) (dbcio_graph env)
743 evalEnv = mkDatabaseContextEvalEnv (dbcio_transId env) (dbcio_graph env)
747− transId = dbcio_transId env
748 graph = dbcio_graph env
744 graph = dbcio_graph env
749 case runDatabaseContextEvalMonad currentContext evalEnv (evalGraphRefDatabaseContextExpr gfExpr) of
745 case runDatabaseContextEvalMonad currentContext evalEnv (evalGraphRefDatabaseContextExpr gfExpr) of
750 Left err -> pure (Left err)
746 Left err -> pure (Left err)
@@ -755,8 +751,8 @@ evalGraphRefDatabaseContextIOExpr (CreateArbitraryRelation relVarName attrExprs
755 case existingRelVar of
751 case existingRelVar of
756 Nothing -> pure $ Left (RelVarNotDefinedError relVarName)
752 Nothing -> pure $ Left (RelVarNotDefinedError relVarName)
757 Just existingRel -> do
753 Just existingRel -> do
758− let env = freshGraphRefRelationalExprEnv graph
754+ let gfEnv = freshGraphRefRelationalExprEnv graph
759− case runGraphRefRelationalExprM env (typeForGraphRefRelationalExpr existingRel) of
755+ case runGraphRefRelationalExprM gfEnv (typeForGraphRefRelationalExpr existingRel) of
760 Left err -> pure (Left err)
756 Left err -> pure (Left err)
761 Right relType -> do
757 Right relType -> do
762 let expectedAttributes = attributes relType
758 let expectedAttributes = attributes relType
@@ -793,7 +789,6 @@ checkConstraints context transId graph@(TransactionGraph graphHeads transSet) =
793 tempTrans = Transaction U.nil (TransactionInfo (transId NE.:| []) tempStamp) tempSchemas
789 tempTrans = Transaction U.nil (TransactionInfo (transId NE.:| []) tempStamp) tempSchemas
795 deps = inclusionDependencies context
791 deps = inclusionDependencies context
796− reEnv = mkRelationalExprEnv context transId graph
797 -- no optimization available here, really? perhaps the optimizer should be passed down to here or the eval function should be passed through the environment
792 -- no optimization available here, really? perhaps the optimizer should be passed down to here or the eval function should be passed through the environment
798 checkIncDep depName (InclusionDependency subsetExpr supersetExpr) = do
793 checkIncDep depName (InclusionDependency subsetExpr supersetExpr) = do
799 let process = runProcessExprM transId graph
794 let process = runProcessExprM transId graph
@@ -806,8 +801,8 @@ checkConstraints context transId graph@(TransactionGraph graphHeads transSet) =
806 typeSuper <- runGfRel (typeForGraphRefRelationalExpr gfSupersetExpr)
801 typeSuper <- runGfRel (typeForGraphRefRelationalExpr gfSupersetExpr)
807 when (typeSub /= typeSuper) (Left (RelationTypeMismatchError (attributes typeSub) (attributes typeSuper)))
802 when (typeSub /= typeSuper) (Left (RelationTypeMismatchError (attributes typeSub) (attributes typeSuper)))
808 let checkExpr = Equals gfSupersetExpr (Union gfSubsetExpr gfSupersetExpr)
803 let checkExpr = Equals gfSupersetExpr (Union gfSubsetExpr gfSupersetExpr)
809− gfEvald = runGraphRefRelationalExprM gfEnv (evalGraphRefRelationalExpr checkExpr)
804+ gfEvald = runGraphRefRelationalExprM gfEnv' (evalGraphRefRelationalExpr checkExpr)
810− gfEnv = freshGraphRefRelationalExprEnv potentialGraph
805+ gfEnv' = freshGraphRefRelationalExprEnv potentialGraph
811 case gfEvald of
806 case gfEvald of
812 Left err -> Left err
807 Left err -> Left err
813 Right resultRel -> if resultRel == relationTrue then
808 Right resultRel -> if resultRel == relationTrue then
@@ -930,7 +925,7 @@ tupleExprCheckNewAttrName attrName rel = if isRight (attributeForName attrName r
931extendGraphRefTupleExpressionProcessor :: Relation -> GraphRefExtendTupleExpr -> GraphRefRelationalExprM (Attributes, RelationTuple -> Either RelationalError RelationTuple)
926extendGraphRefTupleExpressionProcessor :: Relation -> GraphRefExtendTupleExpr -> GraphRefRelationalExprM (Attributes, RelationTuple -> Either RelationalError RelationTuple)
932extendGraphRefTupleExpressionProcessor relIn (AttributeExtendTupleExpr newAttrName atomExpr) = do
927extendGraphRefTupleExpressionProcessor relIn (AttributeExtendTupleExpr newAttrName atomExpr) = do
933− renv <- askEnv
928+-- renv <- askEnv
934 -- check that the attribute name is not in use
929 -- check that the attribute name is not in use
935 case tupleExprCheckNewAttrName newAttrName relIn of
930 case tupleExprCheckNewAttrName newAttrName relIn of
936 Left err -> throwError err
931 Left err -> throwError err
@@ -939,7 +934,8 @@ extendGraphRefTupleExpressionProcessor relIn (AttributeExtendTupleExpr newAttrNa
939 atomExprType' <- verifyGraphRefAtomExprTypes relIn atomExpr atomExprType
934 atomExprType' <- verifyGraphRefAtomExprTypes relIn atomExpr atomExprType
940 let newAttrs = A.attributesFromList [Attribute newAttrName atomExprType']
935 let newAttrs = A.attributesFromList [Attribute newAttrName atomExprType']
941 newAndOldAttrs = A.addAttributes (attributes relIn) newAttrs
936 newAndOldAttrs = A.addAttributes (attributes relIn) newAttrs
937+ pure (newAndOldAttrs, \tup ->
942− pure (newAndOldAttrs, \tup -> let substate = mergeTuplesIntoGraphRefRelationalExprEnv tup renv in
938+ --let substate = mergeTuplesIntoGraphRefRelationalExprEnv tup renv in
943 case unimplemented {-runReader (evalAtomExpr tup atomExpr) substate-} of
939 case unimplemented {-runReader (evalAtomExpr tup atomExpr) substate-} of
944 Left err -> Left err
940 Left err -> Left err
945 Right atom -> Right (tupleAtomExtend newAttrName atom tup)
941 Right atom -> Right (tupleAtomExtend newAttrName atom tup)
@@ -1011,6 +1007,7 @@ typeForGraphRefAtomExpr attrs (AttributeAtomExpr attrName) = do
1011 Left _ -> case atomForAttributeName attrName envTup of
1007 Left _ -> case atomForAttributeName attrName envTup of
1012 Right atom -> pure (atomTypeForAtom atom)
1008 Right atom -> pure (atomTypeForAtom atom)
1013 Left _ -> throwError err
1009 Left _ -> throwError err
1010+ Left err -> throwError err
1015typeForGraphRefAtomExpr _ (NakedAtomExpr atom) = pure (atomTypeForAtom atom)
1012typeForGraphRefAtomExpr _ (NakedAtomExpr atom) = pure (atomTypeForAtom atom)
1016typeForGraphRefAtomExpr attrs (FunctionAtomExpr funcName atomArgs transId) = do
1013typeForGraphRefAtomExpr attrs (FunctionAtomExpr funcName atomArgs transId) = do
@@ -1049,6 +1046,8 @@ verifyGraphRefAtomExprTypes relIn (AttributeAtomExpr attrName) expectedType = do
1049 case A.attributeForName attrName attrs' of
1046 case A.attributeForName attrName attrs' of
1050 Left err' -> throwError err'
1047 Left err' -> throwError err'
1051 Right attrType -> lift $ except $ atomTypeVerify expectedType (A.atomType attrType)
1048 Right attrType -> lift $ except $ atomTypeVerify expectedType (A.atomType attrType)
1049+ Left err -> throwError err
1052verifyGraphRefAtomExprTypes _ (NakedAtomExpr atom) expectedType =
1051verifyGraphRefAtomExprTypes _ (NakedAtomExpr atom) expectedType =
1053 lift $ except $ atomTypeVerify expectedType (atomTypeForAtom atom)
1052 lift $ except $ atomTypeVerify expectedType (atomTypeForAtom atom)
1054verifyGraphRefAtomExprTypes relIn (FunctionAtomExpr funcName funcArgExprs tid) expectedType = do
1053verifyGraphRefAtomExprTypes relIn (FunctionAtomExpr funcName funcArgExprs tid) expectedType = do
@@ -1165,14 +1164,72 @@ evalAttributeNames attrNames expr = do
1165 Right attrExprType -> pure (Right (A.attributeNameSet (attributes attrExprType)))
1164 Right attrExprType -> pure (Right (A.attributeNameSet (attributes attrExprType)))
1167+--temporary implementation until we have a proper planner+executor
1168evalGraphRefRelationalExpr :: GraphRefRelationalExpr -> GraphRefRelationalExprM Relation
1168evalGraphRefRelationalExpr :: GraphRefRelationalExpr -> GraphRefRelationalExprM Relation
1169−--evalGraphRefRelationalExpr (MakeRelationaFromExprs
1169+evalGraphRefRelationalExpr (MakeRelationFromExprs mAttrExprs tupleExprs) = do
1170+ mAttrs <- case mAttrExprs of
1172+ Just . A.attributesFromList <$> mapM evalGraphRefAttrExpr (fromMaybe [] mAttrExprs)
1173+ Nothing -> pure Nothing
1174+ tuples <- mapM (evalGraphRefTupleExpr mAttrs) tupleExprs
1175+ let attrs = fromMaybe firstTupleAttrs mAttrs
1176+ firstTupleAttrs = if null tuples then A.emptyAttributes else tupleAttributes (head tuples)
1177+ lift $ except $ mkRelation attrs (RelationTupleSet tuples)
1178+evalGraphRefRelationalExpr (MakeStaticRelation attributeSet tupleSet) =
1179+ lift $ except $ mkRelation attributeSet tupleSet
1180+evalGraphRefRelationalExpr (ExistingRelation rel) = pure rel
1170evalGraphRefRelationalExpr (RelationVariable name tid) = do
1181evalGraphRefRelationalExpr (RelationVariable name tid) = do
1171 ctx <- concreteDatabaseContext <$> gfTransForId tid
1182 ctx <- concreteDatabaseContext <$> gfTransForId tid
1172 case M.lookup name (relationVariables ctx) of
1183 case M.lookup name (relationVariables ctx) of
1173 Nothing -> throwError (RelVarNotDefinedError name)
1184 Nothing -> throwError (RelVarNotDefinedError name)
1174 Just rv -> evalGraphRefRelationalExpr rv
1185 Just rv -> evalGraphRefRelationalExpr rv
1175−
1186+evalGraphRefRelationalExpr (Project attrNames expr) = do
1187+ attrNameSet <- evalGraphRefAttributeNames attrNames expr
1188+ rel <- evalGraphRefRelationalExpr expr
1189+ lift $ except $ project attrNameSet rel
1190+evalGraphRefRelationalExpr (Union exprA exprB) = do
1191+ relA <- evalGraphRefRelationalExpr exprA
1192+ relB <- evalGraphRefRelationalExpr exprB
1193+ lift $ except $ union relA relB
1194+evalGraphRefRelationalExpr (Join exprA exprB) = do
1195+ relA <- evalGraphRefRelationalExpr exprA
1196+ relB <- evalGraphRefRelationalExpr exprB
1197+ lift $ except $ join relA relB
1198+evalGraphRefRelationalExpr (Rename oldName newName expr) = do
1199+ rel <- evalGraphRefRelationalExpr expr
1200+ lift $ except $ rename oldName newName rel
1201+evalGraphRefRelationalExpr (Difference exprA exprB) = do
1202+ relA <- evalGraphRefRelationalExpr exprA
1203+ relB <- evalGraphRefRelationalExpr exprB
1204+ lift $ except $ difference relA relB
1205+evalGraphRefRelationalExpr (Group groupAttrNames newAttrName expr) = do
1206+ groupNames <- evalGraphRefAttributeNames groupAttrNames expr
1207+ rel <- evalGraphRefRelationalExpr expr
1208+ lift $ except $ group groupNames newAttrName rel
1209+evalGraphRefRelationalExpr (Ungroup groupAttrName expr) = do
1210+ rel <- evalGraphRefRelationalExpr expr
1211+ lift $ except $ ungroup groupAttrName rel
1212+evalGraphRefRelationalExpr (Restrict predExpr expr) = do
1213+ rel <- evalGraphRefRelationalExpr expr
1214+ filt <- predicateRestrictionFilter (attributes rel) predExpr
1215+ lift $ except $ restrict filt rel
1216+evalGraphRefRelationalExpr (Equals exprA exprB) = do
1217+ relA <- evalGraphRefRelationalExpr exprA
1218+ relB <- evalGraphRefRelationalExpr exprB
1219+ pure $ if relA == relB then relationTrue else relationFalse
1220+evalGraphRefRelationalExpr (NotEquals exprA exprB) = do
1221+ relA <- evalGraphRefRelationalExpr exprA
1222+ relB <- evalGraphRefRelationalExpr exprB
1223+ pure $ if relA == relB then relationFalse else relationTrue
1224+evalGraphRefRelationalExpr (Extend extendTupleExpr expr) = do
1225+ rel <- evalGraphRefRelationalExpr expr
1226+ (newAttrs, tupProc) <- extendGraphRefTupleExpressionProcessor rel extendTupleExpr
1227+ lift $ except $ relMogrify tupProc newAttrs rel
1228+evalGraphRefRelationalExpr expr@With{} = do
1229+ --strategy A: add relation variables to the contexts in the graph
1230+ --strategy B: drop in macros in place (easier programmatically)
1231+ --strategy B implementation
1232+ evalGraphRefRelationalExpr (substituteWithNameMacros [] expr)
1177dbContextForTransId :: TransactionId -> TransactionGraph -> Either RelationalError DatabaseContext
1234dbContextForTransId :: TransactionId -> TransactionGraph -> Either RelationalError DatabaseContext
1178dbContextForTransId tid graph = do
1235dbContextForTransId tid graph = do
@@ -1193,28 +1250,71 @@ transactionForId tid graph
1193typeForGraphRefRelationalExpr :: GraphRefRelationalExpr -> GraphRefRelationalExprM Relation
1250typeForGraphRefRelationalExpr :: GraphRefRelationalExpr -> GraphRefRelationalExprM Relation
1194typeForGraphRefRelationalExpr (MakeStaticRelation attrs _) = lift $ except $ mkRelation attrs emptyTupleSet
1251typeForGraphRefRelationalExpr (MakeStaticRelation attrs _) = lift $ except $ mkRelation attrs emptyTupleSet
1195typeForGraphRefRelationalExpr (ExistingRelation rel) = pure (emptyRelationWithAttrs (attributes rel))
1252typeForGraphRefRelationalExpr (ExistingRelation rel) = pure (emptyRelationWithAttrs (attributes rel))
1196−typeForGraphRefRelationalExpr (MakeRelationFromExprs mAttrExprs tupleExprs) =
1253+typeForGraphRefRelationalExpr (MakeRelationFromExprs mAttrExprs tupleExprs) = do
1197− case mAttrExprs of
1254+ mAttrs <- case mAttrExprs of
1198− Just attrExprs -> do
1255+ Just attrExprs -> do
1199− attrs <- mapM (\e -> evalGraphRefAttributeExpr e) attrExprs
1256+ attrs <- mapM (\e -> evalGraphRefAttributeExpr e) attrExprs
1200− pure (emptyRelationWithAttrs (attributesFromList attrs))
1257+ pure (Just (attributesFromList attrs))
1258+ Nothing -> pure Nothing
1259+ tuples <- mapM (evalGraphRefTupleExpr mAttrs) tupleExprs
1260+ let retAttrs = case tuples of
1261+ (tup:_) -> tupleAttributes tup
1262+ [] -> fromMaybe A.emptyAttributes mAttrs
1263+ pure $ emptyRelationWithAttrs retAttrs
1201typeForGraphRefRelationalExpr (RelationVariable rvName tid) = do
1265typeForGraphRefRelationalExpr (RelationVariable rvName tid) = do
1202 relVars <- relationVariables . concreteDatabaseContext <$> gfTransForId tid
1266 relVars <- relationVariables . concreteDatabaseContext <$> gfTransForId tid
1203 case M.lookup rvName relVars of
1267 case M.lookup rvName relVars of
1204 Nothing -> throwError (RelVarNotDefinedError rvName)
1268 Nothing -> throwError (RelVarNotDefinedError rvName)
1205 Just rvExpr -> typeForGraphRefRelationalExpr rvExpr
1269 Just rvExpr -> typeForGraphRefRelationalExpr rvExpr
1206typeForGraphRefRelationalExpr (Project attrNames expr) = do
1270typeForGraphRefRelationalExpr (Project attrNames expr) = do
1207− exprType <- typeForGraphRefRelationalExpr expr
1271+ exprType' <- typeForGraphRefRelationalExpr expr
1208 projectionAttrs <- evalGraphRefAttributeNames attrNames expr
1272 projectionAttrs <- evalGraphRefAttributeNames attrNames expr
1209− lift $ except $ project projectionAttrs exprType
1273+ lift $ except $ project projectionAttrs exprType'
1210−
1274+typeForGraphRefRelationalExpr (Union exprA exprB) = do
1275+ exprA' <- typeForGraphRefRelationalExpr exprA
1276+ exprB' <- typeForGraphRefRelationalExpr exprB
1277+ lift $ except $ union exprA' exprB'
1278+typeForGraphRefRelationalExpr (Join exprA exprB) = do
1279+ exprA' <- typeForGraphRefRelationalExpr exprA
1280+ exprB' <- typeForGraphRefRelationalExpr exprB
1281+ lift $ except $ join exprA' exprB'
1282+typeForGraphRefRelationalExpr (Rename oldAttr newAttr expr) = do
1283+ expr' <- typeForGraphRefRelationalExpr expr
1284+ lift $ except $ rename oldAttr newAttr expr'
1285+typeForGraphRefRelationalExpr (Difference exprA exprB) = do
1286+ exprA' <- typeForGraphRefRelationalExpr exprA
1287+ exprB' <- typeForGraphRefRelationalExpr exprB
1288+ lift $ except $ difference exprA' exprB'
1289+typeForGraphRefRelationalExpr (Group groupNames attrName expr) = do
1290+ expr' <- typeForGraphRefRelationalExpr expr
1291+ groupNames' <- evalGraphRefAttributeNames groupNames expr
1292+ lift $ except $ group groupNames' attrName expr'
1293+typeForGraphRefRelationalExpr (Ungroup groupAttrName expr) = do
1294+ expr' <- typeForGraphRefRelationalExpr expr
1295+ lift $ except $ ungroup groupAttrName expr'
1296+typeForGraphRefRelationalExpr (Restrict pred' expr) = do
1297+ expr' <- typeForGraphRefRelationalExpr expr
1298+ filt <- predicateRestrictionFilter (attributes expr') pred'
1299+ lift $ except $ restrict filt expr'
1300+typeForGraphRefRelationalExpr Equals{} = do
1302+typeForGraphRefRelationalExpr NotEquals{} = do
1304+typeForGraphRefRelationalExpr (Extend extendTupleExpr expr) = do
1305+ rel <- typeForGraphRefRelationalExpr expr
1306+ evalGraphRefRelationalExpr (Extend extendTupleExpr (ExistingRelation rel))
1307+typeForGraphRefRelationalExpr expr@With{} = do
1308+ let expr' = substituteWithNameMacros [] expr
1309+ typeForGraphRefRelationalExpr expr'
1211evalGraphRefAttributeNames :: GraphRefAttributeNames -> GraphRefRelationalExpr -> GraphRefRelationalExprM (S.Set AttributeName)
1311evalGraphRefAttributeNames :: GraphRefAttributeNames -> GraphRefRelationalExpr -> GraphRefRelationalExprM (S.Set AttributeName)
1212evalGraphRefAttributeNames attrNames expr = do
1312evalGraphRefAttributeNames attrNames expr = do
1213− exprType <- typeForGraphRefRelationalExpr expr
1313+ exprType' <- typeForGraphRefRelationalExpr expr
1214− let typeNameSet = S.fromList (V.toList (A.attributeNames (attributes exprType)))
1314+ let typeNameSet = S.fromList (V.toList (A.attributeNames (attributes exprType')))
1215 case attrNames of
1315 case attrNames of
1216 AttributeNames names ->
1316 AttributeNames names ->
1217− case A.projectionAttributesForNames names (attributes exprType) of
1317+ case A.projectionAttributesForNames names (attributes exprType') of
1218 Left err -> throwError err
1318 Left err -> throwError err
1219 Right attrs -> pure (S.fromList (V.toList (A.attributeNames attrs)))
1319 Right attrs -> pure (S.fromList (V.toList (A.attributeNames attrs)))
@@ -1248,6 +1348,7 @@ evalGraphRefAttributeExpr (AttributeAndTypeNameExpr attrName tCons tid) = do
1248 case validateAtomType aType tConsMap of
1348 case validateAtomType aType tConsMap of
1249 Left err -> throwError err
1349 Left err -> throwError err
1250 Right _ -> pure (Attribute attrName aType)
1350 Right _ -> pure (Attribute attrName aType)
1351+evalGraphRefAttributeExpr (NakedAttributeExpr attr) = pure attr
1252mkEmptyRelVars :: RelationVariables -> RelationVariables
1353mkEmptyRelVars :: RelationVariables -> RelationVariables
1253mkEmptyRelVars rvMap = M.map mkEmptyRelVar rvMap
1354mkEmptyRelVars rvMap = M.map mkEmptyRelVar rvMap
@@ -1263,11 +1364,11 @@ mkEmptyRelVars rvMap = M.map mkEmptyRelVar rvMap
1263 mkEmptyRelVar (Difference exprA exprB) = Difference (mkEmptyRelVar exprA) (mkEmptyRelVar exprB)
1364 mkEmptyRelVar (Difference exprA exprB) = Difference (mkEmptyRelVar exprA) (mkEmptyRelVar exprB)
1264 mkEmptyRelVar (Group attrNames attrName expr) = Group attrNames attrName (mkEmptyRelVar expr)
1365 mkEmptyRelVar (Group attrNames attrName expr) = Group attrNames attrName (mkEmptyRelVar expr)
1265 mkEmptyRelVar (Ungroup attrName expr) = Ungroup attrName (mkEmptyRelVar expr)
1366 mkEmptyRelVar (Ungroup attrName expr) = Ungroup attrName (mkEmptyRelVar expr)
1266− mkEmptyRelVar (Restrict pred expr) = Restrict pred (mkEmptyRelVar expr)
1367+ mkEmptyRelVar (Restrict pred' expr) = Restrict pred' (mkEmptyRelVar expr)
1267 mkEmptyRelVar (Equals exprA exprB) = Equals (mkEmptyRelVar exprA) (mkEmptyRelVar exprB)
1368 mkEmptyRelVar (Equals exprA exprB) = Equals (mkEmptyRelVar exprA) (mkEmptyRelVar exprB)
1268 mkEmptyRelVar (NotEquals exprA exprB) = NotEquals (mkEmptyRelVar exprA) (mkEmptyRelVar exprB)
1369 mkEmptyRelVar (NotEquals exprA exprB) = NotEquals (mkEmptyRelVar exprA) (mkEmptyRelVar exprB)
1269 mkEmptyRelVar (Extend extTuple expr) = Extend extTuple (mkEmptyRelVar expr)
1370 mkEmptyRelVar (Extend extTuple expr) = Extend extTuple (mkEmptyRelVar expr)
1270− mkEmptyRelVar (With macros expr) = With (map (\(rv, expr) -> (rv, mkEmptyRelVar expr)) macros) (mkEmptyRelVar expr)
1371+ mkEmptyRelVar (With macros expr) = With (map (\(rv, expr') -> (rv, mkEmptyRelVar expr')) macros) (mkEmptyRelVar expr)
1273dbErr :: RelationalError -> DatabaseContextEvalMonad ()
1374dbErr :: RelationalError -> DatabaseContextEvalMonad ()