Search in sources :

Example 6 with SelectExpression

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

the class SqlppGroupByVisitor method visit.

@Override
public Expression visit(SelectBlock selectBlock, ILangExpression arg) throws CompilationException {
    // Traverses the select block in the order of "from", "let"s, "where",
    // "group by", "let"s, "having" and "select".
    FromClause fromClause = selectBlock.getFromClause();
    if (selectBlock.hasFromClause()) {
        fromClause.accept(this, arg);
    }
    if (selectBlock.hasLetClauses()) {
        List<LetClause> letList = selectBlock.getLetList();
        for (LetClause letClause : letList) {
            letClause.accept(this, arg);
        }
    }
    if (selectBlock.hasWhereClause()) {
        selectBlock.getWhereClause().accept(this, arg);
    }
    if (selectBlock.hasGroupbyClause()) {
        GroupbyClause groupbyClause = selectBlock.getGroupbyClause();
        groupbyClause.accept(this, fromClause);
        Collection<VariableExpr> visibleVarsInCurrentScope = SqlppVariableUtil.getBindingVariables(groupbyClause);
        VariableExpr groupVar = groupbyClause.getGroupVar();
        Set<VariableExpr> groupFieldVars = getGroupFieldVariables(groupbyClause);
        Collection<VariableExpr> freeVariablesInGbyLets = new HashSet<>();
        if (selectBlock.hasLetClausesAfterGroupby()) {
            List<LetClause> letListAfterGby = selectBlock.getLetListAfterGroupby();
            for (LetClause letClauseAfterGby : letListAfterGby) {
                letClauseAfterGby.accept(this, arg);
                // Rewrites each let clause after the group-by.
                SqlppRewriteUtil.rewriteExpressionUsingGroupVariable(groupVar, groupFieldVars, letClauseAfterGby, context);
                Collection<VariableExpr> freeVariablesInLet = SqlppVariableUtil.getFreeVariables(letClauseAfterGby.getBindingExpr());
                freeVariablesInLet.removeAll(visibleVarsInCurrentScope);
                freeVariablesInGbyLets.addAll(freeVariablesInLet);
                visibleVarsInCurrentScope.add(letClauseAfterGby.getVarExpr());
            }
        }
        Collection<VariableExpr> freeVariables = new HashSet<>();
        if (selectBlock.hasHavingClause()) {
            // Rewrites the having clause.
            HavingClause havingClause = selectBlock.getHavingClause();
            havingClause.accept(this, arg);
            SqlppRewriteUtil.rewriteExpressionUsingGroupVariable(groupVar, groupFieldVars, havingClause, context);
            freeVariables.addAll(SqlppVariableUtil.getFreeVariables(havingClause));
        }
        SelectExpression parentSelectExpression = (SelectExpression) arg;
        // We cannot rewrite ORDER BY and LIMIT if it's a SET operation query.
        if (!parentSelectExpression.getSelectSetOperation().hasRightInputs()) {
            if (parentSelectExpression.hasOrderby()) {
                // Rewrites the ORDER BY clause.
                OrderbyClause orderbyClause = parentSelectExpression.getOrderbyClause();
                orderbyClause.accept(this, arg);
                SqlppRewriteUtil.rewriteExpressionUsingGroupVariable(groupVar, groupFieldVars, orderbyClause, context);
                freeVariables.addAll(SqlppVariableUtil.getFreeVariables(orderbyClause));
            }
            if (parentSelectExpression.hasLimit()) {
                // Rewrites the LIMIT clause.
                LimitClause limitClause = parentSelectExpression.getLimitClause();
                limitClause.accept(this, arg);
                SqlppRewriteUtil.rewriteExpressionUsingGroupVariable(groupVar, groupFieldVars, limitClause, context);
                freeVariables.addAll(SqlppVariableUtil.getFreeVariables(limitClause));
            }
        }
        // Visits the select clause.
        SelectClause selectClause = selectBlock.getSelectClause();
        selectClause.accept(this, arg);
        // Rewrites the select clause.
        SqlppRewriteUtil.rewriteExpressionUsingGroupVariable(groupVar, groupFieldVars, selectClause, context);
        freeVariables.addAll(SqlppVariableUtil.getFreeVariables(selectClause));
        freeVariables.removeAll(visibleVarsInCurrentScope);
        // Gets the final free variables.
        freeVariables.addAll(freeVariablesInGbyLets);
        // Gets outer scope variables.
        Collection<VariableExpr> decorVars = SqlppVariableUtil.getLiveVariables(scopeChecker.getCurrentScope(), true);
        decorVars.removeAll(visibleVarsInCurrentScope);
        // Need path resolution or not?
        boolean needResolution = !decorVars.containsAll(freeVariables);
        // Otherwise, we only need to retain used free variables.
        if (needResolution) {
            // Tracks used variables, including WITH variables.
            decorVars.retainAll(freeVariables);
            // Adds all non-WITH outer scope variables, for path resolution.
            Collection<VariableExpr> visibleOuterScopeNonWithVars = SqlppVariableUtil.getLiveVariables(scopeChecker.getCurrentScope(), false);
            visibleOuterScopeNonWithVars.removeAll(visibleVarsInCurrentScope);
            decorVars.addAll(visibleOuterScopeNonWithVars);
        } else {
            // Only retains used free variables.
            decorVars.retainAll(freeVariables);
        }
        if (!decorVars.isEmpty()) {
            // Adds used WITH variables.
            Collection<VariableExpr> visibleOuterScopeNonWithVars = SqlppVariableUtil.getLiveVariables(scopeChecker.getCurrentScope(), false);
            visibleOuterScopeNonWithVars.retainAll(freeVariables);
            decorVars.addAll(visibleOuterScopeNonWithVars);
            // Adds necessary decoration variables for the GROUP BY.
            // NOTE: we need to include WITH binding variables so as they can be evaluated before
            // the GROUP BY instead of being inlined as part of nested pipepline. The current optimzier
            // is not able to optimize the latter case. The following query is such an example:
            // asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dapd/q2-11
            List<GbyVariableExpressionPair> decorList = new ArrayList<>();
            for (VariableExpr var : decorVars) {
                decorList.add(new GbyVariableExpressionPair((VariableExpr) SqlppRewriteUtil.deepCopy(var), (Expression) SqlppRewriteUtil.deepCopy(var)));
            }
            groupbyClause.getDecorPairList().addAll(decorList);
        }
    } else {
        selectBlock.getSelectClause().accept(this, arg);
    }
    return null;
}
Also used : LimitClause(org.apache.asterix.lang.common.clause.LimitClause) SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) LetClause(org.apache.asterix.lang.common.clause.LetClause) ArrayList(java.util.ArrayList) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) HavingClause(org.apache.asterix.lang.sqlpp.clause.HavingClause) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) OrderbyClause(org.apache.asterix.lang.common.clause.OrderbyClause) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) HashSet(java.util.HashSet)

Example 7 with SelectExpression

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

the class SubstituteGroupbyExpressionVisitor method visit.

@Override
public Expression visit(SelectBlock selectBlock, ILangExpression arg) throws CompilationException {
    if (selectBlock.hasGroupbyClause()) {
        Map<Expression, Expression> map = new HashMap<>();
        for (GbyVariableExpressionPair gbyKeyPair : selectBlock.getGroupbyClause().getGbyPairList()) {
            Expression gbyKeyExpr = gbyKeyPair.getExpr();
            if (gbyKeyExpr.getKind() != Kind.VARIABLE_EXPRESSION) {
                map.put(gbyKeyExpr, gbyKeyPair.getVar());
            }
        }
        // Creates a substitution visitor.
        SubstituteGroupbyExpressionVisitor visitor = new SubstituteGroupbyExpressionVisitor(context, map);
        // Rewrites LET/HAVING/SELECT clauses.
        if (selectBlock.hasLetClausesAfterGroupby()) {
            for (LetClause letClause : selectBlock.getLetListAfterGroupby()) {
                letClause.accept(this, arg);
            }
        }
        if (selectBlock.hasHavingClause()) {
            selectBlock.getHavingClause().accept(visitor, arg);
        }
        selectBlock.getSelectClause().accept(visitor, arg);
        SelectExpression selectExpression = (SelectExpression) arg;
        // For SET operation queries, the GROUP BY key variables 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 : 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) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression)

Example 8 with SelectExpression

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

the class SetOperationVisitor method visit.

@Override
public Expression visit(SelectExpression selectExpression, ILangExpression arg) throws CompilationException {
    // Recursively visit nested select expressions.
    SelectSetOperation selectSetOperation = selectExpression.getSelectSetOperation();
    if (!selectSetOperation.hasRightInputs() || !(selectExpression.hasOrderby() || selectExpression.hasLimit())) {
        return super.visit(selectExpression, arg);
    }
    OrderbyClause orderBy = selectExpression.getOrderbyClause();
    LimitClause limit = selectExpression.getLimitClause();
    // Wraps the set operation part with a subquery.
    SelectExpression nestedSelectExpression = new SelectExpression(null, selectSetOperation, null, null, true);
    // Binding variable for the subquery.
    VariableExpr newBindingVar = new VariableExpr(context.newVariable());
    FromTerm newFromTerm = new FromTerm(nestedSelectExpression, newBindingVar, null, null);
    FromClause newFromClause = new FromClause(new ArrayList<>(Collections.singletonList(newFromTerm)));
    SelectClause selectClause = new SelectClause(new SelectElement(newBindingVar), null, false);
    SelectBlock selectBlock = new SelectBlock(selectClause, newFromClause, null, null, null, null, null);
    SelectSetOperation newSelectSetOperation = new SelectSetOperation(new SetOperationInput(selectBlock, null), null);
    // Puts together the generated select-from-where query and order by/limit.
    SelectExpression newSelectExpression = new SelectExpression(selectExpression.getLetList(), newSelectSetOperation, orderBy, limit, selectExpression.isSubquery());
    return super.visit(newSelectExpression, arg);
}
Also used : LimitClause(org.apache.asterix.lang.common.clause.LimitClause) SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) SelectElement(org.apache.asterix.lang.sqlpp.clause.SelectElement) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) SetOperationInput(org.apache.asterix.lang.sqlpp.struct.SetOperationInput) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) OrderbyClause(org.apache.asterix.lang.common.clause.OrderbyClause) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm)

Example 9 with SelectExpression

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

the class SqlppExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(SelectSetOperation selectSetOperation, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    SetOperationInput leftInput = selectSetOperation.getLeftInput();
    if (!selectSetOperation.hasRightInputs()) {
        return leftInput.accept(this, tupSource);
    }
    List<ILangExpression> inputExprs = new ArrayList<>();
    inputExprs.add(leftInput.selectBlock() ? new SelectExpression(null, new SelectSetOperation(leftInput, null), null, null, true) : leftInput.getSubquery());
    for (SetOperationRight setOperationRight : selectSetOperation.getRightInputs()) {
        SetOpType setOpType = setOperationRight.getSetOpType();
        if (setOpType != SetOpType.UNION || setOperationRight.isSetSemantics()) {
            throw new CompilationException("Operation " + setOpType + (setOperationRight.isSetSemantics() ? " with set semantics" : "ALL") + " is not supported.");
        }
        SetOperationInput rightInput = setOperationRight.getSetOperationRightInput();
        inputExprs.add(rightInput.selectBlock() ? new SelectExpression(null, new SelectSetOperation(rightInput, null), null, null, true) : rightInput.getSubquery());
    }
    return translateUnionAllFromInputExprs(inputExprs, tupSource);
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) SetOperationInput(org.apache.asterix.lang.sqlpp.struct.SetOperationInput) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) SetOpType(org.apache.asterix.lang.sqlpp.optype.SetOpType) ArrayList(java.util.ArrayList) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) SetOperationRight(org.apache.asterix.lang.sqlpp.struct.SetOperationRight)

Example 10 with SelectExpression

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

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