use of org.apache.asterix.lang.sqlpp.clause.HavingClause in project asterixdb by apache.
the class SqlppGroupByVisitor method visit.
@Override
public Expression visit(SelectBlock selectBlock, ILangExpression arg) throws CompilationException {
// Traverses the select block in the order of "from", "let"s, "where",
// "group by", "let"s, "having" and "select".
FromClause fromClause = selectBlock.getFromClause();
if (selectBlock.hasFromClause()) {
fromClause.accept(this, arg);
}
if (selectBlock.hasLetClauses()) {
List<LetClause> letList = selectBlock.getLetList();
for (LetClause letClause : letList) {
letClause.accept(this, arg);
}
}
if (selectBlock.hasWhereClause()) {
selectBlock.getWhereClause().accept(this, arg);
}
if (selectBlock.hasGroupbyClause()) {
GroupbyClause groupbyClause = selectBlock.getGroupbyClause();
groupbyClause.accept(this, fromClause);
Collection<VariableExpr> visibleVarsInCurrentScope = SqlppVariableUtil.getBindingVariables(groupbyClause);
VariableExpr groupVar = groupbyClause.getGroupVar();
Set<VariableExpr> groupFieldVars = getGroupFieldVariables(groupbyClause);
Collection<VariableExpr> freeVariablesInGbyLets = new HashSet<>();
if (selectBlock.hasLetClausesAfterGroupby()) {
List<LetClause> letListAfterGby = selectBlock.getLetListAfterGroupby();
for (LetClause letClauseAfterGby : letListAfterGby) {
letClauseAfterGby.accept(this, arg);
// Rewrites each let clause after the group-by.
SqlppRewriteUtil.rewriteExpressionUsingGroupVariable(groupVar, groupFieldVars, letClauseAfterGby, context);
Collection<VariableExpr> freeVariablesInLet = SqlppVariableUtil.getFreeVariables(letClauseAfterGby.getBindingExpr());
freeVariablesInLet.removeAll(visibleVarsInCurrentScope);
freeVariablesInGbyLets.addAll(freeVariablesInLet);
visibleVarsInCurrentScope.add(letClauseAfterGby.getVarExpr());
}
}
Collection<VariableExpr> freeVariables = new HashSet<>();
if (selectBlock.hasHavingClause()) {
// Rewrites the having clause.
HavingClause havingClause = selectBlock.getHavingClause();
havingClause.accept(this, arg);
SqlppRewriteUtil.rewriteExpressionUsingGroupVariable(groupVar, groupFieldVars, havingClause, context);
freeVariables.addAll(SqlppVariableUtil.getFreeVariables(havingClause));
}
SelectExpression parentSelectExpression = (SelectExpression) arg;
// We cannot rewrite ORDER BY and LIMIT if it's a SET operation query.
if (!parentSelectExpression.getSelectSetOperation().hasRightInputs()) {
if (parentSelectExpression.hasOrderby()) {
// Rewrites the ORDER BY clause.
OrderbyClause orderbyClause = parentSelectExpression.getOrderbyClause();
orderbyClause.accept(this, arg);
SqlppRewriteUtil.rewriteExpressionUsingGroupVariable(groupVar, groupFieldVars, orderbyClause, context);
freeVariables.addAll(SqlppVariableUtil.getFreeVariables(orderbyClause));
}
if (parentSelectExpression.hasLimit()) {
// Rewrites the LIMIT clause.
LimitClause limitClause = parentSelectExpression.getLimitClause();
limitClause.accept(this, arg);
SqlppRewriteUtil.rewriteExpressionUsingGroupVariable(groupVar, groupFieldVars, limitClause, context);
freeVariables.addAll(SqlppVariableUtil.getFreeVariables(limitClause));
}
}
// Visits the select clause.
SelectClause selectClause = selectBlock.getSelectClause();
selectClause.accept(this, arg);
// Rewrites the select clause.
SqlppRewriteUtil.rewriteExpressionUsingGroupVariable(groupVar, groupFieldVars, selectClause, context);
freeVariables.addAll(SqlppVariableUtil.getFreeVariables(selectClause));
freeVariables.removeAll(visibleVarsInCurrentScope);
// Gets the final free variables.
freeVariables.addAll(freeVariablesInGbyLets);
// Gets outer scope variables.
Collection<VariableExpr> decorVars = SqlppVariableUtil.getLiveVariables(scopeChecker.getCurrentScope(), true);
decorVars.removeAll(visibleVarsInCurrentScope);
// Need path resolution or not?
boolean needResolution = !decorVars.containsAll(freeVariables);
// Otherwise, we only need to retain used free variables.
if (needResolution) {
// Tracks used variables, including WITH variables.
decorVars.retainAll(freeVariables);
// Adds all non-WITH outer scope variables, for path resolution.
Collection<VariableExpr> visibleOuterScopeNonWithVars = SqlppVariableUtil.getLiveVariables(scopeChecker.getCurrentScope(), false);
visibleOuterScopeNonWithVars.removeAll(visibleVarsInCurrentScope);
decorVars.addAll(visibleOuterScopeNonWithVars);
} else {
// Only retains used free variables.
decorVars.retainAll(freeVariables);
}
if (!decorVars.isEmpty()) {
// Adds used WITH variables.
Collection<VariableExpr> visibleOuterScopeNonWithVars = SqlppVariableUtil.getLiveVariables(scopeChecker.getCurrentScope(), false);
visibleOuterScopeNonWithVars.retainAll(freeVariables);
decorVars.addAll(visibleOuterScopeNonWithVars);
// Adds necessary decoration variables for the GROUP BY.
// NOTE: we need to include WITH binding variables so as they can be evaluated before
// the GROUP BY instead of being inlined as part of nested pipepline. The current optimzier
// is not able to optimize the latter case. The following query is such an example:
// asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dapd/q2-11
List<GbyVariableExpressionPair> decorList = new ArrayList<>();
for (VariableExpr var : decorVars) {
decorList.add(new GbyVariableExpressionPair((VariableExpr) SqlppRewriteUtil.deepCopy(var), (Expression) SqlppRewriteUtil.deepCopy(var)));
}
groupbyClause.getDecorPairList().addAll(decorList);
}
} else {
selectBlock.getSelectClause().accept(this, arg);
}
return null;
}
use of org.apache.asterix.lang.sqlpp.clause.HavingClause 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.sqlpp.clause.HavingClause in project asterixdb by apache.
the class DeepCopyVisitor method visit.
@Override
public SelectBlock visit(SelectBlock selectBlock, Void arg) throws CompilationException {
FromClause fromClause = null;
List<LetClause> letClauses = new ArrayList<>();
WhereClause whereClause = null;
GroupbyClause gbyClause = null;
List<LetClause> gbyLetClauses = new ArrayList<>();
HavingClause havingClause = null;
SelectClause selectCluase;
// "group by", "let"s, "having" and "select".
if (selectBlock.hasFromClause()) {
fromClause = (FromClause) selectBlock.getFromClause().accept(this, arg);
}
if (selectBlock.hasLetClauses()) {
List<LetClause> letList = selectBlock.getLetList();
for (LetClause letClause : letList) {
letClauses.add((LetClause) letClause.accept(this, arg));
}
}
if (selectBlock.hasWhereClause()) {
whereClause = (WhereClause) selectBlock.getWhereClause().accept(this, arg);
}
if (selectBlock.hasGroupbyClause()) {
gbyClause = (GroupbyClause) selectBlock.getGroupbyClause().accept(this, arg);
}
if (selectBlock.hasLetClausesAfterGroupby()) {
List<LetClause> letListAfterGby = selectBlock.getLetListAfterGroupby();
for (LetClause letClauseAfterGby : letListAfterGby) {
gbyLetClauses.add((LetClause) letClauseAfterGby.accept(this, arg));
}
}
if (selectBlock.hasHavingClause()) {
havingClause = (HavingClause) selectBlock.getHavingClause().accept(this, arg);
}
selectCluase = (SelectClause) selectBlock.getSelectClause().accept(this, arg);
return new SelectBlock(selectCluase, fromClause, letClauses, whereClause, gbyClause, gbyLetClauses, havingClause);
}
use of org.apache.asterix.lang.sqlpp.clause.HavingClause 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