Re: BUG #14268: NULL parameter conversion

From: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
To: Jordan Gigov <coladict(at)gmail(dot)com>
Cc: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, "pgsql-bugs\(at)postgresql(dot)org" <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #14268: NULL parameter conversion
Date: 2016-08-02 09:52:26
Message-ID: 874m73ilqd.fsf@news-spur.riddles.org.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

>>>>> "Jordan" == Jordan Gigov <coladict(at)gmail(dot)com> writes:

Jordan> While I don't plan on spending 180€ for the active ISO 9075
Jordan> specifications, I did find a working draft of 9075-2 where
Jordan> under section 6.13 <cast specification> in the general rules it
Jordan> says:

Jordan> "If the <cast operand> specifies NULL, then the result of CS is
Jordan> the null value and no further General Rules of this Subclause
Jordan> are applied."

"specifies NULL" is not the same thing as "has the null value" (see 2c).
By "specifies NULL" it means that the <cast operand> is an <implicitly
typed value expression> which is a <null specification> (which is the
literal token NULL). The effect of this is that CAST(NULL AS T) works
for any type T and returns the null value of that type. (The spec only
allows the typeless literal NULL in contexts from which a type can be
inferred for it.)

Nothing about this supports the idea that an expression of _known_ type
that simply happens to have the value NULL can be converted to some
other type. For example, a <value expression> which happens to have the
null value satisfies general rule 2c, but in order to get that far it
must first satisfy all of the syntax rules, including syntax rule 6
which specifies which data types are convertible. What this means is
that if x is some value (column, parameter, whatever) which happens to
be null (and its type is known, since in the spec the type of all value
expressions are known), then CAST(x AS T) is valid and returns the null
value of type T if and only if the type of x is convertible to T.

More to the point, where the type of parameter x is both known and not
assignable to the column C, then the statement

insert into T(C) values (x);

needs to generate an error _before the value of x is known_.

Note also that in pg it is legal to pass parameters of "unknown" type
such that their actual expected type is deduced from context; if one
sends a Parse for

insert into T(C) values ($1);

without specifying a known type oid for $1, then it will be deduced as
being of the type of T.C, and if the passed value is in fact the null
value then no conversion error will occur.

--
Andrew (irc:RhodiumToad)

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message eduardo_santana 2016-08-02 11:21:46 BUG #14274: Missing tablespace info in pg_tables
Previous Message Christoph Berg 2016-08-02 09:29:01 Re: pg_size_pretty, SHOW, and spaces