porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.51k stars 275 forks source link

Empty array should probably produce invalid sql and throw #525

Open RyanDurk opened 1 year ago

RyanDurk commented 1 year ago

The sql call below returns an empty array to the rows variable. I'm not sure how the query is being translated exactly (is there a way to see the generated query?) but I think throwing would be more useful than gracefully returning an empty array.


const arr = []
const rows = await sql`select *
        from generate_series(1, 5) i
        where i not in ${sql(arr)};`

  await sql.end()
  console.log(rows)```
mattbishop commented 1 year ago

Postgres gives a syntax error if you try to run SELECT IN with an empty array:

select *
from generate_series(1, 5) i
where i not in ();

[42601] ERROR: syntax error at or near ")" Position: 55
RyanDurk commented 1 year ago

Postgres gives a syntax error if you try to run SELECT IN with an empty array:

select *
from generate_series(1, 5) i
where i not in ();

[42601] ERROR: syntax error at or near ")" Position: 55

Just to clarify, postgres does. Postgresjs does not, and instead returns an empty array for that query. Which is the issue.

porsager commented 1 year ago

Hi @RyanDurk .. Yeah, it might be a bit too magical that Postgres.js sends (null) if the array is empty. It's something to consider changing with v4 since it's breaking. I'm not sure if it should go, or if the behaviour should simply be documented better. What do you think?

sk-shishi commented 1 year ago

The behavior is even more surprising with bulk inserts. It just hangs when the array is empty.

porsager commented 1 year ago

@sk-shishi that sounds like a different issue. Could you post more details?