Обсуждение: Re: [GENERAL] 'a' == 'a '

Поиск
Список
Период
Сортировка

Re: [GENERAL] 'a' == 'a '

От
Bruce Momjian
Дата:
Dann Corbit wrote:
> > But isn't collating sequence related to ordering?  How does this
> relate
> > to padding?
> 
> Right.  Collating sequence is how ordering is defined.  But when you
> compare two character types, they are supposed to pad according to the
> collating sequence.  So whether you blank fill or pad with some special
> character when performing a comparison is defined by the collating
> sequence and not by the character type.  Since we see (for instance)
> that bpchar(n) and varchar(n) pad differently when performing a
> comparison, we must assume that they have a different collating
> sequence.  So the question is "what is it?"
> 
> It is always possible that I have misread the standard.

OK, I understand now.  It is tempting to think that the difference
between char() and varchar() is that internally they use a different
collating sequences, but that isn't the case.  If it were, space would
be ignored during comparisons any place in the string, when in fact, is
it is only trailing space that char() ignores, e.g.:test=> SELECT 'a '::CHAR(10) = 'a'::CHAR(10); ?column?----------
t(1row)test=> SELECT 'a '::VARCHAR(10) = 'a'::VARCHAR(10); ?column?---------- f(1 row)test=> SELECT 'a'::CHAR(10) = '
a'::CHAR(10);?column?---------- f(1 row)test=> SELECT 'a'::VARCHAR(10) = ' a'::VARCHAR(10); ?column?---------- f(1
row)

Our docs already have:
http://candle.pha.pa.us/main/writings/pgsql/sgml/datatype-character.htmlValues of type character are physically padded
withspaces to thespecified width n, and are stored and displayed that way. However, thepadding spaces are treated as
semanticallyinsignificant. Trailingspaces are disregarded when comparing two values of type character, andthey will be
removedwhen converting a character value to one of theother string types. Note that trailing spaces are
semanticallysignificantin character varying and text values.
 

What additional documentation is needed?

--  Bruce Momjian                        |  http://candle.pha.pa.us pgman@candle.pha.pa.us               |  (610)
359-1001+  If your life is a hard drive,     |  13 Roberts Road +  Christ can be your backup.        |  Newtown Square,
Pennsylvania19073
 


Re: [GENERAL] 'a' == 'a '

От
Tom Lane
Дата:
Bruce Momjian <pgman@candle.pha.pa.us> writes:
> OK, I understand now.  It is tempting to think that the difference
> between char() and varchar() is that internally they use a different
> collating sequences, but that isn't the case.  If it were, space would
> be ignored during comparisons any place in the string, when in fact, is
> it is only trailing space that char() ignores, e.g.:

No, you don't understand.  The standard defines PAD SPACE as making
*trailing* spaces irrelevant to comparisons, not embedded or leading
spaces.  And they regard PAD SPACE as an attribute of a collation rather
than of the string datatype.  This seems pretty wacko to me (in
particular it's hard to see how NO PAD is useful with char(N) storage)
but that's what they did.

> What additional documentation is needed?

Some specific discussion of the relationship to the standard would be
helpful, perhaps.
        regards, tom lane


Re: [GENERAL] 'a' == 'a '

От
Bruce Momjian
Дата:
Tom Lane wrote:
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > OK, I understand now.  It is tempting to think that the difference
> > between char() and varchar() is that internally they use a different
> > collating sequences, but that isn't the case.  If it were, space would
> > be ignored during comparisons any place in the string, when in fact, is
> > it is only trailing space that char() ignores, e.g.:
> 
> No, you don't understand.  The standard defines PAD SPACE as making
> *trailing* spaces irrelevant to comparisons, not embedded or leading
> spaces.  And they regard PAD SPACE as an attribute of a collation rather
> than of the string datatype.  This seems pretty wacko to me (in
> particular it's hard to see how NO PAD is useful with char(N) storage)
> but that's what they did.

So you can have two collating sequences where in one trailing space is
significant, and another that isn't?  Strange.

> > What additional documentation is needed?
> 
> Some specific discussion of the relationship to the standard would be
> helpful, perhaps.

I guess, but explaining it seems pretty complex in itself, and I am
unsure what value it adds.

--  Bruce Momjian                        |  http://candle.pha.pa.us pgman@candle.pha.pa.us               |  (610)
359-1001+  If your life is a hard drive,     |  13 Roberts Road +  Christ can be your backup.        |  Newtown Square,
Pennsylvania19073
 


Re: [GENERAL] 'a' == 'a '

От
Andrew Dunstan
Дата:

Bruce Momjian wrote:

>>>What additional documentation is needed?
>>>      
>>>
>>Some specific discussion of the relationship to the standard would be
>>helpful, perhaps.
>>    
>>
>
>I guess, but explaining it seems pretty complex in itself, and I am
>unsure what value it adds.
>
>  
>

It will give us something to keep the language lawyers occupied :-)

cheers

andrew