Skip to content

Commit

Permalink
fix: Support Short type for BigDecimal conversion functions (#1746)
Browse files Browse the repository at this point in the history
* support Short type for BigDecimal conversion functions

- in case of avg aggregate functions for h2 db - it returns Short type and fail convert it to BigDecimal.

* short doesn't have toBigDecimal extension function

* Add unit test for H2 (both v1 and v2)

---------

Co-authored-by: Chantal Loncle <82039410+bog-walk@users.noreply.github.com>
  • Loading branch information
timeking and bog-walk authored Jan 18, 2024
1 parent 526b2b8 commit fea957d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ class DecimalColumnType(
}
is Long -> value.toBigDecimal()
is Int -> value.toBigDecimal()
is Short -> value.toLong().toBigDecimal()
else -> error("Unexpected value of type Decimal: $value of ${value::class.qualifiedName}")
}.setScale(scale, RoundingMode.HALF_EVEN)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ class H2Tests : DatabaseTestsBase() {
}
}

@Test
fun testH2V1WithBigDecimalFunctionThatReturnsShort() {
val testTable = object : Table("test_table") {
val number = short("number")
}

withDb(TestDB.allH2TestDB) {
try {
SchemaUtils.create(testTable)

testTable.batchInsert(listOf<Short>(2, 4, 6, 8, 10)) { n ->
this[testTable.number] = n
}

val average = testTable.number.avg()
val result = testTable.select(average).single()[average]
assertEquals("6.00".toBigDecimal(), result)
} finally {
SchemaUtils.drop(testTable)
}
}
}

class WrappedTransactionManager(val transactionManager: TransactionManager) :
TransactionManager by transactionManager

Expand Down

0 comments on commit fea957d

Please sign in to comment.