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 @@ -48,7 +48,9 @@
import static com.facebook.presto.sql.relational.Expressions.call;
import static com.facebook.presto.sql.relational.Expressions.constant;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.util.Objects.requireNonNull;
import static java.util.function.Function.identity;

/**
* For multiple approx_percentile() function calls on the same column with different percentile arguments, combine them to one call on an array of percentile arguments.
Expand Down Expand Up @@ -224,9 +226,13 @@ public Result apply(AggregationNode aggregationNode, Captures captures, Context
x -> x.getCall().getDisplayName().equals(APPROX_PERCENTILE) && !(x.getCall().getType() instanceof ArrayType)
).collect(Collectors.toList());

// Remove aggregations which occurs more than once, as we assumes that there are no duplicates in later stage
Map<AggregationNode.Aggregation, Long> aggregationOccurrences = approxPercentile.stream().collect(Collectors.groupingBy(identity(), Collectors.counting()));
ImmutableList<AggregationNode.Aggregation> candidateApproxPercentile = approxPercentile.stream().filter(x -> aggregationOccurrences.get(x) == 1).collect(toImmutableList());

// Group the aggregations on the same column and have the same function handle
Map<RowExpression, Map<FunctionHandle, List<AggregationNode.Aggregation>>> sameColumnHandle =
approxPercentile.stream().collect(Collectors.groupingBy(x -> x.getCall().getArguments().get(0), LinkedHashMap::new,
candidateApproxPercentile.stream().collect(Collectors.groupingBy(x -> x.getCall().getArguments().get(0), LinkedHashMap::new,
Collectors.groupingBy(x -> x.getFunctionHandle(), LinkedHashMap::new, Collectors.toList())));

// Each list contains the aggregations which can be combined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,19 @@ public void testArrayLimit()
}
})).doesNotFire();
}

@Test
public void testWithDuplicate()
{
tester().assertThat(new CombineApproxPercentileFunctions(getMetadata().getFunctionAndTypeManager()))
.on(p -> p.aggregation(af -> {
p.variable("col", BIGINT);
af.globalGrouping()
.addAggregation(p.variable("approx_percentile_1"), p.rowExpression("approx_percentile(col, 0.1)", ParsingOptions.DecimalLiteralTreatment.AS_DOUBLE))
.addAggregation(p.variable("approx_percentile_2"), p.rowExpression("approx_percentile(col, 0.1)", ParsingOptions.DecimalLiteralTreatment.AS_DOUBLE))
.addAggregation(p.variable("approx_percentile_3"), p.rowExpression("approx_percentile(col, 0.2)", ParsingOptions.DecimalLiteralTreatment.AS_DOUBLE))
.addAggregation(p.variable("approx_percentile_4"), p.rowExpression("approx_percentile(col, 0.2)", ParsingOptions.DecimalLiteralTreatment.AS_DOUBLE))
.source(p.values(p.variable("col")));
})).doesNotFire();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6156,7 +6156,9 @@ public void testApproxPercentileMerged()
" approx_percentile(totalprice, 2, 0.5)," +
" approx_percentile(totalprice, 2, 0.8)," +
" approx_percentile(totalprice, 2, 0.4, 0.001)," +
" approx_percentile(totalprice, 2, 0.7, 0.001)\n" +
" approx_percentile(totalprice, 2, 0.7, 0.001)," +
" approx_percentile(orderkey, 0.9)," +
" approx_percentile(orderkey, 0.8+0.1)\n" +
"FROM orders\n" +
"GROUP BY orderstatus");

Expand All @@ -6172,6 +6174,8 @@ public void testApproxPercentileMerged()
Double totalPrice08Weighted = (Double) row.getField(8);
Double totalPrice04WeightedAccuracy = (Double) row.getField(9);
Double totalPrice07WeightedAccuracy = (Double) row.getField(10);
Long orderKey09v1 = ((Number) row.getField(11)).longValue();
Long orderKey09v2 = ((Number) row.getField(12)).longValue();

List<Long> orderKeys = Ordering.natural().sortedCopy(orderKeyByStatus.get(status));
List<Double> totalPrices = Ordering.natural().sortedCopy(totalPriceByStatus.get(status));
Expand Down Expand Up @@ -6206,6 +6210,12 @@ public void testApproxPercentileMerged()

assertTrue(totalPrice07WeightedAccuracy >= totalPrices.get((int) (0.69 * totalPrices.size())));
assertTrue(totalPrice07WeightedAccuracy <= totalPrices.get((int) (0.71 * totalPrices.size())));

assertTrue(orderKey09v1 >= orderKeys.get((int) (0.89 * orderKeys.size())));
assertTrue(orderKey09v1 <= orderKeys.get((int) (0.91 * orderKeys.size())));

assertTrue(orderKey09v2 >= orderKeys.get((int) (0.89 * orderKeys.size())));
assertTrue(orderKey09v2 <= orderKeys.get((int) (0.91 * orderKeys.size())));
}
}

Expand Down