diff --git a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchComplexMetricSerde.java b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchComplexMetricSerde.java index d2ca2378d56..997b32461dc 100644 --- a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchComplexMetricSerde.java +++ b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchComplexMetricSerde.java @@ -72,12 +72,14 @@ public class DoublesSketchComplexMetricSerde extends ComplexMetricSerde final Object object = inputRow.getRaw(metricName); if (object instanceof String) { // everything is a string during ingestion String objectString = (String) object; - // Autodetection of the input format: a number or base64 encoded sketch + // Autodetection of the input format: empty string, number, or base64 encoded sketch // A serialized DoublesSketch, as currently implemented, always has 0 in the first 6 bits. // This corresponds to "A" in base64, so it is not a digit - if (Character.isDigit((objectString).charAt(0))) { + if (objectString.isEmpty()) { + return DoublesSketchOperations.EMPTY_SKETCH; + } else if (Character.isDigit(objectString.charAt(0))) { try { - Double doubleValue = Double.parseDouble(objectString); + double doubleValue = Double.parseDouble(objectString); UpdateDoublesSketch sketch = DoublesSketch.builder().setK(MIN_K).build(); sketch.update(doubleValue); return sketch; diff --git a/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchComplexMetricSerdeTest.java b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchComplexMetricSerdeTest.java new file mode 100644 index 00000000000..276a9be322c --- /dev/null +++ b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchComplexMetricSerdeTest.java @@ -0,0 +1,56 @@ +/* + * 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.datasketches.quantiles; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.yahoo.sketches.quantiles.DoublesSketch; +import org.apache.druid.data.input.MapBasedInputRow; +import org.apache.druid.segment.serde.ComplexMetricExtractor; +import org.junit.Assert; +import org.junit.Test; + +public class DoublesSketchComplexMetricSerdeTest +{ + @Test + public void testExtractorOnEmptyString() + { + final DoublesSketchComplexMetricSerde serde = new DoublesSketchComplexMetricSerde(); + final ComplexMetricExtractor extractor = serde.getExtractor(); + final DoublesSketch sketch = (DoublesSketch) extractor.extractValue( + new MapBasedInputRow(0L, ImmutableList.of(), ImmutableMap.of("foo", "")), + "foo" + ); + Assert.assertEquals(0, sketch.getRetainedItems()); + } + + @Test + public void testExtractorOnNumber() + { + final DoublesSketchComplexMetricSerde serde = new DoublesSketchComplexMetricSerde(); + final ComplexMetricExtractor extractor = serde.getExtractor(); + final DoublesSketch sketch = (DoublesSketch) extractor.extractValue( + new MapBasedInputRow(0L, ImmutableList.of(), ImmutableMap.of("foo", "3.1")), + "foo" + ); + Assert.assertEquals(1, sketch.getRetainedItems()); + Assert.assertEquals(3.1d, sketch.getMaxValue(), 0.01d); + } +}