>From Janhavi Tripurwar <[email protected]>:
Janhavi Tripurwar has uploaded this change for review. (
https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/21370?usp=email )
Change subject: wip compilation op 1
......................................................................
wip compilation op 1
Change-Id: Ic27c7ee13c3cb78f4e7d3dc43a2ae0e2be0bf07e
---
M
hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/RemoveUnusedAssignAndAggregateRule.java
1 file changed, 23 insertions(+), 30 deletions(-)
git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb
refs/changes/70/21370/1
diff --git
a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/RemoveUnusedAssignAndAggregateRule.java
b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/RemoveUnusedAssignAndAggregateRule.java
index 629ac92..d587394 100644
---
a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/RemoveUnusedAssignAndAggregateRule.java
+++
b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/RemoveUnusedAssignAndAggregateRule.java
@@ -50,6 +50,7 @@
import
org.apache.hyracks.algebricks.core.algebra.operators.logical.WindowOperator;
import
org.apache.hyracks.algebricks.core.algebra.operators.logical.visitors.VariableUtilities;
import org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
+import org.apache.hyracks.util.annotations.AiProvenance;
/**
* Removes unused variables from Assign, Unnest, Aggregate, RunningAggregate,
UnionAll, and Group-by operators.
@@ -61,9 +62,14 @@
private Map<Mutable<ILogicalOperator>, Set<LogicalVariable>>
assignedVarMap = new LinkedHashMap<>();
private Set<LogicalVariable> assignedVarSet = new HashSet<>();
- // Keep the variables that are used after ASSIGN, UNNEST, AGGREGATE,
UNION, WINDOW,
- // and GROUP operators.
- private Map<Mutable<ILogicalOperator>, Set<LogicalVariable>>
accumulatedUsedVarFromRootMap = new LinkedHashMap<>();
+ // The set of variables used anywhere in the plan subtree being processed.
In an Algebricks
+ // logical plan a variable is produced once and consumed only by its
ancestors (data flows
+ // strictly upward; scoping prevents sibling/descendant use, and nested
plans reference only
+ // their own input's variables). Hence, for a variable produced by a
target operator,
+ // "used by an ancestor on the root->op path" is equivalent to "used
anywhere in the plan".
+ // This lets us replace the previous per-operator accumulated-used-vars
map (which required a
+ // HashSet copy at every child while descending) with a single plan-wide
set built in one pass.
+ private final Set<LogicalVariable> globalUsedVars = new HashSet<>();
private boolean isTransformed = false;
@@ -85,8 +91,7 @@
}
clear();
- Set<LogicalVariable> accumulatedUsedVarFromRootSet = new HashSet<>();
- collectUnusedAssignedVars(opRef, accumulatedUsedVarFromRootSet, true,
context);
+ collectUnusedAssignedVars(opRef, true, context);
// If there are ASSIGN, UNNEST, AGGREGATE, UNION, and GROUP operators
in the plan,
// we try to remove these operators if the produced variables from
these
@@ -104,17 +109,13 @@
*/
private Set<LogicalVariable>
removeAssignVarFromConsideration(Mutable<ILogicalOperator> opRef) {
ILogicalOperator op = opRef.getValue();
- Set<LogicalVariable> assignVarsSetForThisOp = null;
- Set<LogicalVariable> usedVarsSetForThisOp =
accumulatedUsedVarFromRootMap.get(opRef);
-
- assignVarsSetForThisOp = assignedVarMap.get(opRef);
+ Set<LogicalVariable> assignVarsSetForThisOp =
assignedVarMap.get(opRef);
if (assignVarsSetForThisOp != null &&
!assignVarsSetForThisOp.isEmpty()) {
Iterator<LogicalVariable> varIter =
assignVarsSetForThisOp.iterator();
while (varIter.hasNext()) {
LogicalVariable v = varIter.next();
- if ((usedVarsSetForThisOp != null &&
usedVarsSetForThisOp.contains(v))
- || survivedUnionSourceVarSet.contains(v)) {
+ if (globalUsedVars.contains(v) ||
survivedUnionSourceVarSet.contains(v)) {
varIter.remove();
}
}
@@ -342,17 +343,17 @@
return changed;
}
- private void collectUnusedAssignedVars(Mutable<ILogicalOperator> opRef,
- Set<LogicalVariable> accumulatedUsedVarFromRootSet, boolean first,
IOptimizationContext context)
+ @AiProvenance(agent = AiProvenance.Agent.CLAUDE_OPUS_4_8, tool =
AiProvenance.Tool.CLAUDE_CODE_CLI, contributionKind =
AiProvenance.ContributionKind.REFACTORED, notes = "Replace per-operator
accumulated-used-vars map (with a HashSet copy per child) "
+ + "by a single plan-wide globalUsedVars set; compile-time only,
plan-preserving.")
+ private void collectUnusedAssignedVars(Mutable<ILogicalOperator> opRef,
boolean first, IOptimizationContext context)
throws AlgebricksException {
AbstractLogicalOperator op = (AbstractLogicalOperator)
opRef.getValue();
if (!first) {
context.addToDontApplySet(this, op);
}
Set<LogicalVariable> assignVarsSetInThisOp = new HashSet<>();
- Set<LogicalVariable> usedVarsSetInThisOp = new HashSet<>();
- // Add used variables in this operator to the accumulated used
variables set?
+ // Add used variables in this operator to the global used variables
set?
boolean addUsedVarsInThisOp = true;
// ASSIGN, AGGREGATE, UNNEST, UNIONALL, or GROUP operator found?
boolean targetOpFound = false;
@@ -422,29 +423,21 @@
}
if (addUsedVarsInThisOp) {
- VariableUtilities.getUsedVariables(op, usedVarsSetInThisOp);
- accumulatedUsedVarFromRootSet.addAll(usedVarsSetInThisOp);
- // We may have visited this operator before if there are multiple
- // paths in the plan.
- if (accumulatedUsedVarFromRootMap.containsKey(opRef)) {
-
accumulatedUsedVarFromRootMap.get(opRef).addAll(accumulatedUsedVarFromRootSet);
- } else {
- accumulatedUsedVarFromRootMap.put(opRef,
accumulatedUsedVarFromRootSet);
- }
- } else {
- accumulatedUsedVarFromRootMap.put(opRef,
accumulatedUsedVarFromRootSet);
+ // Accumulate this operator's used variables directly into the
single plan-wide set.
+ // No per-child copy is needed: membership in globalUsedVars is
equivalent to the old
+ // per-operator "used on the root->op path" test (see
globalUsedVars field comment).
+ VariableUtilities.getUsedVariables(op, globalUsedVars);
}
for (Mutable<ILogicalOperator> c : op.getInputs()) {
- collectUnusedAssignedVars(c, new
HashSet<LogicalVariable>(accumulatedUsedVarFromRootSet), false, context);
+ collectUnusedAssignedVars(c, false, context);
}
if (op.hasNestedPlans()) {
AbstractOperatorWithNestedPlans opWithNested =
(AbstractOperatorWithNestedPlans) op;
for (ILogicalPlan plan : opWithNested.getNestedPlans()) {
for (Mutable<ILogicalOperator> r : plan.getRoots()) {
- collectUnusedAssignedVars(r, new
HashSet<LogicalVariable>(accumulatedUsedVarFromRootSet), false,
- context);
+ collectUnusedAssignedVars(r, false, context);
}
}
}
@@ -469,7 +462,7 @@
private void clear() {
assignedVarMap.clear();
assignedVarSet.clear();
- accumulatedUsedVarFromRootMap.clear();
+ globalUsedVars.clear();
survivedUnionSourceVarSet.clear();
isTransformed = false;
}
--
To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/21370?usp=email
To unsubscribe, or for help writing mail filters, visit
https://asterix-gerrit.ics.uci.edu/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Change-Id: Ic27c7ee13c3cb78f4e7d3dc43a2ae0e2be0bf07e
Gerrit-Change-Number: 21370
Gerrit-PatchSet: 1
Gerrit-Owner: Janhavi Tripurwar <[email protected]>