You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by cw...@apache.org on 2019/04/24 22:14:04 UTC
[incubator-druid] 03/20: DoublesSketchComplexMetricSerde: Handle
empty strings. (#7429)
This is an automated email from the ASF dual-hosted git repository.
cwylie pushed a commit to branch 0.14.1-incubating
in repository https://gitbox.apache.org/repos/asf/incubator-druid.git
commit edcf73b9fda68f802f1898f61defd42e1abc13ef
Author: Gian Merlino <gi...@gmail.com>
AuthorDate: Mon Apr 8 18:01:31 2019 -0700
DoublesSketchComplexMetricSerde: Handle empty strings. (#7429)
---
.../quantiles/DoublesSketchComplexMetricSerde.java | 8 ++--
.../DoublesSketchComplexMetricSerdeTest.java | 56 ++++++++++++++++++++++
2 files changed, 61 insertions(+), 3 deletions(-)
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 d2ca237..997b324 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 0000000..276a9be
--- /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);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org