agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Bruce Momjian <bruce@momjian.us>
To: Thomas Fanghaenel <tfanghaenel@salesforce.com>
Cc: Kevin Grittner <kgrittn@ymail.com>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Subject: Re: [SQL] Comparison semantics of CHAR data type
Date: Thu, 13 Feb 2014 21:47:01 -0500
Message-ID: <20140214024701.GA3243@momjian.us> (raw)
In-Reply-To: <20131016181711.GC18048@momjian.us>
References: <CAK+WP1xdmyswEehMuetNztM4H199Z1w9KWRHVMKzyyFM+hV=zA@mail.gmail.com>
	<20131011194437.GA3614@momjian.us>
	<1381525130.59803.YahooMailNeo@web162902.mail.bf1.yahoo.com>
	<CAK+WP1x5NOXXoh36cEcT=JRZ=_a7=53cb66TfAzMM9DnzFqURw@mail.gmail.com>
	<20131016181711.GC18048@momjian.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>

On Wed, Oct 16, 2013 at 02:17:11PM -0400, Bruce Momjian wrote:
> > > You can see the UTF8 case is fine because \n is considered greater
> > > than space, but in the C locale, where \n is less than space, the
> > > false return value shows the problem with
> > > internal_bpchar_pattern_compare() trimming the string and first
> > > comparing on lengths.  This is exactly the problem you outline, where
> > > space trimming assumes everything is less than a space.
> > 
> > For collations other than C some of those issues that have to do with
> > string comparisons might simply be hidden, depending on how strcoll()
> > handles inputs off different lengths: If strcoll() applies implicit
> > space padding to the shorter value, there won't be any visible
> > difference in ordering between bpchar and varchar values.  If strcoll()
> > does not apply such space padding, the right-trimming of bpchar values
> > causes very similar issues even in a en_US collation.

I have added the attached C comment to explain the problem, and added a
TODO item to fix it if we ever break binary upgrading.

Does anyone think this warrants a doc mention?

-- 
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + Everyone has their own god. +

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Attachments:

  [text/x-diff] char.diff (1.0K, ../20140214024701.GA3243@momjian.us/2-char.diff)
  download | inline diff:
diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c
new file mode 100644
index 502ca44..284b5d1
*** a/src/backend/utils/adt/varchar.c
--- b/src/backend/utils/adt/varchar.c
*************** bpcharcmp(PG_FUNCTION_ARGS)
*** 846,851 ****
--- 846,863 ----
  				len2;
  	int			cmp;
  
+ 	/*
+ 	 * Trimming trailing spaces off of both strings can cause a string
+ 	 * with a character less than a space to compare greater than a
+ 	 * space-extended string, e.g. this returns false:
+ 	 *		SELECT E'ab\n'::CHAR(10) < E'ab '::CHAR(10);
+ 	 * even though '\n' is less than the space if CHAR(10) was
+ 	 * space-extended.  The correct solution would be to trim only
+ 	 * the longer string to be the same length of the shorter, if
+ 	 * possible, then do the comparison.  However, changing this
+ 	 * might break existing indexes, breaking binary upgrades.
+ 	 * For details, see http://www.postgresql.org/message-id/CAK+WP1xdmyswEehMuetNztM4H199Z1w9KWRHVMKzyyFM+hV=zA@mail.gmail.com
+ 	 */
  	len1 = bcTruelen(arg1);
  	len2 = bcTruelen(arg2);
  

view thread (8+ messages)  latest in thread

Message-ID: <20140214024701.GA3243@momjian.us>
Permalink:  ../20140214024701.GA3243@momjian.us/
Also on:    postgresql.org/message-id/20140214024701.GA3243@momjian.us

reply

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: bruce@momjian.us, tfanghaenel@salesforce.com, kgrittn@ymail.com, pgsql-hackers@postgresql.org
  Subject: Re: [SQL] Comparison semantics of CHAR data type
  In-Reply-To: <20140214024701.GA3243@momjian.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox