Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(DoubleColumnType): correctly handle precision when casting Float to DoubleColumnType for a real column #2115

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ class DoubleColumnType : ColumnType<Double>() {
override fun sqlType(): String = currentDialect.dataTypeProvider.doubleType()
override fun valueFromDB(value: Any): Double = when (value) {
is Double -> value
// Cast as string to prevent precision loss
is Float -> value.toString().toDouble()
is Number -> value.toDouble()
is String -> value.toDouble()
else -> error("Unexpected value of type Double: $value of ${value::class.qualifiedName}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.jetbrains.exposed.sql.tests.shared.types

import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.junit.Test

class DoubleColumnTypeTests : DatabaseTestsBase() {
object TestTable : IntIdTable("double_table") {
val amount = double("amount")
}

@Test
fun `test correctly gets data from the DB`() {
withTables(TestTable) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests passes without the fix, understandably because the create DDL is using DOUBLE PRECISION for all DB, which doesn't match your failing use case.

It's ok to leave this test as a control, but please add a second test that uses a REAL column instead, which will fail without the fix. Something like this:

@Test
fun testInsertAndSelectFromRealColumn() {
    withDb {
        val originalColumnDDL = TestTable.amount.descriptionDdl()
        val realColumnDDL = originalColumnDDL.replace(" DOUBLE PRECISION ", " REAL ")

        // create table with double() column that uses SQL type REAL
        TestTable.ddl
            .map { it.replace(originalColumnDDL, realColumnDDL) }
            .forEach { exec(it) }

        val id = TestTable.insertAndGetId {
            it[amount] = 9.23
       }

        TestTable.selectAll().where { TestTable.id eq id }.singleOrNull()?.let {
            assertEquals(9.23, it[TestTable.amount])
        }

        SchemaUtils.drop(TestTable)
    }
}

Also, please change the name of the first test to use camelCase and be a bit more descriptive, anything like for example:
testInsertAndSelectFromDoubleColumn. This naming convention more easily allows us to potentially add tests as samples in our KDocs in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah thanks for providing that test case! i wanted to do this, but couldn't find the correctly way to get the column type, thanks!! updating now!

val id = TestTable.insertAndGetId {
it[amount] = 9.23
}

TestTable.selectAll().where { TestTable.id eq id }.singleOrNull()?.let {
assertEquals(9.23, it[TestTable.amount])
}
}
}
}
Loading