use of org.apache.asterix.lang.sqlpp.expression.SelectExpression 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.sqlpp.expression.SelectExpression 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);
}
}
use of org.apache.asterix.lang.sqlpp.expression.SelectExpression in project asterixdb by apache.
the class DeepCopyVisitor method visit.
@Override
public SelectExpression visit(SelectExpression selectExpression, Void arg) throws CompilationException {
List<LetClause> lets = new ArrayList<>();
SelectSetOperation select;
OrderbyClause orderby = null;
LimitClause limit = null;
// visit let list
if (selectExpression.hasLetClauses()) {
for (LetClause letClause : selectExpression.getLetList()) {
lets.add((LetClause) letClause.accept(this, arg));
}
}
// visit the main select.
select = (SelectSetOperation) selectExpression.getSelectSetOperation().accept(this, arg);
// visit order by
if (selectExpression.hasOrderby()) {
List<Expression> orderExprs = new ArrayList<>();
for (Expression orderExpr : selectExpression.getOrderbyClause().getOrderbyList()) {
orderExprs.add((Expression) orderExpr.accept(this, arg));
}
orderby = new OrderbyClause(orderExprs, selectExpression.getOrderbyClause().getModifierList());
}
// visit limit
if (selectExpression.hasLimit()) {
limit = (LimitClause) selectExpression.getLimitClause().accept(this, arg);
}
return new SelectExpression(lets, select, orderby, limit, selectExpression.isSubquery());
}
use of org.apache.asterix.lang.sqlpp.expression.SelectExpression in project asterixdb by apache.
the class FreeVariableVisitor method visit.
@Override
public Void visit(SelectExpression selectExpression, Collection<VariableExpr> freeVars) throws CompilationException {
Collection<VariableExpr> letsFreeVars = new HashSet<>();
Collection<VariableExpr> selectFreeVars = new HashSet<>();
visitLetClauses(selectExpression.getLetList(), letsFreeVars);
// visit order by
if (selectExpression.hasOrderby()) {
for (Expression orderExpr : selectExpression.getOrderbyClause().getOrderbyList()) {
orderExpr.accept(this, selectFreeVars);
}
}
// visit limit
if (selectExpression.hasLimit()) {
selectExpression.getLimitClause().accept(this, selectFreeVars);
}
// visit the main select
selectExpression.getSelectSetOperation().accept(this, selectFreeVars);
// Removed let binding variables.
selectFreeVars.removeAll(SqlppVariableUtil.getBindingVariables(selectExpression.getLetList()));
freeVars.addAll(letsFreeVars);
freeVars.addAll(selectFreeVars);
return null;
}
Aggregations