@@ -28,9 +28,9 @@ tupleAtoms (RelationTuple _ tupVec) = tupVec
29atomForAttributeName :: AttributeName -> RelationTuple -> Either RelationalError Atom
29atomForAttributeName :: AttributeName -> RelationTuple -> Either RelationalError Atom
30atomForAttributeName attrName (RelationTuple tupAttrs tupVec) = case V.findIndex (\attr -> attributeName attr == attrName) tupAttrs of
30atomForAttributeName attrName (RelationTuple tupAttrs tupVec) = case V.findIndex (\attr -> attributeName attr == attrName) tupAttrs of
31− Nothing -> Left $ NoSuchAttributeNameError attrName
31+ Nothing -> Left $ NoSuchAttributeNamesError (S.singleton attrName)
32 Just index -> case tupVec V.!? index of
32 Just index -> case tupVec V.!? index of
33− Nothing -> Left $ NoSuchAttributeNameError attrName
33+ Nothing -> Left $ NoSuchAttributeNamesError (S.singleton attrName)
34 Just atom -> Right atom
34 Just atom -> Right atom
36{- -- resolve naming clash with Attribute and Relation later
36{- -- resolve naming clash with Attribute and Relation later
@@ -40,12 +40,21 @@ atomTypeForAttributeName attrName tup = do
40 return $ atomTypeForAtom atom
40 return $ atomTypeForAtom atom
43−atomsForAttributeNames :: V.Vector AttributeName -> RelationTuple -> V.Vector Atom
43+atomsForAttributeNames :: V.Vector AttributeName -> RelationTuple -> Either RelationalError (V.Vector Atom)
44−atomsForAttributeNames attrNames tuple = V.foldr folder V.empty attrNames
44+atomsForAttributeNames attrNames tuple = do
45+ vindices <- vectorIndicesForAttributeNames attrNames (tupleAttributes tuple)
46+ return $ V.map (\index -> tupleAtoms tuple V.! index) vindices
48+vectorIndicesForAttributeNames :: V.Vector AttributeName -> Attributes -> Either RelationalError (V.Vector Int)
49+vectorIndicesForAttributeNames attrNameVec attrs = if not $ V.null unknownAttrNames then
50+ Left $ NoSuchAttributeNamesError (S.fromList (V.toList unknownAttrNames))
52+ Right $ V.map mapper attrNameVec
46− folder attrName acc = case atomForAttributeName attrName tuple of
54+ unknownAttrNames = V.filter ((flip V.notElem) (attributeNames attrs)) attrNameVec
47− Left _ -> acc
55+ mapper attrName = case V.elemIndex attrName (V.map attributeName attrs) of
48− Right atom -> V.snoc acc atom
56+ Nothing -> undefined
50relationForAttributeName :: AttributeName -> RelationTuple -> Either RelationalError Relation
59relationForAttributeName :: AttributeName -> RelationTuple -> Either RelationalError Relation
51relationForAttributeName attrName tuple = do
60relationForAttributeName attrName tuple = do
@@ -71,13 +80,20 @@ mkRelationTuples attrs atomsVec = map mapper atomsVec
71 mapper = mkRelationTuple attrs
80 mapper = mkRelationTuple attrs
73singleTupleSetJoin :: RelationTuple -> RelationTupleSet -> RelationTupleSet
82singleTupleSetJoin :: RelationTuple -> RelationTupleSet -> RelationTupleSet
74−singleTupleSetJoin tup tupSet = HS.foldr tupleJoiner HS.empty tupSet
83+singleTupleSetJoin tup tupSet = HS.foldr tupleJoiner HS.empty tupSet --get rid of this fold here
76 tupleJoiner tuple2 accumulator = case joinedTuple of
85 tupleJoiner tuple2 accumulator = case joinedTuple of
77 Nothing -> accumulator
86 Nothing -> accumulator
78 Just relTuple -> HS.insert relTuple accumulator
87 Just relTuple -> HS.insert relTuple accumulator
79 where joinedTuple = singleTupleJoin tup tuple2
88 where joinedTuple = singleTupleJoin tup tuple2
91+singleTupleSetJoin :: RelationTuple -> RelationTupleSet -> RelationTupleSet
92+singleTupleSetJoin tup1 tupSet = HS.union
94+ mapper tup2 = singleTupleJoin tup1 tup2
81-- the keys/values don't need to be resorted
97-- the keys/values don't need to be resorted
82-- if the keys share some keys and values, then merge the tuples
98-- if the keys share some keys and values, then merge the tuples
83singleTupleJoin :: RelationTuple -> RelationTuple -> Maybe RelationTuple
99singleTupleJoin :: RelationTuple -> RelationTuple -> Maybe RelationTuple