Search in sources :

Example 16 with LetClause

use of org.apache.asterix.lang.common.clause.LetClause in project asterixdb by apache.

the class SqlppCloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(SelectExpression selectExpression, VariableSubstitutionEnvironment env) throws CompilationException {
    boolean subquery = selectExpression.isSubquery();
    List<LetClause> newLetList = new ArrayList<>();
    SelectSetOperation newSelectSetOperation;
    OrderbyClause newOrderbyClause = null;
    LimitClause newLimitClause = null;
    VariableSubstitutionEnvironment currentEnv = env;
    Pair<ILangExpression, VariableSubstitutionEnvironment> p;
    if (selectExpression.hasLetClauses()) {
        for (LetClause letClause : selectExpression.getLetList()) {
            p = letClause.accept(this, currentEnv);
            newLetList.add(letClause);
            currentEnv = p.second;
        }
    }
    p = selectExpression.getSelectSetOperation().accept(this, env);
    newSelectSetOperation = (SelectSetOperation) p.first;
    currentEnv = p.second;
    if (selectExpression.hasOrderby()) {
        p = selectExpression.getOrderbyClause().accept(this, currentEnv);
        newOrderbyClause = (OrderbyClause) p.first;
        currentEnv = p.second;
    }
    if (selectExpression.hasLimit()) {
        p = selectExpression.getLimitClause().accept(this, currentEnv);
        newLimitClause = (LimitClause) p.first;
        currentEnv = p.second;
    }
    return new Pair<>(new SelectExpression(newLetList, newSelectSetOperation, newOrderbyClause, newLimitClause, subquery), currentEnv);
}
Also used : LimitClause(org.apache.asterix.lang.common.clause.LimitClause) VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) LetClause(org.apache.asterix.lang.common.clause.LetClause) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) ArrayList(java.util.ArrayList) OrderbyClause(org.apache.asterix.lang.common.clause.OrderbyClause) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 17 with LetClause

use of org.apache.asterix.lang.common.clause.LetClause in project asterixdb by apache.

the class SqlppFormatPrintVisitor method visit.

@Override
public Void visit(SelectExpression selectStatement, Integer step) throws CompilationException {
    if (selectStatement.isSubquery()) {
        out.print("(");
    }
    int selectStep = selectStatement.isSubquery() ? step + 2 : step;
    if (selectStatement.hasLetClauses()) {
        for (LetClause letClause : selectStatement.getLetList()) {
            letClause.accept(this, selectStep);
        }
    }
    selectStatement.getSelectSetOperation().accept(this, selectStep);
    if (selectStatement.hasOrderby()) {
        selectStatement.getOrderbyClause().accept(this, selectStep);
    }
    if (selectStatement.hasLimit()) {
        selectStatement.getLimitClause().accept(this, selectStep);
    }
    if (selectStatement.isSubquery()) {
        out.print(skip(step) + " )");
    }
    return null;
}
Also used : LetClause(org.apache.asterix.lang.common.clause.LetClause)

Example 18 with LetClause

use of org.apache.asterix.lang.common.clause.LetClause in project asterixdb by apache.

the class AbstractSqlppExpressionScopingVisitor method visit.

@Override
public Expression visit(SelectExpression selectExpression, ILangExpression arg) throws CompilationException {
    Scope scopeBeforeSelectExpression = scopeChecker.getCurrentScope();
    scopeChecker.createNewScope();
    // visit let list
    if (selectExpression.hasLetClauses()) {
        for (LetClause letClause : selectExpression.getLetList()) {
            // Variables defined in WITH clauses are considered as named access instead of real variables.
            letClause.getVarExpr().getVar().setNamedValueAccess(true);
            letClause.accept(this, selectExpression);
        }
        scopeChecker.createNewScope();
    }
    // visit the main select.
    selectExpression.getSelectSetOperation().accept(this, selectExpression);
    // visit order by
    if (selectExpression.hasOrderby()) {
        selectExpression.getOrderbyClause().accept(this, selectExpression);
    }
    // visit limit
    if (selectExpression.hasLimit()) {
        selectExpression.getLimitClause().accept(this, selectExpression);
    }
    // Exit scopes that were entered within this select expression
    while (scopeChecker.getCurrentScope() != scopeBeforeSelectExpression) {
        scopeChecker.removeCurrentScope();
    }
    return selectExpression;
}
Also used : Scope(org.apache.asterix.lang.common.context.Scope) LetClause(org.apache.asterix.lang.common.clause.LetClause)

Example 19 with LetClause

use of org.apache.asterix.lang.common.clause.LetClause in project asterixdb by apache.

the class InlineColumnAliasVisitor method visit.

@Override
public Expression visit(SelectBlock selectBlock, ILangExpression arg) throws CompilationException {
    // Gets the map from select clause.
    Map<Expression, Expression> map = getMap(selectBlock.getSelectClause());
    // Removes all FROM/LET binding variables
    if (selectBlock.hasFromClause()) {
        map.keySet().removeAll(SqlppVariableUtil.getBindingVariables(selectBlock.getFromClause()));
    }
    if (selectBlock.hasLetClauses()) {
        map.keySet().removeAll(SqlppVariableUtil.getBindingVariables(selectBlock.getLetList()));
    }
    // Creates a substitution visitor.
    SqlppSubstituteExpressionVisitor visitor = new SqlppSubstituteExpressionVisitor(context, map);
    // Rewrites GROUP BY/LET/HAVING clauses.
    if (selectBlock.hasGroupbyClause()) {
        selectBlock.getGroupbyClause().accept(visitor, arg);
    }
    if (selectBlock.hasLetClausesAfterGroupby()) {
        for (LetClause letClause : selectBlock.getLetListAfterGroupby()) {
            letClause.accept(visitor, arg);
        }
    }
    if (selectBlock.hasHavingClause()) {
        selectBlock.getHavingClause().accept(visitor, arg);
    }
    SelectExpression selectExpression = (SelectExpression) arg;
    // For SET operation queries, column aliases will not substitute ORDER BY nor LIMIT expressions.
    if (!selectExpression.getSelectSetOperation().hasRightInputs()) {
        if (selectExpression.hasOrderby()) {
            selectExpression.getOrderbyClause().accept(visitor, arg);
        }
        if (selectExpression.hasLimit()) {
            selectExpression.getLimitClause().accept(visitor, arg);
        }
    }
    return super.visit(selectBlock, arg);
}
Also used : SqlppSubstituteExpressionVisitor(org.apache.asterix.lang.sqlpp.visitor.SqlppSubstituteExpressionVisitor) LetClause(org.apache.asterix.lang.common.clause.LetClause) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression)

Example 20 with LetClause

use of org.apache.asterix.lang.common.clause.LetClause 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

LetClause (org.apache.asterix.lang.common.clause.LetClause)23 Expression (org.apache.asterix.lang.common.base.Expression)11 ArrayList (java.util.ArrayList)9 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)9 GroupbyClause (org.apache.asterix.lang.common.clause.GroupbyClause)8 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)8 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)7 WhereClause (org.apache.asterix.lang.common.clause.WhereClause)6 HashMap (java.util.HashMap)5 DistinctClause (org.apache.asterix.lang.aql.clause.DistinctClause)5 ForClause (org.apache.asterix.lang.aql.clause.ForClause)5 Clause (org.apache.asterix.lang.common.base.Clause)5 FLWOGRExpression (org.apache.asterix.lang.aql.expression.FLWOGRExpression)4 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)4 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)4 Pair (org.apache.hyracks.algebricks.common.utils.Pair)4 LimitClause (org.apache.asterix.lang.common.clause.LimitClause)3 OrderbyClause (org.apache.asterix.lang.common.clause.OrderbyClause)3 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)3 FromClause (org.apache.asterix.lang.sqlpp.clause.FromClause)3