List of usage examples for org.apache.commons.lang3.mutable Mutable getValue
T getValue();
From source file:org.apache.asterix.optimizer.rules.am.InvertedIndexAccessMethod.java
private Mutable<ILogicalOperator> createPanicNestedLoopJoinPlan(Mutable<ILogicalOperator> joinRef, OptimizableOperatorSubTree indexSubTree, OptimizableOperatorSubTree probeSubTree, IOptimizableFuncExpr optFuncExpr, Index chosenIndex, Map<LogicalVariable, LogicalVariable> panicVarMap, IOptimizationContext context) throws AlgebricksException { LogicalVariable inputSearchVar = getInputSearchVar(optFuncExpr, indexSubTree); // We split the plan into two "branches", and add selections on each side. AbstractLogicalOperator replicateOp = new ReplicateOperator(2); replicateOp.getInputs().add(new MutableObject<ILogicalOperator>(probeSubTree.getRoot())); replicateOp.setExecutionMode(ExecutionMode.PARTITIONED); context.computeAndSetTypeEnvironmentForOperator(replicateOp); // Create select ops for removing tuples that are filterable and not filterable, respectively. IVariableTypeEnvironment probeTypeEnv = context.getOutputTypeEnvironment(probeSubTree.getRoot()); IAType inputSearchVarType;// www . j a v a2s. c om if (chosenIndex.isEnforcingKeyFileds()) { inputSearchVarType = optFuncExpr.getFieldType(optFuncExpr.findLogicalVar(inputSearchVar)); } else { inputSearchVarType = (IAType) probeTypeEnv.getVarType(inputSearchVar); } Mutable<ILogicalOperator> isFilterableSelectOpRef = new MutableObject<ILogicalOperator>(); Mutable<ILogicalOperator> isNotFilterableSelectOpRef = new MutableObject<ILogicalOperator>(); createIsFilterableSelectOps(replicateOp, inputSearchVar, inputSearchVarType, optFuncExpr, chosenIndex, context, isFilterableSelectOpRef, isNotFilterableSelectOpRef); List<LogicalVariable> originalLiveVars = new ArrayList<LogicalVariable>(); VariableUtilities.getLiveVariables(indexSubTree.getRoot(), originalLiveVars); // Copy the scan subtree in indexSubTree. LogicalOperatorDeepCopyWithNewVariablesVisitor deepCopyVisitor = new LogicalOperatorDeepCopyWithNewVariablesVisitor( context, context); ILogicalOperator scanSubTree = deepCopyVisitor.deepCopy(indexSubTree.getRoot()); Map<LogicalVariable, LogicalVariable> copyVarMap = deepCopyVisitor.getInputToOutputVariableMapping(); panicVarMap.putAll(copyVarMap); List<LogicalVariable> copyLiveVars = new ArrayList<LogicalVariable>(); VariableUtilities.getLiveVariables(scanSubTree, copyLiveVars); // Replace the inputs of the given join op, and replace variables in its // condition since we deep-copied one of the scanner subtrees which // changed variables. AbstractBinaryJoinOperator joinOp = (AbstractBinaryJoinOperator) joinRef.getValue(); for (Map.Entry<LogicalVariable, LogicalVariable> entry : copyVarMap.entrySet()) { joinOp.getCondition().getValue().substituteVar(entry.getKey(), entry.getValue()); } joinOp.getInputs().clear(); joinOp.getInputs().add(new MutableObject<ILogicalOperator>(scanSubTree)); // Make sure that the build input (which may be materialized causing blocking) comes from // the split+select, otherwise the plan will have a deadlock. joinOp.getInputs().add(isNotFilterableSelectOpRef); context.computeAndSetTypeEnvironmentForOperator(joinOp); // Return the new root of the probeSubTree. return isFilterableSelectOpRef; }
From source file:org.apache.asterix.optimizer.rules.am.InvertedIndexAccessMethod.java
private ScalarFunctionCallExpression findTokensFunc(FunctionIdentifier funcId, IOptimizableFuncExpr optFuncExpr, int subTreeIndex) { //find either a gram-tokens or a word-tokens function that exists in optFuncExpr.subTrees' assignsAndUnnests OptimizableOperatorSubTree subTree = null; LogicalVariable targetVar = null;/* www. j av a 2s . c o m*/ subTree = optFuncExpr.getOperatorSubTree(subTreeIndex); if (subTree == null) { return null; } targetVar = optFuncExpr.getLogicalVar(subTreeIndex); if (targetVar == null) { return null; } for (AbstractLogicalOperator op : subTree.getAssignsAndUnnests()) { if (op.getOperatorTag() != LogicalOperatorTag.ASSIGN) { continue; } List<Mutable<ILogicalExpression>> exprList = ((AssignOperator) op).getExpressions(); for (Mutable<ILogicalExpression> expr : exprList) { if (expr.getValue().getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) { continue; } AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr.getValue(); if (funcExpr.getFunctionIdentifier() != funcId) { continue; } ILogicalExpression varExpr = funcExpr.getArguments().get(0).getValue(); if (varExpr.getExpressionTag() != LogicalExpressionTag.VARIABLE) { continue; } if (((VariableReferenceExpression) varExpr).getVariableReference() == targetVar) { continue; } return (ScalarFunctionCallExpression) funcExpr; } } return null; }
From source file:org.apache.asterix.optimizer.rules.am.OptimizableOperatorSubTree.java
public boolean initFromSubTree(Mutable<ILogicalOperator> subTreeOpRef) throws AlgebricksException { reset();/*from w ww.j a va 2 s . c o m*/ rootRef = subTreeOpRef; root = subTreeOpRef.getValue(); // Examine the op's children to match the expected patterns. AbstractLogicalOperator subTreeOp = (AbstractLogicalOperator) subTreeOpRef.getValue(); do { // Skip select operator. if (subTreeOp.getOperatorTag() == LogicalOperatorTag.SELECT) { subTreeOpRef = subTreeOp.getInputs().get(0); subTreeOp = (AbstractLogicalOperator) subTreeOpRef.getValue(); } // Check primary-index pattern. if (subTreeOp.getOperatorTag() != LogicalOperatorTag.ASSIGN && subTreeOp.getOperatorTag() != LogicalOperatorTag.UNNEST) { // Pattern may still match if we are looking for primary index matches as well. return initializeDataSource(subTreeOpRef); } // Match (assign | unnest)+. while ((subTreeOp.getOperatorTag() == LogicalOperatorTag.ASSIGN || subTreeOp.getOperatorTag() == LogicalOperatorTag.UNNEST)) { if (!OperatorPropertiesUtil.isMovable(subTreeOp)) { return false; } else { getAssignsAndUnnestsRefs().add(subTreeOpRef); getAssignsAndUnnests().add(subTreeOp); } subTreeOpRef = subTreeOp.getInputs().get(0); subTreeOp = (AbstractLogicalOperator) subTreeOpRef.getValue(); } } while (subTreeOp.getOperatorTag() == LogicalOperatorTag.SELECT); // Match data source (datasource scan or primary index search). return initializeDataSource(subTreeOpRef); }
From source file:org.apache.asterix.optimizer.rules.am.OptimizableOperatorSubTree.java
private boolean initializeDataSource(Mutable<ILogicalOperator> subTreeOpRef) { AbstractLogicalOperator subTreeOp = (AbstractLogicalOperator) subTreeOpRef.getValue(); if (subTreeOp.getOperatorTag() == LogicalOperatorTag.DATASOURCESCAN) { setDataSourceType(DataSourceType.DATASOURCE_SCAN); setDataSourceRef(subTreeOpRef);// w ww . j a v a 2s . c om return true; } else if (subTreeOp.getOperatorTag() == LogicalOperatorTag.EMPTYTUPLESOURCE) { setDataSourceType(DataSourceType.COLLECTION_SCAN); setDataSourceRef(subTreeOpRef); return true; } else if (subTreeOp.getOperatorTag() == LogicalOperatorTag.UNNEST_MAP) { // There can be multiple unnest-map or datasource-scan operators // if index-nested-loop-join has been applied by IntroduceJoinAccessMethodRule. // So, we need to traverse the whole path from the subTreeOp. boolean dataSourceFound = false; while (true) { if (subTreeOp.getOperatorTag() == LogicalOperatorTag.UNNEST_MAP) { UnnestMapOperator unnestMapOp = (UnnestMapOperator) subTreeOp; ILogicalExpression unnestExpr = unnestMapOp.getExpressionRef().getValue(); if (unnestExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) { AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) unnestExpr; if (f.getFunctionIdentifier().equals(AsterixBuiltinFunctions.INDEX_SEARCH)) { AccessMethodJobGenParams jobGenParams = new AccessMethodJobGenParams(); jobGenParams.readFromFuncArgs(f.getArguments()); if (jobGenParams.isPrimaryIndex()) { if (getDataSourceRef() == null) { setDataSourceRef(subTreeOpRef); setDataSourceType(DataSourceType.PRIMARY_INDEX_LOOKUP); } else { // One datasource already exists. This is an additional datasource. initializeIxJoinOuterAddtionalDataSourcesIfEmpty(); getIxJoinOuterAdditionalDataSourceTypes() .add(DataSourceType.PRIMARY_INDEX_LOOKUP); getIxJoinOuterAdditionalDataSourceRefs().add(subTreeOpRef); } dataSourceFound = true; } } else if (f.getFunctionIdentifier().equals(AsterixBuiltinFunctions.EXTERNAL_LOOKUP)) { // External lookup case if (getDataSourceRef() == null) { setDataSourceRef(subTreeOpRef); setDataSourceType(DataSourceType.EXTERNAL_SCAN); } else { // One datasource already exists. This is an additional datasource. initializeIxJoinOuterAddtionalDataSourcesIfEmpty(); getIxJoinOuterAdditionalDataSourceTypes().add(DataSourceType.EXTERNAL_SCAN); getIxJoinOuterAdditionalDataSourceRefs().add(subTreeOpRef); } dataSourceFound = true; } } } else if (subTreeOp.getOperatorTag() == LogicalOperatorTag.DATASOURCESCAN) { initializeIxJoinOuterAddtionalDataSourcesIfEmpty(); getIxJoinOuterAdditionalDataSourceTypes().add(DataSourceType.DATASOURCE_SCAN); getIxJoinOuterAdditionalDataSourceRefs().add(subTreeOpRef); dataSourceFound = true; } else if (subTreeOp.getOperatorTag() == LogicalOperatorTag.EMPTYTUPLESOURCE) { initializeIxJoinOuterAddtionalDataSourcesIfEmpty(); getIxJoinOuterAdditionalDataSourceTypes().add(DataSourceType.COLLECTION_SCAN); getIxJoinOuterAdditionalDataSourceRefs().add(subTreeOpRef); } // Traverse the subtree while there are operators in the path. if (subTreeOp.hasInputs()) { subTreeOpRef = subTreeOp.getInputs().get(0); subTreeOp = (AbstractLogicalOperator) subTreeOpRef.getValue(); } else { break; } } if (dataSourceFound) { return true; } } return false; }
From source file:org.apache.asterix.optimizer.rules.am.RTreeAccessMethod.java
@Override public boolean applyJoinPlanTransformation(Mutable<ILogicalOperator> joinRef, OptimizableOperatorSubTree leftSubTree, OptimizableOperatorSubTree rightSubTree, Index chosenIndex, AccessMethodAnalysisContext analysisCtx, IOptimizationContext context, boolean isLeftOuterJoin, boolean hasGroupBy) throws AlgebricksException { // Determine if the index is applicable on the left or right side (if both, we arbitrarily prefer the left // side).// w w w . j a va 2 s . c om Dataset dataset = analysisCtx.indexDatasetMap.get(chosenIndex); OptimizableOperatorSubTree indexSubTree; OptimizableOperatorSubTree probeSubTree; // We assume that the left subtree is the outer branch and the right subtree is the inner branch. // This assumption holds true since we only use an index from the right subtree. // The following is just a sanity check. if (rightSubTree.hasDataSourceScan() && dataset.getDatasetName().equals(rightSubTree.getDataset().getDatasetName())) { indexSubTree = rightSubTree; probeSubTree = leftSubTree; } else { return false; } LogicalVariable newNullPlaceHolderVar = null; if (isLeftOuterJoin) { // get a new null place holder variable that is the first field variable of the primary key // from the indexSubTree's datasourceScanOp newNullPlaceHolderVar = indexSubTree.getDataSourceVariables().get(0); } // TODO: We can probably do something smarter here based on selectivity or MBR area. ILogicalOperator primaryIndexUnnestOp = createSecondaryToPrimaryPlan(indexSubTree, probeSubTree, chosenIndex, analysisCtx, true, isLeftOuterJoin, true, context); if (primaryIndexUnnestOp == null) { return false; } if (isLeftOuterJoin && hasGroupBy) { // reset the null place holder variable AccessMethodUtils.resetLOJNullPlaceholderVariableInGroupByOp(analysisCtx, newNullPlaceHolderVar, context); } indexSubTree.getDataSourceRef().setValue(primaryIndexUnnestOp); // Change join into a select with the same condition. AbstractBinaryJoinOperator joinOp = (AbstractBinaryJoinOperator) joinRef.getValue(); SelectOperator topSelect = new SelectOperator(joinOp.getCondition(), isLeftOuterJoin, newNullPlaceHolderVar); topSelect.getInputs().add(indexSubTree.getRootRef()); topSelect.setExecutionMode(ExecutionMode.LOCAL); context.computeAndSetTypeEnvironmentForOperator(topSelect); // Replace the original join with the new subtree rooted at the select op. joinRef.setValue(topSelect); return true; }
From source file:org.apache.asterix.optimizer.rules.ByNameToByIndexFieldAccessRule.java
@Override public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException { ILogicalOperator op = opRef.getValue(); if (op.acceptExpressionTransform(exprRef -> rewriteExpressionReference(op, exprRef, context))) { op.removeAnnotation(AsterixOperatorAnnotations.PUSHED_FIELD_ACCESS); context.computeAndSetTypeEnvironmentForOperator(op); return true; }/*from w w w. j a v a2 s . co m*/ return false; }
From source file:org.apache.asterix.optimizer.rules.ByNameToByIndexFieldAccessRule.java
private boolean rewriteExpressionReference(ILogicalOperator op, Mutable<ILogicalExpression> exprRef, IOptimizationContext context) throws AlgebricksException { ILogicalExpression expr = exprRef.getValue(); if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) { return false; }/*from ww w .j a v a2 s . c o m*/ boolean changed = false; AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr; for (Mutable<ILogicalExpression> funcArgRef : funcExpr.getArguments()) { if (rewriteExpressionReference(op, funcArgRef, context)) { changed = true; } } AbstractFunctionCallExpression fce = (AbstractFunctionCallExpression) expr; if (fce.getFunctionIdentifier() != AsterixBuiltinFunctions.FIELD_ACCESS_BY_NAME) { return changed; } changed |= extractFirstArg(fce, op, context); IVariableTypeEnvironment env = context.getOutputTypeEnvironment(op); IAType t = (IAType) env.getType(fce.getArguments().get(0).getValue()); changed |= rewriteFieldAccess(exprRef, fce, getActualType(t)); return changed; }
From source file:org.apache.asterix.optimizer.rules.CancelUnnestWithNestedListifyRule.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 . java 2s . c o m UnnestOperator unnest1 = (UnnestOperator) op1; ILogicalExpression expr = unnest1.getExpressionRef().getValue(); LogicalVariable unnestedVar; switch (expr.getExpressionTag()) { case VARIABLE: unnestedVar = ((VariableReferenceExpression) expr).getVariableReference(); break; case FUNCTION_CALL: 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; } unnestedVar = ((VariableReferenceExpression) functionCallArgExpr).getVariableReference(); break; default: return false; } if (varUsedAbove.contains(unnestedVar)) { return false; } Mutable<ILogicalOperator> opRef2 = op1.getInputs().get(0); AbstractLogicalOperator r = (AbstractLogicalOperator) opRef2.getValue(); if (r.getOperatorTag() != LogicalOperatorTag.GROUP) { return false; } // go inside of a group-by plan GroupByOperator gby = (GroupByOperator) r; if (gby.getNestedPlans().size() != 1) { return false; } if (gby.getNestedPlans().get(0).getRoots().size() != 1) { return false; } AbstractLogicalOperator nestedPlanRoot = (AbstractLogicalOperator) gby.getNestedPlans().get(0).getRoots() .get(0).getValue(); if (nestedPlanRoot.getOperatorTag() != LogicalOperatorTag.AGGREGATE) { return false; } AggregateOperator agg = (AggregateOperator) nestedPlanRoot; Mutable<ILogicalOperator> aggInputOpRef = agg.getInputs().get(0); if (agg.getVariables().size() > 1) { return false; } if (OperatorManipulationUtil.ancestorOfOperators(agg, ImmutableSet.of(LogicalOperatorTag.LIMIT, LogicalOperatorTag.ORDER, LogicalOperatorTag.GROUP, LogicalOperatorTag.DISTINCT))) { 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(); ArrayList<LogicalVariable> assgnVars = new ArrayList<LogicalVariable>(1); assgnVars.add(unnest1.getVariable()); ArrayList<Mutable<ILogicalExpression>> assgnExprs = new ArrayList<Mutable<ILogicalExpression>>(1); assgnExprs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(paramVar))); AssignOperator assign = new AssignOperator(assgnVars, assgnExprs); LogicalVariable posVar = unnest1.getPositionalVariable(); if (posVar == null) { // Creates assignment for group-by keys. ArrayList<LogicalVariable> gbyKeyAssgnVars = new ArrayList<LogicalVariable>(); ArrayList<Mutable<ILogicalExpression>> gbyKeyAssgnExprs = new ArrayList<Mutable<ILogicalExpression>>(); for (int i = 0; i < gby.getGroupByList().size(); i++) { if (gby.getGroupByList().get(i).first != null) { gbyKeyAssgnVars.add(gby.getGroupByList().get(i).first); gbyKeyAssgnExprs.add(gby.getGroupByList().get(i).second); } } // Moves the nested pipeline before aggregation out of the group-by op. Mutable<ILogicalOperator> bottomOpRef = aggInputOpRef; AbstractLogicalOperator bottomOp = (AbstractLogicalOperator) bottomOpRef.getValue(); while (bottomOp.getOperatorTag() != LogicalOperatorTag.NESTEDTUPLESOURCE) { bottomOpRef = bottomOp.getInputs().get(0); bottomOp = (AbstractLogicalOperator) bottomOpRef.getValue(); } // Removes the group-by operator. opRef.setValue(assign); assign.getInputs().add(aggInputOpRef); AssignOperator gbyKeyAssign = new AssignOperator(gbyKeyAssgnVars, gbyKeyAssgnExprs); gbyKeyAssign.getInputs().add(gby.getInputs().get(0)); bottomOpRef.setValue(gbyKeyAssign); context.computeAndSetTypeEnvironmentForOperator(gbyKeyAssign); context.computeAndSetTypeEnvironmentForOperator(assign); } else { // if positional variable is used in unnest, the unnest will be pushed into the group-by as a running-aggregate // First create assign for the unnest variable List<LogicalVariable> nestedAssignVars = new ArrayList<LogicalVariable>(); List<Mutable<ILogicalExpression>> nestedAssignExprs = new ArrayList<Mutable<ILogicalExpression>>(); nestedAssignVars.add(unnest1.getVariable()); nestedAssignExprs.add(new MutableObject<ILogicalExpression>(arg0)); AssignOperator nestedAssign = new AssignOperator(nestedAssignVars, nestedAssignExprs); nestedAssign.getInputs().add(opRef2); // Then create running aggregation for the positional variable List<LogicalVariable> raggVars = new ArrayList<LogicalVariable>(); List<Mutable<ILogicalExpression>> raggExprs = new ArrayList<Mutable<ILogicalExpression>>(); raggVars.add(posVar); StatefulFunctionCallExpression fce = new StatefulFunctionCallExpression( FunctionUtil.getFunctionInfo(AsterixBuiltinFunctions.TID), UnpartitionedPropertyComputer.INSTANCE); raggExprs.add(new MutableObject<ILogicalExpression>(fce)); RunningAggregateOperator raggOp = new RunningAggregateOperator(raggVars, raggExprs); raggOp.setExecutionMode(unnest1.getExecutionMode()); RunningAggregatePOperator raggPOp = new RunningAggregatePOperator(); raggOp.setPhysicalOperator(raggPOp); raggOp.getInputs().add(nestedPlanRoot.getInputs().get(0)); gby.getNestedPlans().get(0).getRoots().set(0, new MutableObject<ILogicalOperator>(raggOp)); opRef.setValue(nestedAssign); context.computeAndSetTypeEnvironmentForOperator(nestedAssign); context.computeAndSetTypeEnvironmentForOperator(raggOp); context.computeAndSetTypeEnvironmentForOperator(gby); } return true; }
From source file:org.apache.asterix.optimizer.rules.CheckInsertUpsertReturningRule.java
@Override public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException { if (checked) { return false; }/*from w ww.j a va 2 s . co m*/ if (InsertUpsertCheckUtil.check(opRef.getValue())) { throw new CompilationException(ErrorCode.COMPILATION_INVALID_RETURNING_EXPRESSION); } checked = true; return false; }
From source file:org.apache.asterix.optimizer.rules.ConstantFoldingRule.java
@Override public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException { ILogicalOperator op = opRef.getValue(); if (context.checkIfInDontApplySet(this, op)) { return false; }/*from w ww. ja va2 s . com*/ return op.acceptExpressionTransform(cfv); }