pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Philip Semanchuk <philip@americanefficient.com>
To: Roman Cervenak <roman@cervenak.info>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: IN clause behaving badly with missing comma and line break
Date: Wed, 18 Jan 2023 10:20:57 -0500
Message-ID: <1E97D89B-3195-4A55-918B-2C1152EEE497@americanefficient.com> (raw)
In-Reply-To: <CAGjExY3rn0YEYnCiRkJ+R9czsmz6Fa4xkYaOaY35x6B-UC_53A@mail.gmail.com>
References: <CAGjExY3rn0YEYnCiRkJ+R9czsmz6Fa4xkYaOaY35x6B-UC_53A@mail.gmail.com>



> On Jan 18, 2023, at 2:51 AM, Roman Cervenak <roman@cervenak.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




view thread (5+ messages)  latest in thread

Message-ID: <1E97D89B-3195-4A55-918B-2C1152EEE497@americanefficient.com>
Permalink:  ../1E97D89B-3195-4A55-918B-2C1152EEE497@americanefficient.com/
Also on:    postgresql.org/message-id/1E97D89B-3195-4A55-918B-2C1152EEE497@americanefficient.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: philip@americanefficient.com, roman@cervenak.info
  Subject: Re: IN clause behaving badly with missing comma and line break
  In-Reply-To: <1E97D89B-3195-4A55-918B-2C1152EEE497@americanefficient.com>
  References: <CAGjExY3rn0YEYnCiRkJ+R9czsmz6Fa4xkYaOaY35x6B-UC_53A@mail.gmail.com> <1E97D89B-3195-4A55-918B-2C1152EEE497@americanefficient.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox