private ILogicalOperator visit(ILogicalOperator op) throws AlgebricksException { List<Map<LogicalVariable, LogicalVariable>> varMapSnapshots = new ArrayList<>(); for (Mutable<ILogicalOperator> childRef : op.getInputs()) { ILogicalOperator newChild = childRef.getValue().accept(this, null); childRef.setValue(newChild); // Replaces variables in op with the mapping obtained from one // child. VariableUtilities.substituteVariables(op, varMap, ctx); // Keep the map from current child and move to the next child. varMapSnapshots.add(new HashMap<LogicalVariable, LogicalVariable>(varMap)); varMap.clear(); } // Combine mappings from all children. for (Map<LogicalVariable, LogicalVariable> map : varMapSnapshots) { varMap.putAll(map); } // Only propagates necessary mappings to the parent operator. Set<LogicalVariable> liveVars = new HashSet<LogicalVariable>(); VariableUtilities.getLiveVariables(op, liveVars); varMap.values().retainAll(liveVars); return op; }
/** * Adds the free variables of the operator to the given set. * * @param op * @param freeVars */ public static void getFreeVariablesInOp(ILogicalOperator op, Set<LogicalVariable> freeVars) throws AlgebricksException { // Obs: doesn't return expected result for op. with nested plans. VariableUtilities.getUsedVariables(op, freeVars); HashSet<LogicalVariable> produced = new HashSet<>(); VariableUtilities.getProducedVariables(op, produced); for (LogicalVariable v : produced) { freeVars.remove(v); } }
public static void getSubplanLocalLiveVariables(ILogicalOperator op, Collection<LogicalVariable> liveVariables) throws AlgebricksException { VariableUtilities.getLiveVariables(op, liveVariables); Set<LogicalVariable> locallyProducedVars = new HashSet<>(); VariableUtilities.getProducedVariablesInDescendantsAndSelf(op, locallyProducedVars); liveVariables.retainAll(locallyProducedVars); }
private boolean opsAreIndependent(ILogicalOperator unnestOp, ILogicalOperator outer) throws AlgebricksException { if (unnestOp.equals(outer)) { return false; } List<LogicalVariable> opUsedVars = new ArrayList<LogicalVariable>(); VariableUtilities.getUsedVariables(unnestOp, opUsedVars); Set<LogicalVariable> op2LiveVars = new HashSet<LogicalVariable>(); VariableUtilities.getLiveVariables(outer, op2LiveVars); for (LogicalVariable usedVar : opUsedVars) { if (op2LiveVars.contains(usedVar)) { return false; } } return true; }
VariableUtilities.getUsedVariables(aggregate, free); VariableUtilities.getProducedVariables(op3, prod); free.removeAll(prod); VariableUtilities.getUsedVariables(op3, free); botRef = op3Ref; op3Ref = op3.getInputs().get(0); return false; VariableUtilities.getUsedVariables(join, free); pkVars = new HashSet<LogicalVariable>(); VariableUtilities.getLiveVariables(subplanInput, pkVars); VariableUtilities.getProducedVariablesInDescendantsAndSelf(subplanInput, producedVars); ILogicalOperator inputToRunningAggregate = right.getInputs().get(0).getValue(); Set<LogicalVariable> producedVars = new ListSet<LogicalVariable>(); VariableUtilities.getProducedVariables(inputToRunningAggregate, producedVars); if (!producedVars.isEmpty()) { VariableUtilities.getLiveVariables(subplan.getInputs().get(0).getValue(), underVars); underVars.removeAll(pkVars); Map<LogicalVariable, LogicalVariable> mappedVars = buildVarExprList(pkVars, context, g, g.getGroupByList());
public static boolean retainInputs(List<LogicalVariable> dataSourceVariables, ILogicalOperator sourceOp, List<Mutable<ILogicalOperator>> afterSelectRefs) throws AlgebricksException { List<LogicalVariable> usedVars = new ArrayList<>(); List<LogicalVariable> producedVars = new ArrayList<>(); List<LogicalVariable> liveVars = new ArrayList<>(); VariableUtilities.getLiveVariables(sourceOp, liveVars); for (Mutable<ILogicalOperator> opMutable : afterSelectRefs) { ILogicalOperator op = opMutable.getValue(); VariableUtilities.getUsedVariables(op, usedVars); VariableUtilities.getProducedVariables(op, producedVars); } usedVars.removeAll(producedVars); usedVars.removeAll(dataSourceVariables); usedVars.retainAll(liveVars); return usedVars.isEmpty() ? false : true; }
@Override public Void visitNestedTupleSourceOperator(NestedTupleSourceOperator op, Void arg) throws AlgebricksException { VariableUtilities.getLiveVariables(op.getSourceOperator(), schemaVariables); return null; }
@Override public Void visitInsertDeleteUpsertOperator(InsertDeleteUpsertOperator op, Void arg) throws AlgebricksException { // produced first VariableUtilities.getProducedVariables(op, schemaVariables); // then propagated for (Mutable<ILogicalOperator> c : op.getInputs()) { VariableUtilities.getLiveVariables(c.getValue(), schemaVariables); } return null; }
private ILogicalOperator copyAndSubstituteVar(ILogicalOperator op, ILogicalOperator argOp) throws AlgebricksException { ILogicalOperator newOp = OperatorManipulationUtil.deepCopy(argOp); variableMapping.clear(); IsomorphismUtilities.mapVariablesTopDown(op, argOp, variableMapping); List<LogicalVariable> liveVars = new ArrayList<>(); for (int i = 0; i < argOp.getInputs().size(); i++) { VariableUtilities.getLiveVariables(argOp.getInputs().get(i).getValue(), liveVars); } List<LogicalVariable> producedVars = new ArrayList<>(); VariableUtilities.getProducedVariables(argOp, producedVars); List<LogicalVariable> producedVarsNew = new ArrayList<>(); VariableUtilities.getProducedVariables(op, producedVarsNew); if (producedVars.size() != producedVarsNew.size()) { return newOp; } for (Entry<LogicalVariable, LogicalVariable> map : variableMapping.entrySet()) { if (liveVars.contains(map.getKey())) { VariableUtilities.substituteVariables(newOp, map.getKey(), map.getValue(), null); } } for (int i = 0; i < producedVars.size(); i++) { VariableUtilities.substituteVariables(newOp, producedVars.get(i), producedVarsNew.get(i), null); } return newOp; }
private boolean independentOperator(ILogicalOperator op, Set<LogicalVariable> liveVarsBeforeSubplan) throws AlgebricksException { Set<LogicalVariable> usedVars = new HashSet<>(); VariableUtilities.getUsedVariables(op, usedVars); return liveVarsBeforeSubplan.containsAll(usedVars); }
@Override public Void visitUnionOperator(UnionAllOperator op, Void arg) throws AlgebricksException { VariableUtilities.getProducedVariables(op, schemaVariables); return null; }
public static void getProducedVariablesInDescendantsAndSelf(ILogicalOperator op, Collection<LogicalVariable> vars) throws AlgebricksException { // DFS traversal VariableUtilities.getProducedVariables(op, vars); for (Mutable<ILogicalOperator> c : op.getInputs()) { getProducedVariablesInDescendantsAndSelf(c.getValue(), vars); } }
public static void substituteVariables(ILogicalOperator op, List<Pair<LogicalVariable, LogicalVariable>> oldVarNewVarMapHistory, ITypingContext ctx) throws AlgebricksException { for (Pair<LogicalVariable, LogicalVariable> entry : oldVarNewVarMapHistory) { VariableUtilities.substituteVariables(op, entry.first, entry.second, ctx); } }
@Override public Boolean visitUnionOperator(UnionAllOperator op, ILogicalOperator arg) throws AlgebricksException { AbstractLogicalOperator aop = (AbstractLogicalOperator) arg; if (aop.getOperatorTag() != LogicalOperatorTag.UNIONALL) { return Boolean.FALSE; } UnionAllOperator unionOpArg = (UnionAllOperator) copyAndSubstituteVar(op, arg); List<Triple<LogicalVariable, LogicalVariable, LogicalVariable>> mapping = op.getVariableMappings(); List<Triple<LogicalVariable, LogicalVariable, LogicalVariable>> mappingArg = unionOpArg.getVariableMappings(); if (mapping.size() != mappingArg.size()) { return Boolean.FALSE; } return VariableUtilities.varListEqualUnordered(mapping, mappingArg); }
VariableUtilities.getUsedVariables(op2, used); } else if (op2.getOperatorTag() == LogicalOperatorTag.SUBPLAN) { VariableUtilities.getUsedVariables(op2, used); Set<LogicalVariable> subplanProducedAndDown = new HashSet<LogicalVariable>(); VariableUtilities.getProducedVariablesInDescendantsAndSelf(op3, subplanProducedAndDown); used.removeAll(subplanProducedAndDown); } else {
@Override public Void visitSubplanOperator(SubplanOperator op, Void arg) throws AlgebricksException { Set<LogicalVariable> producedVars = new HashSet<>(); Set<LogicalVariable> liveVars = new HashSet<>(); for (ILogicalPlan p : op.getNestedPlans()) { for (Mutable<ILogicalOperator> r : p.getRoots()) { VariableUtilities.getProducedVariablesInDescendantsAndSelf(r.getValue(), producedVars); VariableUtilities.getSubplanLocalLiveVariables(r.getValue(), liveVars); } } producedVars.retainAll(liveVars); producedVariables.addAll(producedVars); return null; }
public static void getUsedVariablesInDescendantsAndSelf(ILogicalOperator op, Collection<LogicalVariable> vars) throws AlgebricksException { // DFS traversal VariableUtilities.getUsedVariables(op, vars); for (Mutable<ILogicalOperator> c : op.getInputs()) { getUsedVariablesInDescendantsAndSelf(c.getValue(), vars); } }
VariableUtilities.getProducedVariables(op, producedVars); IVariableTypeEnvironment env = op.computeOutputTypeEnvironment(context); for (int i = 0; i < producedVars.size(); i++) { context.computeAndSetTypeEnvironmentForOperator(newAssignOperator); newAssignOperator.computeOutputTypeEnvironment(context); VariableUtilities.substituteVariables(parent, recordVar, newAssignVar, context); return newAssignVar; if (replacedVar != null) { VariableUtilities.substituteVariables(parent, recordVar, replacedVar, context); return replacedVar;
/*** * @param context * the optimization context * @param subplanInputOperator * the input operator to the target SubplanOperator * @param nts * the NestedTupleSourceOperator to be replaced by <code>subplanInputOperator</code> * @throws AlgebricksException */ public InlineLeftNtsInSubplanJoinFlatteningVisitor(IOptimizationContext context, ILogicalOperator subplanInputOperator, ILogicalOperator nts) throws AlgebricksException { this.context = context; this.subplanInputOperator = subplanInputOperator; this.targetNts = nts; VariableUtilities.getSubplanLocalLiveVariables(subplanInputOperator, liveVarsFromSubplanInput); }
public static void substituteVariablesInDescendantsAndSelf(ILogicalOperator op, LogicalVariable v1, LogicalVariable v2, ITypingContext ctx) throws AlgebricksException { for (Mutable<ILogicalOperator> childOp : op.getInputs()) { substituteVariablesInDescendantsAndSelf(childOp.getValue(), v1, v2, ctx); } substituteVariables(op, v1, v2, true, ctx); }