// case 1: simple and implication
String query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id > 3) AND (id > 5);" +
"store b into 'empty';";
LogicalPlan newLogicalPlan = Util.buildLp(pigServer, query);
PlanOptimizer optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id > 5);" +
"store b into 'empty';";
LogicalPlan expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 2: simple or implication
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id > 3) OR (id > 5);" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id > 3);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 3: constant expression eval
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id > 3+4*2);" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id > 11);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 4: simple NOT
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by NOT(NOT(NOT(id > 3)));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id <= 3);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 5: redundant NOT
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by NOT(NOT(id > 3));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id > 3);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 6: negative
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id > 3) AND (v1 is null);" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id > 3) AND (v1 is null);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 7: is not null
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by NOT(v1 is null);" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (v1 is not null);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 8: combo I
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by NOT((id > 1) OR ((v1 is null) AND (id > 5)));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id <= 1);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 9: combo II: lhs <-> rhs
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by NOT(((id > 5) AND (v1 is null)) OR (id > 1));"+
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id <= 1);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 10: complementary OR
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by ((id < 1) OR (id >= 1));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = load 'd.txt' as (id:int, v1, v2);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 11: OR Equality elimination
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by ((id < 1) OR (id < 1));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id < 1);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 12: AND Equality elimination
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by ((id < 1) AND (id < 1));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id < 1);" + "store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 13: negative case
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by ((id < 1) AND (v1 is null));" + "store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by ((id < 1) AND (v1 is NULL));" + "store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 14: combo III
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by NOT((id > 1) OR ((v1 is null) AND (id > 1+2*2)));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id <= 1);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 15: combo III: negative
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (((id > 5) OR (v1 < 3)) AND ((id > 4) OR (v1 > 5)));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (((id > 5) OR (v1 < 3)) AND ((id > 4) OR (v1 > 5)));" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 15: combo III: negative
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (((id > 5) OR (v1 > 3)) AND ((id > 4) OR (v1 > 5)));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (((id > 5) OR (v1 > 3)) AND ((id > 4) OR (v1 > 5)));" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 16: conflicting OR
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by ((id < 1) OR (id > 1));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by ((id < 1) OR (id > 1));" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 17: conflicting AND: negtive case for now
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by ((id < 1) AND (id > 1));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by ((id < 1) AND (id > 1));" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 18: combo IV: negative
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (((id > 5) OR (v1 > 3)) AND ((id < 8) OR (v1 > 5)));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (((id > 5) OR (v1 > 3)) AND ((id < 8) OR (v1 > 5)));" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 19: negative AND
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id > 3) AND (id < 5);" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id > 3) AND (id < 5);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 20: negative OR
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id > 3) OR (id < 5);" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1, v2)) by (id > 3) OR (id < 5);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 20: combo V: negative
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (((v1 > 3) OR (id > 5)) AND ((id < 8) OR (v1 > 5)));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (((v1 > 3) OR (id > 5)) AND ((id < 8) OR (v1 > 5)));" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 22: combo V: negative
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (((v1 > 3) OR (id > 5)) AND ((v1 > 5) OR (id < 8)));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (((v1 > 3) OR (id > 5)) AND ((v1 > 5) OR (id < 8)));" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 23: combo VI: extremely degenerate
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by ((((id > 1) OR (id > 2)) AND ((id > 3) OR (id > 4))) AND (((id > 5) OR (id > 6)) AND ((id > 7) OR (id > 8))));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (id > 7);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 24: combo VII: extremely degenerate
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by ((((id > 1) OR (id > 2)) AND ((id > 3) OR (id > 4))) AND (id > 7));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (id > 7);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 25: combo VII: extremely degenerate
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by ((((id > 1) OR (id > 2)) AND ((id > 3) OR (id > 4))) AND (((id > 5) AND (id > 7))));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (id > 7);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 26: combo VIII: lhs<->rhs for case 25
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by ((((id > 7) AND (id > 5))) AND (((id > 4) OR (id > 3)) AND ((id > 2) OR (id > 1))));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (id > 7);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 27: combo VII: rhs<->lhs for case 24
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by ((id > 7) AND (((id > 4) OR (id > 3)) AND ((id > 2) OR (id > 1))));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (id > 7);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 28: complex equality
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (((id > 4) OR (id > 3)) AND ((id > 3) OR (id > 4)));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (id > 3);" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 29: complex equality
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (((id > 4) OR (v1 > 3)) AND ((v1 > 3) OR (id > 4)));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by ((id > 4) OR (v1 > 3));" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 30: complex equality
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by (((id > 4) OR (v1 > 3)) OR ((v1 > 3) OR (id > 4)));" +
"store b into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
query = "b = filter (load 'd.txt' as (id:int, v1:int, v2)) by ((id > 4) OR (v1 > 3));" +
"store b into 'empty';";
expected = Util.buildLp(pigServer, query);
assertTrue(expected.isEqual(newLogicalPlan));
// case 31: See PIG-2067
query = "A = load 'a.dat' as (cookie);" +
"B = load 'b.dat' as (cookie);" +
"C = cogroup A by cookie, B by cookie;" +
"E = filter C by COUNT(B)>0 AND COUNT(A)>0;" +
"store E into 'empty';";
newLogicalPlan = Util.buildLp(pigServer, query);
optimizer = new MyPlanOptimizer(newLogicalPlan, 10);
optimizer.optimize();
// Make sure in this case, we don't optimize
query = "A = load 'a.dat' as (cookie);" +
"B = load 'b.dat' as (cookie);" +
"C = cogroup A by cookie, B by cookie;" +