Search in sources :

Example 1 with Union

use of com.yahoo.sketches.theta.Union 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().build(1 << 4, 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().build(1 << 4, 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(com.yahoo.sketches.theta.Sketch) Union(com.yahoo.sketches.theta.Union) Test(org.junit.Test) GroupByQueryRunnerTest(io.druid.query.groupby.GroupByQueryRunnerTest)

Example 2 with Union

use of com.yahoo.sketches.theta.Union in project druid by druid-io.

the class SketchBufferAggregator method getUnion.

//Note that this is not threadsafe and I don't think it needs to be
private Union getUnion(ByteBuffer buf, int position) {
    Union union = unions.get(position);
    if (union == null) {
        Memory mem = new MemoryRegion(nm, position, maxIntermediateSize);
        union = (Union) SetOperation.wrap(mem);
        unions.put(position, union);
    }
    return union;
}
Also used : Memory(com.yahoo.memory.Memory) NativeMemory(com.yahoo.memory.NativeMemory) MemoryRegion(com.yahoo.memory.MemoryRegion) Union(com.yahoo.sketches.theta.Union)

Example 3 with Union

use of com.yahoo.sketches.theta.Union 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().build(sketchSize, Family.UNION);
            for (Object o : holders) {
                ((SketchHolder) o).updateUnion(union);
            }
            return SketchHolder.of(union);
        case INTERSECT:
            Intersection intersection = (Intersection) SetOperation.builder().build(sketchSize, Family.INTERSECTION);
            for (Object o : holders) {
                intersection.update(((SketchHolder) o).getSketch());
            }
            return SketchHolder.of(intersection.getResult(false, null));
        case NOT:
            if (holders.length < 1) {
                throw new IllegalArgumentException("A-Not-B requires atleast 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().build(sketchSize, Family.A_NOT_B);
                anotb.update(result, ((SketchHolder) holders[i]).getSketch());
                result = anotb.getResult(false, null);
            }
            return SketchHolder.of(result);
        default:
            throw new IllegalArgumentException("Unknown sketch operation " + func);
    }
}
Also used : Intersection(com.yahoo.sketches.theta.Intersection) AnotB(com.yahoo.sketches.theta.AnotB) Sketch(com.yahoo.sketches.theta.Sketch) Union(com.yahoo.sketches.theta.Union)

Example 4 with Union

use of com.yahoo.sketches.theta.Union in project druid by druid-io.

the class SketchHolder method combine.

public static Object combine(Object o1, Object o2, int nomEntries) {
    SketchHolder holder1 = (SketchHolder) o1;
    SketchHolder holder2 = (SketchHolder) o2;
    if (holder1.obj instanceof Union) {
        Union union = (Union) holder1.obj;
        holder2.updateUnion(union);
        holder1.invalidateCache();
        return holder1;
    } else if (holder2.obj instanceof Union) {
        Union union = (Union) holder2.obj;
        holder1.updateUnion(union);
        holder2.invalidateCache();
        return holder2;
    } else {
        Union union = (Union) SetOperation.builder().build(nomEntries, Family.UNION);
        holder1.updateUnion(union);
        holder2.updateUnion(union);
        return SketchHolder.of(union);
    }
}
Also used : Union(com.yahoo.sketches.theta.Union)

Example 5 with Union

use of com.yahoo.sketches.theta.Union in project Gaffer by gchq.

the class UnionAggregatorTest method testEquals.

@Test
public void testEquals() {
    final Union sketch1 = Sketches.setOperationBuilder().buildUnion();
    sketch1.update("A");
    final UnionAggregator sketchAggregator1 = new UnionAggregator();
    sketchAggregator1.aggregate(new Union[] { sketch1 });
    final Union sketch2 = Sketches.setOperationBuilder().buildUnion();
    sketch2.update("A");
    final UnionAggregator sketchAggregator2 = new UnionAggregator();
    sketchAggregator2.aggregate(new Union[] { sketch2 });
    assertEquals(sketchAggregator1, sketchAggregator2);
    sketch2.update("B");
    sketchAggregator2.aggregate(new Union[] { sketch2 });
    assertNotEquals(sketchAggregator1, sketchAggregator2);
}
Also used : Union(com.yahoo.sketches.theta.Union) Test(org.junit.Test) AggregateFunctionTest(uk.gov.gchq.gaffer.function.AggregateFunctionTest)

Aggregations

Union (com.yahoo.sketches.theta.Union)17 Test (org.junit.Test)4 CompactSketch (com.yahoo.sketches.theta.CompactSketch)2 Intersection (com.yahoo.sketches.theta.Intersection)2 Sketch (com.yahoo.sketches.theta.Sketch)2 Test (org.junit.jupiter.api.Test)2 Entity (uk.gov.gchq.gaffer.data.element.Entity)2 AggregateFunctionTest (uk.gov.gchq.gaffer.function.AggregateFunctionTest)2 Memory (com.yahoo.memory.Memory)1 MemoryRegion (com.yahoo.memory.MemoryRegion)1 NativeMemory (com.yahoo.memory.NativeMemory)1 AnotB (com.yahoo.sketches.theta.AnotB)1 GroupByQueryRunnerTest (io.druid.query.groupby.GroupByQueryRunnerTest)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 DataBag (org.apache.pig.data.DataBag)1 Edge (uk.gov.gchq.gaffer.data.element.Edge)1 Element (uk.gov.gchq.gaffer.data.element.Element)1 DataGenerator13 (uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator13)1 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)1