NullToEmptyBag extends EvalFunc<DataBag> { @Override public DataBag exec(Tuple tuple) throws IOException { if (tuple == null || tuple.size() != 1) { return null; } DataBag db = (DataBag) tuple.get(0); if (db == null) { BagFactory bagFactory = BagFactory.getInstance(); return bagFactory.newDefaultBag(); } return db; }  @Override DataBag exec(Tuple tuple); @Override Schema outputSchema(Schema input);  }
@Test public void testUDF() throws IOException { NullToEmptyBag udf = new NullToEmptyBag(); TupleFactory tupleFactory = TupleFactory.getInstance(); BagFactory bagFactory = BagFactory.getInstance(); DataBag emptyBag = bagFactory.newDefaultBag(); DataBag bag = bagFactory.newDefaultBag(Lists.newArrayList(tupleFactory.newTuple("tup1"), tupleFactory.newTuple())); assertNull(udf.exec(null)); assertNull(udf.exec(tupleFactory.newTuple())); assertEquals(emptyBag, udf.exec(tupleFactory.newTuple((DataBag)null))); assertEquals(emptyBag, udf.exec(tupleFactory.newTuple(emptyBag))); assertEquals(bag, udf.exec(tupleFactory.newTuple(bag))); assertNull(udf.exec(tupleFactory.newTuple(Lists.newArrayList(bag, emptyBag)))); }