From: Tom Lane <tgl@sss.pgh.pa.us>
To: Amit Langote <amitlangote09@gmail.com>
Cc: tneumann@users.sourceforge.net
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #12458: Comparison with CHAR is inconsistent between string types
Date: Thu, 08 Jan 2015 10:14:53 -0500
Message-ID: <22129.1420730093@sss.pgh.pa.us> (raw)
In-Reply-To: <CA+HiwqHjF2JW7E1LhK4UXEnWhOxMcMV7ke+ensode-eH8yz+cg@mail.gmail.com>
References: <20150107213932.11501.27271@wrigleys.postgresql.org>
<CA+HiwqHjF2JW7E1LhK4UXEnWhOxMcMV7ke+ensode-eH8yz+cg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Amit Langote <amitlangote09@gmail.com> writes:
> On Thu, Jan 8, 2015 at 6:39 AM, <tneumann@users.sourceforge.net> 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
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: tgl@sss.pgh.pa.us, amitlangote09@gmail.com, tneumann@users.sourceforge.net
Subject: Re: BUG #12458: Comparison with CHAR is inconsistent between string types
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>
* 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