From 08a527d01ab7209c92918ddb2ad5dee478e13d5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20L=C3=A9aut=C3=A9?= Date: Mon, 14 Sep 2015 14:45:30 -0700 Subject: [PATCH] fix NPE with regex extraction function --- .../query/extraction/RegexDimExtractionFn.java | 6 +++++- .../query/extraction/RegexDimExtractionFnTest.java | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/processing/src/main/java/io/druid/query/extraction/RegexDimExtractionFn.java b/processing/src/main/java/io/druid/query/extraction/RegexDimExtractionFn.java index cc374ad229e..b3c33b68e14 100644 --- a/processing/src/main/java/io/druid/query/extraction/RegexDimExtractionFn.java +++ b/processing/src/main/java/io/druid/query/extraction/RegexDimExtractionFn.java @@ -20,6 +20,7 @@ package io.druid.query.extraction; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.base.Preconditions; +import com.google.common.base.Strings; import com.metamx.common.StringUtils; import java.nio.ByteBuffer; @@ -59,8 +60,11 @@ public class RegexDimExtractionFn extends DimExtractionFn @Override public String apply(String dimValue) { + if (dimValue == null) { + return null; + } Matcher matcher = pattern.matcher(dimValue); - return matcher.find() ? matcher.group(1) : dimValue; + return Strings.emptyToNull(matcher.find() ? matcher.group(1) : dimValue); } @JsonProperty("expr") diff --git a/processing/src/test/java/io/druid/query/extraction/RegexDimExtractionFnTest.java b/processing/src/test/java/io/druid/query/extraction/RegexDimExtractionFnTest.java index b7ee137e8ee..3da70ba3f53 100644 --- a/processing/src/test/java/io/druid/query/extraction/RegexDimExtractionFnTest.java +++ b/processing/src/test/java/io/druid/query/extraction/RegexDimExtractionFnTest.java @@ -102,6 +102,20 @@ public class RegexDimExtractionFnTest Assert.assertTrue(extracted.contains("c")); } + + @Test + public void testNullAndEmpty() + { + String regex = "(.*)/.*/.*"; + ExtractionFn extractionFn = new RegexDimExtractionFn(regex); + // no match, map empty input value to null + Assert.assertEquals(null, extractionFn.apply("")); + // null value, returns null + Assert.assertEquals(null, extractionFn.apply(null)); + // empty match, map empty result to null + Assert.assertEquals(null, extractionFn.apply("/a/b")); + } + @Test public void testSerde() throws Exception {