Fix NPE in TopNLexicographicResultBuilder.addEntry() (#2835)

This commit is contained in:
Jonathan Wei 2016-04-13 17:27:16 -07:00 committed by Gian Merlino
parent 639d0630b8
commit a26134575b
2 changed files with 60 additions and 1 deletions

View File

@ -131,7 +131,7 @@ public class TopNLexicographicResultBuilder implements TopNResultBuilder
public TopNResultBuilder addEntry(DimensionAndMetricValueExtractor dimensionAndMetricValueExtractor)
{
Object dimensionValueObj = dimensionAndMetricValueExtractor.getDimensionValue(dimSpec.getOutputName());
String dimensionValue = dimensionValueObj.toString();
String dimensionValue = dimensionValueObj == null ? null : dimensionValueObj.toString();
if (shouldAdd(dimensionValue)) {
pQueue.add(

View File

@ -37,6 +37,7 @@ import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ -461,4 +462,62 @@ public class TopNBinaryFnTest
Assert.assertEquals(expected.getTimestamp(), actual.getTimestamp());
assertTopNMergeResult(expected.getValue(), actual.getValue());
}
@Test
public void testMergeLexicographicWithInvalidDimName()
{
Result<TopNResultValue> result1 = new Result<TopNResultValue>(
currTime,
new TopNResultValue(
ImmutableList.<Map<String, Object>>of(
ImmutableMap.<String, Object>of(
"rows", 1L,
"index", 2L,
"testdim", "1"
)
)
)
);
Result<TopNResultValue> result2 = new Result<TopNResultValue>(
currTime,
new TopNResultValue(
ImmutableList.<Map<String, Object>>of(
ImmutableMap.<String, Object>of(
"rows", 2L,
"index", 3L,
"testdim", "1"
)
)
)
);
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("INVALID_DIM_NAME", null);
resultMap.put("rows", 3L);
resultMap.put("index", 5L);
Result<TopNResultValue> expected = new Result<TopNResultValue>(
currTime,
new TopNResultValue(
ImmutableList.<Map<String, Object>>of(
resultMap
)
)
);
Result<TopNResultValue> actual = new TopNBinaryFn(
TopNResultMerger.identity,
QueryGranularity.ALL,
new DefaultDimensionSpec("INVALID_DIM_NAME", null),
new LexicographicTopNMetricSpec(null),
2,
aggregatorFactories,
postAggregators
).apply(
result1,
result2
);
Assert.assertEquals(expected.getTimestamp(), actual.getTimestamp());
assertTopNMergeResult(expected.getValue(), actual.getValue());
}
}