Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y9Enm-0006Lm-18 for pgsql-bugs@arkaria.postgresql.org; Thu, 08 Jan 2015 15:15:10 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y9Enl-000533-HQ for pgsql-bugs@arkaria.postgresql.org; Thu, 08 Jan 2015 15:15:09 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y9Enj-00051k-CT for pgsql-bugs@postgresql.org; Thu, 08 Jan 2015 15:15:07 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y9EnZ-00017z-Tj for pgsql-bugs@postgresql.org; Thu, 08 Jan 2015 15:15:05 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id t08FErNP022130; Thu, 8 Jan 2015 10:14:53 -0500 From: Tom Lane To: Amit Langote cc: tneumann@users.sourceforge.net, pgsql-bugs@postgresql.org Subject: Re: BUG #12458: Comparison with CHAR is inconsistent between string types In-reply-to: References: <20150107213932.11501.27271@wrigleys.postgresql.org> Comments: In-reply-to Amit Langote message dated "Thu, 08 Jan 2015 17:07:38 +0900" Date: Thu, 08 Jan 2015 10:14:53 -0500 Message-ID: <22129.1420730093@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 Amit Langote writes: > On Thu, Jan 8, 2015 at 6:39 AM, wrote: >> The following two queries return different results: >> >> select 'A'::char(1)='A '::text; >> select 'A'::char(1)='A '::varchar(10); >> >> I would expect both queries to return "true", and this is also suggested by >> the documentation (section 8.3.), but the text comparison returns false. I see nothing in 8.3 addressing the question of which type has precedence for cross-type comparisons. > Quick inspection of pg_operator suggests there is no '=' operator that > takes varchar as either of the operands. Not sure why that is so. It > appears the comparison proceeds using =(char, char) operator with the > varchar operand appropriately coerced. Hence the result. The available operators are char=char and text=text (varchar has no operators of its own, it just uses text's operators). For an input of the form char=text, the text=text operator wins on the grounds of text being a preferred type; see http://www.postgresql.org/docs/9.4/static/typeconv-oper.html rule 3d. So we coerce char to text and compare using text's rules, in which trailing spaces are significant. But if the input is char=varchar, only the char=char operator has any exact matches, so it wins at rule 3c. Then we'll coerce varchar to char and compare using char's rules, in which trailing spaces are not significant. So it's behaving as expected and documented. Whether this behavior is desirable is a different question of course. I read the SQL spec as requiring us to use PAD SPACE semantics for char=varchar comparisons, so that case is forced. char=text is outside the bounds of the spec (since text isn't in the spec) so we don't have to do it the same way; and we choose not to because text's semantics are generally more natural. 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