@Override public BigDecimal exec(Tuple input) throws IOException { try { BigDecimal sum = sum(input); if (sum == null) { // either we were handed an empty bag or a bag // filled with nulls - return null in this case return null; } BigDecimal count = count(input); BigDecimal avg = null; if (count.compareTo(BigDecimal.ZERO) > 0) avg = div(sum, count); return avg; } catch (ExecException ee) { throw ee; } }
@Override public void accumulate(Tuple b) throws IOException { try { BigDecimal sum = sum(b); if(sum == null) { return; } // set default values if (intermediateSum == null || intermediateCount == null) { intermediateSum = BigDecimal.ZERO; intermediateCount = BigDecimal.ZERO; } BigDecimal count = (BigDecimal)count(b); if (count.compareTo(BigDecimal.ZERO) > 0) { intermediateCount = intermediateCount.add(count); intermediateSum = intermediateSum.add(sum); } } catch (ExecException ee) { throw ee; } catch (Exception e) { int errCode = 2106; String msg = "Error while computing average in " + this.getClass().getSimpleName(); throw new ExecException(msg, errCode, PigException.BUG, e); } }
@Override public BigDecimal exec(Tuple input) throws IOException { try { DataBag b = (DataBag)input.get(0); Tuple combined = combine(b); BigDecimal sum = (BigDecimal)combined.get(0); if (sum == null) { return null; } BigDecimal count = (BigDecimal)combined.get(1); BigDecimal avg = null; if (count.compareTo(BigDecimal.ZERO) > 0) { avg = div(sum,count); } return avg; } catch (ExecException ee) { throw ee; } catch (Exception e) { int errCode = 2106; String msg = "Error while computing average in " + this.getClass().getSimpleName(); throw new ExecException(msg, errCode, PigException.BUG, e); } } }
@Override public BigDecimal getValue() { BigDecimal avg = null; if (intermediateCount != null && (intermediateCount.compareTo(BigDecimal.ZERO) > 0)) { avg = div(intermediateSum,intermediateCount); } return avg; } }
@Override public Tuple exec(Tuple input) throws IOException { try { DataBag b = (DataBag)input.get(0); return combine(b); } catch (ExecException ee) { throw ee; } catch (Exception e) { int errCode = 2106; String msg = "Error while computing average in " + this.getClass().getSimpleName(); throw new ExecException(msg, errCode, PigException.BUG, e); } } }