use of org.apache.datasketches.theta.Union in project druid by druid-io.
the class SketchBufferAggregatorHelper method get.
/**
* Helper for implementing {@link org.apache.druid.query.aggregation.BufferAggregator#get} and
* {@link org.apache.druid.query.aggregation.VectorAggregator#get}.
*/
public Object get(ByteBuffer buf, int position) {
Int2ObjectMap<Union> unionMap = unions.get(buf);
Union union = unionMap != null ? unionMap.get(position) : null;
if (union == null) {
return SketchHolder.EMPTY;
}
// to return the ordered sketch here
return SketchHolder.of(union.getResult(true, null));
}
use of org.apache.datasketches.theta.Union in project druid by druid-io.
the class SketchBufferAggregator method aggregate.
@Override
public void aggregate(ByteBuffer buf, int position) {
Object update = selector.getObject();
if (update == null) {
return;
}
Union union = helper.getOrCreateUnion(buf, position);
SketchAggregator.updateUnion(union, update);
}
use of org.apache.datasketches.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().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);
}
}
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 numRows, final int[] positions, @Nullable final int[] rows, final int positionOffset) {
final Object[] vector = objectSupplier.get();
for (int i = 0; i < numRows; i++) {
final Object o = vector[rows != null ? rows[i] : i];
if (o != null) {
final int position = positions[i] + positionOffset;
final Union union = helper.getOrCreateUnion(buf, position);
SketchAggregator.updateUnion(union, o);
}
}
}
use of org.apache.datasketches.theta.Union in project druid by druid-io.
the class SketchToStringPostAggregatorTest method testComparator.
@Test
public void testComparator() {
Union u1 = (Union) SetOperation.builder().setNominalEntries(10).build(Family.UNION);
u1.update(10L);
Union u2 = (Union) SetOperation.builder().setNominalEntries(10).build(Family.UNION);
u2.update(20L);
PostAggregator field1 = EasyMock.createMock(PostAggregator.class);
EasyMock.expect(field1.compute(EasyMock.anyObject(Map.class))).andReturn(SketchHolder.of(u1)).anyTimes();
PostAggregator field2 = EasyMock.createMock(PostAggregator.class);
EasyMock.expect(field2.compute(EasyMock.anyObject(Map.class))).andReturn(SketchHolder.of(u2)).anyTimes();
EasyMock.replay(field1, field2);
SketchToStringPostAggregator postAgg1 = new SketchToStringPostAggregator("summary", field1);
SketchToStringPostAggregator postAgg2 = new SketchToStringPostAggregator("summary", field2);
String summary1 = (String) postAgg1.compute(ImmutableMap.of());
String summary2 = (String) postAgg2.compute(ImmutableMap.of());
Assert.assertEquals(0, postAgg1.getComparator().compare(summary1, summary2));
}
Aggregations