Search in sources :

Example 91 with Mutable

use of org.apache.commons.lang3.mutable.Mutable in project asterixdb by apache.

the class InlineAllNtsInSubplanVisitor method visitGroupByOperator.

@Override
public ILogicalOperator visitGroupByOperator(GroupByOperator op, Void arg) throws AlgebricksException {
    visitSingleInputOperator(op);
    Set<LogicalVariable> groupKeyVars = new HashSet<>();
    // VariableReferenceExpressions.
    for (Pair<LogicalVariable, Mutable<ILogicalExpression>> keyVarExprRef : op.getGroupByList()) {
        ILogicalExpression expr = keyVarExprRef.second.getValue();
        if (expr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
            VariableReferenceExpression varExpr = (VariableReferenceExpression) expr;
            LogicalVariable sourceVar = varExpr.getVariableReference();
            updateInputToOutputVarMapping(sourceVar, keyVarExprRef.first, false);
            groupKeyVars.add(keyVarExprRef.first);
        }
    }
    // Add correlated key variables into group-by keys.
    Map<LogicalVariable, LogicalVariable> addedGroupKeyMapping = new HashMap<>();
    for (LogicalVariable keyVar : correlatedKeyVars) {
        if (!groupKeyVars.contains(keyVar)) {
            LogicalVariable newVar = context.newVar();
            op.getGroupByList().add(new Pair<LogicalVariable, Mutable<ILogicalExpression>>(newVar, new MutableObject<ILogicalExpression>(new VariableReferenceExpression(keyVar))));
            addedGroupKeyMapping.put(keyVar, newVar);
        }
    }
    // Updates decor list.
    Iterator<Pair<LogicalVariable, Mutable<ILogicalExpression>>> decorExprIter = op.getDecorList().iterator();
    while (decorExprIter.hasNext()) {
        ILogicalExpression expr = decorExprIter.next().second.getValue();
        if (expr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
            VariableReferenceExpression varExpr = (VariableReferenceExpression) expr;
            if (correlatedKeyVars.contains(varExpr.getVariableReference())) {
                decorExprIter.remove();
            }
        }
    }
    // Updates the var mapping for added group-by keys.
    for (Map.Entry<LogicalVariable, LogicalVariable> entry : addedGroupKeyMapping.entrySet()) {
        updateInputToOutputVarMapping(entry.getKey(), entry.getValue(), false);
    }
    return op;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 92 with Mutable

use of org.apache.commons.lang3.mutable.Mutable in project asterixdb by apache.

the class InlineAllNtsInSubplanVisitor method createUnnestForAggregatedList.

private Pair<ILogicalOperator, LogicalVariable> createUnnestForAggregatedList(LogicalVariable aggVar) {
    LogicalVariable unnestVar = context.newVar();
    // Creates an unnest function expression.
    Mutable<ILogicalExpression> unnestArg = new MutableObject<ILogicalExpression>(new VariableReferenceExpression(aggVar));
    List<Mutable<ILogicalExpression>> unnestArgList = new ArrayList<Mutable<ILogicalExpression>>();
    unnestArgList.add(unnestArg);
    Mutable<ILogicalExpression> unnestExpr = new MutableObject<ILogicalExpression>(new UnnestingFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SCAN_COLLECTION), unnestArgList));
    ILogicalOperator unnestOp = new UnnestOperator(unnestVar, unnestExpr);
    return new Pair<ILogicalOperator, LogicalVariable>(unnestOp, unnestVar);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Mutable(org.apache.commons.lang3.mutable.Mutable) LeftOuterUnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 93 with Mutable

use of org.apache.commons.lang3.mutable.Mutable in project asterixdb by apache.

the class InlineAllNtsInSubplanVisitor method createRecordConstructorAssignOp.

private Pair<ILogicalOperator, LogicalVariable> createRecordConstructorAssignOp(Set<LogicalVariable> inputLiveVars) {
    // Creates a nested record.
    List<Mutable<ILogicalExpression>> recordConstructorArgs = new ArrayList<>();
    for (LogicalVariable inputLiveVar : inputLiveVars) {
        if (!correlatedKeyVars.contains(inputLiveVar)) {
            recordConstructorArgs.add(new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AString(Integer.toString(inputLiveVar.getId()))))));
            recordConstructorArgs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(inputLiveVar)));
        }
    }
    LogicalVariable recordVar = context.newVar();
    Mutable<ILogicalExpression> recordExprRef = new MutableObject<ILogicalExpression>(new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.OPEN_RECORD_CONSTRUCTOR), recordConstructorArgs));
    AssignOperator assignOp = new AssignOperator(recordVar, recordExprRef);
    return new Pair<ILogicalOperator, LogicalVariable>(assignOp, recordVar);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ArrayList(java.util.ArrayList) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) AString(org.apache.asterix.om.base.AString) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 94 with Mutable

use of org.apache.commons.lang3.mutable.Mutable in project asterixdb by apache.

the class InlineLeftNtsInSubplanJoinFlatteningVisitor method visitOrderOperator.

@Override
public ILogicalOperator visitOrderOperator(OrderOperator op, Void arg) throws AlgebricksException {
    boolean underJoin = hasJoinAncestor;
    visitSingleInputOperator(op);
    if (!rewritten || !underJoin) {
        return op;
    }
    // Adjust the ordering if its input operator pipeline has been rewritten.
    List<Pair<IOrder, Mutable<ILogicalExpression>>> orderExprList = new ArrayList<>();
    // Adds keyVars to the prefix of sorting columns.
    for (LogicalVariable liveVar : liveVarsFromSubplanInput) {
        orderExprList.add(new Pair<IOrder, Mutable<ILogicalExpression>>(OrderOperator.ASC_ORDER, new MutableObject<ILogicalExpression>(new VariableReferenceExpression(liveVar))));
    }
    orderExprList.addAll(op.getOrderExpressions());
    // Creates an order operator with the new expression list.
    OrderOperator orderOp = new OrderOperator(orderExprList);
    orderOp.getInputs().addAll(op.getInputs());
    context.computeAndSetTypeEnvironmentForOperator(orderOp);
    return orderOp;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) IOrder(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator.IOrder) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ArrayList(java.util.ArrayList) OrderOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator) Pair(org.apache.hyracks.algebricks.common.utils.Pair) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 95 with Mutable

use of org.apache.commons.lang3.mutable.Mutable in project asterixdb by apache.

the class InvertedIndexAccessMethod method analyzeGetItemFuncExpr.

public boolean analyzeGetItemFuncExpr(AbstractFunctionCallExpression funcExpr, List<AbstractLogicalOperator> assignsAndUnnests, AccessMethodAnalysisContext analysisCtx) throws AlgebricksException {
    if (funcExpr.getFunctionIdentifier() != BuiltinFunctions.GET_ITEM) {
        return false;
    }
    ILogicalExpression arg1 = funcExpr.getArguments().get(0).getValue();
    ILogicalExpression arg2 = funcExpr.getArguments().get(1).getValue();
    // The second arg is the item index to be accessed. It must be a constant.
    if (arg2.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
        return false;
    }
    // If it is a variable we must track its origin in the assigns to get the original function expr.
    if (arg1.getExpressionTag() != LogicalExpressionTag.VARIABLE && arg1.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression matchedFuncExpr = null;
    // The get-item arg is function call, directly check if it's optimizable.
    if (arg1.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
        matchedFuncExpr = (AbstractFunctionCallExpression) arg1;
    }
    // The get-item arg is a variable. Search the assigns and unnests for its origination function.
    int matchedAssignOrUnnestIndex = -1;
    if (arg1.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
        VariableReferenceExpression varRefExpr = (VariableReferenceExpression) arg1;
        // Try to find variable ref expr in all assigns.
        for (int i = 0; i < assignsAndUnnests.size(); i++) {
            AbstractLogicalOperator op = assignsAndUnnests.get(i);
            if (op.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
                AssignOperator assign = (AssignOperator) op;
                List<LogicalVariable> assignVars = assign.getVariables();
                List<Mutable<ILogicalExpression>> assignExprs = assign.getExpressions();
                for (int j = 0; j < assignVars.size(); j++) {
                    LogicalVariable var = assignVars.get(j);
                    if (var != varRefExpr.getVariableReference()) {
                        continue;
                    }
                    // We've matched the variable in the first assign. Now analyze the originating function.
                    ILogicalExpression matchedExpr = assignExprs.get(j).getValue();
                    if (matchedExpr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
                        return false;
                    }
                    matchedFuncExpr = (AbstractFunctionCallExpression) matchedExpr;
                    break;
                }
            } else {
                UnnestOperator unnest = (UnnestOperator) op;
                LogicalVariable var = unnest.getVariable();
                if (var == varRefExpr.getVariableReference()) {
                    ILogicalExpression matchedExpr = unnest.getExpressionRef().getValue();
                    if (matchedExpr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
                        return false;
                    }
                    AbstractFunctionCallExpression unnestFuncExpr = (AbstractFunctionCallExpression) matchedExpr;
                    if (unnestFuncExpr.getFunctionIdentifier() != BuiltinFunctions.SCAN_COLLECTION) {
                        return false;
                    }
                    matchedFuncExpr = (AbstractFunctionCallExpression) unnestFuncExpr.getArguments().get(0).getValue();
                }
            }
            // We've already found a match.
            if (matchedFuncExpr != null) {
                matchedAssignOrUnnestIndex = i;
                break;
            }
        }
    }
    // Check that the matched function is optimizable by this access method.
    if (!secondLevelFuncIdents.contains(matchedFuncExpr.getFunctionIdentifier())) {
        return false;
    }
    boolean selectMatchFound = analyzeSelectSimilarityCheckFuncExprArgs(matchedFuncExpr, assignsAndUnnests, matchedAssignOrUnnestIndex, analysisCtx);
    boolean joinMatchFound = analyzeJoinSimilarityCheckFuncExprArgs(matchedFuncExpr, assignsAndUnnests, matchedAssignOrUnnestIndex, analysisCtx);
    if (selectMatchFound || joinMatchFound) {
        return true;
    }
    return false;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Mutable(org.apache.commons.lang3.mutable.Mutable) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)

Aggregations

Mutable (org.apache.commons.lang3.mutable.Mutable)213 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)167 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)150 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)139 ArrayList (java.util.ArrayList)123 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)109 MutableObject (org.apache.commons.lang3.mutable.MutableObject)76 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)68 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)65 Pair (org.apache.hyracks.algebricks.common.utils.Pair)62 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)58 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)58 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)48 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)39 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)31 HashSet (java.util.HashSet)28 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)28 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)27 IAType (org.apache.asterix.om.types.IAType)27 List (java.util.List)25