## Re: Hash function for numeric (WIP)

From: Tom Lane Neil Conway pgsql-patches Re: Hash function for numeric (WIP) 2007-04-27 14:02:25 14201.1177682545@sss.pgh.pa.us (view raw or whole thread) 2007-04-27 07:47:49 from Neil Conway  2007-04-27 08:09:38 from Tom Lane   2007-04-27 14:02:25 from Tom Lane    2007-04-28 22:47:10 from Neil Conway     2007-04-30 04:04:10 from Tom Lane      2007-05-04 03:03:59 from Neil Conway       2007-05-04 03:57:24 from Tom Lane        2007-05-07 01:23:05 from Neil Conway         2007-05-07 01:30:14 from Tom Lane          2007-05-07 01:51:59 from Neil Conway          2007-05-08 19:00:49 from Neil Conway           2007-05-08 19:19:37 from Tom Lane   2007-04-28 22:41:26 from Neil Conway pgsql-patches
```I wrote:
> I feel uncomfortable about this proposal because it will compute
> different hashes for values that differ only in having different
> numbers of trailing zeroes.  Now the numeric.c code is supposed to
> suppress extra trailing zeroes on output, but that's never been a
> correctness property ... are we willing to make it one?

> There are various related cases involving unstripped leading zeroes.

> Another point is that sign = NUMERIC_NAN makes it a NAN regardless
> of any other fields; ignoring the sign does not get the right result
> here.

Something else I just remembered is that ndigits = 0 makes it a zero
regardless of the weight.

Perhaps a sufficiently robust way would be to form the hash as the
XOR of each supplied digit, circular-shifted by say 3 times the
digit's weight.  This is insensitive to leading/trailing zeroes:

if (is NAN)
return -1;	// or any other fixed value
hash = 0;
shift = 3 * weight;
for (i = 0; i < ndigits; i++)
{
thisshift = (shift & 31);
hash |= ((uint32) digit[i]) << thisshift;
if (thisshift > 0)
hash |= ((uint32) digit[i]) >> (32 - thisshift);
shift -= 3;
}
return hash;

That might look pretty ugly, but then again hash_any isn't especially
cheap.

regards, tom lane

```

### pgsql-patches by date

 Next: From: Bruce Momjian Date: 2007-04-27 14:30:49 Subject: Re: New version of GENERATED/IDENTITY, was Re: parser dilemma Previous: From: Heikki Linnakangas Date: 2007-04-27 08:44:16 Subject: Re: [BUGS] BUG #3245: PANIC: failed to re-find shared lock object