Skip to content

Commit

Permalink
Issue #1641 - Fixed ceiling, floor and round return types. (#1665)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Kraus <tomas.kraus@oracle.com>
  • Loading branch information
Tomas-Kraus committed Aug 30, 2022
1 parent 1c799cd commit 4181361
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,8 @@ protected Object getObjectThroughOptimizedDataConversion(
: platform.getConversionManager().getDefaultNullValue(ClassConstants.TIME_ODATETIME);
}
}
} else if (fieldType == ClassConstants.BIGDECIMAL) {
value = resultSet.getBigDecimal(columnNumber);
} else if (fieldType == ClassConstants.BIGINTEGER) {
value = resultSet.getBigDecimal(columnNumber);
if (value != null) return ((BigDecimal)value).toBigInteger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// - Issue 1442: Implement New Jakarta Persistence 3.1 Features
package org.eclipse.persistence.jpa.test.criteria;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

Expand Down Expand Up @@ -60,16 +61,16 @@ public class TestMathFunctions {
private EntityManagerFactory emf;

private final NumberEntity[] NUMBER = {
new NumberEntity(0, 0L, 0F, 0D),
new NumberEntity(1, 1L, 1F, 1D),
new NumberEntity(2, -1L, -1F, -1D),
new NumberEntity(3, 42L, 42.42F, 42.42D),
new NumberEntity(4, -342L, -342.42F, -342.42D),
new NumberEntity(5, 4L, 4F, 4D),
new NumberEntity(6, -4L, -4F, -4D),
new NumberEntity(7, 4L, 14.23F, 14.23D),
new NumberEntity(8, 6L, 44.7542383252F, 44.7542383252D),
new NumberEntity(9, 8L, -214.2457321233F, -214.2457321233D)
new NumberEntity(0, 0L, 0F, 0D, BigDecimal.valueOf(0D)),
new NumberEntity(1, 1L, 1F, 1D, BigDecimal.valueOf(1D)),
new NumberEntity(2, -1L, -1F, -1D, BigDecimal.valueOf(-1D)),
new NumberEntity(3, 42L, 42.42F, 42.42D, BigDecimal.valueOf(42.42D)),
new NumberEntity(4, -342L, -342.42F, -342.42D, BigDecimal.valueOf(-342.42D)),
new NumberEntity(5, 4L, 4F, 4D, BigDecimal.valueOf(4D)),
new NumberEntity(6, -4L, -4F, -4D, BigDecimal.valueOf(-4D)),
new NumberEntity(7, 4L, 14.23F, 14.23D, BigDecimal.valueOf(14.23D)),
new NumberEntity(8, 6L, 44.7542383252F, 44.7542383252D, BigDecimal.valueOf(44.7542383252D)),
new NumberEntity(9, 8L, -214.2457321233F, -214.2457321233D, BigDecimal.valueOf(-214.2457321233D))
};

@Before
Expand Down Expand Up @@ -663,4 +664,49 @@ public void testRoundFloatMethodWithNegative() {
}
}

// Issue #1641: Returned value must match argument type for CEILING(n)
@Test
public void testCeilingKeepBigDecimalParamType() {
try (final EntityManager em = emf.createEntityManager()) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Number> cq = cb.createQuery(Number.class);
Root<NumberEntity> number = cq.from(NumberEntity.class);
cq.select(cb.ceiling(number.get("bdValue")));
cq.where(cb.equal(number.get("id"), 8));
Number result = em.createQuery(cq).getSingleResult();
MatcherAssert.assertThat(result, Matchers.is(Matchers.instanceOf(BigDecimal.class)));
MatcherAssert.assertThat(result.doubleValue(), Matchers.equalTo(Math.ceil(NUMBER[8].getBdValue().doubleValue())));
}
}

// Issue #1641: Returned value must match argument type for FLOOR(n)
@Test
public void testFloorKeepBigDecimalParamType() {
try (final EntityManager em = emf.createEntityManager()) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Number> cq = cb.createQuery(Number.class);
Root<NumberEntity> number = cq.from(NumberEntity.class);
cq.select(cb.floor(number.get("bdValue")));
cq.where(cb.equal(number.get("id"), 8));
Number result = em.createQuery(cq).getSingleResult();
MatcherAssert.assertThat(result, Matchers.is(Matchers.instanceOf(BigDecimal.class)));
MatcherAssert.assertThat(result.doubleValue(), Matchers.equalTo(Math.floor(NUMBER[8].getBdValue().doubleValue())));
}
}

// Issue #1641: Returned value must match argument type for ROUND(n,1)
@Test
public void testRoundKeepBigDecimalParamType() {
try (final EntityManager em = emf.createEntityManager()) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Number> cq = cb.createQuery(Number.class);
Root<NumberEntity> number = cq.from(NumberEntity.class);
cq.select(cb.round(number.get("bdValue"), 1));
cq.where(cb.equal(number.get("id"), 8));
Number result = em.createQuery(cq).getSingleResult();
MatcherAssert.assertThat(result, Matchers.is(Matchers.instanceOf(BigDecimal.class)));
MatcherAssert.assertThat(result.doubleValue(), Matchers.equalTo(Double.valueOf(Math.round(NUMBER[8].getBdValue().doubleValue()*10))/10));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// - Issue 1442: Implement New Jakarta Persistence 3.1 Features
package org.eclipse.persistence.jpa.test.criteria;

import java.math.BigDecimal;
import java.util.List;

import jakarta.persistence.EntityManager;
Expand Down Expand Up @@ -56,12 +57,12 @@ public class TestSimpleCase {
private EntityManagerFactory emf;

private final NumberEntity[] NUMBER = {
new NumberEntity(1, 0L, 0F, 0D),
new NumberEntity(2, 3L, 10F, 10D),
new NumberEntity(3, 3L, 100F, 100D),
new NumberEntity(4, 5L, 10F, 10D),
new NumberEntity(5, 5L, 100F, 100D),
new NumberEntity(6, 5L, 1000F, 1000D)
new NumberEntity(1, 0L, 0F, 0D, BigDecimal.valueOf(0D)),
new NumberEntity(2, 3L, 10F, 10D, BigDecimal.valueOf(10D)),
new NumberEntity(3, 3L, 100F, 100D, BigDecimal.valueOf(100D)),
new NumberEntity(4, 5L, 10F, 10D, BigDecimal.valueOf(10D)),
new NumberEntity(5, 5L, 100F, 100D, BigDecimal.valueOf(100D)),
new NumberEntity(6, 5L, 1000F, 1000D, BigDecimal.valueOf(1000D))
};

@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
// - Issue 1442: Implement New Jakarta Persistence 3.1 Features
package org.eclipse.persistence.jpa.test.criteria.model;

import java.math.BigDecimal;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

Expand All @@ -33,14 +36,19 @@ public class NumberEntity {

private Double doubleValue;

@Column(precision=15, scale=10)
private BigDecimal bdValue;

public NumberEntity() {
}

public NumberEntity(final Integer id, final Long longValue, final Float floatValue, final Double doubleValue) {
public NumberEntity(final Integer id, final Long longValue,
final Float floatValue, final Double doubleValue, final BigDecimal bdValue) {
this.setId(id);
this.setLongValue(longValue);
this.setFloatValue(floatValue);
this.setDoubleValue(doubleValue);
this.setBdValue(bdValue);
}

public Integer getId() {
Expand Down Expand Up @@ -75,4 +83,12 @@ public void setDoubleValue(final Double doubleValue) {
this.doubleValue = doubleValue;
}

public BigDecimal getBdValue() {
return bdValue;
}

public void setBdValue(BigDecimal bdValue) {
this.bdValue = bdValue;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// - Issue 1442: Implement New Jakarta Persistence 3.1 Features
package org.eclipse.persistence.jpa.test.query;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

Expand Down Expand Up @@ -58,16 +59,16 @@ public class TestMathFunctions {
private EntityManagerFactory emf;

private final NumberEntity[] NUMBER = {
new NumberEntity(0, 0L, 0F, 0D),
new NumberEntity(1, 1L, 1F, 1D),
new NumberEntity(2, -1L, -1F, -1D),
new NumberEntity(3, 42L, 42.42F, 42.42D),
new NumberEntity(4, -342L, -342.42F, -342.42D),
new NumberEntity(5, 4L, 4F, 4D),
new NumberEntity(6, -4L, -4F, -4D),
new NumberEntity(7, 4L, 14.23F, 14.23D),
new NumberEntity(8, 6L, 44.7542383252F, 44.7542383252D),
new NumberEntity(9, 8L, -214.2457321233F, -214.2457321233D)
new NumberEntity(0, 0L, 0F, 0D, BigDecimal.valueOf(0D)),
new NumberEntity(1, 1L, 1F, 1D, BigDecimal.valueOf(1D)),
new NumberEntity(2, -1L, -1F, -1D, BigDecimal.valueOf(-1D)),
new NumberEntity(3, 42L, 42.42F, 42.42D, BigDecimal.valueOf(42.42D)),
new NumberEntity(4, -342L, -342.42F, -342.42D, BigDecimal.valueOf(-342.42D)),
new NumberEntity(5, 4L, 4F, 4D, BigDecimal.valueOf(4D)),
new NumberEntity(6, -4L, -4F, -4D, BigDecimal.valueOf(-4D)),
new NumberEntity(7, 4L, 14.23F, 14.23D, BigDecimal.valueOf(14.23D)),
new NumberEntity(8, 6L, 44.7542383252F, 44.7542383252D, BigDecimal.valueOf(44.7542383252D)),
new NumberEntity(9, 8L, -214.2457321233F, -214.2457321233D, BigDecimal.valueOf(-214.2457321233D))
};

@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ public Expression<Integer> sign(Expression<? extends Number> x) {
*/
@Override
public <N extends Number> Expression<N> ceiling(Expression<N> x) {
return new FunctionExpressionImpl(this.metamodel, ClassConstants.NUMBER,
return new FunctionExpressionImpl(this.metamodel, x.getJavaType(),
ExpressionMath.ceil(((InternalSelection)x).getCurrentNode()), buildList(x), "ceiling");
}

Expand All @@ -1293,7 +1293,7 @@ public <N extends Number> Expression<N> ceiling(Expression<N> x) {
*/
@Override
public <N extends Number> Expression<N> floor(Expression<N> x) {
return new FunctionExpressionImpl(this.metamodel, ClassConstants.NUMBER,
return new FunctionExpressionImpl(this.metamodel, x.getJavaType(),
ExpressionMath.floor(((InternalSelection)x).getCurrentNode()), buildList(x), "floor");
}

Expand Down Expand Up @@ -1359,7 +1359,7 @@ public Expression<Double> power(Expression<? extends Number> x, Number y) {
*/
@Override
public <T extends Number> Expression<T> round(Expression<T> x, Integer n) {
return new FunctionExpressionImpl(this.metamodel, ClassConstants.NUMBER,
return new FunctionExpressionImpl(this.metamodel, x.getJavaType(),
ExpressionMath.round(((InternalSelection)x).getCurrentNode(), n),
buildList(x, internalLiteral(n)), "round");
}
Expand Down

0 comments on commit 4181361

Please sign in to comment.