{
if(mode == ExecType.LOCAL) {
//We don't need this optimisation to happen in the local mode.
//so we do nothing here.
} else {
LOSort sort = (LOSort)predecessor;
if (sort.getLimit()==-1)
sort.setLimit(limit.getLimit());
else
sort.setLimit(sort.getLimit()<limit.getLimit()?sort.getLimit():limit.getLimit());
try {
mPlan.removeAndReconnect(limit);
} catch (Exception e) {
int errCode = 2012;
String msg = "Can not remove LOLimit after LOSort";
throw new OptimizerException(msg, errCode, PigException.BUG, e);
}
}
}
// Limit is merged into another LOLimit
else if (predecessor instanceof LOLimit)
{
LOLimit beforeLimit = (LOLimit)predecessor;
beforeLimit.setLimit(beforeLimit.getLimit()<limit.getLimit()?beforeLimit.getLimit():limit.getLimit());
try {
mPlan.removeAndReconnect(limit);
} catch (Exception e) {
int errCode = 2012;
String msg = "Can not remove LOLimit after LOLimit";
throw new OptimizerException(msg, errCode, PigException.BUG, e);
}
}
// Limit and OrderBy (LOSort) can be separated by split
else if (predecessor instanceof LOSplitOutput) {
if(mode == ExecType.LOCAL) {
//We don't need this optimisation to happen in the local mode.
//so we do nothing here.
} else {
List<LogicalOperator> grandparants = mPlan
.getPredecessors(predecessor);
// After insertion of splitters, any node in the plan can
// have at most one predecessor
if (grandparants != null && grandparants.size() != 0
&& grandparants.get(0) instanceof LOSplit) {
List<LogicalOperator> greatGrandparants = mPlan
.getPredecessors(grandparants.get(0));
if (greatGrandparants != null
&& greatGrandparants.size() != 0
&& greatGrandparants.get(0) instanceof LOSort) {
LOSort sort = (LOSort)greatGrandparants.get(0);
LOSort newSort = new LOSort(
sort.getPlan(),
new OperatorKey(
sort.getOperatorKey().scope,
NodeIdGenerator
.getGenerator()
.getNextNodeId(
sort.getOperatorKey().scope)),
sort.getSortColPlans(),
sort.getAscendingCols(),
sort.getUserFunc());
newSort.setLimit(limit.getLimit());
try {
mPlan.replace(limit, newSort);
} catch (PlanException e) {
int errCode = 2012;
String msg = "Can not replace LOLimit with LOSort after splitter";