Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix NPE with regex extraction function #1731

Merged
merged 1 commit into from
Sep 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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