-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add PercentileTDigest support for MergeAndRollup aggregation #18088
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
Merged
Jackie-Jiang
merged 7 commits into
apache:master
from
justahuman1:svallabh/tdigest-merge-rollup
Apr 22, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9ac773b
feat: add PercentileTDigest support for MergeAndRollup aggregation
justahuman1 d556a16
fix: compilation errors and add empty byte[] guard to PercentileTDige…
justahuman1 3f5793c
Address review comments: add JavaDoc, factory tests, fix comments
justahuman1 7902fa6
Apply suggestion from @justahuman1
justahuman1 b60766b
Apply suggestion from @justahuman1
justahuman1 a7cd736
fix: replace illegal ImmutableMap import with Map.of
justahuman1 99aa269
address PR feedback: treat empty bytes as missing, improve error message
justahuman1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...java/org/apache/pinot/core/segment/processing/aggregator/PercentileTDigestAggregator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.pinot.core.segment.processing.aggregator; | ||
|
|
||
| import com.tdunning.math.stats.TDigest; | ||
| import java.util.Map; | ||
| import org.apache.pinot.core.common.ObjectSerDeUtils; | ||
| import org.apache.pinot.core.query.aggregation.function.PercentileTDigestAggregationFunction; | ||
| import org.apache.pinot.segment.spi.Constants; | ||
|
|
||
|
|
||
| /** | ||
| * Aggregator for merging serialized TDigest sketches during segment processing | ||
| * (e.g., MergeAndRollup). Handles both {@code PERCENTILETDIGEST} and | ||
| * {@code PERCENTILERAWTDIGEST} aggregation types | ||
| */ | ||
| public class PercentileTDigestAggregator implements ValueAggregator { | ||
|
|
||
| @Override | ||
| public Object aggregate(Object value1, Object value2, Map<String, String> functionParameters) { | ||
| byte[] bytes1 = (byte[]) value1; | ||
| byte[] bytes2 = (byte[]) value2; | ||
|
|
||
| // Treat empty byte arrays (default null value for BYTES columns) as missing values | ||
| if (bytes1.length == 0) { | ||
| return bytes2; | ||
| } else if (bytes2.length == 0) { | ||
| return bytes1; | ||
| } | ||
|
|
||
| int compression = getCompression(functionParameters); | ||
| TDigest first = ObjectSerDeUtils.TDIGEST_SER_DE.deserialize(bytes1); | ||
| TDigest second = ObjectSerDeUtils.TDIGEST_SER_DE.deserialize(bytes2); | ||
| TDigest merged = TDigest.createMergingDigest(compression); | ||
| merged.add(first); | ||
| merged.add(second); | ||
| return ObjectSerDeUtils.TDIGEST_SER_DE.serialize(merged); | ||
| } | ||
|
|
||
| private int getCompression(Map<String, String> functionParameters) { | ||
| String compressionParam = functionParameters.get(Constants.PERCENTILETDIGEST_COMPRESSION_FACTOR_KEY); | ||
| return compressionParam != null | ||
| ? Integer.parseInt(compressionParam) | ||
| : PercentileTDigestAggregationFunction.DEFAULT_TDIGEST_COMPRESSION; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
.../org/apache/pinot/core/segment/processing/aggregator/PercentileTDigestAggregatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.pinot.core.segment.processing.aggregator; | ||
|
|
||
| import com.tdunning.math.stats.TDigest; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import org.apache.pinot.core.common.ObjectSerDeUtils; | ||
| import org.apache.pinot.segment.spi.AggregationFunctionType; | ||
| import org.apache.pinot.segment.spi.Constants; | ||
| import org.apache.pinot.spi.data.FieldSpec.DataType; | ||
| import org.testng.annotations.BeforeMethod; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertNotNull; | ||
| import static org.testng.Assert.assertTrue; | ||
|
|
||
|
|
||
| public class PercentileTDigestAggregatorTest { | ||
|
|
||
| private PercentileTDigestAggregator _aggregator; | ||
|
|
||
| @BeforeMethod | ||
| public void setUp() { | ||
| _aggregator = new PercentileTDigestAggregator(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAggregateWithDefaultCompression() { | ||
| TDigest first = TDigest.createMergingDigest(100); | ||
| for (int i = 0; i < 100; i++) { | ||
| first.add(i); | ||
| } | ||
| TDigest second = TDigest.createMergingDigest(100); | ||
| for (int i = 100; i < 200; i++) { | ||
| second.add(i); | ||
| } | ||
|
|
||
| byte[] value1 = ObjectSerDeUtils.TDIGEST_SER_DE.serialize(first); | ||
| byte[] value2 = ObjectSerDeUtils.TDIGEST_SER_DE.serialize(second); | ||
|
|
||
| Map<String, String> functionParameters = new HashMap<>(); | ||
| byte[] result = (byte[]) _aggregator.aggregate(value1, value2, functionParameters); | ||
|
|
||
| TDigest resultDigest = ObjectSerDeUtils.TDIGEST_SER_DE.deserialize(result); | ||
| assertNotNull(resultDigest); | ||
| assertEquals(resultDigest.size(), 200); | ||
| assertEquals(resultDigest.quantile(0.5), 99.5, 1); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAggregateWithCustomCompression() { | ||
| TDigest first = TDigest.createMergingDigest(100); | ||
| for (int i = 0; i < 50; i++) { | ||
| first.add(i); | ||
| } | ||
| TDigest second = TDigest.createMergingDigest(100); | ||
| for (int i = 50; i < 100; i++) { | ||
| second.add(i); | ||
| } | ||
|
|
||
| byte[] value1 = ObjectSerDeUtils.TDIGEST_SER_DE.serialize(first); | ||
| byte[] value2 = ObjectSerDeUtils.TDIGEST_SER_DE.serialize(second); | ||
|
|
||
| Map<String, String> functionParameters = new HashMap<>(); | ||
| functionParameters.put(Constants.PERCENTILETDIGEST_COMPRESSION_FACTOR_KEY, "200"); | ||
|
|
||
| byte[] result = (byte[]) _aggregator.aggregate(value1, value2, functionParameters); | ||
|
|
||
| TDigest resultDigest = ObjectSerDeUtils.TDIGEST_SER_DE.deserialize(result); | ||
| assertNotNull(resultDigest); | ||
| assertEquals(resultDigest.size(), 100); | ||
| assertEquals(resultDigest.quantile(0.5), 49.5, 1); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAggregateWithBothEmptyBytes() { | ||
| byte[] empty1 = new byte[0]; | ||
| byte[] empty2 = new byte[0]; | ||
|
|
||
| Map<String, String> functionParameters = new HashMap<>(); | ||
| byte[] result = (byte[]) _aggregator.aggregate(empty1, empty2, functionParameters); | ||
|
|
||
| // Both empty — treat as missing, return empty bytes | ||
| assertEquals(result.length, 0); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAggregateWithFirstEmptyBytes() { | ||
| TDigest second = TDigest.createMergingDigest(100); | ||
| for (int i = 0; i < 50; i++) { | ||
| second.add(i); | ||
| } | ||
| byte[] empty = new byte[0]; | ||
| byte[] value2 = ObjectSerDeUtils.TDIGEST_SER_DE.serialize(second); | ||
|
|
||
| Map<String, String> functionParameters = new HashMap<>(); | ||
| byte[] result = (byte[]) _aggregator.aggregate(empty, value2, functionParameters); | ||
|
|
||
| // Should return the non-empty side as-is | ||
| assertEquals(result, value2); | ||
| TDigest resultDigest = ObjectSerDeUtils.TDIGEST_SER_DE.deserialize(result); | ||
| assertEquals(resultDigest.size(), 50); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAggregateWithSecondEmptyBytes() { | ||
| TDigest first = TDigest.createMergingDigest(100); | ||
| for (int i = 0; i < 50; i++) { | ||
| first.add(i); | ||
| } | ||
| byte[] value1 = ObjectSerDeUtils.TDIGEST_SER_DE.serialize(first); | ||
| byte[] empty = new byte[0]; | ||
|
|
||
| Map<String, String> functionParameters = new HashMap<>(); | ||
| byte[] result = (byte[]) _aggregator.aggregate(value1, empty, functionParameters); | ||
|
|
||
| // Should return the non-empty side as-is | ||
| assertEquals(result, value1); | ||
| TDigest resultDigest = ObjectSerDeUtils.TDIGEST_SER_DE.deserialize(result); | ||
| assertEquals(resultDigest.size(), 50); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFactoryReturnsAggregatorForNonRawType() { | ||
| ValueAggregator aggregator = ValueAggregatorFactory.getValueAggregator( | ||
| AggregationFunctionType.PERCENTILETDIGEST, DataType.BYTES); | ||
| assertTrue(aggregator instanceof PercentileTDigestAggregator); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFactoryReturnsAggregatorForRawType() { | ||
| ValueAggregator aggregator = ValueAggregatorFactory.getValueAggregator( | ||
| AggregationFunctionType.PERCENTILERAWTDIGEST, DataType.BYTES); | ||
| assertTrue(aggregator instanceof PercentileTDigestAggregator); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.