Search in sources :

Example 6 with GroupbyClause

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

the class SqlppCloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(SelectBlock selectBlock, VariableSubstitutionEnvironment env) throws CompilationException {
    Pair<ILangExpression, VariableSubstitutionEnvironment> newFrom = null;
    Pair<ILangExpression, VariableSubstitutionEnvironment> newLet;
    Pair<ILangExpression, VariableSubstitutionEnvironment> newWhere = null;
    Pair<ILangExpression, VariableSubstitutionEnvironment> newGroupby = null;
    Pair<ILangExpression, VariableSubstitutionEnvironment> newHaving = null;
    Pair<ILangExpression, VariableSubstitutionEnvironment> newSelect;
    List<LetClause> newLetClauses = new ArrayList<>();
    List<LetClause> newLetClausesAfterGby = new ArrayList<>();
    VariableSubstitutionEnvironment currentEnv = new VariableSubstitutionEnvironment(env);
    if (selectBlock.hasFromClause()) {
        newFrom = selectBlock.getFromClause().accept(this, currentEnv);
        currentEnv = newFrom.second;
    }
    if (selectBlock.hasLetClauses()) {
        for (LetClause letClause : selectBlock.getLetList()) {
            newLet = letClause.accept(this, currentEnv);
            currentEnv = newLet.second;
            newLetClauses.add(letClause);
        }
    }
    if (selectBlock.hasWhereClause()) {
        newWhere = selectBlock.getWhereClause().accept(this, currentEnv);
        currentEnv = newWhere.second;
    }
    if (selectBlock.hasGroupbyClause()) {
        newGroupby = selectBlock.getGroupbyClause().accept(this, currentEnv);
        currentEnv = newGroupby.second;
        if (selectBlock.hasLetClausesAfterGroupby()) {
            for (LetClause letClauseAfterGby : selectBlock.getLetListAfterGroupby()) {
                newLet = letClauseAfterGby.accept(this, currentEnv);
                currentEnv = newLet.second;
                newLetClausesAfterGby.add(letClauseAfterGby);
            }
        }
    }
    if (selectBlock.hasHavingClause()) {
        newHaving = selectBlock.getHavingClause().accept(this, currentEnv);
        currentEnv = newHaving.second;
    }
    newSelect = selectBlock.getSelectClause().accept(this, currentEnv);
    currentEnv = newSelect.second;
    FromClause fromClause = newFrom == null ? null : (FromClause) newFrom.first;
    WhereClause whereClause = newWhere == null ? null : (WhereClause) newWhere.first;
    GroupbyClause groupbyClause = newGroupby == null ? null : (GroupbyClause) newGroupby.first;
    HavingClause havingClause = newHaving == null ? null : (HavingClause) newHaving.first;
    return new Pair<>(new SelectBlock((SelectClause) newSelect.first, fromClause, newLetClauses, whereClause, groupbyClause, newLetClausesAfterGby, havingClause), currentEnv);
}
Also used : SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) LetClause(org.apache.asterix.lang.common.clause.LetClause) ArrayList(java.util.ArrayList) WhereClause(org.apache.asterix.lang.common.clause.WhereClause) VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) HavingClause(org.apache.asterix.lang.sqlpp.clause.HavingClause) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 7 with GroupbyClause

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

the class CloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(GroupbyClause gc, VariableSubstitutionEnvironment env) throws CompilationException {
    VariableSubstitutionEnvironment newSubs = env;
    List<GbyVariableExpressionPair> newGbyList = VariableCloneAndSubstitutionUtil.substInVarExprPair(context, gc.getGbyPairList(), newSubs, this);
    List<GbyVariableExpressionPair> newDecorList = gc.hasDecorList() ? VariableCloneAndSubstitutionUtil.substInVarExprPair(context, gc.getDecorPairList(), newSubs, this) : new ArrayList<>();
    VariableExpr newGroupVar = null;
    if (gc.hasGroupVar()) {
        newGroupVar = generateNewVariable(context, gc.getGroupVar());
    }
    Map<Expression, VariableExpr> newWithMap = new HashMap<>();
    if (gc.hasWithMap()) {
        for (Entry<Expression, VariableExpr> entry : gc.getWithVarMap().entrySet()) {
            Expression newKeyVar = (Expression) entry.getKey().accept(this, env).first;
            VariableExpr newValueVar = generateNewVariable(context, entry.getValue());
            newWithMap.put(newKeyVar, newValueVar);
        }
    }
    List<Pair<Expression, Identifier>> newGroupFieldList = new ArrayList<>();
    if (gc.hasGroupFieldList()) {
        for (Pair<Expression, Identifier> varId : gc.getGroupFieldList()) {
            Expression newExpr = (Expression) varId.first.accept(this, env).first;
            newGroupFieldList.add(new Pair<>(newExpr, varId.second));
        }
    }
    GroupbyClause newGroup = new GroupbyClause(newGbyList, newDecorList, newWithMap, newGroupVar, newGroupFieldList, gc.hasHashGroupByHint(), gc.isGroupAll());
    return new Pair<>(newGroup, newSubs);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) 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) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) Expression(org.apache.asterix.lang.common.base.Expression) 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 8 with GroupbyClause

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

the class ClauseComparator method extractLetBindingVariables.

// Extracts the variables to be substituted.
private Map<VariableExpr, Expression> extractLetBindingVariables(List<Clause> clauses, GroupbyClause cuttingGbyClause) throws CompilationException {
    Map<VariableExpr, Expression> varExprMap = new HashMap<VariableExpr, Expression>();
    int gbyIndex = clauses.indexOf(cuttingGbyClause);
    for (int i = gbyIndex + 1; i < clauses.size(); i++) {
        Clause cl = clauses.get(i);
        if (cl.getClauseType() == ClauseType.LET_CLAUSE) {
            LetClause letClause = (LetClause) cl;
            // inline let variables one by one iteratively.
            letClause.setBindingExpr((Expression) AQLVariableSubstitutionUtil.substituteVariable(letClause.getBindingExpr(), varExprMap));
            varExprMap.put(letClause.getVarExpr(), letClause.getBindingExpr());
        }
    }
    return varExprMap;
}
Also used : LetClause(org.apache.asterix.lang.common.clause.LetClause) HashMap(java.util.HashMap) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Expression(org.apache.asterix.lang.common.base.Expression) 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 9 with GroupbyClause

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

the class ClauseComparator method extractUnnestAfterGroupby.

// Extracts all clauses that led by a "for" clause after the first group-by
// clause in the input clause list.
// Those extracted clauses will be removed from the input clause list.
/**
     * @param clauseList
     *            , a list of clauses
     * @return the cutting group-by clause and the list of extracted clauses.
     * @throws CompilationException
     */
private Pair<GroupbyClause, List<Clause>> extractUnnestAfterGroupby(List<Clause> clauseList) throws CompilationException {
    List<Clause> nestedClauses = new ArrayList<Clause>();
    GroupbyClause cuttingGbyClause = null;
    boolean meetGroupBy = false;
    boolean nestedClauseStarted = false;
    for (Clause cl : clauseList) {
        if (cl.getClauseType() == ClauseType.GROUP_BY_CLAUSE) {
            meetGroupBy = true;
            cuttingGbyClause = (GroupbyClause) cl;
            continue;
        }
        if (meetGroupBy && cl.getClauseType() == ClauseType.FOR_CLAUSE) {
            nestedClauseStarted = true;
        }
        if (nestedClauseStarted) {
            nestedClauses.add(cl);
        }
    }
    clauseList.removeAll(nestedClauses);
    return new Pair<GroupbyClause, List<Clause>>(cuttingGbyClause, nestedClauses);
}
Also used : GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) ArrayList(java.util.ArrayList) 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) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 10 with GroupbyClause

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

the class ClauseComparator method extractDefinedCollectionVariables.

// Extracts the variables to be substituted with a path access.
private Map<VariableExpr, Expression> extractDefinedCollectionVariables(List<Clause> clauses, GroupbyClause cuttingGbyClause, String generatedAlias) {
    Map<VariableExpr, Expression> varExprMap = new HashMap<VariableExpr, Expression>();
    List<VariableExpr> varToSubstitute = collectProducedVariablesFromGroupby(cuttingGbyClause);
    int gbyIndex = clauses.indexOf(cuttingGbyClause);
    for (int i = gbyIndex + 1; i < clauses.size(); i++) {
        Clause cl = clauses.get(i);
        if (cl.getClauseType() == ClauseType.LET_CLAUSE) {
            varToSubstitute.add(((LetClause) cl).getVarExpr());
        }
    }
    for (VariableExpr var : varToSubstitute) {
        varExprMap.put(var, new FieldAccessor(new VariableExpr(new VarIdentifier(generatedAlias)), new VarIdentifier(var.getVar().getValue().substring(1))));
    }
    return varExprMap;
}
Also used : HashMap(java.util.HashMap) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Expression(org.apache.asterix.lang.common.base.Expression) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) 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) FieldAccessor(org.apache.asterix.lang.common.expression.FieldAccessor)

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