Skip to content

Empty array should probably produce invalid sql and throw #525

Open
@RyanDurk

Description

@RyanDurk

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)```

Activity

mattbishop

mattbishop commented on Nov 26, 2022

@mattbishop

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

RyanDurk commented on Nov 26, 2022

@RyanDurk
Author

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

porsager commented on Jan 3, 2023

@porsager
Owner

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

sk-shishi commented on Jan 7, 2023

@sk-shishi
Contributor

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

porsager

porsager commented on Jan 7, 2023

@porsager
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mattbishop@porsager@RyanDurk@sk-shishi

        Issue actions

          Empty array should probably produce invalid sql and throw · Issue #525 · porsager/postgres