Search in sources :

Example 46 with VariableExpr

use of org.apache.asterix.lang.common.expression.VariableExpr 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)

Example 47 with VariableExpr

use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.

the class OperatorExpressionVisitor method processInOperator.

private Expression processInOperator(OperatorExpr operatorExpr, OperatorType opType) throws CompilationException {
    VariableExpr bindingVar = new VariableExpr(context.newVariable());
    Expression itemExpr = operatorExpr.getExprList().get(0);
    Expression collectionExpr = operatorExpr.getExprList().get(1);
    OperatorExpr comparison = new OperatorExpr();
    comparison.addOperand(itemExpr);
    comparison.addOperand(bindingVar);
    comparison.setCurrentop(true);
    if (opType == OperatorType.IN) {
        comparison.addOperator("=");
        return new QuantifiedExpression(Quantifier.SOME, new ArrayList<>(Collections.singletonList(new QuantifiedPair(bindingVar, collectionExpr))), comparison);
    } else {
        comparison.addOperator("!=");
        return new QuantifiedExpression(Quantifier.EVERY, new ArrayList<>(Collections.singletonList(new QuantifiedPair(bindingVar, collectionExpr))), comparison);
    }
}
Also used : OperatorExpr(org.apache.asterix.lang.common.expression.OperatorExpr) QuantifiedPair(org.apache.asterix.lang.common.struct.QuantifiedPair) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) Expression(org.apache.asterix.lang.common.base.Expression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr)

Example 48 with VariableExpr

use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.

the class DeepCopyVisitor method visit.

@Override
public NestClause visit(NestClause nestClause, Void arg) throws CompilationException {
    Expression rightExpression = (Expression) nestClause.getRightExpression().accept(this, arg);
    VariableExpr rightVar = (VariableExpr) nestClause.getRightVariable().accept(this, arg);
    VariableExpr rightPositionVar = nestClause.getPositionalVariable() == null ? null : (VariableExpr) nestClause.getPositionalVariable().accept(this, arg);
    Expression conditionExpresion = (Expression) nestClause.getConditionExpression().accept(this, arg);
    return new NestClause(nestClause.getJoinType(), rightExpression, rightVar, rightPositionVar, conditionExpresion);
}
Also used : ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) NestClause(org.apache.asterix.lang.sqlpp.clause.NestClause) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr)

Example 49 with VariableExpr

use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.

the class DeepCopyVisitor method visit.

@Override
public JoinClause visit(JoinClause joinClause, Void arg) throws CompilationException {
    Expression rightExpression = (Expression) joinClause.getRightExpression().accept(this, arg);
    VariableExpr rightVar = (VariableExpr) joinClause.getRightVariable().accept(this, arg);
    VariableExpr rightPositionVar = joinClause.getPositionalVariable() == null ? null : (VariableExpr) joinClause.getPositionalVariable().accept(this, arg);
    Expression conditionExpresion = (Expression) joinClause.getConditionExpression().accept(this, arg);
    return new JoinClause(joinClause.getJoinType(), rightExpression, rightVar, rightPositionVar, conditionExpresion);
}
Also used : ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) JoinClause(org.apache.asterix.lang.sqlpp.clause.JoinClause)

Example 50 with VariableExpr

use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.

the class VariableCheckAndRewriteVisitor method visit.

@Override
public Expression visit(FieldAccessor fa, ILangExpression parent) throws CompilationException {
    Expression leadingExpr = fa.getExpr();
    if (leadingExpr.getKind() != Kind.VARIABLE_EXPRESSION) {
        fa.setExpr(leadingExpr.accept(this, fa));
        return fa;
    } else {
        VariableExpr varExpr = (VariableExpr) leadingExpr;
        String lastIdentifier = fa.getIdent().getValue();
        Expression resolvedExpr = resolve(varExpr, /** Resolves within the dataverse that has the same name as the variable name. */
        SqlppVariableUtil.toUserDefinedVariableName(varExpr.getVar().getValue()).getValue(), lastIdentifier, fa, parent);
        if (resolvedExpr.getKind() == Kind.CALL_EXPRESSION) {
            CallExpr callExpr = (CallExpr) resolvedExpr;
            if (callExpr.getFunctionSignature().equals(datasetFunction)) {
                // The field access is resolved to be a dataset access in the form of "dataverse.dataset".
                return resolvedExpr;
            }
        }
        fa.setExpr(resolvedExpr);
        return fa;
    }
}
Also used : Expression(org.apache.asterix.lang.common.base.Expression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) CallExpr(org.apache.asterix.lang.common.expression.CallExpr)

Aggregations

VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)59 Expression (org.apache.asterix.lang.common.base.Expression)35 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)29 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)20 ArrayList (java.util.ArrayList)19 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)18 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)17 Pair (org.apache.hyracks.algebricks.common.utils.Pair)16 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)13 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)12 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)11 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)11 HashSet (java.util.HashSet)9 HashMap (java.util.HashMap)8 LetClause (org.apache.asterix.lang.common.clause.LetClause)8 Identifier (org.apache.asterix.lang.common.struct.Identifier)8 ForClause (org.apache.asterix.lang.aql.clause.ForClause)7 GroupbyClause (org.apache.asterix.lang.common.clause.GroupbyClause)7 FromTerm (org.apache.asterix.lang.sqlpp.clause.FromTerm)6 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)6