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(SelectExpression selectExpression, VariableSubstitutionEnvironment env) throws CompilationException {
boolean subquery = selectExpression.isSubquery();
List<LetClause> newLetList = new ArrayList<>();
SelectSetOperation newSelectSetOperation;
OrderbyClause newOrderbyClause = null;
LimitClause newLimitClause = null;
VariableSubstitutionEnvironment currentEnv = env;
Pair<ILangExpression, VariableSubstitutionEnvironment> p;
if (selectExpression.hasLetClauses()) {
for (LetClause letClause : selectExpression.getLetList()) {
p = letClause.accept(this, currentEnv);
newLetList.add(letClause);
currentEnv = p.second;
}
}
p = selectExpression.getSelectSetOperation().accept(this, env);
newSelectSetOperation = (SelectSetOperation) p.first;
currentEnv = p.second;
if (selectExpression.hasOrderby()) {
p = selectExpression.getOrderbyClause().accept(this, currentEnv);
newOrderbyClause = (OrderbyClause) p.first;
currentEnv = p.second;
}
if (selectExpression.hasLimit()) {
p = selectExpression.getLimitClause().accept(this, currentEnv);
newLimitClause = (LimitClause) p.first;
currentEnv = p.second;
}
return new Pair<>(new SelectExpression(newLetList, newSelectSetOperation, newOrderbyClause, newLimitClause, subquery), currentEnv);
}
use of org.apache.asterix.lang.common.clause.LetClause in project asterixdb by apache.
the class SqlppFormatPrintVisitor method visit.
@Override
public Void visit(SelectExpression selectStatement, Integer step) throws CompilationException {
if (selectStatement.isSubquery()) {
out.print("(");
}
int selectStep = selectStatement.isSubquery() ? step + 2 : step;
if (selectStatement.hasLetClauses()) {
for (LetClause letClause : selectStatement.getLetList()) {
letClause.accept(this, selectStep);
}
}
selectStatement.getSelectSetOperation().accept(this, selectStep);
if (selectStatement.hasOrderby()) {
selectStatement.getOrderbyClause().accept(this, selectStep);
}
if (selectStatement.hasLimit()) {
selectStatement.getLimitClause().accept(this, selectStep);
}
if (selectStatement.isSubquery()) {
out.print(skip(step) + " )");
}
return null;
}
use of org.apache.asterix.lang.common.clause.LetClause in project asterixdb by apache.
the class AbstractSqlppExpressionScopingVisitor method visit.
@Override
public Expression visit(SelectExpression selectExpression, ILangExpression arg) throws CompilationException {
Scope scopeBeforeSelectExpression = scopeChecker.getCurrentScope();
scopeChecker.createNewScope();
// visit let list
if (selectExpression.hasLetClauses()) {
for (LetClause letClause : selectExpression.getLetList()) {
// Variables defined in WITH clauses are considered as named access instead of real variables.
letClause.getVarExpr().getVar().setNamedValueAccess(true);
letClause.accept(this, selectExpression);
}
scopeChecker.createNewScope();
}
// visit the main select.
selectExpression.getSelectSetOperation().accept(this, selectExpression);
// visit order by
if (selectExpression.hasOrderby()) {
selectExpression.getOrderbyClause().accept(this, selectExpression);
}
// visit limit
if (selectExpression.hasLimit()) {
selectExpression.getLimitClause().accept(this, selectExpression);
}
// Exit scopes that were entered within this select expression
while (scopeChecker.getCurrentScope() != scopeBeforeSelectExpression) {
scopeChecker.removeCurrentScope();
}
return selectExpression;
}
use of org.apache.asterix.lang.common.clause.LetClause in project asterixdb by apache.
the class InlineColumnAliasVisitor method visit.
@Override
public Expression visit(SelectBlock selectBlock, ILangExpression arg) throws CompilationException {
// Gets the map from select clause.
Map<Expression, Expression> map = getMap(selectBlock.getSelectClause());
// Removes all FROM/LET binding variables
if (selectBlock.hasFromClause()) {
map.keySet().removeAll(SqlppVariableUtil.getBindingVariables(selectBlock.getFromClause()));
}
if (selectBlock.hasLetClauses()) {
map.keySet().removeAll(SqlppVariableUtil.getBindingVariables(selectBlock.getLetList()));
}
// Creates a substitution visitor.
SqlppSubstituteExpressionVisitor visitor = new SqlppSubstituteExpressionVisitor(context, map);
// Rewrites GROUP BY/LET/HAVING clauses.
if (selectBlock.hasGroupbyClause()) {
selectBlock.getGroupbyClause().accept(visitor, arg);
}
if (selectBlock.hasLetClausesAfterGroupby()) {
for (LetClause letClause : selectBlock.getLetListAfterGroupby()) {
letClause.accept(visitor, arg);
}
}
if (selectBlock.hasHavingClause()) {
selectBlock.getHavingClause().accept(visitor, arg);
}
SelectExpression selectExpression = (SelectExpression) arg;
// For SET operation queries, column aliases 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 InlineWithExpressionVisitor method visit.
@Override
public Expression visit(SelectExpression selectExpression, ILangExpression arg) throws CompilationException {
if (selectExpression.hasLetClauses()) {
// Inlines the leading WITH list.
Map<Expression, Expression> varExprMap = new HashMap<>();
List<LetClause> withs = selectExpression.getLetList();
Iterator<LetClause> with = withs.iterator();
while (with.hasNext()) {
LetClause letClause = with.next();
// Replaces the let binding Expr.
Expression expr = letClause.getBindingExpr();
Expression newBindingExpr = SqlppRewriteUtil.substituteExpression(expr, varExprMap, context);
letClause.setBindingExpr(newBindingExpr);
// Performs the rewriting recursively in the newBindingExpr itself.
super.visit(newBindingExpr, arg);
// Removes the WITH entry and adds variable-expr mapping into the varExprMap.
with.remove();
Expression bindingExpr = letClause.getBindingExpr();
// Wraps the binding expression with IndependentSubquery, so that free identifier references
// in the binding expression will not be resolved use outer-scope variables.
varExprMap.put(letClause.getVarExpr(), bindingExpr);
}
// Inlines WITH expressions into the select expression.
SelectExpression newSelectExpression = (SelectExpression) substituteExpression(selectExpression, varExprMap, context);
// Continues to visit the rewritten select expression.
return super.visit(newSelectExpression, arg);
} else {
// Continues to visit inside the select expression.
return super.visit(selectExpression, arg);
}
}
Aggregations