From 78ed4c0a666c463cfa64175515cd1764c5e3933d Mon Sep 17 00:00:00 2001 From: Hans Van Akelyen Date: Mon, 16 Nov 2026 15:57:44 +0100 Subject: [PATCH] Group By: a nearest-rank percentile of a group without values is null, fixes #9512 --- .../pipeline/transforms/groupby/GroupBy.java | 4 +- .../transforms/groupby/GroupByTest.java | 44 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/plugins/transforms/groupby/src/main/java/org/apache/hop/pipeline/transforms/groupby/GroupBy.java b/plugins/transforms/groupby/src/main/java/org/apache/hop/pipeline/transforms/groupby/GroupBy.java index 59ea72612c7..21f23aea347 100644 --- a/plugins/transforms/groupby/src/main/java/org/apache/hop/pipeline/transforms/groupby/GroupBy.java +++ b/plugins/transforms/groupby/src/main/java/org/apache/hop/pipeline/transforms/groupby/GroupBy.java @@ -928,7 +928,9 @@ Object[] getAggregateResult() throws HopValueException { Double[] latencies = new Double[latenciesList.size()]; latencies = latenciesList.toArray(latencies); int index = (int) Math.ceil((percentileValue / 100) * latencies.length); - ag = latencies[index - 1]; + // No values in the group (all null, or no rows at all): no percentile. The 0th + // percentile is the smallest value. + ag = latencies.length == 0 ? null : latencies[Math.max(index, 1) - 1]; break; case Aggregation.TYPE_GROUP_COUNT_ANY, Aggregation.TYPE_GROUP_COUNT_ALL: ag = data.counts[i]; diff --git a/plugins/transforms/groupby/src/test/java/org/apache/hop/pipeline/transforms/groupby/GroupByTest.java b/plugins/transforms/groupby/src/test/java/org/apache/hop/pipeline/transforms/groupby/GroupByTest.java index c5c929b4067..cf662389118 100644 --- a/plugins/transforms/groupby/src/test/java/org/apache/hop/pipeline/transforms/groupby/GroupByTest.java +++ b/plugins/transforms/groupby/src/test/java/org/apache/hop/pipeline/transforms/groupby/GroupByTest.java @@ -20,6 +20,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @@ -269,6 +270,49 @@ void calcAggregateUpdatesSumAndCount() throws Exception { assertEquals(2L, ((Number) result[1]).longValue()); } + @Test + void percentileNearestRankOfAGroupWithoutValuesIsNull() throws Exception { + data.inputRowMeta = inputRowMeta; + data.subjectnrs = new int[] {1, 1}; + data.counts = new long[2]; + data.mean = new double[2]; + data.previousSums = new Object[0]; + data.previousAvgSum = new Object[0]; + data.previousAvgCount = new long[0]; + data.movingAvgWindows = new ArrayDeque[2]; + + meta.getAggregations().clear(); + meta.getAggregations() + .add( + new Aggregation( + "p75", + "amount", + Aggregation.getTypeDescLongFromCode(Aggregation.TYPE_GROUP_PERCENTILE_NEAREST_RANK), + "75")); + meta.getAggregations() + .add( + new Aggregation( + "p0", + "amount", + Aggregation.getTypeDescLongFromCode(Aggregation.TYPE_GROUP_PERCENTILE_NEAREST_RANK), + "0")); + + // Only null amounts: no values to rank + groupBy.newAggregate(new Object[] {"A", null}); + groupBy.calcAggregate(new Object[] {"A", null}); + Object[] result = groupBy.getAggregateResult(); + assertNull(result[0]); + assertNull(result[1]); + + // A 0th percentile is the smallest value, not an index before the first one + groupBy.newAggregate(new Object[] {"A", 4.0}); + groupBy.calcAggregate(new Object[] {"A", 4.0}); + groupBy.calcAggregate(new Object[] {"A", 2.0}); + result = groupBy.getAggregateResult(); + assertEquals(4.0, (Double) result[0], 1e-9); + assertEquals(2.0, (Double) result[1], 1e-9); + } + @Test void batchCompleteFlushesCurrentGroup() throws Exception { QueueRowSet input = new QueueRowSet();