Search in sources :

Example 16 with VariableExpr

use of org.apache.asterix.lang.common.expression.VariableExpr 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 17 with VariableExpr

use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.

the class ClauseComparator method visit.

@Override
public Void visit(FLWOGRExpression flwor, Integer step) throws CompilationException {
    if (step != 0) {
        out.println("(");
    }
    List<Clause> clauseList = new ArrayList<Clause>();
    clauseList.addAll(flwor.getClauseList());
    // Processes data-independent let clauses.
    if (hasFor(clauseList)) {
        processLeadingLetClauses(step, clauseList);
    }
    // Distill unnecessary order-bys before a group-by.
    distillRedundantOrderby(clauseList);
    // Correlated "for" clauses after group-by.
    Pair<GroupbyClause, List<Clause>> extraction = extractUnnestAfterGroupby(clauseList);
    GroupbyClause cuttingGbyClause = extraction.first;
    List<Clause> unnestClauseList = extraction.second;
    Expression returnExpr = flwor.getReturnExpr();
    if (unnestClauseList.size() == 0) {
        if (hasFor(clauseList)) {
            out.print(skip(step) + "select element ");
            returnExpr.accept(this, step + 2);
            out.println();
        } else {
            // The FLOWGR only contains let-return, then inline let binding expressions into the return expression.
            Map<VariableExpr, Expression> varExprMap = extractLetBindingVariables(clauseList, cuttingGbyClause);
            returnExpr = (Expression) AQLVariableSubstitutionUtil.substituteVariable(returnExpr, varExprMap);
            returnExpr.accept(this, step);
            return null;
        }
    }
    String generated = generateVariableSymbol();
    if (unnestClauseList.size() > 0) {
        Map<VariableExpr, Expression> varExprMap = extractDefinedCollectionVariables(clauseList, cuttingGbyClause, generated);
        returnExpr = (Expression) AQLVariableSubstitutionUtil.substituteVariable(returnExpr, varExprMap);
        List<Clause> newUnnestClauses = new ArrayList<Clause>();
        for (Clause nestedCl : unnestClauseList) {
            newUnnestClauses.add((Clause) AQLVariableSubstitutionUtil.substituteVariable(nestedCl, varExprMap));
        }
        unnestClauseList = newUnnestClauses;
        out.print(skip(step) + "select element " + (hasDistinct(unnestClauseList) ? "distinct " : ""));
        returnExpr.accept(this, step + 2);
        out.println();
        out.println(skip(step) + "from");
        out.print(skip(step + 2) + "( select element " + (hasDistinct(clauseList) ? "distinct " : "") + "{");
        int index = 0;
        int size = varExprMap.size();
        for (VariableExpr var : varExprMap.keySet()) {
            out.print("\'" + var.getVar().getValue().substring(1) + "\':" + var.getVar().getValue().substring(1));
            if (++index < size) {
                out.print(COMMA);
            }
        }
        out.println("}");
    }
    reorder(clauseList);
    reorder(unnestClauseList);
    mergeConsecutiveWhereClauses(clauseList);
    mergeConsecutiveWhereClauses(unnestClauseList);
    boolean firstFor = true;
    boolean firstLet = true;
    int forStep = unnestClauseList.size() == 0 ? step : step + 3;
    int size = clauseList.size();
    // "for"s.
    for (int i = 0; i < size; ++i) {
        Clause cl = clauseList.get(i);
        if (cl.getClauseType() == ClauseType.FOR_CLAUSE) {
            boolean hasConsequentFor = false;
            if (i < size - 1) {
                Clause nextCl = clauseList.get(i + 1);
                hasConsequentFor = nextCl.getClauseType() == ClauseType.FOR_CLAUSE;
            }
            visitForClause((ForClause) cl, forStep, firstFor, hasConsequentFor);
            firstFor = false;
        } else if (cl.getClauseType() == ClauseType.LET_CLAUSE) {
            boolean hasConsequentLet = false;
            if (i < size - 1) {
                Clause nextCl = clauseList.get(i + 1);
                hasConsequentLet = nextCl.getClauseType() == ClauseType.LET_CLAUSE;
            }
            visitLetClause((LetClause) cl, forStep, firstLet, hasConsequentLet);
            firstLet = false;
        } else {
            cl.accept(this, forStep);
        }
        if (cl.getClauseType() == ClauseType.FROM_CLAUSE || cl.getClauseType() == ClauseType.GROUP_BY_CLAUSE) {
            firstLet = true;
        }
    }
    if (unnestClauseList.size() > 0) {
        out.println(skip(forStep - 1) + ") as " + generated.substring(1) + ",");
        for (Clause nestedCl : unnestClauseList) {
            if (nestedCl.getClauseType() == ClauseType.FOR_CLAUSE) {
                visitForClause((ForClause) nestedCl, step - 1, firstFor, false);
            } else {
                nestedCl.accept(this, step);
            }
        }
    }
    if (step > 0) {
        out.print(skip(step - 2) + ")");
    }
    return null;
}
Also used : LetClause(org.apache.asterix.lang.common.clause.LetClause) ArrayList(java.util.ArrayList) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Expression(org.apache.asterix.lang.common.base.Expression) ArrayList(java.util.ArrayList) List(java.util.List) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ForClause(org.apache.asterix.lang.aql.clause.ForClause) DistinctClause(org.apache.asterix.lang.aql.clause.DistinctClause) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) LetClause(org.apache.asterix.lang.common.clause.LetClause) Clause(org.apache.asterix.lang.common.base.Clause) WhereClause(org.apache.asterix.lang.common.clause.WhereClause)

Example 18 with VariableExpr

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

Example 19 with VariableExpr

use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.

the class CloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(FunctionDecl fd, VariableSubstitutionEnvironment env) throws CompilationException {
    List<VarIdentifier> newList = new ArrayList<>(fd.getParamList().size());
    for (VarIdentifier vi : fd.getParamList()) {
        VariableExpr varExpr = new VariableExpr(vi);
        if (!env.constainsOldVar(varExpr)) {
            throw new CompilationException("Parameter " + vi + " does not appear in the substitution list.");
        }
        Expression newExpr = env.findSubstitution(varExpr);
        if (newExpr.getKind() != Kind.VARIABLE_EXPRESSION) {
            throw new CompilationException("Parameter " + vi + " cannot be substituted by a non-variable expression.");
        }
        newList.add(((VariableExpr) newExpr).getVar());
    }
    Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = fd.getFuncBody().accept(this, env);
    FunctionDecl newF = new FunctionDecl(fd.getSignature(), newList, (Expression) p1.first);
    return new Pair<>(newF, env);
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) 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) ArrayList(java.util.ArrayList) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl) 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 20 with VariableExpr

use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.

the class SqlppExpressionToPlanTranslator method processSelectClause.

// Generates the return expression for a select clause.
private Pair<ILogicalOperator, LogicalVariable> processSelectClause(SelectBlock selectBlock, Mutable<ILogicalOperator> tupSrc) throws CompilationException {
    SelectClause selectClause = selectBlock.getSelectClause();
    Expression returnExpr;
    if (selectClause.selectElement()) {
        returnExpr = selectClause.getSelectElement().getExpression();
    } else {
        returnExpr = generateReturnExpr(selectClause, selectBlock);
    }
    Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(returnExpr, tupSrc);
    LogicalVariable returnVar;
    ILogicalOperator returnOperator;
    if (returnExpr.getKind() == Kind.VARIABLE_EXPRESSION) {
        VariableExpr varExpr = (VariableExpr) returnExpr;
        returnOperator = eo.second.getValue();
        returnVar = context.getVar(varExpr.getVar().getId());
    } else {
        returnVar = context.newVar();
        returnOperator = new AssignOperator(returnVar, new MutableObject<ILogicalExpression>(eo.first));
        returnOperator.getInputs().add(eo.second);
    }
    if (selectClause.distinct()) {
        DistinctOperator distinctOperator = new DistinctOperator(mkSingletonArrayList(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(returnVar))));
        distinctOperator.getInputs().add(new MutableObject<ILogicalOperator>(returnOperator));
        return new Pair<>(distinctOperator, returnVar);
    } else {
        return new Pair<>(returnOperator, returnVar);
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) DistinctOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DistinctOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) AggregateFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) MutableObject(org.apache.commons.lang3.mutable.MutableObject) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Aggregations

VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)59 Expression (org.apache.asterix.lang.common.base.Expression)35 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)29 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)20 ArrayList (java.util.ArrayList)19 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)18 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)17 Pair (org.apache.hyracks.algebricks.common.utils.Pair)16 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)13 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)12 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)11 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)11 HashSet (java.util.HashSet)9 HashMap (java.util.HashMap)8 LetClause (org.apache.asterix.lang.common.clause.LetClause)8 Identifier (org.apache.asterix.lang.common.struct.Identifier)8 ForClause (org.apache.asterix.lang.aql.clause.ForClause)7 GroupbyClause (org.apache.asterix.lang.common.clause.GroupbyClause)7 FromTerm (org.apache.asterix.lang.sqlpp.clause.FromTerm)6 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)6