Re: Old question - failed to find conversion function from - Mailing list pgsql-general

From Tom Lane
Subject Re: Old question - failed to find conversion function from
Date
Msg-id 24675.1121786346@sss.pgh.pa.us
Whole thread Raw
In response to Re: Old question - failed to find conversion function from  (Richard Huxton <dev@archonet.com>)
Responses Re: Old question - failed to find conversion function from
List pgsql-general
Richard Huxton <dev@archonet.com> writes:
> [* Actually, I think NULLs are typed in SQL, which means you should be
> able to get type violations. ]

I'm pretty sure the entire construct is illegal per a strict reading of
the SQL spec --- the spec only allows NULL to appear in contexts where a
datatype can be assigned to it immediately.  Per spec you'd have to
write this as
    select 1 where 5 in (select cast(null as integer));

In the spec, NULL is not a general <expression>, it's a <contextually
typed value expression>, and those are only allowed as the immediate
argument of a CAST(), the immediate column value of an INSERT or UPDATE,
and one or two other very circumscribed cases.  SQL99 section 6.4 is
very clear about what they intend:

         2) The declared type DT of a <null specification> NS is determined
            by the context in which NS appears. NS is effectively replaced
            by CAST ( NS AS DT ).

            NOTE 70 - In every such context, NS is uniquely associated with
            some expression or site of declared type DT, which thereby
            becomes the declared type of NS.

PG's ability to infer a type for a NULL constant goes well beyond what
the spec allows --- but it does have limits.

            regards, tom lane

pgsql-general by date:

Previous
From: "Ilja Golshtein"
Date:
Subject: Re: Old question - failed to find conversion function from "unknown"
Next
From: Tom Lane
Date:
Subject: Re: Hot to restrict access to subset of data