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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations