|
6 | 6 | * Copyright (c) 1994, Regents of the University of California
|
7 | 7 | *
|
8 | 8 | *
|
9 |
| - * $Id: nodeHash.c,v 1.31 1999/02/13 23:15:22 momjian Exp $ |
| 9 | + * $Id: nodeHash.c,v 1.32 1999/04/07 23:33:30 tgl Exp $ |
10 | 10 | *
|
11 | 11 | *-------------------------------------------------------------------------
|
12 | 12 | */
|
@@ -41,7 +41,7 @@ extern int NBuffers;
|
41 | 41 | static int HashTBSize;
|
42 | 42 |
|
43 | 43 | static void mk_hj_temp(char *tempname);
|
44 |
| -static int hashFunc(char *key, int len); |
| 44 | +static int hashFunc(Datum key, int len, bool byVal); |
45 | 45 | static int ExecHashPartition(Hash *node);
|
46 | 46 | static RelativeAddr hashTableAlloc(int size, HashJoinTable hashtable);
|
47 | 47 | static void ExecHashOverflowInsert(HashJoinTable hashtable,
|
@@ -580,10 +580,8 @@ ExecHashGetBucket(HashJoinTable hashtable,
|
580 | 580 | * compute the hash function
|
581 | 581 | * ------------------
|
582 | 582 | */
|
583 |
| - if (execConstByVal) |
584 |
| - bucketno = hashFunc((char *) &keyval, execConstLen) % hashtable->totalbuckets; |
585 |
| - else |
586 |
| - bucketno = hashFunc((char *) keyval, execConstLen) % hashtable->totalbuckets; |
| 583 | + bucketno = hashFunc(keyval, execConstLen, execConstByVal) % hashtable->totalbuckets; |
| 584 | + |
587 | 585 | #ifdef HJDEBUG
|
588 | 586 | if (bucketno >= hashtable->nbuckets)
|
589 | 587 | printf("hash(%d) = %d SAVED\n", keyval, bucketno);
|
@@ -771,41 +769,45 @@ ExecScanHashBucket(HashJoinState *hjstate,
|
771 | 769 | * ----------------------------------------------------------------
|
772 | 770 | */
|
773 | 771 | static int
|
774 |
| -hashFunc(char *key, int len) |
| 772 | +hashFunc(Datum key, int len, bool byVal) |
775 | 773 | {
|
776 |
| - unsigned int h; |
777 |
| - int l; |
778 |
| - unsigned char *k; |
| 774 | + unsigned int h = 0; |
| 775 | + unsigned char *k; |
779 | 776 |
|
780 |
| - /* |
781 |
| - * If this is a variable length type, then 'k' points to a "struct |
782 |
| - * varlena" and len == -1. NOTE: VARSIZE returns the "real" data |
783 |
| - * length plus the sizeof the "vl_len" attribute of varlena (the |
784 |
| - * length information). 'k' points to the beginning of the varlena |
785 |
| - * struct, so we have to use "VARDATA" to find the beginning of the |
786 |
| - * "real" data. |
787 |
| - */ |
788 |
| - if (len == -1) |
789 |
| - { |
790 |
| - l = VARSIZE(key) - VARHDRSZ; |
791 |
| - k = (unsigned char *) VARDATA(key); |
792 |
| - } |
793 |
| - else |
794 |
| - { |
795 |
| - l = len; |
796 |
| - k = (unsigned char *) key; |
| 777 | + if (byVal) { |
| 778 | + /* |
| 779 | + * If it's a by-value data type, use the 'len' least significant bytes |
| 780 | + * of the Datum value. This should do the right thing on either |
| 781 | + * bigendian or littleendian hardware --- see the Datum access |
| 782 | + * macros in c.h. |
| 783 | + */ |
| 784 | + while (len-- > 0) { |
| 785 | + h = (h * PRIME1) ^ (key & 0xFF); |
| 786 | + key >>= 8; |
| 787 | + } |
| 788 | + } else { |
| 789 | + /* |
| 790 | + * If this is a variable length type, then 'k' points to a "struct |
| 791 | + * varlena" and len == -1. NOTE: VARSIZE returns the "real" data |
| 792 | + * length plus the sizeof the "vl_len" attribute of varlena (the |
| 793 | + * length information). 'k' points to the beginning of the varlena |
| 794 | + * struct, so we have to use "VARDATA" to find the beginning of the |
| 795 | + * "real" data. |
| 796 | + */ |
| 797 | + if (len == -1) |
| 798 | + { |
| 799 | + len = VARSIZE(key) - VARHDRSZ; |
| 800 | + k = (unsigned char *) VARDATA(key); |
| 801 | + } |
| 802 | + else |
| 803 | + { |
| 804 | + k = (unsigned char *) key; |
| 805 | + } |
| 806 | + while (len-- > 0) |
| 807 | + h = (h * PRIME1) ^ (*k++); |
797 | 808 | }
|
798 | 809 |
|
799 |
| - h = 0; |
800 |
| - |
801 |
| - /* |
802 |
| - * Convert string to integer |
803 |
| - */ |
804 |
| - while (l--) |
805 |
| - h = h * PRIME1 ^ (*k++); |
806 |
| - h %= PRIME2; |
807 |
| - |
808 |
| - return h; |
| 810 | + return h % PRIME2; |
809 | 811 | }
|
810 | 812 |
|
811 | 813 | /* ----------------------------------------------------------------
|
|
0 commit comments