on master help sign in
add with macro with multiple views
commit0b24d9d2914c3fb8d1265555bfc70d495e4a7fa2 authorJohnny Liao <dycan0716@gmail.com> dateWed 20 Jun 2018, 10:27 (8 years ago) parenta81e186
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
@@ -130,7 +130,7 @@ relOperators = [
130130 ]
131131
132132relExprP :: RelationalMarkerExpr a => Parser (RelationalExprBase a)
133−relExprP = makeExprParser relTerm relOperators
133+relExprP = try withMacroExprP <|> makeExprParser relTerm relOperators
134134
135135relVarP :: RelationalMarkerExpr a => Parser (RelationalExprBase a)
136136relVarP = RelationVariable <$> identifier <*> parseMarkerP
@@ -237,3 +237,18 @@ boolAtomP = do
237237
238238relationAtomExprP :: RelationalMarkerExpr a => Parser (AtomExprBase a)
239239relationAtomExprP = RelationAtomExpr <$> makeRelationP
240+
241+withMacroExprP :: RelationalMarkerExpr a => Parser (RelationalExprBase a)
242+withMacroExprP = do
243+ reservedOp "WITH"
244+ views <- parens (sepBy createViewP comma)
245+ expr <-relExprP
246+ pure $ With views expr
247+
248+createViewP :: RelationalMarkerExpr a => Parser (RelVarName, RelationalExprBase a)
249+createViewP = do
250+ name <- identifier
251+ reservedOp "AS"
252+ expr <- relExprP
253+ pure $ (name, expr)
254+
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+