Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y9FY1-0008Kf-4L for pgsql-bugs@arkaria.postgresql.org; Thu, 08 Jan 2015 16:02:57 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y9FY0-0007dT-Jb for pgsql-bugs@arkaria.postgresql.org; Thu, 08 Jan 2015 16:02:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y9FXz-0007bs-8g for pgsql-bugs@postgresql.org; Thu, 08 Jan 2015 16:02:55 +0000 Received: from cl-3144.ham-01.de.sixxs.net ([2001:6f8:900:c47::2] helo=tneu02.synserver.de) by magus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y9FXt-0007ZK-3v for pgsql-bugs@postgresql.org; Thu, 08 Jan 2015 16:02:54 +0000 Received: from [131.159.16.195] (deskkemper01.informatik.tu-muenchen.de [131.159.16.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by tneu02.synserver.de (Postfix) with ESMTPSA id B620E402D3; Thu, 8 Jan 2015 17:02:44 +0100 (CET) Message-ID: <54AEAA23.7000405@users.sourceforge.net> Date: Thu, 08 Jan 2015 17:02:43 +0100 From: Thomas Neumann User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: Tom Lane , Amit Langote CC: pgsql-bugs@postgresql.org Subject: Re: BUG #12458: Comparison with CHAR is inconsistent between string types References: <20150107213932.11501.27271@wrigleys.postgresql.org> <22129.1420730093@sss.pgh.pa.us> In-Reply-To: <22129.1420730093@sss.pgh.pa.us> Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: 7bit 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 >>> 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. you are right, I did not read 8.3 careful enough. It says that "trailing spaces are treated as semantically insignificant and disregarded when comparing two values of type character", but nothing about mixed types. The SQL standard 8,2 , Rule 3, b) says that "" If the length in characters of X is not equal to the length in characters of Y, then the shorter string is effectively replaced, for the purposes of comparison, with a copy of itself that has been extended to the length of the longer string by concatenation on the right of one or more pad characters, where the pad character is chosen based on CS. If CS has the NO PAD characteristic, then the pad character is an implementation-dependent character different from any character in the character set of X and Y that collates less than any string under CS. Otherwise, the pad character is a . "" which would seem to suggest that spaces have to be ignore when comparing CHAR with anything else (as the other string type is free to choose any pad character, but CHAR will always pad with spaces as needed). But of course you can deviate from that if you wish. I just found it surprising that text behaved different from varchar, which can lead to very subtle bugs. Thomas -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs