fix NPE with regex extraction function

This commit is contained in:
Xavier Léauté 2015-09-14 14:45:30 -07:00
parent 5ff92664f8
commit 08a527d01a
2 changed files with 19 additions and 1 deletions

View File

@ -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")

View File

@ -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
{