Search in sources :

Example 11 with SelectExpression

use of org.apache.asterix.lang.sqlpp.expression.SelectExpression 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 12 with SelectExpression

use of org.apache.asterix.lang.sqlpp.expression.SelectExpression 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)

Example 13 with SelectExpression

use of org.apache.asterix.lang.sqlpp.expression.SelectExpression in project asterixdb by apache.

the class DeepCopyVisitor method visit.

@Override
public SelectExpression visit(SelectExpression selectExpression, Void arg) throws CompilationException {
    List<LetClause> lets = new ArrayList<>();
    SelectSetOperation select;
    OrderbyClause orderby = null;
    LimitClause limit = null;
    // visit let list
    if (selectExpression.hasLetClauses()) {
        for (LetClause letClause : selectExpression.getLetList()) {
            lets.add((LetClause) letClause.accept(this, arg));
        }
    }
    // visit the main select.
    select = (SelectSetOperation) selectExpression.getSelectSetOperation().accept(this, arg);
    // visit order by
    if (selectExpression.hasOrderby()) {
        List<Expression> orderExprs = new ArrayList<>();
        for (Expression orderExpr : selectExpression.getOrderbyClause().getOrderbyList()) {
            orderExprs.add((Expression) orderExpr.accept(this, arg));
        }
        orderby = new OrderbyClause(orderExprs, selectExpression.getOrderbyClause().getModifierList());
    }
    // visit limit
    if (selectExpression.hasLimit()) {
        limit = (LimitClause) selectExpression.getLimitClause().accept(this, arg);
    }
    return new SelectExpression(lets, select, orderby, limit, selectExpression.isSubquery());
}
Also used : LimitClause(org.apache.asterix.lang.common.clause.LimitClause) 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) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) ArrayList(java.util.ArrayList) OrderbyClause(org.apache.asterix.lang.common.clause.OrderbyClause) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression)

Example 14 with SelectExpression

use of org.apache.asterix.lang.sqlpp.expression.SelectExpression in project asterixdb by apache.

the class FreeVariableVisitor method visit.

@Override
public Void visit(SelectExpression selectExpression, Collection<VariableExpr> freeVars) throws CompilationException {
    Collection<VariableExpr> letsFreeVars = new HashSet<>();
    Collection<VariableExpr> selectFreeVars = new HashSet<>();
    visitLetClauses(selectExpression.getLetList(), letsFreeVars);
    // visit order by
    if (selectExpression.hasOrderby()) {
        for (Expression orderExpr : selectExpression.getOrderbyClause().getOrderbyList()) {
            orderExpr.accept(this, selectFreeVars);
        }
    }
    // visit limit
    if (selectExpression.hasLimit()) {
        selectExpression.getLimitClause().accept(this, selectFreeVars);
    }
    // visit the main select
    selectExpression.getSelectSetOperation().accept(this, selectFreeVars);
    // Removed let binding variables.
    selectFreeVars.removeAll(SqlppVariableUtil.getBindingVariables(selectExpression.getLetList()));
    freeVars.addAll(letsFreeVars);
    freeVars.addAll(selectFreeVars);
    return null;
}
Also used : CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) HashSet(java.util.HashSet)

Aggregations

SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)14 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)10 Expression (org.apache.asterix.lang.common.base.Expression)9 SelectSetOperation (org.apache.asterix.lang.sqlpp.clause.SelectSetOperation)8 ArrayList (java.util.ArrayList)7 LetClause (org.apache.asterix.lang.common.clause.LetClause)6 SetOperationInput (org.apache.asterix.lang.sqlpp.struct.SetOperationInput)6 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)5 SelectBlock (org.apache.asterix.lang.sqlpp.clause.SelectBlock)5 LimitClause (org.apache.asterix.lang.common.clause.LimitClause)4 OrderbyClause (org.apache.asterix.lang.common.clause.OrderbyClause)4 FromClause (org.apache.asterix.lang.sqlpp.clause.FromClause)4 SelectClause (org.apache.asterix.lang.sqlpp.clause.SelectClause)4 HashMap (java.util.HashMap)3 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)3 FromTerm (org.apache.asterix.lang.sqlpp.clause.FromTerm)3 SelectElement (org.apache.asterix.lang.sqlpp.clause.SelectElement)3 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)3 SetOperationRight (org.apache.asterix.lang.sqlpp.struct.SetOperationRight)3 Pair (org.apache.hyracks.algebricks.common.utils.Pair)3