2003-01-11 00:54:24 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* execGrouping.c
|
|
|
|
* executor utility routines for grouping, hashing, and aggregation
|
|
|
|
*
|
2011-04-13 01:19:24 +02:00
|
|
|
* Note: we currently assume that equality and hashing functions are not
|
|
|
|
* collation-sensitive, so the code in this file has no support for passing
|
|
|
|
* collation settings through from callers. That may have to change someday.
|
|
|
|
*
|
2013-01-01 23:15:01 +01:00
|
|
|
* Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
|
2003-01-11 00:54:24 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/executor/execGrouping.c
|
2003-01-11 00:54:24 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "executor/executor.h"
|
Install defenses against overflow in BuildTupleHashTable().
The planner can sometimes compute very large values for numGroups, and in
cases where we have no alternative to building a hashtable, such a value
will get fed directly to BuildTupleHashTable as its nbuckets parameter.
There were two ways in which that could go bad. First, BuildTupleHashTable
declared the parameter as "int" but most callers were passing "long"s,
so on 64-bit machines undetected overflow could occur leading to a bogus
negative value. The obvious fix for that is to change the parameter to
"long", which is what I've done in HEAD. In the back branches that seems a
bit risky, though, since third-party code might be calling this function.
So for them, just put in a kluge to treat negative inputs as INT_MAX.
Second, hash_create can go nuts with extremely large requested table sizes
(notably, my_log2 becomes an infinite loop for inputs larger than
LONG_MAX/2). What seems most appropriate to avoid that is to bound the
initial table size request to work_mem.
This fixes bug #6035 reported by Daniel Schreiber. Although the reported
case only occurs back to 8.4 since it involves WITH RECURSIVE, I think
it's a good idea to install the defenses in all supported branches.
2011-05-23 18:52:46 +02:00
|
|
|
#include "miscadmin.h"
|
2003-06-23 00:04:55 +02:00
|
|
|
#include "utils/lsyscache.h"
|
2006-07-11 18:35:33 +02:00
|
|
|
#include "utils/memutils.h"
|
2003-01-11 00:54:24 +01:00
|
|
|
|
|
|
|
|
2003-08-19 03:13:41 +02:00
|
|
|
static TupleHashTable CurTupleHashTable = NULL;
|
|
|
|
|
|
|
|
static uint32 TupleHashTableHash(const void *key, Size keysize);
|
2004-08-29 07:07:03 +02:00
|
|
|
static int TupleHashTableMatch(const void *key1, const void *key2,
|
|
|
|
Size keysize);
|
2003-08-19 03:13:41 +02:00
|
|
|
|
|
|
|
|
2003-01-11 00:54:24 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Utility routines for grouping tuples together
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* execTuplesMatch
|
|
|
|
* Return true if two tuples match in all the indicated fields.
|
2003-01-12 05:03:34 +01:00
|
|
|
*
|
|
|
|
* This actually implements SQL's notion of "not distinct". Two nulls
|
|
|
|
* match, a null and a not-null don't match.
|
2003-01-11 00:54:24 +01:00
|
|
|
*
|
2005-03-16 22:38:10 +01:00
|
|
|
* slot1, slot2: the tuples to compare (must have same columns!)
|
2003-01-11 00:54:24 +01:00
|
|
|
* numCols: the number of attributes to be examined
|
|
|
|
* matchColIdx: array of attribute column numbers
|
|
|
|
* eqFunctions: array of fmgr lookup info for the equality functions to use
|
|
|
|
* evalContext: short-term memory context for executing the functions
|
|
|
|
*
|
|
|
|
* NB: evalContext is reset each time!
|
|
|
|
*/
|
|
|
|
bool
|
2005-03-16 22:38:10 +01:00
|
|
|
execTuplesMatch(TupleTableSlot *slot1,
|
|
|
|
TupleTableSlot *slot2,
|
2003-01-11 00:54:24 +01:00
|
|
|
int numCols,
|
|
|
|
AttrNumber *matchColIdx,
|
|
|
|
FmgrInfo *eqfunctions,
|
|
|
|
MemoryContext evalContext)
|
|
|
|
{
|
|
|
|
MemoryContext oldContext;
|
|
|
|
bool result;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Reset and switch into the temp context. */
|
|
|
|
MemoryContextReset(evalContext);
|
|
|
|
oldContext = MemoryContextSwitchTo(evalContext);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We cannot report a match without checking all the fields, but we can
|
|
|
|
* report a non-match as soon as we find unequal fields. So, start
|
|
|
|
* comparing at the last field (least significant sort key). That's the
|
|
|
|
* most likely to be different if we are dealing with sorted input.
|
2003-01-11 00:54:24 +01:00
|
|
|
*/
|
|
|
|
result = true;
|
|
|
|
|
|
|
|
for (i = numCols; --i >= 0;)
|
|
|
|
{
|
|
|
|
AttrNumber att = matchColIdx[i];
|
|
|
|
Datum attr1,
|
|
|
|
attr2;
|
|
|
|
bool isNull1,
|
|
|
|
isNull2;
|
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
attr1 = slot_getattr(slot1, att, &isNull1);
|
2003-01-11 00:54:24 +01:00
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
attr2 = slot_getattr(slot2, att, &isNull2);
|
2003-01-11 00:54:24 +01:00
|
|
|
|
|
|
|
if (isNull1 != isNull2)
|
|
|
|
{
|
|
|
|
result = false; /* one null and one not; they aren't equal */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNull1)
|
|
|
|
continue; /* both are null, treat as equal */
|
|
|
|
|
|
|
|
/* Apply the type-specific equality function */
|
|
|
|
|
|
|
|
if (!DatumGetBool(FunctionCall2(&eqfunctions[i],
|
|
|
|
attr1, attr2)))
|
|
|
|
{
|
|
|
|
result = false; /* they aren't equal */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldContext);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-01-12 05:03:34 +01:00
|
|
|
/*
|
|
|
|
* execTuplesUnequal
|
|
|
|
* Return true if two tuples are definitely unequal in the indicated
|
|
|
|
* fields.
|
|
|
|
*
|
|
|
|
* Nulls are neither equal nor unequal to anything else. A true result
|
|
|
|
* is obtained only if there are non-null fields that compare not-equal.
|
|
|
|
*
|
|
|
|
* Parameters are identical to execTuplesMatch.
|
|
|
|
*/
|
|
|
|
bool
|
2005-03-16 22:38:10 +01:00
|
|
|
execTuplesUnequal(TupleTableSlot *slot1,
|
|
|
|
TupleTableSlot *slot2,
|
2003-01-12 05:03:34 +01:00
|
|
|
int numCols,
|
|
|
|
AttrNumber *matchColIdx,
|
|
|
|
FmgrInfo *eqfunctions,
|
|
|
|
MemoryContext evalContext)
|
|
|
|
{
|
|
|
|
MemoryContext oldContext;
|
|
|
|
bool result;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Reset and switch into the temp context. */
|
|
|
|
MemoryContextReset(evalContext);
|
|
|
|
oldContext = MemoryContextSwitchTo(evalContext);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We cannot report a match without checking all the fields, but we can
|
|
|
|
* report a non-match as soon as we find unequal fields. So, start
|
|
|
|
* comparing at the last field (least significant sort key). That's the
|
|
|
|
* most likely to be different if we are dealing with sorted input.
|
2003-01-12 05:03:34 +01:00
|
|
|
*/
|
|
|
|
result = false;
|
|
|
|
|
|
|
|
for (i = numCols; --i >= 0;)
|
|
|
|
{
|
|
|
|
AttrNumber att = matchColIdx[i];
|
|
|
|
Datum attr1,
|
|
|
|
attr2;
|
|
|
|
bool isNull1,
|
|
|
|
isNull2;
|
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
attr1 = slot_getattr(slot1, att, &isNull1);
|
2003-01-12 05:03:34 +01:00
|
|
|
|
|
|
|
if (isNull1)
|
|
|
|
continue; /* can't prove anything here */
|
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
attr2 = slot_getattr(slot2, att, &isNull2);
|
2003-01-12 05:03:34 +01:00
|
|
|
|
|
|
|
if (isNull2)
|
|
|
|
continue; /* can't prove anything here */
|
|
|
|
|
|
|
|
/* Apply the type-specific equality function */
|
|
|
|
|
|
|
|
if (!DatumGetBool(FunctionCall2(&eqfunctions[i],
|
|
|
|
attr1, attr2)))
|
|
|
|
{
|
|
|
|
result = true; /* they are unequal */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldContext);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-01-11 00:54:24 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* execTuplesMatchPrepare
|
2003-01-12 05:03:34 +01:00
|
|
|
* Look up the equality functions needed for execTuplesMatch or
|
2007-02-06 03:59:15 +01:00
|
|
|
* execTuplesUnequal, given an array of equality operator OIDs.
|
2003-01-12 05:03:34 +01:00
|
|
|
*
|
|
|
|
* The result is a palloc'd array.
|
2003-01-11 00:54:24 +01:00
|
|
|
*/
|
|
|
|
FmgrInfo *
|
2007-01-10 19:06:05 +01:00
|
|
|
execTuplesMatchPrepare(int numCols,
|
|
|
|
Oid *eqOperators)
|
2003-01-11 00:54:24 +01:00
|
|
|
{
|
2007-01-10 19:06:05 +01:00
|
|
|
FmgrInfo *eqFunctions = (FmgrInfo *) palloc(numCols * sizeof(FmgrInfo));
|
2003-01-11 00:54:24 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < numCols; i++)
|
|
|
|
{
|
2007-01-10 19:06:05 +01:00
|
|
|
Oid eq_opr = eqOperators[i];
|
2003-01-11 00:54:24 +01:00
|
|
|
Oid eq_function;
|
|
|
|
|
2007-01-10 19:06:05 +01:00
|
|
|
eq_function = get_opcode(eq_opr);
|
|
|
|
fmgr_info(eq_function, &eqFunctions[i]);
|
2003-01-11 00:54:24 +01:00
|
|
|
}
|
|
|
|
|
2007-01-10 19:06:05 +01:00
|
|
|
return eqFunctions;
|
2003-01-11 00:54:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-06-23 00:04:55 +02:00
|
|
|
* execTuplesHashPrepare
|
|
|
|
* Look up the equality and hashing functions needed for a TupleHashTable.
|
2003-01-11 00:54:24 +01:00
|
|
|
*
|
2003-06-23 00:04:55 +02:00
|
|
|
* This is similar to execTuplesMatchPrepare, but we also need to find the
|
2007-01-10 19:06:05 +01:00
|
|
|
* hash functions associated with the equality operators. *eqFunctions and
|
|
|
|
* *hashFunctions receive the palloc'd result arrays.
|
2007-02-06 03:59:15 +01:00
|
|
|
*
|
|
|
|
* Note: we expect that the given operators are not cross-type comparisons.
|
2003-01-11 00:54:24 +01:00
|
|
|
*/
|
2003-06-23 00:04:55 +02:00
|
|
|
void
|
2007-01-10 19:06:05 +01:00
|
|
|
execTuplesHashPrepare(int numCols,
|
|
|
|
Oid *eqOperators,
|
|
|
|
FmgrInfo **eqFunctions,
|
|
|
|
FmgrInfo **hashFunctions)
|
2003-01-11 00:54:24 +01:00
|
|
|
{
|
2003-06-23 00:04:55 +02:00
|
|
|
int i;
|
2003-01-11 00:54:24 +01:00
|
|
|
|
2007-01-10 19:06:05 +01:00
|
|
|
*eqFunctions = (FmgrInfo *) palloc(numCols * sizeof(FmgrInfo));
|
|
|
|
*hashFunctions = (FmgrInfo *) palloc(numCols * sizeof(FmgrInfo));
|
2003-06-23 00:04:55 +02:00
|
|
|
|
|
|
|
for (i = 0; i < numCols; i++)
|
2003-01-11 00:54:24 +01:00
|
|
|
{
|
2007-01-10 19:06:05 +01:00
|
|
|
Oid eq_opr = eqOperators[i];
|
2003-06-23 00:04:55 +02:00
|
|
|
Oid eq_function;
|
2007-01-30 02:33:36 +01:00
|
|
|
Oid left_hash_function;
|
|
|
|
Oid right_hash_function;
|
2003-06-23 00:04:55 +02:00
|
|
|
|
2007-01-10 19:06:05 +01:00
|
|
|
eq_function = get_opcode(eq_opr);
|
2007-01-30 02:33:36 +01:00
|
|
|
if (!get_op_hash_functions(eq_opr,
|
|
|
|
&left_hash_function, &right_hash_function))
|
2003-07-21 19:05:12 +02:00
|
|
|
elog(ERROR, "could not find hash function for hash operator %u",
|
2003-06-23 00:04:55 +02:00
|
|
|
eq_opr);
|
2007-02-06 03:59:15 +01:00
|
|
|
/* We're not supporting cross-type cases here */
|
2007-01-30 02:33:36 +01:00
|
|
|
Assert(left_hash_function == right_hash_function);
|
2007-01-10 19:06:05 +01:00
|
|
|
fmgr_info(eq_function, &(*eqFunctions)[i]);
|
2007-01-30 02:33:36 +01:00
|
|
|
fmgr_info(right_hash_function, &(*hashFunctions)[i]);
|
2003-01-11 00:54:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Utility routines for all-in-memory hash tables
|
|
|
|
*
|
|
|
|
* These routines build hash tables for grouping tuples together (eg, for
|
|
|
|
* hash aggregation). There is one entry for each not-distinct set of tuples
|
|
|
|
* presented.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct an empty TupleHashTable
|
|
|
|
*
|
|
|
|
* numCols, keyColIdx: identify the tuple fields to use as lookup key
|
|
|
|
* eqfunctions: equality comparison functions to use
|
2003-06-23 00:04:55 +02:00
|
|
|
* hashfunctions: datatype-specific hashing functions to use
|
2003-08-19 03:13:41 +02:00
|
|
|
* nbuckets: initial estimate of hashtable size
|
2003-01-11 00:54:24 +01:00
|
|
|
* entrysize: size of each entry (at least sizeof(TupleHashEntryData))
|
|
|
|
* tablecxt: memory context in which to store table and table entries
|
|
|
|
* tempcxt: short-lived context for evaluation hash and comparison functions
|
|
|
|
*
|
2007-02-06 03:59:15 +01:00
|
|
|
* The function arrays may be made with execTuplesHashPrepare(). Note they
|
|
|
|
* are not cross-type functions, but expect to see the table datatype(s)
|
|
|
|
* on both sides.
|
2003-01-11 00:54:24 +01:00
|
|
|
*
|
2003-06-23 00:04:55 +02:00
|
|
|
* Note that keyColIdx, eqfunctions, and hashfunctions must be allocated in
|
|
|
|
* storage that will live as long as the hashtable does.
|
2003-01-11 00:54:24 +01:00
|
|
|
*/
|
|
|
|
TupleHashTable
|
|
|
|
BuildTupleHashTable(int numCols, AttrNumber *keyColIdx,
|
|
|
|
FmgrInfo *eqfunctions,
|
2003-06-23 00:04:55 +02:00
|
|
|
FmgrInfo *hashfunctions,
|
Install defenses against overflow in BuildTupleHashTable().
The planner can sometimes compute very large values for numGroups, and in
cases where we have no alternative to building a hashtable, such a value
will get fed directly to BuildTupleHashTable as its nbuckets parameter.
There were two ways in which that could go bad. First, BuildTupleHashTable
declared the parameter as "int" but most callers were passing "long"s,
so on 64-bit machines undetected overflow could occur leading to a bogus
negative value. The obvious fix for that is to change the parameter to
"long", which is what I've done in HEAD. In the back branches that seems a
bit risky, though, since third-party code might be calling this function.
So for them, just put in a kluge to treat negative inputs as INT_MAX.
Second, hash_create can go nuts with extremely large requested table sizes
(notably, my_log2 becomes an infinite loop for inputs larger than
LONG_MAX/2). What seems most appropriate to avoid that is to bound the
initial table size request to work_mem.
This fixes bug #6035 reported by Daniel Schreiber. Although the reported
case only occurs back to 8.4 since it involves WITH RECURSIVE, I think
it's a good idea to install the defenses in all supported branches.
2011-05-23 18:52:46 +02:00
|
|
|
long nbuckets, Size entrysize,
|
2003-01-11 00:54:24 +01:00
|
|
|
MemoryContext tablecxt, MemoryContext tempcxt)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
TupleHashTable hashtable;
|
2003-08-19 03:13:41 +02:00
|
|
|
HASHCTL hash_ctl;
|
2003-01-11 00:54:24 +01:00
|
|
|
|
|
|
|
Assert(nbuckets > 0);
|
|
|
|
Assert(entrysize >= sizeof(TupleHashEntryData));
|
|
|
|
|
Install defenses against overflow in BuildTupleHashTable().
The planner can sometimes compute very large values for numGroups, and in
cases where we have no alternative to building a hashtable, such a value
will get fed directly to BuildTupleHashTable as its nbuckets parameter.
There were two ways in which that could go bad. First, BuildTupleHashTable
declared the parameter as "int" but most callers were passing "long"s,
so on 64-bit machines undetected overflow could occur leading to a bogus
negative value. The obvious fix for that is to change the parameter to
"long", which is what I've done in HEAD. In the back branches that seems a
bit risky, though, since third-party code might be calling this function.
So for them, just put in a kluge to treat negative inputs as INT_MAX.
Second, hash_create can go nuts with extremely large requested table sizes
(notably, my_log2 becomes an infinite loop for inputs larger than
LONG_MAX/2). What seems most appropriate to avoid that is to bound the
initial table size request to work_mem.
This fixes bug #6035 reported by Daniel Schreiber. Although the reported
case only occurs back to 8.4 since it involves WITH RECURSIVE, I think
it's a good idea to install the defenses in all supported branches.
2011-05-23 18:52:46 +02:00
|
|
|
/* Limit initial table size request to not more than work_mem */
|
|
|
|
nbuckets = Min(nbuckets, (long) ((work_mem * 1024L) / entrysize));
|
|
|
|
|
2003-08-19 03:13:41 +02:00
|
|
|
hashtable = (TupleHashTable) MemoryContextAlloc(tablecxt,
|
2005-10-15 04:49:52 +02:00
|
|
|
sizeof(TupleHashTableData));
|
2003-01-11 00:54:24 +01:00
|
|
|
|
|
|
|
hashtable->numCols = numCols;
|
|
|
|
hashtable->keyColIdx = keyColIdx;
|
2007-02-06 03:59:15 +01:00
|
|
|
hashtable->tab_hash_funcs = hashfunctions;
|
|
|
|
hashtable->tab_eq_funcs = eqfunctions;
|
2003-01-11 00:54:24 +01:00
|
|
|
hashtable->tablecxt = tablecxt;
|
|
|
|
hashtable->tempcxt = tempcxt;
|
|
|
|
hashtable->entrysize = entrysize;
|
2005-10-15 04:49:52 +02:00
|
|
|
hashtable->tableslot = NULL; /* will be made on first lookup */
|
2005-03-16 22:38:10 +01:00
|
|
|
hashtable->inputslot = NULL;
|
2007-02-06 03:59:15 +01:00
|
|
|
hashtable->in_hash_funcs = NULL;
|
|
|
|
hashtable->cur_eq_funcs = NULL;
|
2003-08-19 03:13:41 +02:00
|
|
|
|
|
|
|
MemSet(&hash_ctl, 0, sizeof(hash_ctl));
|
|
|
|
hash_ctl.keysize = sizeof(TupleHashEntryData);
|
|
|
|
hash_ctl.entrysize = entrysize;
|
|
|
|
hash_ctl.hash = TupleHashTableHash;
|
|
|
|
hash_ctl.match = TupleHashTableMatch;
|
|
|
|
hash_ctl.hcxt = tablecxt;
|
Install defenses against overflow in BuildTupleHashTable().
The planner can sometimes compute very large values for numGroups, and in
cases where we have no alternative to building a hashtable, such a value
will get fed directly to BuildTupleHashTable as its nbuckets parameter.
There were two ways in which that could go bad. First, BuildTupleHashTable
declared the parameter as "int" but most callers were passing "long"s,
so on 64-bit machines undetected overflow could occur leading to a bogus
negative value. The obvious fix for that is to change the parameter to
"long", which is what I've done in HEAD. In the back branches that seems a
bit risky, though, since third-party code might be calling this function.
So for them, just put in a kluge to treat negative inputs as INT_MAX.
Second, hash_create can go nuts with extremely large requested table sizes
(notably, my_log2 becomes an infinite loop for inputs larger than
LONG_MAX/2). What seems most appropriate to avoid that is to bound the
initial table size request to work_mem.
This fixes bug #6035 reported by Daniel Schreiber. Although the reported
case only occurs back to 8.4 since it involves WITH RECURSIVE, I think
it's a good idea to install the defenses in all supported branches.
2011-05-23 18:52:46 +02:00
|
|
|
hashtable->hashtab = hash_create("TupleHashTable", nbuckets,
|
2003-08-19 03:13:41 +02:00
|
|
|
&hash_ctl,
|
2005-10-15 04:49:52 +02:00
|
|
|
HASH_ELEM | HASH_FUNCTION | HASH_COMPARE | HASH_CONTEXT);
|
2003-01-11 00:54:24 +01:00
|
|
|
|
|
|
|
return hashtable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find or create a hashtable entry for the tuple group containing the
|
2007-02-06 03:59:15 +01:00
|
|
|
* given tuple. The tuple must be the same type as the hashtable entries.
|
2003-01-11 00:54:24 +01:00
|
|
|
*
|
2003-01-12 05:03:34 +01:00
|
|
|
* If isnew is NULL, we do not create new entries; we return NULL if no
|
|
|
|
* match is found.
|
|
|
|
*
|
|
|
|
* If isnew isn't NULL, then a new entry is created if no existing entry
|
|
|
|
* matches. On return, *isnew is true if the entry is newly created,
|
|
|
|
* false if it existed already. Any extra space in a new entry has been
|
|
|
|
* zeroed.
|
2003-01-11 00:54:24 +01:00
|
|
|
*/
|
|
|
|
TupleHashEntry
|
|
|
|
LookupTupleHashEntry(TupleHashTable hashtable, TupleTableSlot *slot,
|
|
|
|
bool *isnew)
|
|
|
|
{
|
|
|
|
TupleHashEntry entry;
|
|
|
|
MemoryContext oldContext;
|
2003-08-19 03:13:41 +02:00
|
|
|
TupleHashTable saveCurHT;
|
2005-03-16 22:38:10 +01:00
|
|
|
TupleHashEntryData dummy;
|
2003-08-19 03:13:41 +02:00
|
|
|
bool found;
|
2003-01-11 00:54:24 +01:00
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
/* If first time through, clone the input slot to make table slot */
|
|
|
|
if (hashtable->tableslot == NULL)
|
|
|
|
{
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
|
|
|
|
oldContext = MemoryContextSwitchTo(hashtable->tablecxt);
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
/*
|
|
|
|
* We copy the input tuple descriptor just for safety --- we assume
|
|
|
|
* all input tuples will have equivalent descriptors.
|
|
|
|
*/
|
|
|
|
tupdesc = CreateTupleDescCopy(slot->tts_tupleDescriptor);
|
|
|
|
hashtable->tableslot = MakeSingleTupleTableSlot(tupdesc);
|
|
|
|
MemoryContextSwitchTo(oldContext);
|
|
|
|
}
|
|
|
|
|
2003-08-19 03:13:41 +02:00
|
|
|
/* Need to run the hash functions in short-lived context */
|
2003-01-11 00:54:24 +01:00
|
|
|
oldContext = MemoryContextSwitchTo(hashtable->tempcxt);
|
|
|
|
|
2003-08-19 03:13:41 +02:00
|
|
|
/*
|
|
|
|
* Set up data needed by hash and match functions
|
|
|
|
*
|
2004-08-29 07:07:03 +02:00
|
|
|
* We save and restore CurTupleHashTable just in case someone manages to
|
|
|
|
* invoke this code re-entrantly.
|
2003-08-19 03:13:41 +02:00
|
|
|
*/
|
2005-03-16 22:38:10 +01:00
|
|
|
hashtable->inputslot = slot;
|
2007-02-06 03:59:15 +01:00
|
|
|
hashtable->in_hash_funcs = hashtable->tab_hash_funcs;
|
|
|
|
hashtable->cur_eq_funcs = hashtable->tab_eq_funcs;
|
|
|
|
|
2003-08-19 03:13:41 +02:00
|
|
|
saveCurHT = CurTupleHashTable;
|
|
|
|
CurTupleHashTable = hashtable;
|
|
|
|
|
|
|
|
/* Search the hash table */
|
2005-03-16 22:38:10 +01:00
|
|
|
dummy.firstTuple = NULL; /* flag to reference inputslot */
|
2003-08-19 03:13:41 +02:00
|
|
|
entry = (TupleHashEntry) hash_search(hashtable->hashtab,
|
2005-03-16 22:38:10 +01:00
|
|
|
&dummy,
|
2003-08-19 03:13:41 +02:00
|
|
|
isnew ? HASH_ENTER : HASH_FIND,
|
|
|
|
&found);
|
|
|
|
|
|
|
|
if (isnew)
|
|
|
|
{
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
/* found pre-existing entry */
|
|
|
|
*isnew = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2005-05-29 06:23:07 +02:00
|
|
|
* created new entry
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Zero any caller-requested space in the entry. (This zaps the
|
|
|
|
* "key data" dynahash.c copied into the new entry, but we don't
|
|
|
|
* care since we're about to overwrite it anyway.)
|
2003-08-19 03:13:41 +02:00
|
|
|
*/
|
|
|
|
MemSet(entry, 0, hashtable->entrysize);
|
|
|
|
|
|
|
|
/* Copy the first tuple into the table context */
|
|
|
|
MemoryContextSwitchTo(hashtable->tablecxt);
|
2006-06-28 19:05:49 +02:00
|
|
|
entry->firstTuple = ExecCopySlotMinimalTuple(slot);
|
2003-08-19 03:13:41 +02:00
|
|
|
|
|
|
|
*isnew = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CurTupleHashTable = saveCurHT;
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldContext);
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2007-02-06 03:59:15 +01:00
|
|
|
/*
|
|
|
|
* Search for a hashtable entry matching the given tuple. No entry is
|
|
|
|
* created if there's not a match. This is similar to the non-creating
|
|
|
|
* case of LookupTupleHashEntry, except that it supports cross-type
|
|
|
|
* comparisons, in which the given tuple is not of the same type as the
|
|
|
|
* table entries. The caller must provide the hash functions to use for
|
|
|
|
* the input tuple, as well as the equality functions, since these may be
|
|
|
|
* different from the table's internal functions.
|
|
|
|
*/
|
|
|
|
TupleHashEntry
|
|
|
|
FindTupleHashEntry(TupleHashTable hashtable, TupleTableSlot *slot,
|
|
|
|
FmgrInfo *eqfunctions,
|
|
|
|
FmgrInfo *hashfunctions)
|
|
|
|
{
|
|
|
|
TupleHashEntry entry;
|
|
|
|
MemoryContext oldContext;
|
|
|
|
TupleHashTable saveCurHT;
|
|
|
|
TupleHashEntryData dummy;
|
|
|
|
|
|
|
|
/* Need to run the hash functions in short-lived context */
|
|
|
|
oldContext = MemoryContextSwitchTo(hashtable->tempcxt);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up data needed by hash and match functions
|
|
|
|
*
|
|
|
|
* We save and restore CurTupleHashTable just in case someone manages to
|
|
|
|
* invoke this code re-entrantly.
|
|
|
|
*/
|
|
|
|
hashtable->inputslot = slot;
|
|
|
|
hashtable->in_hash_funcs = hashfunctions;
|
|
|
|
hashtable->cur_eq_funcs = eqfunctions;
|
|
|
|
|
|
|
|
saveCurHT = CurTupleHashTable;
|
|
|
|
CurTupleHashTable = hashtable;
|
|
|
|
|
|
|
|
/* Search the hash table */
|
|
|
|
dummy.firstTuple = NULL; /* flag to reference inputslot */
|
|
|
|
entry = (TupleHashEntry) hash_search(hashtable->hashtab,
|
|
|
|
&dummy,
|
|
|
|
HASH_FIND,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
CurTupleHashTable = saveCurHT;
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldContext);
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2003-08-19 03:13:41 +02:00
|
|
|
/*
|
|
|
|
* Compute the hash value for a tuple
|
|
|
|
*
|
2006-06-28 19:05:49 +02:00
|
|
|
* The passed-in key is a pointer to TupleHashEntryData. In an actual hash
|
|
|
|
* table entry, the firstTuple field points to a tuple (in MinimalTuple
|
|
|
|
* format). LookupTupleHashEntry sets up a dummy TupleHashEntryData with a
|
|
|
|
* NULL firstTuple field --- that cues us to look at the inputslot instead.
|
|
|
|
* This convention avoids the need to materialize virtual input tuples unless
|
|
|
|
* they actually need to get copied into the table.
|
2003-08-19 03:13:41 +02:00
|
|
|
*
|
|
|
|
* CurTupleHashTable must be set before calling this, since dynahash.c
|
|
|
|
* doesn't provide any API that would let us get at the hashtable otherwise.
|
|
|
|
*
|
|
|
|
* Also, the caller must select an appropriate memory context for running
|
2006-06-28 19:05:49 +02:00
|
|
|
* the hash functions. (dynahash.c doesn't change CurrentMemoryContext.)
|
2003-08-19 03:13:41 +02:00
|
|
|
*/
|
|
|
|
static uint32
|
|
|
|
TupleHashTableHash(const void *key, Size keysize)
|
|
|
|
{
|
2006-06-28 19:05:49 +02:00
|
|
|
MinimalTuple tuple = ((const TupleHashEntryData *) key)->firstTuple;
|
2005-03-16 22:38:10 +01:00
|
|
|
TupleTableSlot *slot;
|
2003-08-19 03:13:41 +02:00
|
|
|
TupleHashTable hashtable = CurTupleHashTable;
|
|
|
|
int numCols = hashtable->numCols;
|
|
|
|
AttrNumber *keyColIdx = hashtable->keyColIdx;
|
2007-02-06 03:59:15 +01:00
|
|
|
FmgrInfo *hashfunctions;
|
2003-08-19 03:13:41 +02:00
|
|
|
uint32 hashkey = 0;
|
|
|
|
int i;
|
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
if (tuple == NULL)
|
|
|
|
{
|
|
|
|
/* Process the current input tuple for the table */
|
|
|
|
slot = hashtable->inputslot;
|
2007-02-06 03:59:15 +01:00
|
|
|
hashfunctions = hashtable->in_hash_funcs;
|
2005-03-16 22:38:10 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Process a tuple already stored in the table */
|
|
|
|
/* (this case never actually occurs in current dynahash.c code) */
|
|
|
|
slot = hashtable->tableslot;
|
2006-06-28 19:05:49 +02:00
|
|
|
ExecStoreMinimalTuple(tuple, slot, false);
|
2007-02-06 03:59:15 +01:00
|
|
|
hashfunctions = hashtable->tab_hash_funcs;
|
2005-03-16 22:38:10 +01:00
|
|
|
}
|
|
|
|
|
2003-01-11 00:54:24 +01:00
|
|
|
for (i = 0; i < numCols; i++)
|
|
|
|
{
|
|
|
|
AttrNumber att = keyColIdx[i];
|
|
|
|
Datum attr;
|
|
|
|
bool isNull;
|
|
|
|
|
|
|
|
/* rotate hashkey left 1 bit at each step */
|
|
|
|
hashkey = (hashkey << 1) | ((hashkey & 0x80000000) ? 1 : 0);
|
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
attr = slot_getattr(slot, att, &isNull);
|
2003-06-23 00:04:55 +02:00
|
|
|
|
|
|
|
if (!isNull) /* treat nulls as having hash key 0 */
|
|
|
|
{
|
|
|
|
uint32 hkey;
|
|
|
|
|
2007-02-06 03:59:15 +01:00
|
|
|
hkey = DatumGetUInt32(FunctionCall1(&hashfunctions[i],
|
2003-06-23 00:04:55 +02:00
|
|
|
attr));
|
|
|
|
hashkey ^= hkey;
|
|
|
|
}
|
2003-01-11 00:54:24 +01:00
|
|
|
}
|
|
|
|
|
2003-08-19 03:13:41 +02:00
|
|
|
return hashkey;
|
2003-01-11 00:54:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-08-19 03:13:41 +02:00
|
|
|
* See whether two tuples (presumably of the same hash value) match
|
|
|
|
*
|
2005-03-16 22:38:10 +01:00
|
|
|
* As above, the passed pointers are pointers to TupleHashEntryData.
|
2003-01-11 00:54:24 +01:00
|
|
|
*
|
2003-08-19 03:13:41 +02:00
|
|
|
* CurTupleHashTable must be set before calling this, since dynahash.c
|
|
|
|
* doesn't provide any API that would let us get at the hashtable otherwise.
|
|
|
|
*
|
|
|
|
* Also, the caller must select an appropriate memory context for running
|
|
|
|
* the compare functions. (dynahash.c doesn't change CurrentMemoryContext.)
|
2003-01-11 00:54:24 +01:00
|
|
|
*/
|
2003-08-19 03:13:41 +02:00
|
|
|
static int
|
|
|
|
TupleHashTableMatch(const void *key1, const void *key2, Size keysize)
|
2003-01-11 00:54:24 +01:00
|
|
|
{
|
2006-06-28 19:05:49 +02:00
|
|
|
MinimalTuple tuple1 = ((const TupleHashEntryData *) key1)->firstTuple;
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
#ifdef USE_ASSERT_CHECKING
|
2006-06-28 19:05:49 +02:00
|
|
|
MinimalTuple tuple2 = ((const TupleHashEntryData *) key2)->firstTuple;
|
2005-03-16 22:38:10 +01:00
|
|
|
#endif
|
|
|
|
TupleTableSlot *slot1;
|
|
|
|
TupleTableSlot *slot2;
|
2003-08-19 03:13:41 +02:00
|
|
|
TupleHashTable hashtable = CurTupleHashTable;
|
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
/*
|
|
|
|
* We assume that dynahash.c will only ever call us with the first
|
|
|
|
* argument being an actual table entry, and the second argument being
|
|
|
|
* LookupTupleHashEntry's dummy TupleHashEntryData. The other direction
|
|
|
|
* could be supported too, but is not currently used by dynahash.c.
|
|
|
|
*/
|
|
|
|
Assert(tuple1 != NULL);
|
|
|
|
slot1 = hashtable->tableslot;
|
2006-06-28 19:05:49 +02:00
|
|
|
ExecStoreMinimalTuple(tuple1, slot1, false);
|
2005-03-16 22:38:10 +01:00
|
|
|
Assert(tuple2 == NULL);
|
|
|
|
slot2 = hashtable->inputslot;
|
|
|
|
|
2007-02-06 03:59:15 +01:00
|
|
|
/* For crosstype comparisons, the inputslot must be first */
|
|
|
|
if (execTuplesMatch(slot2,
|
|
|
|
slot1,
|
2003-08-19 03:13:41 +02:00
|
|
|
hashtable->numCols,
|
|
|
|
hashtable->keyColIdx,
|
2007-02-06 03:59:15 +01:00
|
|
|
hashtable->cur_eq_funcs,
|
2003-08-19 03:13:41 +02:00
|
|
|
hashtable->tempcxt))
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
2003-01-11 00:54:24 +01:00
|
|
|
}
|