use of com.yahoo.sketches.tuple.DoubleSummaryFactory 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);
}
use of com.yahoo.sketches.tuple.DoubleSummaryFactory 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);
}
use of com.yahoo.sketches.tuple.DoubleSummaryFactory 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);
}
}
use of com.yahoo.sketches.tuple.DoubleSummaryFactory in project sketches-pig by DataSketches.
the class DoubleSummarySketchToEstimatesTest method emptySketch.
@Test
public void emptySketch() throws Exception {
EvalFunc<Tuple> func = new DoubleSummarySketchToEstimates();
UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).build();
Tuple inputTuple = PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray()));
Tuple resultTuple = func.exec(inputTuple);
Assert.assertNotNull(resultTuple);
Assert.assertEquals(resultTuple.size(), 2);
Assert.assertEquals(resultTuple.get(0), 0.0);
Assert.assertEquals(resultTuple.get(0), 0.0);
}
use of com.yahoo.sketches.tuple.DoubleSummaryFactory in project sketches-pig by DataSketches.
the class DoubleSummarySketchToPercentileTest method normalCase.
@Test
public void normalCase() throws Exception {
EvalFunc<Double> func = new DoubleSummarySketchToPercentile();
UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).build();
int iterations = 100000;
for (int i = 0; i < iterations; i++) sketch.update(i, (double) i);
for (int i = 0; i < iterations; i++) sketch.update(i, (double) i);
Tuple inputTuple = PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray()), 50.0);
double result = func.exec(inputTuple);
Assert.assertEquals(result, iterations, iterations * 0.02);
}
Aggregations