-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
MSSQL Overflow Error on Floats in JSON #5788
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
Comments
Caused by copying from the mysql dialect here: sqlalchemy/lib/sqlalchemy/dialects/mysql/base.py Line 1460 in a8f51f3
|
so the accessor is currently called as_float() so it seems like it certainly should be casting to FLOAT for that, and luckily we are in betas for SQL Server's feature so we can just change it. @gordthompson was the DECIMAL(10, 6) to get tests to pass or can we just change that? also as_numeric(p, s) is nice as well, I wonder why that wasn't added, can do this for 1.4 too. |
ah mysql. does mysql have a bug w/ the above? |
@zzzeek - I'll have a look. |
Repro code does not fail for
|
… however, if I look at the actual value returned I see that it just failed silently: result = (
session.execute(select(Test.value[("test", "value")].as_float()))
.scalars()
.all()
)
print(result) # [9999.999999] |
Here's a patch that fixes the
|
What seems odd to me is that sqlalchemy/lib/sqlalchemy/sql/sqltypes.py Line 2471 in a8f51f3
produces an object whose |
What seems to work best on most MySQL / MariaDB versions is this:
however it does lose precision on MySQL 5.7 for the given test value "1234567.89". it seems obvious that to support precision decimals in json we need |
Mike Bayer referenced this issue: poc for as numeric https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2427 |
hey gord - the above gerrit is a short POC for as_numeric(). |
Gord Thompson has proposed a fix for this issue in the master branch: Fix issues with JSON and float/numeric https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2428 |
@zzzeek - I didn't see your change before submitting mine. I'll reconcile the two shortly. |
Thanks for the awesomely quick turn-around! BTW there appears to be a slight difference between
Just mentioning that as the fix uses Thanks! |
… personally, I always use |
I think here we are looking for precision + scale because we are looking to get a numeric value out of structured results for which we would assume these things are known. numeric/decimal is not as much of a database agnostic-decision we can make right now. folks can always use cast() manually when they want certain kinds of data on certain kinds of platforms . |
Gord Thompson has proposed a fix for this issue in the master branch: Fix issues with JSON and float/numeric https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2428 |
Describe the bug
When a
Column
is declared as JSON and a field contains a numeric value with more precision thatDecimal(10, 6)
, Microsoft SQL Sever fails with anOverflow Error
Expected behavior
Session
established, run the following code:Error
Versions.
Additional context
The issue is caused by the cast on that line: https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2118/19/lib/sqlalchemy/dialects/mssql/base.py#2045
Using
FLOAT
instead ofDECIMAL(10, 6)
fixes the issue at the expense of some conversion and potential loss of precision - but it does not cause an error.Note: Having a
as_numeric(p,s)
in the JSON implementation would be nice. So that we could generate a cast asFLOAT
or asDECIMAL(p, s)
depending on whatas_xxxx
as used.Thanks!
The text was updated successfully, but these errors were encountered: