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