Search in sources :

Example 1 with SqlppSubstituteExpressionVisitor

use of org.apache.asterix.lang.sqlpp.visitor.SqlppSubstituteExpressionVisitor in project asterixdb by apache.

the class SqlppRewriteUtil method substituteExpression.

/**
     * Substitutes expression with replacement expressions according to the exprMap.
     *
     * @param expression
     *            ,
     *            an input expression.
     * @param exprMap
     *            a map that maps expressions to their corresponding replacement expressions.
     * @return an expression, where sub-expressions of the input expression (including the input expression itself)
     *         are replaced with deep copies with their mapped replacements in the exprMap if there exists such a
     *         replacement expression.
     * @throws CompilationException
     */
public static Expression substituteExpression(Expression expression, Map<Expression, Expression> exprMap, LangRewritingContext context) throws CompilationException {
    if (exprMap.isEmpty()) {
        return expression;
    }
    // Creates a wrapper query for the expression so that if the expression itself
    // is the key, it can also be replaced.
    Query wrapper = new Query(false);
    wrapper.setBody(expression);
    // Creates a substitution visitor.
    SqlppSubstituteExpressionVisitor visitor = new SqlppSubstituteExpressionVisitor(context, exprMap);
    wrapper.accept(visitor, wrapper);
    return wrapper.getBody();
}
Also used : Query(org.apache.asterix.lang.common.statement.Query) SqlppSubstituteExpressionVisitor(org.apache.asterix.lang.sqlpp.visitor.SqlppSubstituteExpressionVisitor)

Example 2 with SqlppSubstituteExpressionVisitor

use of org.apache.asterix.lang.sqlpp.visitor.SqlppSubstituteExpressionVisitor 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

SqlppSubstituteExpressionVisitor (org.apache.asterix.lang.sqlpp.visitor.SqlppSubstituteExpressionVisitor)2 Expression (org.apache.asterix.lang.common.base.Expression)1 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)1 LetClause (org.apache.asterix.lang.common.clause.LetClause)1 Query (org.apache.asterix.lang.common.statement.Query)1 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)1