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

[GitHub] [pinot] mayankshriv commented on a diff in pull request #10288: Add Theta Sketch Aggregation for StarTree Indexes

mayankshriv commented on code in PR #10288:
URL: https://github.com/apache/pinot/pull/10288#discussion_r1108948702


##########
pinot-spi/src/main/java/org/apache/pinot/spi/utils/CommonConstants.java:
##########
@@ -91,6 +91,10 @@ public static class Helix {
     public static final String DEFAULT_HYPERLOGLOG_LOG2M_KEY = "default.hyperloglog.log2m";
     public static final int DEFAULT_HYPERLOGLOG_LOG2M = 8;
 
+    // 2 to the power of 16, for tradeoffs see datasketches library documentation:
+    // https://datasketches.apache.org/docs/Theta/ThetaErrorTable.html
+    public static final int DEFAULT_THETA_SKETCH_NOMINAL_ENTRIES = 65536;

Review Comment:
   We might want to make this configurable to allow for accuracy vs storage tradeoff.



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/aggregator/DistinctCountThetaSketchValueAggregator.java:
##########
@@ -0,0 +1,117 @@
+/**
+ * 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.aggregator;
+
+import org.apache.datasketches.theta.SingleItemSketch;
+import org.apache.datasketches.theta.Sketch;
+import org.apache.datasketches.theta.Union;
+import org.apache.pinot.segment.local.utils.CustomSerDeUtils;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+import org.apache.pinot.spi.data.FieldSpec.DataType;
+import org.apache.pinot.spi.utils.CommonConstants;
+
+
+public class DistinctCountThetaSketchValueAggregator implements ValueAggregator<Object, Sketch> {
+  public static final DataType AGGREGATED_VALUE_TYPE = DataType.BYTES;
+
+  @Override
+  public AggregationFunctionType getAggregationType() {
+    return AggregationFunctionType.DISTINCTCOUNTTHETASKETCH;
+  }
+
+  @Override
+  public DataType getAggregatedValueType() {
+    return AGGREGATED_VALUE_TYPE;
+  }
+
+  // This changes a lot similar to the Bitmap aggregator
+  private int _maxByteSize;
+
+  // Utility method to create a theta sketch with one item in it
+  private Sketch singleItemSketch(Object rawValue) {
+    return SingleItemSketch.create(rawValue.hashCode()).compact();
+  }
+
+  // Utility method to merge two sketches
+  private Sketch union(Sketch left, Sketch right) {
+    // TODO: Handle configurable nominal entries for StarTreeBuilder
+    Union u = Union.builder()
+      .setNominalEntries(CommonConstants.Helix.DEFAULT_THETA_SKETCH_NOMINAL_ENTRIES)
+      .buildUnion();
+    u.update(left);
+    u.update(right);
+    return u.getResult().compact();
+  }
+
+  @Override
+  public Sketch getInitialAggregatedValue(Object rawValue) {
+    Sketch initialValue;
+    if (rawValue instanceof byte[]) {
+      byte[] bytes = (byte[]) rawValue;
+      initialValue = deserializeAggregatedValue(bytes);
+      _maxByteSize = Math.max(_maxByteSize, bytes.length);
+    } else {
+      initialValue = singleItemSketch(rawValue);
+      _maxByteSize = Math.max(_maxByteSize, initialValue.getCurrentBytes(true));
+    }
+    return initialValue;

Review Comment:
   Yeah, this is more of a contract than enforcement. 



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/aggregator/DistinctCountThetaSketchValueAggregator.java:
##########
@@ -0,0 +1,138 @@
+/**
+ * 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.aggregator;
+
+import org.apache.datasketches.theta.Sketch;
+import org.apache.datasketches.theta.Sketches;
+import org.apache.datasketches.theta.Union;
+import org.apache.datasketches.theta.UpdateSketch;
+import org.apache.pinot.segment.local.utils.CustomSerDeUtils;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+import org.apache.pinot.spi.data.FieldSpec.DataType;
+import org.apache.pinot.spi.utils.CommonConstants;
+
+
+public class DistinctCountThetaSketchValueAggregator implements ValueAggregator<Object, Sketch> {
+  public static final DataType AGGREGATED_VALUE_TYPE = DataType.BYTES;
+
+  Union _union;
+
+  public DistinctCountThetaSketchValueAggregator() {
+    // TODO: Handle configurable nominal entries for StarTreeBuilder
+    _union = Union.builder()
+            .setNominalEntries(CommonConstants.Helix.DEFAULT_THETA_SKETCH_NOMINAL_ENTRIES)
+            .buildUnion();
+  };
+
+  @Override
+  public AggregationFunctionType getAggregationType() {
+    return AggregationFunctionType.DISTINCTCOUNTTHETASKETCH;
+  }
+
+  @Override
+  public DataType getAggregatedValueType() {
+    return AGGREGATED_VALUE_TYPE;
+  }
+
+  // This changes a lot similar to the Bitmap aggregator
+  private int _maxByteSize;
+
+  // Utility method to create a theta sketch with one item in it
+  private Sketch singleItemSketch(Object rawValue) {
+    // TODO: Handle configurable nominal entries for StarTreeBuilder
+    UpdateSketch sketch = Sketches.updateSketchBuilder()
+      .setNominalEntries(CommonConstants.Helix.DEFAULT_THETA_SKETCH_NOMINAL_ENTRIES)
+      .build();
+    if (rawValue instanceof String) {

Review Comment:
   There are other data types (eg `Boolean`, `JSON`) etc that might need to be handled. Seems like other valueAggregators have the same issue.
   
   cc: @Jackie-Jiang 



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