Search in sources :

Example 11 with DoubleSummaryDeserializer

use of com.yahoo.sketches.tuple.DoubleSummaryDeserializer in project sketches-pig by DataSketches.

the class DataToDoubleSummarySketchTest method execMinMode.

@Test
public void execMinMode() throws Exception {
    EvalFunc<Tuple> func = new DataToDoubleSummarySketch("32", "Min");
    DataBag bag = BagFactory.getInstance().newDefaultBag();
    bag.add(PigUtil.objectsToTuple("a", 1.0));
    bag.add(PigUtil.objectsToTuple("b", 2.0));
    bag.add(PigUtil.objectsToTuple("a", 2.0));
    bag.add(PigUtil.objectsToTuple("b", 1.0));
    Tuple inputTuple = PigUtil.objectsToTuple(bag);
    Tuple resultTuple = func.exec(inputTuple);
    Assert.assertNotNull(resultTuple);
    Assert.assertEquals(resultTuple.size(), 1);
    DataByteArray bytes = (DataByteArray) resultTuple.get(0);
    Assert.assertTrue(bytes.size() > 0);
    Sketch<DoubleSummary> sketch = Sketches.heapifySketch(Memory.wrap(bytes.get()), new DoubleSummaryDeserializer());
    Assert.assertEquals(sketch.getEstimate(), 2.0, 0.0);
    SketchIterator<DoubleSummary> it = sketch.iterator();
    while (it.next()) {
        Assert.assertEquals(it.getSummary().getValue(), 1.0);
    }
}
Also used : DataBag(org.apache.pig.data.DataBag) DoubleSummary(com.yahoo.sketches.tuple.DoubleSummary) DoubleSummaryDeserializer(com.yahoo.sketches.tuple.DoubleSummaryDeserializer) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Example 12 with DoubleSummaryDeserializer

use of com.yahoo.sketches.tuple.DoubleSummaryDeserializer in project sketches-pig by DataSketches.

the class DataToDoubleSummarySketchTest method algebraicIntermediateFinal.

@Test
public void algebraicIntermediateFinal() throws Exception {
    EvalFunc<Tuple> func = new DataToDoubleSummarySketch.IntermediateFinal("32");
    Tuple inputTuple = TupleFactory.getInstance().newTuple(1);
    DataBag bag = BagFactory.getInstance().newDefaultBag();
    inputTuple.set(0, bag);
    // this is to simulate the output from Initial
    bag.add(PigUtil.objectsToTuple(PigUtil.tuplesToBag(PigUtil.objectsToTuple("a", 1.0))));
    // this is to simulate the output from a prior call of IntermediateFinal
    UpdatableSketch<Double, DoubleSummary> ts = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).build();
    ts.update("b", 1.0);
    ts.update("a", 2.0);
    ts.update("b", 2.0);
    Sketch<DoubleSummary> cs = ts.compact();
    bag.add(PigUtil.objectsToTuple(new DataByteArray(cs.toByteArray())));
    Tuple resultTuple = func.exec(inputTuple);
    Assert.assertNotNull(resultTuple);
    Assert.assertEquals(resultTuple.size(), 1);
    DataByteArray bytes = (DataByteArray) resultTuple.get(0);
    Assert.assertTrue(bytes.size() > 0);
    Sketch<DoubleSummary> sketch = Sketches.heapifySketch(Memory.wrap(bytes.get()), new DoubleSummaryDeserializer());
    Assert.assertEquals(sketch.getEstimate(), 2.0, 0.0);
    SketchIterator<DoubleSummary> it = sketch.iterator();
    while (it.next()) {
        Assert.assertEquals(it.getSummary().getValue(), 3.0);
    }
}
Also used : DataBag(org.apache.pig.data.DataBag) DoubleSummaryFactory(com.yahoo.sketches.tuple.DoubleSummaryFactory) DoubleSummary(com.yahoo.sketches.tuple.DoubleSummary) DoubleSummaryDeserializer(com.yahoo.sketches.tuple.DoubleSummaryDeserializer) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Example 13 with DoubleSummaryDeserializer

use of com.yahoo.sketches.tuple.DoubleSummaryDeserializer in project sketches-pig by DataSketches.

the class DataToDoubleSummarySketchTest method accumulator.

@Test
public void accumulator() throws Exception {
    Accumulator<Tuple> func = new DataToDoubleSummarySketch("32");
    Tuple inputTuple = TupleFactory.getInstance().newTuple(1);
    DataBag bag = BagFactory.getInstance().newDefaultBag();
    bag.add(PigUtil.objectsToTuple("a", 1.0));
    inputTuple.set(0, bag);
    func.accumulate(inputTuple);
    inputTuple = TupleFactory.getInstance().newTuple(1);
    bag = BagFactory.getInstance().newDefaultBag();
    bag.add(PigUtil.objectsToTuple("b", 1.0));
    bag.add(PigUtil.objectsToTuple("a", 2.0));
    bag.add(PigUtil.objectsToTuple("b", 2.0));
    inputTuple.set(0, bag);
    func.accumulate(inputTuple);
    Tuple resultTuple = func.getValue();
    Assert.assertNotNull(resultTuple);
    Assert.assertEquals(resultTuple.size(), 1);
    DataByteArray bytes = (DataByteArray) resultTuple.get(0);
    Assert.assertTrue(bytes.size() > 0);
    Sketch<DoubleSummary> sketch = Sketches.heapifySketch(Memory.wrap(bytes.get()), new DoubleSummaryDeserializer());
    Assert.assertEquals(sketch.getEstimate(), 2.0, 0.0);
    SketchIterator<DoubleSummary> it = sketch.iterator();
    while (it.next()) {
        Assert.assertEquals(it.getSummary().getValue(), 3.0);
    }
    // after cleanup, the value should always be 0
    func.cleanup();
    resultTuple = func.getValue();
    Assert.assertNotNull(resultTuple);
    Assert.assertEquals(resultTuple.size(), 1);
    bytes = (DataByteArray) resultTuple.get(0);
    Assert.assertTrue(bytes.size() > 0);
    Sketch<DoubleSummary> sketch2 = Sketches.heapifySketch(Memory.wrap(bytes.get()), new DoubleSummaryDeserializer());
    Assert.assertEquals(sketch2.getEstimate(), 0.0, 0.0);
}
Also used : DataBag(org.apache.pig.data.DataBag) DoubleSummary(com.yahoo.sketches.tuple.DoubleSummary) DoubleSummaryDeserializer(com.yahoo.sketches.tuple.DoubleSummaryDeserializer) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Example 14 with DoubleSummaryDeserializer

use of com.yahoo.sketches.tuple.DoubleSummaryDeserializer in project sketches-pig by DataSketches.

the class DataToDoubleSummarySketchTest method algebraicIntermediateFinalMaxMode.

@Test
public void algebraicIntermediateFinalMaxMode() throws Exception {
    EvalFunc<Tuple> func = new DataToDoubleSummarySketch.IntermediateFinal("32", "Max");
    Tuple inputTuple = TupleFactory.getInstance().newTuple(1);
    DataBag bag = BagFactory.getInstance().newDefaultBag();
    inputTuple.set(0, bag);
    // this is to simulate the output from Initial
    bag.add(PigUtil.objectsToTuple(PigUtil.tuplesToBag(PigUtil.objectsToTuple("a", 1.0))));
    bag.add(PigUtil.objectsToTuple(PigUtil.tuplesToBag(PigUtil.objectsToTuple("b", 1.0))));
    bag.add(PigUtil.objectsToTuple(PigUtil.tuplesToBag(PigUtil.objectsToTuple("a", 2.0))));
    bag.add(PigUtil.objectsToTuple(PigUtil.tuplesToBag(PigUtil.objectsToTuple("b", 2.0))));
    Tuple resultTuple = func.exec(inputTuple);
    Assert.assertNotNull(resultTuple);
    Assert.assertEquals(resultTuple.size(), 1);
    DataByteArray bytes = (DataByteArray) resultTuple.get(0);
    Assert.assertTrue(bytes.size() > 0);
    Sketch<DoubleSummary> sketch = Sketches.heapifySketch(Memory.wrap(bytes.get()), new DoubleSummaryDeserializer());
    Assert.assertEquals(sketch.getEstimate(), 2.0, 0.0);
    SketchIterator<DoubleSummary> it = sketch.iterator();
    while (it.next()) {
        Assert.assertEquals(it.getSummary().getValue(), 2.0);
    }
}
Also used : DataBag(org.apache.pig.data.DataBag) DoubleSummary(com.yahoo.sketches.tuple.DoubleSummary) DoubleSummaryDeserializer(com.yahoo.sketches.tuple.DoubleSummaryDeserializer) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Example 15 with DoubleSummaryDeserializer

use of com.yahoo.sketches.tuple.DoubleSummaryDeserializer in project sketches-pig by DataSketches.

the class UnionDoubleSummarySketchTest method accumulatorNotABag.

@Test
public void accumulatorNotABag() throws Exception {
    Accumulator<Tuple> func = new UnionDoubleSummarySketch("32");
    func.accumulate(PigUtil.objectsToTuple((Object) null));
    Tuple resultTuple = func.getValue();
    Assert.assertNotNull(resultTuple);
    Assert.assertEquals(resultTuple.size(), 1);
    DataByteArray bytes = (DataByteArray) resultTuple.get(0);
    Assert.assertTrue(bytes.size() > 0);
    Sketch<DoubleSummary> sketch = Sketches.heapifySketch(Memory.wrap(bytes.get()), new DoubleSummaryDeserializer());
    Assert.assertEquals(sketch.getEstimate(), 0.0);
}
Also used : DoubleSummary(com.yahoo.sketches.tuple.DoubleSummary) DoubleSummaryDeserializer(com.yahoo.sketches.tuple.DoubleSummaryDeserializer) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Aggregations

DoubleSummary (com.yahoo.sketches.tuple.DoubleSummary)20 DoubleSummaryDeserializer (com.yahoo.sketches.tuple.DoubleSummaryDeserializer)20 DataByteArray (org.apache.pig.data.DataByteArray)20 Tuple (org.apache.pig.data.Tuple)20 Test (org.testng.annotations.Test)20 DataBag (org.apache.pig.data.DataBag)13 DoubleSummaryFactory (com.yahoo.sketches.tuple.DoubleSummaryFactory)9 UpdatableSketch (com.yahoo.sketches.tuple.UpdatableSketch)8 UpdatableSketchBuilder (com.yahoo.sketches.tuple.UpdatableSketchBuilder)5 Random (java.util.Random)1