mirror of https://github.com/apache/druid.git
fix NPE with regex extraction function
This commit is contained in:
parent
5ff92664f8
commit
08a527d01a
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue