on master help sign in
add DataFrame datatype. :showsorteddataframe <relExpr> <attrName>
commitbaaa5b231f16820abccd6f76b5017ef0de111581 authorJohnny Liao <dycan0716@gmail.com> dateTue 19 Jun 2018, 08:28 (8 years ago) parenta81e186
3 files changed+69−7
src/bin/TutorialD/Interpreter/Base.hs+3 src/bin/TutorialD/Interpreter/RODatabaseContextOperator.hs+23−1 src/lib/ProjectM36/Base.hs+43−6
src/bin/TutorialD/Interpreter/Base.hs+3
@@ -41,6 +41,8 @@ displayOpResult (DisplayParseErrorResult mPromptLength err) = do
4141 pointyString len = T.justifyRight (len + fromIntegral errorIndent) '_' "^"
4242 maybe (pure ()) (TIO.putStrLn . pointyString) mPromptLength
4343 TIO.putStr ("ERR:" <> errString)
44+displayOpResult (DisplayDataFrameResult dFrame) = do
45+ TIO.putStrLn (showRelation $ fromDataFrame dFrame)
4446
4547spaceConsumer :: Parser ()
4648spaceConsumer = Lex.space (void spaceChar) (Lex.skipLineComment "--") (Lex.skipBlockComment "{-" "-}")
@@ -127,6 +129,7 @@ data TutorialDOperatorResult = QuitResult |
127129 DisplayResult StringType |
128130 DisplayIOResult (IO ()) |
129131 DisplayRelationResult Relation |
132+ DisplayDataFrameResult DataFrame |
130133 DisplayErrorResult StringType |
131134 DisplayParseErrorResult (Maybe PromptLength) (ParseError Char Dec) | -- Int refers to length of prompt text
132135 QuietSuccessResult
src/bin/TutorialD/Interpreter/RODatabaseContextOperator.hs+23−1
@@ -1,6 +1,8 @@
11{-# LANGUAGE GADTs #-}
22module TutorialD.Interpreter.RODatabaseContextOperator where
33import ProjectM36.Base
4+import ProjectM36.Attribute (attributeForName)
5+import ProjectM36.Relation (attributes)
46import ProjectM36.Error
57import ProjectM36.InclusionDependency
68import qualified ProjectM36.Client as C
@@ -26,6 +28,7 @@ data RODatabaseContextOperator where
2628 ShowRelationVariables :: RODatabaseContextOperator
2729 ShowAtomFunctions :: RODatabaseContextOperator
2830 ShowDatabaseContextFunctions :: RODatabaseContextOperator
31+ ShowSortedDataFrame :: RelationalExpr -> AttributeName -> RODatabaseContextOperator
2932 Quit :: RODatabaseContextOperator
3033 deriving (Show)
3134
@@ -81,6 +84,7 @@ roDatabaseContextOperatorP = typeP
8184 <|> showTypesP
8285 <|> showAtomFunctionsP
8386 <|> showDatabaseContextFunctionsP
87+ <|> showSortedDataFrameP
8488 <|> quitP
8589
8690--logically, these read-only operations could happen purely, but not if a remote call is required
@@ -148,6 +152,18 @@ evalRODatabaseContextOp sessionId conn ShowDatabaseContextFunctions = do
148152 Left err -> pure $ DisplayErrorResult (T.pack (show err))
149153 Right rel -> evalRODatabaseContextOp sessionId conn (ShowRelation (ExistingRelation rel))
150154
155+evalRODatabaseContextOp sessionId conn (ShowSortedDataFrame expr attrName) = do
156+ res <- C.executeRelationalExpr sessionId conn expr
157+ case res of
158+ Left err -> pure $ DisplayErrorResult $ T.pack (show err)
159+ Right rel -> do
160+ let attrs = attributes rel
161+ case attributeForName attrName attrs of
162+ Left err -> pure $ DisplayErrorResult $ T.pack (show err)
163+ Right attr -> do
164+ pure $ DisplayDataFrameResult $ sortDataFrameBy attr . toDataFrame $ rel
165+
166+
151167evalRODatabaseContextOp _ _ Quit = pure QuitResult
152168
153169interpretRODatabaseContextOp :: C.SessionId -> C.Connection -> T.Text -> IO TutorialDOperatorResult
@@ -155,4 +171,10 @@ interpretRODatabaseContextOp sessionId conn tutdstring = case parse roDatabaseCo
155171 Left err -> pure $ DisplayErrorResult (T.pack (show err))
156172 Right parsed -> evalRODatabaseContextOp sessionId conn parsed
157173
158−
174+showSortedDataFrameP :: Parser RODatabaseContextOperator
175+showSortedDataFrameP = do
176+ reservedOp ":showsorteddataframe"
177+ ShowSortedDataFrame <$> relExprP <*> identifier
178+
179+
180+
src/lib/ProjectM36/Base.hs+43−6
@@ -37,7 +37,7 @@ data Atom = IntegerAtom Integer |
3737 BoolAtom Bool |
3838 RelationAtom Relation |
3939 ConstructedAtom DataConstructorName AtomType [Atom]
40− deriving (Eq, Show, Binary, Typeable, NFData, Generic)
40+ deriving (Eq, Ord, Show, Binary, Typeable, NFData, Generic)
4141
4242instance Hashable Atom where
4343 hashWithSalt salt (ConstructedAtom dConsName _ atoms) = salt `hashWithSalt` atoms
@@ -77,7 +77,9 @@ data AtomType = IntAtomType |
7777 TypeVariableType TypeVarName
7878 --wildcard used in Atom Functions and tuples for data constructors which don't provide all arguments to the type constructor
7979 deriving (Eq, NFData, Generic, Binary, Show)
80−
80+
81+instance Ord AtomType where
82+ compare = undefined
8183type TypeVarMap = M.Map TypeVarName AtomType
8284
8385instance Hashable TypeVarMap where
@@ -122,6 +124,16 @@ instance Eq RelationTupleSet where
122124
123125instance NFData RelationTupleSet where rnf = genericRnf
124126
127+sortDataFrameBy :: Attribute -> DataFrame -> DataFrame
128+sortDataFrameBy attr (DataFrame attrs tupSet) =
129+ case attr `elem` attrs of
130+ True -> DataFrame attrs $ sortTupleSetBy (compareRelationTupleBy attr) tupSet
131+ False -> error $ "No such attribute: " ++ show attr
132+
133+sortTupleSetBy :: (RelationTuple -> RelationTuple -> Ordering) -> RelationTupleSet -> RelationTupleSet
134+sortTupleSetBy compare' (RelationTupleSet tuples) =
135+ RelationTupleSet $ L.sortBy compare' tuples
136+
125137--the same hash must be generated for equal tuples so that the hashset equality works
126138instance Hashable RelationTuple where
127139 --sanity check the tuple for attribute and tuple counts
@@ -145,15 +157,32 @@ instance Binary RelationTuple
145157instance Eq RelationTuple where
146158 (==) tuple1@(RelationTuple attrs1 _) tuple2@(RelationTuple attrs2 _) = attributesEqual attrs1 attrs2 && atomsEqual
147159 where
148− atomForAttribute attr (RelationTuple attrs tupVec) = case V.findIndex (== attr) attrs of
149− Nothing -> Nothing
150− Just index -> tupVec V.!? index
151160 atomsEqual = V.all (== True) $ V.map (\attr -> atomForAttribute attr tuple1 == atomForAttribute attr tuple2) attrs1
152161
162+atomForAttribute attr (RelationTuple attrs tupVec) =
163+ case V.findIndex (== attr) attrs of
164+ Nothing -> Nothing
165+ Just index -> tupVec V.!? index
166+
153167instance NFData RelationTuple where rnf = genericRnf
154168
169+data DataFrameTuple = DataFrameTuple Attributes (V.Vector Atom) deriving (Eq, Show, Generic)
170+
171+compareRelationTupleBy :: Attribute -> RelationTuple -> RelationTuple -> Ordering
172+compareRelationTupleBy attr tuple1@(RelationTuple attrs1 tupVec1) tuple2@(RelationTuple attrs2 tupVec2) =
173+ case attr `elem` attrs1 && attr `elem` attrs2 of
174+ True -> compare (atomForAttribute attr tuple1) (atomForAttribute attr tuple2)
175+ False -> error "Comparing two RelationTuples by an non-existent common attribute."
176+
155177data Relation = Relation Attributes RelationTupleSet deriving (Show, Generic,Typeable)
156178
179+instance Ord Relation where
180+ compare (Relation attrs1 tupSet1) (Relation attrs2 tupSet2) = undefined
181+ (<) = undefined
182+ (<=) = undefined
183+ (>=) = undefined
184+ (>) = undefined
185+
157186instance Eq Relation where
158187 Relation attrs1 tupSet1 == Relation attrs2 tupSet2 = attributesEqual attrs1 attrs2 && tupSet1 == tupSet2
159188
@@ -167,7 +196,15 @@ instance Hashable Relation where
167196 sortedAttrs = map snd (sortedAttributesIndices attrs)
168197
169198instance Binary Relation
170−
199+
200+data DataFrame = DataFrame Attributes RelationTupleSet deriving (Show, Generic, Typeable)
201+
202+toDataFrame :: Relation -> DataFrame
203+toDataFrame (Relation attr tupSet) = DataFrame attr tupSet
204+
205+fromDataFrame :: DataFrame -> Relation
206+fromDataFrame (DataFrame attr tupSet) = Relation attr tupSet
207+
171208-- | Used to represent the number of tuples in a relation.
172209data RelationCardinality = Countable | Finite Int deriving (Eq, Show, Generic, Ord)
173210