From: Thomas Neumann <tneumann@users.sourceforge.net>
To: Tom Lane <tgl@sss.pgh.pa.us>
To: Amit Langote <amitlangote09@gmail.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #12458: Comparison with CHAR is inconsistent between string types
Date: Thu, 08 Jan 2015 17:02:43 +0100
Message-ID: <54AEAA23.7000405@users.sourceforge.net> (raw)
In-Reply-To: <22129.1420730093@sss.pgh.pa.us>
References: <20150107213932.11501.27271@wrigleys.postgresql.org>
<CA+HiwqHjF2JW7E1LhK4UXEnWhOxMcMV7ke+ensode-eH8yz+cg@mail.gmail.com>
<22129.1420730093@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
>>> 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 <comparison predicate>, 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 <space>.
""
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tneumann@users.sourceforge.net, tgl@sss.pgh.pa.us, amitlangote09@gmail.com
Subject: Re: BUG #12458: Comparison with CHAR is inconsistent between string types
In-Reply-To: <54AEAA23.7000405@users.sourceforge.net>
References: <20150107213932.11501.27271@wrigleys.postgresql.org> <CA+HiwqHjF2JW7E1LhK4UXEnWhOxMcMV7ke+ensode-eH8yz+cg@mail.gmail.com> <22129.1420730093@sss.pgh.pa.us> <54AEAA23.7000405@users.sourceforge.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox