Search in sources :

Example 1 with Sketch

use of org.apache.datasketches.theta.Sketch 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 Sketch

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

the class SketchHolder method getEstimateWithErrorBounds.

public SketchEstimateWithErrorBounds getEstimateWithErrorBounds(int errorBoundsStdDev) {
    Sketch sketch = getSketch();
    SketchEstimateWithErrorBounds result = new SketchEstimateWithErrorBounds(getEstimate(), sketch.getUpperBound(errorBoundsStdDev), sketch.getLowerBound(errorBoundsStdDev), errorBoundsStdDev);
    return result;
}
Also used : Sketch(org.apache.datasketches.theta.Sketch)

Example 3 with Sketch

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

the class SketchAggregationTest method testSketchAggregatorFactoryComparator.

@Test
public void testSketchAggregatorFactoryComparator() {
    Comparator<Object> comparator = SketchHolder.COMPARATOR;
    Assert.assertEquals(0, comparator.compare(null, null));
    Union union1 = (Union) SetOperation.builder().setNominalEntries(1 << 4).build(Family.UNION);
    union1.update("a");
    union1.update("b");
    Sketch sketch1 = union1.getResult();
    Assert.assertEquals(-1, comparator.compare(null, SketchHolder.of(sketch1)));
    Assert.assertEquals(1, comparator.compare(SketchHolder.of(sketch1), null));
    Union union2 = (Union) SetOperation.builder().setNominalEntries(1 << 4).build(Family.UNION);
    union2.update("a");
    union2.update("b");
    union2.update("c");
    Sketch sketch2 = union2.getResult();
    Assert.assertEquals(-1, comparator.compare(SketchHolder.of(sketch1), SketchHolder.of(sketch2)));
    Assert.assertEquals(-1, comparator.compare(SketchHolder.of(sketch1), SketchHolder.of(union2)));
    Assert.assertEquals(1, comparator.compare(SketchHolder.of(sketch2), SketchHolder.of(sketch1)));
    Assert.assertEquals(1, comparator.compare(SketchHolder.of(sketch2), SketchHolder.of(union1)));
    Assert.assertEquals(1, comparator.compare(SketchHolder.of(union2), SketchHolder.of(union1)));
    Assert.assertEquals(1, comparator.compare(SketchHolder.of(union2), SketchHolder.of(sketch1)));
}
Also used : Sketch(org.apache.datasketches.theta.Sketch) UpdateSketch(org.apache.datasketches.theta.UpdateSketch) Union(org.apache.datasketches.theta.Union) GroupByQueryRunnerTest(org.apache.druid.query.groupby.GroupByQueryRunnerTest) Test(org.junit.Test)

Aggregations

Sketch (org.apache.datasketches.theta.Sketch)3 Union (org.apache.datasketches.theta.Union)2 AnotB (org.apache.datasketches.theta.AnotB)1 Intersection (org.apache.datasketches.theta.Intersection)1 UpdateSketch (org.apache.datasketches.theta.UpdateSketch)1 GroupByQueryRunnerTest (org.apache.druid.query.groupby.GroupByQueryRunnerTest)1 Test (org.junit.Test)1