Search in sources :

Example 11 with DoubleSummary

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

the class UnionDoubleSummarySketchTest method algebraicIntemediateFinalRandomized.

@Test
public void algebraicIntemediateFinalRandomized() throws Exception {
    EvalFunc<Tuple> func = new UnionDoubleSummarySketch.IntermediateFinal("16384");
    DataBag bag = BagFactory.getInstance().newDefaultBag();
    long key = 1;
    Random rnd = new Random();
    long uniques = 0;
    long updates = 0;
    // this is to simulate the output from a prior call of IntermediateFinal
    {
        UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).setNominalEntries(16384).build();
        for (int i = 0; i < 40000; i++) sketch.update(key++, rnd.nextDouble() * 20);
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
        uniques += 40000;
        updates += 40000;
    }
    // overlap
    key -= 20000;
    {
        UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).setNominalEntries(16384).build();
        for (int i = 0; i < 60000; i++) sketch.update(key++, rnd.nextDouble() * 20);
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
        uniques += 40000;
        updates += 60000;
    }
    // overlap
    key -= 20000;
    {
        UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).setNominalEntries(16384).build();
        for (int i = 0; i < 60000; i++) sketch.update(key++, rnd.nextDouble() * 20);
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
        uniques += 40000;
        updates += 60000;
    }
    // overlap
    key -= 20000;
    {
        UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).setNominalEntries(16384).build();
        for (int i = 0; i < 60000; i++) sketch.update(key++, rnd.nextDouble() * 20);
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
        uniques += 40000;
        updates += 60000;
    }
    {
        UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).setNominalEntries(16384).build();
        for (int i = 0; i < 40000; i++) sketch.update(key++, rnd.nextDouble() * 20);
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
        uniques += 40000;
        updates += 40000;
    }
    {
        UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).setNominalEntries(16384).build();
        for (int i = 0; i < 40000; i++) sketch.update(key++, rnd.nextDouble() * 20);
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
        uniques += 40000;
        updates += 40000;
    }
    // overlap
    key -= 20000;
    {
        UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).setNominalEntries(16384).build();
        for (int i = 0; i < 60000; i++) sketch.update(key++, rnd.nextDouble() * 20);
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
        uniques += 40000;
        updates += 60000;
    }
    Tuple resultTuple = func.exec(PigUtil.objectsToTuple(bag));
    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(), uniques, uniques * 0.01);
    double sum = 0;
    SketchIterator<DoubleSummary> it = sketch.iterator();
    while (it.next()) {
        sum += it.getSummary().getValue();
    }
    // each update added 10 to the total on average
    // there is a slight chance of failing here
    Assert.assertEquals(sum / sketch.getTheta(), updates * 10.0, updates * 10.0 * 0.02);
}
Also used : DataBag(org.apache.pig.data.DataBag) DoubleSummaryFactory(com.yahoo.sketches.tuple.DoubleSummaryFactory) UpdatableSketch(com.yahoo.sketches.tuple.UpdatableSketch) Random(java.util.Random) 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 DoubleSummary

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

the class UnionDoubleSummarySketchTest method accumulatorEmptySketch.

@Test
public void accumulatorEmptySketch() throws Exception {
    Accumulator<Tuple> func = new UnionDoubleSummarySketch("4096");
    DataBag bag = BagFactory.getInstance().newDefaultBag();
    {
        UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).build();
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
    }
    func.accumulate(PigUtil.objectsToTuple(bag));
    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 : UpdatableSketch(com.yahoo.sketches.tuple.UpdatableSketch) DataBag(org.apache.pig.data.DataBag) DoubleSummary(com.yahoo.sketches.tuple.DoubleSummary) UpdatableSketchBuilder(com.yahoo.sketches.tuple.UpdatableSketchBuilder) DoubleSummaryDeserializer(com.yahoo.sketches.tuple.DoubleSummaryDeserializer) DoubleSummaryFactory(com.yahoo.sketches.tuple.DoubleSummaryFactory) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Example 13 with DoubleSummary

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

the class DoubleSummarySketchToEstimates method exec.

@Override
public Tuple exec(final Tuple input) throws IOException {
    if ((input == null) || (input.size() == 0)) {
        return null;
    }
    final DataByteArray dba = (DataByteArray) input.get(0);
    final Sketch<DoubleSummary> sketch = Sketches.heapifySketch(Memory.wrap(dba.get()), SUMMARY_DESERIALIZER);
    final Tuple output = TupleFactory.getInstance().newTuple(2);
    output.set(0, sketch.getEstimate());
    double sum = 0;
    final SketchIterator<DoubleSummary> it = sketch.iterator();
    while (it.next()) {
        sum += it.getSummary().getValue();
    }
    output.set(1, sum / sketch.getTheta());
    return output;
}
Also used : DoubleSummary(com.yahoo.sketches.tuple.DoubleSummary) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple)

Example 14 with DoubleSummary

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

the class DoubleSummarySketchToPercentile method exec.

@Override
public Double exec(final Tuple input) throws IOException {
    if (input.size() != 2) {
        throw new IllegalArgumentException("expected two inputs: sketch and pecentile");
    }
    final DataByteArray dba = (DataByteArray) input.get(0);
    final Sketch<DoubleSummary> sketch = Sketches.heapifySketch(Memory.wrap(dba.get()), SUMMARY_DESERIALIZER);
    final double percentile = (double) input.get(1);
    if ((percentile < 0) || (percentile > 100)) {
        throw new IllegalArgumentException("percentile must be between 0 and 100");
    }
    final UpdateDoublesSketch qs = DoublesSketch.builder().setK(QUANTILES_SKETCH_SIZE).build();
    final SketchIterator<DoubleSummary> it = sketch.iterator();
    while (it.next()) {
        qs.update(it.getSummary().getValue());
    }
    return qs.getQuantile(percentile / 100);
}
Also used : UpdateDoublesSketch(com.yahoo.sketches.quantiles.UpdateDoublesSketch) DoubleSummary(com.yahoo.sketches.tuple.DoubleSummary) DataByteArray(org.apache.pig.data.DataByteArray)

Example 15 with DoubleSummary

use of com.yahoo.sketches.tuple.DoubleSummary 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)

Aggregations

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