List of usage examples for org.apache.commons.lang3.mutable Mutable getValue
T getValue();
From source file:org.apache.asterix.optimizer.rules.PushGroupByThroughProduct.java
private void push(Mutable<ILogicalOperator> opRefGby, Mutable<ILogicalOperator> opRefJoin, int branch, List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> decorToPush, List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> decorNotToPush, IOptimizationContext context) throws AlgebricksException { GroupByOperator gby = (GroupByOperator) opRefGby.getValue(); AbstractBinaryJoinOperator join = (AbstractBinaryJoinOperator) opRefJoin.getValue(); gby.getDecorList().clear();//ww w . ja v a 2 s . c o m gby.getDecorList().addAll(decorToPush); for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : decorNotToPush) { LogicalVariable v1 = p.first; if (v1 != null) { VariableReferenceExpression varRef = (VariableReferenceExpression) p.second.getValue(); LogicalVariable v2 = varRef.getVariableReference(); OperatorManipulationUtil.substituteVarRec(join, v2, v1, true, context); } } Mutable<ILogicalOperator> branchRef = join.getInputs().get(branch); ILogicalOperator opBranch = branchRef.getValue(); opRefJoin.setValue(opBranch); branchRef.setValue(gby); opRefGby.setValue(join); }
From source file:org.apache.asterix.optimizer.rules.PushLimitIntoOrderByRule.java
@Override public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException { AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue(); // The current operator should be LIMIT operator. if (op.getOperatorTag() != LogicalOperatorTag.LIMIT) { return false; }/*ww w. j av a 2 s. c o m*/ Mutable<ILogicalOperator> opRef2 = op.getInputs().get(0); AbstractLogicalOperator op2 = (AbstractLogicalOperator) opRef2.getValue(); if (context.checkAndAddToAlreadyCompared(op, op2)) { return false; } // Should be ORDER operator if (op2.getOperatorTag() != LogicalOperatorTag.ORDER) { return false; } else { // ORDER operator is followed by LIMIT. Thus we can check whether we can apply this rule. boolean res = pushLimitIntoOrder(opRef, opRef2, context); if (res) { OperatorPropertiesUtil.typeOpRec(opRef, context); } return res; } }
From source file:org.apache.asterix.optimizer.rules.PushLimitIntoOrderByRule.java
/** * Generate new ORDER operator that uses TopKSort module and replaces the old ORDER operator. *//*w w w . j a v a 2 s . c om*/ private boolean pushLimitIntoOrder(Mutable<ILogicalOperator> opRef, Mutable<ILogicalOperator> opRef2, IOptimizationContext context) throws AlgebricksException { PhysicalOptimizationConfig physicalOptimizationConfig = context.getPhysicalOptimizationConfig(); LimitOperator limitOp = (LimitOperator) opRef.getValue(); OrderOperator orderOp = (OrderOperator) opRef2.getValue(); long topK = -1; // We don't push-down LIMIT into in-memory sort. if (orderOp.getPhysicalOperator().getOperatorTag() != PhysicalOperatorTag.STABLE_SORT) { return false; } // Get the LIMIT constant if (limitOp.getMaxObjects().getValue().getExpressionTag() == LogicalExpressionTag.CONSTANT) { // Currently, we support LIMIT with a constant value. topK = AccessMethodUtils.getInt64Constant(limitOp.getMaxObjects()); // If topK is huge, there is no reason to use topK sort module // since the original external sort's performance might be better. if (topK > Integer.MAX_VALUE) { return false; } if (topK < 0) { topK = 0; } } else { return false; } // Get the offset constant if there is one. If one presents, then topK = topK + offset. // This is because we can't apply offset to the external sort. // Final topK will be applied through LIMIT. if (limitOp.getOffset().getValue() != null) { if (limitOp.getOffset().getValue().getExpressionTag() == LogicalExpressionTag.CONSTANT) { long offset = AccessMethodUtils.getInt64Constant(limitOp.getOffset()); if (offset < 0) { offset = 0; } // Check the overflow case. if (offset >= Integer.MAX_VALUE - topK) { return false; } topK += offset; } else { return false; } } // Create the new ORDER operator, set the topK value, and replace the current one. OrderOperator newOrderOp = new OrderOperator(orderOp.getOrderExpressions(), (int) topK); newOrderOp.setPhysicalOperator(new StableSortPOperator( physicalOptimizationConfig.getMaxFramesExternalSort(), newOrderOp.getTopK())); newOrderOp.getInputs().addAll(orderOp.getInputs()); newOrderOp.setExecutionMode(orderOp.getExecutionMode()); newOrderOp.recomputeSchema(); newOrderOp.computeDeliveredPhysicalProperties(context); opRef2.setValue(newOrderOp); context.computeAndSetTypeEnvironmentForOperator(newOrderOp); context.addToDontApplySet(this, limitOp); return true; }
From source file:org.apache.asterix.optimizer.rules.RemoveLeftOuterUnnestForLeftOuterJoinRule.java
@Override public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException { ILogicalOperator op1 = opRef.getValue(); // Checks the plan pattern. if (!checkOperatorPattern(op1)) { return false; }/* w w w. j ava2 s .c o m*/ LeftOuterUnnestOperator outerUnnest = (LeftOuterUnnestOperator) op1; GroupByOperator gbyOperator = (GroupByOperator) outerUnnest.getInputs().get(0).getValue(); LeftOuterJoinOperator lojOperator = (LeftOuterJoinOperator) gbyOperator.getInputs().get(0).getValue(); // Checks whether the left outer unnest and the group-by operator are qualified for rewriting. Triple<Boolean, ILogicalExpression, ILogicalExpression> checkGbyResult = checkUnnestAndGby(outerUnnest, gbyOperator); // The argument for listify and not(is-missing(...)) check should be variables. if (!isVariableReference(checkGbyResult.second) || !isVariableReference(checkGbyResult.third)) { return false; } // Checks whether both the listify variable and the condition test variable are from the right input // branch of the left outer join. LogicalVariable listifyVar = ((VariableReferenceExpression) checkGbyResult.second).getVariableReference(); LogicalVariable conditionTestVar = ((VariableReferenceExpression) checkGbyResult.third) .getVariableReference(); if (!checkListifyAndConditionVar(lojOperator, listifyVar, conditionTestVar)) { return false; } // Does the rewrite. removeGroupByAndOuterUnnest(opRef, context, outerUnnest, gbyOperator, lojOperator, listifyVar); return true; }
From source file:org.apache.asterix.optimizer.rules.RemoveRedundantListifyRule.java
private boolean applies(Mutable<ILogicalOperator> opRef, Set<LogicalVariable> varUsedAbove, IOptimizationContext context) throws AlgebricksException { AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue(); if (op1.getOperatorTag() != LogicalOperatorTag.UNNEST) { return false; }/*from w w w . ja v a 2s. co m*/ UnnestOperator unnest1 = (UnnestOperator) op1; ILogicalExpression expr = unnest1.getExpressionRef().getValue(); if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) { return false; } if (((AbstractFunctionCallExpression) expr) .getFunctionIdentifier() != AsterixBuiltinFunctions.SCAN_COLLECTION) { return false; } AbstractFunctionCallExpression functionCall = (AbstractFunctionCallExpression) expr; ILogicalExpression functionCallArgExpr = functionCall.getArguments().get(0).getValue(); if (functionCallArgExpr.getExpressionTag() != LogicalExpressionTag.VARIABLE) { return false; } LogicalVariable unnestedVar = ((VariableReferenceExpression) functionCallArgExpr).getVariableReference(); if (varUsedAbove.contains(unnestedVar)) { return false; } Mutable<ILogicalOperator> aggregateParentRef = opRef; AbstractLogicalOperator r = op1; boolean metAggregate = false; while (r.getInputs().size() == 1) { aggregateParentRef = r.getInputs().get(0); r = (AbstractLogicalOperator) aggregateParentRef.getValue(); if (r.getOperatorTag() == LogicalOperatorTag.ASSIGN) { AssignOperator assign = (AssignOperator) r; List<LogicalVariable> variables = assign.getVariables(); // The assign operator doesn't produce any variable that is used by the unnest. if (variables.contains(unnestedVar)) { return false; } } else { if (r.getOperatorTag() == LogicalOperatorTag.AGGREGATE) { metAggregate = true; } break; } } if (!metAggregate) { return false; } AggregateOperator agg = (AggregateOperator) r; if (agg.getVariables().size() > 1) { return false; } LogicalVariable aggVar = agg.getVariables().get(0); ILogicalExpression aggFun = agg.getExpressions().get(0).getValue(); if (!aggVar.equals(unnestedVar) || ((AbstractLogicalExpression) aggFun).getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) { return false; } AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) aggFun; if (!AsterixBuiltinFunctions.LISTIFY.equals(f.getFunctionIdentifier())) { return false; } if (f.getArguments().size() != 1) { return false; } ILogicalExpression arg0 = f.getArguments().get(0).getValue(); if (((AbstractLogicalExpression) arg0).getExpressionTag() != LogicalExpressionTag.VARIABLE) { return false; } LogicalVariable paramVar = ((VariableReferenceExpression) arg0).getVariableReference(); List<LogicalVariable> assgnVars = new ArrayList<>(1); assgnVars.add(unnest1.getVariable()); List<Mutable<ILogicalExpression>> assgnExprs = new ArrayList<>(1); assgnExprs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(paramVar))); AssignOperator assign = new AssignOperator(assgnVars, assgnExprs); assign.getInputs().add(agg.getInputs().get(0)); context.computeAndSetTypeEnvironmentForOperator(assign); LogicalVariable posVar = unnest1.getPositionalVariable(); if (posVar == null) { // Removes the aggregate operator. aggregateParentRef.setValue(assign); } else { List<LogicalVariable> raggVars = new ArrayList<>(1); raggVars.add(posVar); List<Mutable<ILogicalExpression>> rAggExprs = new ArrayList<>(1); StatefulFunctionCallExpression tidFun = new StatefulFunctionCallExpression( FunctionUtil.getFunctionInfo(AsterixBuiltinFunctions.TID), UnpartitionedPropertyComputer.INSTANCE); rAggExprs.add(new MutableObject<ILogicalExpression>(tidFun)); RunningAggregateOperator rAgg = new RunningAggregateOperator(raggVars, rAggExprs); rAgg.getInputs().add(new MutableObject<ILogicalOperator>(assign)); aggregateParentRef.setValue(rAgg); context.computeAndSetTypeEnvironmentForOperator(rAgg); } // Removes the unnest operator. opRef.setValue(unnest1.getInputs().get(0).getValue()); return true; }
From source file:org.apache.asterix.optimizer.rules.RemoveRedundantListifyRule.java
private boolean appliesForReverseCase(Mutable<ILogicalOperator> opRef, Set<LogicalVariable> varUsedAbove, IOptimizationContext context) throws AlgebricksException { AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue(); if (op1.getOperatorTag() != LogicalOperatorTag.AGGREGATE) { return false; }/* ww w . j a va2 s . c o m*/ AggregateOperator agg = (AggregateOperator) op1; if (agg.getVariables().size() > 1 || agg.getVariables().size() <= 0) { return false; } LogicalVariable aggVar = agg.getVariables().get(0); ILogicalExpression aggFun = agg.getExpressions().get(0).getValue(); AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) aggFun; if (!AsterixBuiltinFunctions.LISTIFY.equals(f.getFunctionIdentifier())) { return false; } if (f.getArguments().size() != 1) { return false; } ILogicalExpression arg0 = f.getArguments().get(0).getValue(); if (((AbstractLogicalExpression) arg0).getExpressionTag() != LogicalExpressionTag.VARIABLE) { return false; } LogicalVariable aggInputVar = ((VariableReferenceExpression) arg0).getVariableReference(); if (varUsedAbove.contains(aggInputVar)) { return false; } if (agg.getInputs().size() == 0) { return false; } AbstractLogicalOperator op2 = (AbstractLogicalOperator) agg.getInputs().get(0).getValue(); if (op2.getOperatorTag() != LogicalOperatorTag.UNNEST) { return false; } UnnestOperator unnest = (UnnestOperator) op2; if (unnest.getPositionalVariable() != null) { return false; } if (!unnest.getVariable().equals(aggInputVar)) { return false; } ILogicalExpression unnestArg = unnest.getExpressionRef().getValue(); if (unnestArg.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) { return false; } AbstractFunctionCallExpression scanFunc = (AbstractFunctionCallExpression) unnestArg; if (scanFunc.getFunctionIdentifier() != AsterixBuiltinFunctions.SCAN_COLLECTION) { return false; } if (scanFunc.getArguments().size() != 1) { return false; } List<LogicalVariable> assgnVars = new ArrayList<>(1); assgnVars.add(aggVar); AssignOperator assign = new AssignOperator(assgnVars, scanFunc.getArguments()); assign.getInputs().add(unnest.getInputs().get(0)); context.computeAndSetTypeEnvironmentForOperator(assign); opRef.setValue(assign); return true; }
From source file:org.apache.asterix.optimizer.rules.RemoveSortInFeedIngestionRule.java
@Override public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException { AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue(); if (op.getOperatorTag() != LogicalOperatorTag.INSERT_DELETE_UPSERT) { return false; }/* w ww. j a v a2 s .c om*/ AbstractLogicalOperator insertOp = op; AbstractLogicalOperator descendantOp = (AbstractLogicalOperator) op.getInputs().get(0).getValue(); boolean isSourceAFeed = false; while (descendantOp != null) { if (descendantOp.getOperatorTag() == LogicalOperatorTag.DATASOURCESCAN) { AqlDataSource dataSource = (AqlDataSource) ((DataSourceScanOperator) descendantOp).getDataSource(); if (dataSource.getDatasourceType() == AqlDataSourceType.FEED) { isSourceAFeed = true; } break; } if (descendantOp.getInputs().isEmpty()) { break; } descendantOp = (AbstractLogicalOperator) descendantOp.getInputs().get(0).getValue(); } if (isSourceAFeed) { AbstractLogicalOperator prevOp = (AbstractLogicalOperator) insertOp.getInputs().get(0).getValue(); if (prevOp.getOperatorTag() == LogicalOperatorTag.ORDER) { insertOp.getInputs().set(0, prevOp.getInputs().get(0)); return true; } } return false; }
From source file:org.apache.asterix.optimizer.rules.RemoveUnusedOneToOneEquiJoinRule.java
private boolean removeUnusedJoin(Mutable<ILogicalOperator> opRef) throws AlgebricksException { AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue(); boolean modified = false; usedVars.clear();//w ww . ja v a2 s. com VariableUtilities.getUsedVariables(op, usedVars); // Propagate used variables from parents downwards. parentsUsedVars.addAll(usedVars); int numInputs = op.getInputs().size(); for (int i = 0; i < numInputs; i++) { Mutable<ILogicalOperator> childOpRef = op.getInputs().get(i); int unusedJoinBranchIndex = removeJoinFromInputBranch(childOpRef); if (unusedJoinBranchIndex >= 0) { int usedBranchIndex = (unusedJoinBranchIndex == 0) ? 1 : 0; // Remove join at input index i, by hooking up op's input i with // the join's branch at unusedJoinBranchIndex. AbstractBinaryJoinOperator joinOp = (AbstractBinaryJoinOperator) childOpRef.getValue(); op.getInputs().set(i, joinOp.getInputs().get(usedBranchIndex)); modified = true; } // Descend into children. if (removeUnusedJoin(childOpRef)) { modified = true; } } return modified; }
From source file:org.apache.asterix.optimizer.rules.RemoveUnusedOneToOneEquiJoinRule.java
private int removeJoinFromInputBranch(Mutable<ILogicalOperator> opRef) throws AlgebricksException { AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue(); if (op.getOperatorTag() != LogicalOperatorTag.INNERJOIN) { return -1; }//from ww w. ja v a2 s . c o m AbstractBinaryJoinOperator joinOp = (AbstractBinaryJoinOperator) op; // Make sure the join is an equi-join. if (!isEquiJoin(joinOp.getCondition())) { return -1; } int unusedJoinBranchIndex = -1; for (int i = 0; i < joinOp.getInputs().size(); i++) { liveVars.clear(); VariableUtilities.getLiveVariables(joinOp.getInputs().get(i).getValue(), liveVars); if (liveVars.isEmpty()) { // The branch does not produce any variable, i.e., it only contains an empty tuple source. return i; } liveVars.retainAll(parentsUsedVars); if (liveVars.isEmpty()) { // None of the live variables from this branch are used by its parents. unusedJoinBranchIndex = i; break; } } if (unusedJoinBranchIndex < 0) { // The variables from both branches are used in the upstream plan. We cannot remove this join. return -1; } // Check whether one of the join branches is unused. usedVars.clear(); VariableUtilities.getUsedVariables(joinOp, usedVars); // Check whether all used variables originate from primary keys of exactly the same dataset. // Collect a list of datascans whose primary key variables are used in the join condition. gatherProducingDataScans(opRef, usedVars, dataScans); if (dataScans.size() < 2) { // Either branch does not use its primary key in the join condition. return -1; } // Check that all datascans scan the same dataset, and that the join condition // only used primary key variables of those datascans. for (int i = 0; i < dataScans.size(); i++) { if (i > 0) { DatasetDataSource prevAqlDataSource = (DatasetDataSource) dataScans.get(i - 1).getDataSource(); DatasetDataSource currAqlDataSource = (DatasetDataSource) dataScans.get(i).getDataSource(); if (!prevAqlDataSource.getDataset().equals(currAqlDataSource.getDataset())) { return -1; } } // Remove from the used variables all the primary key vars of this dataset. fillPKVars(dataScans.get(i), pkVars); usedVars.removeAll(pkVars); } if (!usedVars.isEmpty()) { // The join condition also uses some other variables that are not primary // keys from datasource scans of the same dataset. return -1; } // Suppose we Project B over A.a ~= B.b, where A's fields are involved in a selective operator. // We expect the post-plan will NOT prune the join part derived from A. if (unusedJoinBranchIndex >= 0 && isSelectionAboveDataScan(opRef.getValue().getInputs().get(unusedJoinBranchIndex))) { unusedJoinBranchIndex = -1; } return unusedJoinBranchIndex; }
From source file:org.apache.asterix.optimizer.rules.RemoveUnusedOneToOneEquiJoinRule.java
private boolean isSelectionAboveDataScan(Mutable<ILogicalOperator> opRef) { boolean hasSelection = false; AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue(); LogicalOperatorTag tag = op.getOperatorTag(); switch (tag) { case DATASOURCESCAN: return false; case UNNEST_MAP: case LEFT_OUTER_UNNEST_MAP: case LIMIT:/*ww w . java2s. c om*/ case SELECT: return true; default: for (Mutable<ILogicalOperator> inputOp : op.getInputs()) { hasSelection |= isSelectionAboveDataScan(inputOp); } } return hasSelection; }