MagicStack / MagicStack/asyncpg

Support typed prepared statements

Open
#1,121 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
8.1k
Forks
468
PR merge metrics
No merged PRs in 30d

Description

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
```SQL
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
```SQL
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
```sql
PREPARE my_query AS
SELECT SUM(CASE WHEN v = 'A' THEN $1::int ELSE $2::int END)
FROM (VALUES ('A'), ('B')) as q0(v);
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the prepared-statement examples in PostgreSQL through asyncpg and inspect how parameter types are handled. Done means the reported SUM(text) failure is addressed by supporting typed prepared statements or a way for callers to specify parameter types, with coverage for the shown query.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.