use of org.apache.asterix.lang.common.clause.LetClause 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.common.clause.LetClause in project asterixdb by apache.
the class SqlppInlineUdfsVisitor method visit.
@Override
public Boolean visit(SelectBlock selectBlock, List<FunctionDecl> funcs) throws CompilationException {
boolean changed = false;
if (selectBlock.hasFromClause()) {
changed |= selectBlock.getFromClause().accept(this, funcs);
}
if (selectBlock.hasLetClauses()) {
for (LetClause letClause : selectBlock.getLetList()) {
changed |= letClause.accept(this, funcs);
}
}
if (selectBlock.hasWhereClause()) {
changed |= selectBlock.getWhereClause().accept(this, funcs);
}
if (selectBlock.hasGroupbyClause()) {
changed |= selectBlock.getGroupbyClause().accept(this, funcs);
}
if (selectBlock.hasLetClausesAfterGroupby()) {
for (LetClause letClause : selectBlock.getLetListAfterGroupby()) {
changed |= letClause.accept(this, funcs);
}
}
if (selectBlock.hasHavingClause()) {
changed |= selectBlock.getHavingClause().accept(this, funcs);
}
changed |= selectBlock.getSelectClause().accept(this, funcs);
return changed;
}
use of org.apache.asterix.lang.common.clause.LetClause in project asterixdb by apache.
the class SqlppInlineUdfsVisitor method extractLetBindingVariableExpressionMappings.
private Map<Expression, Expression> extractLetBindingVariableExpressionMappings(List<LetClause> letClauses) throws CompilationException {
Map<Expression, Expression> varExprMap = new HashMap<>();
for (LetClause lc : letClauses) {
// inline let variables one by one iteratively.
lc.setBindingExpr(SqlppRewriteUtil.substituteExpression(lc.getBindingExpr(), varExprMap, context));
varExprMap.put(lc.getVarExpr(), lc.getBindingExpr());
}
return varExprMap;
}
use of org.apache.asterix.lang.common.clause.LetClause 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.clause.LetClause 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);
}
}
Aggregations