use of org.apache.asterix.lang.common.expression.GbyVariableExpressionPair 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.expression.GbyVariableExpressionPair in project asterixdb by apache.
the class AbstractInlineUdfsVisitor method visit.
@Override
public Boolean visit(GroupbyClause gc, List<FunctionDecl> arg) throws CompilationException {
boolean changed = false;
for (GbyVariableExpressionPair p : gc.getGbyPairList()) {
Pair<Boolean, Expression> be = inlineUdfsInExpr(p.getExpr(), arg);
p.setExpr(be.second);
if (be.first) {
changed = true;
}
}
for (GbyVariableExpressionPair p : gc.getDecorPairList()) {
Pair<Boolean, Expression> be = inlineUdfsInExpr(p.getExpr(), arg);
p.setExpr(be.second);
if (be.first) {
changed = true;
}
}
return changed;
}
use of org.apache.asterix.lang.common.expression.GbyVariableExpressionPair in project asterixdb by apache.
the class ClauseComparator method printDelimitedGbyExpressions.
@Override
protected void printDelimitedGbyExpressions(List<GbyVariableExpressionPair> gbyList, int step) throws CompilationException {
int gbySize = gbyList.size();
int gbyIndex = 0;
for (GbyVariableExpressionPair pair : gbyList) {
pair.getExpr().accept(this, step);
if (pair.getVar() != null) {
out.print(" as ");
pair.getVar().accept(this, step);
}
if (++gbyIndex < gbySize) {
out.print(COMMA);
}
}
}
use of org.apache.asterix.lang.common.expression.GbyVariableExpressionPair in project asterixdb by apache.
the class ClauseComparator method collectProducedVariablesFromGroupby.
// Collects produced variables from group-by.
private List<VariableExpr> collectProducedVariablesFromGroupby(GroupbyClause gbyClause) {
List<VariableExpr> producedVars = new ArrayList<VariableExpr>();
for (GbyVariableExpressionPair keyPair : gbyClause.getGbyPairList()) {
producedVars.add(keyPair.getVar());
}
for (GbyVariableExpressionPair keyPair : gbyClause.getDecorPairList()) {
producedVars.add(keyPair.getVar());
}
producedVars.addAll(gbyClause.getWithVarMap().values());
return producedVars;
}
use of org.apache.asterix.lang.common.expression.GbyVariableExpressionPair in project asterixdb by apache.
the class AbstractSqlppExpressionScopingVisitor method visit.
@Override
public Expression visit(GroupbyClause gc, ILangExpression arg) throws CompilationException {
// After a GROUP BY, variables defined before the current SELECT BLOCK (e.g., in a WITH clause
// or an outer scope query) should still be visible.
Scope newScope = new Scope(scopeChecker, scopeChecker.getCurrentScope().getParentScope());
// Puts all group-by variables into the symbol set of the new scope.
for (GbyVariableExpressionPair gbyKeyVarExpr : gc.getGbyPairList()) {
gbyKeyVarExpr.setExpr(visit(gbyKeyVarExpr.getExpr(), gc));
VariableExpr gbyKeyVar = gbyKeyVarExpr.getVar();
if (gbyKeyVar != null) {
addNewVarSymbolToScope(newScope, gbyKeyVar.getVar());
}
}
if (gc.hasGroupFieldList()) {
for (Pair<Expression, Identifier> gbyField : gc.getGroupFieldList()) {
gbyField.first = visit(gbyField.first, arg);
}
}
if (gc.hasDecorList()) {
for (GbyVariableExpressionPair decorVarExpr : gc.getDecorPairList()) {
decorVarExpr.setExpr(visit(decorVarExpr.getExpr(), gc));
VariableExpr decorVar = decorVarExpr.getVar();
if (decorVar != null) {
addNewVarSymbolToScope(newScope, decorVar.getVar());
}
}
}
if (gc.hasGroupVar()) {
addNewVarSymbolToScope(scopeChecker.getCurrentScope(), gc.getGroupVar().getVar());
}
if (gc.hasWithMap()) {
Map<Expression, VariableExpr> newWithMap = new HashMap<>();
for (Entry<Expression, VariableExpr> entry : gc.getWithVarMap().entrySet()) {
Expression expr = visit(entry.getKey(), arg);
Expression newKey = expr;
VariableExpr value = entry.getValue();
addNewVarSymbolToScope(newScope, value.getVar());
newWithMap.put(newKey, value);
}
gc.setWithVarMap(newWithMap);
}
// Replaces the current scope with the new scope.
scopeChecker.replaceCurrentScope(newScope);
return null;
}
Aggregations