Search in sources :

Example 86 with MutableObject

use of org.apache.commons.lang3.mutable.MutableObject in project AgriCraft by AgriCraft.

the class AgriClocheRecipe method getRecipeOutput.

@Override
public ItemStack getRecipeOutput() {
    MutableObject<ItemStack> product = new MutableObject<>();
    this.getSeed().getPlant().getAllPossibleProducts(stack -> {
        if (product.getValue() == null) {
            product.setValue(stack);
        }
    });
    ItemStack result = product.getValue();
    return result == null ? ItemStack.EMPTY : result;
}
Also used : ItemStack(net.minecraft.item.ItemStack) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 87 with MutableObject

use of org.apache.commons.lang3.mutable.MutableObject in project hive by apache.

the class TestReflectionObjectInspectors method testObjectInspectorThreadSafety.

@Test
public void testObjectInspectorThreadSafety() throws InterruptedException {
    // 5 workers to run getReflectionObjectInspector concurrently
    final int workerCount = 5;
    final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(workerCount);
    final MutableObject exception = new MutableObject();
    Thread runner = new Thread(new Runnable() {

        @Override
        @SuppressWarnings("unchecked")
        public void run() {
            Future<ObjectInspector>[] results = (Future<ObjectInspector>[]) new Future[workerCount];
            Pair<Type, ObjectInspectorFactory.ObjectInspectorOptions>[] types = (Pair<Type, ObjectInspectorFactory.ObjectInspectorOptions>[]) new Pair[] { Pair.of(Complex.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT), Pair.of(MyStruct.class, ObjectInspectorFactory.ObjectInspectorOptions.JAVA) };
            try {
                for (int i = 0; i < 20; i++) {
                    // repeat 20 times
                    for (final Pair<Type, ObjectInspectorFactory.ObjectInspectorOptions> t : types) {
                        ObjectInspectorFactory.objectInspectorCache.asMap().clear();
                        for (int k = 0; k < workerCount; k++) {
                            results[k] = executorService.schedule(new Callable<ObjectInspector>() {

                                @Override
                                public ObjectInspector call() throws Exception {
                                    return ObjectInspectorFactory.getReflectionObjectInspector(t.getLeft(), t.getRight());
                                }
                            }, 50, TimeUnit.MILLISECONDS);
                        }
                        ObjectInspector oi = results[0].get();
                        for (int k = 1; k < workerCount; k++) {
                            assertEquals(oi, results[k].get());
                        }
                    }
                }
            } catch (Throwable e) {
                exception.setValue(e);
            }
        }
    });
    try {
        runner.start();
        // timeout in 5 minutes
        long endTime = System.currentTimeMillis() + 300000;
        while (runner.isAlive()) {
            if (System.currentTimeMillis() > endTime) {
                // Interrupt the runner thread
                runner.interrupt();
                fail("Timed out waiting for the runner to finish");
            }
            runner.join(10000);
        }
        if (exception.getValue() != null) {
            fail("Got exception: " + exception.getValue());
        }
    } finally {
        executorService.shutdownNow();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) JavaConstantStringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaConstantStringObjectInspector) Type(java.lang.reflect.Type) Future(java.util.concurrent.Future) PrimitiveObjectInspectorFactory(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 88 with MutableObject

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

the class PushSubplanIntoGroupByRule method rewritePre.

@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    ILogicalOperator parentOperator = opRef.getValue();
    if (context.checkIfInDontApplySet(this, parentOperator)) {
        return false;
    }
    context.addToDontApplySet(this, parentOperator);
    VariableUtilities.getUsedVariables(parentOperator, usedVarsSoFar);
    if (parentOperator.getInputs().size() <= 0) {
        return false;
    }
    boolean changed = false;
    GroupByOperator gby = null;
    for (Mutable<ILogicalOperator> ref : parentOperator.getInputs()) {
        AbstractLogicalOperator op = (AbstractLogicalOperator) ref.getValue();
        /** Only processes subplan operator. */
        List<SubplanOperator> subplans = new ArrayList<SubplanOperator>();
        if (op.getOperatorTag() == LogicalOperatorTag.SUBPLAN) {
            while (op.getOperatorTag() == LogicalOperatorTag.SUBPLAN) {
                SubplanOperator currentSubplan = (SubplanOperator) op;
                subplans.add(currentSubplan);
                op = (AbstractLogicalOperator) op.getInputs().get(0).getValue();
            }
            /** Only processes the case a group-by operator is the input of the subplan operators. */
            if (op.getOperatorTag() == LogicalOperatorTag.GROUP) {
                gby = (GroupByOperator) op;
                List<ILogicalPlan> newGbyNestedPlans = new ArrayList<ILogicalPlan>();
                for (SubplanOperator subplan : subplans) {
                    List<ILogicalPlan> subplanNestedPlans = subplan.getNestedPlans();
                    List<ILogicalPlan> gbyNestedPlans = gby.getNestedPlans();
                    List<ILogicalPlan> subplanNestedPlansToRemove = new ArrayList<ILogicalPlan>();
                    for (ILogicalPlan subplanNestedPlan : subplanNestedPlans) {
                        List<Mutable<ILogicalOperator>> rootOpRefs = subplanNestedPlan.getRoots();
                        List<Mutable<ILogicalOperator>> rootOpRefsToRemove = new ArrayList<Mutable<ILogicalOperator>>();
                        for (Mutable<ILogicalOperator> rootOpRef : rootOpRefs) {
                            /** Gets free variables in the root operator of a nested plan and its descent. */
                            Set<LogicalVariable> freeVars = new ListSet<LogicalVariable>();
                            VariableUtilities.getUsedVariablesInDescendantsAndSelf(rootOpRef.getValue(), freeVars);
                            Set<LogicalVariable> producedVars = new ListSet<LogicalVariable>();
                            VariableUtilities.getProducedVariablesInDescendantsAndSelf(rootOpRef.getValue(), producedVars);
                            freeVars.removeAll(producedVars);
                            /** * Checks whether the above freeVars are all contained in live variables * of one nested plan inside the group-by operator. * If yes, then the subplan can be pushed into the nested plan of the group-by. */
                            for (ILogicalPlan gbyNestedPlanOriginal : gbyNestedPlans) {
                                // add a subplan in the original gby
                                if (!newGbyNestedPlans.contains(gbyNestedPlanOriginal)) {
                                    newGbyNestedPlans.add(gbyNestedPlanOriginal);
                                }
                                // add a pushed subplan
                                ILogicalPlan gbyNestedPlan = OperatorManipulationUtil.deepCopy(gbyNestedPlanOriginal, context);
                                List<Mutable<ILogicalOperator>> gbyRootOpRefs = gbyNestedPlan.getRoots();
                                for (int rootIndex = 0; rootIndex < gbyRootOpRefs.size(); rootIndex++) {
                                    //set the nts for a original subplan
                                    Mutable<ILogicalOperator> originalGbyRootOpRef = gbyNestedPlanOriginal.getRoots().get(rootIndex);
                                    Mutable<ILogicalOperator> originalGbyNtsRef = downToNts(originalGbyRootOpRef);
                                    NestedTupleSourceOperator originalNts = (NestedTupleSourceOperator) originalGbyNtsRef.getValue();
                                    originalNts.setDataSourceReference(new MutableObject<ILogicalOperator>(gby));
                                    //push a new subplan if possible
                                    Mutable<ILogicalOperator> gbyRootOpRef = gbyRootOpRefs.get(rootIndex);
                                    Set<LogicalVariable> liveVars = new ListSet<LogicalVariable>();
                                    VariableUtilities.getLiveVariables(gbyRootOpRef.getValue(), liveVars);
                                    if (liveVars.containsAll(freeVars)) {
                                        /** Does the actual push. */
                                        Mutable<ILogicalOperator> ntsRef = downToNts(rootOpRef);
                                        ntsRef.setValue(gbyRootOpRef.getValue());
                                        // Removes unused vars.
                                        AggregateOperator aggOp = (AggregateOperator) gbyRootOpRef.getValue();
                                        for (int varIndex = aggOp.getVariables().size() - 1; varIndex >= 0; varIndex--) {
                                            if (!freeVars.contains(aggOp.getVariables().get(varIndex))) {
                                                aggOp.getVariables().remove(varIndex);
                                                aggOp.getExpressions().remove(varIndex);
                                            }
                                        }
                                        gbyRootOpRef.setValue(rootOpRef.getValue());
                                        rootOpRefsToRemove.add(rootOpRef);
                                        // Sets the nts for a new pushed plan.
                                        Mutable<ILogicalOperator> oldGbyNtsRef = downToNts(gbyRootOpRef);
                                        NestedTupleSourceOperator nts = (NestedTupleSourceOperator) oldGbyNtsRef.getValue();
                                        nts.setDataSourceReference(new MutableObject<ILogicalOperator>(gby));
                                        newGbyNestedPlans.add(gbyNestedPlan);
                                        changed = true;
                                        continue;
                                    }
                                }
                            }
                        }
                        rootOpRefs.removeAll(rootOpRefsToRemove);
                        if (rootOpRefs.size() == 0) {
                            subplanNestedPlansToRemove.add(subplanNestedPlan);
                        }
                    }
                    subplanNestedPlans.removeAll(subplanNestedPlansToRemove);
                }
                if (changed) {
                    ref.setValue(gby);
                    gby.getNestedPlans().clear();
                    gby.getNestedPlans().addAll(newGbyNestedPlans);
                }
            }
        }
    }
    if (changed) {
        cleanup(gby);
        context.computeAndSetTypeEnvironmentForOperator(gby);
        context.computeAndSetTypeEnvironmentForOperator(parentOperator);
    }
    return changed;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) NestedTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) SubplanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) ListSet(org.apache.hyracks.algebricks.common.utils.ListSet) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)

Example 89 with MutableObject

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

the class SetAlgebricksPhysicalOperatorsRule method generateMergeAggregationExpressions.

private static boolean generateMergeAggregationExpressions(GroupByOperator gby, IOptimizationContext context) throws AlgebricksException {
    if (gby.getNestedPlans().size() != 1) {
        //an aggregate and a nested-tuple-source.
        throw new AlgebricksException("External group-by currently works only for one nested plan with one root containing" + "an aggregate and a nested-tuple-source.");
    }
    ILogicalPlan p0 = gby.getNestedPlans().get(0);
    if (p0.getRoots().size() != 1) {
        //an aggregate and a nested-tuple-source.
        throw new AlgebricksException("External group-by currently works only for one nested plan with one root containing" + "an aggregate and a nested-tuple-source.");
    }
    IMergeAggregationExpressionFactory mergeAggregationExpressionFactory = context.getMergeAggregationExpressionFactory();
    Mutable<ILogicalOperator> r0 = p0.getRoots().get(0);
    AbstractLogicalOperator r0Logical = (AbstractLogicalOperator) r0.getValue();
    if (r0Logical.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
        return false;
    }
    // Check whether there are multiple aggregates in the sub plan.
    ILogicalOperator r1Logical = r0Logical;
    while (r1Logical.hasInputs()) {
        r1Logical = r1Logical.getInputs().get(0).getValue();
        if (r1Logical.getOperatorTag() == LogicalOperatorTag.AGGREGATE) {
            return false;
        }
    }
    AggregateOperator aggOp = (AggregateOperator) r0.getValue();
    List<Mutable<ILogicalExpression>> aggFuncRefs = aggOp.getExpressions();
    List<LogicalVariable> originalAggVars = aggOp.getVariables();
    int n = aggOp.getExpressions().size();
    List<Mutable<ILogicalExpression>> mergeExpressionRefs = new ArrayList<Mutable<ILogicalExpression>>();
    for (int i = 0; i < n; i++) {
        ILogicalExpression mergeExpr = mergeAggregationExpressionFactory.createMergeAggregation(originalAggVars.get(i), aggFuncRefs.get(i).getValue(), context);
        if (mergeExpr == null) {
            return false;
        }
        mergeExpressionRefs.add(new MutableObject<ILogicalExpression>(mergeExpr));
    }
    aggOp.setMergeExpressions(mergeExpressionRefs);
    return true;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) IMergeAggregationExpressionFactory(org.apache.hyracks.algebricks.core.algebra.expressions.IMergeAggregationExpressionFactory) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) ArrayList(java.util.ArrayList) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)

Example 90 with MutableObject

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

the class IntroduceGroupByForSubplanRule method buildVarExprList.

private Map<LogicalVariable, LogicalVariable> buildVarExprList(Collection<LogicalVariable> vars, IOptimizationContext context, GroupByOperator g, List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> outVeList) throws AlgebricksException {
    Map<LogicalVariable, LogicalVariable> m = new HashMap<LogicalVariable, LogicalVariable>();
    for (LogicalVariable ov : vars) {
        LogicalVariable newVar = context.newVar();
        ILogicalExpression varExpr = new VariableReferenceExpression(newVar);
        outVeList.add(new Pair<LogicalVariable, Mutable<ILogicalExpression>>(ov, new MutableObject<ILogicalExpression>(varExpr)));
        for (ILogicalPlan p : g.getNestedPlans()) {
            for (Mutable<ILogicalOperator> r : p.getRoots()) {
                OperatorManipulationUtil.substituteVarRec((AbstractLogicalOperator) r.getValue(), ov, newVar, true, context);
            }
        }
        AbstractLogicalOperator opUnder = (AbstractLogicalOperator) g.getInputs().get(0).getValue();
        OperatorManipulationUtil.substituteVarRec(opUnder, ov, newVar, true, context);
        m.put(ov, newVar);
    }
    return m;
}
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) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) HashMap(java.util.HashMap) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Aggregations

Mutable (org.apache.commons.lang3.mutable.Mutable)119 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)113 MutableObject (org.apache.commons.lang3.mutable.MutableObject)111 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)101 ArrayList (java.util.ArrayList)93 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)91 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)78 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)54 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)49 Pair (org.apache.hyracks.algebricks.common.utils.Pair)47 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)46 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)35 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)29 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)26 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)25 List (java.util.List)23 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)23 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)23 UnnestingFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression)23 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)22