on master help sign in
implement rename for Tutorial D
7555bfbAgentM
Relation.hs+1−1
@@ -136,7 +136,7 @@ rename oldAttrName newAttrName rel@(Relation oldAttrs oldTupSet) =
136 mkRelation newAttrs newTupSet
136 mkRelation newAttrs newTupSet
137 where
137 where
138 attributeValid = attributeNamesContained (S.singleton oldAttrName) (attributeNames rel)
138 attributeValid = attributeNamesContained (S.singleton oldAttrName) (attributeNames rel)
139− newAttrs = M.insert newAttrName (renameAttribute newAttrName (oldAttrs M.! oldAttrName)) oldAttrs
139+ newAttrs = M.delete oldAttrName $ M.insert newAttrName (renameAttribute newAttrName (oldAttrs M.! oldAttrName)) oldAttrs
140 newTupSet = HS.map tupsetmapper oldTupSet
140 newTupSet = HS.map tupsetmapper oldTupSet
141 tupsetmapper tuple = tupleRenameAttribute oldAttrName newAttrName tuple
141 tupsetmapper tuple = tupleRenameAttribute oldAttrName newAttrName tuple
142
142
RelationExpr.hs+8−1
@@ -25,6 +25,7 @@ data RelationalExpr where
25 Join :: RelationalExpr -> RelationalExpr -> RelationalExpr
25 Join :: RelationalExpr -> RelationalExpr -> RelationalExpr
26 Assign :: String -> RelationalExpr -> RelationalExpr
26 Assign :: String -> RelationalExpr -> RelationalExpr
27 MultipleExpr :: [RelationalExpr] -> RelationalExpr
27 MultipleExpr :: [RelationalExpr] -> RelationalExpr
28+ Rename :: AttributeName -> AttributeName -> RelationalExpr -> RelationalExpr
28 --Restrict :: RExpr.RestrictionExpr -> RelationalExpr -> RelationalExpr
29 --Restrict :: RExpr.RestrictionExpr -> RelationalExpr -> RelationalExpr
29{- maybe break this into multiple steps:
30{- maybe break this into multiple steps:
301. check relational types for match (attribute counts) (typechecking step
311. check relational types for match (attribute counts) (typechecking step
@@ -82,7 +83,13 @@ eval (MakeStaticRelation attributes tupleSet) = do
82 case mkRelation attributes tupleSet of
83 case mkRelation attributes tupleSet of
83 Right rel -> return $ Right rel
84 Right rel -> return $ Right rel
84 Left err -> return $ Left err
85 Left err -> return $ Left err
85−
86+
87+eval (Rename oldAttrName newAttrName relExpr) = do
88+ evald <- eval relExpr
89+ case evald of
90+ Right rel -> return $ rename oldAttrName newAttrName rel
91+ Left err -> return $ Left err
92+
86emptyRelVarContext :: RelVarContext
93emptyRelVarContext :: RelVarContext
87emptyRelVarContext = M.empty
94emptyRelVarContext = M.empty
88
95
TutorialDInterpreter.hs+13
@@ -79,9 +79,22 @@ assignP = do
79 relVarName <- identifier
79 relVarName <- identifier
80 reservedOp ":="
80 reservedOp ":="
81 return $ Assign relVarName
81 return $ Assign relVarName
82+
83+renameClause = do
84+ oldAttr <- identifier
85+ reservedOp "as"
86+ newAttr <- identifier
87+ return $ (oldAttr, newAttr)
88+
89+renameP :: Parser (RelationalExpr -> RelationalExpr)
90+renameP = do
91+ reservedOp "rename"
92+ (oldAttr, newAttr) <- braces renameClause
93+ return $ Rename oldAttr newAttr
82
94
83relOperators = [
95relOperators = [
84 [Postfix projectOp],
96 [Postfix projectOp],
97+ [Postfix renameP],
85 [Infix (reservedOp "join" >> return Join) AssocLeft],
98 [Infix (reservedOp "join" >> return Join) AssocLeft],
86 [Infix (reservedOp "union" >> return Union) AssocLeft],
99 [Infix (reservedOp "union" >> return Union) AssocLeft],
87 [Prefix (try assignP)]
100 [Prefix (try assignP)]
TutorialDInterpreterTest.hs+3−1
@@ -13,9 +13,11 @@ main = runTestTT (TestList tests)
13 ("false", Right relationFalse),
13 ("false", Right relationFalse),
14 ("true union false", Right relationTrue),
14 ("true union false", Right relationTrue),
15 ("true; false", Right relationFalse),
15 ("true; false", Right relationFalse),
16− ("a:=relation{a int}", mkRelation simpleAAttributes HS.empty)
16+ ("a:=relation{a int}", mkRelation simpleAAttributes HS.empty),
17+ ("b:=relation{c int} rename {c as d}", mkRelation simpleBAttributes HS.empty)
17 ]
18 ]
18 simpleAAttributes = M.fromList [("a", Attribute "a" IntAtomType)]
19 simpleAAttributes = M.fromList [("a", Attribute "a" IntAtomType)]
20+ simpleBAttributes = M.fromList [("d", Attribute "d" IntAtomType)]
19
21
20
22
21testSimple1 = TestCase $ assertTutdEqual "true" (Right relationTrue)
23testSimple1 = TestCase $ assertTutdEqual "true" (Right relationTrue)