You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by "chiquelo (via GitHub)" <gi...@apache.org> on 2023/05/02 19:21:19 UTC

[GitHub] [pinot] chiquelo commented on a diff in pull request #10643: Add PercentileKLL aggregation function

chiquelo commented on code in PR #10643:
URL: https://github.com/apache/pinot/pull/10643#discussion_r1182958934


##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/PercentileKLLAggregationFunction.java:
##########
@@ -0,0 +1,248 @@
+/**
+ * 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.google.common.base.Preconditions;
+import java.util.List;
+import java.util.Map;
+import org.apache.datasketches.kll.KllDoublesSketch;
+import org.apache.datasketches.memory.Memory;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema.ColumnDataType;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+import org.apache.pinot.spi.data.FieldSpec.DataType;
+
+/**
+ * <p>
+ *  {@code PercentileKLLAggregationFunction} provides an approximate percentile calculator using the KLL algorithm
+ *  from <a href="https://datasketches.apache.org/docs/KLL/KLLSketch.html">Apache DataSketches library</a>.
+ * </p>
+ * <p>
+ *  The interface is similar to plain 'Percentile' function except for the optional K value which determines
+ *  the size, hence the accuracy of the sketch.
+ * </p>
+ * <p><b>PERCENTILE_KLL(col, percentile, kValue)</b></p>
+ * <p>E.g.:</p>
+ * <ul>
+ *   <li><b>PERCENTILE_KLL(col, 90)</b></li>
+ *   <li><b>PERCENTILE_KLL(col, 99.9, 800)</b></li>
+ * </ul>
+ *
+ * <p>
+ *   There is a variation of the function (<b>PERCENTILE_RAW_KLL</b>) that returns the Base64 encoded
+ *   sketch object to be used externally.
+ * </p>
+ */
+public class PercentileKLLAggregationFunction
+    extends BaseSingleInputAggregationFunction<KllDoublesSketch, Comparable> {
+
+  protected final double _percentile;
+  protected int _kValue = 200; // size of the sketch. This is the default size used by DataSketches lib as well
+
+  public PercentileKLLAggregationFunction(List<ExpressionContext> arguments) {
+    super(arguments.get(0));
+
+    // Check that there are correct number of arguments
+    int numArguments = arguments.size();
+    Preconditions.checkArgument(numArguments == 2 || numArguments == 3,
+        "Expecting 2 or 3 arguments for PercentileKLL function: "
+            + "PERCENTILE_KLL(column, percentile, k=200");
+
+    _percentile = arguments.get(1).getLiteral().getDoubleValue();
+    if (numArguments == 3) {
+      _kValue = arguments.get(2).getLiteral().getIntValue();

Review Comment:
   Makes sense. Did not know about SketchesArgumentException. That should be good enough if bubbled up properly



-- 
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: commits-unsubscribe@pinot.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org