Search in sources :

Example 16 with HyperLogLog

use of com.facebook.airlift.stats.cardinality.HyperLogLog in project presto by prestodb.

the class ApproximateSetAggregation method input.

@InputFunction
@LiteralParameters("x")
public static void input(@AggregationState HyperLogLogState state, @SqlType("varchar(x)") Slice value, @SqlType(StandardTypes.DOUBLE) double maxStandardError) {
    HyperLogLog hll = HyperLogLogUtils.getOrCreateHyperLogLog(state, maxStandardError);
    state.addMemoryUsage(-hll.estimatedInMemorySize());
    hll.add(value);
    state.addMemoryUsage(hll.estimatedInMemorySize());
}
Also used : HyperLogLog(com.facebook.airlift.stats.cardinality.HyperLogLog) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) InputFunction(com.facebook.presto.spi.function.InputFunction)

Example 17 with HyperLogLog

use of com.facebook.airlift.stats.cardinality.HyperLogLog in project presto by prestodb.

the class TestHyperLogLogFunctions method getUniqueElements.

private List<HyperLogLog> getUniqueElements(int blockSize, long uniqueElements) {
    ImmutableList.Builder<HyperLogLog> builder = ImmutableList.builder();
    for (int j = 0; j < blockSize; j++) {
        // create a single HyperLogLog column
        HyperLogLog firstHll = HyperLogLog.newInstance(NUMBER_OF_BUCKETS);
        // populate column with even partitions of the unique elements
        for (long i = j * uniqueElements / blockSize; i < j * uniqueElements / blockSize + uniqueElements / blockSize; i++) {
            firstHll.add(i);
        }
        builder.add(firstHll);
    }
    return builder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) HyperLogLog(com.facebook.airlift.stats.cardinality.HyperLogLog)

Example 18 with HyperLogLog

use of com.facebook.airlift.stats.cardinality.HyperLogLog in project presto by prestodb.

the class TestHyperLogLogFunctions method getMergeProjection.

private String getMergeProjection(List<HyperLogLog> list) {
    String projection = "merge_hll(ARRAY[";
    Iterator<HyperLogLog> iterator = list.listIterator();
    ImmutableList.Builder<String> casts = ImmutableList.builder();
    for (HyperLogLog current : list) {
        Slice firstSerial = current.serialize();
        byte[] firstBytes = firstSerial.getBytes();
        String firstEncode = BaseEncoding.base16().lowerCase().encode(firstBytes);
        // create an iterable with all our cast statements
        casts.add("CAST(X'" + firstEncode + "' AS HyperLogLog)");
    }
    projection += Joiner.on(", ").join(casts.build());
    projection += "])";
    return projection;
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Slice(io.airlift.slice.Slice) HyperLogLog(com.facebook.airlift.stats.cardinality.HyperLogLog)

Example 19 with HyperLogLog

use of com.facebook.airlift.stats.cardinality.HyperLogLog in project presto by prestodb.

the class TestHyperLogLogFunctions method getCardinalityProjection.

private String getCardinalityProjection(List<HyperLogLog> list) {
    String projection = "cardinality(merge_hll(ARRAY[";
    Iterator<HyperLogLog> iterator = list.listIterator();
    ImmutableList.Builder<String> casts = ImmutableList.builder();
    for (HyperLogLog current : list) {
        Slice firstSerial = current.serialize();
        byte[] firstBytes = firstSerial.getBytes();
        String firstEncode = BaseEncoding.base16().lowerCase().encode(firstBytes);
        // create an iterable with all our cast statements
        casts.add("CAST(X'" + firstEncode + "' AS HyperLogLog)");
    }
    projection += Joiner.on(", ").join(casts.build());
    projection += "]))";
    return projection;
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Slice(io.airlift.slice.Slice) HyperLogLog(com.facebook.airlift.stats.cardinality.HyperLogLog)

Example 20 with HyperLogLog

use of com.facebook.airlift.stats.cardinality.HyperLogLog in project presto by prestodb.

the class HyperLogLogFunctions method scalarMerge.

@ScalarFunction("merge_hll")
@Description("merge the contents of an array of HyperLogLogs")
@SqlType(StandardTypes.HYPER_LOG_LOG)
@SqlNullable
public static Slice scalarMerge(@SqlType("array(HyperLogLog)") Block block) {
    if (block.getPositionCount() == 0) {
        return null;
    }
    HyperLogLog merged = null;
    int firstNonNullIndex = 0;
    while (firstNonNullIndex < block.getPositionCount() && block.isNull(firstNonNullIndex)) {
        firstNonNullIndex++;
    }
    if (firstNonNullIndex == block.getPositionCount()) {
        return null;
    }
    Slice initialSlice = block.getSlice(firstNonNullIndex, 0, block.getSliceLength(firstNonNullIndex));
    merged = HyperLogLog.newInstance(initialSlice);
    for (int i = firstNonNullIndex; i < block.getPositionCount(); i++) {
        Slice currentSlice = block.getSlice(i, 0, block.getSliceLength(i));
        if (!block.isNull(i)) {
            merged.mergeWith(HyperLogLog.newInstance(currentSlice));
        }
    }
    return merged.serialize();
}
Also used : Slice(io.airlift.slice.Slice) HyperLogLog(com.facebook.airlift.stats.cardinality.HyperLogLog) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

HyperLogLog (com.facebook.airlift.stats.cardinality.HyperLogLog)25 InputFunction (com.facebook.presto.spi.function.InputFunction)6 Slice (io.airlift.slice.Slice)6 SqlType (com.facebook.presto.spi.function.SqlType)4 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)3 TypeParameter (com.facebook.presto.spi.function.TypeParameter)3 ImmutableList (com.google.common.collect.ImmutableList)3 SliceInput (io.airlift.slice.SliceInput)2 Block (com.facebook.presto.common.block.Block)1 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)1 SqlVarbinary (com.facebook.presto.common.type.SqlVarbinary)1 PrestoException (com.facebook.presto.spi.PrestoException)1 Description (com.facebook.presto.spi.function.Description)1 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)1 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)1 SqlNullable (com.facebook.presto.spi.function.SqlNullable)1 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)1 SliceOutput (io.airlift.slice.SliceOutput)1 IntArrayList (it.unimi.dsi.fastutil.ints.IntArrayList)1 IntList (it.unimi.dsi.fastutil.ints.IntList)1