Skip to content

Commit

Permalink
Make Numeric.sum return null when there are no inputs or all null inp…
Browse files Browse the repository at this point in the history
…uts (#5461)

* Make sum of nulls or no values return null

* Another unit test.
  • Loading branch information
chipkent authored May 7, 2024
1 parent 2dbb2f9 commit cd444b6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
14 changes: 14 additions & 0 deletions engine/function/src/templates/Numeric.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,7 @@ public class Numeric {
}

double sum = 0;
long nullCount = 0;

try ( final ${pt.vectorIterator} vi = values.iterator() ) {
while ( vi.hasNext() ) {
Expand All @@ -1499,10 +1500,16 @@ public class Numeric {

if (!isNull(c)) {
sum += c;
} else {
nullCount++;
}
}
}

if (nullCount == values.size()) {
return NULL_DOUBLE;
}

return sum;
}
<#else>
Expand All @@ -1512,17 +1519,24 @@ public class Numeric {
}

long sum = 0;
long nullCount = 0;

try ( final ${pt.vectorIterator} vi = values.iterator() ) {
while ( vi.hasNext() ) {
final ${pt.primitive} c = vi.${pt.iteratorNext}();

if (!isNull(c)) {
sum += c;
} else {
nullCount++;
}
}
}

if (nullCount == values.size()) {
return NULL_LONG;
}

return sum;
}
</#if>
Expand Down
15 changes: 10 additions & 5 deletions engine/function/src/templates/TestNumeric.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -567,36 +567,41 @@ public class TestNumeric extends BaseArrayTestCase {

public void test${pt.boxed}Sum1() {
assertTrue(Math.abs(15 - sum(new ${pt.vectorDirect}(new ${pt.primitive}[]{4, 5, 6}))) == 0.0);
assertTrue(Math.abs(0 - sum(new ${pt.vectorDirect}())) == 0.0);
assertTrue(Math.abs(0 - sum(new ${pt.vectorDirect}(${pt.null}))) == 0.0);
assertTrue(Math.abs(20 - sum(new ${pt.vectorDirect}(new ${pt.primitive}[]{5, ${pt.null}, 15}))) == 0.0);
<#if pt.valueType.isFloat >
assertEquals(NULL_DOUBLE, sum((${pt.vector}) null));
assertEquals(NULL_DOUBLE, sum(new ${pt.vectorDirect}(new ${pt.primitive}[]{})));
assertEquals(NULL_DOUBLE, sum(new ${pt.vectorDirect}(new ${pt.primitive}[]{${pt.null}, ${pt.null}})));
assertEquals(Double.POSITIVE_INFINITY, sum(new ${pt.vectorDirect}(new ${pt.primitive}[]{4, Float.POSITIVE_INFINITY, 6})));
assertEquals(Double.POSITIVE_INFINITY, sum(new ${pt.vectorDirect}(new ${pt.primitive}[]{4, Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY})));
assertEquals(Double.NEGATIVE_INFINITY, sum(new ${pt.vectorDirect}(new ${pt.primitive}[]{4, Float.NEGATIVE_INFINITY, 6})));
assertEquals(Double.NEGATIVE_INFINITY, sum(new ${pt.vectorDirect}(new ${pt.primitive}[]{4, Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY})));
assertEquals(Double.NaN, sum(new ${pt.vectorDirect}(new ${pt.primitive}[]{4, Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY})));
assertEquals(Double.NaN, sum(new ${pt.vectorDirect}(new ${pt.primitive}[]{4, Float.NaN, 6})));
<#else>
assertEquals(NULL_LONG, sum((${pt.vector}) null));
assertEquals(NULL_LONG, sum(new ${pt.vectorDirect}(new ${pt.primitive}[]{})));
assertEquals(NULL_LONG, sum(new ${pt.vectorDirect}(new ${pt.primitive}[]{${pt.null}, ${pt.null}})));
</#if>

}

public void test${pt.boxed}Sum2() {
assertTrue(Math.abs(15 - sum(new ${pt.primitive}[]{4, 5, 6})) == 0.0);
assertTrue(Math.abs(0 - sum(new ${pt.primitive}[]{})) == 0.0);
assertTrue(Math.abs(0 - sum(new ${pt.primitive}[]{${pt.null}})) == 0.0);
assertTrue(Math.abs(20 - sum(new ${pt.primitive}[]{5, ${pt.null}, 15})) == 0.0);
<#if pt.valueType.isFloat >
assertEquals(NULL_DOUBLE, sum((${pt.primitive}[]) null));
assertEquals(NULL_DOUBLE, sum(new ${pt.primitive}[]{}));
assertEquals(NULL_DOUBLE, sum(new ${pt.primitive}[]{${pt.null}, ${pt.null}}));
assertEquals(Double.POSITIVE_INFINITY, sum(new ${pt.primitive}[]{4, Float.POSITIVE_INFINITY, 6}));
assertEquals(Double.POSITIVE_INFINITY, sum(new ${pt.primitive}[]{4, Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY}));
assertEquals(Double.NEGATIVE_INFINITY, sum(new ${pt.primitive}[]{4, Float.NEGATIVE_INFINITY, 6}));
assertEquals(Double.NEGATIVE_INFINITY, sum(new ${pt.primitive}[]{4, Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY}));
assertEquals(Double.NaN, sum(new ${pt.primitive}[]{4, Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY}));
assertEquals(Double.NaN, sum(new ${pt.primitive}[]{4, Float.NaN, 6}));
<#else>
assertEquals(NULL_LONG, sum((${pt.primitive}[]) null));
assertEquals(NULL_LONG, sum(new ${pt.primitive}[]{}));
assertEquals(NULL_LONG, sum(new ${pt.primitive}[]{${pt.null}, ${pt.null}}));
</#if>
}

Expand Down

0 comments on commit cd444b6

Please sign in to comment.