Package org.apache.pig.newplan.logical.relational

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


    @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


    }

    @Override
    protected OperatorPlan buildPattern() {
        LogicalPlan plan = new LogicalPlan();
        LogicalRelationalOperator foreach = new LOForEach(plan);
        plan.add( foreach );
        return plan;
    }
View Full Code Here

            // distinct(flatten({(1), (1)})) is (1). However,
            // flatten(distinct({(1), (1)})) is (1), (1)
           
            // in both cases correctness is not affected
           
            LOForEach foreach = (LOForEach)matched.getSources().get(0);
            LOGenerate gen = OptimizerUtils.findGenerate( foreach );
            if( !OptimizerUtils.hasFlatten( gen ) )
                return false;
           
            List<Operator> succs = currentPlan.getSuccessors( foreach );
View Full Code Here

        @Override
        public void transform(OperatorPlan matched) throws FrontendException {
            subPlan = new OperatorSubPlan( currentPlan );
           
            LOForEach foreach = (LOForEach)matched.getSources().get(0);
            Operator next = currentPlan.getSuccessors( foreach ).get(0);
            if( next instanceof LOSort ) {
                Operator pred = currentPlan.getPredecessors( foreach ).get( 0 );
                List<Operator> succs = new ArrayList<Operator>();
                succs.addAll(currentPlan.getSuccessors( next ));
                Pair<Integer, Integer> pos1 = currentPlan.disconnect( pred, foreach );
                Pair<Integer, Integer> pos2 = currentPlan.disconnect( foreach, next );
                currentPlan.connect( pred, pos1.first, next, pos2.second );

                if( succs != null ) {
                    for( Operator succ : succs ) {
                        Pair<Integer, Integer> pos = currentPlan.disconnect( next, succ );
                        currentPlan.connect( next, pos.first, foreach, 0 );
                        currentPlan.connect( foreach, 0, succ, pos.second );
                    }
                } else {
                    currentPlan.connect( next, foreach );
                }
            } else if( next instanceof LOCross || next instanceof LOJoin ) {
                List<Operator> preds = currentPlan.getPredecessors( next );
                List<Integer> fieldsToBeFlattaned = new ArrayList<Integer>();
                Map<Integer, LogicalSchema> cachedUserDefinedSchema = new HashMap<Integer, LogicalSchema>();
                boolean[] flags = null;
                int fieldCount = 0;
                for( Operator op : preds ) {
                    if( op == foreach ) {
                        LOGenerate gen = OptimizerUtils.findGenerate( foreach );
                        flags = gen.getFlattenFlags();
                        for( int i = 0; i < flags.length; i++ ) {
                            if( flags[i] ) {
                                fieldsToBeFlattaned.add(fieldCount);
                                if (gen.getUserDefinedSchema()!=null && gen.getUserDefinedSchema().get(i)!=null) {
                                    cachedUserDefinedSchema.put(fieldCount, gen.getUserDefinedSchema().get(i));
                                    gen.getUserDefinedSchema().set(i, null);
                                }
                                fieldCount++;
                            } else {
                                fieldCount++;
                            }
                        }
                    } else {
                        fieldCount += ( (LogicalRelationalOperator)op ).getSchema().size();
                    }
                }
               
               
                boolean[] flattenFlags = new boolean[fieldCount];
                List<LogicalSchema> mUserDefinedSchema = null;
                if (cachedUserDefinedSchema!=null) {
                    mUserDefinedSchema = new ArrayList<LogicalSchema>();
                    for (int i=0;i<fieldCount;i++)
                        mUserDefinedSchema.add(null);
                }
                for( Integer i : fieldsToBeFlattaned ) {
                    flattenFlags[i] = true;
                    if (cachedUserDefinedSchema.containsKey(i)) {
                        mUserDefinedSchema.set(i, cachedUserDefinedSchema.get(i));
                    }
                }
               
                // Now create a new foreach after cross/join and insert it into the plan.
                LOForEach newForeach = new LOForEach( currentPlan );
                LogicalPlan innerPlan = new LogicalPlan();
                List<LogicalExpressionPlan> exprs = new ArrayList<LogicalExpressionPlan>( fieldCount );
                LOGenerate gen = new LOGenerate( innerPlan, exprs, flattenFlags );
                if (mUserDefinedSchema!=null)
                    gen.setUserDefinedSchema(mUserDefinedSchema);
                innerPlan.add( gen );
                newForeach.setInnerPlan( innerPlan );
                for( int i = 0; i < fieldCount; i++ ) {
                    LogicalExpressionPlan expr = new LogicalExpressionPlan();
                    expr.add( new ProjectExpression( expr, i, -1, gen ) );
                    exprs.add( expr );
                   
                    LOInnerLoad innerLoad = new LOInnerLoad(innerPlan, newForeach, i);
                    innerPlan.add(innerLoad);
                    innerPlan.connect(innerLoad, gen);
                }
               
                newForeach.setAlias(((LogicalRelationalOperator)next).getAlias());
               
                Operator opAfterX = null;
                List<Operator> succs = currentPlan.getSuccessors( next );
                if( succs == null || succs.size() == 0 ) {
                    currentPlan.add( newForeach );
View Full Code Here

        return newFs;
    }
   
    public static LOForEach addForEachAfter(LogicalPlan plan, LogicalRelationalOperator op, int branch,
            Set<Integer> columnsToDrop) throws FrontendException {
        LOForEach foreach = new LOForEach(plan);
       
        plan.add(foreach);
        List<Operator> next = plan.getSuccessors(op);
        if (next != null) {
            LogicalRelationalOperator nextOp = (LogicalRelationalOperator)next.get(branch);
            plan.insertBetween(op, foreach, nextOp);
        }
        else {
            plan.connect(op, foreach);
        }
       
        LogicalPlan innerPlan = new LogicalPlan();
        foreach.setInnerPlan(innerPlan);
       
        LogicalSchema schema = op.getSchema();
       
        // build foreach inner plan
        List<LogicalExpressionPlan> exps = new ArrayList<LogicalExpressionPlan>();
View Full Code Here

        planTester.buildPlan("c = foreach a generate a0, flatten(a2) as (q1, q2);");
        org.apache.pig.impl.logicalLayer.LogicalPlan lp = planTester.buildPlan("d = join c by a0, b by b0;");
       
        LogicalPlan newLogicalPlan = migrateAndOptimizePlan( lp );
       
        LOForEach foreach = (LOForEach)newLogicalPlan.getSinks().get( 0 );
        Assert.assertTrue(foreach.getSchema().getField(1).alias.equals("q1"));
        Assert.assertTrue(foreach.getSchema().getField(2).alias.equals("q2"));
    }
View Full Code Here

        aschema.addField(new LogicalSchema.LogicalFieldSchema("id", null, DataType.BYTEARRAY));
        aschema.addField(new LogicalSchema.LogicalFieldSchema("d", null, DataType.BYTEARRAY));
        LOLoad load = new LOLoad(new FileSpec("/test/d.txt", new FuncSpec("org.apache.pig.builtin.PigStorage")), aschema, expected, null);
        expected.add(load);
       
        LOForEach foreach = new LOForEach(expected);
        org.apache.pig.newplan.logical.relational.LogicalPlan innerPlan = new org.apache.pig.newplan.logical.relational.LogicalPlan();
        LOInnerLoad l1 = new LOInnerLoad(innerPlan, foreach, 0);
        innerPlan.add(l1);
        LOInnerLoad l2 = new LOInnerLoad(innerPlan, foreach, 1);
        innerPlan.add(l2);
       
        List<LogicalExpressionPlan> eps = new ArrayList<LogicalExpressionPlan>();
        LOGenerate gen = new LOGenerate(innerPlan, eps, new boolean[] {false, true});
        LogicalExpressionPlan p1 = new LogicalExpressionPlan();
        p1.add(new ProjectExpression(p1, 0, -1, gen));
        LogicalExpressionPlan p2 = new LogicalExpressionPlan();
        p2.add(new ProjectExpression(p2, 1, -1, gen));
        eps.add(p1);
        eps.add(p2);
       
       
        innerPlan.add(gen);
        innerPlan.connect(l1, gen);
        innerPlan.connect(l2, gen);
       
        foreach.setInnerPlan(innerPlan);     
        expected.add(foreach);
       
        LOStore s = new LOStore(expected, new FileSpec("/test/empty", new FuncSpec("org.apache.pig.builtin.PigStorage")));
     
        expected.add(s);
       
        expected.connect(load, foreach);
        expected.connect(foreach, s);
       
        assertTrue(expected.isEqual(newPlan));
       
        LogicalSchema schema = foreach.getSchema();
        aschema = new LogicalSchema();
        aschema.addField(new LogicalSchema.LogicalFieldSchema("id", null, DataType.BYTEARRAY));
        aschema.addField(new LogicalSchema.LogicalFieldSchema("d", null, DataType.BYTEARRAY));
        assertTrue(schema.isEqual(aschema));
    }
View Full Code Here

        aschema.addField(new LogicalSchema.LogicalFieldSchema("d", aschema2, DataType.BAG));       
       
        LOLoad load = new LOLoad(new FileSpec("/test/d.txt", new FuncSpec("org.apache.pig.builtin.PigStorage")), aschema, expected, null);
        expected.add(load);        
       
        LOForEach foreach2 = new LOForEach(expected);
        org.apache.pig.newplan.logical.relational.LogicalPlan innerPlan = new org.apache.pig.newplan.logical.relational.LogicalPlan();
        LOInnerLoad l1 = new LOInnerLoad(innerPlan, foreach2, 0);
        innerPlan.add(l1);
        LOInnerLoad l2 = new LOInnerLoad(innerPlan, foreach2, 1);
        innerPlan.add(l2);
       
        List<LogicalExpressionPlan>  eps = new ArrayList<LogicalExpressionPlan>();
        LOGenerate gen = new LOGenerate(innerPlan, eps, new boolean[] {false, true});
        LogicalExpressionPlan p1 = new LogicalExpressionPlan();
        new ProjectExpression(p1, 0, -1, gen);
        LogicalExpressionPlan p2 = new LogicalExpressionPlan();       
        new ProjectExpression(p2, 1, -1, gen);
        eps.add(p1);
        eps.add(p2);
       
        innerPlan.add(gen);
        innerPlan.connect(l1, gen);
        innerPlan.connect(l2, gen);
       
        foreach2.setInnerPlan(innerPlan);     
        expected.add(foreach2);
               
        LOStore s = new LOStore(expected, new FileSpec("/test/empty", new FuncSpec("org.apache.pig.builtin.PigStorage")));
     
        expected.add(s);
       
        expected.connect(load, foreach2);
   
        expected.connect(foreach2, s);
       
        System.out.println(newPlan);
       
        System.out.println(expected);
       
        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

        org.apache.pig.impl.logicalLayer.LogicalPlan plan = lpt.buildPlan( "store C into 'empty';" )
        LogicalPlan newLogicalPlan = migratePlan( plan );
       
        int forEachCount1 = getForEachOperatorCount( newLogicalPlan );
        int outputExprCount1 = getOutputExprCount( newLogicalPlan );
        LOForEach foreach1 = getForEachOperator( newLogicalPlan );
        Assert.assertTrue( foreach1.getAlias().equals( "C" ) );
              
        PlanOptimizer optimizer = new MyPlanOptimizer( newLogicalPlan, 3 );
        optimizer.optimize();
       
        int forEachCount2 = getForEachOperatorCount( newLogicalPlan );
        Assert.assertEquals( 1, forEachCount1 - forEachCount2 );
        int outputExprCount2 = getOutputExprCount( newLogicalPlan );
        Assert.assertTrue( outputExprCount1 == outputExprCount2 );
        LOForEach foreach2 = getForEachOperator( newLogicalPlan );
        Assert.assertTrue( foreach2.getAlias().equals( "C" ) );
    }
View Full Code Here

        org.apache.pig.impl.logicalLayer.LogicalPlan plan = lpt.buildPlan( "store C into 'empty';" )
        LogicalPlan newLogicalPlan = migratePlan( plan );
       
        int forEachCount1 = getForEachOperatorCount( newLogicalPlan );
        int outputExprCount1 = getOutputExprCount( newLogicalPlan );
        LOForEach foreach1 = getForEachOperator( newLogicalPlan );
        Assert.assertTrue( foreach1.getAlias().equals( "C" ) );
              
        PlanOptimizer optimizer = new MyPlanOptimizer( newLogicalPlan, 3 );
        optimizer.optimize();
       
        int forEachCount2 = getForEachOperatorCount( newLogicalPlan );
        // The number of FOREACHes didn't change because one is genereated because of type cast and
        // one is reduced because of the merge.
        Assert.assertEquals( 0, forEachCount1 - forEachCount2 );
        int outputExprCount2 = getOutputExprCount( newLogicalPlan );
        Assert.assertTrue( outputExprCount1 == outputExprCount2 );
        LOForEach foreach2 = getForEachOperator( newLogicalPlan );
        Assert.assertTrue( foreach2.getAlias().equals( "C" ) );
        LogicalSchema newSchema = foreach2.getSchema();
        Assert.assertTrue(newSchema.getField(0).alias.equals("x"));
        Assert.assertTrue(newSchema.getField(1).alias.equals("y"));
    }
View Full Code Here

TOP

Related Classes of org.apache.pig.newplan.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.