Skip to content

SELECT query to database returns wrong decimal values. #1160

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

Closed
JavaProgswing opened this issue Jun 14, 2024 · 3 comments
Closed

SELECT query to database returns wrong decimal values. #1160

JavaProgswing opened this issue Jun 14, 2024 · 3 comments

Comments

@JavaProgswing
Copy link

JavaProgswing commented Jun 14, 2024

  • asyncpg version: 0.29.0
  • PostgreSQL version: 16.2
  • Python version: 3.11.9

I have this postgres table:

    version float4,
    releaseurl varchar(128),
    releaseinfo varchar(128),
    timestamp int4,

where version is the primary key.

I use this select request to fetch the latest version
SELECT version, releaseinfo, timestamp FROM ... ORDER BY version DESC LIMIT 1

it returns me 3.0999999046325684 in asyncpg python.
yet the database has no such value, I was guessing perhaps a precision error on that and it was getting 3.1.

Cuz the database does have 3.1 and when i use the same query on java JDBC, it does return 3.1

@thomas-mckay
Copy link

thomas-mckay commented Jun 20, 2024

Probably a precision error when converting to a Python float: https://magicstack.github.io/asyncpg/current/usage.html#id3

Try casting to decimal in your query, see if it changes.

On a side-note, though related if I understand your table correctly, I'd suggest using a fixed-length array of integers for version numbers. Even if you're absolutely sure you'll never have values like 3.14.2, a version number is not a float (or a decimal). And comparison becomes natural with an array of ints:

  • [3, 14] > [3, 5]
  • 3.14 < 3.5

@JavaProgswing
Copy link
Author

Oh yep having a list of integers is definitely better. I'll do that.

I'll try casting and see if it works. Cuz was working normally in a other python library.

@JavaProgswing
Copy link
Author

Yep, works when casting to text.

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

No branches or pull requests

2 participants