on master help sign in
rearrange dataframe module
2d313acAgentM
project-m36.cabal+1
@@ -95,6 +95,7 @@ Library
95 ProjectM36.TransactionGraph.Merge,
95 ProjectM36.TransactionGraph.Merge,
96 ProjectM36.TransGraphRelationalExpression,
96 ProjectM36.TransGraphRelationalExpression,
97 ProjectM36.Base,
97 ProjectM36.Base,
98+ ProjectM36.DataFrame,
98 ProjectM36.Attribute,
99 ProjectM36.Attribute,
99 ProjectM36.AttributeNames,
100 ProjectM36.AttributeNames,
100 ProjectM36.Tuple,
101 ProjectM36.Tuple,
src/bin/TutorialD/Interpreter/RODatabaseContextOperator.hs+1
@@ -2,6 +2,7 @@
2module TutorialD.Interpreter.RODatabaseContextOperator where
2module TutorialD.Interpreter.RODatabaseContextOperator where
3import ProjectM36.Base
3import ProjectM36.Base
4import ProjectM36.Attribute (attributeForName)
4import ProjectM36.Attribute (attributeForName)
5+import ProjectM36.DataFrame
5import ProjectM36.Relation (attributes)
6import ProjectM36.Relation (attributes)
6import ProjectM36.Error
7import ProjectM36.Error
7import ProjectM36.InclusionDependency
8import ProjectM36.InclusionDependency
src/lib/ProjectM36/Base.hs+5−61
@@ -37,7 +37,7 @@ data Atom = IntegerAtom Integer |
37 BoolAtom Bool |
37 BoolAtom Bool |
38 RelationAtom Relation |
38 RelationAtom Relation |
39 ConstructedAtom DataConstructorName AtomType [Atom]
39 ConstructedAtom DataConstructorName AtomType [Atom]
40− deriving (Eq, Ord, Show, Binary, Typeable, NFData, Generic)
40+ deriving (Eq, Show, Binary, Typeable, NFData, Generic)
41
41
42instance Hashable Atom where
42instance Hashable Atom where
43 hashWithSalt salt (ConstructedAtom dConsName _ atoms) = salt `hashWithSalt` atoms
43 hashWithSalt salt (ConstructedAtom dConsName _ atoms) = salt `hashWithSalt` atoms
@@ -124,19 +124,6 @@ instance Eq RelationTupleSet where
124
124
125instance NFData RelationTupleSet where rnf = genericRnf
125instance NFData RelationTupleSet where rnf = genericRnf
126
126
127−
128−data AttributeOrderExpr = AttributeOrderExpr AttributeName Order deriving(Show)
129−data AttributeOrder = AttributeOrder Attribute Order deriving (Show)
130−data Order = ASC | DESC deriving (Eq,Show)
131−
132−sortDataFrameBy :: [AttributeOrder] -> DataFrame -> DataFrame
133−sortDataFrameBy attrOrders (DataFrame attrs tuples) =
134− DataFrame attrs $ sortTuplesBy (compareRelationTupleByAttributeOrders attrOrders) tuples
135−
136−sortTuplesBy :: (RelationTuple -> RelationTuple -> Ordering) -> [RelationTuple] -> [RelationTuple]
137−sortTuplesBy compare' tuples =
138− L.sortBy compare' tuples
139−
140--the same hash must be generated for equal tuples so that the hashset equality works
127--the same hash must be generated for equal tuples so that the hashset equality works
141instance Hashable RelationTuple where
128instance Hashable RelationTuple where
142 --sanity check the tuple for attribute and tuple counts
129 --sanity check the tuple for attribute and tuple counts
@@ -160,45 +147,16 @@ instance Binary RelationTuple
160instance Eq RelationTuple where
147instance Eq RelationTuple where
161 (==) tuple1@(RelationTuple attrs1 _) tuple2@(RelationTuple attrs2 _) = attributesEqual attrs1 attrs2 && atomsEqual
148 (==) tuple1@(RelationTuple attrs1 _) tuple2@(RelationTuple attrs2 _) = attributesEqual attrs1 attrs2 && atomsEqual
162 where
149 where
150+ atomForAttribute attr (RelationTuple attrs tupVec) = case V.findIndex (== attr) attrs of
151+ Nothing -> Nothing
152+ Just index -> tupVec V.!? index
163− atomsEqual = V.all (== True) $ V.map (\attr -> atomForAttribute attr tuple1 == atomForAttribute attr tuple2) attrs1
153+ atomsEqual = V.all (== True) $ V.map (\attr -> atomForAttribute attr tuple1 == atomForAttribute attr tuple2) attrs1
164
154
165−atomForAttribute attr (RelationTuple attrs tupVec) =
166− case V.findIndex (== attr) attrs of
167− Nothing -> Nothing
168− Just index -> tupVec V.!? index
169−
170instance NFData RelationTuple where rnf = genericRnf
155instance NFData RelationTuple where rnf = genericRnf
171
156
172−data DataFrameTuple = DataFrameTuple Attributes (V.Vector Atom) deriving (Eq, Show, Generic)
173−
174−compareRelationTupleByAttributeOrders :: [AttributeOrder] -> RelationTuple -> RelationTuple -> Ordering
175−compareRelationTupleByAttributeOrders attributeOrders tup1 tup2 =
176− let order_ (AttributeOrder _ ord) = ord
177− attr_ (AttributeOrder attr _) = attr
178− compare' (AttributeOrder attr order) = if order == DESC
179− then compareRelationTupleByOneAttribute attr tup2 tup1
180− else compareRelationTupleByOneAttribute attr tup1 tup2
181− res = map compare' attributeOrders in
182− case L.find (/=EQ) res of
183− Nothing -> EQ
184− Just order -> order
185−
186−
187−compareRelationTupleByOneAttribute :: Attribute -> RelationTuple -> RelationTuple -> Ordering
188−compareRelationTupleByOneAttribute attr tuple1@(RelationTuple attrs1 tupVec1) tuple2@(RelationTuple attrs2 tupVec2) =
189− case attr `elem` attrs1 && attr `elem` attrs2 of
190− True -> compare (atomForAttribute attr tuple1) (atomForAttribute attr tuple2)
191− False -> error "Comparing two RelationTuples by an non-existent common attribute."
192
157
193data Relation = Relation Attributes RelationTupleSet deriving (Show, Generic,Typeable)
158data Relation = Relation Attributes RelationTupleSet deriving (Show, Generic,Typeable)
194
159
195−instance Ord Relation where
196− compare (Relation attrs1 tupSet1) (Relation attrs2 tupSet2) = undefined
197− (<) = undefined
198− (<=) = undefined
199− (>=) = undefined
200− (>) = undefined
201−
202instance Eq Relation where
160instance Eq Relation where
203 Relation attrs1 tupSet1 == Relation attrs2 tupSet2 = attributesEqual attrs1 attrs2 && tupSet1 == tupSet2
161 Relation attrs1 tupSet1 == Relation attrs2 tupSet2 = attributesEqual attrs1 attrs2 && tupSet1 == tupSet2
204
162
@@ -213,20 +171,6 @@ instance Hashable Relation where
213
171
214instance Binary Relation
172instance Binary Relation
215
173
216−data DataFrame = DataFrame Attributes [RelationTuple] deriving (Show, Generic, Typeable)
217−
218−take' :: Integer -> DataFrame -> DataFrame
219−take' n (DataFrame attrs tuples) = DataFrame attrs (take (fromInteger n) tuples)
220−
221−drop' :: Integer -> DataFrame -> DataFrame
222−drop' n (DataFrame attrs tuples) = DataFrame attrs (drop (fromInteger n) tuples)
223−
224−toDataFrame :: Relation -> DataFrame
225−toDataFrame (Relation attrs (RelationTupleSet tuples)) = DataFrame attrs tuples
226−
227−fromDataFrame :: DataFrame -> Relation
228−fromDataFrame (DataFrame attrs tuples) = Relation attrs (RelationTupleSet tuples)
229−
230-- | Used to represent the number of tuples in a relation.
174-- | Used to represent the number of tuples in a relation.
231data RelationCardinality = Countable | Finite Int deriving (Eq, Show, Generic, Ord)
175data RelationCardinality = Countable | Finite Int deriving (Eq, Show, Generic, Ord)
232
176
src/lib/ProjectM36/Tuple.hs−7
@@ -48,13 +48,6 @@ atomForAttributeName attrName (RelationTuple tupAttrs tupVec) = case V.findIndex
48 Nothing -> Left (NoSuchAttributeNamesError (S.singleton attrName))
48 Nothing -> Left (NoSuchAttributeNamesError (S.singleton attrName))
49 Just atom -> Right atom
49 Just atom -> Right atom
50
50
51−{- -- resolve naming clash with Attribute and Relation later
52−atomTypeForAttributeName :: AttributeName -> RelationTuple -> Either RelationalError Atom
53−atomTypeForAttributeName attrName tup = do
54− atom <- atomForAttributeName attrName tup
55− return $ atomTypeForAtom atom
56−-}
57−
58atomsForAttributeNames :: V.Vector AttributeName -> RelationTuple -> Either RelationalError (V.Vector Atom)
51atomsForAttributeNames :: V.Vector AttributeName -> RelationTuple -> Either RelationalError (V.Vector Atom)
59atomsForAttributeNames attrNames tuple =
52atomsForAttributeNames attrNames tuple =
60 V.map (\index -> tupleAtoms tuple V.! index) <$> vectorIndicesForAttributeNames attrNames (tupleAttributes tuple)
53 V.map (\index -> tupleAtoms tuple V.! index) <$> vectorIndicesForAttributeNames attrNames (tupleAttributes tuple)