Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix approx_percentile to have constant pct for input rows #24600

Merged
merged 1 commit into from
Feb 21, 2025
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 @@ -92,16 +92,15 @@ private static void addInput(
checkAccuracy(accuracy);
digest = new QuantileDigest(accuracy);
state.setDigest(digest);
state.setPercentile(percentile);
}
else {
state.addMemoryUsage(-digest.estimatedInMemorySizeInBytes());
}

checkPercentile(percentile, state.getPercentile());
digest.add(value, weight);
state.addMemoryUsage(digest.estimatedInMemorySizeInBytes());

// use last percentile
state.setPercentile(percentile);
}

@CombineFunction
Expand Down Expand Up @@ -137,6 +136,11 @@ public static void output(@AggregationState DigestAndPercentileState state, Bloc
}
}

static void checkPercentile(double percentile, double statePercentile)
{
checkCondition(percentile == statePercentile, INVALID_FUNCTION_ARGUMENT, "Percentile argument must be constant for all input rows: %s vs. %s", percentile, statePercentile);
}

static void checkAccuracy(double accuracy)
{
checkCondition(0 < accuracy && accuracy < 1, INVALID_FUNCTION_ARGUMENT, "Percentile accuracy must be strictly between 0 and 1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.facebook.presto.common.type.Type;
import com.facebook.presto.metadata.FunctionAndTypeManager;
import com.facebook.presto.metadata.MetadataManager;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.function.JavaAggregationFunctionImplementation;
import com.google.common.collect.ImmutableList;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -495,6 +496,16 @@ public void testDoublePartialStep()
createRLEBlock(ImmutableList.of(0.5, 0.8), 3));
}

@Test(expectedExceptions = PrestoException.class, expectedExceptionsMessageRegExp = "Percentile argument must be constant for all input rows: 0.3 vs. 0.1")
public void testNonConstantPercentile()
{
assertAggregation(
DOUBLE_APPROXIMATE_PERCENTILE_AGGREGATION,
null,
createDoublesBlock(1.0, 2.0, 3.0),
createDoublesBlock(0.1, 0.3, 0.5));
}

private static JavaAggregationFunctionImplementation getAggregation(Type... arguments)
{
return FUNCTION_AND_TYPE_MANAGER.getJavaAggregateFunctionImplementation(FUNCTION_AND_TYPE_MANAGER.lookupFunction("approx_percentile", fromTypes(arguments)));
Expand Down
Loading