Search in sources :

Example 1 with AnotB

use of com.yahoo.sketches.theta.AnotB 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 2 with AnotB

use of com.yahoo.sketches.theta.AnotB in project sketches-pig by DataSketches.

the class AexcludeB method exec.

// @formatter:off
/**
 * Top Level Exec Function.
 * <p>
 * This method accepts a <b>Sketch AnotB Input Tuple</b> and returns a
 * <b>Sketch Tuple</b>.
 * </p>
 *
 * <b>Sketch AnotB Input Tuple</b>
 * <ul>
 *   <li>Tuple: TUPLE (Must contain 2 fields): <br>
 *   Java data type: Pig DataType: Description
 *     <ul>
 *       <li>index 0: DataByteArray: BYTEARRAY: Sketch A</li>
 *       <li>index 1: DataByteArray: BYTEARRAY: Sketch B</li>
 *     </ul>
 *   </li>
 * </ul>
 *
 * <p>
 * Any other input tuple will throw an exception!
 * </p>
 *
 * <b>Sketch Tuple</b>
 * <ul>
 *   <li>Tuple: TUPLE (Contains exactly 1 field)
 *     <ul>
 *       <li>index 0: DataByteArray: BYTEARRAY = The serialization of a Sketch object.</li>
 *     </ul>
 *   </li>
 * </ul>
 *
 * @throws ExecException from Pig.
 */
// @formatter:on
// TOP LEVEL EXEC
@Override
public Tuple exec(final Tuple inputTuple) throws IOException {
    // The exec is a stateless function.  It operates on the input and returns a result.
    // It can only call static functions.
    final Object objA = extractFieldAtIndex(inputTuple, 0);
    Sketch sketchA = null;
    if (objA != null) {
        final DataByteArray dbaA = (DataByteArray) objA;
        final Memory srcMem = Memory.wrap(dbaA.get());
        sketchA = Sketch.wrap(srcMem, seed_);
    }
    final Object objB = extractFieldAtIndex(inputTuple, 1);
    Sketch sketchB = null;
    if (objB != null) {
        final DataByteArray dbaB = (DataByteArray) objB;
        final Memory srcMem = Memory.wrap(dbaB.get());
        sketchB = Sketch.wrap(srcMem, seed_);
    }
    final AnotB aNOTb = SetOperation.builder().setSeed(seed_).buildANotB();
    aNOTb.update(sketchA, sketchB);
    final CompactSketch compactSketch = aNOTb.getResult(true, null);
    return compactOrderedSketchToTuple(compactSketch);
}
Also used : CompactSketch(com.yahoo.sketches.theta.CompactSketch) Memory(com.yahoo.memory.Memory) AnotB(com.yahoo.sketches.theta.AnotB) CompactSketch(com.yahoo.sketches.theta.CompactSketch) Sketch(com.yahoo.sketches.theta.Sketch) DataByteArray(org.apache.pig.data.DataByteArray)

Aggregations

AnotB (com.yahoo.sketches.theta.AnotB)2 Sketch (com.yahoo.sketches.theta.Sketch)2 Memory (com.yahoo.memory.Memory)1 CompactSketch (com.yahoo.sketches.theta.CompactSketch)1 Intersection (com.yahoo.sketches.theta.Intersection)1 Union (com.yahoo.sketches.theta.Union)1 DataByteArray (org.apache.pig.data.DataByteArray)1