Search in sources :

Example 6 with ArrayOfDoublesUpdatableSketchBuilder

use of org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketchBuilder in project druid by druid-io.

the class ArrayOfDoublesSketchToEstimatePostAggregatorTest method testComparator.

@Test
public void testComparator() {
    ArrayOfDoublesUpdatableSketch s1 = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(16).setNumberOfValues(2).build();
    s1.update("foo", new double[] { 1.0, 2.0 });
    ArrayOfDoublesUpdatableSketch s2 = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(16).setNumberOfValues(2).build();
    s2.update("foo", new double[] { 2.0, 2.0 });
    s2.update("bar", new double[] { 3.0, 4.0 });
    PostAggregator field1 = EasyMock.createMock(PostAggregator.class);
    EasyMock.expect(field1.compute(EasyMock.anyObject(Map.class))).andReturn(s1).anyTimes();
    PostAggregator field2 = EasyMock.createMock(PostAggregator.class);
    EasyMock.expect(field2.compute(EasyMock.anyObject(Map.class))).andReturn(s2).anyTimes();
    EasyMock.replay(field1, field2);
    final ArrayOfDoublesSketchToEstimatePostAggregator postAgg1 = new ArrayOfDoublesSketchToEstimatePostAggregator("a", field1);
    final ArrayOfDoublesSketchToEstimatePostAggregator postAgg2 = new ArrayOfDoublesSketchToEstimatePostAggregator("a", field2);
    // estimate1 is 1.0, estimate2 is 2.0
    Double estimate1 = postAgg1.compute(ImmutableMap.of());
    Double estimate2 = postAgg2.compute(ImmutableMap.of());
    Assert.assertEquals(-1, postAgg1.getComparator().compare(estimate1, estimate2));
}
Also used : ArrayOfDoublesUpdatableSketchBuilder(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketchBuilder) ArrayOfDoublesUpdatableSketch(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketch) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) ConstantPostAggregator(org.apache.druid.query.aggregation.post.ConstantPostAggregator) Test(org.junit.Test)

Example 7 with ArrayOfDoublesUpdatableSketchBuilder

use of org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketchBuilder in project druid by druid-io.

the class ArrayOfDoublesSketchToNumEntriesPostAggregatorTest method testComparator.

@Test
public void testComparator() {
    ArrayOfDoublesUpdatableSketch s1 = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(16).setNumberOfValues(2).build();
    s1.update("foo", new double[] { 1.0, 2.0 });
    ArrayOfDoublesUpdatableSketch s2 = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(16).setNumberOfValues(2).build();
    s2.update("foo", new double[] { 2.0, 2.0 });
    s2.update("bar", new double[] { 3.0, 4.0 });
    PostAggregator field1 = EasyMock.createMock(PostAggregator.class);
    EasyMock.expect(field1.compute(EasyMock.anyObject(Map.class))).andReturn(s1).anyTimes();
    PostAggregator field2 = EasyMock.createMock(PostAggregator.class);
    EasyMock.expect(field2.compute(EasyMock.anyObject(Map.class))).andReturn(s2).anyTimes();
    EasyMock.replay(field1, field2);
    final ArrayOfDoublesSketchToNumEntriesPostAggregator postAgg1 = new ArrayOfDoublesSketchToNumEntriesPostAggregator("a", field1);
    final ArrayOfDoublesSketchToNumEntriesPostAggregator postAgg2 = new ArrayOfDoublesSketchToNumEntriesPostAggregator("a", field2);
    // computes number of entries per sketch, which is 1 for s1 and 2 for s2
    Integer numEntries1 = postAgg1.compute(ImmutableMap.of());
    Integer numEntries2 = postAgg2.compute(ImmutableMap.of());
    Assert.assertEquals(-1, postAgg1.getComparator().compare(numEntries1, numEntries2));
}
Also used : ArrayOfDoublesUpdatableSketchBuilder(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketchBuilder) ArrayOfDoublesUpdatableSketch(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketch) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) ConstantPostAggregator(org.apache.druid.query.aggregation.post.ConstantPostAggregator) Test(org.junit.Test)

Example 8 with ArrayOfDoublesUpdatableSketchBuilder

use of org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketchBuilder in project sketches-core by DataSketches.

the class ReadOnlyMemoryTest method wrapAndTryUpdatingSketch.

@Test
public void wrapAndTryUpdatingSketch() {
    final ArrayOfDoublesUpdatableSketch sketch1 = new ArrayOfDoublesUpdatableSketchBuilder().build();
    sketch1.update(1, new double[] { 1 });
    final ArrayOfDoublesUpdatableSketch sketch2 = (ArrayOfDoublesUpdatableSketch) ArrayOfDoublesSketches.wrapSketch(Memory.wrap(sketch1.toByteArray()));
    Assert.assertEquals(sketch2.getEstimate(), 1.0);
    sketch2.toByteArray();
    boolean thrown = false;
    try {
        sketch2.update(2, new double[] { 1 });
    } catch (final SketchesReadOnlyException e) {
        thrown = true;
    }
    try {
        sketch2.trim();
    } catch (final SketchesReadOnlyException e) {
        thrown = true;
    }
    Assert.assertTrue(thrown);
}
Also used : ArrayOfDoublesUpdatableSketchBuilder(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketchBuilder) ArrayOfDoublesUpdatableSketch(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketch) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 9 with ArrayOfDoublesUpdatableSketchBuilder

use of org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketchBuilder in project sketches-core by DataSketches.

the class ReadOnlyMemoryTest method heapifyAndUpdateUnion.

@Test
public void heapifyAndUpdateUnion() {
    final int numUniques = 10000;
    int key = 0;
    final ArrayOfDoublesUpdatableSketch sketch1 = new ArrayOfDoublesUpdatableSketchBuilder().build();
    for (int i = 0; i < numUniques; i++) {
        sketch1.update(key++, new double[] { 1 });
    }
    final ArrayOfDoublesUnion union1 = new ArrayOfDoublesSetOperationBuilder().buildUnion();
    union1.union(sketch1);
    final ArrayOfDoublesUnion union2 = ArrayOfDoublesSketches.heapifyUnion(Memory.wrap(union1.toByteArray()));
    final ArrayOfDoublesSketch resultSketch = union2.getResult();
    Assert.assertTrue(resultSketch.isEstimationMode());
    Assert.assertEquals(resultSketch.getEstimate(), numUniques, numUniques * 0.04);
    // make sure union update actually needs to modify the union
    final ArrayOfDoublesUpdatableSketch sketch2 = new ArrayOfDoublesUpdatableSketchBuilder().build();
    for (int i = 0; i < numUniques; i++) {
        sketch2.update(key++, new double[] { 1 });
    }
    union2.union(sketch2);
}
Also used : ArrayOfDoublesUpdatableSketchBuilder(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketchBuilder) ArrayOfDoublesUpdatableSketch(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketch) ArrayOfDoublesUnion(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUnion) ArrayOfDoublesSetOperationBuilder(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesSetOperationBuilder) ArrayOfDoublesSketch(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesSketch) Test(org.testng.annotations.Test)

Example 10 with ArrayOfDoublesUpdatableSketchBuilder

use of org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketchBuilder in project druid by druid-io.

the class ArrayOfDoublesSketchSetOpPostAggregatorTest method testComparator.

@Test
public void testComparator() {
    ArrayOfDoublesUpdatableSketch s1 = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(16).setNumberOfValues(2).build();
    s1.update("foo", new double[] { 1.0, 2.0 });
    ArrayOfDoublesUpdatableSketch s2 = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(16).setNumberOfValues(2).build();
    s2.update("foo", new double[] { 2.0, 2.0 });
    s2.update("bar", new double[] { 3.0, 4.0 });
    // duplicate
    ArrayOfDoublesUpdatableSketch s3 = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(16).setNumberOfValues(2).build();
    s3.update("foo", new double[] { 1.0, 2.0 });
    ArrayOfDoublesUpdatableSketch s4 = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(16).setNumberOfValues(2).build();
    s4.update("foo", new double[] { 2.0, 2.0 });
    s4.update("bar", new double[] { 3.0, 4.0 });
    PostAggregator field1 = EasyMock.createMock(PostAggregator.class);
    EasyMock.expect(field1.compute(EasyMock.anyObject(Map.class))).andReturn(s1).anyTimes();
    PostAggregator field2 = EasyMock.createMock(PostAggregator.class);
    EasyMock.expect(field2.compute(EasyMock.anyObject(Map.class))).andReturn(s2).anyTimes();
    PostAggregator field3 = EasyMock.createMock(PostAggregator.class);
    EasyMock.expect(field3.compute(EasyMock.anyObject(Map.class))).andReturn(s3).anyTimes();
    PostAggregator field4 = EasyMock.createMock(PostAggregator.class);
    EasyMock.expect(field4.compute(EasyMock.anyObject(Map.class))).andReturn(s4).anyTimes();
    EasyMock.replay(field1, field2, field3, field4);
    final ArrayOfDoublesSketchSetOpPostAggregator postAgg1 = new ArrayOfDoublesSketchSetOpPostAggregator("a", "UNION", 16, 2, ImmutableList.of(field1, field2));
    final ArrayOfDoublesSketchSetOpPostAggregator postAgg2 = new ArrayOfDoublesSketchSetOpPostAggregator("a", "UNION", 16, 2, ImmutableList.of(field3, field4));
    Comparator comparator = postAgg1.getComparator();
    ArrayOfDoublesSketch sketch1 = postAgg1.compute(ImmutableMap.of());
    ArrayOfDoublesSketch sketch2 = postAgg2.compute(ImmutableMap.of());
    // comparator compares value of each sketches estimate so should be identical
    Assert.assertEquals(0, comparator.compare(sketch1, sketch2));
    Assert.assertEquals(0, Double.compare(sketch1.getEstimate(), sketch2.getEstimate()));
}
Also used : ArrayOfDoublesUpdatableSketchBuilder(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketchBuilder) ArrayOfDoublesUpdatableSketch(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketch) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) ConstantPostAggregator(org.apache.druid.query.aggregation.post.ConstantPostAggregator) ArrayOfDoublesSketch(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesSketch) Comparator(java.util.Comparator) Test(org.junit.Test)

Aggregations

ArrayOfDoublesUpdatableSketchBuilder (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketchBuilder)12 ArrayOfDoublesUpdatableSketch (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketch)11 Test (org.junit.Test)6 PostAggregator (org.apache.druid.query.aggregation.PostAggregator)5 ConstantPostAggregator (org.apache.druid.query.aggregation.post.ConstantPostAggregator)5 ArrayOfDoublesSketch (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesSketch)4 Test (org.testng.annotations.Test)4 Comparator (java.util.Comparator)2 SketchesReadOnlyException (org.apache.datasketches.SketchesReadOnlyException)2 ArrayOfDoublesSetOperationBuilder (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesSetOperationBuilder)2 ArrayOfDoublesUnion (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUnion)2 BufferedWriter (java.io.BufferedWriter)1 Path (java.nio.file.Path)1 Random (java.util.Random)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 WritableMemory (org.apache.datasketches.memory.WritableMemory)1 DoublesSketch (org.apache.datasketches.quantiles.DoublesSketch)1 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)1 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)1 TestObjectColumnSelector (org.apache.druid.query.aggregation.TestObjectColumnSelector)1