Search in sources :

Example 1 with SqlppRewriteUtil.substituteExpression

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);
    }
}
Also used : LetClause(org.apache.asterix.lang.common.clause.LetClause) HashMap(java.util.HashMap) Expression(org.apache.asterix.lang.common.base.Expression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) SqlppRewriteUtil.substituteExpression(org.apache.asterix.lang.sqlpp.util.SqlppRewriteUtil.substituteExpression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression)

Aggregations

HashMap (java.util.HashMap)1 Expression (org.apache.asterix.lang.common.base.Expression)1 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)1 LetClause (org.apache.asterix.lang.common.clause.LetClause)1 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)1 SqlppRewriteUtil.substituteExpression (org.apache.asterix.lang.sqlpp.util.SqlppRewriteUtil.substituteExpression)1