Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 18F271337C22 for ; Wed, 13 Apr 2011 15:33:59 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 23902-07-4 for ; Wed, 13 Apr 2011 18:33:51 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 99B171337C2A for ; Wed, 13 Apr 2011 15:32:37 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p3DIWVcU009800; Wed, 13 Apr 2011 14:32:31 -0400 (EDT) To: "Jeff Wu" cc: pgsql-bugs@postgresql.org Subject: Re: BUG #5974: UNION construct type cast gives poor error message In-reply-to: <201104122018.p3CKIlWR042915@wwwmaster.postgresql.org> References: <201104122018.p3CKIlWR042915@wwwmaster.postgresql.org> Comments: In-reply-to "Jeff Wu" message dated "Tue, 12 Apr 2011 20:18:47 +0000" Date: Wed, 13 Apr 2011 14:32:31 -0400 Message-ID: <9799.1302719551@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201104/79 X-Sequence-Number: 29998 "Jeff Wu" writes: > The UNION construct (as noted on this page: > http://www.postgresql.org/docs/9.0/static/typeconv-union-case.html) will > cast unknown types to TEXT, however, if you try to do three or more UNIONs > the order in which the UNIONs are executed will cause some columns to be > cast to TEXT prematurely. The result is a type mismatch error. Really the *right* fix for this would be to resolve the common type just once across the whole nest of set operations. That wouldn't be terribly difficult from a coding standpoint, I think. The reason we haven't done it is that it looks like the SQL standard requires type resolution for set-ops to happen one pair of input relations at a time. See SQL:2008 7.13 , in which everything that's said about UNION/INTERSECT/EXCEPT is phrased in terms of exactly two input subqueries; for instance INTERSECT's result type is defined in syntax rule 18b as: The declared type of the i-th column of TR is determined by applying Subclause 9.3, "Result of data type combinations", to the declared types of the i-th column of T1 and the i-th column of T2. If anyone can think of a way to read the spec to allow subclause 9.3 to be applied to the whole set of columns at once, we could make this work less surprisingly. Or maybe we could find out that some other products do it like that despite what the spec says? regards, tom lane