on master help sign in
Merge branch 'feature/add_with_relvar_macro' of https://github.com/YuMingLiao/project-m36 into YuMingLiao-feature/add_with_relvar_macro
commite69a0637e7add357efda1d27ecc3d3f7b86e5646 authorAgentM <agentm@themactionfaction.com> dateWed 25 Jul 2018, 09:36 (8 years ago) parentab0ee020b24d9d
4 files changed+40−3
src/bin/TutorialD/Interpreter/RelationalExpr.hs+16−1 src/lib/ProjectM36/Base.hs+3−1 src/lib/ProjectM36/RelationalExpression.hs+16 src/lib/ProjectM36/StaticOptimizer.hs+5−1
src/bin/TutorialD/Interpreter/RelationalExpr.hs+16−1
@@ -129,7 +129,7 @@ relOperators = [
129129 ]
130130
131131relExprP :: RelationalMarkerExpr a => Parser (RelationalExprBase a)
132−relExprP = makeExprParser relTerm relOperators
132+relExprP = try withMacroExprP <|> makeExprParser relTerm relOperators
133133
134134relVarP :: RelationalMarkerExpr a => Parser (RelationalExprBase a)
135135relVarP = RelationVariable <$> identifier <*> parseMarkerP
@@ -235,3 +235,18 @@ boolAtomP = do
235235
236236relationAtomExprP :: RelationalMarkerExpr a => Parser (AtomExprBase a)
237237relationAtomExprP = RelationAtomExpr <$> makeRelationP
238+
239+withMacroExprP :: RelationalMarkerExpr a => Parser (RelationalExprBase a)
240+withMacroExprP = do
241+ reservedOp "WITH"
242+ views <- parens (sepBy createViewP comma)
243+ expr <-relExprP
244+ pure $ With views expr
245+
246+createViewP :: RelationalMarkerExpr a => Parser (RelVarName, RelationalExprBase a)
247+createViewP = do
248+ name <- identifier
249+ reservedOp "AS"
250+ expr <- relExprP
251+ pure $ (name, expr)
252+
src/lib/ProjectM36/Base.hs+3−1
@@ -208,8 +208,10 @@ data RelationalExprBase a =
208208 --- | Returns the true relation iff
209209 Equals (RelationalExprBase a) (RelationalExprBase a) |
210210 NotEquals (RelationalExprBase a) (RelationalExprBase a) |
211− Extend (ExtendTupleExprBase a) (RelationalExprBase a)
211+ Extend (ExtendTupleExprBase a) (RelationalExprBase a) |
212212 --Summarize :: AtomExpr -> AttributeName -> RelationalExpr -> RelationalExpr -> RelationalExpr -- a special case of Extend
213+ --Evaluate relationalExpr with scoped views
214+ With [(RelVarName,RelationalExprBase a)] (RelationalExprBase a)
213215 deriving (Show, Eq, Generic, NFData)
214216
215217instance Binary RelationalExpr
src/lib/ProjectM36/RelationalExpression.hs+16
@@ -214,6 +214,22 @@ evalRelationalExpr (Equals relExprA relExprB) = do
214214 Left err -> return $ Left err
215215 Right relB -> return $ Right $ if relA == relB then relationTrue else relationFalse
216216
217+evalRelationalExpr (With views mainExpr) = do
218+ rstate <- ask
219+ --let ctx = stateElemsContext rstate
220+
221+ let addScopedView ctx (vname,vexpr) = case runState (evalDatabaseContextExpr (Assign vname vexpr)) (freshDatabaseState ctx) of
222+ (Left err,_) -> (Left err)
223+ (Right (), elems@(ctx',_,_)) -> (Right ctx')
224+
225+ case foldM addScopedView (stateElemsContext rstate) views of
226+ Left err -> return $ Left err
227+ Right ctx'' -> do
228+ let evalMainExpr = \expr -> runReader (evalRelationalExpr expr) (RelationalExprStateElems ctx'')
229+ case evalMainExpr mainExpr of
230+ Left err -> return $ Left err
231+ Right rel -> return $ Right rel
232+
217233--warning: copy-pasta from above- refactor
218234evalRelationalExpr (NotEquals relExprA relExprB) = do
219235 evaldA <- evalRelationalExpr relExprA
src/lib/ProjectM36/StaticOptimizer.hs+5−1
@@ -134,6 +134,8 @@ applyStaticRelationalOptimization e@(NotEquals _ _) = pure $ Right e
134134
135135applyStaticRelationalOptimization e@(Extend _ _) = pure $ Right e
136136
137+applyStaticRelationalOptimization e@(With _ _) = pure $ Right e
138+
137139applyStaticDatabaseOptimization :: DatabaseContextExpr -> DatabaseState (Either RelationalError DatabaseContextExpr)
138140applyStaticDatabaseOptimization x@NoOperation = pure $ Right x
139141applyStaticDatabaseOptimization x@(Define _ _) = pure $ Right x
@@ -378,6 +380,7 @@ applyStaticRestrictionCollapse expr =
378380 MakeStaticRelation _ _ -> expr
379381 ExistingRelation _ -> expr
380382 RelationVariable _ _ -> expr
383+ With _ _ -> expr
381384 Project attrs subexpr ->
382385 Project attrs (applyStaticRestrictionCollapse subexpr)
383386 Union sub1 sub2 ->
@@ -420,6 +423,7 @@ applyStaticRestrictionPushdown expr = case expr of
420423 MakeStaticRelation _ _ -> expr
421424 ExistingRelation _ -> expr
422425 RelationVariable _ _ -> expr
426+ With _ _ -> expr
423427 Project _ _ -> expr
424428 --this transformation cannot be inverted because the projection attributes might not exist in the inverted version
425429 Restrict restrictAttrs (Project projAttrs subexpr) ->
@@ -450,4 +454,4 @@ applyStaticRestrictionPushdown expr = case expr of
450454 Extend n sub ->
451455 Extend n (applyStaticRestrictionPushdown sub)
452456
453−
457+