List of usage examples for org.apache.commons.lang3.mutable Mutable setValue
void setValue(T value);
From source file:edu.uci.ics.hyracks.algebricks.rewriter.rules.EliminateSubplanRule.java
private void elimSubplanOverEts(Mutable<ILogicalOperator> opRef, IOptimizationContext ctx) throws AlgebricksException { SubplanOperator subplan = (SubplanOperator) opRef.getValue(); for (ILogicalPlan p : subplan.getNestedPlans()) { for (Mutable<ILogicalOperator> r : p.getRoots()) { OperatorManipulationUtil.ntsToEts(r, ctx); }// w ww .j a v a 2 s.c om } LinkedList<Mutable<ILogicalOperator>> allRoots = subplan.allRootsInReverseOrder(); if (allRoots.size() == 1) { opRef.setValue(allRoots.get(0).getValue()); } else { ILogicalOperator topOp = null; for (Mutable<ILogicalOperator> r : allRoots) { if (topOp == null) { topOp = r.getValue(); } else { LeftOuterJoinOperator j = new LeftOuterJoinOperator( new MutableObject<ILogicalExpression>(ConstantExpression.TRUE)); j.getInputs().add(new MutableObject<ILogicalOperator>(topOp)); j.getInputs().add(r); ctx.setOutputTypeEnvironment(j, j.computeOutputTypeEnvironment(ctx)); topOp = j; } } opRef.setValue(topOp); } }
From source file:edu.uci.ics.asterix.optimizer.rules.PushFieldAccessRule.java
private void pushAccessDown(Mutable<ILogicalOperator> fldAccessOpRef, ILogicalOperator op2, Mutable<ILogicalOperator> inputOfOp2, IOptimizationContext context, String finalAnnot) throws AlgebricksException { ILogicalOperator fieldAccessOp = fldAccessOpRef.getValue(); fldAccessOpRef.setValue(op2); List<Mutable<ILogicalOperator>> faInpList = fieldAccessOp.getInputs(); faInpList.clear();// www .j av a2 s. c om faInpList.add(new MutableObject<ILogicalOperator>(inputOfOp2.getValue())); inputOfOp2.setValue(fieldAccessOp); // typing context.computeAndSetTypeEnvironmentForOperator(fieldAccessOp); context.computeAndSetTypeEnvironmentForOperator(op2); propagateFieldAccessRec(inputOfOp2, context, finalAnnot); }
From source file:edu.uci.ics.hyracks.algebricks.rewriter.rules.EnforceStructuralPropertiesRule.java
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"); }// w w w.jav a2 s . c o m if (localProperties == null || localProperties.isEmpty()) { return; } Mutable<ILogicalOperator> topOp = new MutableObject<ILogicalOperator>(); topOp.setValue(op.getInputs().get(i).getValue()); LinkedList<LocalOrderProperty> oList = new LinkedList<LocalOrderProperty>(); 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<OrderColumn>(); 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); printOp((AbstractLogicalOperator) topOp.getValue()); }
From source file:edu.uci.ics.hyracks.algebricks.rewriter.rules.RemoveUnusedAssignAndAggregateRule.java
private void removeUnusedAssigns(Mutable<ILogicalOperator> opRef, Set<LogicalVariable> toRemove, IOptimizationContext context) throws AlgebricksException { AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue(); while (removeFromAssigns(op, toRemove, context) == 0) { if (op.getOperatorTag() == LogicalOperatorTag.AGGREGATE) { break; }/*from w ww. j a va2 s. c o m*/ op = (AbstractLogicalOperator) op.getInputs().get(0).getValue(); opRef.setValue(op); } Iterator<Mutable<ILogicalOperator>> childIter = op.getInputs().iterator(); while (childIter.hasNext()) { Mutable<ILogicalOperator> cRef = childIter.next(); removeUnusedAssigns(cRef, toRemove, context); } if (op.hasNestedPlans()) { AbstractOperatorWithNestedPlans opWithNest = (AbstractOperatorWithNestedPlans) op; Iterator<ILogicalPlan> planIter = opWithNest.getNestedPlans().iterator(); while (planIter.hasNext()) { ILogicalPlan p = planIter.next(); for (Mutable<ILogicalOperator> r : p.getRoots()) { removeUnusedAssigns(r, toRemove, context); } } // Removes redundant nested plans that produces nothing for (int i = opWithNest.getNestedPlans().size() - 1; i >= 0; i--) { ILogicalPlan nestedPlan = opWithNest.getNestedPlans().get(i); List<Mutable<ILogicalOperator>> rootsToBeRemoved = new ArrayList<Mutable<ILogicalOperator>>(); for (Mutable<ILogicalOperator> r : nestedPlan.getRoots()) { ILogicalOperator topOp = r.getValue(); Set<LogicalVariable> producedVars = new ListSet<LogicalVariable>(); VariableUtilities.getProducedVariablesInDescendantsAndSelf(topOp, producedVars); if (producedVars.size() == 0) { rootsToBeRemoved.add(r); } } // Makes sure the operator should have at least ONE nested plan even it is empty // (because a lot of places uses this assumption, TODO(yingyib): clean them up). if (nestedPlan.getRoots().size() == rootsToBeRemoved.size() && opWithNest.getNestedPlans().size() > 1) { nestedPlan.getRoots().removeAll(rootsToBeRemoved); opWithNest.getNestedPlans().remove(nestedPlan); } } } }
From source file:edu.uci.ics.asterix.optimizer.rules.ExtractFunctionsFromJoinConditionRule.java
private boolean assignFunctionExpressions(AbstractLogicalOperator joinOp, ILogicalExpression expr, IOptimizationContext context) throws AlgebricksException { if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) { return false; }//from w w w . j a va2 s . com AbstractFunctionCallExpression fexp = (AbstractFunctionCallExpression) expr; FunctionIdentifier fi = fexp.getFunctionIdentifier(); boolean modified = false; if (fi.equals(AlgebricksBuiltinFunctions.AND) || fi.equals(AlgebricksBuiltinFunctions.OR) || fi.equals(AsterixBuiltinFunctions.GET_ITEM)) { for (Mutable<ILogicalExpression> a : fexp.getArguments()) { if (assignFunctionExpressions(joinOp, a.getValue(), context)) { modified = true; } } return modified; } else if (AlgebricksBuiltinFunctions.isComparisonFunction(fi) || AsterixBuiltinFunctions.isSimilarityFunction(fi)) { for (Mutable<ILogicalExpression> exprRef : fexp.getArguments()) { if (exprRef.getValue().getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) { LogicalVariable newVar = context.newVar(); AssignOperator newAssign = new AssignOperator(newVar, new MutableObject<ILogicalExpression>(exprRef.getValue().cloneExpression())); newAssign.setExecutionMode(joinOp.getExecutionMode()); // Place assign below joinOp. List<LogicalVariable> used = new ArrayList<LogicalVariable>(); VariableUtilities.getUsedVariables(newAssign, used); Mutable<ILogicalOperator> leftBranchRef = joinOp.getInputs().get(0); ILogicalOperator leftBranch = leftBranchRef.getValue(); List<LogicalVariable> leftBranchVariables = new ArrayList<LogicalVariable>(); VariableUtilities.getLiveVariables(leftBranch, leftBranchVariables); if (leftBranchVariables.containsAll(used)) { // place assign on left branch newAssign.getInputs().add(new MutableObject<ILogicalOperator>(leftBranch)); leftBranchRef.setValue(newAssign); modified = true; } else { Mutable<ILogicalOperator> rightBranchRef = joinOp.getInputs().get(1); ILogicalOperator rightBranch = rightBranchRef.getValue(); List<LogicalVariable> rightBranchVariables = new ArrayList<LogicalVariable>(); VariableUtilities.getLiveVariables(rightBranch, rightBranchVariables); if (rightBranchVariables.containsAll(used)) { // place assign on right branch newAssign.getInputs().add(new MutableObject<ILogicalOperator>(rightBranch)); rightBranchRef.setValue(newAssign); modified = true; } } if (modified) { // Replace original expr with variable reference. exprRef.setValue(new VariableReferenceExpression(newVar)); context.computeAndSetTypeEnvironmentForOperator(newAssign); context.computeAndSetTypeEnvironmentForOperator(joinOp); } } } return modified; } else { return false; } }
From source file:edu.uci.ics.asterix.optimizer.rules.PushAggregateIntoGroupbyRule.java
/** * @param expr/*from w w w . j av a2s. c o m*/ * @param aggVars * @param gbyWithAgg * @param context * @return a pair whose first member is a boolean which is true iff * something was changed in the expression tree rooted at expr. The * second member is the result of transforming expr. * @throws AlgebricksException */ private Pair<Boolean, ILogicalExpression> extractAggFunctionsFromExpression(Mutable<ILogicalExpression> exprRef, Map<LogicalVariable, GroupByOperator> gbyWithAgg, Map<ILogicalExpression, ILogicalExpression> aggregateExprToVarExpr, IOptimizationContext context) throws AlgebricksException { ILogicalExpression expr = exprRef.getValue(); switch (expr.getExpressionTag()) { case FUNCTION_CALL: { AbstractFunctionCallExpression fce = (AbstractFunctionCallExpression) expr; FunctionIdentifier fi = AsterixBuiltinFunctions.getAggregateFunction(fce.getFunctionIdentifier()); if (fi != null) { ILogicalExpression a1 = fce.getArguments().get(0).getValue(); if (a1.getExpressionTag() == LogicalExpressionTag.VARIABLE) { LogicalVariable argVar = ((VariableReferenceExpression) a1).getVariableReference(); GroupByOperator gbyOp = gbyWithAgg.get(argVar); if (gbyOp != null) { if (!aggregateExprToVarExpr.containsKey(expr)) { LogicalVariable newVar = context.newVar(); AggregateFunctionCallExpression aggFun = AsterixBuiltinFunctions .makeAggregateFunctionExpression(fi, fce.getArguments()); rewriteGroupByAggregate(argVar, gbyOp, aggFun, newVar, context); ILogicalExpression newVarExpr = new VariableReferenceExpression(newVar); aggregateExprToVarExpr.put(expr, newVarExpr); return new Pair<Boolean, ILogicalExpression>(Boolean.TRUE, newVarExpr); } else { ILogicalExpression varExpr = aggregateExprToVarExpr.get(expr); return new Pair<Boolean, ILogicalExpression>(Boolean.TRUE, varExpr); } } } } boolean change = false; for (Mutable<ILogicalExpression> a : fce.getArguments()) { Pair<Boolean, ILogicalExpression> aggArg = extractAggFunctionsFromExpression(a, gbyWithAgg, aggregateExprToVarExpr, context); if (aggArg.first.booleanValue()) { a.setValue(aggArg.second); change = true; } } return new Pair<Boolean, ILogicalExpression>(change, fce); } case VARIABLE: case CONSTANT: { return new Pair<Boolean, ILogicalExpression>(Boolean.FALSE, expr); } default: { throw new IllegalArgumentException(); } } }
From source file:edu.uci.ics.hyracks.algebricks.rewriter.rules.EnforceStructuralPropertiesRule.java
private void setNewOp(Mutable<ILogicalOperator> opRef, AbstractLogicalOperator newOp, IOptimizationContext context) throws AlgebricksException { ILogicalOperator oldOp = opRef.getValue(); opRef.setValue(newOp); newOp.getInputs().add(new MutableObject<ILogicalOperator>(oldOp)); newOp.recomputeSchema();//from w ww .ja va 2 s . c om newOp.computeDeliveredPhysicalProperties(context); context.computeAndSetTypeEnvironmentForOperator(newOp); AlgebricksConfig.ALGEBRICKS_LOGGER.finest(">>>> Structural properties for " + newOp.getPhysicalOperator() + ": " + newOp.getDeliveredPhysicalProperties() + "\n"); PhysicalOptimizationsUtil.computeFDsAndEquivalenceClasses(newOp, context); }
From source file:edu.uci.ics.asterix.optimizer.rules.IntroduceDynamicTypeCastRule.java
/** * Inject a function to wrap a variable when necessary * * @param requiredRecordType// www.j a v a 2 s . c om * the required record type * @param recordVar * the record variable * @param parent * the current parent operator to be rewritten * @param context * the optimization context * @param fd * the function to be injected * @return true if cast is injected; false otherwise. * @throws AlgebricksException */ public static LogicalVariable addWrapperFunction(ARecordType requiredRecordType, LogicalVariable recordVar, ILogicalOperator parent, IOptimizationContext context, FunctionIdentifier fd) throws AlgebricksException { List<Mutable<ILogicalOperator>> opRefs = parent.getInputs(); for (int index = 0; index < opRefs.size(); index++) { Mutable<ILogicalOperator> opRef = opRefs.get(index); ILogicalOperator op = opRef.getValue(); /** get produced vars */ List<LogicalVariable> producedVars = new ArrayList<LogicalVariable>(); VariableUtilities.getProducedVariables(op, producedVars); IVariableTypeEnvironment env = op.computeOutputTypeEnvironment(context); for (int i = 0; i < producedVars.size(); i++) { LogicalVariable var = producedVars.get(i); if (var.equals(recordVar)) { /** insert an assign operator to call the function on-top-of the variable */ IAType actualType = (IAType) env.getVarType(var); AbstractFunctionCallExpression cast = new ScalarFunctionCallExpression( FunctionUtils.getFunctionInfo(fd)); cast.getArguments() .add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(var))); /** enforce the required record type */ TypeComputerUtilities.setRequiredAndInputTypes(cast, requiredRecordType, actualType); LogicalVariable newAssignVar = context.newVar(); AssignOperator newAssignOperator = new AssignOperator(newAssignVar, new MutableObject<ILogicalExpression>(cast)); newAssignOperator.getInputs().add(new MutableObject<ILogicalOperator>(op)); opRef.setValue(newAssignOperator); context.computeAndSetTypeEnvironmentForOperator(newAssignOperator); newAssignOperator.computeOutputTypeEnvironment(context); VariableUtilities.substituteVariables(parent, recordVar, newAssignVar, context); return newAssignVar; } } /** recursive descend to the operator who produced the recordVar */ LogicalVariable replacedVar = addWrapperFunction(requiredRecordType, recordVar, op, context, fd); if (replacedVar != null) { /** substitute the recordVar by the replacedVar for operators who uses recordVar */ VariableUtilities.substituteVariables(parent, recordVar, replacedVar, context); return replacedVar; } } return null; }
From source file:edu.uci.ics.asterix.optimizer.rules.PushAggregateIntoGroupbyRule.java
private boolean collectVarsBottomUp(Mutable<ILogicalOperator> opRef, IOptimizationContext context, Map<LogicalVariable, Integer> gbyListifyVarsCount, Map<LogicalVariable, GroupByOperator> gbyWithAgg, Map<LogicalVariable, Integer> gbyAggVarToPlanIndex, Map<ILogicalExpression, ILogicalExpression> aggregateExprToVarExpr) throws AlgebricksException { AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue(); context.addToDontApplySet(this, op1); boolean change = false; for (Mutable<ILogicalOperator> child : op1.getInputs()) { if (collectVarsBottomUp(child, context, gbyListifyVarsCount, gbyWithAgg, gbyAggVarToPlanIndex, aggregateExprToVarExpr)) { change = true;// ww w . ja v a2 s. c o m } } // Need to use a list instead of a hash-set, because a var. may appear // several times in the same op. List<LogicalVariable> used = new LinkedList<LogicalVariable>(); VariableUtilities.getUsedVariables(op1, used); switch (op1.getOperatorTag()) { case ASSIGN: case SELECT: { boolean found = false; // Do some prefiltering: check if the Assign uses any gby vars. for (LogicalVariable v : used) { if (gbyListifyVarsCount.get(v) != null) { found = true; break; } } if (found) { if (op1.getOperatorTag() == LogicalOperatorTag.ASSIGN) { AssignOperator assign = (AssignOperator) op1; for (Mutable<ILogicalExpression> exprRef : assign.getExpressions()) { Pair<Boolean, ILogicalExpression> p = extractAggFunctionsFromExpression(exprRef, gbyWithAgg, aggregateExprToVarExpr, context); if (p.first) { change = true; exprRef.setValue(p.second); } } } if (op1.getOperatorTag() == LogicalOperatorTag.SELECT) { SelectOperator select = (SelectOperator) op1; Mutable<ILogicalExpression> exprRef = select.getCondition(); Pair<Boolean, ILogicalExpression> p = extractAggFunctionsFromExpression(exprRef, gbyWithAgg, aggregateExprToVarExpr, context); if (p.first) { change = true; exprRef.setValue(p.second); } } used.clear(); VariableUtilities.getUsedVariables(op1, used); // increment the count for the ones which are still used for (LogicalVariable v : used) { Integer m = gbyListifyVarsCount.get(v); if (m != null) { gbyListifyVarsCount.put(v, m + 1); } } } break; } case SUBPLAN: { for (LogicalVariable v : used) { Integer m = gbyListifyVarsCount.get(v); if (m != null) { GroupByOperator gbyOp = gbyWithAgg.get(v); if (pushSubplanAsAggIntoGby(opRef, gbyOp, v, gbyListifyVarsCount, gbyWithAgg, gbyAggVarToPlanIndex, context)) { change = true; } else { gbyListifyVarsCount.put(v, m + 1); } break; } } break; } case GROUP: { List<LogicalVariable> vars = collectOneVarPerAggFromGroupOp((GroupByOperator) op1); if (vars != null) { for (int i = 0; i < vars.size(); i++) { LogicalVariable v = vars.get(i); if (v != null) { gbyListifyVarsCount.put(v, 0); gbyAggVarToPlanIndex.put(v, i); gbyWithAgg.put(v, (GroupByOperator) op1); } } } break; } default: { for (LogicalVariable v : used) { Integer m = gbyListifyVarsCount.get(v); if (m != null) { gbyListifyVarsCount.put(v, m + 1); } } } } return change; }
From source file:edu.uci.ics.asterix.optimizer.rules.temporal.TranslateIntervalExpressionRule.java
@Override public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException { AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue(); if (op.getOperatorTag() != LogicalOperatorTag.SELECT) { return false; }/*from w w w. ja v a 2 s . c o m*/ SelectOperator selectOp = (SelectOperator) op; Mutable<ILogicalExpression> exprRef = selectOp.getCondition(); boolean modified = false; ILogicalExpression expr = exprRef.getValue(); if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) { return false; } AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr; if (funcExpr.getArguments().size() != 2) { return false; } ILogicalExpression interval1 = funcExpr.getArguments().get(0).getValue(); ILogicalExpression interval2 = funcExpr.getArguments().get(1).getValue(); if (funcExpr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.INTERVAL_MEETS)) { exprRef.setValue(getEqualExpr(getIntervalEndExpr(interval1), getIntervalStartExpr(interval2))); modified = true; } else if (funcExpr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.INTERVAL_MET_BY)) { exprRef.setValue(getEqualExpr(getIntervalStartExpr(interval1), getIntervalEndExpr(interval2))); modified = true; } else if (funcExpr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.INTERVAL_STARTS)) { ILogicalExpression startExpr = getEqualExpr(getIntervalStartExpr(interval1), getIntervalStartExpr(interval2)); ILogicalExpression endExpr = getLessThanOrEqualExpr(getIntervalEndExpr(interval1), getIntervalEndExpr(interval2)); exprRef.setValue(getAndExpr(startExpr, endExpr)); modified = true; } else if (funcExpr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.INTERVAL_STARTED_BY)) { ILogicalExpression startExpr = getEqualExpr(getIntervalStartExpr(interval1), getIntervalStartExpr(interval2)); ILogicalExpression endExpr = getLessThanOrEqualExpr(getIntervalEndExpr(interval2), getIntervalEndExpr(interval1)); exprRef.setValue(getAndExpr(startExpr, endExpr)); modified = true; } else if (funcExpr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.INTERVAL_ENDS)) { ILogicalExpression endExpr = getEqualExpr(getIntervalEndExpr(interval1), getIntervalEndExpr(interval2)); ILogicalExpression startExpr = getLessThanOrEqualExpr(getIntervalStartExpr(interval1), getIntervalStartExpr(interval2)); exprRef.setValue(getAndExpr(startExpr, endExpr)); modified = true; } else if (funcExpr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.INTERVAL_ENDED_BY)) { ILogicalExpression endExpr = getEqualExpr(getIntervalEndExpr(interval1), getIntervalEndExpr(interval2)); ILogicalExpression startExpr = getLessThanOrEqualExpr(getIntervalStartExpr(interval2), getIntervalStartExpr(interval1)); exprRef.setValue(getAndExpr(startExpr, endExpr)); modified = true; } else if (funcExpr.getFunctionInfo().equals(AsterixBuiltinFunctions.INTERVAL_BEFORE)) { // Requires new strategy, no translation for this interval and the remaining listed. } else if (funcExpr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.INTERVAL_AFTER)) { } else if (funcExpr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.INTERVAL_OVERLAPS)) { } else if (funcExpr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.INTERVAL_OVERLAPPED_BY)) { } else if (funcExpr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.INTERVAL_OVERLAPPING)) { } else if (funcExpr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.INTERVAL_COVERS)) { } else if (funcExpr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.INTERVAL_COVERED_BY)) { } return modified; }