Search in sources :

Example 6 with VariableSubstitutionEnvironment

use of org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment in project asterixdb by apache.

the class SqlppCloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(SelectSetOperation selectSetOperation, VariableSubstitutionEnvironment env) throws CompilationException {
    SetOperationInput leftInput = selectSetOperation.getLeftInput();
    SetOperationInput newLeftInput;
    Pair<ILangExpression, VariableSubstitutionEnvironment> leftResult;
    // Sets the left input.
    if (leftInput.selectBlock()) {
        leftResult = leftInput.getSelectBlock().accept(this, env);
        newLeftInput = new SetOperationInput((SelectBlock) leftResult.first, null);
    } else {
        leftResult = leftInput.getSubquery().accept(this, env);
        newLeftInput = new SetOperationInput(null, (SelectExpression) leftResult.first);
    }
    // Sets the right input
    List<SetOperationRight> newRightInputs = new ArrayList<>();
    if (selectSetOperation.hasRightInputs()) {
        for (SetOperationRight right : selectSetOperation.getRightInputs()) {
            SetOperationInput newRightInput;
            SetOperationInput rightInput = right.getSetOperationRightInput();
            if (rightInput.selectBlock()) {
                Pair<ILangExpression, VariableSubstitutionEnvironment> rightResult = rightInput.getSelectBlock().accept(this, env);
                newRightInput = new SetOperationInput((SelectBlock) rightResult.first, null);
            } else {
                Pair<ILangExpression, VariableSubstitutionEnvironment> rightResult = rightInput.getSubquery().accept(this, env);
                newRightInput = new SetOperationInput(null, (SelectExpression) rightResult.first);
            }
            newRightInputs.add(new SetOperationRight(right.getSetOpType(), right.isSetSemantics(), newRightInput));
        }
    }
    SelectSetOperation newSelectSetOperation = new SelectSetOperation(newLeftInput, newRightInputs);
    return new Pair<>(newSelectSetOperation, selectSetOperation.hasRightInputs() ? env : leftResult.second);
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) SetOperationInput(org.apache.asterix.lang.sqlpp.struct.SetOperationInput) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) ArrayList(java.util.ArrayList) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) SetOperationRight(org.apache.asterix.lang.sqlpp.struct.SetOperationRight) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 7 with VariableSubstitutionEnvironment

use of org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment in project asterixdb by apache.

the class SqlppCloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(HavingClause havingClause, VariableSubstitutionEnvironment env) throws CompilationException {
    Pair<ILangExpression, VariableSubstitutionEnvironment> p = havingClause.getFilterExpression().accept(this, env);
    HavingClause newHavingClause = new HavingClause((Expression) p.first);
    return new Pair<>(newHavingClause, p.second);
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) HavingClause(org.apache.asterix.lang.sqlpp.clause.HavingClause) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 8 with VariableSubstitutionEnvironment

use of org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment in project asterixdb by apache.

the class VariableCloneAndSubstitutionUtil method eliminateSubstFromList.

public static VariableSubstitutionEnvironment eliminateSubstFromList(VariableExpr variableExpr, VariableSubstitutionEnvironment arg) {
    VariableSubstitutionEnvironment newArg = new VariableSubstitutionEnvironment(arg);
    newArg.removeSubstitution(variableExpr);
    return newArg;
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)

Example 9 with VariableSubstitutionEnvironment

use of org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment 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 10 with VariableSubstitutionEnvironment

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

Aggregations

VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)31 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)28 Pair (org.apache.hyracks.algebricks.common.utils.Pair)26 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)15 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)14 ArrayList (java.util.ArrayList)13 Expression (org.apache.asterix.lang.common.base.Expression)12 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)11 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)7 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)6 LetClause (org.apache.asterix.lang.common.clause.LetClause)4 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)4 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)3 ForClause (org.apache.asterix.lang.aql.clause.ForClause)2 GroupbyClause (org.apache.asterix.lang.common.clause.GroupbyClause)2 LimitClause (org.apache.asterix.lang.common.clause.LimitClause)2 WhereClause (org.apache.asterix.lang.common.clause.WhereClause)2 FunctionDecl (org.apache.asterix.lang.common.statement.FunctionDecl)2 FromClause (org.apache.asterix.lang.sqlpp.clause.FromClause)2 FromTerm (org.apache.asterix.lang.sqlpp.clause.FromTerm)2