Skip to content

Commit

Permalink
Issue #1538: Type Conversion error with the CEILING/FLOOR Operations
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 authored and lukasj committed Jun 15, 2022
1 parent e546902 commit f3b820c
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,34 @@ protected Object getObjectThroughOptimizedDataConversion(ResultSet resultSet, Da
} else if ((fieldType == ClassConstants.SHORT) || (fieldType == ClassConstants.PSHORT)) {
value = resultSet.getShort(columnNumber);
isPrimitive = (Short) value == 0;
// Sometimes field type is just Number and it's child type is stored as field.typeName
} else if (fieldType == ClassConstants.NUMBER) {
switch(field.typeName) {
case "java.lang.Byte":
value = resultSet.getByte(columnNumber);
isPrimitive = (Byte) value == 0;
break;
case "java.lang.Short":
value = resultSet.getShort(columnNumber);
isPrimitive = (Short) value == 0;
break;
case "java.lang.Integer":
value = resultSet.getInt(columnNumber);
isPrimitive = (Integer) value == 0;
break;
case "java.lang.Long":
value = resultSet.getLong(columnNumber);
isPrimitive = (Long) value == 0L;
break;
case "java.lang.Float":
value = resultSet.getFloat(columnNumber);
isPrimitive = (Float) value == 0f;
break;
case "java.lang.Double":
value = resultSet.getDouble(columnNumber);
isPrimitive = (Double) value == 0f;
break;
}
} else if ((fieldType == ClassConstants.BOOLEAN) || (fieldType == ClassConstants.PBOOLEAN)) {
value = resultSet.getBoolean(columnNumber);
isPrimitive = !((Boolean) value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ public class TestMathFunctions {
private EntityManagerFactory emf;

private final NumberEntity[] NUMBER = {
new NumberEntity(0, 0L, 0D),
new NumberEntity(1, 1L, 1D),
new NumberEntity(2, -1L, -1D),
new NumberEntity(3, 42L, 42.42D),
new NumberEntity(4, -342L, -342.42D),
new NumberEntity(5, 4L, 4D),
new NumberEntity(6, -4L, -4D),
new NumberEntity(7, 4L, 14.23D),
new NumberEntity(8, 6L, 44.7542383252D),
new NumberEntity(9, 8L, -214.2457321233D)
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)
};

@Before
Expand Down Expand Up @@ -157,7 +157,7 @@ public void testSignMethodWithNegative() {

// Call SELECT CEILING(n.doubleValue) FROM NumberEntity n WHERE n.id = id
// using CriteriaQuery
private static Double callCeiling(final EntityManager em, final int id) {
private static Double callCeilingDouble(final EntityManager em, final int id) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Double> cq = cb.createQuery(Double.class);
Root<NumberEntity> number = cq.from(NumberEntity.class);
Expand All @@ -166,38 +166,78 @@ private static Double callCeiling(final EntityManager em, final int id) {
return em.createQuery(cq).getSingleResult();
}

// Call CEILING(n) on n=0.
// Call SELECT CEILING(n.floatValue) FROM NumberEntity n WHERE n.id = id
// using CriteriaQuery
private static Float callCeilingFloat(final EntityManager em, final int id) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Float> cq = cb.createQuery(Float.class);
Root<NumberEntity> number = cq.from(NumberEntity.class);
cq.select(cb.ceiling(number.get("floatValue")));
cq.where(cb.equal(number.get("id"), id));
return em.createQuery(cq).getSingleResult();
}

// Call CEILING(n) on Double n=0.
@Test
public void testCeilingMethodWithZero() {
public void testCeilingDoubleMethodWithZero() {
try (final EntityManager em = emf.createEntityManager()) {
Double result = callCeiling(em, 0);
Double result = callCeilingDouble(em, 0);
Assert.assertEquals(Double.valueOf(0), result);
}
}

// Call CEILING(n) on n>0.
// Call CEILING(n) on Float n=0.
@Test
public void testCeilingFloatMethodWithZero() {
try (final EntityManager em = emf.createEntityManager()) {
Float result = callCeilingFloat(em, 0);
Assert.assertEquals(Float.valueOf(0), result);
}
}

// Call CEILING(n) on Double n>0.
@Test
public void testCeilingMethodWithPositive() {
public void testCeilingDoubleMethodWithPositive() {
try (final EntityManager em = emf.createEntityManager()) {
Double result = callCeiling(em, 3);
Double result = callCeilingDouble(em, 3);
Assert.assertEquals(
Double.valueOf(NUMBER[3].getLongValue().intValue()+1), result);
}
}

// Call CEILING(n) on n<0.
// Call CEILING(n) on Float n>0.
@Test
public void testCeilingMethodWithNegative() {
public void testCeilingFloatMethodWithPositive() {
try (final EntityManager em = emf.createEntityManager()) {
Double result = callCeiling(em, 4);
Float result = callCeilingFloat(em, 3);
Assert.assertEquals(
Float.valueOf(NUMBER[3].getLongValue().intValue()+1), result);
}
}

// Call CEILING(n) on Double n<0.
@Test
public void testCeilingDoubleMethodWithNegative() {
try (final EntityManager em = emf.createEntityManager()) {
Double result = callCeilingDouble(em, 4);
Assert.assertEquals(
Double.valueOf(NUMBER[4].getLongValue().intValue()), result);
}
}

// Call CEILING(n) on Float n<0.
@Test
public void testCeilingFloatMethodWithNegative() {
try (final EntityManager em = emf.createEntityManager()) {
Float result = callCeilingFloat(em, 4);
Assert.assertEquals(
Float.valueOf(NUMBER[4].getLongValue().intValue()), result);
}
}

// Call SELECT FLOOR(n.doubleValue) FROM NumberEntity n WHERE n.id = id
// using CriteriaQuery
private static Double callFloor(final EntityManager em, final int id) {
private static Double callFloorDouble(final EntityManager em, final int id) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Double> cq = cb.createQuery(Double.class);
Root<NumberEntity> number = cq.from(NumberEntity.class);
Expand All @@ -206,35 +246,75 @@ private static Double callFloor(final EntityManager em, final int id) {
return em.createQuery(cq).getSingleResult();
}

// Call FLOOR(n) on n=0.
// Call SELECT FLOOR(n.floatValue) FROM NumberEntity n WHERE n.id = id
// using CriteriaQuery
private static Float callFloorFloat(final EntityManager em, final int id) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Float> cq = cb.createQuery(Float.class);
Root<NumberEntity> number = cq.from(NumberEntity.class);
cq.select(cb.floor(number.get("floatValue")));
cq.where(cb.equal(number.get("id"), id));
return em.createQuery(cq).getSingleResult();
}

// Call FLOOR(n) on Double n=0.
@Test
public void testFloorMethodWithZero() {
public void testFloorDoubleMethodWithZero() {
try (final EntityManager em = emf.createEntityManager()) {
Double result = callFloor(em, 0);
Double result = callFloorDouble(em, 0);
Assert.assertEquals(Double.valueOf(0), result);
}
}

// Call FLOOR(n) on n>0.
// Call FLOOR(n) on Float n=0.
@Test
public void testFloorFloatMethodWithZero() {
try (final EntityManager em = emf.createEntityManager()) {
Float result = callFloorFloat(em, 0);
Assert.assertEquals(Float.valueOf(0), result);
}
}

// Call FLOOR(n) on Double n>0.
@Test
public void testFloorMethodWithPositive() {
public void testFloorDoubleMethodWithPositive() {
try (final EntityManager em = emf.createEntityManager()) {
Double result = callFloor(em, 3);
Double result = callFloorDouble(em, 3);
Assert.assertEquals(
Double.valueOf(NUMBER[3].getLongValue().intValue()), result);
}
}

// Call FLOOR(n) on n<0.
// Call FLOOR(n) on Float n>0.
@Test
public void testFloorMethodWithNegative() {
public void testFloorFloatMethodWithPositive() {
try (final EntityManager em = emf.createEntityManager()) {
Double result = callFloor(em, 4);
Float result = callFloorFloat(em, 3);
Assert.assertEquals(
Float.valueOf(NUMBER[3].getLongValue().intValue()), result);
}
}

// Call FLOOR(n) on Double n<0.
@Test
public void testFloorDoubleMethodWithNegative() {
try (final EntityManager em = emf.createEntityManager()) {
Double result = callFloorDouble(em, 4);
Assert.assertEquals(
Double.valueOf(NUMBER[4].getLongValue().intValue()-1), result);
}
}

// Call FLOOR(n) on Float n<0.
@Test
public void testFloorFloatMethodWithNegative() {
try (final EntityManager em = emf.createEntityManager()) {
Float result = callFloorFloat(em, 4);
Assert.assertEquals(
Float.valueOf(NUMBER[4].getLongValue().intValue()-1), result);
}
}

// Call SELECT EXP(n.doubleValue) FROM NumberEntity n WHERE n.id = id
// using CriteriaQuery
private static Double callExp(final EntityManager em, final int id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public class TestSimpleCase {
private EntityManagerFactory emf;

private final NumberEntity[] NUMBER = {
new NumberEntity(1, 0L, 0D),
new NumberEntity(2, 3L, 10D),
new NumberEntity(3, 3L, 100D),
new NumberEntity(4, 5L, 10D),
new NumberEntity(5, 5L, 100D),
new NumberEntity(6, 5L, 1000D)
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)
};

@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ public class NumberEntity {

private Long longValue;

private Float floatValue;

private Double doubleValue;

public NumberEntity() {
}

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

Expand All @@ -56,6 +59,14 @@ public void setLongValue(final Long longValue) {
this.longValue = longValue;
}

public Float getFloatValue() {
return floatValue;
}

public void setFloatValue(final Float floatValue) {
this.floatValue = floatValue;
}

public Double getDoubleValue() {
return doubleValue;
}
Expand Down

0 comments on commit f3b820c

Please sign in to comment.