Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.facebook.presto.operator.scalar;

import com.facebook.airlift.stats.cardinality.HyperLogLog;
import com.facebook.airlift.stats.cardinality.PrivateLpcaSketch;
import com.facebook.presto.common.block.Block;
import com.facebook.presto.common.type.StandardTypes;
import com.facebook.presto.spi.function.Description;
Expand All @@ -24,6 +25,9 @@

import static com.facebook.presto.operator.aggregation.ApproximateSetAggregation.DEFAULT_STANDARD_ERROR;
import static com.facebook.presto.operator.aggregation.HyperLogLogUtils.standardErrorToBuckets;
import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
import static com.facebook.presto.spi.function.SqlFunctionVisibility.EXPERIMENTAL;
import static com.facebook.presto.util.Failures.checkCondition;

public final class HyperLogLogFunctions
{
Expand All @@ -37,6 +41,16 @@ public static long cardinality(@SqlType(StandardTypes.HYPER_LOG_LOG) Slice seria
return HyperLogLog.newInstance(serializedHll).cardinality();
}

@ScalarFunction(visibility = EXPERIMENTAL)
@Description("compute the noisy cardinality of a HyperLogLog instance")
@SqlType(StandardTypes.BIGINT)
public static long noisyCardinality(@SqlType(StandardTypes.HYPER_LOG_LOG) Slice serializedHll, @SqlType(StandardTypes.DOUBLE) double epsilon)
{
checkCondition(epsilon > 0, INVALID_FUNCTION_ARGUMENT, "Epsilon must be greater than 0");
PrivateLpcaSketch privacySketch = new PrivateLpcaSketch(HyperLogLog.newInstance(serializedHll), 0.1 * epsilon, 0.9 * epsilon);
return privacySketch.cardinality();
}

@ScalarFunction
@Description("an empty HyperLogLog instance")
@SqlType(StandardTypes.HYPER_LOG_LOG)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ public void testCardinalitySingleColumn()
functionAssertions.assertFunctionWithError(projection, BIGINT, uniqueElements, error);
}

@Test
public void testNoisyCardinalitySingleColumn()
{
int[] uniqueElementsCount = {0, 10000, 100000, 1000000};
for (int uniqueElements : uniqueElementsCount) {
double error = uniqueElements * 0.05;
HyperLogLog hll = HyperLogLog.newInstance(NUMBER_OF_BUCKETS);
for (int i = 0; i < uniqueElements; i++) {
hll.add(i);
}
String projection = getNoisyCardinalityProjection(hll);
functionAssertions.assertFunctionWithError(projection, BIGINT, uniqueElements, error);
}
}

@Test
public void testCardinalityTwoColumns()
{
Expand Down Expand Up @@ -206,4 +221,12 @@ private String getMergeProjection(List<HyperLogLog> list)

return projection;
}

private String getNoisyCardinalityProjection(HyperLogLog hll)
{
Slice serializedHll = hll.serialize();
byte[] hllBytes = serializedHll.getBytes();
String encodedHll = BaseEncoding.base16().lowerCase().encode(hllBytes);
return String.format("noisy_cardinality(CAST(X'%s' AS HyperLogLog), infinity())", encodedHll);
}
}