From 254a8eb7e0c04eb8e6fe2c5d5950913faf821be2 Mon Sep 17 00:00:00 2001 From: Adarsh Sanjeev Date: Fri, 8 Dec 2023 11:43:04 +0530 Subject: [PATCH] Add null checks for HllSketchHolder (#15502) Fixes a potential NPE which could occur while folding the HllSketchAggregator. If the sketch is null, druid could return a null HllSketchHolder object. Adding a null check here could help here Resolves a null pointer exception in HllSketchAggregatorFactory --- .../hll/HllSketchAggregatorFactory.java | 8 ++++++-- .../hll/HllSketchHolderObjectStrategy.java | 1 + .../hll/HllSketchAggregatorFactoryTest.java | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchAggregatorFactory.java b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchAggregatorFactory.java index 1c908be244a..eb874dbea27 100644 --- a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchAggregatorFactory.java +++ b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchAggregatorFactory.java @@ -172,8 +172,12 @@ public abstract class HllSketchAggregatorFactory extends AggregatorFactory @Override public void fold(final ColumnValueSelector selector) { - final HllSketchHolder sketch = (HllSketchHolder) selector.getObject(); - union.update(sketch.getSketch()); + final HllSketchHolder sketchHolder = (HllSketchHolder) selector.getObject(); + // sketchHolder can be null here, if the sketch is empty. This is an optimisation done by + // HllSketchHolderObjectStrategy. If the holder is null, this should be a no-op. + if (sketchHolder != null) { + union.update(sketchHolder.getSketch()); + } } @Nullable diff --git a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchHolderObjectStrategy.java b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchHolderObjectStrategy.java index 18b7f03319e..93597a2621e 100644 --- a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchHolderObjectStrategy.java +++ b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchHolderObjectStrategy.java @@ -45,6 +45,7 @@ public class HllSketchHolderObjectStrategy implements ObjectStrategy