Search in sources :

Example 1 with EvalFunc

use of org.apache.pig.EvalFunc 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);
}
Also used : DataBag(org.apache.pig.data.DataBag) FuncSpec(org.apache.pig.FuncSpec) TupleFactory(org.apache.pig.data.TupleFactory) EvalFunc(org.apache.pig.EvalFunc) BagFactory(org.apache.pig.data.BagFactory) Sketch(com.yahoo.sketches.theta.Sketch) PigUtil.tupleToSketch(com.yahoo.sketches.pig.theta.PigUtil.tupleToSketch) DataToSketch(com.yahoo.sketches.pig.theta.DataToSketch) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Example 2 with EvalFunc

use of org.apache.pig.EvalFunc in project sketches-pig by DataSketches.

the class UnionStringsSketchTest method algebraicInitial.

@Test
public void algebraicInitial() throws Exception {
    @SuppressWarnings("unchecked") EvalFunc<Tuple> func = (EvalFunc<Tuple>) Class.forName(new UnionStringsSketch().getInitial()).newInstance();
    DataBag bag = BAG_FACTORY.newDefaultBag();
    bag.add(TUPLE_FACTORY.newTuple());
    Tuple resultTuple = func.exec(TUPLE_FACTORY.newTuple(bag));
    Assert.assertNotNull(resultTuple);
    Assert.assertEquals(resultTuple.size(), 1);
    Assert.assertTrue(resultTuple.get(0) instanceof DataBag);
    Assert.assertEquals(((DataBag) resultTuple.get(0)).size(), 1);
}
Also used : DataBag(org.apache.pig.data.DataBag) EvalFunc(org.apache.pig.EvalFunc) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Example 3 with EvalFunc

use of org.apache.pig.EvalFunc in project sketches-pig by DataSketches.

the class UnionStringsSketchTest method algebraicIntermediateFinalEmptyInputTuple.

@Test
public void algebraicIntermediateFinalEmptyInputTuple() throws Exception {
    @SuppressWarnings("unchecked") EvalFunc<Tuple> func = (EvalFunc<Tuple>) Class.forName(new UnionStringsSketch().getIntermed()).newInstance();
    Tuple resultTuple = func.exec(TUPLE_FACTORY.newTuple());
    ItemsSketch<String> sketch = getSketch(resultTuple);
    Assert.assertTrue(sketch.isEmpty());
}
Also used : EvalFunc(org.apache.pig.EvalFunc) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Example 4 with EvalFunc

use of org.apache.pig.EvalFunc in project sketches-pig by DataSketches.

the class DataToSketchTest method algebraicIntermediateEmptyInputTuple.

@Test
public void algebraicIntermediateEmptyInputTuple() throws Exception {
    @SuppressWarnings("unchecked") EvalFunc<Tuple> func = (EvalFunc<Tuple>) Class.forName(new DataToSketch().getIntermed()).getConstructor(String.class).newInstance("10");
    Tuple result = func.exec(tupleFactory.newTuple());
    HllSketch sketch = getSketch((DataByteArray) result.get(0));
    Assert.assertTrue(sketch.isEmpty());
    Assert.assertEquals(sketch.getLgConfigK(), 10);
}
Also used : HllSketch(com.yahoo.sketches.hll.HllSketch) EvalFunc(org.apache.pig.EvalFunc) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Example 5 with EvalFunc

use of org.apache.pig.EvalFunc in project sketches-pig by DataSketches.

the class DataToSketchTest method algebraicFinalFromIntermediate.

@Test
public void algebraicFinalFromIntermediate() throws Exception {
    @SuppressWarnings("unchecked") EvalFunc<DataByteArray> func = (EvalFunc<DataByteArray>) Class.forName(new DataToSketch().getFinal()).newInstance();
    HllSketch inputSketch = new HllSketch(12);
    inputSketch.update("a");
    inputSketch.update("b");
    DataBag bag = bagFactory.newDefaultBag();
    bag.add(tupleFactory.newTuple(new DataByteArray(inputSketch.toCompactByteArray())));
    DataByteArray result = func.exec(tupleFactory.newTuple(bag));
    HllSketch sketch = getSketch(result);
    Assert.assertFalse(sketch.isEmpty());
    Assert.assertEquals(sketch.getEstimate(), 2.0, 0.01);
}
Also used : HllSketch(com.yahoo.sketches.hll.HllSketch) DataBag(org.apache.pig.data.DataBag) EvalFunc(org.apache.pig.EvalFunc) DataByteArray(org.apache.pig.data.DataByteArray) Test(org.testng.annotations.Test)

Aggregations

EvalFunc (org.apache.pig.EvalFunc)44 Test (org.testng.annotations.Test)44 Tuple (org.apache.pig.data.Tuple)35 HllSketch (com.yahoo.sketches.hll.HllSketch)18 DataBag (org.apache.pig.data.DataBag)16 DataByteArray (org.apache.pig.data.DataByteArray)16 FuncSpec (org.apache.pig.FuncSpec)12 ItemsSketch (com.yahoo.sketches.quantiles.ItemsSketch)2 BagFactory (org.apache.pig.data.BagFactory)2 TupleFactory (org.apache.pig.data.TupleFactory)2 DataToSketch (com.yahoo.sketches.pig.theta.DataToSketch)1 PigUtil.tupleToSketch (com.yahoo.sketches.pig.theta.PigUtil.tupleToSketch)1 Sketch (com.yahoo.sketches.theta.Sketch)1 Schema (org.apache.pig.impl.logicalLayer.schema.Schema)1