Search in sources :

Example 31 with UpdateSketch

use of org.apache.datasketches.theta.UpdateSketch in project sketches-core by DataSketches.

the class TupleExamplesTest method example3.

@Test
public void example3() {
    // Load source sketches
    final UpdatableSketch<Integer, IntegerSummary> tupleSk = tupleBldr.build();
    final UpdateSketch thetaSk = thetaBldr.build();
    for (int i = 1; i <= 12; i++) {
        tupleSk.update(i, 1);
        thetaSk.update(i + 3);
    }
    // Union, stateless: tuple1, tuple2
    final Union<IntegerSummary> union = new Union<>(isso);
    final CompactSketch<IntegerSummary> ucsk = union.union(tupleSk, thetaSk, ufactory.newSummary().update(1));
    int entries = ucsk.getRetainedEntries();
    println("Union: " + entries);
    final SketchIterator<IntegerSummary> uiter = ucsk.iterator();
    int counter = 1;
    int twos = 0;
    int ones = 0;
    while (uiter.next()) {
        final int i = uiter.getSummary().getValue();
        // 9 entries = 2, 6 entries = 1
        println(counter++ + ", " + i);
        if (i == 1) {
            ones++;
        }
        if (i == 2) {
            twos++;
        }
    }
    assertEquals(ones, 6);
    assertEquals(twos, 9);
    // Intersection stateless: tuple1, tuple2
    final Intersection<IntegerSummary> inter = new Intersection<>(isso);
    final CompactSketch<IntegerSummary> icsk = inter.intersect(tupleSk, thetaSk, ufactory.newSummary().update(1));
    entries = icsk.getRetainedEntries();
    println("Intersection: " + entries);
    final SketchIterator<IntegerSummary> iiter = icsk.iterator();
    counter = 1;
    while (iiter.next()) {
        final int i = iiter.getSummary().getValue();
        // 9 entries = 2
        println(counter++ + ", " + i);
        assertEquals(i, 1);
    }
}
Also used : IntegerSummary(org.apache.datasketches.tuple.aninteger.IntegerSummary) UpdateSketch(org.apache.datasketches.theta.UpdateSketch) Test(org.testng.annotations.Test)

Example 32 with UpdateSketch

use of org.apache.datasketches.theta.UpdateSketch in project sketches-core by DataSketches.

the class AdoubleIntersectionTest method checkEstimatingIntersectionWithThetaOverlapping.

@Test
public void checkEstimatingIntersectionWithThetaOverlapping() {
    final UpdateSketch thSkA = new UpdateSketchBuilder().setLogNominalEntries(4).build();
    final UpdateSketch thSkB = new UpdateSketchBuilder().setLogNominalEntries(10).build();
    // dense mode, low theta
    for (int i = 0; i < 64; i++) {
        thSkA.update(i);
    }
    // exact overlapping
    for (int i = 32; i < 96; i++) {
        thSkB.update(i);
    }
    final DoubleSummary dsum = new DoubleSummaryFactory(mode).newSummary();
    final Intersection<DoubleSummary> intersection = new Intersection<>(new DoubleSummarySetOperations(mode, mode));
    CompactSketch<DoubleSummary> result;
    intersection.intersect(thSkA, dsum);
    intersection.intersect(thSkB, dsum);
    result = intersection.getResult();
    Assert.assertEquals(result.getRetainedEntries(), 14);
    thSkB.reset();
    // exact, disjoint
    for (int i = 100; i < 164; i++) {
        thSkB.update(i);
    }
    // remove existing entries
    intersection.intersect(thSkB, dsum);
    result = intersection.getResult();
    Assert.assertEquals(result.getRetainedEntries(), 0);
    intersection.intersect(thSkB, dsum);
    result = intersection.getResult();
    Assert.assertEquals(result.getRetainedEntries(), 0);
}
Also used : Intersection(org.apache.datasketches.tuple.Intersection) UpdateSketchBuilder(org.apache.datasketches.theta.UpdateSketchBuilder) UpdateSketch(org.apache.datasketches.theta.UpdateSketch) Test(org.testng.annotations.Test)

Example 33 with UpdateSketch

use of org.apache.datasketches.theta.UpdateSketch in project sketches-core by DataSketches.

the class AdoubleIntersectionTest method checkExactIntersectionWithTheta.

@Test
public void checkExactIntersectionWithTheta() {
    final UpdateSketch thSkNull = null;
    final UpdateSketch thSkEmpty = new UpdateSketchBuilder().build();
    final UpdateSketch thSk10 = new UpdateSketchBuilder().build();
    final UpdateSketch thSk15 = new UpdateSketchBuilder().build();
    for (int i = 0; i < 10; i++) {
        thSk10.update(i);
    }
    // overlap = 5
    for (int i = 0; i < 10; i++) {
        thSk15.update(i + 5);
    }
    DoubleSummary dsum = new DoubleSummaryFactory(mode).newSummary();
    final Intersection<DoubleSummary> intersection = new Intersection<>(new DoubleSummarySetOperations(mode, mode));
    CompactSketch<DoubleSummary> result;
    try {
        intersection.getResult();
        fail();
    }// OK.
     catch (final SketchesStateException e) {
    }
    try {
        intersection.intersect(thSkNull, dsum);
        fail();
    }// OK
     catch (final SketchesArgumentException e) {
    }
    intersection.intersect(thSkEmpty, dsum);
    result = intersection.getResult();
    // Empty after empty first call
    Assert.assertTrue(result.isEmpty());
    intersection.reset();
    intersection.intersect(thSk10, dsum);
    result = intersection.getResult();
    // Returns valid first call
    Assert.assertEquals(result.getEstimate(), 10.0);
    intersection.reset();
    // Valid first call
    intersection.intersect(thSk10, dsum);
    intersection.intersect(thSkEmpty, dsum);
    result = intersection.getResult();
    // Returns Empty after empty second call
    Assert.assertTrue(result.isEmpty());
    intersection.reset();
    intersection.intersect(thSk10, dsum);
    intersection.intersect(thSk15, dsum);
    result = intersection.getResult();
    // Returns intersection
    Assert.assertEquals(result.getEstimate(), 5.0);
    intersection.reset();
    dsum = null;
    try {
        intersection.intersect(thSk10, dsum);
        fail();
    } catch (final SketchesArgumentException e) {
    }
}
Also used : Intersection(org.apache.datasketches.tuple.Intersection) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) UpdateSketchBuilder(org.apache.datasketches.theta.UpdateSketchBuilder) SketchesStateException(org.apache.datasketches.SketchesStateException) UpdateSketch(org.apache.datasketches.theta.UpdateSketch) Test(org.testng.annotations.Test)

Example 34 with UpdateSketch

use of org.apache.datasketches.theta.UpdateSketch in project sketches-core by DataSketches.

the class AdoubleUnionTest method checkUnionUpdateWithTheta.

@Test
public void checkUnionUpdateWithTheta() {
    final Union<DoubleSummary> union = new Union<>(new DoubleSummarySetOperations(mode, mode));
    UpdateSketch usk = null;
    DoubleSummary dsum = null;
    try {
        union.union(usk, dsum);
        fail();
    } catch (final SketchesArgumentException e) {
    }
    usk = new UpdateSketchBuilder().build();
    try {
        union.union(usk, dsum);
        fail();
    } catch (final SketchesArgumentException e) {
    }
    dsum = new DoubleSummaryFactory(mode).newSummary();
    for (int i = 0; i < 10; i++) {
        usk.update(i);
    }
    union.union(usk, dsum);
    Assert.assertEquals(union.getResult().getEstimate(), 10.0);
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) UpdateSketchBuilder(org.apache.datasketches.theta.UpdateSketchBuilder) Union(org.apache.datasketches.tuple.Union) UpdateSketch(org.apache.datasketches.theta.UpdateSketch) Test(org.testng.annotations.Test)

Example 35 with UpdateSketch

use of org.apache.datasketches.theta.UpdateSketch in project sketches-core by DataSketches.

the class CornerCaseTupleSetOperationsTest method estimationDegenerate.

@Test
public void estimationDegenerate() {
    IntegerSketch tupleA = getTupleSketch(SkType.ESTIMATION, MIDP_FLT, LT_LOWP_V);
    IntegerSketch tupleB = getTupleSketch(SkType.DEGENERATE, LOWP_FLT, GT_LOWP_V);
    UpdateSketch thetaB = getThetaSketch(SkType.DEGENERATE, LOWP_FLT, GT_LOWP_V);
    final double expectedIntersectTheta = LOWP_FLT;
    final int expectedIntersectCount = 0;
    final boolean expectedIntersectEmpty = false;
    final double expectedAnotbTheta = LOWP_FLT;
    final int expectedAnotbCount = 1;
    final boolean expectedAnotbEmpty = false;
    final double expectedUnionTheta = LOWP_FLT;
    final int expectedUnionCount = 1;
    final boolean expectedUnionEmpty = false;
    checks(tupleA, tupleB, thetaB, expectedIntersectTheta, expectedIntersectCount, expectedIntersectEmpty, expectedAnotbTheta, expectedAnotbCount, expectedAnotbEmpty, expectedUnionTheta, expectedUnionCount, expectedUnionEmpty);
}
Also used : UpdateSketch(org.apache.datasketches.theta.UpdateSketch) Test(org.testng.annotations.Test)

Aggregations

UpdateSketch (org.apache.datasketches.theta.UpdateSketch)46 Test (org.testng.annotations.Test)42 DoubleSummary (org.apache.datasketches.tuple.adouble.DoubleSummary)12 AnotB (org.apache.datasketches.tuple.AnotB)6 JaccardSimilarity.dissimilarityTest (org.apache.datasketches.tuple.JaccardSimilarity.dissimilarityTest)6 JaccardSimilarity.similarityTest (org.apache.datasketches.tuple.JaccardSimilarity.similarityTest)6 UpdateSketchBuilder (org.apache.datasketches.theta.UpdateSketchBuilder)5 Intersection (org.apache.datasketches.tuple.Intersection)4 MapBasedRow (org.apache.druid.data.input.MapBasedRow)3 TestColumnSelectorFactory (org.apache.druid.query.groupby.epinephelinae.TestColumnSelectorFactory)3 Test (org.junit.Test)3 SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)2 IntegerSummary (org.apache.datasketches.tuple.aninteger.IntegerSummary)2 GroupByQueryRunnerTest (org.apache.druid.query.groupby.GroupByQueryRunnerTest)2 SketchesStateException (org.apache.datasketches.SketchesStateException)1 CompactSketch (org.apache.datasketches.theta.CompactSketch)1 Intersection (org.apache.datasketches.theta.Intersection)1 Union (org.apache.datasketches.tuple.Union)1 SketchHolder (org.apache.druid.query.aggregation.datasketches.theta.SketchHolder)1 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)1