Search in sources :

Example 81 with Expression

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;
}
Also used : Query(org.apache.asterix.lang.common.statement.Query) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression)

Example 82 with Expression

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);
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) JoinClause(org.apache.asterix.lang.sqlpp.clause.JoinClause) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 83 with Expression

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;
}
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 84 with Expression

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);
}
Also used : StringLiteral(org.apache.asterix.lang.common.literal.StringLiteral) 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) ArrayList(java.util.ArrayList) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) CallExpr(org.apache.asterix.lang.common.expression.CallExpr)

Example 85 with Expression

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);
}
Also used : SqlppSubstituteExpressionVisitor(org.apache.asterix.lang.sqlpp.visitor.SqlppSubstituteExpressionVisitor) LetClause(org.apache.asterix.lang.common.clause.LetClause) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression)

Aggregations

Expression (org.apache.asterix.lang.common.base.Expression)105 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)75 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)52 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)41 ArrayList (java.util.ArrayList)37 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)36 Pair (org.apache.hyracks.algebricks.common.utils.Pair)35 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)32 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)32 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)22 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)19 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)19 FLWOGRExpression (org.apache.asterix.lang.aql.expression.FLWOGRExpression)17 Mutable (org.apache.commons.lang3.mutable.Mutable)17 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)17 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)16 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)16 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)16 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)16 UnnestingFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression)16