public final static List<IAlgebraicRewriteRule> buildIntroduceProjectRuleCollection() { List<IAlgebraicRewriteRule> project = new LinkedList<IAlgebraicRewriteRule>(); project.add(new IntroduceProjectsRule()); return project; }
@Override public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException { if (hasRun) { return false; } hasRun = true; // Collect all used variables after each operator, including the used variables in itself in the plan. // This is necessary since introduceProjects() may generate a wrong project if it doesn't have the information // for all paths in the plan in case there are two or more branches since it can only deal one path at a time. // So, a variable used in one path might be removed while the method traverses another path. Set<LogicalVariable> parentUsedVars = new LinkedHashSet<>(); collectUsedVars(opRef, parentUsedVars); // Introduce projects return introduceProjects(null, -1, opRef, Collections.<LogicalVariable> emptySet(), context); }
/** * Collect all used variables after each operator, including the used variables in itself in the plan. * Collecting information in a separate method is required since there can be multiple paths in the plan * and introduceProjects() method can deal with only one path at a time during conducting depth-first-search. */ protected void collectUsedVars(Mutable<ILogicalOperator> opRef, Set<LogicalVariable> parentUsedVars) throws AlgebricksException { AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue(); Set<LogicalVariable> usedVarsPerOp = new LinkedHashSet<>(); VariableUtilities.getUsedVariables(op, usedVarsPerOp); usedVarsPerOp.addAll(parentUsedVars); if (allUsedVarsAfterOpMap.get(op) == null) { allUsedVarsAfterOpMap.put(op, usedVarsPerOp); } else { allUsedVarsAfterOpMap.get(op).addAll(usedVarsPerOp); } for (Mutable<ILogicalOperator> inputOpRef : op.getInputs()) { collectUsedVars(inputOpRef, usedVarsPerOp); } }
if (introduceProjects(op, i, inputOpRef, parentsUsedVars, context)) { modified = true;
public static final List<IAlgebraicRewriteRule> buildPhysicalRewritesTopLevelRuleCollection( ICcApplicationContext appCtx) { List<IAlgebraicRewriteRule> physicalRewritesTopLevel = new LinkedList<>(); physicalRewritesTopLevel.add(new PushNestedOrderByUnderPreSortedGroupByRule()); physicalRewritesTopLevel.add(new CopyLimitDownRule()); // CopyLimitDownRule may generates non-topmost limits with numeric_adds functions. // We are going to apply a constant folding rule again for this case. physicalRewritesTopLevel.add(new ConstantFoldingRule(appCtx)); physicalRewritesTopLevel.add(new PushLimitIntoOrderByRule()); physicalRewritesTopLevel.add(new PushLimitIntoPrimarySearchRule()); // remove assigns that could become unused after PushLimitIntoPrimarySearchRule physicalRewritesTopLevel.add(new RemoveUnusedAssignAndAggregateRule()); physicalRewritesTopLevel.add(new IntroduceProjectsRule()); physicalRewritesTopLevel.add(new SetAlgebricksPhysicalOperatorsRule()); physicalRewritesTopLevel.add(new IntroduceRapidFrameFlushProjectAssignRule()); physicalRewritesTopLevel.add(new SetExecutionModeRule()); physicalRewritesTopLevel.add(new IntroduceRandomPartitioningFeedComputationRule()); return physicalRewritesTopLevel; }