Re: IN clause behaving badly with missing comma and line break

From: Philip Semanchuk <philip(at)americanefficient(dot)com>
To: Roman Cervenak <roman(at)cervenak(dot)info>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: IN clause behaving badly with missing comma and line break
Date: 2023-01-18 15:20:57
Message-ID: 1E97D89B-3195-4A55-918B-2C1152EEE497@americanefficient.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

> On Jan 18, 2023, at 2:51 AM, Roman Cervenak <roman(at)cervenak(dot)info> wrote:
>
> Hello,
> if you use IN() clause (I've tested in WHERE and CASE) and you forget comma between values, it is syntax error (correct behaviour). But if there is newline between those two values, suddenly it is not syntax error (query will run successfully), and all values are simply ignored.
>
> Examples:
>
> WITH sample AS (SELECT 'c' AS t) SELECT CASE WHEN t IN ('a','b') THEN 1 WHEN t IN ('c'
> 'd') THEN 2 END FROM sample;
>
> WITH sample AS (SELECT 'c' AS t)
> SELECT * FROM sample WHERE t IN ('a'
> 'c’);

As David said, this is expected behavior, although it’s subtle and easy to overlook. We’ve adopted a formatting standard that helps us to avoid surprises due to missing commas. We put one string literal on each line and place the commas all in the same column with a significant amount of white space to the left of the commas. With this safeguard in place, it’s very easy to spot a missing comma.

WHERE t IN ('a' ,
'foo' ,
'bar' ,
)

Cheers
Philip

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Francisco Olarte 2023-01-18 16:06:21 Re: IN clause behaving badly with missing comma and line break
Previous Message Alex Richman 2023-01-18 11:16:57 Re: Logical Replica ReorderBuffer Size Accounting Issues