Search in sources :

Example 1 with UpdateSketch

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

the class TupleExamplesTest method example1.

@Test
public void example1() {
    // 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 stateful: tuple, theta
    final Union<IntegerSummary> union = new Union<>(isso);
    union.union(tupleSk);
    union.union(thetaSk, ufactory.newSummary().update(1));
    final CompactSketch<IntegerSummary> ucsk = union.getResult();
    int entries = ucsk.getRetainedEntries();
    println("Union Stateful: tuple, theta: " + 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 stateful: tuple, theta
    final Intersection<IntegerSummary> inter = new Intersection<>(isso);
    inter.intersect(tupleSk);
    inter.intersect(thetaSk, ifactory.newSummary().update(1));
    final CompactSketch<IntegerSummary> icsk = inter.getResult();
    entries = icsk.getRetainedEntries();
    println("Intersection Stateful: tuple, theta: " + entries);
    final SketchIterator<IntegerSummary> iiter = icsk.iterator();
    counter = 1;
    while (iiter.next()) {
        final int i = iiter.getSummary().getValue();
        // 9 entries = 1
        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 2 with UpdateSketch

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

the class AdoubleIntersectionTest method checkExactIntersectionWithThetaDisjoint.

@Test
public void checkExactIntersectionWithThetaDisjoint() {
    final UpdateSketch thSkA = new UpdateSketchBuilder().setLogNominalEntries(10).build();
    final UpdateSketch thSkB = new UpdateSketchBuilder().setLogNominalEntries(10).build();
    int key = 0;
    for (int i = 0; i < 32; i++) {
        thSkA.update(key++);
    }
    for (int i = 0; i < 32; i++) {
        thSkB.update(key++);
    }
    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(), 0);
    // an intersection with no entries must survive more updates
    intersection.intersect(thSkA, dsum);
    result = intersection.getResult();
    Assert.assertEquals(result.getRetainedEntries(), 0);
    intersection.reset();
}
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 3 with UpdateSketch

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

the class CornerCaseTupleSetOperationsTest method emptyEstimation.

@Test
public void emptyEstimation() {
    IntegerSketch tupleA = getTupleSketch(SkType.EMPTY, 0, 0);
    IntegerSketch tupleB = getTupleSketch(SkType.ESTIMATION, LOWP_FLT, LT_LOWP_V);
    UpdateSketch thetaB = getThetaSketch(SkType.ESTIMATION, LOWP_FLT, LT_LOWP_V);
    final double expectedIntersectTheta = 1.0;
    final int expectedIntersectCount = 0;
    final boolean expectedIntersectEmpty = true;
    final double expectedAnotbTheta = 1.0;
    final int expectedAnotbCount = 0;
    final boolean expectedAnotbEmpty = true;
    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)

Example 4 with UpdateSketch

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

the class CornerCaseTupleSetOperationsTest method exactEstimation.

@Test
public void exactEstimation() {
    IntegerSketch tupleA = getTupleSketch(SkType.EXACT, 0, LT_LOWP_V);
    IntegerSketch tupleB = getTupleSketch(SkType.ESTIMATION, LOWP_FLT, LT_LOWP_V);
    UpdateSketch thetaB = getThetaSketch(SkType.ESTIMATION, LOWP_FLT, LT_LOWP_V);
    final double expectedIntersectTheta = LOWP_FLT;
    final int expectedIntersectCount = 1;
    final boolean expectedIntersectEmpty = false;
    final double expectedAnotbTheta = LOWP_FLT;
    final int expectedAnotbCount = 0;
    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)

Example 5 with UpdateSketch

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

the class CornerCaseTupleSetOperationsTest method getThetaSketch.

// NOTE: p and value arguments are used for every case
private static UpdateSketch getThetaSketch(SkType skType, float p, long updateKey) {
    UpdateSketchBuilder bldr = new UpdateSketchBuilder();
    bldr.setLogNominalEntries(4);
    bldr.setResizeFactor(ResizeFactor.X4);
    UpdateSketch sk;
    switch(skType) {
        case EMPTY:
            {
                // { 1.0,  0, T} p and value are not used
                sk = bldr.build();
                break;
            }
        case EXACT:
            {
                // { 1.0, >0, F} p is not used
                sk = bldr.build();
                sk.update(updateKey);
                break;
            }
        case ESTIMATION:
            {
                // {<1.0, >0, F}
                checkValidUpdate(p, updateKey);
                bldr.setP(p);
                sk = bldr.build();
                sk.update(updateKey);
                break;
            }
        case DEGENERATE:
            {
                // {<1.0,  0, F}
                checkInvalidUpdate(p, updateKey);
                bldr.setP(p);
                sk = bldr.build();
                sk.update(updateKey);
                break;
            }
        // should not happen
        default:
            {
                return null;
            }
    }
    return sk;
}
Also used : UpdateSketchBuilder(org.apache.datasketches.theta.UpdateSketchBuilder) UpdateSketch(org.apache.datasketches.theta.UpdateSketch)

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