Package org.apache.pig.experimental.logical.relational

Examples of org.apache.pig.experimental.logical.relational.LOForEach


        aschema.addField(new LogicalSchema.LogicalFieldSchema("d", aschema2, DataType.BAG));       
       
        LOLoad load = new LOLoad(new FileSpec("file:///test/d.txt", new FuncSpec("org.apache.pig.builtin.PigStorage")), aschema, expected);
        expected.add(load);        
       
        LOForEach foreach2 = new LOForEach(expected);
        org.apache.pig.experimental.logical.relational.LogicalPlan innerPlan = new org.apache.pig.experimental.logical.relational.LogicalPlan();
        LOInnerLoad l1 = new LOInnerLoad(innerPlan, foreach2, 0);
        innerPlan.add(l1);
        LOInnerLoad l2 = new LOInnerLoad(innerPlan, foreach2, 1);
       
        List<LogicalExpressionPlan>  eps = new ArrayList<LogicalExpressionPlan>();
        LogicalExpressionPlan p1 = new LogicalExpressionPlan();
        new ProjectExpression(p1, DataType.BYTEARRAY, 0, 0);
        LogicalExpressionPlan p2 = new LogicalExpressionPlan();       
        new ProjectExpression(p2, DataType.BAG, 1, 0);
        eps.add(p1);
        eps.add(p2);
       
        LOGenerate gen = new LOGenerate(innerPlan, eps, new boolean[] {false, true});
        innerPlan.add(gen);
        innerPlan.connect(l1, gen);
        innerPlan.connect(l2, gen);
       
        foreach2.setInnerPlan(innerPlan);     
        expected.add(foreach2);
               
        LOStore s = new LOStore(expected, new FileSpec("file:///test/empty", new FuncSpec("org.apache.pig.builtin.PigStorage")));
     
        expected.add(s);
       
        expected.connect(load, foreach2);
   
        expected.connect(foreach2, s);
        try {
            UidStamper stamper = new UidStamper(expected);
            stamper.visit();        
        }catch(Exception e) {
            throw new VisitorException(e);
        }
       
        assertTrue(expected.isEqual(newPlan));
       
        LogicalSchema schema = foreach2.getSchema();
        aschema = new LogicalSchema();
        aschema.addField(new LogicalSchema.LogicalFieldSchema("id", null, DataType.BYTEARRAY));
        aschema.addField(new LogicalSchema.LogicalFieldSchema("d::id", null, DataType.INTEGER));
        aschema.addField(new LogicalSchema.LogicalFieldSchema("s", null, DataType.BYTEARRAY));
        assertTrue(schema.isEqual(aschema));
View Full Code Here


        // Load (name, cuisines{t:(name)}) -> foreach gen name,flatten(cuisines)
        // -> filter name == 'joe' --> stor
       
        LogicalPlan plan = null;
        LOLoad load = null;
        LOForEach foreach = null;
        LOFilter filter = null;
        LOStore stor = null;
       
        plan = new LogicalPlan();
       
        LogicalSchema schema = new LogicalSchema();
        schema.addField(new LogicalSchema.LogicalFieldSchema("name", null, DataType.CHARARRAY));
        LogicalSchema bagSchema = new LogicalSchema();
        LogicalSchema bagTupleSchema = new LogicalSchema();
        bagTupleSchema.addField( new LogicalSchema.LogicalFieldSchema("name", null, DataType.CHARARRAY) );
        bagSchema.addField( new LogicalSchema.LogicalFieldSchema( "t", bagTupleSchema, DataType.TUPLE ) );
        schema.addField(new LogicalSchema.LogicalFieldSchema("cuisines", bagSchema, DataType.BAG));
       
        load = new LOLoad(null, schema, plan);
        load.setAlias("A");
        plan.add(load);
       
        foreach = new LOForEach(plan);
       
        LogicalPlan innerPlan = new LogicalPlan();
        LOInnerLoad innerLoad = new LOInnerLoad(innerPlan, foreach, 0);
        innerPlan.add(innerLoad);
       
        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);
       
        plan.connect(foreach, 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(filter).contains(foreach) );
        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(filter).contains(load) );
        assertEquals( 1, plan.getPredecessors(filter).size() );
        assertEquals( load.getSchema().getField(0).uid, namePrj2.getUid() );
        assertEquals( namePrj2.getUid(), prjName.getUid() );
       
        assertTrue( plan.getPredecessors(foreach).contains(filter) );
        assertEquals( 1, plan.getPredecessors(foreach).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() );
    }
View Full Code Here

        // Load (name, cuisines{t:(name)}) -> foreach gen name,flatten(cuisines)
        // -> filter $1 == 'joe' --> filter name == 'joe' --> stor
       
        LogicalPlan plan = null;
        LOLoad load = null;
        LOForEach foreach = null;
        LOFilter filter = null;
        LOStore stor = null;
       
        plan = new LogicalPlan();
       
        LogicalSchema schema = new LogicalSchema();
        schema.addField(new LogicalSchema.LogicalFieldSchema("name", null, DataType.CHARARRAY));
        LogicalSchema bagSchema = new LogicalSchema();
        LogicalSchema bagTupleSchema = new LogicalSchema();
        bagTupleSchema.addField( new LogicalSchema.LogicalFieldSchema("name", null, DataType.CHARARRAY) );
        bagSchema.addField( new LogicalSchema.LogicalFieldSchema( "t", bagTupleSchema, DataType.TUPLE ) );
        schema.addField(new LogicalSchema.LogicalFieldSchema("cuisines", bagSchema, DataType.BAG));
       
        load = new LOLoad(null, schema, plan);
        load.setAlias("A");
        plan.add(load);
       
        foreach = new LOForEach(plan);
       
        LogicalPlan innerPlan = new LogicalPlan();
        LOInnerLoad innerLoad = new LOInnerLoad(innerPlan, foreach, 0);
        innerPlan.add(innerLoad);
       
        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, 1);
        filter2Plan.add(name2Prj2);
        ConstantExpression const2Exp = new ConstantExpression(filter2Plan, DataType.CHARARRAY, "joe");
        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(filter2) );
        assertEquals( 1, plan.getPredecessors(stor).size() );
       
        assertTrue( plan.getPredecessors(filter2).contains(foreach) );
        assertEquals( 1, plan.getPredecessors(filter2).size() );
       
        assertTrue( plan.getPredecessors(foreach).contains(filter) );
        assertEquals( 1, plan.getPredecessors(foreach).size() );
       
        assertTrue( plan.getPredecessors(filter).contains(load) );
        assertEquals( 1, plan.getPredecessors(filter).size() );
        assertEquals( load.getSchema().getField(0).uid, namePrj2.getUid() );
        assertEquals( namePrj2.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) );
        assertEquals( 2, foreach.getInnerPlan().getSources().size() );
       
    }
View Full Code Here

        // Load (name, cuisines{t:(name)}) -> foreach gen name,cuisines
        // -> filter name == 'joe2' --> filter name == 'joe' --> stor
       
        LogicalPlan plan = null;
        LOLoad load = null;
        LOForEach foreach = null;
        LOFilter filter = null;
        LOStore stor = null;
       
        plan = new LogicalPlan();
       
        LogicalSchema schema = new LogicalSchema();
        schema.addField(new LogicalSchema.LogicalFieldSchema("name", null, DataType.CHARARRAY));
        LogicalSchema bagSchema = new LogicalSchema();
        LogicalSchema bagTupleSchema = new LogicalSchema();
        bagTupleSchema.addField( new LogicalSchema.LogicalFieldSchema("name", null, DataType.CHARARRAY) );
        bagSchema.addField( new LogicalSchema.LogicalFieldSchema( "t", bagTupleSchema, DataType.TUPLE ) );
        schema.addField(new LogicalSchema.LogicalFieldSchema("cuisines", bagSchema, DataType.BAG));
       
        load = new LOLoad(null, schema, plan);
        load.setAlias("A");
        plan.add(load);
       
        foreach = new LOForEach(plan);
       
        LogicalPlan innerPlan = new LogicalPlan();
        LOInnerLoad innerLoad = new LOInnerLoad(innerPlan, foreach, 0);
        innerPlan.add(innerLoad);
       
        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) );
        assertEquals( 2, foreach.getInnerPlan().getSources().size() );       
    }
View Full Code Here

        // Load (name, cuisines{t:(name)}) -> foreach gen name,cuisines
        // -> filter $1 == 'joe2' --> filter $1 == 'joe' --> stor
       
        LogicalPlan plan = null;
        LOLoad load = null;
        LOForEach foreach = null;
        LOFilter filter = null;
        LOStore stor = null;
       
        plan = new LogicalPlan();
       
        LogicalSchema schema = new LogicalSchema();
        schema.addField(new LogicalSchema.LogicalFieldSchema("name", null, DataType.CHARARRAY));
        LogicalSchema bagSchema = new LogicalSchema();
        LogicalSchema bagTupleSchema = new LogicalSchema();
        bagTupleSchema.addField( new LogicalSchema.LogicalFieldSchema("name", null, DataType.CHARARRAY) );
        bagSchema.addField( new LogicalSchema.LogicalFieldSchema( "t", bagTupleSchema, DataType.TUPLE ) );
        schema.addField(new LogicalSchema.LogicalFieldSchema("cuisines", bagSchema, DataType.BAG));
       
        load = new LOLoad(null, schema, plan);
        load.setAlias("A");
        plan.add(load);
       
        foreach = new LOForEach(plan);
       
        LogicalPlan innerPlan = new LogicalPlan();
        LOInnerLoad innerLoad = new LOInnerLoad(innerPlan, foreach, 0);
        innerPlan.add(innerLoad);
       
        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, 1);
        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, 1);
        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(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() );
       
        assertFalse( prjCuisines.getUid() == namePrj2.getUid() );
        assertFalse( prjCuisines.getUid() == name2Prj2.getUid() );
       
        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() );   
    }
View Full Code Here

        // Load (name, cuisines{t:(name)}) -> foreach gen name,flatten(cuisines)
        // -> filter $1 == 'joe' --> stor

        LogicalPlan plan = null;
        LOLoad load = null;
        LOForEach foreach = null;
        LOFilter filter = null;
        LOStore stor = null;

        plan = new LogicalPlan();

        LogicalSchema schema = new LogicalSchema();
        schema.addField(new LogicalSchema.LogicalFieldSchema("name", null, DataType.CHARARRAY));
        LogicalSchema bagSchema = new LogicalSchema();
        LogicalSchema bagTupleSchema = new LogicalSchema();
        bagTupleSchema.addField( new LogicalSchema.LogicalFieldSchema("name", null, DataType.CHARARRAY) );
        bagSchema.addField( new LogicalSchema.LogicalFieldSchema( "t", bagTupleSchema, DataType.TUPLE ) );
        schema.addField(new LogicalSchema.LogicalFieldSchema("cuisines", bagSchema, DataType.BAG));
       
        load = new LOLoad(null, schema, plan);
        load.setAlias("A");
        plan.add(load);
       
        foreach = new LOForEach(plan);
       
        LogicalPlan innerPlan = new LogicalPlan();
        LOInnerLoad innerLoad = new LOInnerLoad(innerPlan, foreach, 0);
        innerPlan.add(innerLoad);
       
        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, 1);
        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);
       
        plan.connect(foreach, 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(filter).contains(foreach) );
        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(filter) );
        assertEquals( 1, plan.getPredecessors(stor).size() );
       
        assertFalse( prjCuisines.getUid() == namePrj2.getUid() );
       
        assertTrue( plan.getPredecessors(filter).contains(foreach) );
        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() );
    }
View Full Code Here

        // Load (name, cuisines{t:(name)}) -> foreach gen name,cuisines
        // -> filter name == 'joe' --> stor
       
        LogicalPlan plan = null;
        LOLoad load = null;
        LOForEach foreach = null;
        LOFilter filter = null;
        LOStore stor = null;
       
        plan = new LogicalPlan();
       
        LogicalSchema schema = new LogicalSchema();
        schema.addField(new LogicalSchema.LogicalFieldSchema("name", null, DataType.CHARARRAY));
        LogicalSchema bagSchema = new LogicalSchema();
        LogicalSchema bagTupleSchema = new LogicalSchema();
        bagTupleSchema.addField( new LogicalSchema.LogicalFieldSchema("name", null, DataType.CHARARRAY) );
        bagSchema.addField( new LogicalSchema.LogicalFieldSchema( "t", bagTupleSchema, DataType.TUPLE ) );
        schema.addField(new LogicalSchema.LogicalFieldSchema("cuisines", bagSchema, DataType.BAG));
       
        load = new LOLoad(null, schema, plan);
        load.setAlias("A");
        plan.add(load);
       
        foreach = new LOForEach(plan);
       
        LogicalPlan innerPlan = new LogicalPlan();
        LOInnerLoad innerLoad = new LOInnerLoad(innerPlan, foreach, 0);
        innerPlan.add(innerLoad);
       
        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] = false;
       
        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);
       
        plan.connect(foreach, 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(filter).contains(foreach) );
        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(filter).contains(load) );
        assertEquals( 1, plan.getPredecessors(filter).size() );
       
        assertTrue( plan.getPredecessors(foreach).contains(filter) );
        assertEquals( 1, plan.getPredecessors(foreach).size() );
       
        assertEquals( load.getSchema().getField(0).uid , namePrj2.getUid() );
        assertEquals( namePrj2.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) );
        assertEquals( 2, foreach.getInnerPlan().getSources().size() );
    }
View Full Code Here

                        // add another LOForEach
                        if (next instanceof LOForEach) {
                            return;
                        }
                       
                        LOForEach foreach = new LOForEach(load.getPlan());
                       
                        // add foreach to the base plan                      
                        p.add(foreach);
                                      
                        Pair<Integer,Integer> disconnectedPos = p.disconnect(load, next);
                        p.connect(load, disconnectedPos.first.intValue(), foreach, 0 );
                        p.connect(foreach, 0, next, disconnectedPos.second.intValue());
                       
                        // add foreach to the subplan
                        subPlan.add(foreach);
                       
                        LogicalPlan innerPlan = new LogicalPlan();
                        foreach.setInnerPlan(innerPlan);
                       
                        // build foreach inner plan
                        List<LogicalExpressionPlan> exps = new ArrayList<LogicalExpressionPlan>();             
                        LOGenerate gen = new LOGenerate(innerPlan, exps, new boolean[requiredFields.getFields().size()]);
                        innerPlan.add(gen);
View Full Code Here

            return false;
        }
       
        @SuppressWarnings("unchecked")
        private void addForeach(LogicalRelationalOperator op) throws IOException {           
            LOForEach foreach = new LOForEach(op.getPlan());
           
            // add foreach to the base plan
            LogicalPlan p = (LogicalPlan)op.getPlan();
            p.add(foreach);
            List<Operator> next = p.getSuccessors(op);          
            if (next != null) {
                Operator[] nextArray = next.toArray(new Operator[0]);
                for(Operator n: nextArray) {                 
                    Pair<Integer, Integer> pos = p.disconnect(op, n);          
                    p.connect(foreach, pos.first, n, pos.second);
                }
            }
           
            p.connect(op, foreach);                       
           
            LogicalPlan innerPlan = new LogicalPlan();
            foreach.setInnerPlan(innerPlan);
                     
            // get output columns
            Set<Integer> cols = (Set<Integer>)op.getAnnotation(REQUIREDCOLS);           
           
            // build foreach inner plan
View Full Code Here

    @Override
    protected OperatorPlan buildPattern() {
        // the pattern that this rule looks for
        // is foreach -> filter
        LogicalPlan plan = new LogicalPlan();
        LogicalRelationalOperator foreach = new LOForEach(plan);
        LogicalRelationalOperator filter = new LOFilter(plan);
       
        plan.add(foreach);
        plan.add(filter);
        plan.connect(foreach, filter);
View Full Code Here

TOP

Related Classes of org.apache.pig.experimental.logical.relational.LOForEach

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.