LogicalPlanTester planTester = new LogicalPlanTester() ;
planTester.buildPlan(" A = load 'input' using PigStorage('\t');");
planTester.buildPlan(" B = order A by $0;");
LogicalPlan lp = planTester.buildPlan("store B into '/tmp';");
PhysicalPlan pp = Util.buildPhysicalPlan(lp, pc);
MROperPlan mrPlan = Util.buildMRPlan(pp, pc);
int count = 1;
MapReduceOper mrOper = mrPlan.getRoots().get(0);
while(mrPlan.getSuccessors(mrOper) != null) {
mrOper = mrPlan.getSuccessors(mrOper).get(0);
++count;
}
// Before optimizer visits, number of MR jobs = 3.
assertEquals(3,count);
SampleOptimizer so = new SampleOptimizer(mrPlan);
so.visit();
count = 1;
mrOper = mrPlan.getRoots().get(0);
while(mrPlan.getSuccessors(mrOper) != null) {
mrOper = mrPlan.getSuccessors(mrOper).get(0);
++count;
}
// After optimizer visits, number of MR jobs = 2.
assertEquals(2,count);
// Test if RandomSampleLoader got pushed to top.
mrOper = mrPlan.getRoots().get(0);
List<PhysicalOperator> phyOps = mrOper.mapPlan.getRoots();
assertEquals(1, phyOps.size());
assertTrue(phyOps.get(0) instanceof POLoad);
assertTrue(((POLoad)phyOps.get(0)).getLFile().getFuncName().equals("org.apache.pig.impl.builtin.RandomSampleLoader"));
// Test RandomSampleLoader is not present anymore in second MR job.
phyOps = mrPlan.getSuccessors(mrOper).get(0).mapPlan.getRoots();
assertEquals(1, phyOps.size());
assertTrue(phyOps.get(0) instanceof POLoad);
assertFalse(((POLoad)phyOps.get(0)).getLFile().getFuncName().equals("org.apache.pig.impl.builtin.RandomSampleLoader"));
}