private void mergeOneReducePlanWithIndex(PhysicalPlan from,
PhysicalPlan to, int initial, int current, byte mapKeyType) throws VisitorException {
POPackage pk = (POPackage)from.getRoots().get(0);
from.remove(pk);
Packager fromPkgr = pk.getPkgr();
if (!(fromPkgr instanceof MultiQueryPackager)) {
// XXX the index of the original keyInfo map is always 0,
// we need to shift the index so that the lookups works
// with the new indexed key
addShiftedKeyInfoIndex(initial, fromPkgr);
}
int total = current - initial;
MultiQueryPackager toPkgr = (MultiQueryPackager) ((POPackage) to
.getRoots().get(0)).getPkgr();
int pkCount = 0;
if (fromPkgr instanceof MultiQueryPackager) {
List<Packager> pkgs = ((MultiQueryPackager) fromPkgr)
.getPackagers();
for (Packager p : pkgs) {
((MultiQueryPackager) toPkgr).addPackager(p);
pkCount++;
}
toPkgr.addIsKeyWrappedList(((MultiQueryPackager) fromPkgr)
.getIsKeyWrappedList());
addShiftedKeyInfoIndex(initial, current,
(MultiQueryPackager) fromPkgr);
} else {
toPkgr.addPackager(fromPkgr, mapKeyType);
pkCount = 1;
}
if (pkCount != total) {
int errCode = 2146;
String msg = "Internal Error. Inconsistency in key index found during optimization.";
throw new OptimizerException(msg, errCode, PigException.BUG);
}
PODemux demux = (PODemux)to.getLeaves().get(0);
int plCount = 0;
PhysicalOperator root = from.getRoots().get(0);
if (root instanceof PODemux) {
// flattening the inner plans of the demux operator.
// This is based on the fact that if a plan has a demux
// operator, then it's the only operator in the plan.
List<PhysicalPlan> pls = ((PODemux)root).getPlans();
for (PhysicalPlan pl : pls) {
demux.addPlan(pl);
plCount++;
}
} else {
demux.addPlan(from);
plCount = 1;
}
if (plCount != total) {
int errCode = 2146;
String msg = "Internal Error. Inconsistency in key index found during optimization.";
throw new OptimizerException(msg, errCode, PigException.BUG);
}
if (toPkgr.isSameMapKeyType()) {
toPkgr.setKeyType(fromPkgr.getKeyType());
} else {
toPkgr.setKeyType(DataType.TUPLE);
}
}