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 · 1 comment
Open

Incorrect Decimal('Infinity') adaptation #1724

hordiienko-v opened this issue Aug 24, 2024 · 1 comment

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"

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