@@ -28,9 +28,9 @@ tupleAtoms (RelationTuple _ tupVec) = tupVec
2828
2929atomForAttributeName :: AttributeName -> RelationTuple -> Either RelationalError Atom
3030atomForAttributeName 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)
3232 Just index -> case tupVec V.!? index of
33− Nothing -> Left $ NoSuchAttributeNameError attrName
33+ Nothing -> Left $ NoSuchAttributeNamesError (S.singleton attrName)
3434 Just atom -> Right atom
3535
3636{- -- resolve naming clash with Attribute and Relation later
@@ -40,12 +40,21 @@ atomTypeForAttributeName attrName tup = do
4040 return $ atomTypeForAtom atom
4141-}
4242
43−atomsForAttributeNames :: V.Vector AttributeName -> RelationTuple -> V.Vector Atom
44−atomsForAttributeNames attrNames tuple = V.foldr folder V.empty attrNames
43+atomsForAttributeNames :: V.Vector AttributeName -> RelationTuple -> Either RelationalError (V.Vector Atom)
44+atomsForAttributeNames attrNames tuple = do
45+ vindices <- vectorIndicesForAttributeNames attrNames (tupleAttributes tuple)
46+ return $ V.map (\index -> tupleAtoms tuple V.! index) vindices
47+
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))
51+ else
52+ Right $ V.map mapper attrNameVec
4553 where
46− folder attrName acc = case atomForAttributeName attrName tuple of
47− Left _ -> acc
48− Right atom -> V.snoc acc atom
54+ unknownAttrNames = V.filter ((flip V.notElem) (attributeNames attrs)) attrNameVec
55+ mapper attrName = case V.elemIndex attrName (V.map attributeName attrs) of
56+ Nothing -> undefined
57+ Just index -> index
4958
5059relationForAttributeName :: AttributeName -> RelationTuple -> Either RelationalError Relation
5160relationForAttributeName attrName tuple = do
@@ -71,13 +80,20 @@ mkRelationTuples attrs atomsVec = map mapper atomsVec
7180 mapper = mkRelationTuple attrs
7281
7382singleTupleSetJoin :: 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
7584 where
7685 tupleJoiner tuple2 accumulator = case joinedTuple of
7786 Nothing -> accumulator
7887 Just relTuple -> HS.insert relTuple accumulator
7988 where joinedTuple = singleTupleJoin tup tuple2
80−
89+
90+{-
91+singleTupleSetJoin :: RelationTuple -> RelationTupleSet -> RelationTupleSet
92+singleTupleSetJoin tup1 tupSet = HS.union
93+ where
94+ mapper tup2 = singleTupleJoin tup1 tup2
95+-}
96+
8197-- the keys/values don't need to be resorted
8298-- if the keys share some keys and values, then merge the tuples
8399singleTupleJoin :: RelationTuple -> RelationTuple -> Maybe RelationTuple