use of org.apache.asterix.lang.common.base.Expression 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.Expression 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.Expression 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.Expression in project asterixdb by apache.
the class AbstractSqlppExpressionScopingVisitor method wrapWithResolveFunction.
// Rewrites for an undefined variable reference, which potentially could be a syntatic sugar.
protected Expression wrapWithResolveFunction(VariableExpr expr, Set<VariableExpr> liveVars) throws CompilationException {
List<Expression> argList = new ArrayList<>();
//Ignore the parser-generated prefix "$" for a dataset.
String varName = SqlppVariableUtil.toUserDefinedVariableName(expr.getVar().getValue()).getValue();
argList.add(new LiteralExpr(new StringLiteral(varName)));
argList.addAll(liveVars);
return new CallExpr(resolveFunction, argList);
}
use of org.apache.asterix.lang.common.base.Expression 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