Search in sources :

Example 6 with ArrayOfDoublesUpdatableSketchBuilder

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

the class DataToArrayOfDoublesSketchTest method algebraicIntermediateFinal.

@Test
public void algebraicIntermediateFinal() throws Exception {
    EvalFunc<Tuple> func = new DataToArrayOfDoublesSketch.IntermediateFinal("1");
    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
    {
        ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().build();
        sketch.update("b", new double[] { 1.0 });
        sketch.update("a", new double[] { 2.0 });
        sketch.update("b", new double[] { 2.0 });
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().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);
    ArrayOfDoublesSketch sketch = ArrayOfDoublesSketches.heapifySketch(Memory.wrap(bytes.get()));
    Assert.assertEquals(sketch.getEstimate(), 2.0, 0.0);
    for (double[] values : sketch.getValues()) {
        Assert.assertEquals(values[0], 3.0);
    }
}
Also used : ArrayOfDoublesUpdatableSketchBuilder(com.yahoo.sketches.tuple.ArrayOfDoublesUpdatableSketchBuilder) DataBag(org.apache.pig.data.DataBag) ArrayOfDoublesUpdatableSketch(com.yahoo.sketches.tuple.ArrayOfDoublesUpdatableSketch) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) ArrayOfDoublesSketch(com.yahoo.sketches.tuple.ArrayOfDoublesSketch) Test(org.testng.annotations.Test)

Example 7 with ArrayOfDoublesUpdatableSketchBuilder

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

the class UnionArrayOfDoublesSketchTest method accumulatorEmptySketch.

@Test
public void accumulatorEmptySketch() throws Exception {
    Accumulator<Tuple> func = new UnionArrayOfDoublesSketch("1");
    DataBag bag = BagFactory.getInstance().newDefaultBag();
    {
        ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().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);
    ArrayOfDoublesSketch sketch = ArrayOfDoublesSketches.heapifySketch(Memory.wrap(bytes.get()));
    Assert.assertEquals(sketch.getEstimate(), 0.0);
}
Also used : ArrayOfDoublesUpdatableSketchBuilder(com.yahoo.sketches.tuple.ArrayOfDoublesUpdatableSketchBuilder) DataBag(org.apache.pig.data.DataBag) ArrayOfDoublesUpdatableSketch(com.yahoo.sketches.tuple.ArrayOfDoublesUpdatableSketch) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) ArrayOfDoublesSketch(com.yahoo.sketches.tuple.ArrayOfDoublesSketch) Test(org.testng.annotations.Test)

Example 8 with ArrayOfDoublesUpdatableSketchBuilder

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

the class UnionArrayOfDoublesSketchTest method algebraicIntemediateFinalSingleCall.

@Test
public void algebraicIntemediateFinalSingleCall() throws Exception {
    EvalFunc<Tuple> func = new UnionArrayOfDoublesSketch.IntermediateFinal("1024", "1");
    DataBag bag = BagFactory.getInstance().newDefaultBag();
    long value = 1;
    // this is to simulate the output from a prior call of IntermediateFinal
    {
        ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(1024).build();
        for (int i = 0; i < 10000; i++) sketch.update(value++, new double[] { 1.0 });
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
    }
    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);
    ArrayOfDoublesSketch sketch = ArrayOfDoublesSketches.heapifySketch(Memory.wrap(bytes.get()));
    Assert.assertEquals(sketch.getEstimate(), 10000.0, 10000.0 * 0.02);
    for (double[] values : sketch.getValues()) {
        Assert.assertEquals(values[0], 1.0, 0.0);
    }
}
Also used : ArrayOfDoublesUpdatableSketchBuilder(com.yahoo.sketches.tuple.ArrayOfDoublesUpdatableSketchBuilder) DataBag(org.apache.pig.data.DataBag) ArrayOfDoublesUpdatableSketch(com.yahoo.sketches.tuple.ArrayOfDoublesUpdatableSketch) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) ArrayOfDoublesSketch(com.yahoo.sketches.tuple.ArrayOfDoublesSketch) Test(org.testng.annotations.Test)

Example 9 with ArrayOfDoublesUpdatableSketchBuilder

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

the class UnionArrayOfDoublesSketchTest method exec.

@Test
public void exec() throws Exception {
    EvalFunc<Tuple> func = new UnionArrayOfDoublesSketch("4096", "1");
    DataBag bag = BagFactory.getInstance().newDefaultBag();
    {
        ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().build();
        sketch.update(1, new double[] { 1.0 });
        sketch.update(2, new double[] { 1.0 });
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
    }
    {
        ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().build();
        sketch.update(1, new double[] { 1.0 });
        sketch.update(2, new double[] { 1.0 });
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
    }
    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);
    ArrayOfDoublesSketch sketch = ArrayOfDoublesSketches.heapifySketch(Memory.wrap(bytes.get()));
    Assert.assertEquals(sketch.getEstimate(), 2.0, 0.0);
    for (double[] values : sketch.getValues()) {
        Assert.assertEquals(values[0], 2.0, 0.0);
    }
}
Also used : ArrayOfDoublesUpdatableSketchBuilder(com.yahoo.sketches.tuple.ArrayOfDoublesUpdatableSketchBuilder) DataBag(org.apache.pig.data.DataBag) ArrayOfDoublesUpdatableSketch(com.yahoo.sketches.tuple.ArrayOfDoublesUpdatableSketch) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) ArrayOfDoublesSketch(com.yahoo.sketches.tuple.ArrayOfDoublesSketch) Test(org.testng.annotations.Test)

Example 10 with ArrayOfDoublesUpdatableSketchBuilder

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

the class UnionArrayOfDoublesSketchTest method algebraicIntemediateFinalRandomized.

@Test
public void algebraicIntemediateFinalRandomized() throws Exception {
    EvalFunc<Tuple> func = new UnionArrayOfDoublesSketch.IntermediateFinal("16384", "1");
    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
    {
        ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(16384).build();
        for (int i = 0; i < 40000; i++) sketch.update(key++, new double[] { rnd.nextDouble() * 20 });
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
        uniques += 40000;
        updates += 40000;
    }
    // overlap
    key -= 20000;
    {
        ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(16384).build();
        for (int i = 0; i < 60000; i++) sketch.update(key++, new double[] { rnd.nextDouble() * 20 });
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
        uniques += 40000;
        updates += 60000;
    }
    // overlap
    key -= 20000;
    {
        ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(16384).build();
        for (int i = 0; i < 60000; i++) sketch.update(key++, new double[] { rnd.nextDouble() * 20 });
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
        uniques += 40000;
        updates += 60000;
    }
    // overlap
    key -= 20000;
    {
        ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(16384).build();
        for (int i = 0; i < 60000; i++) sketch.update(key++, new double[] { rnd.nextDouble() * 20 });
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
        uniques += 40000;
        updates += 60000;
    }
    {
        ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(16384).build();
        for (int i = 0; i < 40000; i++) sketch.update(key++, new double[] { rnd.nextDouble() * 20 });
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
        uniques += 40000;
        updates += 40000;
    }
    {
        ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(16384).build();
        for (int i = 0; i < 40000; i++) sketch.update(key++, new double[] { rnd.nextDouble() * 20 });
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
        uniques += 40000;
        updates += 40000;
    }
    // overlap
    key -= 20000;
    {
        ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(16384).build();
        for (int i = 0; i < 60000; i++) sketch.update(key++, new double[] { 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);
    ArrayOfDoublesSketch sketch = ArrayOfDoublesSketches.heapifySketch(Memory.wrap(bytes.get()));
    Assert.assertEquals(sketch.getEstimate(), uniques, uniques * 0.01);
    double sum = 0;
    for (double[] values : sketch.getValues()) {
        sum += values[0];
    }
    // 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 : ArrayOfDoublesUpdatableSketchBuilder(com.yahoo.sketches.tuple.ArrayOfDoublesUpdatableSketchBuilder) DataBag(org.apache.pig.data.DataBag) ArrayOfDoublesUpdatableSketch(com.yahoo.sketches.tuple.ArrayOfDoublesUpdatableSketch) Random(java.util.Random) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) ArrayOfDoublesSketch(com.yahoo.sketches.tuple.ArrayOfDoublesSketch) Test(org.testng.annotations.Test)

Aggregations

ArrayOfDoublesUpdatableSketchBuilder (com.yahoo.sketches.tuple.ArrayOfDoublesUpdatableSketchBuilder)32 ArrayOfDoublesUpdatableSketch (com.yahoo.sketches.tuple.ArrayOfDoublesUpdatableSketch)31 DataByteArray (org.apache.pig.data.DataByteArray)31 Test (org.testng.annotations.Test)29 Tuple (org.apache.pig.data.Tuple)26 DataBag (org.apache.pig.data.DataBag)11 ArrayOfDoublesSketch (com.yahoo.sketches.tuple.ArrayOfDoublesSketch)8 TTest (org.apache.commons.math3.stat.inference.TTest)6 Random (java.util.Random)4 DoublesSketch (com.yahoo.sketches.quantiles.DoublesSketch)2 ArrayOfDoublesSetOperationBuilder (com.yahoo.sketches.tuple.ArrayOfDoublesSetOperationBuilder)1 ArrayOfDoublesUnion (com.yahoo.sketches.tuple.ArrayOfDoublesUnion)1