Search in sources :

Example 1 with VariableSubstitutionEnvironment

use of org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment in project asterixdb by apache.

the class AQLVariableSubstitutionUtil method substituteVariable.

public static ILangExpression substituteVariable(ILangExpression expression, Map<VariableExpr, Expression> varExprMap) throws CompilationException {
    AQLCloneAndSubstituteVariablesVisitor visitor = new AQLCloneAndSubstituteVariablesVisitor(new LangRewritingContext(0));
    VariableSubstitutionEnvironment env = new VariableSubstitutionEnvironment(varExprMap);
    return expression.accept(visitor, env).first;
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) AQLCloneAndSubstituteVariablesVisitor(org.apache.asterix.lang.aql.visitor.AQLCloneAndSubstituteVariablesVisitor) LangRewritingContext(org.apache.asterix.lang.common.rewrites.LangRewritingContext)

Example 2 with VariableSubstitutionEnvironment

use of org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment in project asterixdb by apache.

the class SqlppCloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(NestClause nestClause, VariableSubstitutionEnvironment env) throws CompilationException {
    VariableExpr rightVar = nestClause.getRightVariable();
    VariableExpr newRightVar = generateNewVariable(context, rightVar);
    VariableExpr newRightPosVar = nestClause.hasPositionalVariable() ? generateNewVariable(context, nestClause.getPositionalVariable()) : null;
    // Visits the right expression.
    Expression rightExpr = (Expression) nestClause.getRightExpression().accept(this, env).first;
    // Visits the condition.
    VariableSubstitutionEnvironment currentEnv = new VariableSubstitutionEnvironment(env);
    currentEnv.removeSubstitution(newRightVar);
    if (newRightPosVar != null) {
        currentEnv.removeSubstitution(newRightPosVar);
    }
    // The condition can refer to the newRightVar and newRightPosVar.
    Expression conditionExpr = (Expression) nestClause.getConditionExpression().accept(this, currentEnv).first;
    NestClause newJoinClause = new NestClause(nestClause.getJoinType(), rightExpr, newRightVar, newRightPosVar, conditionExpr);
    return new Pair<>(newJoinClause, currentEnv);
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) NestClause(org.apache.asterix.lang.sqlpp.clause.NestClause) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 3 with VariableSubstitutionEnvironment

use of org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment in project asterixdb by apache.

the class SqlppCloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(FromClause fromClause, VariableSubstitutionEnvironment env) throws CompilationException {
    VariableSubstitutionEnvironment currentEnv = new VariableSubstitutionEnvironment(env);
    List<FromTerm> newFromTerms = new ArrayList<>();
    for (FromTerm fromTerm : fromClause.getFromTerms()) {
        Pair<ILangExpression, VariableSubstitutionEnvironment> p = fromTerm.accept(this, currentEnv);
        newFromTerms.add((FromTerm) p.first);
        // A right from term could be correlated from a left from term,
        // therefore we propagate the substitution environment.
        currentEnv = p.second;
    }
    return new Pair<>(new FromClause(newFromTerms), currentEnv);
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) ArrayList(java.util.ArrayList) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 4 with VariableSubstitutionEnvironment

use of org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment in project asterixdb by apache.

the class SqlppCloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(FromTerm fromTerm, VariableSubstitutionEnvironment env) throws CompilationException {
    VariableExpr leftVar = fromTerm.getLeftVariable();
    VariableExpr newLeftVar = generateNewVariable(context, leftVar);
    VariableExpr newLeftPosVar = fromTerm.hasPositionalVariable() ? generateNewVariable(context, fromTerm.getPositionalVariable()) : null;
    Expression newLeftExpr = (Expression) visitUnnesBindingExpression(fromTerm.getLeftExpression(), env).first;
    List<AbstractBinaryCorrelateClause> newCorrelateClauses = new ArrayList<>();
    VariableSubstitutionEnvironment currentEnv = new VariableSubstitutionEnvironment(env);
    currentEnv.removeSubstitution(newLeftVar);
    if (newLeftPosVar != null) {
        currentEnv.removeSubstitution(newLeftPosVar);
    }
    for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses()) {
        if (correlateClause.getClauseType() == ClauseType.UNNEST_CLAUSE) {
            // The right-hand-side of unnest could be correlated with the left side,
            // therefore we propagate the substitution environment of the left-side.
            Pair<ILangExpression, VariableSubstitutionEnvironment> p = correlateClause.accept(this, currentEnv);
            currentEnv = p.second;
            newCorrelateClauses.add((AbstractBinaryCorrelateClause) p.first);
        } else {
            // The right-hand-side of join and nest could not be correlated with the left side,
            // therefore we propagate the original substitution environment.
            newCorrelateClauses.add((AbstractBinaryCorrelateClause) correlateClause.accept(this, env).first);
            // Join binding variables should be removed for further traversal.
            currentEnv.removeSubstitution(correlateClause.getRightVariable());
            if (correlateClause.hasPositionalVariable()) {
                currentEnv.removeSubstitution(correlateClause.getPositionalVariable());
            }
        }
    }
    return new Pair<>(new FromTerm(newLeftExpr, newLeftVar, newLeftPosVar, newCorrelateClauses), currentEnv);
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) AbstractBinaryCorrelateClause(org.apache.asterix.lang.sqlpp.clause.AbstractBinaryCorrelateClause) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) ArrayList(java.util.ArrayList) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 5 with VariableSubstitutionEnvironment

use of org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment in project asterixdb by apache.

the class SqlppCloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(UnnestClause unnestClause, VariableSubstitutionEnvironment env) throws CompilationException {
    VariableExpr rightVar = unnestClause.getRightVariable();
    VariableExpr newRightVar = generateNewVariable(context, rightVar);
    VariableExpr newRightPosVar = unnestClause.hasPositionalVariable() ? generateNewVariable(context, unnestClause.getPositionalVariable()) : null;
    // Visits the right expression.
    Expression rightExpr = (Expression) visitUnnesBindingExpression(unnestClause.getRightExpression(), env).first;
    // Visits the condition.
    VariableSubstitutionEnvironment currentEnv = new VariableSubstitutionEnvironment(env);
    currentEnv.removeSubstitution(newRightVar);
    if (newRightPosVar != null) {
        currentEnv.removeSubstitution(newRightPosVar);
    }
    // The condition can refer to the newRightVar and newRightPosVar.
    UnnestClause newJoinClause = new UnnestClause(unnestClause.getJoinType(), rightExpr, newRightVar, newRightPosVar);
    return new Pair<>(newJoinClause, currentEnv);
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) UnnestClause(org.apache.asterix.lang.sqlpp.clause.UnnestClause) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Aggregations

VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)31 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)28 Pair (org.apache.hyracks.algebricks.common.utils.Pair)26 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)15 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)14 ArrayList (java.util.ArrayList)13 Expression (org.apache.asterix.lang.common.base.Expression)12 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)11 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)7 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)6 LetClause (org.apache.asterix.lang.common.clause.LetClause)4 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)4 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)3 ForClause (org.apache.asterix.lang.aql.clause.ForClause)2 GroupbyClause (org.apache.asterix.lang.common.clause.GroupbyClause)2 LimitClause (org.apache.asterix.lang.common.clause.LimitClause)2 WhereClause (org.apache.asterix.lang.common.clause.WhereClause)2 FunctionDecl (org.apache.asterix.lang.common.statement.FunctionDecl)2 FromClause (org.apache.asterix.lang.sqlpp.clause.FromClause)2 FromTerm (org.apache.asterix.lang.sqlpp.clause.FromTerm)2