Search in sources :

Example 6 with ILangExpression

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

the class SubstituteGroupbyExpressionVisitor method visit.

@Override
public Expression visit(SelectBlock selectBlock, ILangExpression arg) throws CompilationException {
    if (selectBlock.hasGroupbyClause()) {
        Map<Expression, Expression> map = new HashMap<>();
        for (GbyVariableExpressionPair gbyKeyPair : selectBlock.getGroupbyClause().getGbyPairList()) {
            Expression gbyKeyExpr = gbyKeyPair.getExpr();
            if (gbyKeyExpr.getKind() != Kind.VARIABLE_EXPRESSION) {
                map.put(gbyKeyExpr, gbyKeyPair.getVar());
            }
        }
        // Creates a substitution visitor.
        SubstituteGroupbyExpressionVisitor visitor = new SubstituteGroupbyExpressionVisitor(context, map);
        // Rewrites LET/HAVING/SELECT clauses.
        if (selectBlock.hasLetClausesAfterGroupby()) {
            for (LetClause letClause : selectBlock.getLetListAfterGroupby()) {
                letClause.accept(this, arg);
            }
        }
        if (selectBlock.hasHavingClause()) {
            selectBlock.getHavingClause().accept(visitor, arg);
        }
        selectBlock.getSelectClause().accept(visitor, arg);
        SelectExpression selectExpression = (SelectExpression) arg;
        // For SET operation queries, the GROUP BY key variables 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 : 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) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression)

Example 7 with ILangExpression

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

the class SqlppAstPrintUtil method toString.

/**
     * @param exprs
     *            a list of language expression.
     * @return an AST of the input language expressions.
     * @throws CompilationException
     */
public static String toString(List<ILangExpression> exprs) throws CompilationException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    PrintWriter output = new PrintWriter(bos);
    QueryPrintVisitor visitor = astPrintVisitorFactory.createLangVisitor(output);
    for (ILangExpression expr : exprs) {
        expr.accept(visitor, 0);
    }
    output.close();
    return bos.toString();
}
Also used : QueryPrintVisitor(org.apache.asterix.lang.common.visitor.QueryPrintVisitor) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PrintWriter(java.io.PrintWriter)

Example 8 with ILangExpression

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

the class VariableCloneAndSubstitutionUtil method substInVarExprPair.

public static List<GbyVariableExpressionPair> substInVarExprPair(LangRewritingContext context, List<GbyVariableExpressionPair> gbyVeList, VariableSubstitutionEnvironment newSubs, CloneAndSubstituteVariablesVisitor visitor) throws CompilationException {
    VariableSubstitutionEnvironment subs = newSubs;
    List<GbyVariableExpressionPair> veList = new LinkedList<>();
    for (GbyVariableExpressionPair vep : gbyVeList) {
        VariableExpr oldGbyVar = vep.getVar();
        VariableExpr newGbyVar = null;
        if (oldGbyVar != null) {
            newGbyVar = visitor.generateNewVariable(context, oldGbyVar);
            subs = eliminateSubstFromList(newGbyVar, subs);
        }
        Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = vep.getExpr().accept(visitor, subs);
        GbyVariableExpressionPair ve2 = new GbyVariableExpressionPair(newGbyVar, (Expression) p1.first);
        veList.add(ve2);
    }
    return veList;
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) LinkedList(java.util.LinkedList)

Example 9 with ILangExpression

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

the class AbstractInlineUdfsVisitor method inlineUdfsInExpr.

protected Pair<Boolean, Expression> inlineUdfsInExpr(Expression expr, List<FunctionDecl> arg) throws CompilationException {
    if (expr.getKind() != Kind.CALL_EXPRESSION) {
        boolean r = expr.accept(this, arg);
        return new Pair<>(r, expr);
    }
    CallExpr f = (CallExpr) expr;
    boolean r = expr.accept(this, arg);
    FunctionDecl implem = findFuncDeclaration(f.getFunctionSignature(), arg);
    if (implem == null) {
        return new Pair<>(r, expr);
    } else {
        // Rewrite the function body itself (without setting unbounded variables to dataset access).
        // TODO(buyingyi): throw an exception for recursive function definition or limit the stack depth.
        implem.setFuncBody(rewriteFunctionBody(implem.getFuncBody()));
        // it's one of the functions we want to inline
        List<LetClause> clauses = new ArrayList<>();
        Iterator<VarIdentifier> paramIter = implem.getParamList().iterator();
        VariableSubstitutionEnvironment subts = new VariableSubstitutionEnvironment();
        for (Expression e : f.getExprList()) {
            VarIdentifier param = paramIter.next();
            // variable inlining to take care of this.
            if (e.getKind() == Kind.VARIABLE_EXPRESSION) {
                subts.addSubstituion(new VariableExpr(param), e);
            } else {
                VarIdentifier newV = context.newVariable();
                Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = e.accept(cloneVisitor, new VariableSubstitutionEnvironment());
                LetClause c = new LetClause(new VariableExpr(newV), (Expression) p1.first);
                clauses.add(c);
                subts.addSubstituion(new VariableExpr(param), new VariableExpr(newV));
            }
        }
        Pair<ILangExpression, VariableSubstitutionEnvironment> p2 = implem.getFuncBody().accept(cloneVisitor, subts);
        Expression resExpr;
        if (clauses.isEmpty()) {
            resExpr = (Expression) p2.first;
        } else {
            resExpr = generateQueryExpression(clauses, (Expression) p2.first);
        }
        return new Pair<>(true, resExpr);
    }
}
Also used : LetClause(org.apache.asterix.lang.common.clause.LetClause) ArrayList(java.util.ArrayList) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl) VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) Expression(org.apache.asterix.lang.common.base.Expression) CallExpr(org.apache.asterix.lang.common.expression.CallExpr) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair) QuantifiedPair(org.apache.asterix.lang.common.struct.QuantifiedPair)

Example 10 with ILangExpression

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

the class AQLCloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(ForClause fc, VariableSubstitutionEnvironment env) throws CompilationException {
    Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = fc.getInExpr().accept(this, env);
    VariableExpr varExpr = fc.getVarExpr();
    VariableExpr newVe = generateNewVariable(context, varExpr);
    VariableSubstitutionEnvironment resultEnv = new VariableSubstitutionEnvironment(env);
    resultEnv.removeSubstitution(varExpr);
    VariableExpr newPosVarExpr = null;
    if (fc.hasPosVar()) {
        VariableExpr posVarExpr = fc.getPosVarExpr();
        newPosVarExpr = generateNewVariable(context, posVarExpr);
        resultEnv.removeSubstitution(posVarExpr);
    }
    ForClause newFor = new ForClause(newVe, (Expression) p1.first, newPosVarExpr);
    return new Pair<ILangExpression, VariableSubstitutionEnvironment>(newFor, resultEnv);
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ForClause(org.apache.asterix.lang.aql.clause.ForClause) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

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