Skip to content

fix: quote negative zero floats and decimals with a leading space - #1480

Merged
dvarrazzo merged 1 commit into
psycopg:masterfrom
arieleven:fix/negative-zero-quote
Nov 8, 2026
Merged

dvarrazzo merged 1 commit into
psycopg:masterfrom
arieleven:fix/negative-zero-quote

Conversation

@arieleven

Copy link
Copy Markdown
Contributor

Requested by Taltal · project thread

When a value is merged into the query on the client side (sql.Literal, sql.SQL.format(), ClientCursor), the float and Decimal text dumpers put a space in front of negative numbers, so that select 1-%s doesn't become select 1--1. The check is obj >= 0, which is true for -0.0 and Decimal("-0"), but their text form still starts with -:

>>> sql.SQL("UPDATE t SET x = x-{} WHERE id = {}").format(-0.0, 5).as_string()
'UPDATE t SET x = x--0.0 WHERE id = 5'

Everything after -- is then a comment, here including the WHERE clause. Same output with the Python and the C implementation. Negative zero is easy to get without noticing (-0.0 * 5, round(-0.04, 1), Decimal("-0.00") read from a file).

The fix checks the sign on the dumped text instead (value.startswith(b"-")), in _SpecialValuesDumper.quote() and in the C FloatDumper.quote() / DecimalDumper.quote(). Special values such as '-Infinity'::float8 are returned before that check, so they don't change. Integer dumpers are unchanged, since an int can't be negative zero.

Tests: added test_quote_minus_zero to tests/types/test_numeric.py. It doesn't need a database. With PSYCOPG_IMPL=python it fails on master and passes with this change. I couldn't build the C extension locally (no libpq headers), so the C side relies on CI: I checked that _psycopg.pyx still cythonizes and passes cython-lint, and the installed 3.3.8 binary wheel fails the new test as expected. isort, black, flake8, mypy and codespell pass on the touched files. I also added a news entry.

I used Claude Code to help find and fix this. I have reviewed the change and can answer questions about it.

arieleven added a commit to arieleven/psycopg that referenced this pull request Nov 5, 2026

@dvarrazzo dvarrazzo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, good catch.

Proposing a few fixes. When you are ready please squash the commits.

Comment thread docs/news.rst Resolved
Comment thread psycopg_c/psycopg_c/types/numeric.pyx Resolved
Comment thread tests/types/test_numeric.py Resolved
Comment thread psycopg_c/psycopg_c/types/numeric.pyx Resolved
Comment thread psycopg/psycopg/types/numeric.py Resolved
@arieleven

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I've shortened the news entry, folded the negative zero cases into the existing test_quote_float and test_quote_numeric, and used your comment wording (b2273aa).

On PyBytes_AS_STRING(value)[0] vs value.startswith(b"-"): I couldn't build psycopg_c locally, so I benchmarked the two checks in a standalone Cython module that copies quote() (Cython 3.3.1, clang -O2, timeit with 2M calls × 7 repeats). PyBytes_AS_STRING was consistently a little faster: 76 vs 80 ns for a positive value and 99 vs 104 ns for a negative one through the full quote path, and 30.8 vs 32.8 ns for the check alone, so the .pyx now uses it. Indexing is safe because a float or Decimal never dumps to empty text.

I'll squash the commits before merge.

@dvarrazzo
dvarrazzo force-pushed the fix/negative-zero-quote branch 2 times, most recently from 860e9be to 85ba923 Compare November 7, 2026 17:55
@dvarrazzo

Copy link
Copy Markdown
Member

Thank you for the changes. Rebased, will merge after tests pass.

Adding the space avoids that a placeholder preceded with a minus sign
such as '-%s' is converted to '--' and interpreted as a comment.
This was already done for negative numbers, but -0 failed the test.
@dvarrazzo
dvarrazzo force-pushed the fix/negative-zero-quote branch from 85ba923 to 17c5743 Compare November 8, 2026 11:34
@dvarrazzo
dvarrazzo merged commit a8348a8 into psycopg:master Nov 8, 2026
1 of 57 checks passed
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 this pull request may close these issues.

2 participants