use of org.apache.asterix.lang.common.base.ILangExpression in project asterixdb by apache.
the class AbstractSqlppSimpleExpressionVisitor method visit.
@Override
public Expression visit(InsertStatement insertStatement, ILangExpression arg) throws CompilationException {
Expression returnExpr = insertStatement.getReturnExpression();
if (returnExpr != null) {
insertStatement.setReturnExpression(visit(returnExpr, arg));
}
Query bodyQuery = insertStatement.getQuery();
bodyQuery.accept(this, arg);
return null;
}
use of org.apache.asterix.lang.common.base.ILangExpression in project asterixdb by apache.
the class SqlppCloneAndSubstituteVariablesVisitor method visit.
@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(JoinClause joinClause, VariableSubstitutionEnvironment env) throws CompilationException {
VariableExpr rightVar = joinClause.getRightVariable();
VariableExpr newRightVar = generateNewVariable(context, rightVar);
VariableExpr newRightPosVar = joinClause.hasPositionalVariable() ? generateNewVariable(context, joinClause.getPositionalVariable()) : null;
// Visits the right expression.
Expression newRightExpr = (Expression) visitUnnesBindingExpression(joinClause.getRightExpression(), env).first;
// Visits the condition.
VariableSubstitutionEnvironment currentEnv = new VariableSubstitutionEnvironment(env);
currentEnv.removeSubstitution(newRightVar);
if (newRightPosVar != null) {
currentEnv.removeSubstitution(newRightPosVar);
}
// The condition can refer to the newRightVar and newRightPosVar.
Expression conditionExpr = (Expression) joinClause.getConditionExpression().accept(this, currentEnv).first;
JoinClause newJoinClause = new JoinClause(joinClause.getJoinType(), newRightExpr, newRightVar, newRightPosVar, conditionExpr);
return new Pair<>(newJoinClause, currentEnv);
}
use of org.apache.asterix.lang.common.base.ILangExpression in project asterixdb by apache.
the class AbstractSqlppExpressionScopingVisitor method visit.
@Override
public Expression visit(GroupbyClause gc, ILangExpression arg) throws CompilationException {
// After a GROUP BY, variables defined before the current SELECT BLOCK (e.g., in a WITH clause
// or an outer scope query) should still be visible.
Scope newScope = new Scope(scopeChecker, scopeChecker.getCurrentScope().getParentScope());
// Puts all group-by variables into the symbol set of the new scope.
for (GbyVariableExpressionPair gbyKeyVarExpr : gc.getGbyPairList()) {
gbyKeyVarExpr.setExpr(visit(gbyKeyVarExpr.getExpr(), gc));
VariableExpr gbyKeyVar = gbyKeyVarExpr.getVar();
if (gbyKeyVar != null) {
addNewVarSymbolToScope(newScope, gbyKeyVar.getVar());
}
}
if (gc.hasGroupFieldList()) {
for (Pair<Expression, Identifier> gbyField : gc.getGroupFieldList()) {
gbyField.first = visit(gbyField.first, arg);
}
}
if (gc.hasDecorList()) {
for (GbyVariableExpressionPair decorVarExpr : gc.getDecorPairList()) {
decorVarExpr.setExpr(visit(decorVarExpr.getExpr(), gc));
VariableExpr decorVar = decorVarExpr.getVar();
if (decorVar != null) {
addNewVarSymbolToScope(newScope, decorVar.getVar());
}
}
}
if (gc.hasGroupVar()) {
addNewVarSymbolToScope(scopeChecker.getCurrentScope(), gc.getGroupVar().getVar());
}
if (gc.hasWithMap()) {
Map<Expression, VariableExpr> newWithMap = new HashMap<>();
for (Entry<Expression, VariableExpr> entry : gc.getWithVarMap().entrySet()) {
Expression expr = visit(entry.getKey(), arg);
Expression newKey = expr;
VariableExpr value = entry.getValue();
addNewVarSymbolToScope(newScope, value.getVar());
newWithMap.put(newKey, value);
}
gc.setWithVarMap(newWithMap);
}
// Replaces the current scope with the new scope.
scopeChecker.replaceCurrentScope(newScope);
return null;
}
use of org.apache.asterix.lang.common.base.ILangExpression 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.base.ILangExpression 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