Search in sources :

Example 36 with VariableExpr

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

the class CloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(QuantifiedExpression qe, VariableSubstitutionEnvironment env) throws CompilationException {
    List<QuantifiedPair> oldPairs = qe.getQuantifiedList();
    List<QuantifiedPair> newPairs = new ArrayList<>(oldPairs.size());
    VariableSubstitutionEnvironment newSubs = env;
    for (QuantifiedPair t : oldPairs) {
        VariableExpr newVar = generateNewVariable(context, t.getVarExpr());
        newSubs = VariableCloneAndSubstitutionUtil.eliminateSubstFromList(newVar, newSubs);
        Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = visitUnnesBindingExpression(t.getExpr(), newSubs);
        QuantifiedPair t2 = new QuantifiedPair(newVar, (Expression) p1.first);
        newPairs.add(t2);
    }
    Pair<ILangExpression, VariableSubstitutionEnvironment> p2 = qe.getSatisfiesExpr().accept(this, newSubs);
    QuantifiedExpression qe2 = new QuantifiedExpression(qe.getQuantifier(), newPairs, (Expression) p2.first);
    return new Pair<>(qe2, newSubs);
}
Also used : QuantifiedPair(org.apache.asterix.lang.common.struct.QuantifiedPair) VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) ArrayList(java.util.ArrayList) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) 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 37 with VariableExpr

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

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

the class FormatPrintVisitor method visit.

@Override
public Void visit(GroupbyClause gc, Integer step) throws CompilationException {
    if (gc.hasHashGroupByHint()) {
        out.println(skip(step) + "/* +hash */");
    }
    out.print(skip(step) + "group by ");
    printDelimitedGbyExpressions(gc.getGbyPairList(), step + 2);
    if (gc.hasDecorList()) {
        out.print(" decor ");
        printDelimitedGbyExpressions(gc.getDecorPairList(), step + 2);
    }
    if (gc.hasWithMap()) {
        out.print(" with ");
        Map<Expression, VariableExpr> withVarMap = gc.getWithVarMap();
        int index = 0;
        int size = withVarMap.size();
        for (Entry<Expression, VariableExpr> entry : withVarMap.entrySet()) {
            Expression key = entry.getKey();
            VariableExpr value = entry.getValue();
            key.accept(this, step + 2);
            if (!key.equals(value)) {
                out.print(" as ");
                value.accept(this, step + 2);
            }
            if (++index < size) {
                out.print(COMMA);
            }
        }
    }
    out.println();
    return null;
}
Also used : TypeReferenceExpression(org.apache.asterix.lang.common.expression.TypeReferenceExpression) Expression(org.apache.asterix.lang.common.base.Expression) TypeExpression(org.apache.asterix.lang.common.expression.TypeExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr)

Example 39 with VariableExpr

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

the class Scope method getLiveVariables.

public Set<VariableExpr> getLiveVariables() {
    Set<VariableExpr> vars = new HashSet<VariableExpr>();
    Iterator<Identifier> identifierIterator = liveSymbols();
    while (identifierIterator.hasNext()) {
        Identifier identifier = identifierIterator.next();
        if (identifier instanceof VarIdentifier) {
            vars.add(new VariableExpr((VarIdentifier) identifier));
        }
    }
    return vars;
}
Also used : VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) Identifier(org.apache.asterix.lang.common.struct.Identifier) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) HashSet(java.util.HashSet)

Example 40 with VariableExpr

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

Aggregations

VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)59 Expression (org.apache.asterix.lang.common.base.Expression)35 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)29 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)20 ArrayList (java.util.ArrayList)19 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)18 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)17 Pair (org.apache.hyracks.algebricks.common.utils.Pair)16 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)13 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)12 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)11 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)11 HashSet (java.util.HashSet)9 HashMap (java.util.HashMap)8 LetClause (org.apache.asterix.lang.common.clause.LetClause)8 Identifier (org.apache.asterix.lang.common.struct.Identifier)8 ForClause (org.apache.asterix.lang.aql.clause.ForClause)7 GroupbyClause (org.apache.asterix.lang.common.clause.GroupbyClause)7 FromTerm (org.apache.asterix.lang.sqlpp.clause.FromTerm)6 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)6