use of org.apache.asterix.lang.sqlpp.util.SqlppRewriteUtil.substituteExpression in project asterixdb by apache.
the class InlineWithExpressionVisitor method visit.
@Override
public Expression visit(SelectExpression selectExpression, ILangExpression arg) throws CompilationException {
if (selectExpression.hasLetClauses()) {
// Inlines the leading WITH list.
Map<Expression, Expression> varExprMap = new HashMap<>();
List<LetClause> withs = selectExpression.getLetList();
Iterator<LetClause> with = withs.iterator();
while (with.hasNext()) {
LetClause letClause = with.next();
// Replaces the let binding Expr.
Expression expr = letClause.getBindingExpr();
Expression newBindingExpr = SqlppRewriteUtil.substituteExpression(expr, varExprMap, context);
letClause.setBindingExpr(newBindingExpr);
// Performs the rewriting recursively in the newBindingExpr itself.
super.visit(newBindingExpr, arg);
// Removes the WITH entry and adds variable-expr mapping into the varExprMap.
with.remove();
Expression bindingExpr = letClause.getBindingExpr();
// Wraps the binding expression with IndependentSubquery, so that free identifier references
// in the binding expression will not be resolved use outer-scope variables.
varExprMap.put(letClause.getVarExpr(), bindingExpr);
}
// Inlines WITH expressions into the select expression.
SelectExpression newSelectExpression = (SelectExpression) substituteExpression(selectExpression, varExprMap, context);
// Continues to visit the rewritten select expression.
return super.visit(newSelectExpression, arg);
} else {
// Continues to visit inside the select expression.
return super.visit(selectExpression, arg);
}
}
Aggregations