PhysicalPlan to, int initial, int current, byte mapKeyType) throws VisitorException {
POPackage cpk = (POPackage)from.getRoots().get(0);
from.remove(cpk);
if (cpk instanceof POMultiQueryPackage) {
POMultiQueryPackage mpkg = (POMultiQueryPackage)cpk;
setBaseIndexOnPackage(initial, mpkg);
}
PODemux demux = (PODemux)to.getLeaves().get(0);
boolean isSameKeyType = demux.isSameMapKeyType();
PhysicalOperator leaf = from.getLeaves().get(0);
if (leaf instanceof POLocalRearrange) {
POLocalRearrange clr = (POLocalRearrange)leaf;
try {
clr.setMultiQueryIndex(initial);
} catch (ExecException e) {
int errCode = 2136;
String msg = "Internal Error. Unable to set multi-query index for optimization.";
throw new OptimizerException(msg, errCode, PigException.BUG, e);
}
// change the map key type to tuple when
// multiple splittees have different map key types
if (!isSameKeyType) {
clr.setKeyType(DataType.TUPLE);
}
} else if (leaf instanceof PODemux) {
PODemux locDemux = (PODemux)leaf;
setBaseIndexOnDemux(initial, locDemux);
}
POMultiQueryPackage pkg = (POMultiQueryPackage)to.getRoots().get(0);
// if current > initial + 1, it means we had
// a split in the map of the MROper we are trying to
// merge. In that case we would have changed the indices
// of the POLocalRearranges in the split to be in the
// range initial to current. To handle key, value pairs
// coming out of those POLocalRearranges, we replicate
// the Package as many times (in this case, the package
// would have to be a POMultiQueryPackage since we had
// a POSplit in the map). That Package would have a baseindex
// correctly set (in the beginning of this method) and would
// be able to handle the outputs from the different
// POLocalRearranges.
for (int i=initial; i<current; i++) {
pkg.addPackage(cpk);
}
// all packages should have the same key type
if (!isSameKeyType) {
cpk.setKeyType(DataType.TUPLE);
}
pkg.setKeyType(cpk.getKeyType());
// See comment above for why we replicated the Package
// in the from plan - for the same reason, we replicate
// the Demux operators now.
for (int i=initial; i<current; i++) {