Skip to content

Commit

Permalink
Fix aggregate function errors (#1375)
Browse files Browse the repository at this point in the history
* Test cases

* Fix AllTrue and AnyTrue

* Fix standard deviation aggregators

* Tests

---------

Co-authored-by: JP <jonathan.i.percival@gmail.com>
  • Loading branch information
holly-smile and JPercival committed Jul 3, 2024
1 parent ee6d3d7 commit 36ff443
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class AllTrueEvaluator {

public static Boolean allTrue(Object src) {
if (src == null) {
return null;
return true;
}

if (src instanceof Iterable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
public class AnyTrueEvaluator {

public static Boolean anyTrue(Object src) {

if (src == null) {
return null;
return false;
}

if (src instanceof Iterable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public static Object popStdDev(Object source, State state) {
}

Object variance = PopulationVarianceEvaluator.popVariance(source, state);
// The cases in which PopulationVariance returns null are the same as those where PopulationStdDev does.
if(variance == null) {
return null;
}

return variance instanceof BigDecimal
? PowerEvaluator.power(variance, new BigDecimal("0.5"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public static Object stdDev(Object source, State state) {
}

Object variance = VarianceEvaluator.variance(source, state);
// The cases in which Variance returns null are the same as those where StdDev does.
if(variance == null) {
return null;
}

return variance instanceof BigDecimal
? PowerEvaluator.power(variance, new BigDecimal("0.5"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.*;

import java.math.BigDecimal;
import java.util.Arrays;

import org.hamcrest.core.IsNull;
import org.junit.jupiter.api.Test;
import org.opencds.cqf.cql.engine.elm.executing.AnyTrueEvaluator;
import org.opencds.cqf.cql.engine.elm.executing.AvgEvaluator;
Expand Down Expand Up @@ -43,6 +46,9 @@ void all_aggregate_function_tests() {
value = results.forExpression("AllTrueEmptyList").value();
assertThat(value, is(true));

value = results.forExpression("AllTrueIsTrueWhenNull").value();
assertThat(value, is(true));

value = results.forExpression("AnyTrueAllTrue").value();
assertThat(value, is(true));

Expand Down Expand Up @@ -70,6 +76,10 @@ void all_aggregate_function_tests() {
value = results.forExpression("AnyTrueEmptyList").value();
assertThat(value, is(false));

value = results.forExpression("AnyTrueIsFalseWhenNull").value();
assertThat(value, is(false));


try {
value = AnyTrueEvaluator.anyTrue(Arrays.asList("this", "is", "error"));
fail();
Expand Down Expand Up @@ -152,15 +162,24 @@ void all_aggregate_function_tests() {
((BigDecimal) value)
.compareTo(new BigDecimal("1.41421356"))); // 23730951454746218587388284504413604736328125

value = results.forExpression("PopulationStdDevIsNull").value();
assertThat(value, is(nullValue()));

value = results.forExpression("PopVarianceTest1").value();
assertEquals(0, ((BigDecimal) value).compareTo(new BigDecimal("2.0")));

value = results.forExpression("PopVarianceIsNull").value();
assertThat(value, is(nullValue()));

value = results.forExpression("StdDevTest1").value();
assertEquals(
0,
((BigDecimal) value)
.compareTo(new BigDecimal("1.58113883"))); // 00841897613935316257993690669536590576171875

value = results.forExpression("StdDevIsNull").value();
assertThat(value, is(nullValue()));

value = results.forExpression("SumTest1").value();
assertThat(value, is(new BigDecimal("20.0")));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ define AllTrueAllTrueFalseTrue : AllTrue({true,false,true})
define AllTrueAllFalseTrueFalse : AllTrue({false,true,false})
define AllTrueNullFirst : AllTrue({null,true,true})
define AllTrueEmptyList : AllTrue({})

define AllTrueIsTrueWhenNull : AllTrue(null)
//AnyTrue
define AnyTrueAllTrue : AnyTrue({true,true})
define AnyTrueAllFalse : AnyTrue({false,false})
Expand All @@ -19,6 +19,7 @@ define AnyTrueFalseFirst : AnyTrue({false,true})
define AnyTrueNullFirstThenTrue : AnyTrue({null,true})
define AnyTrueNullFirstThenFalse : AnyTrue({null,false})
define AnyTrueEmptyList : AnyTrue({})
define AnyTrueIsFalseWhenNull : AnyTrue(null)

//Avg
define AvgTest1: Avg({ 1.0, 2.0, 3.0, 6.0 })
Expand Down Expand Up @@ -57,12 +58,15 @@ define ModeTestTime: Mode({ @T15:59:59.999, @T05:59:59.999, @T20:59:59.999, @T05

//PopulationStdDev
define PopStdDevTest1: PopulationStdDev({ 1.0, 2.0, 3.0, 4.0, 5.0 })
define PopulationStdDevIsNull: PopulationStdDev({ null as Quantity, null as Quantity, null as Quantity })

//PopulationVariance
define PopVarianceTest1: PopulationVariance({ 1.0, 2.0, 3.0, 4.0, 5.0 })
define PopVarianceIsNull: PopulationVariance({ null as Quantity, null as Quantity, null as Quantity })

//StdDev
define StdDevTest1: StdDev({ 1.0, 2.0, 3.0, 4.0, 5.0 })
define StdDevIsNull: StdDev({ null as Quantity, null as Quantity, null as Quantity })

//Sum
define SumTest1: Sum({ 6.0, 2.0, 3.0, 4.0, 5.0 })
Expand Down

0 comments on commit 36ff443

Please sign in to comment.