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

Incorrect Decimal('Infinity') adaptation #1724

Open
hordiienko-v opened this issue Aug 24, 2024 · 3 comments · May be fixed by #1735
Open

Incorrect Decimal('Infinity') adaptation #1724

hordiienko-v opened this issue Aug 24, 2024 · 3 comments · May be fixed by #1735

Comments

@hordiienko-v
Copy link

  • OS: Ubuntu
  • Psycopg version: 2.9.9
  • Python version: 3.9
  • PostgreSQL version: 16.2
  • pip version: 21.2.4

Since v14, PostgreSQL NUMERIC type supports Infinity value. But for now, psycopg2 adapts Infinity as 'NaN'::numeric. This can be avoided by creating custom type adapter that wraps decimal value in quotes, but still, adapting to NaN seems like outdated logic.

from decimal import Decimal

from psycopg2 import connect
from psycopg2._psycopg import connection, cursor

if __name__ == "__main__":
    conn: connection
    curr: cursor

    with connect("dbname=test_db user=test_user password=test_user") as conn:
        with conn.cursor() as curr:
            q = curr.mogrify(
                "INSERT INTO test VALUES (%s), (%s)",
                [Decimal("1"), Decimal("Infinity")],
            )
            print(q)
            # b"INSERT INTO test VALUES (1), ('NaN'::numeric)"
@dvarrazzo
Copy link
Member

Thank you for the report. The solution is probably little enough invasive that we can provide it in the next bugfix release.

Psycopg 3 already does the right thing:

>>> import psycopg.sql
>>> from decimal import Decimal
>>> psycopg.sql.quote(Decimal("Infinity"))
"'Infinity'::numeric"

edgarrmondragon added a commit to edgarrmondragon/psycopg2 that referenced this issue Oct 11, 2024
@edgarrmondragon edgarrmondragon linked a pull request Oct 11, 2024 that will close this issue
@edgarrmondragon
Copy link
Contributor

edgarrmondragon commented Oct 11, 2024

Got a fix: #1735

@dvarrazzo
Copy link
Member

I am reconsidering the opportunity of changing this behaviour. Yes, converting inf -> nan is not correct but otoh I don't know if there are applications out there assuming it...

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

Successfully merging a pull request may close this issue.

3 participants