MagicStack/asyncpg

SELECT query to database returns wrong decimal values.

JavaProgswing opened this issue · 3 comments

  • 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

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

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.

Yep, works when casting to text.