Update execGrouping.cpp

This commit is contained in:
bjyb 2023-09-26 14:48:12 +08:00
parent 1a882a9ee6
commit 76815dc009
1 changed files with 112 additions and 0 deletions

View File

@ -48,6 +48,25 @@ static int TupleHashTableMatch(const void* key1, const void* key2, Size keysize)
*
* NB: evalContext is reset each time!
*/
/*The main purpose of this code is to compare whether two Tuple are equal.
In database, tuple is a basic data structure, which is used to store a series of related values.
The function execTuplesMatch receives two TupleTableSlot pointers (slot1 and slot2),
which point to the tuple to be compared, as well as the number of columns (numCols),
the matching column index (matchColIdx), the equation functions (eqfunctions) and an evalContext.
It first switches to a temporary memory context (evalContext), and then loops through each column,
starting with the last column (the least important sort key). This is because the last column
is most likely to be different when processing sorted input.
For each column, it gets the property values in two tuples and checks whether they are empty.
If one is empty and the other is not, they are not equal, and the function sets the result to false
and jumps out of the loop. If both of them are empty, they are regarded as equal and continue the next cycle.
If both attributes are not empty, then a specific type of equality function will be used to compare whether
they are equal. If not, the function sets the result to false and jumps out of the loop.
Finally, the function switches back to the old memory context and returns the result. If all columns match,
the function will return true, otherwise it will return false.*/
bool execTuplesMatch(TupleTableSlot* slot1, TupleTableSlot* slot2, int numCols, AttrNumber* matchColIdx,
FmgrInfo* eqfunctions, MemoryContext evalContext)
{
@ -166,6 +185,26 @@ bool execTuplesUnequal(TupleTableSlot* slot1, TupleTableSlot* slot2, int numCols
*
* The result is a palloc'd array.
*/
/*The main purpose of this code is to generate an array of function information for
each pair of equality operators for subsequent tuple comparison.
The function execTuplesMatchPrepare takes the number of columns (numCols) and
the array of equality eqOperators (`eq operators`) as parameters.
First, it uses `p palloc to allocate memory for the function information array,
and the length of the array is the number of columns.
Then, it enters a loop, and each iteration in the loop corresponds to a column.
For each column, it gets the equality operator (` eq _ opr`) and the corresponding
function (` eq _ function`). This is done by calling the get_opcode function.
Next, it uses the fmgr_info function to fill the corresponding position
of the function information array.
Finally, the function returns the generated function information array.
This function is usually called before performing tuple matching to
prepare the required function information.*/
FmgrInfo* execTuplesMatchPrepare(int numCols, Oid* eqOperators)
{
FmgrInfo* eqFunctions = (FmgrInfo*)palloc(numCols * sizeof(FmgrInfo));
@ -192,6 +231,26 @@ FmgrInfo* execTuplesMatchPrepare(int numCols, Oid* eqOperators)
*
* Note: we expect that the given operators are not cross-type comparisons.
*/
/*The purpose of this code is to prepare equality function and hash function for tuple hash table.
The' executtupleshahprep' function receives four parameters: number of columns ('numCols'),
equality operator array ('eqOperators'), equality function array ('eqFunctions') and hash function array ('hashFunctions').
First, the function allocates memory for the array of equality functions and hash functions.
Then, it enters a loop, and each iteration in the loop corresponds to a column. For each column,
it gets the equality operator (` eq _ opr`) and the corresponding function (` eq _ function`). This is done by calling the get_opcode function.
Next, it tries to get the hash function of the equality operator. If the hash function cannot be found,
it will report an error and call the `ereport' function, which will send the error information to the error handling system of PostgreSQL.
Then, it asserts that the left and right hash functions are the same, which
means that it does not support cross-type cases.
Finally, it uses the fmgr_info function to fill the corresponding positions
of the array of equality functions and hash functions.
This function is usually called before performing tuple hashing to prepare the required function information.*/
void execTuplesHashPrepare(int numCols, Oid* eqOperators, FmgrInfo** eqFunctions, FmgrInfo** hashFunctions)
{
int i;
@ -248,6 +307,29 @@ void execTuplesHashPrepare(int numCols, Oid* eqOperators, FmgrInfo** eqFunctions
* Note that keyColIdx, eqfunctions, and hashfunctions must be allocated in
* storage that will live as long as the hashtable does.
*/
/*The main purpose of this code is to create a TupleHashTable, which is a data structure for storing tuples,
which is the basic data structure for storing a series of related values in the database.
The function' BuildTupleHashTable' receives a series of parameters, including the number of columns ('numCols'),
key column index ('keyColIdx'), equation function ('eqfunctions'), hash function ('hashfunctions'), number of buckets ('nbuckets'), entrysize ('entrysize').
First, the function checks whether the number of buckets and the entry size are valid. Then,
it limits the request for the initial table size according to the working memory.
Then, it allocates memory in the table context to store TupleHashTableData.
Then, it sets various fields, including column number, key column index, hash function,
equality function, table context, temporary context, entry size, etc.
Then, it clears the memory of the hash_ctl structure and sets its various fields, including key size,
entry size, hash function, matching function and hash context.
Finally, it creates a hashtable using the hash_create function and stores it in the hashtab' field of `hashtable'.
Function returns the created ` hashtable'.
This function is usually called when creating a tuple hash when executing a database query,
and is used to prepare the required data structure.*/
TupleHashTable BuildTupleHashTable(int numCols, AttrNumber* keyColIdx, FmgrInfo* eqfunctions, FmgrInfo* hashfunctions,
long nbuckets, Size entrysize, MemoryContext tablecxt, MemoryContext tempcxt, int workMem)
{
@ -309,6 +391,21 @@ TupleHashTable BuildTupleHashTable(int numCols, AttrNumber* keyColIdx, FmgrInfo*
* hash table if it is new
*
*/
/*The main purpose of this code is to find the TupleTableSlot in the TupleHashTable and insert it as needed.
TupleHashTable is a data structure used to store tuples, which are the basic data structures used to store a series of related values in the database.
The function LookupTupleHashEntry receives four parameters: a TupleHashTable(`hashtable), a TupleTableSlot pointer (`slot`),
a Boolean pointer (isnew) and a Boolean value (isinserthashtbl).
The function first checks whether it is the first time to pass, and if it is, it will clone
the input time slot to make the table time slot.
Then, the function switches to the temporary context, sets the hash and the data needed
by the matching function, and saves the current tuple hash table.
Next, it searches the hash table. If' isinserthashtbl' is true, it will search the hash table and return the found entry
if it is found; If it is not found and' isnew' is not NULL, set' isnew' to true, indicating a new entry. If' isinserthashtbl' is false,
it will only search the hash table, and if it is found, it will return the found entry; If it is not found, it will create a new table.*/
TupleHashEntry LookupTupleHashEntry(TupleHashTable hashtable, TupleTableSlot* slot, bool* isnew, bool isinserthashtbl)
{
TupleHashEntry entry;
@ -510,6 +607,21 @@ static uint32 TupleHashTableHash(const void* key, Size keysize)
* Also, the caller must select an appropriate memory context for running
* the compare functions. (dynahash.c doesn't change CurrentMemoryContext.)
*/
/*This code is used to process a part of tuple hash table, which is a data
structure used to store and retrieve tuple data in PostgreSQL.
The function TupleHashTableMatch' is a comparison function, which is used to compare
whether two tuples are equal. This function is designed to be used with dynahash.c library,
which is a general hash table library and can be used to store and retrieve data.
The function receives three parameters: key1, key2 and keysize. Key1' and' key2' are pointers
to two tuples to be compared, and' keysize' is the size of tuples.
Within the function, firstly, ` key1' and ` key2' are converted into tuples, and then the tuples are stored
in the table slots and input slots of the hash table by using the ` ExecStoreMinimalTuple' function.
Finally, compare whether two tuples are equal by using the execTuplesMatch function.
If two tuples are equal, the function returns 0, otherwise it returns 1.*/
static int TupleHashTableMatch(const void* key1, const void* key2, Size keysize)
{
MinimalTuple tuple1 = ((const TupleHashEntryData*)key1)->firstTuple;