-
Notifications
You must be signed in to change notification settings - Fork 47
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
An option to force decimals to convert to numeric instead of character? #92
Comments
Duplicate of #90. I guess an option to |
It would be great to have the option to avoid conversion to character via e.g. options(). I understand why this change was made but it breaks a bunch of production code in our organisation and we are currently unable to update RJDBC. We use Oracle where the default integer is unfortunately numeric(38,0)... :) |
@TuomoNieminen Interesting, So there are two possible approaches:
In addition, using |
@TuomoNieminen I think I have an idea how we can detect the representability depending how the driver stores the results. Can you, please, run the following test on your database: Connect to it, (attach the q <- dbSendQuery(conn, "SELECT myBigNumber FROM myTable;") Then run the following code: q@md$getColumnClassName(1L) If the result is q@jr$`next`()
o = q@jr$getObject(1L)
o$precision()
o$scale() In theory, if the native representation by the driver is (That said, note that you'll make everything way more efficient in Oracle if you use |
Thanks! We ran the code you suggested and the Oracle "integer's" native representation by the driver is BigDecimal and individual precision and scale could be read with your code. |
We are hit by this big time as well. We have a large application and updating the code to convert every single column from decimal to double is not feasible. We could convert our whole database schema to only contain double columns, but we do have some joins on some decimal columns (mostly latitude and longitude, which could stay decimal because the precision is less than 15) and those would work slightly worse if we had double columns. |
@Stivo Can you please answer my question above, and paste the output here (the precision and size calls)? I never got an answer so I couldn't do what I proposed (check if the precision fits and convert if it does) since I don't have access to an Oracle database. |
We are not working with oracle. I can tell you that our decimal types in SQL server are at maximum (38, 2) or (38, 37) and what the current behaviour is and the old one.
Old behaviour:
|
This of course also happens in all versions and with decimals with a precision as low as 2. See this example:
Prints this: This is just not avoidable when converting from bigdecimal to double. So I really don't see a reason why you do allow this for small values but as soon as the column type becomes larger than precision 15 you say this is not exact enough. |
@Stivo I think you may be misunderstanding the issue - the conversion is not lossy at small values, because the declared precision is maintained, i.e., with 15 digits precision the IEEE FP representation is exact: > print(0.1, digits=15)
[1] 0.1 Anything beyond that is not, because it exceeds the precision that an IEEE double can represent in general (because of the 53-bit significand). Similarly, printing anything beyond 15 digits is meaningless as you illustrated: > print(0.1, digits=20)
[1] 0.10000000000000000555 because the representation does not store those digits (for most numbers). However, this means that converting between decimal and FP double representation is guaranteed to be lossless up to 15 digits, but not beyond. That's where the the rule comes from. But back to the above question - if you want to allow lossy conversion you have now two options:
|
I ran into this problem with DECIMAL(19,8) and found that setting the numeric type map didn’t work in my case, when using 0.2-10. A little debugging showed that this is because when using JTDS with SQL server, reading DECIMAL(19,8), I found that the type returned in the meta data is decimal, rather than numeric. As such, adding a DECIMAL type map solved the problem. For good measure, I also added a type map for NUMERIC, as per above. RJDBC:::dbSetTypeMaps(NUMERIC=function(x) as.numeric(x))
RJDBC:::dbSetTypeMaps(DECIMAL=function(x) as.numeric(x)) |
Hi all -- thank you for your great software!
We have been using RJDBC 0.2-8 with MS SQL Server backend; unfortunately we recently ran into issues trying to upgrade to 0.2-10 (off CRAN): in many cases decimal values that were previously converted to numeric are now returned as character. Most likely this is related to this change in 0.2-9: be457e1.
In our particular case we are fine with the potential loss of precision, but since this is a large code base with many DB accesses etc., changing all the queries and SPs to add explicit casts to floats is not currently realistic, so we're stuck with 0.2-8.
Is there an easier way to force decimal values to return as numeric (perhaps with some option setting?), or if not, would you consider that as a feature request?
Thanks! :)
The text was updated successfully, but these errors were encountered: