List of usage examples for org.apache.commons.lang3.mutable Mutable getValue
T getValue();
From source file:org.apache.asterix.translator.LangExpressionToPlanTranslator.java
private ILogicalOperator translateSubscribeFeed(CompiledSubscribeFeedStatement sfs, DatasetDataSource targetDatasource, LogicalVariable unnestVar, ProjectOperator project, ArrayList<Mutable<ILogicalExpression>> exprs, LogicalVariable resVar, List<Mutable<ILogicalExpression>> varRefsForLoading, Mutable<ILogicalExpression> varRef, ILogicalOperator assign, List<String> additionalFilteringField, AssignOperator additionalFilteringAssign, List<Mutable<ILogicalExpression>> additionalFilteringExpressions) throws AlgebricksException { // if the feed is a change feed (i.e, performs different operations), we need to project op variable InsertDeleteUpsertOperator feedModificationOp; AssignOperator metaAndKeysAssign;/* ww w .j a v a 2 s .c o m*/ List<LogicalVariable> metaAndKeysVars = null; List<Mutable<ILogicalExpression>> metaAndKeysExprs = null; List<Mutable<ILogicalExpression>> metaExpSingletonList = null; Feed feed = metadataProvider.findFeed(sfs.getDataverseName(), sfs.getFeedName()); boolean isChangeFeed = ExternalDataUtils.isChangeFeed(feed.getAdapterConfiguration()); boolean isUpsertFeed = ExternalDataUtils.isUpsertFeed(feed.getAdapterConfiguration()); if (targetDatasource.getDataset().hasMetaPart() || isChangeFeed) { metaAndKeysVars = new ArrayList<>(); metaAndKeysExprs = new ArrayList<>(); if (targetDatasource.getDataset().hasMetaPart()) { // add the meta function IFunctionInfo finfoMeta = FunctionUtil.getFunctionInfo(AsterixBuiltinFunctions.META); ScalarFunctionCallExpression metaFunction = new ScalarFunctionCallExpression(finfoMeta, new MutableObject<>(new VariableReferenceExpression(unnestVar))); // create assign for the meta part LogicalVariable metaVar = context.newVar(); metaExpSingletonList = new ArrayList<>(1); metaExpSingletonList.add(new MutableObject<>(new VariableReferenceExpression(metaVar))); metaAndKeysVars.add(metaVar); metaAndKeysExprs.add(new MutableObject<>(metaFunction)); project.getVariables().add(metaVar); } } if (isChangeFeed) { varRefsForLoading.clear(); for (Mutable<ILogicalExpression> assignExpr : exprs) { if (assignExpr.getValue().getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) { AbstractFunctionCallExpression funcCall = (AbstractFunctionCallExpression) assignExpr .getValue(); funcCall.substituteVar(resVar, unnestVar); LogicalVariable pkVar = context.newVar(); metaAndKeysVars.add(pkVar); metaAndKeysExprs.add(new MutableObject<>(assignExpr.getValue())); project.getVariables().add(pkVar); varRefsForLoading.add(new MutableObject<>(new VariableReferenceExpression(pkVar))); } } // A change feed, we don't need the assign to access PKs feedModificationOp = new InsertDeleteUpsertOperator(targetDatasource, varRef, varRefsForLoading, metaExpSingletonList, InsertDeleteUpsertOperator.Kind.UPSERT, false); // Create and add a new variable used for representing the original record feedModificationOp.setPrevRecordVar(context.newVar()); feedModificationOp.setPrevRecordType(targetDatasource.getItemType()); if (targetDatasource.getDataset().hasMetaPart()) { List<LogicalVariable> metaVars = new ArrayList<>(); metaVars.add(context.newVar()); feedModificationOp.setPrevAdditionalNonFilteringVars(metaVars); List<Object> metaTypes = new ArrayList<>(); metaTypes.add(targetDatasource.getMetaItemType()); feedModificationOp.setPrevAdditionalNonFilteringTypes(metaTypes); } if (additionalFilteringField != null) { feedModificationOp.setPrevFilterVar(context.newVar()); feedModificationOp.setPrevFilterType(((ARecordType) targetDatasource.getItemType()) .getFieldType(additionalFilteringField.get(0))); additionalFilteringAssign.getInputs().clear(); additionalFilteringAssign.getInputs().add(assign.getInputs().get(0)); feedModificationOp.getInputs().add(new MutableObject<>(additionalFilteringAssign)); } else { feedModificationOp.getInputs().add(assign.getInputs().get(0)); } } else { final InsertDeleteUpsertOperator.Kind opKind = isUpsertFeed ? InsertDeleteUpsertOperator.Kind.UPSERT : InsertDeleteUpsertOperator.Kind.INSERT; feedModificationOp = new InsertDeleteUpsertOperator(targetDatasource, varRef, varRefsForLoading, metaExpSingletonList, opKind, false); if (isUpsertFeed) { feedModificationOp.setPrevRecordVar(context.newVar()); feedModificationOp.setPrevRecordType(targetDatasource.getItemType()); } feedModificationOp.getInputs().add(new MutableObject<>(assign)); } if (targetDatasource.getDataset().hasMetaPart() || isChangeFeed) { metaAndKeysAssign = new AssignOperator(metaAndKeysVars, metaAndKeysExprs); metaAndKeysAssign.getInputs().add(project.getInputs().get(0)); project.getInputs().set(0, new MutableObject<>(metaAndKeysAssign)); } feedModificationOp.setAdditionalFilteringExpressions(additionalFilteringExpressions); ILogicalOperator leafOperator = new DelegateOperator(new CommitOperator(true)); leafOperator.getInputs().add(new MutableObject<>(feedModificationOp)); return leafOperator; }
From source file:org.apache.asterix.translator.LangExpressionToPlanTranslator.java
private ILogicalOperator translateUpsert(DatasetDataSource targetDatasource, Mutable<ILogicalExpression> varRef, List<Mutable<ILogicalExpression>> varRefsForLoading, List<Mutable<ILogicalExpression>> additionalFilteringExpressions, ILogicalOperator assign, List<String> additionalFilteringField, LogicalVariable unnestVar, ProjectOperator project, List<Mutable<ILogicalExpression>> exprs, LogicalVariable resVar, AssignOperator additionalFilteringAssign, ICompiledDmlStatement stmt) throws AlgebricksException { if (!targetDatasource.getDataset().allow(project, Dataset.OP_UPSERT)) { throw new AlgebricksException(targetDatasource.getDataset().getDatasetName() + ": upsert into dataset is not supported on Datasets with Meta records"); }//from w ww .j av a 2s . com CompiledUpsertStatement compiledUpsert = (CompiledUpsertStatement) stmt; InsertDeleteUpsertOperator upsertOp; ILogicalOperator leafOperator; if (targetDatasource.getDataset().hasMetaPart()) { if (compiledUpsert.getReturnQuery() != null) { throw new AlgebricksException("Returning not allowed on datasets with Meta records"); } AssignOperator metaAndKeysAssign; List<LogicalVariable> metaAndKeysVars; List<Mutable<ILogicalExpression>> metaAndKeysExprs; List<Mutable<ILogicalExpression>> metaExpSingletonList; metaAndKeysVars = new ArrayList<>(); metaAndKeysExprs = new ArrayList<>(); // add the meta function IFunctionInfo finfoMeta = FunctionUtil.getFunctionInfo(AsterixBuiltinFunctions.META); ScalarFunctionCallExpression metaFunction = new ScalarFunctionCallExpression(finfoMeta, new MutableObject<>(new VariableReferenceExpression(unnestVar))); // create assign for the meta part LogicalVariable metaVar = context.newVar(); metaExpSingletonList = new ArrayList<>(1); metaExpSingletonList.add(new MutableObject<>(new VariableReferenceExpression(metaVar))); metaAndKeysVars.add(metaVar); metaAndKeysExprs.add(new MutableObject<>(metaFunction)); project.getVariables().add(metaVar); varRefsForLoading.clear(); for (Mutable<ILogicalExpression> assignExpr : exprs) { if (assignExpr.getValue().getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) { AbstractFunctionCallExpression funcCall = (AbstractFunctionCallExpression) assignExpr .getValue(); funcCall.substituteVar(resVar, unnestVar); LogicalVariable pkVar = context.newVar(); metaAndKeysVars.add(pkVar); metaAndKeysExprs.add(new MutableObject<>(assignExpr.getValue())); project.getVariables().add(pkVar); varRefsForLoading.add(new MutableObject<>(new VariableReferenceExpression(pkVar))); } } // A change feed, we don't need the assign to access PKs upsertOp = new InsertDeleteUpsertOperator(targetDatasource, varRef, varRefsForLoading, metaExpSingletonList, InsertDeleteUpsertOperator.Kind.UPSERT, false); // Create and add a new variable used for representing the original record upsertOp.setPrevRecordVar(context.newVar()); upsertOp.setPrevRecordType(targetDatasource.getItemType()); if (targetDatasource.getDataset().hasMetaPart()) { List<LogicalVariable> metaVars = new ArrayList<>(); metaVars.add(context.newVar()); upsertOp.setPrevAdditionalNonFilteringVars(metaVars); List<Object> metaTypes = new ArrayList<>(); metaTypes.add(targetDatasource.getMetaItemType()); upsertOp.setPrevAdditionalNonFilteringTypes(metaTypes); } if (additionalFilteringField != null) { upsertOp.setPrevFilterVar(context.newVar()); upsertOp.setPrevFilterType(((ARecordType) targetDatasource.getItemType()) .getFieldType(additionalFilteringField.get(0))); additionalFilteringAssign.getInputs().clear(); additionalFilteringAssign.getInputs().add(assign.getInputs().get(0)); upsertOp.getInputs().add(new MutableObject<>(additionalFilteringAssign)); } else { upsertOp.getInputs().add(assign.getInputs().get(0)); } metaAndKeysAssign = new AssignOperator(metaAndKeysVars, metaAndKeysExprs); metaAndKeysAssign.getInputs().add(project.getInputs().get(0)); project.getInputs().set(0, new MutableObject<>(metaAndKeysAssign)); upsertOp.setAdditionalFilteringExpressions(additionalFilteringExpressions); leafOperator = new DelegateOperator(new CommitOperator(true)); leafOperator.getInputs().add(new MutableObject<>(upsertOp)); } else { upsertOp = new InsertDeleteUpsertOperator(targetDatasource, varRef, varRefsForLoading, InsertDeleteUpsertOperator.Kind.UPSERT, false); upsertOp.setAdditionalFilteringExpressions(additionalFilteringExpressions); upsertOp.getInputs().add(new MutableObject<>(assign)); // Create and add a new variable used for representing the original record ARecordType recordType = (ARecordType) targetDatasource.getItemType(); upsertOp.setPrevRecordVar(context.newVar()); upsertOp.setPrevRecordType(recordType); if (additionalFilteringField != null) { upsertOp.setPrevFilterVar(context.newVar()); upsertOp.setPrevFilterType(recordType.getFieldType(additionalFilteringField.get(0))); } if (compiledUpsert.getReturnQuery() != null) { leafOperator = createReturningQuery(compiledUpsert, upsertOp); } else { leafOperator = new DelegateOperator(new CommitOperator(true)); leafOperator.getInputs().add(new MutableObject<ILogicalOperator>(upsertOp)); } } return leafOperator; }
From source file:org.apache.asterix.translator.LangExpressionToPlanTranslator.java
protected Pair<ILogicalExpression, Mutable<ILogicalOperator>> langExprToAlgExpression(Expression expr, Mutable<ILogicalOperator> topOpRef) throws AsterixException { switch (expr.getKind()) { case VARIABLE_EXPRESSION: VariableReferenceExpression ve = new VariableReferenceExpression( context.getVar(((VariableExpr) expr).getVar().getId())); return new Pair<>(ve, topOpRef); case LITERAL_EXPRESSION: LiteralExpr val = (LiteralExpr) expr; return new Pair<>(new ConstantExpression( new AsterixConstantValue(ConstantHelper.objectFromLiteral(val.getValue()))), topOpRef); default:/* w w w . ja v a 2s . c o m*/ if (expressionNeedsNoNesting(expr)) { Pair<ILogicalOperator, LogicalVariable> p = expr.accept(this, topOpRef); ILogicalExpression exp = ((AssignOperator) p.first).getExpressions().get(0).getValue(); return new Pair<>(exp, p.first.getInputs().get(0)); } else { Mutable<ILogicalOperator> srcRef = new MutableObject<>(); Pair<ILogicalOperator, LogicalVariable> p = expr.accept(this, srcRef); if (p.first.getOperatorTag() == LogicalOperatorTag.SUBPLAN) { if (topOpRef.getValue() != null) { srcRef.setValue(topOpRef.getValue()); } else { // Re-binds the bottom operator reference to {@code topOpRef}. rebindBottomOpRef(p.first, srcRef, topOpRef); } Mutable<ILogicalOperator> top2 = new MutableObject<>(p.first); return new Pair<>(new VariableReferenceExpression(p.second), top2); } else { SubplanOperator s = new SubplanOperator(); s.getInputs().add(topOpRef); srcRef.setValue(new NestedTupleSourceOperator(new MutableObject<>(s))); Mutable<ILogicalOperator> planRoot = new MutableObject<>(p.first); s.setRootOp(planRoot); return new Pair<>(new VariableReferenceExpression(p.second), new MutableObject<>(s)); } } } }
From source file:org.apache.asterix.translator.LangExpressionToPlanTranslator.java
private boolean rebindBottomOpRef(ILogicalOperator currentOp, Mutable<ILogicalOperator> opRef, Mutable<ILogicalOperator> replacementOpRef) { int index = 0; for (Mutable<ILogicalOperator> childRef : currentOp.getInputs()) { if (childRef == opRef) { currentOp.getInputs().set(index, replacementOpRef); return true; } else {// w w w . j a v a 2 s .c om if (rebindBottomOpRef(childRef.getValue(), opRef, replacementOpRef)) { return true; } } ++index; } return false; }
From source file:org.apache.asterix.translator.LangExpressionToPlanTranslator.java
/** * Eliminate shared operator references in a query plan rooted at <code>currentOpRef.getValue()</code>. * Deep copy a new query plan subtree whenever there is a shared operator reference. * * @param currentOpRef,//from w w w . ja va 2 s .c o m * the operator reference to consider * @param opRefSet, * the set storing seen operator references so far. * @return a mapping that maps old variables to new variables, for the ancestors of * <code>currentOpRef</code> to replace variables properly. * @throws AsterixException */ private LinkedHashMap<LogicalVariable, LogicalVariable> eliminateSharedOperatorReference( Mutable<ILogicalOperator> currentOpRef, Set<Mutable<ILogicalOperator>> opRefSet) throws AsterixException { try { opRefSet.add(currentOpRef); AbstractLogicalOperator currentOperator = (AbstractLogicalOperator) currentOpRef.getValue(); // Recursively eliminates shared references in nested plans. if (currentOperator.hasNestedPlans()) { // Since a nested plan tree itself can never be shared with another nested plan tree in // another operator, the operation called in the if block does not need to replace // any variables further for <code>currentOpRef.getValue()</code> nor its ancestor. AbstractOperatorWithNestedPlans opWithNestedPlan = (AbstractOperatorWithNestedPlans) currentOperator; for (ILogicalPlan plan : opWithNestedPlan.getNestedPlans()) { for (Mutable<ILogicalOperator> rootRef : plan.getRoots()) { Set<Mutable<ILogicalOperator>> nestedOpRefSet = new HashSet<>(); eliminateSharedOperatorReference(rootRef, nestedOpRefSet); } } } int childIndex = 0; LinkedHashMap<LogicalVariable, LogicalVariable> varMap = new LinkedHashMap<>(); for (Mutable<ILogicalOperator> childRef : currentOperator.getInputs()) { if (opRefSet.contains(childRef)) { // There is a shared operator reference in the query plan. // Deep copies the child plan. LogicalOperatorDeepCopyWithNewVariablesVisitor visitor = new LogicalOperatorDeepCopyWithNewVariablesVisitor( context, null); ILogicalOperator newChild = childRef.getValue().accept(visitor, null); LinkedHashMap<LogicalVariable, LogicalVariable> cloneVarMap = visitor .getInputToOutputVariableMapping(); // Substitute variables according to the deep copy which generates new variables. VariableUtilities.substituteVariables(currentOperator, cloneVarMap, null); varMap.putAll(cloneVarMap); // Sets the new child. childRef = new MutableObject<>(newChild); currentOperator.getInputs().set(childIndex, childRef); } // Recursively eliminate shared operator reference for the operator subtree, // even if it is a deep copy of some other one. LinkedHashMap<LogicalVariable, LogicalVariable> childVarMap = eliminateSharedOperatorReference( childRef, opRefSet); // Substitute variables according to the new subtree. VariableUtilities.substituteVariables(currentOperator, childVarMap, null); // Updates mapping like <$a, $b> in varMap to <$a, $c>, where there is a mapping <$b, $c> // in childVarMap. for (Map.Entry<LogicalVariable, LogicalVariable> entry : varMap.entrySet()) { LogicalVariable newVar = childVarMap.get(entry.getValue()); if (newVar != null) { entry.setValue(newVar); } } varMap.putAll(childVarMap); ++childIndex; } // Only retain live variables for parent operators to substitute variables. Set<LogicalVariable> liveVars = new HashSet<>(); VariableUtilities.getLiveVariables(currentOperator, liveVars); varMap.values().retainAll(liveVars); return varMap; } catch (AlgebricksException e) { throw new AsterixException(e); } }
From source file:org.apache.asterix.translator.SqlppExpressionToPlanTranslator.java
@Override public Pair<ILogicalOperator, LogicalVariable> visit(FromTerm fromTerm, Mutable<ILogicalOperator> tupSource) throws AsterixException { LogicalVariable fromVar = context.newVar(fromTerm.getLeftVariable()); Expression fromExpr = fromTerm.getLeftExpression(); Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(fromExpr, tupSource); ILogicalOperator unnestOp;/*from w ww. jav a 2 s . co m*/ if (fromTerm.hasPositionalVariable()) { LogicalVariable pVar = context.newVar(fromTerm.getPositionalVariable()); // We set the positional variable type as INT64 type. unnestOp = new UnnestOperator(fromVar, new MutableObject<ILogicalExpression>(makeUnnestExpression(eo.first)), pVar, BuiltinType.AINT64, new AqlPositionWriter()); } else { unnestOp = new UnnestOperator(fromVar, new MutableObject<ILogicalExpression>(makeUnnestExpression(eo.first))); } unnestOp.getInputs().add(eo.second); // Processes joins, unnests, and nests. Mutable<ILogicalOperator> topOpRef = new MutableObject<>(unnestOp); if (fromTerm.hasCorrelateClauses()) { for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses()) { if (correlateClause.getClauseType() == ClauseType.UNNEST_CLAUSE) { // Correlation is allowed. topOpRef = new MutableObject<>(correlateClause.accept(this, topOpRef).first); } else { // Correlation is dis-allowed. uncorrelatedLeftBranchStack.push(topOpRef); topOpRef = new MutableObject<>(correlateClause.accept(this, tupSource).first); } } } return new Pair<>(topOpRef.getValue(), fromVar); }
From source file:org.apache.asterix.translator.SqlppExpressionToPlanTranslator.java
private void replaceNtsWithEtsTopDown(Mutable<ILogicalOperator> opRef) { ILogicalOperator op = opRef.getValue(); if (op.getOperatorTag() == LogicalOperatorTag.NESTEDTUPLESOURCE) { opRef.setValue(new EmptyTupleSourceOperator()); }/* w w w.j a v a 2 s . co m*/ for (Mutable<ILogicalOperator> childRef : op.getInputs()) { replaceNtsWithEtsTopDown(childRef); } }
From source file:org.apache.hyracks.algebricks.compiler.rewriter.rulecontrollers.SequentialFirstRuleCheckFixpointRuleControllerTest.java
@Test public void testRewriteWithRuleCollection() throws Exception { SequentialFirstRuleCheckFixpointRuleController ruleController = new SequentialFirstRuleCheckFixpointRuleController( true);//from w w w. ja va2 s. c o m // Specifies three rules - R1, R2, and R3. IAlgebraicRewriteRule firstRule = mock(IAlgebraicRewriteRule.class); IAlgebraicRewriteRule secondRule = mock(IAlgebraicRewriteRule.class); IAlgebraicRewriteRule thirdRule = mock(IAlgebraicRewriteRule.class); List<IAlgebraicRewriteRule> ruleList = new LinkedList<>(); ruleList.add(firstRule); ruleList.add(secondRule); ruleList.add(thirdRule); @SuppressWarnings("unchecked") Mutable<ILogicalOperator> root = PowerMockito.mock(Mutable.class); AbstractLogicalOperator rootOp = PowerMockito.mock(AbstractLogicalOperator.class); List<Mutable<ILogicalOperator>> emptyList = new ArrayList<>(); PowerMockito.when(root.getValue()).thenReturn(rootOp); PowerMockito.when(rootOp.getInputs()).thenReturn(emptyList); // Case 1: the first rule returns true in the first iteration. // Iteration1: R1 true, R2 false, R3 false // Iteration2: R1 false, R2 false, R3 false PowerMockito.when(firstRule.rewritePre(any(), any())).thenReturn(true).thenReturn(false); PowerMockito.when(secondRule.rewritePre(any(), any())).thenReturn(false); PowerMockito.when(thirdRule.rewritePre(any(), any())).thenReturn(false); ruleController.rewriteWithRuleCollection(root, ruleList); // The count should be two for all rules. verify(firstRule, times(2)).rewritePre(any(), any()); verify(secondRule, times(2)).rewritePre(any(), any()); verify(thirdRule, times(2)).rewritePre(any(), any()); // Case 2: the first rule returns false in the first iteration. // Iteration1: R1 false (R2 and R3 should not be invoked.) reset(firstRule); reset(secondRule); reset(thirdRule); PowerMockito.when(firstRule.rewritePre(any(), any())).thenReturn(false); PowerMockito.when(secondRule.rewritePre(any(), any())).thenReturn(true); PowerMockito.when(thirdRule.rewritePre(any(), any())).thenReturn(true); ruleController.rewriteWithRuleCollection(root, ruleList); // The count should be one for the first rule. verify(firstRule, times(1)).rewritePre(any(), any()); verify(secondRule, times(0)).rewritePre(any(), any()); verify(thirdRule, times(0)).rewritePre(any(), any()); // Case 3: a mixture of returning true/false. // Iteration1: R1 true, R2 true, R3 false // Iteration2: R1 false, R2 true, R3 false // Iteration3: R1 false, R2 false, R3 false // So, the iteration should be stopped after the iteration 3. reset(firstRule); reset(secondRule); reset(thirdRule); PowerMockito.when(firstRule.rewritePre(any(), any())).thenReturn(true).thenReturn(false); PowerMockito.when(secondRule.rewritePre(any(), any())).thenReturn(true).thenReturn(true).thenReturn(false); PowerMockito.when(thirdRule.rewritePre(any(), any())).thenReturn(false); ruleController.rewriteWithRuleCollection(root, ruleList); // The count should be three for all rules. verify(firstRule, times(3)).rewritePre(any(), any()); verify(secondRule, times(3)).rewritePre(any(), any()); verify(thirdRule, times(3)).rewritePre(any(), any()); }
From source file:org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression.java
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("function-call: " + finfo.getFunctionIdentifier() + ", Args:["); // + arguments; boolean first = true; for (Mutable<ILogicalExpression> ref : arguments) { if (first) { first = false;/*from www . j a v a 2s .com*/ } else { sb.append(", "); } sb.append(ref.getValue()); } sb.append("]"); return sb.toString(); }
From source file:org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator.java
@Override public void recomputeSchema() { schema = new ArrayList<LogicalVariable>(); ILogicalOperator topOp = dataSourceReference.getValue(); for (Mutable<ILogicalOperator> i : topOp.getInputs()) { List<LogicalVariable> inputSchema = i.getValue().getSchema(); if (inputSchema != null) { schema.addAll(inputSchema);/* ww w. j a v a 2 s .co m*/ } } }