Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YoEQy-0005pS-3R for pgsql-bugs@arkaria.postgresql.org; Fri, 01 May 2015 17:09:04 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YoEQx-0008HW-BP for pgsql-bugs@arkaria.postgresql.org; Fri, 01 May 2015 17:09:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YoEQw-0008GS-KM for pgsql-bugs@postgresql.org; Fri, 01 May 2015 17:09:02 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1YoEQt-0005nu-Mj for pgsql-bugs@postgresql.org; Fri, 01 May 2015 17:09:01 +0000 Received: from pro.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id t41H8sQn011287; Fri, 1 May 2015 13:08:54 -0400 From: Tom Lane To: Robert Haas cc: Jeff Davis , Postgres-Bugs Subject: Re: Failure to coerce unknown type to specific type In-reply-to: References: <1430.1428543112@sss.pgh.pa.us> <35651.1430347071@sss.pgh.pa.us> <1430459450.2499.17.camel@jeff-desktop> Comments: In-reply-to Robert Haas message dated "Fri, 01 May 2015 08:42:45 -0400" Date: Fri, 01 May 2015 10:08:54 -0700 Message-ID: <37798.1430500134@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Robert Haas writes: > On Fri, May 1, 2015 at 1:50 AM, Jeff Davis wrote: >> => select u=t from (select 'x' as u, 'y'::text as t) s; >> ERROR: failed to find conversion function from unknown to text >> >> That's an elog (not an ereport, just plain elog) for what is a >> high-level query compilation error of a fairly sane-looking query, which >> seems wrong. > I agree. I'm not sure what the right fix is, but that query should > probably work, and if it must fail, it certainly shouldn't elog(). What really ought to happen here, IMO, is that the output columns of the sub-select ought to get resolved to non-unknown types while we are doing parse analysis of the sub-select. I believe the core reason why we haven't done this ages ago is that currently, the "right thing" will happen if you do this: insert into t (a, b) select x, 'foo' from s; namely that after the sub-select is parsed, the INSERT will reach down and coerce the unknown literal to the datatype of b. If we force the output of the sub-select to text earlier, that will stop working (or at least, it will only work in cases where there's an assignment cast from text to b's type). That's a horrid kluge, but if memory serves it's required to handle some case the SQL spec expects to work. It's conceivable that we could preserve the desirable aspects of INSERT/SELECT while eliminating "unknown" outputs from sub-selects, if INSERT were to pass down some context saying "here are the target column types I want", and then the resolution rule in SELECT target list processing would be "if an output column is UNKNOWN, coerce it to the target type provided by context if any, otherwise coerce to text". Not sure how much overhead this would add ... probably not very much, since INSERT will have to identify the target column types sooner or later. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs