use of org.apache.asterix.lang.common.clause.GroupbyClause 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);
}
use of org.apache.asterix.lang.common.clause.GroupbyClause in project asterixdb by apache.
the class CloneAndSubstituteVariablesVisitor method visit.
@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(GroupbyClause gc, VariableSubstitutionEnvironment env) throws CompilationException {
VariableSubstitutionEnvironment newSubs = env;
List<GbyVariableExpressionPair> newGbyList = VariableCloneAndSubstitutionUtil.substInVarExprPair(context, gc.getGbyPairList(), newSubs, this);
List<GbyVariableExpressionPair> newDecorList = gc.hasDecorList() ? VariableCloneAndSubstitutionUtil.substInVarExprPair(context, gc.getDecorPairList(), newSubs, this) : new ArrayList<>();
VariableExpr newGroupVar = null;
if (gc.hasGroupVar()) {
newGroupVar = generateNewVariable(context, gc.getGroupVar());
}
Map<Expression, VariableExpr> newWithMap = new HashMap<>();
if (gc.hasWithMap()) {
for (Entry<Expression, VariableExpr> entry : gc.getWithVarMap().entrySet()) {
Expression newKeyVar = (Expression) entry.getKey().accept(this, env).first;
VariableExpr newValueVar = generateNewVariable(context, entry.getValue());
newWithMap.put(newKeyVar, newValueVar);
}
}
List<Pair<Expression, Identifier>> newGroupFieldList = new ArrayList<>();
if (gc.hasGroupFieldList()) {
for (Pair<Expression, Identifier> varId : gc.getGroupFieldList()) {
Expression newExpr = (Expression) varId.first.accept(this, env).first;
newGroupFieldList.add(new Pair<>(newExpr, varId.second));
}
}
GroupbyClause newGroup = new GroupbyClause(newGbyList, newDecorList, newWithMap, newGroupVar, newGroupFieldList, gc.hasHashGroupByHint(), gc.isGroupAll());
return new Pair<>(newGroup, newSubs);
}
use of org.apache.asterix.lang.common.clause.GroupbyClause 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.GroupbyClause in project asterixdb by apache.
the class ClauseComparator method extractUnnestAfterGroupby.
// Extracts all clauses that led by a "for" clause after the first group-by
// clause in the input clause list.
// Those extracted clauses will be removed from the input clause list.
/**
* @param clauseList
* , a list of clauses
* @return the cutting group-by clause and the list of extracted clauses.
* @throws CompilationException
*/
private Pair<GroupbyClause, List<Clause>> extractUnnestAfterGroupby(List<Clause> clauseList) throws CompilationException {
List<Clause> nestedClauses = new ArrayList<Clause>();
GroupbyClause cuttingGbyClause = null;
boolean meetGroupBy = false;
boolean nestedClauseStarted = false;
for (Clause cl : clauseList) {
if (cl.getClauseType() == ClauseType.GROUP_BY_CLAUSE) {
meetGroupBy = true;
cuttingGbyClause = (GroupbyClause) cl;
continue;
}
if (meetGroupBy && cl.getClauseType() == ClauseType.FOR_CLAUSE) {
nestedClauseStarted = true;
}
if (nestedClauseStarted) {
nestedClauses.add(cl);
}
}
clauseList.removeAll(nestedClauses);
return new Pair<GroupbyClause, List<Clause>>(cuttingGbyClause, nestedClauses);
}
use of org.apache.asterix.lang.common.clause.GroupbyClause 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;
}
Aggregations