Skip to content

Commit

Permalink
Merge pull request #36 from yesamer/kie-issues#1008-7.67.x
Browse files Browse the repository at this point in the history
[7.67.x] [kie-issues#1008] Big Decimal data type conversion Issue (apache#5782)
  • Loading branch information
yesamer committed Mar 18, 2024
2 parents 95d69b1 + 4ae9313 commit 70f105e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -533,28 +533,35 @@ public static String lcFirst(final String name) {
public static Boolean compare(Object left, Object right, EvaluationContext ctx, BiPredicate<Comparable, Comparable> op) {
if ( left == null || right == null ) {
return null;
} else if (left instanceof ChronoPeriod && right instanceof ChronoPeriod) {
}
if (left instanceof ChronoPeriod && right instanceof ChronoPeriod) {
// periods have special compare semantics in FEEL as it ignores "days". Only months and years are compared
Long l = ComparablePeriod.toTotalMonths((ChronoPeriod) left);
Long r = ComparablePeriod.toTotalMonths((ChronoPeriod) right);
return op.test( l, r );
} else if (left instanceof TemporalAccessor && right instanceof TemporalAccessor) {
}
if (left instanceof TemporalAccessor && right instanceof TemporalAccessor) {
// Handle specific cases when both time / datetime
TemporalAccessor l = (TemporalAccessor) left;
TemporalAccessor r = (TemporalAccessor) right;
if (BuiltInType.determineTypeFromInstance(left) == BuiltInType.TIME && BuiltInType.determineTypeFromInstance(right) == BuiltInType.TIME) {
return op.test(valuet(l), valuet(r));
} else if (BuiltInType.determineTypeFromInstance(left) == BuiltInType.DATE_TIME && BuiltInType.determineTypeFromInstance(right) == BuiltInType.DATE_TIME) {
return op.test(valuedt(l, r.query(TemporalQueries.zone())), valuedt(r, l.query(TemporalQueries.zone())));
} // fallback; continue:
}
}
if (left instanceof Number && right instanceof Number) {
// Handle specific cases when both are Number, converting both to BigDecimal
BigDecimal l = getBigDecimalOrNull(left);
BigDecimal r = getBigDecimalOrNull(right);
return op.test(l, r);
}
// last fallback:
if ((left instanceof String && right instanceof String) ||
(left instanceof Number && right instanceof Number) ||
(left instanceof Boolean && right instanceof Boolean) ||
(left instanceof Comparable && left.getClass().isAssignableFrom(right.getClass()))) {
Comparable l = (Comparable) left;
Comparable r = (Comparable) right;
Comparable<?> l = (Comparable<?>) left;
Comparable<?> r = (Comparable<?>) right;
return op.test(l, r);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;

import org.junit.Test;
import org.kie.dmn.feel.lang.FEELProperty;
Expand Down Expand Up @@ -70,6 +71,22 @@ public void testGetGenericAccessor() throws NoSuchMethodException {
assertThat(EvalHelper.getGenericAccessor(TestPojo.class, "feelPropertyIdentifier")).as("getGenericAccessor should work for methods annotated with '@FEELProperty'.").isEqualTo(expectedAccessor);
}

@Test
public void testNumericValuesComparative() {
assertThat(EvalHelper.compare(BigDecimal.valueOf(1), BigDecimal.valueOf(2), null, (l, r) -> l.compareTo(r) < 0)).isTrue();
assertThat(EvalHelper.compare(1.0, 2.0, null, (l, r) -> l.compareTo(r) < 0)).isTrue();
assertThat(EvalHelper.compare(1, 2, null, (l, r) -> l.compareTo(r) > 0)).isFalse();
assertThat(EvalHelper.compare(BigDecimal.valueOf(1), 2, null, (l, r) -> l.compareTo(r) > 0)).isFalse();
assertThat(EvalHelper.compare(1, BigDecimal.valueOf(2), null, (l, r) -> l.compareTo(r) < 0)).isTrue();
assertThat(EvalHelper.compare(BigDecimal.valueOf(1), 2.3, null, (l, r) -> l.compareTo(r) == 0)).isFalse();
assertThat(EvalHelper.compare(1.2, BigDecimal.valueOf(1.2), null, (l, r) -> l.compareTo(r) == 0)).isTrue();
assertThat(EvalHelper.compare(BigDecimal.valueOf(1), 0L, null, (l, r) -> l.compareTo(r) > 0)).isTrue();
assertThat(EvalHelper.compare(10L, BigDecimal.valueOf(2), null, (l, r) -> l.compareTo(r) < 0)).isFalse();
assertThat(EvalHelper.compare(BigInteger.valueOf(1), BigInteger.valueOf(2), null, (l, r) -> l.compareTo(r) == 0)).isFalse();
assertThat(EvalHelper.compare(BigInteger.valueOf(1), 2, null, (l, r) -> l.compareTo(r) < 0)).isTrue();
assertThat(EvalHelper.compare(BigInteger.valueOf(1), 2.3, null, (l, r) -> l.compareTo(r) == 0)).isFalse();
}

private static class TestPojo {
@FEELProperty("feelPropertyIdentifier")
public String getAProperty() {
Expand Down

0 comments on commit 70f105e

Please sign in to comment.