use of org.apache.pig.data.BagFactory in project sketches-pig by DataSketches.
the class DataToSketchTest method textTopExec2.
/*
* DataToSketch <br>
* Tests all possible data types: NULL, BYTE, INTEGER, LONG, FLOAT, DOUBLE,
* BYTEARRAY, CHARARRAY. Tests rejection of a non-simple type.
*/
// still triggers unchecked warning
@SuppressWarnings("unchecked")
@Test
public void textTopExec2() throws IOException {
TupleFactory tupleFactory = TupleFactory.getInstance();
BagFactory bagFactory = BagFactory.getInstance();
String[] ctorArgs = { "128" };
EvalFunc<Tuple> dataUdf = (EvalFunc<Tuple>) PigContext.instantiateFuncFromSpec(new FuncSpec(udfName, ctorArgs));
// EvalFunc<Tuple> resultUdf = (EvalFunc<Tuple>)PigContext.
// instantiateFuncFromSpec(new FuncSpec(resultUdfName));
Tuple t;
DataBag bag = bagFactory.newDefaultBag();
// empty with a null
bag.add(tupleFactory.newTuple());
// 1 empty field
bag.add(tupleFactory.newTuple(1));
// 1
t = tupleFactory.newTuple(1);
t.set(0, new Byte((byte) 1));
bag.add(t);
// 2
t = tupleFactory.newTuple(1);
// int
t.set(0, new Integer(2));
bag.add(t);
// 3
t = tupleFactory.newTuple(1);
t.set(0, new Long(3));
bag.add(t);
// 4
t = tupleFactory.newTuple(1);
t.set(0, new Float(4));
bag.add(t);
// 5
t = tupleFactory.newTuple(1);
t.set(0, new Double(5));
bag.add(t);
// 6
t = tupleFactory.newTuple(1);
byte[] bArr = { 1, 2, 3 };
t.set(0, new DataByteArray(bArr));
bag.add(t);
// -ignore
t = tupleFactory.newTuple(1);
// empty
byte[] bArr2 = new byte[0];
t.set(0, new DataByteArray(bArr2));
bag.add(t);
// 7
t = tupleFactory.newTuple(1);
t.set(0, new Double(-0.0));
bag.add(t);
// 7 duplicate
t = tupleFactory.newTuple(1);
t.set(0, new Double(0.0));
bag.add(t);
// 8
t = tupleFactory.newTuple(1);
String s = "abcde";
t.set(0, s);
bag.add(t);
// - ignore
t = tupleFactory.newTuple(1);
// empty
String s2 = "";
t.set(0, s2);
bag.add(t);
Tuple in = tupleFactory.newTuple(1);
in.set(0, bag);
// should return a sketch
Tuple resultTuple = dataUdf.exec(in);
assertNotNull(resultTuple);
assertEquals(resultTuple.size(), 1);
DataByteArray bytes = (DataByteArray) resultTuple.get(0);
assertTrue(bytes.size() > 0);
Sketch sketch = tupleToSketch(resultTuple, seed_);
assertEquals(sketch.getEstimate(), 8.0, 0.0);
}
use of org.apache.pig.data.BagFactory in project sketches-pig by DataSketches.
the class DataToSketchTest method testRejectionOfNonSimpleType.
// still triggers unchecked warning
@SuppressWarnings("unchecked")
@Test(expectedExceptions = IllegalArgumentException.class)
public void testRejectionOfNonSimpleType() throws IOException {
TupleFactory mTupleFactory = TupleFactory.getInstance();
BagFactory bagFactory = BagFactory.getInstance();
Tuple outerTuple = mTupleFactory.newTuple(1);
DataBag outerBag = bagFactory.newDefaultBag();
Tuple innerTuple = mTupleFactory.newTuple(1);
DataBag innerBag = bagFactory.newDefaultBag();
innerTuple.set(0, innerBag);
outerBag.add(innerTuple);
outerTuple.set(0, outerBag);
String[] ctorArgs = { "128" };
EvalFunc<Tuple> dataUdf = (EvalFunc<Tuple>) PigContext.instantiateFuncFromSpec(new FuncSpec(udfName, ctorArgs));
dataUdf.exec(outerTuple);
}
Aggregations