Search in sources :

Example 6 with Scope

use of org.apache.asterix.lang.common.context.Scope in project asterixdb by apache.

the class ScopeChecker method extendCurrentScope.

public final Scope extendCurrentScope(boolean maskParentScope) {
    Scope scope = extendCurrentScopeNoPush(maskParentScope);
    scopeStack.pop();
    scopeStack.push(scope);
    return scope;
}
Also used : Scope(org.apache.asterix.lang.common.context.Scope)

Example 7 with Scope

use of org.apache.asterix.lang.common.context.Scope in project asterixdb by apache.

the class ScopeChecker method createNewScope.

// Forbidden scopes are used to disallow, in a limit clause, variables
// having the same name as a variable defined by the FLWOR in which that
// limit clause appears.
/**
     * Create a new scope, using the top scope in scopeStack as parent scope
     *
     * @return new scope
     */
public final Scope createNewScope() {
    Scope parent = scopeStack.peek();
    // top one as parent
    Scope scope = new Scope(this, parent);
    scopeStack.push(scope);
    return scope;
}
Also used : Scope(org.apache.asterix.lang.common.context.Scope)

Example 8 with Scope

use of org.apache.asterix.lang.common.context.Scope in project asterixdb by apache.

the class AbstractSqlppExpressionScopingVisitor method visit.

@Override
public Expression visit(IndependentSubquery independentSubquery, ILangExpression arg) throws CompilationException {
    // Masks parent scopes so as that the subquery is independent of the environment.
    // In this way, free variables defined in the subquery will not be resolved using
    // variables defined in the parent scope.
    Scope scope = new Scope(scopeChecker, scopeChecker.getCurrentScope(), true);
    scopeChecker.pushExistingScope(scope);
    independentSubquery.setExpr(visit(independentSubquery.getExpr(), independentSubquery));
    scopeChecker.removeCurrentScope();
    return independentSubquery;
}
Also used : Scope(org.apache.asterix.lang.common.context.Scope)

Example 9 with Scope

use of org.apache.asterix.lang.common.context.Scope 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;
}
Also used : Scope(org.apache.asterix.lang.common.context.Scope) LetClause(org.apache.asterix.lang.common.clause.LetClause)

Example 10 with Scope

use of org.apache.asterix.lang.common.context.Scope 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;
}
Also used : Identifier(org.apache.asterix.lang.common.struct.Identifier) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) Scope(org.apache.asterix.lang.common.context.Scope) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) HashMap(java.util.HashMap) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr)

Aggregations

Scope (org.apache.asterix.lang.common.context.Scope)10 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)3 Expression (org.apache.asterix.lang.common.base.Expression)2 HashMap (java.util.HashMap)1 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)1 LetClause (org.apache.asterix.lang.common.clause.LetClause)1 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)1 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)1 Identifier (org.apache.asterix.lang.common.struct.Identifier)1 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)1 FromTerm (org.apache.asterix.lang.sqlpp.clause.FromTerm)1 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)1 SetOperationRight (org.apache.asterix.lang.sqlpp.struct.SetOperationRight)1 FunctionIdentifier (org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier)1