Coverage Report - datafu.pig.bags.FirstTupleFromBag
 
Classes in this File Line Coverage Branch Coverage Complexity
FirstTupleFromBag
68%
11/16
50%
1/2
4.5
 
 1  
 package datafu.pig.bags;
 2  
 
 3  
 import java.io.IOException;
 4  
 
 5  
 import org.apache.pig.EvalFunc;
 6  
 import org.apache.pig.data.BagFactory;
 7  
 import org.apache.pig.data.DataBag;
 8  
 import org.apache.pig.data.Tuple;
 9  
 import org.apache.pig.data.TupleFactory;
 10  
 import org.apache.pig.impl.logicalLayer.schema.Schema;
 11  
 import org.apache.pig.impl.util.WrappedIOException;
 12  
 
 13  
 /* Given a bag, return the first tuple from the bag
 14  
 
 15  
 For example,
 16  
 FirstTupleFromBag({(1), (2)}, null) -> (1)
 17  
  */
 18  
 
 19  853
 public class FirstTupleFromBag extends EvalFunc<Object>
 20  
 {
 21  1
   private static final BagFactory bagFactory = BagFactory.getInstance();
 22  1
   private static final TupleFactory tupleFactory = TupleFactory.getInstance();
 23  
 
 24  
   @Override
 25  
   public Object exec(Tuple input) throws IOException
 26  
   {
 27  
     // PigStatusReporter reporter = PigStatusReporter.getInstance();
 28  
     try {
 29  1
       DataBag outputBag = bagFactory.newDefaultBag();
 30  1
       long i=0, j, cnt=0;
 31  1
       DataBag inputBag = (DataBag) input.get(0);
 32  1
       Object default_val = input.get(1);
 33  1
       for(Tuple bag2tuple : inputBag){
 34  1
         outputBag.add(bag2tuple);
 35  1
         return bag2tuple;
 36  
       }
 37  0
       return default_val;
 38  
     }
 39  0
     catch (Exception e) {
 40  0
       throw WrappedIOException.wrap("Caught exception processing input of " + this.getClass().getName(), e);
 41  
     }
 42  
   }
 43  
 
 44  
   @Override
 45  
   public Schema outputSchema(Schema input)
 46  
   {
 47  
     try {
 48  125
       return new Schema(input.getField(0).schema);
 49  
     }
 50  0
     catch (Exception e) {
 51  0
       return null;
 52  
     }
 53  
   }
 54  
 }
 55