jon-wei commented on a change in pull request #7331: TDigest backed sketch 
aggregators
URL: https://github.com/apache/incubator-druid/pull/7331#discussion_r282288766
 
 

 ##########
 File path: 
extensions-contrib/tdigestsketch/src/main/java/org/apache/druid/query/aggregation/tdigestsketch/TDigestBuildSketchBufferAggregator.java
 ##########
 @@ -0,0 +1,125 @@
+/*
+ * 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.druid.query.aggregation.tdigestsketch;
+
+import com.google.common.base.Preconditions;
+import com.tdunning.math.stats.MergingDigest;
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import org.apache.druid.java.util.common.IAE;
+import org.apache.druid.query.aggregation.BufferAggregator;
+import org.apache.druid.segment.ColumnValueSelector;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.GuardedBy;
+import java.nio.ByteBuffer;
+import java.util.IdentityHashMap;
+import java.util.Map;
+
+/**
+ * Aggregator that builds t-digest backed sketches using numeric values read 
from {@link ByteBuffer}
+ */
+public class TDigestBuildSketchBufferAggregator implements BufferAggregator
+{
+
+  @Nonnull
+  private final ColumnValueSelector selector;
+  private final int compression;
+
+  @GuardedBy("this")
+  private final Map<ByteBuffer, Int2ObjectMap<MergingDigest>> sketches = new 
IdentityHashMap<>();
+
+  public TDigestBuildSketchBufferAggregator(
+      final ColumnValueSelector valueSelector,
+      @Nullable final Integer compression
+  )
+  {
+    Preconditions.checkNotNull(valueSelector);
+    this.selector = valueSelector;
+    if (compression != null) {
+      this.compression = compression;
+    } else {
+      this.compression = 
TDigestBuildSketchAggregatorFactory.DEFAULT_COMPRESSION;
+    }
+  }
+
+  @Override
+  public synchronized void init(ByteBuffer buffer, int position)
+  {
+    MergingDigest emptyDigest = new MergingDigest(compression);
+    putSketch(buffer, position, emptyDigest);
+  }
+
+  @Override
+  public synchronized void aggregate(ByteBuffer buffer, int position)
+  {
+    MergingDigest sketch = sketches.get(buffer).get(position);
+    Object x = selector.getObject();
+    if (x instanceof Number) {
+      sketch.add(((Number) x).doubleValue());
+    } else {
+      throw new IAE("Unexpected value of type " + x.getClass().getName() + " 
encountered");
+    }
+  }
+
+  @Override
+  public synchronized Object get(final ByteBuffer buffer, final int position)
+  {
+    return sketches.get(buffer).get(position);
 
 Review comment:
   The get() on the buffer aggregator needs to return a snapshot copy of the 
sketch to avoid use-after-free issues (see recently updated javadocs on get() 
and https://github.com/apache/incubator-druid/pull/7464)
   
   

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

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

Reply via email to