chenboat commented on code in PR #11098:
URL: https://github.com/apache/pinot/pull/11098#discussion_r1281307520


##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/FrequentLongsSketchAggregationFunction.java:
##########
@@ -0,0 +1,251 @@
+/**
+ * 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.frequencies.LongsSketch;
+import org.apache.datasketches.memory.Memory;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+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.local.customobject.SerializedFrequentLongsSketch;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+import org.apache.pinot.spi.data.FieldSpec;
+
+
+/**
+ * <p>
+ *  {@code FrequentLongsSketchAggregationFunction} provides an approximate 
FrequentItems aggregation function based on
+ *  <a 
href="https://datasketches.apache.org/docs/Frequency/FrequentItemsOverview.html";>Apache
 DataSketches library</a>.
+ *  It is memory efficient compared to exact counting.
+ * </p>
+ * <p>
+ *   The function takes an INT or LONG column as input and returns a Base64 
encoded sketch object which can be
+ *   deserialized and used to estimate the frequency of items in the dataset 
(how many times they appear).
+ * </p>
+ * <p><b>FREQUENT_STRINGS_SKETCH(col, maxMapSize=256)</b></p>
+ * <p>E.g.:</p>
+ * <ul>
+ *   <li><b>FREQUENT_LONGS_SKETCH(col)</b></li>
+ *   <li><b>FREQUENT_LONGS_SKETCH(col, 1024)</b></li>
+ * </ul>
+ *
+ * <p>
+ *   If the column type is BYTES, the aggregation function will assume it is a 
serialized FrequentItems data sketch
+ *   of type `LongsSketch`and will attempt to deserialize it for merging with 
other sketch objects.
+ * </p>
+ *
+ * <p>
+ *   There is a variation of the function (<b>FREQUENT_STRINGS_SKETCH</b>) 
which accepts STRING type input columns.
+ * </p>
+ */
+public class FrequentLongsSketchAggregationFunction
+    extends BaseSingleInputAggregationFunction<LongsSketch, Comparable<?>> {
+  protected static final int DEFAULT_MAX_MAP_SIZE = 256;
+
+  protected int _maxMapSize;
+
+  public FrequentLongsSketchAggregationFunction(List<ExpressionContext> 
arguments) {
+    super(arguments.get(0));
+    int numArguments = arguments.size();
+    Preconditions.checkArgument(numArguments == 1 || numArguments == 2,
+        "Expecting 1 or 2 arguments for FrequentLongsSketch function: 
FREQUENTITEMSSKETCH(column, maxMapSize");
+    _maxMapSize = numArguments == 2 ? 
arguments.get(1).getLiteral().getIntValue() : DEFAULT_MAX_MAP_SIZE;
+  }
+
+  @Override
+  public AggregationFunctionType getType() {
+    return AggregationFunctionType.FREQUENTLONGSSKETCH;
+  }
+
+  @Override
+  public AggregationResultHolder createAggregationResultHolder() {
+    return new ObjectAggregationResultHolder();
+  }
+
+  @Override
+  public GroupByResultHolder createGroupByResultHolder(int initialCapacity, 
int maxCapacity) {
+    return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+  }
+
+  @Override
+  public void aggregate(int length, AggregationResultHolder 
aggregationResultHolder,
+      Map<ExpressionContext, BlockValSet> blockValSetMap) {
+    BlockValSet valueSet = blockValSetMap.get(_expression);
+    FieldSpec.DataType valueType = valueSet.getValueType();
+
+    LongsSketch sketch = getOrCreateSketch(aggregationResultHolder);
+
+    if (valueType == FieldSpec.DataType.BYTES) {

Review Comment:
   NIT: can we use switch-case here?



-- 
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-unsubscr...@pinot.apache.org

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


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

Reply via email to