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/04/26 22:25:16 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_r1178263145


##########
pinot-core/src/main/java/org/apache/pinot/core/common/ObjectSerDeUtils.java:
##########
@@ -918,6 +923,26 @@ public Sketch deserialize(ByteBuffer byteBuffer) {
     }
   };
 
+  public static final ObjectSerDe<KllSketch> KLL_SKETCH_SER_DE = new ObjectSerDe<KllSketch>() {

Review Comment:
   This should be written as KLL_DOUBLE_SKETCH_SER_DE



##########
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();

Review Comment:
   Is there any check that this argument is between 0 and 100?



##########
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:
   A similar check should be done on the K value. There's could be some memory implications from setting the K to be too high: https://datasketches.apache.org/docs/KLL/KLLAccuracyAndSize.html



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/customobject/SerializedKLL.java:
##########
@@ -0,0 +1,49 @@
+/**
+ * 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.segment.local.customobject;
+
+import java.util.Base64;
+import org.apache.datasketches.kll.KllDoublesSketch;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * Serialized and comparable version of KllDoublesSketch.
+ * Compares two sketches for a specific percentile value.
+ */
+public class SerializedKLL implements Comparable<SerializedKLL> {
+  private final double _quantile;
+  private final KllDoublesSketch _sketch;
+
+  public SerializedKLL(KllDoublesSketch sketch, double percentile) {
+    _sketch = sketch;
+    _quantile = percentile / 100.0;
+  }
+
+  @Override
+  public int compareTo(SerializedKLL other) {
+    checkArgument(other._quantile == _quantile, "Quantile numbers don't matchå");

Review Comment:
   Is this character intended?



##########
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/AggregationFunctionType.java:
##########
@@ -142,6 +146,10 @@ public static AggregationFunctionType getAggregationFunctionType(String function
         return PERCENTILETDIGEST;
       } else if (remainingFunctionName.equals("RAWTDIGEST") || remainingFunctionName.matches("RAWTDIGEST\\d+")) {
         return PERCENTILERAWTDIGEST;
+      } else if (remainingFunctionName.equals("KLL") || remainingFunctionName.matches("KLL\\d+")) {
+        return PERCENTILETDIGEST;

Review Comment:
   This seems like an error



-- 
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