Copilot commented on code in PR #17411:
URL: https://github.com/apache/pinot/pull/17411#discussion_r2663884853
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/BaseDistinctCountSmartSketchAggregationFunction.java:
##########
@@ -670,6 +690,62 @@ protected static Set convertToValueSet(DictIdsWrapper
dictIdsWrapper) {
}
}
+ /**
+ * Aggregate a single dictionary ID for a group. If already a sketch, offer
directly.
+ * Otherwise add to bitmap and track as modified.
+ */
+ private void aggregateDictIdForGroup(GroupByResultHolder
groupByResultHolder, int groupKey, Dictionary dictionary,
+ int dictId, IntSet modifiedGroups) {
+ Object result = groupByResultHolder.getResult(groupKey);
+ if (!(result instanceof DictIdsWrapper)) {
+ // Already converted to sketch, offer directly
+ ((HyperLogLog) result).offer(dictionary.get(dictId));
Review Comment:
The code assumes any result that is not a `DictIdsWrapper` is a
`HyperLogLog`, but `result` could be null on first access for a group. This
will cause a NullPointerException. Add a null check and initialize the result
if it's null before casting.
##########
pinot-core/src/test/java/org/apache/pinot/core/query/aggregation/function/DistinctCountSmartHLLAggregationFunctionTest.java:
##########
@@ -0,0 +1,179 @@
+/**
+ * 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.query.aggregation.function;
+
+import com.clearspring.analytics.stream.cardinality.HyperLogLog;
+import java.util.List;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.spi.data.FieldSpec;
+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 DistinctCountSmartHLLAggregationFunctionTest {
+
+ @Test
+ public void testParameterParsing() {
+ // Test default values
+ DistinctCountSmartHLLAggregationFunction function = new
DistinctCountSmartHLLAggregationFunction(
+ List.of(ExpressionContext.forIdentifier("col")));
+ assertEquals(function.getThreshold(), 100_000);
+ assertEquals(function.getDictIdCardinalityThreshold(), 100_000);
+
+ // Test individual parameters
+ function = new DistinctCountSmartHLLAggregationFunction(
+ List.of(ExpressionContext.forIdentifier("col"),
+ ExpressionContext.forLiteral(FieldSpec.DataType.STRING,
"threshold=50000")));
+ assertEquals(function.getThreshold(), 50_000);
+ assertEquals(function.getDictIdCardinalityThreshold(), 100_000);
+
+ function = new DistinctCountSmartHLLAggregationFunction(
+ List.of(ExpressionContext.forIdentifier("col"),
+ ExpressionContext.forLiteral(FieldSpec.DataType.STRING,
"log2m=8")));
+ assertEquals(function.getThreshold(), 100_000);
+ assertEquals(function.getDictIdCardinalityThreshold(), 100_000);
+
+ function = new DistinctCountSmartHLLAggregationFunction(
+ List.of(ExpressionContext.forIdentifier("col"),
+ ExpressionContext.forLiteral(FieldSpec.DataType.STRING,
"dictThreshold=50000")));
+ assertEquals(function.getThreshold(), 100_000);
+ assertEquals(function.getDictIdCardinalityThreshold(), 50_000);
+
+ // Test disabled dictThreshold (non-positive values)
+ function = new DistinctCountSmartHLLAggregationFunction(
+ List.of(ExpressionContext.forIdentifier("col"),
+ ExpressionContext.forLiteral(FieldSpec.DataType.STRING,
"dictThreshold=-1")));
+ assertEquals(function.getDictIdCardinalityThreshold(), Integer.MAX_VALUE);
+
+ function = new DistinctCountSmartHLLAggregationFunction(
+ List.of(ExpressionContext.forIdentifier("col"),
+ ExpressionContext.forLiteral(FieldSpec.DataType.STRING,
"dictThreshold=0")));
+ assertEquals(function.getDictIdCardinalityThreshold(), Integer.MAX_VALUE);
+
+ // Test multiple parameters together
+ function = new DistinctCountSmartHLLAggregationFunction(
+ List.of(ExpressionContext.forIdentifier("col"),
+ ExpressionContext.forLiteral(FieldSpec.DataType.STRING,
"threshold=200000;log2m=10;dictThreshold=150000")));
+ assertEquals(function.getThreshold(), 200_000);
+ assertEquals(function.getDictIdCardinalityThreshold(), 150_000);
+
+ // Test parameter order independence
+ DistinctCountSmartHLLAggregationFunction function1 = new
DistinctCountSmartHLLAggregationFunction(
+ List.of(ExpressionContext.forIdentifier("col"),
+ ExpressionContext.forLiteral(FieldSpec.DataType.STRING,
"dictThreshold=50000;threshold=100000;log2m=8")));
+ DistinctCountSmartHLLAggregationFunction function2 = new
DistinctCountSmartHLLAggregationFunction(
+ List.of(ExpressionContext.forIdentifier("col"),
+ ExpressionContext.forLiteral(FieldSpec.DataType.STRING,
"log2m=8;dictThreshold=50000;threshold=100000")));
+ assertEquals(function1.getThreshold(), function2.getThreshold());
+ assertEquals(function1.getDictIdCardinalityThreshold(),
function2.getDictIdCardinalityThreshold());
+
+ // Test legacy parameter names
+ function = new DistinctCountSmartHLLAggregationFunction(
+ List.of(ExpressionContext.forIdentifier("col"),
+ ExpressionContext.forLiteral(FieldSpec.DataType.STRING,
"hllConversionThreshold=50000;hllLog2m=10")));
+ assertEquals(function.getThreshold(), 50_000);
+
+ // Test case-insensitive parameters
+ function = new DistinctCountSmartHLLAggregationFunction(
+ List.of(ExpressionContext.forIdentifier("col"),
+ ExpressionContext.forLiteral(FieldSpec.DataType.STRING,
"THRESHOLD=50000;LOG2M=8;DICTTHRESHOLD=100000")));
+ assertEquals(function.getThreshold(), 50_000);
+ assertEquals(function.getDictIdCardinalityThreshold(), 100_000);
+ }
+
+ @Test
+ public void testFunctionMetadata() {
+ DistinctCountSmartHLLAggregationFunction function = new
DistinctCountSmartHLLAggregationFunction(
+ List.of(ExpressionContext.forIdentifier("col")));
+
+ // Test function type
+ assertEquals(function.getType().getName(), "distinctCountSmartHLL");
+
+ // Test result types
+ assertEquals(function.getIntermediateResultColumnType(),
DataSchema.ColumnDataType.OBJECT);
+ assertEquals(function.getFinalResultColumnType(),
DataSchema.ColumnDataType.INT);
+
+ // Test result holder creation
+ assertNotNull(function.createAggregationResultHolder());
+ assertNotNull(function.createGroupByResultHolder(10, 100));
+ }
+
+ @Test
+ public void testHLLOperations() {
+ DistinctCountSmartHLLAggregationFunction function = new
DistinctCountSmartHLLAggregationFunction(
+ List.of(ExpressionContext.forIdentifier("col")));
+
+ // Test merge final results (should sum)
+ Integer finalResult = function.mergeFinalResult(100, 200);
+ assertEquals(finalResult.intValue(), 300);
+
+ // Test extract final result (HLL cardinality)
+ HyperLogLog hll = new HyperLogLog(12);
+ for (int i = 0; i < 1000; i++) {
+ hll.offer(i);
+ }
+ Long cardinality = Long.valueOf(function.extractFinalResult(hll));
Review Comment:
Replace `Long.valueOf()` with direct cast `(Long)` since
`extractFinalResult()` already returns a Long object. Using `Long.valueOf()` on
an object is unnecessary and could cause a NullPointerException if the result
is null.
```suggestion
Long cardinality = (Long) function.extractFinalResult(hll);
```
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/DistinctCountSmartHLLAggregationFunction.java:
##########
@@ -51,13 +51,17 @@
* - threshold: Threshold of the number of distinct values to trigger the
conversion, 100_000 by default. Non-positive
* value means never convert.
* - log2m: Log2m for the converted HyperLogLog, 12 by default.
- * Example of second argument: 'threshold=10;log2m=8'
+ * - dictThreshold: Threshold for dictionary-encoded columns to trigger early
conversion from RoaringBitmap to HLL
+ * during aggregation. 100_000 by default. Set to
Integer.MAX_VALUE to disable and convert only
+ * at finalization.
Review Comment:
The documentation should clarify that non-positive values (≤0) are also
treated as disabled and converted to Integer.MAX_VALUE, consistent with the
implementation in the Parameters class where values ≤0 are set to
Integer.MAX_VALUE.
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/BaseDistinctCountSmartSketchAggregationFunction.java:
##########
@@ -670,6 +690,62 @@ protected static Set convertToValueSet(DictIdsWrapper
dictIdsWrapper) {
}
}
+ /**
+ * Aggregate a single dictionary ID for a group. If already a sketch, offer
directly.
+ * Otherwise add to bitmap and track as modified.
+ */
+ private void aggregateDictIdForGroup(GroupByResultHolder
groupByResultHolder, int groupKey, Dictionary dictionary,
+ int dictId, IntSet modifiedGroups) {
+ Object result = groupByResultHolder.getResult(groupKey);
+ if (!(result instanceof DictIdsWrapper)) {
+ // Already converted to sketch, offer directly
+ ((HyperLogLog) result).offer(dictionary.get(dictId));
+ } else {
+ // Still using bitmap, add dict ID
+ getDictIdBitmap(groupByResultHolder, groupKey, dictionary).add(dictId);
+ modifiedGroups.add(groupKey);
+ }
+ }
+
+ /**
+ * Aggregate multiple dictionary IDs for a group. If already a sketch, offer
directly.
+ * Otherwise add to bitmap and track as modified.
+ */
+ private void aggregateDictIdsForGroup(GroupByResultHolder
groupByResultHolder, int groupKey, Dictionary dictionary,
+ int[] dictIds, IntSet modifiedGroups) {
+ Object result = groupByResultHolder.getResult(groupKey);
+ if (!(result instanceof DictIdsWrapper)) {
+ // Already converted to sketch, offer directly
+ for (int dictId : dictIds) {
+ ((HyperLogLog) result).offer(dictionary.get(dictId));
+ }
Review Comment:
The code assumes any result that is not a `DictIdsWrapper` is a
`HyperLogLog`, but `result` could be null on first access for a group. This
will cause a NullPointerException. Add a null check and initialize the result
if it's null before casting.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]