Search in sources :

Example 46 with ILangExpression

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

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

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

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

Example 50 with ILangExpression

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

Aggregations

ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)56 Expression (org.apache.asterix.lang.common.base.Expression)34 Pair (org.apache.hyracks.algebricks.common.utils.Pair)34 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)28 ArrayList (java.util.ArrayList)23 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)23 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)18 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)18 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)16 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)15 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)9 LetClause (org.apache.asterix.lang.common.clause.LetClause)8 HashMap (java.util.HashMap)5 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)5 FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)4 CallExpr (org.apache.asterix.lang.common.expression.CallExpr)4 FromClause (org.apache.asterix.lang.sqlpp.clause.FromClause)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PrintWriter (java.io.PrintWriter)3 FLWOGRExpression (org.apache.asterix.lang.aql.expression.FLWOGRExpression)3