Search in sources :

Example 11 with LetClause

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

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

the class ClauseComparator method processLeadingLetClauses.

// Processes leading "let"s in a FLWOGR.
private void processLeadingLetClauses(Integer step, List<Clause> clauseList) throws CompilationException {
    List<Clause> processedLetList = new ArrayList<Clause>();
    boolean firstLet = true;
    int size = clauseList.size();
    for (int i = 0; i < size; ++i) {
        Clause cl = clauseList.get(i);
        if (cl.getClauseType() != ClauseType.LET_CLAUSE) {
            break;
        }
        boolean hasConsequentLet = false;
        if (i < size - 1) {
            Clause nextCl = clauseList.get(i + 1);
            hasConsequentLet = nextCl.getClauseType() == ClauseType.LET_CLAUSE;
        }
        visitLetClause((LetClause) cl, step, firstLet, hasConsequentLet);
        firstLet = false;
        processedLetList.add(cl);
    }
    clauseList.removeAll(processedLetList);
}
Also used : 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)

Example 13 with LetClause

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

Example 14 with LetClause

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

the class AqlQueryRewriter method wrapInLets.

private void wrapInLets() {
    // it into a let clause.
    if (topStatement == null) {
        return;
    }
    Expression body = topStatement.getBody();
    if (body.getKind() != Kind.FLWOGR_EXPRESSION) {
        VarIdentifier var = context.newVariable();
        VariableExpr v = new VariableExpr(var);
        LetClause c1 = new LetClause(v, body);
        ArrayList<Clause> clauseList = new ArrayList<>(1);
        clauseList.add(c1);
        FLWOGRExpression newBody = new FLWOGRExpression(clauseList, new VariableExpr(var));
        topStatement.setBody(newBody);
    }
}
Also used : LetClause(org.apache.asterix.lang.common.clause.LetClause) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Expression(org.apache.asterix.lang.common.base.Expression) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) ArrayList(java.util.ArrayList) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) 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)

Example 15 with LetClause

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

Aggregations

LetClause (org.apache.asterix.lang.common.clause.LetClause)23 Expression (org.apache.asterix.lang.common.base.Expression)11 ArrayList (java.util.ArrayList)9 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)9 GroupbyClause (org.apache.asterix.lang.common.clause.GroupbyClause)8 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)8 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)7 WhereClause (org.apache.asterix.lang.common.clause.WhereClause)6 HashMap (java.util.HashMap)5 DistinctClause (org.apache.asterix.lang.aql.clause.DistinctClause)5 ForClause (org.apache.asterix.lang.aql.clause.ForClause)5 Clause (org.apache.asterix.lang.common.base.Clause)5 FLWOGRExpression (org.apache.asterix.lang.aql.expression.FLWOGRExpression)4 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)4 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)4 Pair (org.apache.hyracks.algebricks.common.utils.Pair)4 LimitClause (org.apache.asterix.lang.common.clause.LimitClause)3 OrderbyClause (org.apache.asterix.lang.common.clause.OrderbyClause)3 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)3 FromClause (org.apache.asterix.lang.sqlpp.clause.FromClause)3