Search in sources :

Example 6 with Union

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

the class SketchVectorAggregator method aggregate.

@Override
public void aggregate(final ByteBuffer buf, final int position, final int startRow, final int endRow) {
    final Union union = helper.getOrCreateUnion(buf, position);
    final Object[] vector = objectSupplier.get();
    for (int i = startRow; i < endRow; i++) {
        final Object o = vector[i];
        if (o != null) {
            SketchAggregator.updateUnion(union, o);
        }
    }
}
Also used : Union(org.apache.datasketches.theta.Union)

Example 7 with Union

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

the class SketchBufferAggregatorHelper method getOrCreateUnion.

/**
 * Returns a {@link Union} associated with a particular buffer location.
 *
 * The Union object will be cached in this helper until {@link #close()} is called.
 */
public Union getOrCreateUnion(ByteBuffer buf, int position) {
    Int2ObjectMap<Union> unionMap = unions.get(buf);
    Union union = unionMap != null ? unionMap.get(position) : null;
    if (union != null) {
        return union;
    }
    return createNewUnion(buf, position, true);
}
Also used : Union(org.apache.datasketches.theta.Union)

Example 8 with Union

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

the class SketchBufferAggregatorHelper method createNewUnion.

private Union createNewUnion(ByteBuffer buf, int position, boolean isWrapped) {
    WritableMemory mem = getMemory(buf).writableRegion(position, maxIntermediateSize);
    Union union = isWrapped ? (Union) SetOperation.wrap(mem) : (Union) SetOperation.builder().setNominalEntries(size).build(Family.UNION, mem);
    Int2ObjectMap<Union> unionMap = unions.get(buf);
    if (unionMap == null) {
        unionMap = new Int2ObjectOpenHashMap<>();
        unions.put(buf, unionMap);
    }
    unionMap.put(position, union);
    return union;
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) Union(org.apache.datasketches.theta.Union)

Example 9 with Union

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

the class SketchHolder method combine.

public static SketchHolder 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().setNominalEntries(nomEntries).build(Family.UNION);
        holder1.updateUnion(union);
        holder2.updateUnion(union);
        return SketchHolder.of(union);
    }
}
Also used : Union(org.apache.datasketches.theta.Union)

Example 10 with Union

use of org.apache.datasketches.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().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

Union (org.apache.datasketches.theta.Union)11 Test (org.junit.Test)3 Sketch (org.apache.datasketches.theta.Sketch)2 PostAggregator (org.apache.druid.query.aggregation.PostAggregator)2 FieldAccessPostAggregator (org.apache.druid.query.aggregation.post.FieldAccessPostAggregator)2 WritableMemory (org.apache.datasketches.memory.WritableMemory)1 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