Search in sources :

Example 11 with GbyVariableExpressionPair

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

use of org.apache.asterix.lang.common.expression.GbyVariableExpressionPair in project asterixdb by apache.

the class AbstractInlineUdfsVisitor method visit.

@Override
public Boolean visit(GroupbyClause gc, List<FunctionDecl> arg) throws CompilationException {
    boolean changed = false;
    for (GbyVariableExpressionPair p : gc.getGbyPairList()) {
        Pair<Boolean, Expression> be = inlineUdfsInExpr(p.getExpr(), arg);
        p.setExpr(be.second);
        if (be.first) {
            changed = true;
        }
    }
    for (GbyVariableExpressionPair p : gc.getDecorPairList()) {
        Pair<Boolean, Expression> be = inlineUdfsInExpr(p.getExpr(), arg);
        p.setExpr(be.second);
        if (be.first) {
            changed = true;
        }
    }
    return changed;
}
Also used : 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)

Example 13 with GbyVariableExpressionPair

use of org.apache.asterix.lang.common.expression.GbyVariableExpressionPair in project asterixdb by apache.

the class ClauseComparator method printDelimitedGbyExpressions.

@Override
protected void printDelimitedGbyExpressions(List<GbyVariableExpressionPair> gbyList, int step) throws CompilationException {
    int gbySize = gbyList.size();
    int gbyIndex = 0;
    for (GbyVariableExpressionPair pair : gbyList) {
        pair.getExpr().accept(this, step);
        if (pair.getVar() != null) {
            out.print(" as ");
            pair.getVar().accept(this, step);
        }
        if (++gbyIndex < gbySize) {
            out.print(COMMA);
        }
    }
}
Also used : GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)

Example 14 with GbyVariableExpressionPair

use of org.apache.asterix.lang.common.expression.GbyVariableExpressionPair in project asterixdb by apache.

the class ClauseComparator method collectProducedVariablesFromGroupby.

// Collects produced variables from group-by.
private List<VariableExpr> collectProducedVariablesFromGroupby(GroupbyClause gbyClause) {
    List<VariableExpr> producedVars = new ArrayList<VariableExpr>();
    for (GbyVariableExpressionPair keyPair : gbyClause.getGbyPairList()) {
        producedVars.add(keyPair.getVar());
    }
    for (GbyVariableExpressionPair keyPair : gbyClause.getDecorPairList()) {
        producedVars.add(keyPair.getVar());
    }
    producedVars.addAll(gbyClause.getWithVarMap().values());
    return producedVars;
}
Also used : GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) ArrayList(java.util.ArrayList) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr)

Example 15 with GbyVariableExpressionPair

use of org.apache.asterix.lang.common.expression.GbyVariableExpressionPair in project asterixdb by apache.

the class AbstractSqlppExpressionScopingVisitor method visit.

@Override
public Expression visit(GroupbyClause gc, ILangExpression arg) throws CompilationException {
    // After a GROUP BY, variables defined before the current SELECT BLOCK (e.g., in a WITH clause
    // or an outer scope query) should still be visible.
    Scope newScope = new Scope(scopeChecker, scopeChecker.getCurrentScope().getParentScope());
    // Puts all group-by variables into the symbol set of the new scope.
    for (GbyVariableExpressionPair gbyKeyVarExpr : gc.getGbyPairList()) {
        gbyKeyVarExpr.setExpr(visit(gbyKeyVarExpr.getExpr(), gc));
        VariableExpr gbyKeyVar = gbyKeyVarExpr.getVar();
        if (gbyKeyVar != null) {
            addNewVarSymbolToScope(newScope, gbyKeyVar.getVar());
        }
    }
    if (gc.hasGroupFieldList()) {
        for (Pair<Expression, Identifier> gbyField : gc.getGroupFieldList()) {
            gbyField.first = visit(gbyField.first, arg);
        }
    }
    if (gc.hasDecorList()) {
        for (GbyVariableExpressionPair decorVarExpr : gc.getDecorPairList()) {
            decorVarExpr.setExpr(visit(decorVarExpr.getExpr(), gc));
            VariableExpr decorVar = decorVarExpr.getVar();
            if (decorVar != null) {
                addNewVarSymbolToScope(newScope, decorVar.getVar());
            }
        }
    }
    if (gc.hasGroupVar()) {
        addNewVarSymbolToScope(scopeChecker.getCurrentScope(), gc.getGroupVar().getVar());
    }
    if (gc.hasWithMap()) {
        Map<Expression, VariableExpr> newWithMap = new HashMap<>();
        for (Entry<Expression, VariableExpr> entry : gc.getWithVarMap().entrySet()) {
            Expression expr = visit(entry.getKey(), arg);
            Expression newKey = expr;
            VariableExpr value = entry.getValue();
            addNewVarSymbolToScope(newScope, value.getVar());
            newWithMap.put(newKey, value);
        }
        gc.setWithVarMap(newWithMap);
    }
    // Replaces the current scope with the new scope.
    scopeChecker.replaceCurrentScope(newScope);
    return null;
}
Also used : Identifier(org.apache.asterix.lang.common.struct.Identifier) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) Scope(org.apache.asterix.lang.common.context.Scope) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) HashMap(java.util.HashMap) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr)

Aggregations

GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)16 Expression (org.apache.asterix.lang.common.base.Expression)9 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)9 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)8 ArrayList (java.util.ArrayList)7 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)6 Identifier (org.apache.asterix.lang.common.struct.Identifier)5 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)5 HashMap (java.util.HashMap)4 GroupbyClause (org.apache.asterix.lang.common.clause.GroupbyClause)4 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)3 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)3 Pair (org.apache.hyracks.algebricks.common.utils.Pair)3 LetClause (org.apache.asterix.lang.common.clause.LetClause)2 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)2 SelectClause (org.apache.asterix.lang.sqlpp.clause.SelectClause)2 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)2 FunctionIdentifier (org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1