Re: BUG #14268: NULL parameter conversion - Mailing list pgsql-bugs

From Andrew Gierth
Subject Re: BUG #14268: NULL parameter conversion
Date
Msg-id 874m73ilqd.fsf@news-spur.riddles.org.uk
Whole thread Raw
In response to Re: BUG #14268: NULL parameter conversion  (Jordan Gigov <coladict@gmail.com>)
List pgsql-bugs
>>>>> "Jordan" =3D=3D Jordan Gigov <coladict@gmail.com> writes:

 Jordan> While I don't plan on spending 180=E2=82=AC 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.

--=20
Andrew (irc:RhodiumToad)

pgsql-bugs by date:

Previous
From: Jordan Gigov
Date:
Subject: Re: BUG #14268: NULL parameter conversion
Next
From: eduardo_santana@iol.pt
Date:
Subject: BUG #14274: Missing tablespace info in pg_tables