MagicStack / asyncpg

A fast PostgreSQL Database Client Library for Python/asyncio.
Apache License 2.0
6.88k stars 399 forks source link

Support typed prepared statements #1121

Open mvanderlee opened 7 months ago

mvanderlee commented 7 months ago

While datatypes are optional in prepared statements, not using them can results in type errors such as

function sum(text) does not exist

This is easily reproducible in any PSQL client

PREPARE my_query AS
SELECT SUM(CASE WHEN v = 'A' THEN $1 ELSE $2 END)
FROM (VALUES ('A'), ('B')) as q0(v);
-- ERROR: function sum(text) does not exist

But when I specify the types, it works

PREPARE my_query(int, int) AS
SELECT SUM(CASE WHEN v = 'A' THEN $1 ELSE $2 END)
FROM (VALUES ('A'), ('B')) as q0(v);

EXECUTE my_query(1,0);
-- 1

DEALLOCATE my_query;

asyncpg currently does not include types, which causes me to run into the sum(text) error. I'd expect it to either include types, or allow me to specify the types somehow.

Workaround: Include a cast expression. i.e.: $1::int or CAST($1 AS INT)

Full example

PREPARE my_query AS
SELECT SUM(CASE WHEN v = 'A' THEN $1::int ELSE $2::int END)
FROM (VALUES ('A'), ('B')) as q0(v);