Re: furiously yours - Mailing list pgsql-bugs

From Stephan Szabo
Subject Re: furiously yours
Date
Msg-id Pine.BSF.4.21.0106091549250.41414-100000@megazone23.bigpanda.com
Whole thread Raw
In response to Re: furiously yours  ("Rony Khoury" <rkrk@hotmail.com>)
List pgsql-bugs
That's mostly because your c program is wrong.  If you reorder the strings
you'll see that it's not sorting at all.  It's just giving them back
in the order you gave them.

You should not compare the output from strcmp to -1.  It's <0, 0, >0 not
-1, 0, 1.  When I run the below program and add a printf of the
strcmp values, I get values like 40 and -40.   Also, you'd probably want
to be using strcoll rather than strcmp to make the comparison valid.

[As a side note, I believe this means that the results from varstr_cmp
in varlena.c are not guaranteed to be -1/0/1 as the comment purports
if strncmp doesn't return -1/+1.]

Try the following program and switch which setlocale is enabled:
#include <locale.h>

void main () {
  char tparr[10][500];
  char tpstring[500];
  int i,j;

 /* setlocale(LC_ALL, "en_US"); */
  setlocale(LC_ALL, "C");
  sprintf(tparr[0],"BOU ASSAF");
  sprintf(tparr[1],"BOU ZAHRA");
  sprintf(tparr[2],"BOUHAIDAR");

  for(i=0;i<3;i++) {
    for (j=0;j<3;j++) {
      if (strcoll(tparr[i],tparr[j])<0) {
        sprintf(tpstring,"%s",tparr[i]);
        sprintf(tparr[i],"%s",tparr[j]);
        sprintf(tparr[j],"%s",tpstring);
      }
    }
  }

  for (i=0;i<3;i++) {
    printf("\n---%s",tparr[i]);
  }
  printf("\n");
}

With C you should get:

---BOU ASSAF
---BOU ZAHRA
---BOUHAIDAR

With en_US you should get:

---BOU ASSAF
---BOUHAIDAR
---BOU ZAHRA

On Sun, 10 Jun 2001, Rony Khoury wrote:

>
> Hello again,
>
> If I understood right Postgresql depends on C language to do the sorting,
> and C language depends on the settings of redhat for the sorting task.
> Before going to RedHat with that I took the liberty to do the following test
> and I would like to take your opinion about it first.
>
> I installed a whole new redhat 7.1 version at home, with it came postgresql
> 7.0.3, I then downloaded the rpms for postgresql 7.1.1 from the internet and
> upgraded to it. After doing all the proper installation procedure, I tried
> the sorting on the new system and got the same results as at work.
>
> Now I took the liberety to write a small program in C-language to see how
> the sorting works and surprisingly enough I got the results desired that I
> used to get on the old version. (ie the space is interpreted as < A).
>
> Following is the code I wrote and the results I got. I belive this requires
> your comment before going to redhat with that, I still think that there
> might be some parameters missing somewhere to put things back in order.
>
> Lamer I checked the parameters on my system and did not find LC_ALL nor
> LC_COLL, but I found LANG=en_US. I do not know what these do, so your
> guidance is appretiated in this respect if you think this is related to the
> problem.
>
> The C-Language program is:
>
> main () {
>   char tparr[10][500];
>   char tpstring[500];
>   int i,j;
>
>   sprintf(tparr[0],"BOU ASSAF");
>   sprintf(tparr[1],"BOUHAIDAR");
>   sprintf(tparr[2],"BOU ZAHRA");
>
>   for(i=0;i<3;i++) {
>     for (j=0;j<3;j++) {
>       if (strcmp(tparr[i],tparr[j]) == -1) {
>         sprintf(tpstring,"%s",tparr[i]);
>         sprintf(tparr[i],"%s",tparr[j]);
>         sprintf(tparr[j],"%s",tpstring);
>       }
>     }
>   }
>
>   for (i=0;i<3;i++) {
>     printf("\n---%s",tparr[i]);
>   }
>   printf("\n");
> }
>
>
> and the result is
>
> ---BOU ASSAF
> ---BOU ZAHRA
> ---BOUHAIDAR
>
> while the postgresql continues to interpret this info as
>
> ---BOU ASSAF
> ---BOUHAIDAR
> ---BOU ZAHRA
>
>
> not that these results I'm getting on the same system and is all a new
> system from scratch thus can not be dependant on any previous versions.
>
> Thanks,
> Rony.
> _________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
>

pgsql-bugs by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: furiously yours
Next
From: "Michael Caine"
Date:
Subject: Re(2): REFERENCES fails on derived classes