Search in sources :

Example 1 with GroupbyClause

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

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

the class ClauseComparator method visit.

@Override
public Void visit(FLWOGRExpression flwor, Integer step) throws CompilationException {
    if (step != 0) {
        out.println("(");
    }
    List<Clause> clauseList = new ArrayList<Clause>();
    clauseList.addAll(flwor.getClauseList());
    // Processes data-independent let clauses.
    if (hasFor(clauseList)) {
        processLeadingLetClauses(step, clauseList);
    }
    // Distill unnecessary order-bys before a group-by.
    distillRedundantOrderby(clauseList);
    // Correlated "for" clauses after group-by.
    Pair<GroupbyClause, List<Clause>> extraction = extractUnnestAfterGroupby(clauseList);
    GroupbyClause cuttingGbyClause = extraction.first;
    List<Clause> unnestClauseList = extraction.second;
    Expression returnExpr = flwor.getReturnExpr();
    if (unnestClauseList.size() == 0) {
        if (hasFor(clauseList)) {
            out.print(skip(step) + "select element ");
            returnExpr.accept(this, step + 2);
            out.println();
        } else {
            // The FLOWGR only contains let-return, then inline let binding expressions into the return expression.
            Map<VariableExpr, Expression> varExprMap = extractLetBindingVariables(clauseList, cuttingGbyClause);
            returnExpr = (Expression) AQLVariableSubstitutionUtil.substituteVariable(returnExpr, varExprMap);
            returnExpr.accept(this, step);
            return null;
        }
    }
    String generated = generateVariableSymbol();
    if (unnestClauseList.size() > 0) {
        Map<VariableExpr, Expression> varExprMap = extractDefinedCollectionVariables(clauseList, cuttingGbyClause, generated);
        returnExpr = (Expression) AQLVariableSubstitutionUtil.substituteVariable(returnExpr, varExprMap);
        List<Clause> newUnnestClauses = new ArrayList<Clause>();
        for (Clause nestedCl : unnestClauseList) {
            newUnnestClauses.add((Clause) AQLVariableSubstitutionUtil.substituteVariable(nestedCl, varExprMap));
        }
        unnestClauseList = newUnnestClauses;
        out.print(skip(step) + "select element " + (hasDistinct(unnestClauseList) ? "distinct " : ""));
        returnExpr.accept(this, step + 2);
        out.println();
        out.println(skip(step) + "from");
        out.print(skip(step + 2) + "( select element " + (hasDistinct(clauseList) ? "distinct " : "") + "{");
        int index = 0;
        int size = varExprMap.size();
        for (VariableExpr var : varExprMap.keySet()) {
            out.print("\'" + var.getVar().getValue().substring(1) + "\':" + var.getVar().getValue().substring(1));
            if (++index < size) {
                out.print(COMMA);
            }
        }
        out.println("}");
    }
    reorder(clauseList);
    reorder(unnestClauseList);
    mergeConsecutiveWhereClauses(clauseList);
    mergeConsecutiveWhereClauses(unnestClauseList);
    boolean firstFor = true;
    boolean firstLet = true;
    int forStep = unnestClauseList.size() == 0 ? step : step + 3;
    int size = clauseList.size();
    // "for"s.
    for (int i = 0; i < size; ++i) {
        Clause cl = clauseList.get(i);
        if (cl.getClauseType() == ClauseType.FOR_CLAUSE) {
            boolean hasConsequentFor = false;
            if (i < size - 1) {
                Clause nextCl = clauseList.get(i + 1);
                hasConsequentFor = nextCl.getClauseType() == ClauseType.FOR_CLAUSE;
            }
            visitForClause((ForClause) cl, forStep, firstFor, hasConsequentFor);
            firstFor = false;
        } else if (cl.getClauseType() == ClauseType.LET_CLAUSE) {
            boolean hasConsequentLet = false;
            if (i < size - 1) {
                Clause nextCl = clauseList.get(i + 1);
                hasConsequentLet = nextCl.getClauseType() == ClauseType.LET_CLAUSE;
            }
            visitLetClause((LetClause) cl, forStep, firstLet, hasConsequentLet);
            firstLet = false;
        } else {
            cl.accept(this, forStep);
        }
        if (cl.getClauseType() == ClauseType.FROM_CLAUSE || cl.getClauseType() == ClauseType.GROUP_BY_CLAUSE) {
            firstLet = true;
        }
    }
    if (unnestClauseList.size() > 0) {
        out.println(skip(forStep - 1) + ") as " + generated.substring(1) + ",");
        for (Clause nestedCl : unnestClauseList) {
            if (nestedCl.getClauseType() == ClauseType.FOR_CLAUSE) {
                visitForClause((ForClause) nestedCl, step - 1, firstFor, false);
            } else {
                nestedCl.accept(this, step);
            }
        }
    }
    if (step > 0) {
        out.print(skip(step - 2) + ")");
    }
    return null;
}
Also used : LetClause(org.apache.asterix.lang.common.clause.LetClause) ArrayList(java.util.ArrayList) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Expression(org.apache.asterix.lang.common.base.Expression) ArrayList(java.util.ArrayList) List(java.util.List) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ForClause(org.apache.asterix.lang.aql.clause.ForClause) DistinctClause(org.apache.asterix.lang.aql.clause.DistinctClause) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) LetClause(org.apache.asterix.lang.common.clause.LetClause) Clause(org.apache.asterix.lang.common.base.Clause) WhereClause(org.apache.asterix.lang.common.clause.WhereClause)

Example 3 with GroupbyClause

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

the class DeepCopyVisitor method visit.

@Override
public GroupbyClause visit(GroupbyClause gc, Void arg) throws CompilationException {
    List<GbyVariableExpressionPair> gbyPairList = new ArrayList<>();
    List<GbyVariableExpressionPair> decorPairList = new ArrayList<>();
    Map<Expression, VariableExpr> withVarMap = new HashMap<>();
    VariableExpr groupVarExpr = null;
    List<Pair<Expression, Identifier>> groupFieldList = new ArrayList<>();
    for (GbyVariableExpressionPair gbyVarExpr : gc.getGbyPairList()) {
        VariableExpr var = gbyVarExpr.getVar();
        gbyPairList.add(new GbyVariableExpressionPair(var == null ? null : (VariableExpr) var.accept(this, arg), (Expression) gbyVarExpr.getExpr().accept(this, arg)));
    }
    for (GbyVariableExpressionPair gbyVarExpr : gc.getDecorPairList()) {
        VariableExpr var = gbyVarExpr.getVar();
        decorPairList.add(new GbyVariableExpressionPair(var == null ? null : (VariableExpr) var.accept(this, arg), (Expression) gbyVarExpr.getExpr().accept(this, arg)));
    }
    for (Entry<Expression, VariableExpr> entry : gc.getWithVarMap().entrySet()) {
        withVarMap.put((Expression) entry.getKey().accept(this, arg), (VariableExpr) entry.getValue().accept(this, arg));
    }
    if (gc.hasGroupVar()) {
        groupVarExpr = (VariableExpr) gc.getGroupVar().accept(this, arg);
    }
    for (Pair<Expression, Identifier> field : gc.getGroupFieldList()) {
        groupFieldList.add(new Pair<>((Expression) field.first.accept(this, arg), field.second));
    }
    return new GroupbyClause(gbyPairList, decorPairList, withVarMap, groupVarExpr, groupFieldList, gc.hasHashGroupByHint(), gc.isGroupAll());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Identifier(org.apache.asterix.lang.common.struct.Identifier) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) 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) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair) QuantifiedPair(org.apache.asterix.lang.common.struct.QuantifiedPair)

Example 4 with GroupbyClause

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

the class DeepCopyVisitor method visit.

@Override
public SelectBlock visit(SelectBlock selectBlock, Void arg) throws CompilationException {
    FromClause fromClause = null;
    List<LetClause> letClauses = new ArrayList<>();
    WhereClause whereClause = null;
    GroupbyClause gbyClause = null;
    List<LetClause> gbyLetClauses = new ArrayList<>();
    HavingClause havingClause = null;
    SelectClause selectCluase;
    // "group by", "let"s, "having" and "select".
    if (selectBlock.hasFromClause()) {
        fromClause = (FromClause) selectBlock.getFromClause().accept(this, arg);
    }
    if (selectBlock.hasLetClauses()) {
        List<LetClause> letList = selectBlock.getLetList();
        for (LetClause letClause : letList) {
            letClauses.add((LetClause) letClause.accept(this, arg));
        }
    }
    if (selectBlock.hasWhereClause()) {
        whereClause = (WhereClause) selectBlock.getWhereClause().accept(this, arg);
    }
    if (selectBlock.hasGroupbyClause()) {
        gbyClause = (GroupbyClause) selectBlock.getGroupbyClause().accept(this, arg);
    }
    if (selectBlock.hasLetClausesAfterGroupby()) {
        List<LetClause> letListAfterGby = selectBlock.getLetListAfterGroupby();
        for (LetClause letClauseAfterGby : letListAfterGby) {
            gbyLetClauses.add((LetClause) letClauseAfterGby.accept(this, arg));
        }
    }
    if (selectBlock.hasHavingClause()) {
        havingClause = (HavingClause) selectBlock.getHavingClause().accept(this, arg);
    }
    selectCluase = (SelectClause) selectBlock.getSelectClause().accept(this, arg);
    return new SelectBlock(selectCluase, fromClause, letClauses, whereClause, gbyClause, gbyLetClauses, havingClause);
}
Also used : SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) LetClause(org.apache.asterix.lang.common.clause.LetClause) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) HavingClause(org.apache.asterix.lang.sqlpp.clause.HavingClause) ArrayList(java.util.ArrayList) WhereClause(org.apache.asterix.lang.common.clause.WhereClause)

Example 5 with GroupbyClause

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

the class SqlppGlobalAggregationSugarVisitor method visit.

@Override
public Expression visit(SelectBlock selectBlock, ILangExpression arg) throws CompilationException {
    SelectClause selectClause = selectBlock.getSelectClause();
    if (!selectBlock.hasGroupbyClause() && selectBlock.hasFromClause()) {
        boolean addImplicitGby;
        if (selectClause.selectRegular()) {
            addImplicitGby = isSql92Aggregate(selectClause.getSelectRegular(), selectBlock);
        } else {
            addImplicitGby = isSql92Aggregate(selectClause.getSelectElement(), selectBlock);
        }
        if (addImplicitGby) {
            // Adds an implicit group-by clause for SQL-92 global aggregate.
            List<GbyVariableExpressionPair> gbyPairList = new ArrayList<>();
            List<GbyVariableExpressionPair> decorPairList = new ArrayList<>();
            GroupbyClause gbyClause = new GroupbyClause(gbyPairList, decorPairList, new HashMap<>(), null, null, false, true);
            selectBlock.setGroupbyClause(gbyClause);
        }
    }
    return super.visit(selectBlock, arg);
}
Also used : SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) ArrayList(java.util.ArrayList)

Aggregations

GroupbyClause (org.apache.asterix.lang.common.clause.GroupbyClause)10 ArrayList (java.util.ArrayList)8 LetClause (org.apache.asterix.lang.common.clause.LetClause)7 Expression (org.apache.asterix.lang.common.base.Expression)6 WhereClause (org.apache.asterix.lang.common.clause.WhereClause)6 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)6 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)5 HashMap (java.util.HashMap)4 DistinctClause (org.apache.asterix.lang.aql.clause.DistinctClause)4 ForClause (org.apache.asterix.lang.aql.clause.ForClause)4 Clause (org.apache.asterix.lang.common.base.Clause)4 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)4 SelectClause (org.apache.asterix.lang.sqlpp.clause.SelectClause)4 Pair (org.apache.hyracks.algebricks.common.utils.Pair)4 FLWOGRExpression (org.apache.asterix.lang.aql.expression.FLWOGRExpression)3 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)3 FromClause (org.apache.asterix.lang.sqlpp.clause.FromClause)3 HavingClause (org.apache.asterix.lang.sqlpp.clause.HavingClause)3 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)2 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)2