@@ -9,6 +9,7 @@ import qualified Data.Vector as V
99import ProjectM36.Base
1010import ProjectM36.Tuple
1111import qualified ProjectM36.Attribute as A
12+import qualified ProjectM36.AttributeNames as AS
1213import ProjectM36.TupleSet
1314import ProjectM36.Error
1415import Prelude hiding (join)
@@ -79,18 +80,19 @@ union (Relation attrs1 tupSet1) (Relation attrs2 tupSet2) =
7980 where
8081 newtuples = HS.union tupSet1 tupSet2
8182
82−project :: S.Set AttributeName -> Relation -> Either RelationalError Relation
83+project :: AttributeNames -> Relation -> Either RelationalError Relation
8384project projectionAttrNames rel =
84− if not $ A.attributeNamesContained projectionAttrNames (attributeNames rel)
85+ if not $ A.attributeNamesContained selectedAttrNames (attributeNames rel)
8586 then Left $ AttributeNameMismatchError ""
8687 else
8788 relFold folder (Right $ Relation newAttrs HS.empty) rel
8889 where
89− newAttrs = attributesForNames projectionAttrNames rel
90+ selectedAttrNames = A.attributeNameSet newAttrs
91+ newAttrs = AS.projectionAttributesForAttributeNames (attributes rel) projectionAttrNames
9092 folder :: RelationTuple -> Either RelationalError Relation -> Either RelationalError Relation
9193 folder tupleToProject acc = case acc of
9294 Left err -> Left err
93− Right acc2 -> union acc2 (Relation newAttrs (HS.singleton (tupleProject projectionAttrNames tupleToProject)))
95+ Right acc2 -> union acc2 (Relation newAttrs (HS.singleton (tupleProject selectedAttrNames tupleToProject)))
9496
9597rename :: AttributeName -> AttributeName -> Relation -> Either RelationalError Relation
9698rename oldAttrName newAttrName rel@(Relation oldAttrs oldTupSet) =
@@ -127,9 +129,8 @@ group groupAttrNames newAttrName rel@(Relation oldAttrs tupleSet) = do
127129 relFold folder (Right (Relation newAttrs emptyTupleSet)) nonGroupProjection
128130 where
129131 newAttrs = M.union (attributesForNames nonGroupAttrNames rel) groupAttr
130− groupAttr = Attribute newAttrName RelationAtomType (attributesForNames nonGroupAttrNames rel)
131− newAttrNameSet = S.insert newAttrName nonGroupAttrNames
132− nonGroupAttrNames = S.difference groupAttrNames (attributeNames rel)
132+ groupAttr = Attribute newAttrName RelationAtomType (invertedAttributeNames groupAttrNames (attributes rel))
133+ nonGroupAttrNames = invertAttributeNames (attributes rel) groupAttrNames
133134 --map the projection to add the additional new attribute
134135 --create the new attribute (a new relation) by filtering and projecting the tupleSet
135136 folder tupleFromProjection acc = case acc of
@@ -138,26 +139,18 @@ group groupAttrNames newAttrName rel@(Relation oldAttrs tupleSet) = do
138139-}
139140
140141--algorithm: self-join with image relation
141−group :: S.Set AttributeName -> AttributeName -> Relation -> Either RelationalError Relation
142+group :: AttributeNames -> AttributeName -> Relation -> Either RelationalError Relation
142143group groupAttrNames newAttrName rel = do
143144 nonGroupProjection <- project nonGroupAttrNames rel
144145 relMogrify mogrifier newAttrs nonGroupProjection
145146 where
146− newAttrs = A.addAttribute groupAttr (attributesForNames nonGroupAttrNames rel)
147− groupAttr = Attribute newAttrName (RelationAtomType (attributesForNames groupAttrNames rel))
148− nonGroupAttrNames = A.nonMatchingAttributeNameSet groupAttrNames (attributeNames rel)
147+ newAttrs = A.addAttribute groupAttr (AS.projectionAttributesForAttributeNames (attributes rel) nonGroupAttrNames)
148+ groupAttr = Attribute newAttrName (RelationAtomType (AS.projectionAttributesForAttributeNames (attributes rel) groupAttrNames))
149+ nonGroupAttrNames = AS.invertAttributeNames groupAttrNames
149150 mogrifier tupIn = tupleExtend tupIn (matchingRelTuple tupIn)
150151 matchingRelTuple tupIn = case imageRelationFor tupIn rel of
151152 Right rel2 -> RelationTuple (V.singleton groupAttr) (V.singleton (RelationAtom rel2))
152153 Left _ -> undefined
153−{-
154− folder tupleIn acc = case acc of
155− Left err -> Left err
156− Right acc2 -> union acc2 (Relation newAttrs (HS.singleton (tupleExtend tupleIn (matchingRelTuple tupleIn))))
157− matchingRelTuple groupTuple = case imageRelationFor groupTuple rel of
158− Right rel2 -> RelationTuple (V.singleton groupAttr) (V.singleton (RelationAtom rel2))
159− Left _ -> undefined
160−-}
161154
162155--help restriction function
163156--returns a subrelation of
@@ -245,8 +238,8 @@ relFold folder acc (Relation _ tupleSet) = HS.foldr folder acc tupleSet
245238imageRelationFor :: RelationTuple -> Relation -> Either RelationalError Relation
246239imageRelationFor matchTuple rel = do
247240 restricted <- restrictEq matchTuple rel --restrict across matching tuples
248− project (A.nonMatchingAttributeNameSet (attributeNames rel) (tupleAttributeNameSet matchTuple)) restricted --project across attributes not in rel
249−
241+ let projectionAttrNames = AttributeNames $ A.nonMatchingAttributeNameSet (attributeNames rel) (tupleAttributeNameSet matchTuple)
242+ project projectionAttrNames restricted --project across attributes not in rel
250243
251244--returns a relation-valued attribute image relation for each tuple in rel1
252245--algorithm: