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

[PostgreSQL] Retrieve NUMERIC and DECIMAL as a more compatible Kotlin type #1882

Closed
veyndan opened this issue Aug 20, 2020 · 3 comments · Fixed by #3057
Closed

[PostgreSQL] Retrieve NUMERIC and DECIMAL as a more compatible Kotlin type #1882

veyndan opened this issue Aug 20, 2020 · 3 comments · Fixed by #3057

Comments

@veyndan
Copy link
Collaborator

veyndan commented Aug 20, 2020

We currently retrieve NUMERIC and DECIMAL as kotlin.Long, but this kt type doesn't correctly encapsulate NUMERIC nor DECIMAL, as both these types:

I can't think of a data type that abides by all three constraints. Exposing the type as BigDecimal would be a step in the right direction, but it doesn't support Infinity, -Infinity, or NaN. The only valid solution that I can currently think of is exposing the type as a String, and put the burden on the user to have a custom adapter that meets their constraints (for example, they can use BigDecimal if they don't care about the third constraint).

@AlecKazakova
Copy link
Collaborator

I think ideal case is we have a postgres-specific runtime which has some sort of sealed class that encapsulates all the numeric types

@tropikoder
Copy link

The key feature of the NUMERIC datatype in postgres isn't so much that it can handle huge numbers (though some folks probably use that feature), but that it is exact.

Real and double-precision types are inexact which means numbers after the decimal may change at times in unpleasantly surprising ways.

If you could add support for Numeric types that fit in a Double that would probably cover 90% of use cases. Folks who need larger numbers could have another way to adapt the value to java.

@veyndan
Copy link
Collaborator Author

veyndan commented Dec 20, 2020

@tropikoder I agree that Double would probably cover most people's use cases, so I've made a PR (#2118) as an interim solution.

In the long term, I'd probably go with something like @AlecStrong's suggestion:

sealed class Numeric {
  object PositiveInfinity : Numeric()
  object NegativeInfinity : Numeric()
  object NaN : Numeric()
  data class Finite(val value: BigDecimal) : Numeric()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants