Skip to content

Commit

Permalink
Merge pull request #1731 from metamx/regex-extraction-npe
Browse files Browse the repository at this point in the history
fix NPE with regex extraction function
  • Loading branch information
drcrallen committed Sep 14, 2015
2 parents 5f36e7a + 08a527d commit bd605a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -59,8 +60,11 @@ public byte[] getCacheKey()
@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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ public void testStringExtraction()
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
{
Expand Down

0 comments on commit bd605a0

Please sign in to comment.