use of org.apache.asterix.lang.common.clause.LetClause in project asterixdb by apache.
the class ClauseComparator method visit.
@Override
public Void visit(FLWOGRExpression flwor, Integer step) throws CompilationException {
if (step != 0) {
out.println("(");
}
List<Clause> clauseList = new ArrayList<Clause>();
clauseList.addAll(flwor.getClauseList());
// Processes data-independent let clauses.
if (hasFor(clauseList)) {
processLeadingLetClauses(step, clauseList);
}
// Distill unnecessary order-bys before a group-by.
distillRedundantOrderby(clauseList);
// Correlated "for" clauses after group-by.
Pair<GroupbyClause, List<Clause>> extraction = extractUnnestAfterGroupby(clauseList);
GroupbyClause cuttingGbyClause = extraction.first;
List<Clause> unnestClauseList = extraction.second;
Expression returnExpr = flwor.getReturnExpr();
if (unnestClauseList.size() == 0) {
if (hasFor(clauseList)) {
out.print(skip(step) + "select element ");
returnExpr.accept(this, step + 2);
out.println();
} else {
// The FLOWGR only contains let-return, then inline let binding expressions into the return expression.
Map<VariableExpr, Expression> varExprMap = extractLetBindingVariables(clauseList, cuttingGbyClause);
returnExpr = (Expression) AQLVariableSubstitutionUtil.substituteVariable(returnExpr, varExprMap);
returnExpr.accept(this, step);
return null;
}
}
String generated = generateVariableSymbol();
if (unnestClauseList.size() > 0) {
Map<VariableExpr, Expression> varExprMap = extractDefinedCollectionVariables(clauseList, cuttingGbyClause, generated);
returnExpr = (Expression) AQLVariableSubstitutionUtil.substituteVariable(returnExpr, varExprMap);
List<Clause> newUnnestClauses = new ArrayList<Clause>();
for (Clause nestedCl : unnestClauseList) {
newUnnestClauses.add((Clause) AQLVariableSubstitutionUtil.substituteVariable(nestedCl, varExprMap));
}
unnestClauseList = newUnnestClauses;
out.print(skip(step) + "select element " + (hasDistinct(unnestClauseList) ? "distinct " : ""));
returnExpr.accept(this, step + 2);
out.println();
out.println(skip(step) + "from");
out.print(skip(step + 2) + "( select element " + (hasDistinct(clauseList) ? "distinct " : "") + "{");
int index = 0;
int size = varExprMap.size();
for (VariableExpr var : varExprMap.keySet()) {
out.print("\'" + var.getVar().getValue().substring(1) + "\':" + var.getVar().getValue().substring(1));
if (++index < size) {
out.print(COMMA);
}
}
out.println("}");
}
reorder(clauseList);
reorder(unnestClauseList);
mergeConsecutiveWhereClauses(clauseList);
mergeConsecutiveWhereClauses(unnestClauseList);
boolean firstFor = true;
boolean firstLet = true;
int forStep = unnestClauseList.size() == 0 ? step : step + 3;
int size = clauseList.size();
// "for"s.
for (int i = 0; i < size; ++i) {
Clause cl = clauseList.get(i);
if (cl.getClauseType() == ClauseType.FOR_CLAUSE) {
boolean hasConsequentFor = false;
if (i < size - 1) {
Clause nextCl = clauseList.get(i + 1);
hasConsequentFor = nextCl.getClauseType() == ClauseType.FOR_CLAUSE;
}
visitForClause((ForClause) cl, forStep, firstFor, hasConsequentFor);
firstFor = false;
} else if (cl.getClauseType() == ClauseType.LET_CLAUSE) {
boolean hasConsequentLet = false;
if (i < size - 1) {
Clause nextCl = clauseList.get(i + 1);
hasConsequentLet = nextCl.getClauseType() == ClauseType.LET_CLAUSE;
}
visitLetClause((LetClause) cl, forStep, firstLet, hasConsequentLet);
firstLet = false;
} else {
cl.accept(this, forStep);
}
if (cl.getClauseType() == ClauseType.FROM_CLAUSE || cl.getClauseType() == ClauseType.GROUP_BY_CLAUSE) {
firstLet = true;
}
}
if (unnestClauseList.size() > 0) {
out.println(skip(forStep - 1) + ") as " + generated.substring(1) + ",");
for (Clause nestedCl : unnestClauseList) {
if (nestedCl.getClauseType() == ClauseType.FOR_CLAUSE) {
visitForClause((ForClause) nestedCl, step - 1, firstFor, false);
} else {
nestedCl.accept(this, step);
}
}
}
if (step > 0) {
out.print(skip(step - 2) + ")");
}
return null;
}
use of org.apache.asterix.lang.common.clause.LetClause in project asterixdb by apache.
the class SqlppAstPrintVisitor method visit.
@Override
public Void visit(SelectExpression selectStatement, Integer step) throws CompilationException {
if (selectStatement.isSubquery()) {
out.println(skip(step) + "(");
}
int selectStep = selectStatement.isSubquery() ? step + 1 : 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.println(skip(step) + ")");
}
return null;
}
use of org.apache.asterix.lang.common.clause.LetClause in project asterixdb by apache.
the class FreeVariableVisitor method visitLetClauses.
private void visitLetClauses(List<LetClause> letClauses, Collection<VariableExpr> freeVars) throws CompilationException {
if (letClauses == null || letClauses.isEmpty()) {
return;
}
Collection<VariableExpr> bindingVars = new HashSet<>();
for (LetClause letClause : letClauses) {
Collection<VariableExpr> letFreeVars = new HashSet<>();
letClause.accept(this, letFreeVars);
// Removes previous binding variables.
letFreeVars.removeAll(bindingVars);
freeVars.addAll(letFreeVars);
// Adds let binding variables into the binding variable collection.
bindingVars.add(letClause.getVarExpr());
}
}
use of org.apache.asterix.lang.common.clause.LetClause in project asterixdb by apache.
the class SqlppExpressionToPlanTranslator method visit.
@Override
public Pair<ILogicalOperator, LogicalVariable> visit(SelectExpression selectExpression, Mutable<ILogicalOperator> tupSource) throws CompilationException {
if (selectExpression.isSubquery()) {
context.enterSubplan();
}
Mutable<ILogicalOperator> currentOpRef = tupSource;
if (selectExpression.hasLetClauses()) {
for (LetClause letClause : selectExpression.getLetList()) {
currentOpRef = new MutableObject<>(letClause.accept(this, currentOpRef).first);
}
}
Pair<ILogicalOperator, LogicalVariable> select = selectExpression.getSelectSetOperation().accept(this, currentOpRef);
currentOpRef = new MutableObject<>(select.first);
if (selectExpression.hasOrderby()) {
currentOpRef = new MutableObject<>(selectExpression.getOrderbyClause().accept(this, currentOpRef).first);
}
if (selectExpression.hasLimit()) {
currentOpRef = new MutableObject<>(selectExpression.getLimitClause().accept(this, currentOpRef).first);
}
Pair<ILogicalOperator, LogicalVariable> result = produceSelectPlan(selectExpression.isSubquery(), currentOpRef, select.second);
if (selectExpression.isSubquery()) {
context.exitSubplan();
}
return result;
}
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);
}
Aggregations