in datafu-pig/src/main/java/datafu/pig/stats/IntVAR.java [88:139]
public Tuple exec(Tuple input) throws IOException {
Tuple t = mTupleFactory.newTuple(3);
try {
// input is a bag with one tuple containing
// the column we are trying to get variance
DataBag bg = (DataBag) input.get(0);
Integer i = null;
Iterator<Tuple> iter = bg.iterator();
if(iter.hasNext()) {
Tuple tp = iter.next();
i = (Integer)tp.get(0);
}
if (iter.hasNext())
{
throw new RuntimeException("Expected only one tuple in bag");
}
if (i == null) {
t.set(2, 0L);
t.set(0, null);
t.set(1, null);
}
else {
t.set(2, 1L);
t.set(0, (long)i);
t.set(1, (long)i*i);
}
return t;
} catch(NumberFormatException nfe) {
nfe.printStackTrace();
// invalid input,
// treat this input as null
try {
t.set(0, null);
t.set(1, null);
t.set(2, 0L);
} catch (ExecException e) {
throw e;
}
return t;
} catch (ExecException ee) {
ee.printStackTrace();
throw ee;
} catch (Exception e) {
e.printStackTrace();
int errCode = 2106;
String msg = "Error while computing variance in " + this.getClass().getSimpleName();
throw new ExecException(msg, errCode, PigException.BUG, e);
}
}