Search in sources :

Example 86 with Expression

use of org.apache.asterix.lang.common.base.Expression in project asterixdb by apache.

the class InlineWithExpressionVisitor method visit.

@Override
public Expression visit(SelectExpression selectExpression, ILangExpression arg) throws CompilationException {
    if (selectExpression.hasLetClauses()) {
        // Inlines the leading WITH list.
        Map<Expression, Expression> varExprMap = new HashMap<>();
        List<LetClause> withs = selectExpression.getLetList();
        Iterator<LetClause> with = withs.iterator();
        while (with.hasNext()) {
            LetClause letClause = with.next();
            // Replaces the let binding Expr.
            Expression expr = letClause.getBindingExpr();
            Expression newBindingExpr = SqlppRewriteUtil.substituteExpression(expr, varExprMap, context);
            letClause.setBindingExpr(newBindingExpr);
            // Performs the rewriting recursively in the newBindingExpr itself.
            super.visit(newBindingExpr, arg);
            // Removes the WITH entry and adds variable-expr mapping into the varExprMap.
            with.remove();
            Expression bindingExpr = letClause.getBindingExpr();
            // Wraps the binding expression with IndependentSubquery, so that free identifier references
            // in the binding expression will not be resolved use outer-scope variables.
            varExprMap.put(letClause.getVarExpr(), bindingExpr);
        }
        // Inlines WITH expressions into the select expression.
        SelectExpression newSelectExpression = (SelectExpression) substituteExpression(selectExpression, varExprMap, context);
        // Continues to visit the rewritten select expression.
        return super.visit(newSelectExpression, arg);
    } else {
        // Continues to visit inside the select expression.
        return super.visit(selectExpression, arg);
    }
}
Also used : LetClause(org.apache.asterix.lang.common.clause.LetClause) HashMap(java.util.HashMap) Expression(org.apache.asterix.lang.common.base.Expression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) SqlppRewriteUtil.substituteExpression(org.apache.asterix.lang.sqlpp.util.SqlppRewriteUtil.substituteExpression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression)

Example 87 with Expression

use of org.apache.asterix.lang.common.base.Expression in project asterixdb by apache.

the class OperatorExpressionVisitor method processBetweenOperator.

private Expression processBetweenOperator(OperatorExpr operatorExpr, OperatorType opType) throws CompilationException {
    // The grammar guarantees that the BETWEEN operator gets exactly three expressions.
    Expression target = operatorExpr.getExprList().get(0);
    Expression left = operatorExpr.getExprList().get(1);
    Expression right = operatorExpr.getExprList().get(2);
    // Creates the expression left <= target.
    Expression leftComparison = createLessThanExpression(left, target, operatorExpr.getHints());
    // Creates the expression target <= right.
    Expression rightComparison = createLessThanExpression(target, right, operatorExpr.getHints());
    OperatorExpr andExpr = new OperatorExpr();
    andExpr.addOperand(leftComparison);
    andExpr.addOperand(rightComparison);
    andExpr.addOperator("and");
    return opType == OperatorType.BETWEEN ? andExpr : new CallExpr(new FunctionSignature(null, "not", 1), new ArrayList<>(Collections.singletonList(andExpr)));
}
Also used : OperatorExpr(org.apache.asterix.lang.common.expression.OperatorExpr) Expression(org.apache.asterix.lang.common.base.Expression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) ArrayList(java.util.ArrayList) CallExpr(org.apache.asterix.lang.common.expression.CallExpr) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature)

Example 88 with Expression

use of org.apache.asterix.lang.common.base.Expression 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 89 with Expression

use of org.apache.asterix.lang.common.base.Expression 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 90 with Expression

use of org.apache.asterix.lang.common.base.Expression in project asterixdb by apache.

the class DeepCopyVisitor method visit.

@Override
public SelectExpression visit(SelectExpression selectExpression, Void arg) throws CompilationException {
    List<LetClause> lets = new ArrayList<>();
    SelectSetOperation select;
    OrderbyClause orderby = null;
    LimitClause limit = null;
    // visit let list
    if (selectExpression.hasLetClauses()) {
        for (LetClause letClause : selectExpression.getLetList()) {
            lets.add((LetClause) letClause.accept(this, arg));
        }
    }
    // visit the main select.
    select = (SelectSetOperation) selectExpression.getSelectSetOperation().accept(this, arg);
    // visit order by
    if (selectExpression.hasOrderby()) {
        List<Expression> orderExprs = new ArrayList<>();
        for (Expression orderExpr : selectExpression.getOrderbyClause().getOrderbyList()) {
            orderExprs.add((Expression) orderExpr.accept(this, arg));
        }
        orderby = new OrderbyClause(orderExprs, selectExpression.getOrderbyClause().getModifierList());
    }
    // visit limit
    if (selectExpression.hasLimit()) {
        limit = (LimitClause) selectExpression.getLimitClause().accept(this, arg);
    }
    return new SelectExpression(lets, select, orderby, limit, selectExpression.isSubquery());
}
Also used : LimitClause(org.apache.asterix.lang.common.clause.LimitClause) 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) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) ArrayList(java.util.ArrayList) OrderbyClause(org.apache.asterix.lang.common.clause.OrderbyClause) 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