pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Rémi Aubel <remi.aubel@gmail.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG: Unable to bind a null value typed as a UUID in a PreparedStatement
Date: Wed, 28 Mar 2018 10:30:50 -0400
Message-ID: <20457.1522247450@sss.pgh.pa.us> (raw)
In-Reply-To: <CAG2M1fdAFD3N8vZxp4TYLEYz4Rwu5f94ghafEErvHv387oWf8g@mail.gmail.com>
References: <CAG2M1fdAFD3N8vZxp4TYLEYz4Rwu5f94ghafEErvHv387oWf8g@mail.gmail.com>

=?UTF-8?Q?R=C3=A9mi_Aubel?= <remi.aubel@gmail.com> writes:
> I need to bind a UUID parameter which may be null in a statement like
> "select * from test table where ? is null or ? = c_uuid".
> Whatever approach I use, the driver rejects my request with "ERROR: could
> not determine data type of parameter $1".

Some experimentation suggests that it'd probably work if you wrote the
clauses in the other order:

regression=# create table test_table (c_uuid uuid);
CREATE TABLE
regression=# prepare foo as select * from test_table where $1 is null or $1 = c_uuid;
ERROR:  could not determine data type of parameter $1
LINE 1: prepare foo as select * from test_table where $1 is null or ...
                                                      ^
regression=# prepare foo as select * from test_table where $1 = c_uuid or $1 is null;
PREPARE

In an ideal world, perhaps the order of the parameter references would not
matter, but AFAICS making that work would be mighty hard.  For now, PG's
parser wants to resolve the type of an otherwise-unlabeled parameter
symbol the first time it sees it --- and the context "IS NULL" offers
no clue what type it should be.

Alternatively, you could force the issue with an explicit cast in the
text of the query:

regression=# prepare foo2 as select * from test_table where $1::uuid is null or $1 = c_uuid;
PREPARE

			regards, tom lane




view thread (12+ messages)  latest in thread

Message-ID: <20457.1522247450@sss.pgh.pa.us>
Permalink:  ../20457.1522247450@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/20457.1522247450@sss.pgh.pa.us

 · 

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: tgl@sss.pgh.pa.us, remi.aubel@gmail.com
  Subject: Re: BUG: Unable to bind a null value typed as a UUID in a PreparedStatement
  In-Reply-To: <20457.1522247450@sss.pgh.pa.us>
  References: <CAG2M1fdAFD3N8vZxp4TYLEYz4Rwu5f94ghafEErvHv387oWf8g@mail.gmail.com> <20457.1522247450@sss.pgh.pa.us>

* 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