if (preds == null || preds.size() == 0) {
if (p.getExpression().equals(mOldNode)) {
if (mNewNode == null) {
int errCode = 1100;
String msg = "Replacement node cannot be null.";
throw new VisitorException(msg, errCode, PigException.INPUT);
}
// Change the expression
p.setExpression(mNewNode);
if(p.isStar()) {
//its a project(*)
//no need of changing it
return;
}
if (mContainingNode != null) {
// use the projection mapping of mOldNode or mNewNode along
// with that of mContainingNode
// to figure out the mapping and replace the columns
// appropriately
int oldNodeColumn = p.getCol();
if (mUseOldNode) {
// use mOldNode's projection map and find the column number
// from the input that
ProjectionMap oldNodeMap = mOldNode.getProjectionMap();
if (oldNodeMap == null) {
// bail out if the projection map is null
int errCode = 2156;
String msg = "Error while fixing projections. Projection map of node to be replaced is null.";
throw new VisitorException(msg, errCode,
PigException.BUG);
}
if (!oldNodeMap.changes()) {
// no change required
return;
}
MultiMap<Integer, ProjectionMap.Column> oldNodeMappedFields = oldNodeMap
.getMappedFields();
if (oldNodeMappedFields == null) {
// there is no mapping available bail out
int errCode = 2157;
String msg = "Error while fixing projections. No mapping available in old predecessor to replace column.";
throw new VisitorException(msg, errCode,
PigException.BUG);
}
List<ProjectionMap.Column> columns = (List<ProjectionMap.Column>) oldNodeMappedFields.get(oldNodeColumn);
if (columns == null) {
// there is no mapping for oldNodeColumn
// it could be an added field; bail out
int errCode = 2158;
String msg = "Error during fixing projections. No mapping available in old predecessor for column to be replaced.";
throw new VisitorException(msg, errCode,
PigException.BUG);
}
boolean foundMapping = false;
for (ProjectionMap.Column column : columns) {
Pair<Integer, Integer> pair = column.getInputColumn();
if (pair.first.equals(mPredecessorIndex)) {
ArrayList<Integer> newColumns = new ArrayList<Integer>();
newColumns.add(pair.second);
p.setProjection(newColumns);
foundMapping = true;
break;
}
}
if (!foundMapping) {
// did not find a mapping - bail out
int errCode = 2159;
String msg = "Error during fixing projections. Could not locate replacement column from the old predecessor.";
throw new VisitorException(msg, errCode,
PigException.BUG);
} else {
// done with adjusting the column number of the
// project
return;
}
} else {
// here the projection mapping of new node has to be
// used to figure out
// the reverse mapping. From the newNode projection
// mapping search for
// the key whose value contains the pair (mOldNodeIndex,
// oldNodeColumn)
ProjectionMap newNodeMap = mNewNode.getProjectionMap();
if (newNodeMap == null) {
// did not find a mapping - bail out
int errCode = 2160;
String msg = "Error during fixing projections. Projection map of new predecessor is null.";
throw new VisitorException(msg, errCode,
PigException.BUG);
}
if (!newNodeMap.changes()) {
// no change required
return;
}
MultiMap<Integer, ProjectionMap.Column> newNodeMappedFields = newNodeMap
.getMappedFields();
if (newNodeMappedFields == null) {
// there is no mapping available bail out
int errCode = 2161;
String msg = "Error during fixing projections. No mapping available in new predecessor to replace column.";
throw new VisitorException(msg, errCode,
PigException.BUG);
}
boolean foundMapping = false;
for (Integer key : newNodeMappedFields.keySet()) {
List<ProjectionMap.Column> columns = (List<ProjectionMap.Column>) newNodeMappedFields
.get(key);
if (columns == null) {
// should not happen
int errCode = 2162;
String msg = "Error during fixing projections. Could not locate mapping for column: "
+ key + " in new predecessor.";
throw new VisitorException(msg, errCode,
PigException.BUG);
}
for (ProjectionMap.Column column : columns) {
Pair<Integer, Integer> pair = column.getInputColumn();
if (pair.first.equals(mPredecessorIndex)
&& pair.second.equals(oldNodeColumn)) {
ArrayList<Integer> newColumns = new ArrayList<Integer>();
newColumns.add(key);
p.setProjection(newColumns);
foundMapping = true;
break;
}
}
if (foundMapping) {
// done with adjusting the column number of the
// project
return;
}
}
if (!foundMapping) {
// did not find a mapping - bail out
int errCode = 2163;
String msg = "Error during fixing projections. Could not locate replacement column for column: "
+ oldNodeColumn
+ " in the new predecessor.";
throw new VisitorException(msg, errCode,
PigException.BUG);
}
}
}// end if for containing node != null
}// end if for projection expression equals mOldNode