diff --git a/benchmarks/src/main/java/org/apache/druid/benchmark/DataSketchesHllBenchmark.java b/benchmarks/src/main/java/org/apache/druid/benchmark/DataSketchesHllBenchmark.java
new file mode 100644
index 00000000000..3493f559412
--- /dev/null
+++ b/benchmarks/src/main/java/org/apache/druid/benchmark/DataSketchesHllBenchmark.java
@@ -0,0 +1,123 @@
+/*
+ * 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.benchmark;
+
+import com.yahoo.sketches.hll.HllSketch;
+import org.apache.druid.query.aggregation.AggregatorFactory;
+import org.apache.druid.query.aggregation.BufferAggregator;
+import org.apache.druid.query.aggregation.datasketches.hll.HllSketchMergeAggregatorFactory;
+import org.apache.druid.query.dimension.DimensionSpec;
+import org.apache.druid.segment.ColumnSelectorFactory;
+import org.apache.druid.segment.ColumnValueSelector;
+import org.apache.druid.segment.DimensionSelector;
+import org.apache.druid.segment.column.ColumnCapabilities;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+import java.util.concurrent.TimeUnit;
+
+@BenchmarkMode(Mode.Throughput)
+@OutputTimeUnit(TimeUnit.SECONDS)
+@Warmup(iterations = 5)
+@Measurement(iterations = 15)
+@Fork(1)
+@State(Scope.Benchmark)
+public class DataSketchesHllBenchmark
+{
+ private final AggregatorFactory aggregatorFactory = new HllSketchMergeAggregatorFactory(
+ "hll",
+ "hll",
+ null,
+ null,
+ false
+ );
+
+ private final ByteBuffer buf = ByteBuffer.allocateDirect(aggregatorFactory.getMaxIntermediateSize());
+
+ private BufferAggregator aggregator;
+
+ @Setup(Level.Trial)
+ public void setUp()
+ {
+ aggregator = aggregatorFactory.factorizeBuffered(
+ new ColumnSelectorFactory()
+ {
+ @Override
+ public DimensionSelector makeDimensionSelector(DimensionSpec dimensionSpec)
+ {
+ return null;
+ }
+
+ @Override
+ public ColumnValueSelector makeColumnValueSelector(String columnName)
+ {
+ return null;
+ }
+
+ @Nullable
+ @Override
+ public ColumnCapabilities getColumnCapabilities(String column)
+ {
+ return null;
+ }
+ }
+ );
+ }
+
+ @TearDown(Level.Trial)
+ public void tearDown()
+ {
+ aggregator.close();
+ aggregator = null;
+ }
+
+ @Benchmark
+ public void init(Blackhole bh)
+ {
+ aggregator.init(buf, 0);
+ }
+
+ @Benchmark
+ public Object initAndGet()
+ {
+ aggregator.init(buf, 0);
+ return aggregator.get(buf, 0);
+ }
+
+ @Benchmark
+ public Object initAndSerde()
+ {
+ aggregator.init(buf, 0);
+ return aggregatorFactory.deserialize(((HllSketch) aggregator.get(buf, 0)).toCompactByteArray());
+ }
+}
diff --git a/extensions-core/datasketches/pom.xml b/extensions-core/datasketches/pom.xml
index db91883f1cf..151b5544202 100644
--- a/extensions-core/datasketches/pom.xml
+++ b/extensions-core/datasketches/pom.xml
@@ -34,11 +34,16 @@
../../pom.xml
+
+ 0.13.4
+ 0.12.2
+
+
com.yahoo.datasketches
sketches-core
- 0.13.4
+ ${datasketches.core.version}
com.google.code.findbugs
@@ -46,6 +51,11 @@
+
+ com.yahoo.datasketches
+ memory
+ ${datasketches.memory.version}
+
org.apache.commons
commons-math3
diff --git a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchBuildBufferAggregator.java b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchBuildBufferAggregator.java
index 5532866a2d8..5789127038b 100644
--- a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchBuildBufferAggregator.java
+++ b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchBuildBufferAggregator.java
@@ -23,6 +23,7 @@ import com.google.common.util.concurrent.Striped;
import com.yahoo.memory.WritableMemory;
import com.yahoo.sketches.hll.HllSketch;
import com.yahoo.sketches.hll.TgtHllType;
+import com.yahoo.sketches.hll.Union;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.apache.druid.query.aggregation.BufferAggregator;
@@ -42,7 +43,9 @@ import java.util.concurrent.locks.ReadWriteLock;
public class HllSketchBuildBufferAggregator implements BufferAggregator
{
- /** for locking per buffer position (power of 2 to make index computation faster) */
+ /**
+ * for locking per buffer position (power of 2 to make index computation faster)
+ */
private static final int NUM_STRIPES = 64;
private final ColumnValueSelector