mirror of https://github.com/apache/druid.git
Merge pull request #1731 from metamx/regex-extraction-npe
fix NPE with regex extraction function
This commit is contained in:
commit
bd605a097e
|
@ -20,6 +20,7 @@ package io.druid.query.extraction;
|
||||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
import com.google.common.base.Strings;
|
||||||
import com.metamx.common.StringUtils;
|
import com.metamx.common.StringUtils;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
@ -59,8 +60,11 @@ public class RegexDimExtractionFn extends DimExtractionFn
|
||||||
@Override
|
@Override
|
||||||
public String apply(String dimValue)
|
public String apply(String dimValue)
|
||||||
{
|
{
|
||||||
|
if (dimValue == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
Matcher matcher = pattern.matcher(dimValue);
|
Matcher matcher = pattern.matcher(dimValue);
|
||||||
return matcher.find() ? matcher.group(1) : dimValue;
|
return Strings.emptyToNull(matcher.find() ? matcher.group(1) : dimValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonProperty("expr")
|
@JsonProperty("expr")
|
||||||
|
|
|
@ -102,6 +102,20 @@ public class RegexDimExtractionFnTest
|
||||||
Assert.assertTrue(extracted.contains("c"));
|
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
|
@Test
|
||||||
public void testSerde() throws Exception
|
public void testSerde() throws Exception
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue