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(SelectSetOperation selectSetOperation, VariableSubstitutionEnvironment env) throws CompilationException {
SetOperationInput leftInput = selectSetOperation.getLeftInput();
SetOperationInput newLeftInput;
Pair<ILangExpression, VariableSubstitutionEnvironment> leftResult;
// Sets the left input.
if (leftInput.selectBlock()) {
leftResult = leftInput.getSelectBlock().accept(this, env);
newLeftInput = new SetOperationInput((SelectBlock) leftResult.first, null);
} else {
leftResult = leftInput.getSubquery().accept(this, env);
newLeftInput = new SetOperationInput(null, (SelectExpression) leftResult.first);
}
// Sets the right input
List<SetOperationRight> newRightInputs = new ArrayList<>();
if (selectSetOperation.hasRightInputs()) {
for (SetOperationRight right : selectSetOperation.getRightInputs()) {
SetOperationInput newRightInput;
SetOperationInput rightInput = right.getSetOperationRightInput();
if (rightInput.selectBlock()) {
Pair<ILangExpression, VariableSubstitutionEnvironment> rightResult = rightInput.getSelectBlock().accept(this, env);
newRightInput = new SetOperationInput((SelectBlock) rightResult.first, null);
} else {
Pair<ILangExpression, VariableSubstitutionEnvironment> rightResult = rightInput.getSubquery().accept(this, env);
newRightInput = new SetOperationInput(null, (SelectExpression) rightResult.first);
}
newRightInputs.add(new SetOperationRight(right.getSetOpType(), right.isSetSemantics(), newRightInput));
}
}
SelectSetOperation newSelectSetOperation = new SelectSetOperation(newLeftInput, newRightInputs);
return new Pair<>(newSelectSetOperation, selectSetOperation.hasRightInputs() ? env : leftResult.second);
}
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(HavingClause havingClause, VariableSubstitutionEnvironment env) throws CompilationException {
Pair<ILangExpression, VariableSubstitutionEnvironment> p = havingClause.getFilterExpression().accept(this, env);
HavingClause newHavingClause = new HavingClause((Expression) p.first);
return new Pair<>(newHavingClause, p.second);
}
use of org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment in project asterixdb by apache.
the class VariableCloneAndSubstitutionUtil method eliminateSubstFromList.
public static VariableSubstitutionEnvironment eliminateSubstFromList(VariableExpr variableExpr, VariableSubstitutionEnvironment arg) {
VariableSubstitutionEnvironment newArg = new VariableSubstitutionEnvironment(arg);
newArg.removeSubstitution(variableExpr);
return newArg;
}
use of org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment in project asterixdb by apache.
the class VariableCloneAndSubstitutionUtil method substInVarExprPair.
public static List<GbyVariableExpressionPair> substInVarExprPair(LangRewritingContext context, List<GbyVariableExpressionPair> gbyVeList, VariableSubstitutionEnvironment newSubs, CloneAndSubstituteVariablesVisitor visitor) throws CompilationException {
VariableSubstitutionEnvironment subs = newSubs;
List<GbyVariableExpressionPair> veList = new LinkedList<>();
for (GbyVariableExpressionPair vep : gbyVeList) {
VariableExpr oldGbyVar = vep.getVar();
VariableExpr newGbyVar = null;
if (oldGbyVar != null) {
newGbyVar = visitor.generateNewVariable(context, oldGbyVar);
subs = eliminateSubstFromList(newGbyVar, subs);
}
Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = vep.getExpr().accept(visitor, subs);
GbyVariableExpressionPair ve2 = new GbyVariableExpressionPair(newGbyVar, (Expression) p1.first);
veList.add(ve2);
}
return veList;
}
use of org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment in project asterixdb by apache.
the class AbstractInlineUdfsVisitor method inlineUdfsInExpr.
protected Pair<Boolean, Expression> inlineUdfsInExpr(Expression expr, List<FunctionDecl> arg) throws CompilationException {
if (expr.getKind() != Kind.CALL_EXPRESSION) {
boolean r = expr.accept(this, arg);
return new Pair<>(r, expr);
}
CallExpr f = (CallExpr) expr;
boolean r = expr.accept(this, arg);
FunctionDecl implem = findFuncDeclaration(f.getFunctionSignature(), arg);
if (implem == null) {
return new Pair<>(r, expr);
} else {
// Rewrite the function body itself (without setting unbounded variables to dataset access).
// TODO(buyingyi): throw an exception for recursive function definition or limit the stack depth.
implem.setFuncBody(rewriteFunctionBody(implem.getFuncBody()));
// it's one of the functions we want to inline
List<LetClause> clauses = new ArrayList<>();
Iterator<VarIdentifier> paramIter = implem.getParamList().iterator();
VariableSubstitutionEnvironment subts = new VariableSubstitutionEnvironment();
for (Expression e : f.getExprList()) {
VarIdentifier param = paramIter.next();
// variable inlining to take care of this.
if (e.getKind() == Kind.VARIABLE_EXPRESSION) {
subts.addSubstituion(new VariableExpr(param), e);
} else {
VarIdentifier newV = context.newVariable();
Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = e.accept(cloneVisitor, new VariableSubstitutionEnvironment());
LetClause c = new LetClause(new VariableExpr(newV), (Expression) p1.first);
clauses.add(c);
subts.addSubstituion(new VariableExpr(param), new VariableExpr(newV));
}
}
Pair<ILangExpression, VariableSubstitutionEnvironment> p2 = implem.getFuncBody().accept(cloneVisitor, subts);
Expression resExpr;
if (clauses.isEmpty()) {
resExpr = (Expression) p2.first;
} else {
resExpr = generateQueryExpression(clauses, (Expression) p2.first);
}
return new Pair<>(true, resExpr);
}
}
Aggregations