Skip to content
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 @@ -90,7 +90,7 @@ public Object getRaw(String dimension)
@Override
public Number getMetric(String metric)
{
return Rows.objectToNumber(metric, event.get(metric));
return Rows.objectToNumber(metric, event.get(metric), true);
}

@Override
Expand Down
40 changes: 27 additions & 13 deletions core/src/main/java/org/apache/druid/data/input/Rows.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.stream.Collectors;

/**
*
*/
public final class Rows
{
Expand Down Expand Up @@ -75,25 +76,30 @@ public static List<String> objectToStrings(final Object inputValue)
}

/**
* Convert an object to a number. Nulls are treated as zeroes unless
* druid.generic.useDefaultValueForNull is set to false.
* Convert an object to a number.
*
* If {@link NullHandling#replaceWithDefault()} is true, this method will never return null. If false, it will return
* {@link NullHandling#defaultLongValue()} instead of null.
*
* @param name field name of the object being converted (may be used for exception messages)
* @param inputValue the actual object being converted
* @param name field name of the object being converted (may be used for exception messages)
* @param inputValue the actual object being converted
* @param throwParseExceptions whether this method should throw a {@link ParseException} or use a default/null value
* when {@param inputValue} is not numeric
*
* @return a number
* @return a Number; will not necessarily be the same type as {@param zeroClass}
*
* @throws NullPointerException if the string is null
* @throws ParseException if the column cannot be converted to a number
* @throws ParseException if the input cannot be converted to a number and {@code throwParseExceptions} is true
*/
@Nullable
public static Number objectToNumber(final String name, final Object inputValue)
public static <T extends Number> Number objectToNumber(
final String name,
final Object inputValue,
final boolean throwParseExceptions
)
{
if (inputValue == null) {
return NullHandling.defaultLongValue();
}

if (inputValue instanceof Number) {
} else if (inputValue instanceof Number) {
return (Number) inputValue;
} else if (inputValue instanceof String) {
try {
Expand All @@ -109,10 +115,18 @@ public static Number objectToNumber(final String name, final Object inputValue)
}
}
catch (Exception e) {
throw new ParseException(e, "Unable to parse value[%s] for field[%s]", inputValue, name);
if (throwParseExceptions) {
throw new ParseException(e, "Unable to parse value[%s] for field[%s]", inputValue, name);
} else {
return NullHandling.defaultLongValue();
}
}
} else {
throw new ParseException("Unknown type[%s] for field[%s]", inputValue.getClass(), name);
if (throwParseExceptions) {
throw new ParseException("Unknown type[%s] for field[%s]", inputValue.getClass(), name);
} else {
return NullHandling.defaultLongValue();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ public void buildingSketchesAtIngestionTime() throws Exception
Assert.assertEquals(400.0, sketchObject.getPowerSums()[0], 1e-10);

MomentSketchWrapper sketchObjectWithNulls = (MomentSketchWrapper) row.get(1); // "sketchWithNulls"
// 23 null values, nulls at ingestion time are not replaced with default values for complex metrics inputs
Assert.assertEquals(377.0, sketchObjectWithNulls.getPowerSums()[0], 1e-10);
// 23 null values (377 when nulls are not replaced with default)
Assert.assertEquals(
NullHandling.replaceWithDefault() ? 400.0 : 377.0,
sketchObjectWithNulls.getPowerSums()[0],
1e-10
);

double[] quantilesArray = (double[]) row.get(2); // "quantiles"
Assert.assertEquals(0, quantilesArray[0], 0.05);
Expand All @@ -146,12 +150,16 @@ public void buildingSketchesAtIngestionTime() throws Exception
Assert.assertEquals(0.9969, maxValue, 0.0001);

double[] quantilesArrayWithNulls = (double[]) row.get(5); // "quantilesWithNulls"
Assert.assertEquals(5.0, quantilesArrayWithNulls[0], 0.05);
Assert.assertEquals(7.57, quantilesArrayWithNulls[1], 0.05);
Assert.assertEquals(NullHandling.replaceWithDefault() ? 0.0 : 5.0, quantilesArrayWithNulls[0], 0.05);
Assert.assertEquals(
NullHandling.replaceWithDefault() ? 7.721400294818661d : 7.57,
quantilesArrayWithNulls[1],
0.05
);
Assert.assertEquals(10.0, quantilesArrayWithNulls[2], 0.05);

Double minValueWithNulls = (Double) row.get(6); // "minWithNulls"
Assert.assertEquals(5.0164, minValueWithNulls, 0.0001);
Assert.assertEquals(NullHandling.replaceWithDefault() ? 0.0 : 5.0164, minValueWithNulls, 0.0001);

Double maxValueWithNulls = (Double) row.get(7); // "maxWithNulls"
Assert.assertEquals(9.9788, maxValueWithNulls, 0.0001);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,10 @@ public void buildingSketchesAtIngestionTimeThreeValuesAndNulls() throws Exceptio
List<ResultRow> results = seq.toList();
Assert.assertEquals(1, results.size());
ResultRow row = results.get(0);
Assert.assertEquals("sketch", 30.0, (double) row.get(0), 0);
Assert.assertEquals("estimate", 30.0, (double) row.get(1), 0);
Assert.assertEquals("union", 30.0, (double) row.get(3), 0);
Assert.assertEquals("intersection", 30.0, (double) row.get(4), 0);
Assert.assertEquals("sketch", NullHandling.replaceWithDefault() ? 40.0 : 30.0, (double) row.get(0), 0);
Assert.assertEquals("estimate", NullHandling.replaceWithDefault() ? 40.0 : 30.0, (double) row.get(1), 0);
Assert.assertEquals("union", NullHandling.replaceWithDefault() ? 40.0 : 30.0, (double) row.get(3), 0);
Assert.assertEquals("intersection", NullHandling.replaceWithDefault() ? 40.0 : 30.0, (double) row.get(4), 0);
Assert.assertEquals("anotb", 0, (double) row.get(5), 0);

Object meansObj = row.get(6); // means
Expand All @@ -548,20 +548,20 @@ public void buildingSketchesAtIngestionTimeThreeValuesAndNulls() throws Exceptio
Assert.assertEquals(3, means.length);
Assert.assertEquals(1.0, means[0], 0);
Assert.assertEquals(2.0, means[1], 0);
Assert.assertEquals(3.0, means[2], 0.1);
Assert.assertEquals(NullHandling.replaceWithDefault() ? 2.25 : 3.0, means[2], 0.1);

Object obj = row.get(2); // quantiles-sketch
Assert.assertTrue(obj instanceof DoublesSketch);
DoublesSketch ds = (DoublesSketch) obj;
Assert.assertEquals(30, ds.getN());
Assert.assertEquals(NullHandling.replaceWithDefault() ? 40 : 30, ds.getN());
Assert.assertEquals(2.0, ds.getMinValue(), 0);
Assert.assertEquals(2.0, ds.getMaxValue(), 0);

Object objSketch2 = row.get(7); // quantiles-sketch-with-nulls
Assert.assertTrue(objSketch2 instanceof DoublesSketch);
DoublesSketch ds2 = (DoublesSketch) objSketch2;
Assert.assertEquals(30, ds2.getN());
Assert.assertEquals(3.0, ds2.getMinValue(), 0);
Assert.assertEquals(NullHandling.replaceWithDefault() ? 40 : 30, ds2.getN());
Assert.assertEquals(NullHandling.replaceWithDefault() ? 0.0 : 3.0, ds2.getMinValue(), 0);
Assert.assertEquals(3.0, ds2.getMaxValue(), 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public ApproximateHistogram extractValue(InputRow inputRow, String metricName)
if (rawValue instanceof Collection) {
for (final Object next : ((Collection) rawValue)) {
if (next != null) {
h.offer(Rows.objectToNumber(metricName, next).floatValue());
h.offer(Rows.objectToNumber(metricName, next, true).floatValue());
}
}
} else {
h.offer(Rows.objectToNumber(metricName, rawValue).floatValue());
h.offer(Rows.objectToNumber(metricName, rawValue, true).floatValue());
}

return h;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public FixedBucketsHistogram extractValue(InputRow inputRow, String metricName,
} else if (rawValue instanceof String) {
Number numberAttempt;
try {
numberAttempt = Rows.objectToNumber(metricName, rawValue);
numberAttempt = Rows.objectToNumber(metricName, rawValue, true);
FixedBucketsHistogram fbh = new FixedBucketsHistogram(
aggregatorFactory.getLowerLimit(),
aggregatorFactory.getUpperLimit(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void testSerde()
Assert.assertEquals(5.0f, out.getMetric("m1out").floatValue(), 0.00001);
Assert.assertEquals(100L, out.getMetric("m2out"));
Assert.assertEquals(1, ((HyperLogLogCollector) out.getRaw("m3out")).estimateCardinality(), 0.001);
Assert.assertEquals(0L, out.getMetric("unparseable"));
Assert.assertEquals(NullHandling.defaultLongValue(), out.getMetric("unparseable"));

EasyMock.verify(mockedAggregator);
EasyMock.verify(mockedNullAggregator);
Expand Down

This file was deleted.

This file was deleted.

Loading