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