Search in sources :

Example 86 with MutableObject

use of org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.

the class TypeComputerTest method testTypeComputer.

private boolean testTypeComputer(Class<? extends IResultTypeComputer> c) throws Exception {
    // Mocks the type environment.
    IVariableTypeEnvironment mockTypeEnv = mock(IVariableTypeEnvironment.class);
    // Mocks the metadata provider.
    IMetadataProvider<?, ?> mockMetadataProvider = mock(IMetadataProvider.class);
    // Mocks function expression.
    AbstractFunctionCallExpression mockExpr = mock(AbstractFunctionCallExpression.class);
    FunctionIdentifier fid = mock(FunctionIdentifier.class);
    when(mockExpr.getFunctionIdentifier()).thenReturn(fid);
    when(fid.getName()).thenReturn("testFunction");
    // A function at most has six argument.
    List<Mutable<ILogicalExpression>> argRefs = new ArrayList<>();
    for (int argIndex = 0; argIndex < 6; ++argIndex) {
        ILogicalExpression mockArg = mock(ILogicalExpression.class);
        argRefs.add(new MutableObject<>(mockArg));
        when(mockTypeEnv.getType(mockArg)).thenReturn(BuiltinType.ANY);
    }
    // Sets up arguments for the mocked expression.
    when(mockExpr.getArguments()).thenReturn(argRefs);
    // Sets up required/actual types of the mocked expression.
    Object[] opaqueParameters = new Object[2];
    opaqueParameters[0] = BuiltinType.ANY;
    opaqueParameters[1] = BuiltinType.ANY;
    when(mockExpr.getOpaqueParameters()).thenReturn(opaqueParameters);
    // Tests the return type. It should be either ANY or NULLABLE/MISSABLE.
    IResultTypeComputer instance = (IResultTypeComputer) c.getField("INSTANCE").get(null);
    IAType resultType = instance.computeType(mockExpr, mockTypeEnv, mockMetadataProvider);
    ATypeTag typeTag = resultType.getTypeTag();
    if (typeTag == ATypeTag.ANY) {
        return true;
    }
    if (typeTag == ATypeTag.UNION) {
        AUnionType unionType = (AUnionType) resultType;
        return unionType.isMissableType() && unionType.isNullableType();
    }
    return false;
}
Also used : IResultTypeComputer(org.apache.asterix.om.typecomputer.base.IResultTypeComputer) AUnionType(org.apache.asterix.om.types.AUnionType) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ATypeTag(org.apache.asterix.om.types.ATypeTag) MutableObject(org.apache.commons.lang3.mutable.MutableObject) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment) IAType(org.apache.asterix.om.types.IAType)

Example 87 with MutableObject

use of org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.

the class EnforceStructuralPropertiesRule method addLocalEnforcers.

private void addLocalEnforcers(AbstractLogicalOperator op, int i, List<ILocalStructuralProperty> localProperties, boolean nestedPlan, IOptimizationContext context) throws AlgebricksException {
    if (AlgebricksConfig.DEBUG) {
        AlgebricksConfig.ALGEBRICKS_LOGGER.fine(">>>> Adding local enforcers for local props = " + localProperties + "\n");
    }
    if (localProperties == null || localProperties.isEmpty()) {
        return;
    }
    Mutable<ILogicalOperator> topOp = new MutableObject<>();
    topOp.setValue(op.getInputs().get(i).getValue());
    LinkedList<LocalOrderProperty> oList = new LinkedList<>();
    for (ILocalStructuralProperty prop : localProperties) {
        switch(prop.getPropertyType()) {
            case LOCAL_ORDER_PROPERTY:
                {
                    oList.add((LocalOrderProperty) prop);
                    break;
                }
            case LOCAL_GROUPING_PROPERTY:
                {
                    LocalGroupingProperty g = (LocalGroupingProperty) prop;
                    Collection<LogicalVariable> vars = (g.getPreferredOrderEnforcer() != null) ? g.getPreferredOrderEnforcer() : g.getColumnSet();
                    List<OrderColumn> orderColumns = new ArrayList<>();
                    for (LogicalVariable v : vars) {
                        OrderColumn oc = new OrderColumn(v, OrderKind.ASC);
                        orderColumns.add(oc);
                    }
                    LocalOrderProperty lop = new LocalOrderProperty(orderColumns);
                    oList.add(lop);
                    break;
                }
            default:
                {
                    throw new IllegalStateException();
                }
        }
    }
    if (!oList.isEmpty()) {
        topOp = enforceOrderProperties(oList, topOp, nestedPlan, context);
    }
    op.getInputs().set(i, topOp);
    OperatorPropertiesUtil.computeSchemaAndPropertiesRecIfNull((AbstractLogicalOperator) topOp.getValue(), context);
    OperatorManipulationUtil.setOperatorMode(op);
    printOp((AbstractLogicalOperator) topOp.getValue());
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) LocalGroupingProperty(org.apache.hyracks.algebricks.core.algebra.properties.LocalGroupingProperty) OrderColumn(org.apache.hyracks.algebricks.core.algebra.properties.OrderColumn) LinkedList(java.util.LinkedList) LocalOrderProperty(org.apache.hyracks.algebricks.core.algebra.properties.LocalOrderProperty) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ILocalStructuralProperty(org.apache.hyracks.algebricks.core.algebra.properties.ILocalStructuralProperty) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 88 with MutableObject

use of org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.

the class EnforceStructuralPropertiesRule method enforceOrderProperties.

private Mutable<ILogicalOperator> enforceOrderProperties(List<LocalOrderProperty> oList, Mutable<ILogicalOperator> topOp, boolean isMicroOp, IOptimizationContext context) throws AlgebricksException {
    List<Pair<IOrder, Mutable<ILogicalExpression>>> oe = new LinkedList<>();
    for (LocalOrderProperty orderProperty : oList) {
        for (OrderColumn oc : orderProperty.getOrderColumns()) {
            IOrder ordType = (oc.getOrder() == OrderKind.ASC) ? OrderOperator.ASC_ORDER : OrderOperator.DESC_ORDER;
            Pair<IOrder, Mutable<ILogicalExpression>> pair = new Pair<>(ordType, new MutableObject<ILogicalExpression>(new VariableReferenceExpression(oc.getColumn())));
            oe.add(pair);
        }
    }
    OrderOperator oo = new OrderOperator(oe);
    oo.setExecutionMode(AbstractLogicalOperator.ExecutionMode.LOCAL);
    if (isMicroOp) {
        oo.setPhysicalOperator(new InMemoryStableSortPOperator());
    } else {
        oo.setPhysicalOperator(new StableSortPOperator(physicalOptimizationConfig.getMaxFramesExternalSort()));
    }
    oo.getInputs().add(topOp);
    context.computeAndSetTypeEnvironmentForOperator(oo);
    if (AlgebricksConfig.DEBUG) {
        AlgebricksConfig.ALGEBRICKS_LOGGER.fine(">>>> Added sort enforcer " + oo.getPhysicalOperator() + ".\n");
    }
    return new MutableObject<ILogicalOperator>(oo);
}
Also used : IOrder(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator.IOrder) OrderColumn(org.apache.hyracks.algebricks.core.algebra.properties.OrderColumn) OrderOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator) InMemoryStableSortPOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.InMemoryStableSortPOperator) LinkedList(java.util.LinkedList) 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) LocalOrderProperty(org.apache.hyracks.algebricks.core.algebra.properties.LocalOrderProperty) InMemoryStableSortPOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.InMemoryStableSortPOperator) AbstractStableSortPOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.AbstractStableSortPOperator) StableSortPOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.StableSortPOperator) Pair(org.apache.hyracks.algebricks.common.utils.Pair) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 89 with MutableObject

use of org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.

the class ExtractCommonOperatorsRule method rewriteForOneEquivalentClass.

private boolean rewriteForOneEquivalentClass(List<Mutable<ILogicalOperator>> members, IOptimizationContext context) throws AlgebricksException {
    List<Mutable<ILogicalOperator>> group = new ArrayList<Mutable<ILogicalOperator>>();
    boolean rewritten = false;
    while (members.size() > 0) {
        group.clear();
        Mutable<ILogicalOperator> candidate = members.remove(members.size() - 1);
        group.add(candidate);
        for (int i = members.size() - 1; i >= 0; i--) {
            Mutable<ILogicalOperator> peer = members.get(i);
            if (IsomorphismUtilities.isOperatorIsomorphic(candidate.getValue(), peer.getValue())) {
                group.add(peer);
                members.remove(i);
            }
        }
        boolean[] materializationFlags = computeMaterilizationFlags(group);
        if (group.isEmpty()) {
            continue;
        }
        candidate = group.get(0);
        ReplicateOperator rop = new ReplicateOperator(group.size(), materializationFlags);
        rop.setPhysicalOperator(new ReplicatePOperator());
        Mutable<ILogicalOperator> ropRef = new MutableObject<ILogicalOperator>(rop);
        AbstractLogicalOperator aopCandidate = (AbstractLogicalOperator) candidate.getValue();
        List<Mutable<ILogicalOperator>> originalCandidateParents = childrenToParents.get(candidate);
        rop.setExecutionMode(((AbstractLogicalOperator) candidate.getValue()).getExecutionMode());
        if (aopCandidate.getOperatorTag() == LogicalOperatorTag.EXCHANGE) {
            rop.getInputs().add(candidate);
        } else {
            AbstractLogicalOperator beforeExchange = new ExchangeOperator();
            beforeExchange.setPhysicalOperator(new OneToOneExchangePOperator());
            beforeExchange.setExecutionMode(rop.getExecutionMode());
            Mutable<ILogicalOperator> beforeExchangeRef = new MutableObject<ILogicalOperator>(beforeExchange);
            beforeExchange.getInputs().add(candidate);
            context.computeAndSetTypeEnvironmentForOperator(beforeExchange);
            rop.getInputs().add(beforeExchangeRef);
        }
        context.computeAndSetTypeEnvironmentForOperator(rop);
        for (Mutable<ILogicalOperator> parentRef : originalCandidateParents) {
            AbstractLogicalOperator parent = (AbstractLogicalOperator) parentRef.getValue();
            int index = parent.getInputs().indexOf(candidate);
            if (parent.getOperatorTag() == LogicalOperatorTag.EXCHANGE) {
                parent.getInputs().set(index, ropRef);
                rop.getOutputs().add(parentRef);
            } else {
                AbstractLogicalOperator exchange = new ExchangeOperator();
                exchange.setPhysicalOperator(new OneToOneExchangePOperator());
                exchange.setExecutionMode(rop.getExecutionMode());
                MutableObject<ILogicalOperator> exchangeRef = new MutableObject<ILogicalOperator>(exchange);
                exchange.getInputs().add(ropRef);
                rop.getOutputs().add(exchangeRef);
                context.computeAndSetTypeEnvironmentForOperator(exchange);
                parent.getInputs().set(index, exchangeRef);
                context.computeAndSetTypeEnvironmentForOperator(parent);
            }
        }
        List<LogicalVariable> liveVarsNew = new ArrayList<LogicalVariable>();
        VariableUtilities.getLiveVariables(candidate.getValue(), liveVarsNew);
        ArrayList<Mutable<ILogicalExpression>> assignExprs = new ArrayList<Mutable<ILogicalExpression>>();
        for (LogicalVariable liveVar : liveVarsNew) {
            assignExprs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(liveVar)));
        }
        for (Mutable<ILogicalOperator> ref : group) {
            if (ref.equals(candidate)) {
                continue;
            }
            ArrayList<LogicalVariable> liveVars = new ArrayList<LogicalVariable>();
            Map<LogicalVariable, LogicalVariable> variableMappingBack = new HashMap<LogicalVariable, LogicalVariable>();
            IsomorphismUtilities.mapVariablesTopDown(ref.getValue(), candidate.getValue(), variableMappingBack);
            for (int i = 0; i < liveVarsNew.size(); i++) {
                liveVars.add(variableMappingBack.get(liveVarsNew.get(i)));
            }
            AbstractLogicalOperator assignOperator = new AssignOperator(liveVars, assignExprs);
            assignOperator.setExecutionMode(rop.getExecutionMode());
            assignOperator.setPhysicalOperator(new AssignPOperator());
            AbstractLogicalOperator projectOperator = new ProjectOperator(liveVars);
            projectOperator.setPhysicalOperator(new StreamProjectPOperator());
            projectOperator.setExecutionMode(rop.getExecutionMode());
            AbstractLogicalOperator exchOp = new ExchangeOperator();
            exchOp.setPhysicalOperator(new OneToOneExchangePOperator());
            exchOp.setExecutionMode(rop.getExecutionMode());
            exchOp.getInputs().add(ropRef);
            MutableObject<ILogicalOperator> exchOpRef = new MutableObject<ILogicalOperator>(exchOp);
            rop.getOutputs().add(exchOpRef);
            assignOperator.getInputs().add(exchOpRef);
            projectOperator.getInputs().add(new MutableObject<ILogicalOperator>(assignOperator));
            // set the types
            context.computeAndSetTypeEnvironmentForOperator(exchOp);
            context.computeAndSetTypeEnvironmentForOperator(assignOperator);
            context.computeAndSetTypeEnvironmentForOperator(projectOperator);
            List<Mutable<ILogicalOperator>> parentOpList = childrenToParents.get(ref);
            for (Mutable<ILogicalOperator> parentOpRef : parentOpList) {
                AbstractLogicalOperator parentOp = (AbstractLogicalOperator) parentOpRef.getValue();
                int index = parentOp.getInputs().indexOf(ref);
                ILogicalOperator childOp = parentOp.getOperatorTag() == LogicalOperatorTag.PROJECT ? assignOperator : projectOperator;
                if (!HeuristicOptimizer.isHyracksOp(parentOp.getPhysicalOperator().getOperatorTag())) {
                    parentOp.getInputs().set(index, new MutableObject<ILogicalOperator>(childOp));
                } else {
                    // If the parent operator is a hyracks operator,
                    // an extra one-to-one exchange is needed.
                    AbstractLogicalOperator exchg = new ExchangeOperator();
                    exchg.setPhysicalOperator(new OneToOneExchangePOperator());
                    exchg.setExecutionMode(childOp.getExecutionMode());
                    exchg.getInputs().add(new MutableObject<ILogicalOperator>(childOp));
                    parentOp.getInputs().set(index, new MutableObject<ILogicalOperator>(exchg));
                    context.computeAndSetTypeEnvironmentForOperator(exchg);
                }
                context.computeAndSetTypeEnvironmentForOperator(parentOp);
            }
        }
        rewritten = true;
    }
    return rewritten;
}
Also used : OneToOneExchangePOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.OneToOneExchangePOperator) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExchangeOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.ExchangeOperator) MutableObject(org.apache.commons.lang3.mutable.MutableObject) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) StreamProjectPOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.StreamProjectPOperator) ReplicateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.ReplicateOperator) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ProjectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.ProjectOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) AssignPOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.AssignPOperator) 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) ReplicatePOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.ReplicatePOperator)

Example 90 with MutableObject

use of org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.

the class IntroJoinInsideSubplanRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op0 = (AbstractLogicalOperator) opRef.getValue();
    if (op0.getOperatorTag() != LogicalOperatorTag.SUBPLAN) {
        return false;
    }
    SubplanOperator subplan = (SubplanOperator) op0;
    Mutable<ILogicalOperator> leftRef = subplan.getInputs().get(0);
    if (((AbstractLogicalOperator) leftRef.getValue()).getOperatorTag() == LogicalOperatorTag.EMPTYTUPLESOURCE) {
        return false;
    }
    ListIterator<ILogicalPlan> plansIter = subplan.getNestedPlans().listIterator();
    ILogicalPlan p = null;
    while (plansIter.hasNext()) {
        p = plansIter.next();
    }
    if (p == null) {
        return false;
    }
    if (p.getRoots().size() != 1) {
        return false;
    }
    Mutable<ILogicalOperator> opRef1 = p.getRoots().get(0);
    while (true) {
        AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef1.getValue();
        if (op1.getInputs().size() != 1) {
            return false;
        }
        if (op1.getOperatorTag() == LogicalOperatorTag.SELECT) {
            Mutable<ILogicalOperator> op2Ref = op1.getInputs().get(0);
            AbstractLogicalOperator op2 = (AbstractLogicalOperator) op2Ref.getValue();
            if (op2.getOperatorTag() != LogicalOperatorTag.SELECT && descOrSelfIsScanOrJoin(op2)) {
                Set<LogicalVariable> free2 = new HashSet<LogicalVariable>();
                OperatorPropertiesUtil.getFreeVariablesInSelfOrDesc(op2, free2);
                if (free2.isEmpty()) {
                    Set<LogicalVariable> free1 = new HashSet<LogicalVariable>();
                    OperatorPropertiesUtil.getFreeVariablesInSelfOrDesc(op1, free1);
                    if (!free1.isEmpty()) {
                        OperatorManipulationUtil.ntsToEts(op2Ref, context);
                        NestedTupleSourceOperator nts = new NestedTupleSourceOperator(new MutableObject<ILogicalOperator>(subplan));
                        Mutable<ILogicalOperator> ntsRef = new MutableObject<ILogicalOperator>(nts);
                        Mutable<ILogicalOperator> innerRef = new MutableObject<ILogicalOperator>(op2);
                        InnerJoinOperator join = new InnerJoinOperator(new MutableObject<ILogicalExpression>(ConstantExpression.TRUE), ntsRef, innerRef);
                        op2Ref.setValue(join);
                        context.computeAndSetTypeEnvironmentForOperator(nts);
                        context.computeAndSetTypeEnvironmentForOperator(join);
                        return true;
                    }
                }
            }
        }
        opRef1 = op1.getInputs().get(0);
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) NestedTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) InnerJoinOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.InnerJoinOperator) SubplanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) HashSet(java.util.HashSet) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Aggregations

MutableObject (org.apache.commons.lang3.mutable.MutableObject)111 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)60 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)58 ArrayList (java.util.ArrayList)55 Mutable (org.apache.commons.lang3.mutable.Mutable)55 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)52 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)49 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)28 Pair (org.apache.hyracks.algebricks.common.utils.Pair)27 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)23 List (java.util.List)22 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)21 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)18 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)17 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)14 Test (org.junit.Test)14 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)13 UnnestingFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression)13 NestedTupleSourceOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator)13 HashSet (java.util.HashSet)12