LOInnerLoad innerLoad2 = new LOInnerLoad(innerPlan, foreach, 1);
innerPlan.add(innerLoad2);
LogicalExpressionPlan namePrj = new LogicalExpressionPlan();
ProjectExpression prjName = new ProjectExpression(namePrj, DataType.CHARARRAY, 0, 0);
namePrj.add(prjName);
LogicalExpressionPlan cuisinesPrj = new LogicalExpressionPlan();
ProjectExpression prjCuisines = new ProjectExpression(cuisinesPrj, DataType.BAG, 1, 0);
cuisinesPrj.add(prjCuisines);
List<LogicalExpressionPlan> expPlans = new ArrayList<LogicalExpressionPlan>();
expPlans.add(namePrj);
expPlans.add(cuisinesPrj);
boolean flatten[] = new boolean[2];
flatten[0] = false;
flatten[1] = true;
LOGenerate generate = new LOGenerate(innerPlan, expPlans, flatten);
innerPlan.add(generate);
innerPlan.connect(innerLoad, generate);
innerPlan.connect(innerLoad2, generate);
foreach.setInnerPlan(innerPlan);
foreach.setAlias("B");
plan.add(foreach);
plan.connect(load, foreach);
filter = new LOFilter(plan);
LogicalExpressionPlan filterPlan = new LogicalExpressionPlan();
ProjectExpression namePrj2 = new ProjectExpression(filterPlan, DataType.CHARARRAY, 0, 0);
filterPlan.add(namePrj2);
ConstantExpression constExp = new ConstantExpression(filterPlan, DataType.CHARARRAY, "joe");
filterPlan.add(constExp);
EqualExpression equal = new EqualExpression(filterPlan, namePrj2, constExp);
filterPlan.add(equal);
filter.setFilterPlan(filterPlan);
filter.setAlias("C");
plan.add(filter);
LOFilter filter2 = new LOFilter(plan);
LogicalExpressionPlan filter2Plan = new LogicalExpressionPlan();
ProjectExpression name2Prj2 = new ProjectExpression(filter2Plan, DataType.CHARARRAY, 0, 0);
filter2Plan.add(name2Prj2);
ConstantExpression const2Exp = new ConstantExpression(filter2Plan, DataType.CHARARRAY, "joe2");
filter2Plan.add(const2Exp);
EqualExpression equal2 = new EqualExpression(filter2Plan, namePrj2, constExp);
filter2Plan.add(equal2);
filter2.setFilterPlan(filter2Plan);
filter2.setAlias("C1");
plan.add(filter2);
plan.connect(foreach, filter2);
plan.connect(filter2, filter);
stor = new LOStore(plan);
stor.setAlias("D");
plan.add(stor);
plan.connect(filter,stor);
try {
// Stamp everything with a Uid
UidStamper stamper = new UidStamper(plan);
stamper.visit();
}catch(Exception e) {
assertTrue("Failed to set a valid uid", false );
}
// run filter rule
Rule r = new FilterAboveForeach("FilterAboveFlatten");
Set<Rule> s = new HashSet<Rule>();
s.add(r);
List<Set<Rule>> ls = new ArrayList<Set<Rule>>();
ls.add(s);
// Test Plan before optimizing
List<Operator> list = plan.getSinks();
assertTrue( list.contains(stor) );
list = plan.getSources();
assertTrue( list.contains(load) );
assertTrue( plan.getPredecessors(stor).contains(filter) );
assertEquals( 1, plan.getPredecessors(stor).size() );
assertTrue( plan.getPredecessors(filter2).contains(foreach) );
assertEquals( 1, plan.getPredecessors(filter2).size() );
assertTrue( plan.getPredecessors(foreach).contains(load) );
assertEquals( 1, plan.getPredecessors(foreach).size() );
assertTrue( plan.getPredecessors(filter).contains(filter2) );
assertEquals( 1, plan.getPredecessors(filter).size() );
assertTrue( foreach.getInnerPlan().getSinks().contains(generate) );
assertEquals( 1, foreach.getInnerPlan().getSinks().size() );
assertTrue( foreach.getInnerPlan().getSources().contains(innerLoad) );
assertTrue( foreach.getInnerPlan().getSources().contains(innerLoad2) );
assertEquals( 2, foreach.getInnerPlan().getSources().size() );
// Run the optimizer
MyPlanOptimizer optimizer = new MyPlanOptimizer(plan, ls, 3);
optimizer.addPlanTransformListener(new ProjectionPatcher());
optimizer.addPlanTransformListener(new SchemaPatcher());
optimizer.optimize();
// Test after optimization
list = plan.getSinks();
assertTrue( list.contains(stor) );
list = plan.getSources();
assertTrue( list.contains(load) );
assertTrue( plan.getPredecessors(stor).contains(foreach) );
assertEquals( 1, plan.getPredecessors(stor).size() );
assertTrue( plan.getPredecessors(filter2).contains(load) );
assertEquals( 1, plan.getPredecessors(filter2).size() );
assertTrue( plan.getPredecessors(foreach).contains(filter) );
assertEquals( 1, plan.getPredecessors(foreach).size() );
assertTrue( plan.getPredecessors(filter).contains(filter2) );
assertEquals( 1, plan.getPredecessors(filter).size() );
assertEquals( load.getSchema().getField(0).uid, namePrj2.getUid() );
assertEquals( namePrj2.getUid(), name2Prj2.getUid() );
assertEquals( name2Prj2.getUid(), prjName.getUid() );
assertTrue( foreach.getInnerPlan().getSinks().contains(generate) );
assertEquals( 1, foreach.getInnerPlan().getSinks().size() );
assertTrue( foreach.getInnerPlan().getSources().contains(innerLoad) );
assertTrue( foreach.getInnerPlan().getSources().contains(innerLoad2) );