Search in sources :

Example 1 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(FromClause fromClause, ILangExpression arg) throws CompilationException {
    Scope scopeForFromClause = scopeChecker.extendCurrentScope();
    for (FromTerm fromTerm : fromClause.getFromTerms()) {
        fromTerm.accept(this, fromClause);
        // Merges the variables defined in the current from term into the scope of the current from clause.
        Scope scopeForFromTerm = scopeChecker.removeCurrentScope();
        mergeScopes(scopeForFromClause, scopeForFromTerm);
    }
    return null;
}
Also used : Scope(org.apache.asterix.lang.common.context.Scope) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm)

Example 2 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(SelectSetOperation selectSetOperation, ILangExpression arg) throws CompilationException {
    Scope scopeBeforeCurrentBranch = scopeChecker.getCurrentScope();
    scopeChecker.createNewScope();
    selectSetOperation.getLeftInput().accept(this, arg);
    if (selectSetOperation.hasRightInputs()) {
        for (SetOperationRight right : selectSetOperation.getRightInputs()) {
            // Exit scopes that were entered within a previous select expression
            while (scopeChecker.getCurrentScope() != scopeBeforeCurrentBranch) {
                scopeChecker.removeCurrentScope();
            }
            scopeChecker.createNewScope();
            right.getSetOperationRightInput().accept(this, arg);
        }
        // Exit scopes that were entered within the last branch of the set operation.
        while (scopeChecker.getCurrentScope() != scopeBeforeCurrentBranch) {
            scopeChecker.removeCurrentScope();
        }
    }
    return null;
}
Also used : Scope(org.apache.asterix.lang.common.context.Scope) SetOperationRight(org.apache.asterix.lang.sqlpp.struct.SetOperationRight)

Example 3 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(JoinClause joinClause, ILangExpression arg) throws CompilationException {
    Scope leftScope = scopeChecker.removeCurrentScope();
    scopeChecker.createNewScope();
    // NOTE: the two join branches cannot be correlated, instead of checking
    // the correlation here,
    // we defer the check to the query optimizer.
    joinClause.setRightExpression(visit(joinClause.getRightExpression(), joinClause));
    // Registers the data item variable.
    VariableExpr rightVar = joinClause.getRightVariable();
    addNewVarSymbolToScope(scopeChecker.getCurrentScope(), rightVar.getVar());
    if (joinClause.hasPositionalVariable()) {
        // Registers the positional variable.
        VariableExpr posVar = joinClause.getPositionalVariable();
        addNewVarSymbolToScope(scopeChecker.getCurrentScope(), posVar.getVar());
    }
    Scope rightScope = scopeChecker.removeCurrentScope();
    mergeScopes(leftScope, rightScope);
    scopeChecker.pushExistingScope(leftScope);
    // The condition expression can refer to the just registered variables
    // for the right branch.
    joinClause.setConditionExpression(visit(joinClause.getConditionExpression(), joinClause));
    return null;
}
Also used : Scope(org.apache.asterix.lang.common.context.Scope) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr)

Example 4 with Scope

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

the class SqlppSubstituteExpressionVisitor method preVisit.

// Note: we intentionally override preVisit instead of postVisit because we wants larger expressions
// get substituted first if some of their child expressions are present as keys in the exprMap.
// An example is:
// asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-3/gby-expr-3.3.query.sqlpp
@Override
protected Expression preVisit(Expression expr) throws CompilationException {
    Expression mappedExpr = exprMap.get(expr);
    if (mappedExpr == null) {
        return expr;
    }
    Collection<VariableExpr> freeVars = SqlppVariableUtil.getFreeVariables(expr);
    for (VariableExpr freeVar : freeVars) {
        Scope currentScope = scopeChecker.getCurrentScope();
        if (currentScope.findSymbol(freeVar.getVar().getValue()) != null) {
            // that is being visited, we shouldn't perform the substitution.
            return expr;
        }
    }
    // Makes a deep copy before returning to avoid shared references.
    return (Expression) SqlppRewriteUtil.deepCopy(mappedExpr);
}
Also used : Scope(org.apache.asterix.lang.common.context.Scope) Expression(org.apache.asterix.lang.common.base.Expression) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr)

Example 5 with Scope

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

the class ScopeChecker method extendCurrentScopeNoPush.

public final Scope extendCurrentScopeNoPush(boolean maskParentScope) {
    Scope scope = scopeStack.peek();
    scope = new Scope(this, scope, maskParentScope);
    return scope;
}
Also used : Scope(org.apache.asterix.lang.common.context.Scope)

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