Search in sources :

Example 1 with Intersection

use of org.apache.datasketches.theta.Intersection in project druid by druid-io.

the class SketchHolder method sketchSetOperation.

public static SketchHolder sketchSetOperation(Func func, int sketchSize, Object... holders) {
    // the final stages of query processing, ordered sketch would be of no use.
    switch(func) {
        case UNION:
            Union union = (Union) SetOperation.builder().setNominalEntries(sketchSize).build(Family.UNION);
            for (Object o : holders) {
                ((SketchHolder) o).updateUnion(union);
            }
            return SketchHolder.of(union);
        case INTERSECT:
            Intersection intersection = (Intersection) SetOperation.builder().setNominalEntries(sketchSize).build(Family.INTERSECTION);
            for (Object o : holders) {
                intersection.intersect(((SketchHolder) o).getSketch());
            }
            return SketchHolder.of(intersection.getResult(false, null));
        case NOT:
            if (holders.length < 1) {
                throw new IllegalArgumentException("A-Not-B requires at least 1 sketch");
            }
            if (holders.length == 1) {
                return (SketchHolder) holders[0];
            }
            Sketch result = ((SketchHolder) holders[0]).getSketch();
            for (int i = 1; i < holders.length; i++) {
                AnotB anotb = (AnotB) SetOperation.builder().setNominalEntries(sketchSize).build(Family.A_NOT_B);
                result = anotb.aNotB(result, ((SketchHolder) holders[i]).getSketch());
            }
            return SketchHolder.of(result);
        default:
            throw new IllegalArgumentException("Unknown sketch operation " + func);
    }
}
Also used : Intersection(org.apache.datasketches.theta.Intersection) AnotB(org.apache.datasketches.theta.AnotB) Sketch(org.apache.datasketches.theta.Sketch) Union(org.apache.datasketches.theta.Union)

Example 2 with Intersection

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

the class BoundsOnRatiosInThetaSketchedSetsTest method checkNormalReturns.

@Test
public void checkNormalReturns() {
    // 4K
    final UpdateSketch skA = Sketches.updateSketchBuilder().build();
    final UpdateSketch skC = Sketches.updateSketchBuilder().build();
    final int uA = 10000;
    final int uC = 100000;
    for (int i = 0; i < uA; i++) {
        skA.update(i);
    }
    for (int i = 0; i < uC; i++) {
        skC.update(i + (uA / 2));
    }
    final Intersection inter = Sketches.setOperationBuilder().buildIntersection();
    inter.intersect(skA);
    inter.intersect(skC);
    final CompactSketch skB = inter.getResult();
    double est = BoundsOnRatiosInThetaSketchedSets.getEstimateOfBoverA(skA, skB);
    double lb = BoundsOnRatiosInThetaSketchedSets.getLowerBoundForBoverA(skA, skB);
    double ub = BoundsOnRatiosInThetaSketchedSets.getUpperBoundForBoverA(skA, skB);
    assertTrue(ub > est);
    assertTrue(est > lb);
    assertEquals(est, 0.5, .03);
    println("ub : " + ub);
    println("est: " + est);
    println("lb : " + lb);
    // skA is now empty
    skA.reset();
    est = BoundsOnRatiosInThetaSketchedSets.getEstimateOfBoverA(skA, skB);
    lb = BoundsOnRatiosInThetaSketchedSets.getLowerBoundForBoverA(skA, skB);
    ub = BoundsOnRatiosInThetaSketchedSets.getUpperBoundForBoverA(skA, skB);
    println("ub : " + ub);
    println("est: " + est);
    println("lb : " + lb);
    // Now both are empty
    skC.reset();
    est = BoundsOnRatiosInThetaSketchedSets.getEstimateOfBoverA(skA, skC);
    lb = BoundsOnRatiosInThetaSketchedSets.getLowerBoundForBoverA(skA, skC);
    ub = BoundsOnRatiosInThetaSketchedSets.getUpperBoundForBoverA(skA, skC);
    println("ub : " + ub);
    println("est: " + est);
    println("lb : " + lb);
}
Also used : Intersection(org.apache.datasketches.theta.Intersection) CompactSketch(org.apache.datasketches.theta.CompactSketch) UpdateSketch(org.apache.datasketches.theta.UpdateSketch) Test(org.testng.annotations.Test)

Aggregations

Intersection (org.apache.datasketches.theta.Intersection)2 AnotB (org.apache.datasketches.theta.AnotB)1 CompactSketch (org.apache.datasketches.theta.CompactSketch)1 Sketch (org.apache.datasketches.theta.Sketch)1 Union (org.apache.datasketches.theta.Union)1 UpdateSketch (org.apache.datasketches.theta.UpdateSketch)1 Test (org.testng.annotations.Test)1