use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans in project asterixdb by apache.
the class RemoveRedundantListifyRule method applyRuleDown.
private boolean applyRuleDown(Mutable<ILogicalOperator> opRef, Set<LogicalVariable> varSet, IOptimizationContext context) throws AlgebricksException {
boolean changed = applies(opRef, varSet, context);
changed |= appliesForReverseCase(opRef, varSet, context);
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
VariableUtilities.getUsedVariables(op, varSet);
if (op.hasNestedPlans()) {
// Variables used by the parent operators should be live at op.
Set<LogicalVariable> localLiveVars = new ListSet<LogicalVariable>();
VariableUtilities.getLiveVariables(op, localLiveVars);
varSet.retainAll(localLiveVars);
AbstractOperatorWithNestedPlans aonp = (AbstractOperatorWithNestedPlans) op;
for (ILogicalPlan p : aonp.getNestedPlans()) {
for (Mutable<ILogicalOperator> r : p.getRoots()) {
if (applyRuleDown(r, varSet, context)) {
changed = true;
}
context.addToDontApplySet(this, r.getValue());
}
}
}
for (Mutable<ILogicalOperator> i : op.getInputs()) {
if (applyRuleDown(i, varSet, context)) {
changed = true;
}
context.addToDontApplySet(this, i.getValue());
}
return changed;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans in project asterixdb by apache.
the class LangExpressionToPlanTranslator method eliminateSharedOperatorReference.
/**
* 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,
* 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 CompilationException
*/
private LinkedHashMap<LogicalVariable, LogicalVariable> eliminateSharedOperatorReference(Mutable<ILogicalOperator> currentOpRef, Set<Mutable<ILogicalOperator>> opRefSet) throws CompilationException {
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);
// 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 CompilationException(e);
}
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans in project asterixdb by apache.
the class OperatorManipulationUtil method computeTypeEnvironmentBottomUp.
/**
* Compute type environment of a newly generated operator {@code op} and its input.
*
* @param op,
* the logical operator.
* @param context,the
* optimization context.
* @throws AlgebricksException
*/
public static void computeTypeEnvironmentBottomUp(ILogicalOperator op, ITypingContext context) throws AlgebricksException {
for (Mutable<ILogicalOperator> children : op.getInputs()) {
computeTypeEnvironmentBottomUp(children.getValue(), context);
}
AbstractLogicalOperator abstractOp = (AbstractLogicalOperator) op;
if (abstractOp.hasNestedPlans()) {
for (ILogicalPlan p : ((AbstractOperatorWithNestedPlans) op).getNestedPlans()) {
for (Mutable<ILogicalOperator> rootRef : p.getRoots()) {
computeTypeEnvironmentBottomUp(rootRef.getValue(), context);
}
}
}
context.computeAndSetTypeEnvironmentForOperator(op);
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans in project asterixdb by apache.
the class OperatorManipulationUtil method substituteVarRec.
public static void substituteVarRec(AbstractLogicalOperator op, LogicalVariable v1, LogicalVariable v2, boolean goThroughNts, ITypingContext ctx) throws AlgebricksException {
VariableUtilities.substituteVariables(op, v1, v2, goThroughNts, ctx);
for (Mutable<ILogicalOperator> opRef2 : op.getInputs()) {
substituteVarRec((AbstractLogicalOperator) opRef2.getValue(), v1, v2, goThroughNts, ctx);
}
if (op.getOperatorTag() == LogicalOperatorTag.NESTEDTUPLESOURCE && goThroughNts) {
NestedTupleSourceOperator nts = (NestedTupleSourceOperator) op;
if (nts.getDataSourceReference() != null) {
AbstractLogicalOperator op2 = (AbstractLogicalOperator) nts.getDataSourceReference().getValue().getInputs().get(0).getValue();
substituteVarRec(op2, v1, v2, goThroughNts, ctx);
}
}
if (op.hasNestedPlans()) {
AbstractOperatorWithNestedPlans aonp = (AbstractOperatorWithNestedPlans) op;
for (ILogicalPlan p : aonp.getNestedPlans()) {
for (Mutable<ILogicalOperator> ref : p.getRoots()) {
AbstractLogicalOperator aop = (AbstractLogicalOperator) ref.getValue();
substituteVarRec(aop, v1, v2, goThroughNts, ctx);
}
}
}
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans in project asterixdb by apache.
the class OperatorPropertiesUtil method typeOpRec.
public static void typeOpRec(Mutable<ILogicalOperator> r, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op = (AbstractLogicalOperator) r.getValue();
for (Mutable<ILogicalOperator> i : op.getInputs()) {
typeOpRec(i, context);
}
if (op.hasNestedPlans()) {
for (ILogicalPlan p : ((AbstractOperatorWithNestedPlans) op).getNestedPlans()) {
typePlan(p, context);
}
}
context.computeAndSetTypeEnvironmentForOperator(op);
}
Aggregations