use of org.apache.asterix.lang.common.base.ILangExpression in project asterixdb by apache.
the class SubstituteGroupbyExpressionVisitor method visit.
@Override
public Expression visit(SelectBlock selectBlock, ILangExpression arg) throws CompilationException {
if (selectBlock.hasGroupbyClause()) {
Map<Expression, Expression> map = new HashMap<>();
for (GbyVariableExpressionPair gbyKeyPair : selectBlock.getGroupbyClause().getGbyPairList()) {
Expression gbyKeyExpr = gbyKeyPair.getExpr();
if (gbyKeyExpr.getKind() != Kind.VARIABLE_EXPRESSION) {
map.put(gbyKeyExpr, gbyKeyPair.getVar());
}
}
// Creates a substitution visitor.
SubstituteGroupbyExpressionVisitor visitor = new SubstituteGroupbyExpressionVisitor(context, map);
// Rewrites LET/HAVING/SELECT clauses.
if (selectBlock.hasLetClausesAfterGroupby()) {
for (LetClause letClause : selectBlock.getLetListAfterGroupby()) {
letClause.accept(this, arg);
}
}
if (selectBlock.hasHavingClause()) {
selectBlock.getHavingClause().accept(visitor, arg);
}
selectBlock.getSelectClause().accept(visitor, arg);
SelectExpression selectExpression = (SelectExpression) arg;
// For SET operation queries, the GROUP BY key variables will not substitute ORDER BY nor LIMIT expressions.
if (!selectExpression.getSelectSetOperation().hasRightInputs()) {
if (selectExpression.hasOrderby()) {
selectExpression.getOrderbyClause().accept(visitor, arg);
}
if (selectExpression.hasLimit()) {
selectExpression.getLimitClause().accept(visitor, arg);
}
}
}
return super.visit(selectBlock, arg);
}
use of org.apache.asterix.lang.common.base.ILangExpression in project asterixdb by apache.
the class SqlppAstPrintUtil method toString.
/**
* @param exprs
* a list of language expression.
* @return an AST of the input language expressions.
* @throws CompilationException
*/
public static String toString(List<ILangExpression> exprs) throws CompilationException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintWriter output = new PrintWriter(bos);
QueryPrintVisitor visitor = astPrintVisitorFactory.createLangVisitor(output);
for (ILangExpression expr : exprs) {
expr.accept(visitor, 0);
}
output.close();
return bos.toString();
}
use of org.apache.asterix.lang.common.base.ILangExpression 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.base.ILangExpression 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);
}
}
use of org.apache.asterix.lang.common.base.ILangExpression in project asterixdb by apache.
the class AQLCloneAndSubstituteVariablesVisitor method visit.
@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(ForClause fc, VariableSubstitutionEnvironment env) throws CompilationException {
Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = fc.getInExpr().accept(this, env);
VariableExpr varExpr = fc.getVarExpr();
VariableExpr newVe = generateNewVariable(context, varExpr);
VariableSubstitutionEnvironment resultEnv = new VariableSubstitutionEnvironment(env);
resultEnv.removeSubstitution(varExpr);
VariableExpr newPosVarExpr = null;
if (fc.hasPosVar()) {
VariableExpr posVarExpr = fc.getPosVarExpr();
newPosVarExpr = generateNewVariable(context, posVarExpr);
resultEnv.removeSubstitution(posVarExpr);
}
ForClause newFor = new ForClause(newVe, (Expression) p1.first, newPosVarExpr);
return new Pair<ILangExpression, VariableSubstitutionEnvironment>(newFor, resultEnv);
}
Aggregations