Re: weird error message

From: Michael Moore <michaeljmoore(at)gmail(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>, postgres list <pgsql-sql(at)postgresql(dot)org>
Subject: Re: weird error message
Date: 2016-05-06 15:31:32
Message-ID: CACpWLjO6ouACcuapXyGakCKZcO4OhmHs-7gtU_yv9GOsPSA+nQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Love it! Thanks David.

On Fri, May 6, 2016 at 8:22 AM, David G. Johnston <
david(dot)g(dot)johnston(at)gmail(dot)com> wrote:

> On Fri, May 6, 2016 at 6:53 AM, Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
> wrote:
>
>>
>>
>> hplc=> SELECT COALESCE(dt, i) FROM (SELECT null::text AS dt, null::text
>> AS i) q;
>> coalesce
>> ----------
>>
>> (1 row)
>>
>>
>> hplc=> SELECT COALESCE(dt, i) FROM (SELECT null AS dt, null AS i) q;
>> ERROR: failed to find conversion function from unknown to text
>>
>>
>> So it is not the conversion from NULL to text per se, just when it is
>> done on the output of a derived table. I don't why that is, maybe someone
>> else can chime in.
>>
>
> ​The message would be more accurately written "failed to find implicit
> conversion function from unknown to text".
>
> ​The answer is partially given by #3 in the type conversion documentation:
>
> ​http://www.postgresql.org/docs/9.5/interactive/typeconv-union-case.html
> """
> 3. If all inputs are of type unknown, resolve as type text (the preferred
> type of the string category).
> ​"""
>
> However, the conversion from unknown to text can only happen implicitly if
> the unknown type has not been "locked" due to the value in question being
> passed up from a subquery. The distinction is one I personally call
> "untyped" versus "unknown (typed)". An "untyped" value has an unknown type
> but it can be implicitly cast to any other type. If it ends up being cast
> so that it is "unknown typed" further implicit casts are not allowed -
> there are no implicit casts in the system from "unknown typed".
>
> So dt and i become unknown typed and then passed through the case which
> chooses text (per #3) as the common type but fails when it goes looking for
> an implicit cast from unknown type to text. Removing the subquery the case
> succeeded because the nulls are simply untyped.
>
> I don't know that this is covered all that well in the documentation.
> I've been meaning to write a patch to add substantially what I've written
> above (I've made this similar response a number of times now) but haven't
> gotten around to it yet. It doesn't come up that often and when it does it
> is confusion but hardly problematic.
>
> David J.
>
>
>

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2016-05-06 16:35:24 Re: weird error message
Previous Message David G. Johnston 2016-05-06 15:22:05 Re: weird error message