use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan 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);
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan in project asterixdb by apache.
the class AbstractIntroduceGroupByCombinerRule method tryToPushSubplan.
private Pair<Boolean, ILogicalPlan> tryToPushSubplan(ILogicalPlan nestedPlan, GroupByOperator oldGbyOp, GroupByOperator newGbyOp, BookkeepingInfo bi, List<LogicalVariable> gbyVars, IOptimizationContext context) throws AlgebricksException {
List<Mutable<ILogicalOperator>> pushedRoots = new ArrayList<Mutable<ILogicalOperator>>();
Set<SimilarAggregatesInfo> toReplaceSet = new HashSet<SimilarAggregatesInfo>();
for (Mutable<ILogicalOperator> r : nestedPlan.getRoots()) {
if (!tryToPushRoot(r, oldGbyOp, newGbyOp, bi, gbyVars, context, pushedRoots, toReplaceSet)) {
// For now, if we cannot push everything, give up.
return new Pair<Boolean, ILogicalPlan>(false, null);
}
}
if (pushedRoots.isEmpty()) {
return new Pair<Boolean, ILogicalPlan>(true, null);
} else {
// Replaces the aggregation expressions in the original group-by op with new ones.
ILogicalPlan newPlan = new ALogicalPlanImpl(pushedRoots);
ILogicalPlan plan = fingIdenticalPlan(newGbyOp, newPlan);
replaceOriginalAggFuncs(toReplaceSet);
if (plan == null) {
return new Pair<Boolean, ILogicalPlan>(true, newPlan);
} else {
// Does not add a nested subplan to newGbyOp if there already exists an isomorphic plan.
Set<LogicalVariable> originalVars = new ListSet<LogicalVariable>();
Set<LogicalVariable> newVars = new ListSet<LogicalVariable>();
for (Mutable<ILogicalOperator> rootRef : pushedRoots) {
VariableUtilities.getProducedVariables(rootRef.getValue(), originalVars);
}
for (Mutable<ILogicalOperator> rootRef : plan.getRoots()) {
VariableUtilities.getProducedVariables(rootRef.getValue(), newVars);
}
// Replaces variable exprs referring to the variables produced by newPlan by
// those produced by plan.
Iterator<LogicalVariable> originalVarIter = originalVars.iterator();
Iterator<LogicalVariable> newVarIter = newVars.iterator();
while (originalVarIter.hasNext()) {
LogicalVariable originalVar = originalVarIter.next();
LogicalVariable newVar = newVarIter.next();
for (SimilarAggregatesInfo sai : toReplaceSet) {
for (AggregateExprInfo aei : sai.simAggs) {
ILogicalExpression afce = aei.aggExprRef.getValue();
afce.substituteVar(originalVar, newVar);
}
}
}
return new Pair<Boolean, ILogicalPlan>(true, null);
}
}
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan in project asterixdb by apache.
the class PigletCompiler method compile.
public JobSpecification compile(List<ASTNode> ast) throws AlgebricksException, PigletException {
ILogicalPlan plan = translate(ast);
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("Translated Plan:");
LOGGER.info(getPrettyPrintedPlan(plan));
}
ICompiler compiler = cFactory.createCompiler(plan, metadataProvider, varCounter);
compiler.optimize();
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("Optimized Plan:");
LOGGER.info(getPrettyPrintedPlan(plan));
}
return compiler.createJob(null, null);
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan in project asterixdb by apache.
the class AbstractIntroduceGroupByCombinerRule method opToPush.
private GroupByOperator opToPush(GroupByOperator gbyOp, BookkeepingInfo bi, IOptimizationContext context) throws AlgebricksException {
// Hook up input to new group-by.
Mutable<ILogicalOperator> opRef3 = gbyOp.getInputs().get(0);
ILogicalOperator op3 = opRef3.getValue();
GroupByOperator newGbyOp = new GroupByOperator();
newGbyOp.getInputs().add(new MutableObject<ILogicalOperator>(op3));
// Copy annotations.
Map<String, Object> annotations = newGbyOp.getAnnotations();
annotations.putAll(gbyOp.getAnnotations());
List<LogicalVariable> gbyVars = gbyOp.getGbyVarList();
// Backup nested plans since tryToPushSubplan(...) may mutate them.
List<ILogicalPlan> copiedNestedPlans = new ArrayList<>();
for (ILogicalPlan nestedPlan : gbyOp.getNestedPlans()) {
ILogicalPlan copiedNestedPlan = OperatorManipulationUtil.deepCopy(nestedPlan, gbyOp);
OperatorManipulationUtil.computeTypeEnvironment(copiedNestedPlan, context);
copiedNestedPlans.add(copiedNestedPlan);
}
for (ILogicalPlan p : gbyOp.getNestedPlans()) {
// NOTE: tryToPushSubplan(...) can mutate the nested subplan p.
Pair<Boolean, ILogicalPlan> bip = tryToPushSubplan(p, gbyOp, newGbyOp, bi, gbyVars, context);
if (!bip.first) {
// For now, if we cannot push everything, give up.
// Resets the group-by operator with backup nested plans.
gbyOp.getNestedPlans().clear();
gbyOp.getNestedPlans().addAll(copiedNestedPlans);
return null;
}
ILogicalPlan pushedSubplan = bip.second;
if (pushedSubplan != null) {
newGbyOp.getNestedPlans().add(pushedSubplan);
}
}
ArrayList<LogicalVariable> newOpGbyList = new ArrayList<LogicalVariable>();
ArrayList<LogicalVariable> replGbyList = new ArrayList<LogicalVariable>();
// Find maximal sequence of variable.
for (Map.Entry<GroupByOperator, List<LogicalVariable>> e : bi.modifyGbyMap.entrySet()) {
List<LogicalVariable> varList = e.getValue();
boolean see1 = true;
int sz1 = newOpGbyList.size();
int i = 0;
for (LogicalVariable v : varList) {
if (see1) {
if (i < sz1) {
LogicalVariable v2 = newOpGbyList.get(i);
if (v != v2) {
// cannot linearize
return null;
}
} else {
see1 = false;
newOpGbyList.add(v);
replGbyList.add(context.newVar());
}
i++;
} else {
newOpGbyList.add(v);
replGbyList.add(context.newVar());
}
}
}
// set the vars in the new op
int n = newOpGbyList.size();
for (int i = 0; i < n; i++) {
newGbyOp.addGbyExpression(replGbyList.get(i), new VariableReferenceExpression(newOpGbyList.get(i)));
VariableUtilities.substituteVariables(gbyOp, newOpGbyList.get(i), replGbyList.get(i), false, context);
}
// Sets the global flag to be false.
newGbyOp.setGlobal(false);
// Sets the group all flag.
newGbyOp.setGroupAll(gbyOp.isGroupAll());
return newGbyOp;
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan in project asterixdb by apache.
the class AbstractRuleController method rewriteOperatorRef.
protected boolean rewriteOperatorRef(Mutable<ILogicalOperator> opRef, IAlgebraicRewriteRule rule, boolean enterNestedPlans, boolean fullDFS) throws AlgebricksException {
String preBeforePlan = getPlanString(opRef);
if (rule.rewritePre(opRef, context)) {
String preAfterPlan = getPlanString(opRef);
printRuleApplication(rule, preBeforePlan, preAfterPlan);
return true;
}
boolean rewritten = false;
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
for (Mutable<ILogicalOperator> inp : op.getInputs()) {
if (rewriteOperatorRef(inp, rule, enterNestedPlans, fullDFS)) {
rewritten = true;
if (!fullDFS) {
break;
}
}
}
if (op.hasNestedPlans() && enterNestedPlans) {
AbstractOperatorWithNestedPlans o2 = (AbstractOperatorWithNestedPlans) op;
for (ILogicalPlan p : o2.getNestedPlans()) {
for (Mutable<ILogicalOperator> r : p.getRoots()) {
if (rewriteOperatorRef(r, rule, enterNestedPlans, fullDFS)) {
rewritten = true;
if (!fullDFS) {
break;
}
}
}
if (rewritten && !fullDFS) {
break;
}
}
}
String postBeforePlan = getPlanString(opRef);
if (rule.rewritePost(opRef, context)) {
String postAfterPlan = getPlanString(opRef);
printRuleApplication(rule, postBeforePlan, postAfterPlan);
return true;
}
return rewritten;
}
Aggregations