Merge lp:~zorba-coders/zorba/markos-scratch into lp:zorba

Proposed by Markos Zaharioudakis
Status: Merged
Approved by: Markos Zaharioudakis
Approved revision: 10983
Merged at revision: 11117
Proposed branch: lp:~zorba-coders/zorba/markos-scratch
Merge into: lp:zorba
Diff against target: 495 lines (+221/-200)
2 files modified
src/compiler/rewriter/rules/flwor_rules.cpp (+205/-198)
src/compiler/rewriter/rules/ruleset.h (+16/-2)
To merge this branch: bzr merge lp:~zorba-coders/zorba/markos-scratch
Reviewer Review Type Date Requested Status
Markos Zaharioudakis Approve
Review via email: mp+132101@code.launchpad.net

Commit message

fix annotations of merged AND exprs + slight optimization of the MergeFLWOR rule

Description of the change

fix annotations of merged AND exprs + slight optimization of the MergeFLWOR rule

To post a comment you must log in.
Revision history for this message
Markos Zaharioudakis (markos-za) :
review: Approve
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :

Validation queue job markos-scratch-2012-10-30T13-41-01.818Z is finished. The final status was:

All tests succeeded!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/compiler/rewriter/rules/flwor_rules.cpp'
2--- src/compiler/rewriter/rules/flwor_rules.cpp 2012-10-29 11:41:36 +0000
3+++ src/compiler/rewriter/rules/flwor_rules.cpp 2012-10-30 13:36:42 +0000
4@@ -88,56 +88,59 @@
5 Replace all references to theVarExpr inside the root expr of a given rCtx
6 with theSubstExpr
7 ******************************************************************************/
8-class SubstVars : public PrePostRewriteRule
9+class SubstVars : public RewriteRule
10 {
11 protected:
12 var_expr * theVarExpr;
13 expr * theSubstExpr;
14- std::vector<expr*> thePath;
15+ //std::vector<expr*> thePath;
16
17 public:
18 SubstVars(var_expr* var, expr* subst)
19 :
20- PrePostRewriteRule(RewriteRule::SubstVars, "SubstVars"),
21+ RewriteRule(RewriteRule::SubstVars, "SubstVars"),
22 theVarExpr(var),
23 theSubstExpr(subst)
24 {
25 }
26
27-protected:
28- expr* rewritePre(expr* node, RewriterContext& rCtx);
29- expr* rewritePost(expr* node, RewriterContext& rCtx);
30+ expr* apply(RewriterContext& rCtx, expr* node, bool& modified);
31 };
32
33
34-RULE_REWRITE_PRE(SubstVars)
35-{
36- thePath.push_back(node);
37-
38- if (node == theVarExpr)
39- {
40- std::vector<expr*>::iterator ite = thePath.begin();
41- std::vector<expr*>::iterator end = thePath.end();
42- for (; ite != end; ++ite)
43- {
44- expr::FreeVars& vars = (*ite)->getFreeVars();
45- vars.erase(theVarExpr);
46- vars.insert(theSubstExpr->getFreeVars().begin(),
47- theSubstExpr->getFreeVars().end());
48- }
49-
50- return theSubstExpr;
51- }
52- else
53- {
54- return NULL;
55- }
56-}
57-
58-
59-RULE_REWRITE_POST(SubstVars)
60-{
61- thePath.pop_back();
62+expr* SubstVars::apply(RewriterContext& rCtx, expr* node, bool& modified)
63+{
64+ //thePath.push_back(node);
65+
66+ ExprIterator iter(node);
67+
68+ while (!iter.done())
69+ {
70+ if (**iter == theVarExpr)
71+ {
72+#if 0
73+ std::vector<expr*>::iterator ite = thePath.begin();
74+ std::vector<expr*>::iterator end = thePath.end();
75+ for (; ite != end; ++ite)
76+ {
77+ expr::FreeVars& vars = (*ite)->getFreeVars();
78+ vars.erase(theVarExpr);
79+ vars.insert(theSubstExpr->getFreeVars().begin(),
80+ theSubstExpr->getFreeVars().end());
81+ }
82+#endif
83+ **iter = theSubstExpr;
84+ modified = true;
85+ }
86+ else
87+ {
88+ apply(rCtx, **iter, modified);
89+ }
90+
91+ iter.next();
92+ }
93+
94+ //thePath.pop_back();
95 return NULL;
96 }
97
98@@ -1041,7 +1044,12 @@
99 if (condExpr->get_function_kind() == FunctionConsts::OP_AND_N)
100 {
101 fo_expr* foCondExpr = static_cast<fo_expr*>(condExpr);
102- foWhereExpr->add_args(foCondExpr->get_args());
103+
104+ for (csize i = 0; i < foCondExpr->num_args(); ++i)
105+ {
106+ foWhereExpr->add_arg(foCondExpr->get_arg(i));
107+ expr_tools::fix_annotations(foWhereExpr, foCondExpr->get_arg(i));
108+ }
109 }
110 else
111 {
112@@ -1559,185 +1567,184 @@
113 /******************************************************************************
114
115 *******************************************************************************/
116-RULE_REWRITE_PRE(MergeFLWOR)
117+expr* MergeFLWOR::apply(RewriterContext& rCtx, expr* node, bool& modified)
118 {
119- flwor_expr* flwor = dynamic_cast<flwor_expr *>(node);
120-
121- if (flwor == NULL)
122- return NULL;
123-
124- bool modified = false;
125-
126- // Try to merge an inner flwor that appears in the return clause of the
127- // outer flwor.
128- if (flwor->get_return_expr()->get_expr_kind() == flwor_expr_kind &&
129- !flwor->get_return_expr()->is_sequential())
130+ if (node->get_expr_kind() == flwor_expr_kind ||
131+ node->get_expr_kind() == gflwor_expr_kind)
132 {
133- // TODO: If the return clause is sequential, we can still do the merge,
134- // but we must keep both the outer and the inner materialize clauses.
135-
136- flwor_expr* returnFlwor = static_cast<flwor_expr*>(flwor->get_return_expr());
137-
138- // If the outer flwor is not general, and it contains where, groupby, or
139- // orderby clauses, we cannot merge because for/let clauses cannot appear
140- // after where, groupby, or orderby clauses,
141- if (!flwor->is_general())
142+ flwor_expr* flwor = static_cast<flwor_expr *>(node);
143+
144+ // Try to merge an inner flwor that appears in the return clause of the
145+ // outer flwor.
146+ if (flwor->get_return_expr()->get_expr_kind() == flwor_expr_kind &&
147+ !flwor->get_return_expr()->is_sequential())
148 {
149- csize numClauses = flwor->num_clauses();
150+ // TODO: If the return clause is sequential, we can still do the merge,
151+ // but we must keep both the outer and the inner materialize clauses.
152+
153+ flwor_expr* returnFlwor = static_cast<flwor_expr*>(flwor->get_return_expr());
154+
155+ // If the outer flwor is not general, and it contains where, groupby, or
156+ // orderby clauses, we cannot merge because for/let clauses cannot appear
157+ // after where, groupby, or orderby clauses,
158+ if (!flwor->is_general())
159+ {
160+ csize numClauses = flwor->num_clauses();
161+
162+ for (csize i = 0; i < numClauses; ++i)
163+ {
164+ const flwor_clause* c = flwor->get_clause(i);
165+
166+ if (c->get_kind() == flwor_clause::where_clause ||
167+ c->get_kind() == flwor_clause::group_clause ||
168+ c->get_kind() == flwor_clause::order_clause)
169+ {
170+ goto next1;
171+ }
172+ }
173+ }
174+
175+ csize numClauses = returnFlwor->num_clauses();
176
177 for (csize i = 0; i < numClauses; ++i)
178 {
179- const flwor_clause* c = flwor->get_clause(i);
180-
181- if (c->get_kind() == flwor_clause::where_clause ||
182- c->get_kind() == flwor_clause::group_clause ||
183+ const flwor_clause* c = returnFlwor->get_clause(i);
184+
185+ if (c->get_kind() == flwor_clause::group_clause ||
186 c->get_kind() == flwor_clause::order_clause)
187 {
188 goto next1;
189 }
190 }
191- }
192-
193- csize numClauses = returnFlwor->num_clauses();
194-
195- for (csize i = 0; i < numClauses; ++i)
196- {
197- const flwor_clause* c = returnFlwor->get_clause(i);
198-
199- if (c->get_kind() == flwor_clause::group_clause ||
200- c->get_kind() == flwor_clause::order_clause)
201- {
202- goto next1;
203- }
204- }
205-
206- for (csize i = 0; i < numClauses; ++i)
207- {
208- flwor->add_clause(returnFlwor->get_clause(i));
209- }
210-
211- flwor->set_return_expr(returnFlwor->get_return_expr());
212-
213- modified = true;
214+
215+ for (csize i = 0; i < numClauses; ++i)
216+ {
217+ flwor->add_clause(returnFlwor->get_clause(i));
218+ }
219+
220+ flwor->set_return_expr(returnFlwor->get_return_expr());
221+
222+ modified = true;
223+ }
224+
225+ next1:
226+
227+ csize numClauses = flwor->num_clauses();
228+
229+ // Try to merge an inner flwor that appears in a for/let clause of the outer
230+ // flwor.
231+ for (csize i = 0; i < numClauses; ++i)
232+ {
233+ bool merge = false;
234+ flwor_expr* nestedFlwor = NULL;
235+ csize numNestedClauses;
236+
237+ flwor_clause* c = flwor->get_clause(i);
238+
239+ if (c->get_kind() == flwor_clause::let_clause)
240+ {
241+ expr* domainExpr = static_cast<let_clause*>(c)->get_expr();
242+
243+ if (domainExpr->get_expr_kind() == flwor_expr_kind &&
244+ !domainExpr->is_sequential())
245+ {
246+ nestedFlwor = static_cast<flwor_expr*>(domainExpr);
247+ numNestedClauses = nestedFlwor->num_clauses();
248+ merge = true;
249+
250+ for (csize j = 0; j < numNestedClauses; ++j)
251+ {
252+ flwor_clause* nestedClause = nestedFlwor->get_clause(j);
253+ flwor_clause::ClauseKind nestedClauseKind = nestedClause->get_kind();
254+
255+ if (nestedClauseKind == flwor_clause::for_clause)
256+ {
257+ xqtref_t nestedDomainType =
258+ static_cast<for_clause*>(nestedClause)->get_expr()->get_return_type();
259+
260+ if (nestedDomainType->get_quantifier() != TypeConstants::QUANT_ONE)
261+ {
262+ merge = false;
263+ break;
264+ }
265+ }
266+ else if (nestedClauseKind != flwor_clause::let_clause)
267+ {
268+ merge = false;
269+ break;
270+ }
271+ }
272+ }
273+ }
274+ else if (c->get_kind() == flwor_clause::for_clause &&
275+ static_cast<for_clause*>(c)->get_pos_var() == NULL)
276+ {
277+ expr* domainExpr = static_cast<for_clause*>(c)->get_expr();
278+
279+ if (domainExpr->get_expr_kind() == flwor_expr_kind &&
280+ !domainExpr->is_sequential())
281+ {
282+ nestedFlwor = static_cast<flwor_expr*>(domainExpr);
283+ numNestedClauses = nestedFlwor->num_clauses();
284+ merge = true;
285+
286+ for (csize j = 0; j < numNestedClauses; ++j)
287+ {
288+ flwor_clause* nestedClause = nestedFlwor->get_clause(j);
289+ flwor_clause::ClauseKind nestedClauseKind = nestedClause->get_kind();
290+
291+ if (nestedClauseKind != flwor_clause::let_clause &&
292+ nestedClauseKind != flwor_clause::for_clause)
293+ {
294+#if 1
295+ // temp hack until we have an optimized general flwor
296+ if (nestedClauseKind == flwor_clause::where_clause &&
297+ i == numClauses-1 &&
298+ flwor->get_where() == NULL &&
299+ nestedFlwor->get_return_expr()->get_var() != NULL)
300+ {
301+ continue;
302+ }
303+#endif
304+ merge = false;
305+ break;
306+ }
307+ }
308+ }
309+ }
310+
311+ if (merge)
312+ {
313+ for (csize j = 0; j < numNestedClauses; ++j)
314+ {
315+ flwor_clause* nestedClause = nestedFlwor->get_clause(j);
316+#if 1
317+ if (nestedClause->get_kind() == flwor_clause::where_clause)
318+ flwor->add_clause(i+j+1, nestedClause);
319+ else
320+#endif
321+ flwor->add_clause(i+j, nestedClause);
322+ }
323+
324+ c->set_expr(nestedFlwor->get_return_expr());
325+
326+ numClauses += numNestedClauses;
327+ i += numNestedClauses;
328+
329+ modified = true;
330+ }
331+ }
332 }
333
334- next1:
335-
336- csize numClauses = flwor->num_clauses();
337-
338- // Try to merge an inner flwor that appears in a for/let clause of the outer
339- // flwor.
340- for (csize i = 0; i < numClauses; ++i)
341+ ExprIterator iter(node);
342+
343+ while (!iter.done())
344 {
345- bool merge = false;
346- flwor_expr* nestedFlwor = NULL;
347- csize numNestedClauses;
348-
349- flwor_clause* c = flwor->get_clause(i);
350-
351- if (c->get_kind() == flwor_clause::let_clause)
352- {
353- expr* domainExpr = static_cast<let_clause*>(c)->get_expr();
354-
355- if (domainExpr->get_expr_kind() == flwor_expr_kind &&
356- !domainExpr->is_sequential())
357- {
358- nestedFlwor = static_cast<flwor_expr*>(domainExpr);
359- numNestedClauses = nestedFlwor->num_clauses();
360- merge = true;
361-
362- for (csize j = 0; j < numNestedClauses; ++j)
363- {
364- flwor_clause* nestedClause = nestedFlwor->get_clause(j);
365- flwor_clause::ClauseKind nestedClauseKind = nestedClause->get_kind();
366-
367- if (nestedClauseKind == flwor_clause::for_clause)
368- {
369- xqtref_t nestedDomainType =
370- static_cast<for_clause*>(nestedClause)->get_expr()->get_return_type();
371-
372- if (nestedDomainType->get_quantifier() != TypeConstants::QUANT_ONE)
373- {
374- merge = false;
375- break;
376- }
377- }
378- else if (nestedClauseKind != flwor_clause::let_clause)
379- {
380- merge = false;
381- break;
382- }
383- }
384- }
385- }
386- else if (c->get_kind() == flwor_clause::for_clause &&
387- static_cast<for_clause*>(c)->get_pos_var() == NULL)
388- {
389- expr* domainExpr = static_cast<for_clause*>(c)->get_expr();
390-
391- if (domainExpr->get_expr_kind() == flwor_expr_kind &&
392- !domainExpr->is_sequential())
393- {
394- nestedFlwor = static_cast<flwor_expr*>(domainExpr);
395- numNestedClauses = nestedFlwor->num_clauses();
396- merge = true;
397-
398- for (csize j = 0; j < numNestedClauses; ++j)
399- {
400- flwor_clause* nestedClause = nestedFlwor->get_clause(j);
401- flwor_clause::ClauseKind nestedClauseKind = nestedClause->get_kind();
402-
403- if (nestedClauseKind != flwor_clause::let_clause &&
404- nestedClauseKind != flwor_clause::for_clause)
405- {
406-#if 1
407- // temp hack until we have an optimized general flwor
408- if (nestedClauseKind == flwor_clause::where_clause &&
409- i == numClauses-1 &&
410- flwor->get_where() == NULL &&
411- nestedFlwor->get_return_expr()->get_var() != NULL)
412- {
413- continue;
414- }
415-#endif
416- merge = false;
417- break;
418- }
419- }
420- }
421- }
422-
423- if (merge)
424- {
425- for (ulong j = 0; j < numNestedClauses; ++j)
426- {
427- flwor_clause* nestedClause = nestedFlwor->get_clause(j);
428-#if 1
429- if (nestedClause->get_kind() == flwor_clause::where_clause)
430- flwor->add_clause(i+j+1, nestedClause);
431- else
432-#endif
433- flwor->add_clause(i+j, nestedClause);
434- }
435-
436- c->set_expr(nestedFlwor->get_return_expr());
437-
438- numClauses += numNestedClauses;
439- i += numNestedClauses;
440-
441- modified = true;
442- }
443+ apply(rCtx, **iter, modified);
444+
445+ iter.next();
446 }
447
448- return (modified ? node : NULL);
449-}
450-
451-
452-/******************************************************************************
453-
454-*******************************************************************************/
455-RULE_REWRITE_POST(MergeFLWOR)
456-{
457 return NULL;
458 }
459
460
461=== modified file 'src/compiler/rewriter/rules/ruleset.h'
462--- src/compiler/rewriter/rules/ruleset.h 2012-10-29 11:41:36 +0000
463+++ src/compiler/rewriter/rules/ruleset.h 2012-10-30 13:36:42 +0000
464@@ -43,8 +43,6 @@
465
466 PREPOST_RULE(RefactorPredFLWOR);
467
468-PREPOST_RULE(MergeFLWOR);
469-
470 PREPOST_RULE(EliminateExtraneousPathSteps);
471
472 PREPOST_RULE(InlineFunctions);
473@@ -96,6 +94,22 @@
474 /*******************************************************************************
475
476 ********************************************************************************/
477+class MergeFLWOR : public RewriteRule
478+{
479+public:
480+ MergeFLWOR()
481+ :
482+ RewriteRule(RewriteRule::MergeFLWOR, "MergeFLWOR")
483+ {
484+ }
485+
486+ expr* apply(RewriterContext& rCtx, expr* node, bool& modified);
487+};
488+
489+
490+/*******************************************************************************
491+
492+********************************************************************************/
493 class FoldConst : public RewriteRule
494 {
495 public:

Subscribers

People subscribed via source and target branches