Search in sources :

Example 1 with ArrayOfDoublesSketch

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

the class DataToArrayOfDoublesSketchTest method execEmptyBag.

@Test
public void execEmptyBag() throws Exception {
    EvalFunc<Tuple> func = new DataToArrayOfDoublesSketch("1");
    Tuple inputTuple = PigUtil.objectsToTuple(BagFactory.getInstance().newDefaultBag());
    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(), 0.0);
}
Also used : DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) ArrayOfDoublesSketch(com.yahoo.sketches.tuple.ArrayOfDoublesSketch) Test(org.testng.annotations.Test)

Example 2 with ArrayOfDoublesSketch

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

the class DataToArrayOfDoublesSketchTest method execWithSampling.

@Test
public void execWithSampling() throws Exception {
    EvalFunc<Tuple> func = new DataToArrayOfDoublesSketch("1024", "0.5", "1");
    DataBag bag = BagFactory.getInstance().newDefaultBag();
    int uniques = 10000;
    for (int i = 0; i < uniques; i++) bag.add(PigUtil.objectsToTuple(i, 1.0));
    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);
}
Also used : DataBag(org.apache.pig.data.DataBag) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) ArrayOfDoublesSketch(com.yahoo.sketches.tuple.ArrayOfDoublesSketch) Test(org.testng.annotations.Test)

Example 3 with ArrayOfDoublesSketch

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

the class DataToArrayOfDoublesSketchTest method execAllInputTypes.

@Test
public void execAllInputTypes() throws Exception {
    EvalFunc<Tuple> func = new DataToArrayOfDoublesSketch("32", "1");
    DataBag bag = BagFactory.getInstance().newDefaultBag();
    bag.add(PigUtil.objectsToTuple("a", 1.0));
    bag.add(PigUtil.objectsToTuple("b", 1.0));
    bag.add(PigUtil.objectsToTuple("a", 2.0));
    bag.add(PigUtil.objectsToTuple("b", 2.0));
    bag.add(PigUtil.objectsToTuple(1, 3.0));
    bag.add(PigUtil.objectsToTuple(2L, 3.0));
    bag.add(PigUtil.objectsToTuple(1f, 3.0));
    bag.add(PigUtil.objectsToTuple(2.0, 3.0));
    bag.add(PigUtil.objectsToTuple((byte) 3, 3.0));
    bag.add(PigUtil.objectsToTuple(new DataByteArray("c".getBytes()), 3.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);
    ArrayOfDoublesSketch sketch = ArrayOfDoublesSketches.heapifySketch(Memory.wrap(bytes.get()));
    Assert.assertEquals(sketch.getEstimate(), 8.0, 0.0);
    for (double[] values : sketch.getValues()) {
        Assert.assertEquals(values[0], 3.0);
    }
}
Also used : DataBag(org.apache.pig.data.DataBag) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) ArrayOfDoublesSketch(com.yahoo.sketches.tuple.ArrayOfDoublesSketch) Test(org.testng.annotations.Test)

Example 4 with ArrayOfDoublesSketch

use of com.yahoo.sketches.tuple.ArrayOfDoublesSketch 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 5 with ArrayOfDoublesSketch

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

Aggregations

ArrayOfDoublesSketch (com.yahoo.sketches.tuple.ArrayOfDoublesSketch)26 DataByteArray (org.apache.pig.data.DataByteArray)26 Tuple (org.apache.pig.data.Tuple)22 Test (org.testng.annotations.Test)19 DataBag (org.apache.pig.data.DataBag)12 ArrayOfDoublesUpdatableSketch (com.yahoo.sketches.tuple.ArrayOfDoublesUpdatableSketch)8 ArrayOfDoublesUpdatableSketchBuilder (com.yahoo.sketches.tuple.ArrayOfDoublesUpdatableSketchBuilder)8 SummaryStatistics (org.apache.commons.math3.stat.descriptive.SummaryStatistics)3 ArrayOfDoublesSketchIterator (com.yahoo.sketches.tuple.ArrayOfDoublesSketchIterator)2 DoublesSketchBuilder (com.yahoo.sketches.quantiles.DoublesSketchBuilder)1 UpdateDoublesSketch (com.yahoo.sketches.quantiles.UpdateDoublesSketch)1 Random (java.util.Random)1 TTest (org.apache.commons.math3.stat.inference.TTest)1