88
88
Replace all references to theVarExpr inside the root expr of a given rCtx
90
90
******************************************************************************/
91
class SubstVars : public PrePostRewriteRule
91
class SubstVars : public RewriteRule
94
94
var_expr * theVarExpr;
95
95
expr * theSubstExpr;
96
std::vector<expr*> thePath;
96
//std::vector<expr*> thePath;
99
99
SubstVars(var_expr* var, expr* subst)
101
PrePostRewriteRule(RewriteRule::SubstVars, "SubstVars"),
101
RewriteRule(RewriteRule::SubstVars, "SubstVars"),
103
103
theSubstExpr(subst)
108
expr* rewritePre(expr* node, RewriterContext& rCtx);
109
expr* rewritePost(expr* node, RewriterContext& rCtx);
107
expr* apply(RewriterContext& rCtx, expr* node, bool& modified);
113
RULE_REWRITE_PRE(SubstVars)
115
thePath.push_back(node);
117
if (node == theVarExpr)
119
std::vector<expr*>::iterator ite = thePath.begin();
120
std::vector<expr*>::iterator end = thePath.end();
121
for (; ite != end; ++ite)
123
expr::FreeVars& vars = (*ite)->getFreeVars();
124
vars.erase(theVarExpr);
125
vars.insert(theSubstExpr->getFreeVars().begin(),
126
theSubstExpr->getFreeVars().end());
138
RULE_REWRITE_POST(SubstVars)
111
expr* SubstVars::apply(RewriterContext& rCtx, expr* node, bool& modified)
113
//thePath.push_back(node);
115
ExprIterator iter(node);
119
if (**iter == theVarExpr)
122
std::vector<expr*>::iterator ite = thePath.begin();
123
std::vector<expr*>::iterator end = thePath.end();
124
for (; ite != end; ++ite)
126
expr::FreeVars& vars = (*ite)->getFreeVars();
127
vars.erase(theVarExpr);
128
vars.insert(theSubstExpr->getFreeVars().begin(),
129
theSubstExpr->getFreeVars().end());
132
**iter = theSubstExpr;
137
apply(rCtx, **iter, modified);
143
//thePath.pop_back();
1559
1567
/******************************************************************************
1561
1569
*******************************************************************************/
1562
RULE_REWRITE_PRE(MergeFLWOR)
1570
expr* MergeFLWOR::apply(RewriterContext& rCtx, expr* node, bool& modified)
1564
flwor_expr* flwor = dynamic_cast<flwor_expr *>(node);
1569
bool modified = false;
1571
// Try to merge an inner flwor that appears in the return clause of the
1573
if (flwor->get_return_expr()->get_expr_kind() == flwor_expr_kind &&
1574
!flwor->get_return_expr()->is_sequential())
1572
if (node->get_expr_kind() == flwor_expr_kind ||
1573
node->get_expr_kind() == gflwor_expr_kind)
1576
// TODO: If the return clause is sequential, we can still do the merge,
1577
// but we must keep both the outer and the inner materialize clauses.
1579
flwor_expr* returnFlwor = static_cast<flwor_expr*>(flwor->get_return_expr());
1581
// If the outer flwor is not general, and it contains where, groupby, or
1582
// orderby clauses, we cannot merge because for/let clauses cannot appear
1583
// after where, groupby, or orderby clauses,
1584
if (!flwor->is_general())
1575
flwor_expr* flwor = static_cast<flwor_expr *>(node);
1577
// Try to merge an inner flwor that appears in the return clause of the
1579
if (flwor->get_return_expr()->get_expr_kind() == flwor_expr_kind &&
1580
!flwor->get_return_expr()->is_sequential())
1586
csize numClauses = flwor->num_clauses();
1582
// TODO: If the return clause is sequential, we can still do the merge,
1583
// but we must keep both the outer and the inner materialize clauses.
1585
flwor_expr* returnFlwor = static_cast<flwor_expr*>(flwor->get_return_expr());
1587
// If the outer flwor is not general, and it contains where, groupby, or
1588
// orderby clauses, we cannot merge because for/let clauses cannot appear
1589
// after where, groupby, or orderby clauses,
1590
if (!flwor->is_general())
1592
csize numClauses = flwor->num_clauses();
1594
for (csize i = 0; i < numClauses; ++i)
1596
const flwor_clause* c = flwor->get_clause(i);
1598
if (c->get_kind() == flwor_clause::where_clause ||
1599
c->get_kind() == flwor_clause::group_clause ||
1600
c->get_kind() == flwor_clause::order_clause)
1607
csize numClauses = returnFlwor->num_clauses();
1588
1609
for (csize i = 0; i < numClauses; ++i)
1590
const flwor_clause* c = flwor->get_clause(i);
1592
if (c->get_kind() == flwor_clause::where_clause ||
1593
c->get_kind() == flwor_clause::group_clause ||
1611
const flwor_clause* c = returnFlwor->get_clause(i);
1613
if (c->get_kind() == flwor_clause::group_clause ||
1594
1614
c->get_kind() == flwor_clause::order_clause)
1601
csize numClauses = returnFlwor->num_clauses();
1603
for (csize i = 0; i < numClauses; ++i)
1605
const flwor_clause* c = returnFlwor->get_clause(i);
1607
if (c->get_kind() == flwor_clause::group_clause ||
1608
c->get_kind() == flwor_clause::order_clause)
1614
for (csize i = 0; i < numClauses; ++i)
1616
flwor->add_clause(returnFlwor->get_clause(i));
1619
flwor->set_return_expr(returnFlwor->get_return_expr());
1620
for (csize i = 0; i < numClauses; ++i)
1622
flwor->add_clause(returnFlwor->get_clause(i));
1625
flwor->set_return_expr(returnFlwor->get_return_expr());
1632
csize numClauses = flwor->num_clauses();
1634
// Try to merge an inner flwor that appears in a for/let clause of the outer
1636
for (csize i = 0; i < numClauses; ++i)
1639
flwor_expr* nestedFlwor = NULL;
1640
csize numNestedClauses;
1642
flwor_clause* c = flwor->get_clause(i);
1644
if (c->get_kind() == flwor_clause::let_clause)
1646
expr* domainExpr = static_cast<let_clause*>(c)->get_expr();
1648
if (domainExpr->get_expr_kind() == flwor_expr_kind &&
1649
!domainExpr->is_sequential())
1651
nestedFlwor = static_cast<flwor_expr*>(domainExpr);
1652
numNestedClauses = nestedFlwor->num_clauses();
1655
for (csize j = 0; j < numNestedClauses; ++j)
1657
flwor_clause* nestedClause = nestedFlwor->get_clause(j);
1658
flwor_clause::ClauseKind nestedClauseKind = nestedClause->get_kind();
1660
if (nestedClauseKind == flwor_clause::for_clause)
1662
xqtref_t nestedDomainType =
1663
static_cast<for_clause*>(nestedClause)->get_expr()->get_return_type();
1665
if (nestedDomainType->get_quantifier() != TypeConstants::QUANT_ONE)
1671
else if (nestedClauseKind != flwor_clause::let_clause)
1679
else if (c->get_kind() == flwor_clause::for_clause &&
1680
static_cast<for_clause*>(c)->get_pos_var() == NULL)
1682
expr* domainExpr = static_cast<for_clause*>(c)->get_expr();
1684
if (domainExpr->get_expr_kind() == flwor_expr_kind &&
1685
!domainExpr->is_sequential())
1687
nestedFlwor = static_cast<flwor_expr*>(domainExpr);
1688
numNestedClauses = nestedFlwor->num_clauses();
1691
for (csize j = 0; j < numNestedClauses; ++j)
1693
flwor_clause* nestedClause = nestedFlwor->get_clause(j);
1694
flwor_clause::ClauseKind nestedClauseKind = nestedClause->get_kind();
1696
if (nestedClauseKind != flwor_clause::let_clause &&
1697
nestedClauseKind != flwor_clause::for_clause)
1700
// temp hack until we have an optimized general flwor
1701
if (nestedClauseKind == flwor_clause::where_clause &&
1702
i == numClauses-1 &&
1703
flwor->get_where() == NULL &&
1704
nestedFlwor->get_return_expr()->get_var() != NULL)
1718
for (csize j = 0; j < numNestedClauses; ++j)
1720
flwor_clause* nestedClause = nestedFlwor->get_clause(j);
1722
if (nestedClause->get_kind() == flwor_clause::where_clause)
1723
flwor->add_clause(i+j+1, nestedClause);
1726
flwor->add_clause(i+j, nestedClause);
1729
c->set_expr(nestedFlwor->get_return_expr());
1731
numClauses += numNestedClauses;
1732
i += numNestedClauses;
1626
csize numClauses = flwor->num_clauses();
1628
// Try to merge an inner flwor that appears in a for/let clause of the outer
1630
for (csize i = 0; i < numClauses; ++i)
1739
ExprIterator iter(node);
1741
while (!iter.done())
1633
flwor_expr* nestedFlwor = NULL;
1634
csize numNestedClauses;
1636
flwor_clause* c = flwor->get_clause(i);
1638
if (c->get_kind() == flwor_clause::let_clause)
1640
expr* domainExpr = static_cast<let_clause*>(c)->get_expr();
1642
if (domainExpr->get_expr_kind() == flwor_expr_kind &&
1643
!domainExpr->is_sequential())
1645
nestedFlwor = static_cast<flwor_expr*>(domainExpr);
1646
numNestedClauses = nestedFlwor->num_clauses();
1649
for (csize j = 0; j < numNestedClauses; ++j)
1651
flwor_clause* nestedClause = nestedFlwor->get_clause(j);
1652
flwor_clause::ClauseKind nestedClauseKind = nestedClause->get_kind();
1654
if (nestedClauseKind == flwor_clause::for_clause)
1656
xqtref_t nestedDomainType =
1657
static_cast<for_clause*>(nestedClause)->get_expr()->get_return_type();
1659
if (nestedDomainType->get_quantifier() != TypeConstants::QUANT_ONE)
1665
else if (nestedClauseKind != flwor_clause::let_clause)
1673
else if (c->get_kind() == flwor_clause::for_clause &&
1674
static_cast<for_clause*>(c)->get_pos_var() == NULL)
1676
expr* domainExpr = static_cast<for_clause*>(c)->get_expr();
1678
if (domainExpr->get_expr_kind() == flwor_expr_kind &&
1679
!domainExpr->is_sequential())
1681
nestedFlwor = static_cast<flwor_expr*>(domainExpr);
1682
numNestedClauses = nestedFlwor->num_clauses();
1685
for (csize j = 0; j < numNestedClauses; ++j)
1687
flwor_clause* nestedClause = nestedFlwor->get_clause(j);
1688
flwor_clause::ClauseKind nestedClauseKind = nestedClause->get_kind();
1690
if (nestedClauseKind != flwor_clause::let_clause &&
1691
nestedClauseKind != flwor_clause::for_clause)
1694
// temp hack until we have an optimized general flwor
1695
if (nestedClauseKind == flwor_clause::where_clause &&
1696
i == numClauses-1 &&
1697
flwor->get_where() == NULL &&
1698
nestedFlwor->get_return_expr()->get_var() != NULL)
1712
for (ulong j = 0; j < numNestedClauses; ++j)
1714
flwor_clause* nestedClause = nestedFlwor->get_clause(j);
1716
if (nestedClause->get_kind() == flwor_clause::where_clause)
1717
flwor->add_clause(i+j+1, nestedClause);
1720
flwor->add_clause(i+j, nestedClause);
1723
c->set_expr(nestedFlwor->get_return_expr());
1725
numClauses += numNestedClauses;
1726
i += numNestedClauses;
1743
apply(rCtx, **iter, modified);
1732
return (modified ? node : NULL);
1736
/******************************************************************************
1738
*******************************************************************************/
1739
RULE_REWRITE_POST(MergeFLWOR)