PostgreSQL Source Code  git master
execGrouping.c File Reference
#include "postgres.h"
#include "access/parallel.h"
#include "executor/executor.h"
#include "miscadmin.h"
#include "utils/hashutils.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "lib/simplehash.h"
Include dependency graph for execGrouping.c:

Go to the source code of this file.

Macros

#define SH_PREFIX   tuplehash
 
#define SH_ELEMENT_TYPE   TupleHashEntryData
 
#define SH_KEY_TYPE   MinimalTuple
 
#define SH_KEY   firstTuple
 
#define SH_HASH_KEY(tb, key)   TupleHashTableHash_internal(tb, key)
 
#define SH_EQUAL(tb, a, b)   TupleHashTableMatch(tb, a, b) == 0
 
#define SH_SCOPE   extern
 
#define SH_STORE_HASH
 
#define SH_GET_HASH(tb, a)   a->hash
 
#define SH_DEFINE
 

Functions

static int TupleHashTableMatch (struct tuplehash_hash *tb, const MinimalTuple tuple1, const MinimalTuple tuple2)
 
static uint32 TupleHashTableHash_internal (struct tuplehash_hash *tb, const MinimalTuple tuple)
 
static TupleHashEntry LookupTupleHashEntry_internal (TupleHashTable hashtable, TupleTableSlot *slot, bool *isnew, uint32 hash)
 
ExprStateexecTuplesMatchPrepare (TupleDesc desc, int numCols, const AttrNumber *keyColIdx, const Oid *eqOperators, const Oid *collations, PlanState *parent)
 
void execTuplesHashPrepare (int numCols, const Oid *eqOperators, Oid **eqFuncOids, FmgrInfo **hashFunctions)
 
TupleHashTable BuildTupleHashTableExt (PlanState *parent, TupleDesc inputDesc, int numCols, AttrNumber *keyColIdx, const Oid *eqfuncoids, FmgrInfo *hashfunctions, Oid *collations, long nbuckets, Size additionalsize, MemoryContext metacxt, MemoryContext tablecxt, MemoryContext tempcxt, bool use_variable_hash_iv)
 
TupleHashTable BuildTupleHashTable (PlanState *parent, TupleDesc inputDesc, int numCols, AttrNumber *keyColIdx, const Oid *eqfuncoids, FmgrInfo *hashfunctions, Oid *collations, long nbuckets, Size additionalsize, MemoryContext tablecxt, MemoryContext tempcxt, bool use_variable_hash_iv)
 
void ResetTupleHashTable (TupleHashTable hashtable)
 
TupleHashEntry LookupTupleHashEntry (TupleHashTable hashtable, TupleTableSlot *slot, bool *isnew)
 
uint32 TupleHashTableHash (TupleHashTable hashtable, TupleTableSlot *slot)
 
TupleHashEntry LookupTupleHashEntryHash (TupleHashTable hashtable, TupleTableSlot *slot, bool *isnew, uint32 hash)
 
TupleHashEntry FindTupleHashEntry (TupleHashTable hashtable, TupleTableSlot *slot, ExprState *eqcomp, FmgrInfo *hashfunctions)
 

Macro Definition Documentation

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 48 of file execGrouping.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   TupleHashEntryData

Definition at line 40 of file execGrouping.c.

◆ SH_EQUAL

#define SH_EQUAL (   tb,
  a,
 
)    TupleHashTableMatch(tb, a, b) == 0

Definition at line 44 of file execGrouping.c.

◆ SH_GET_HASH

#define SH_GET_HASH (   tb,
 
)    a->hash

Definition at line 47 of file execGrouping.c.

◆ SH_HASH_KEY

#define SH_HASH_KEY (   tb,
  key 
)    TupleHashTableHash_internal(tb, key)

Definition at line 43 of file execGrouping.c.

◆ SH_KEY

#define SH_KEY   firstTuple

Definition at line 42 of file execGrouping.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   MinimalTuple

Definition at line 41 of file execGrouping.c.

◆ SH_PREFIX

#define SH_PREFIX   tuplehash

Definition at line 39 of file execGrouping.c.

◆ SH_SCOPE

#define SH_SCOPE   extern

Definition at line 45 of file execGrouping.c.

◆ SH_STORE_HASH

#define SH_STORE_HASH

Definition at line 46 of file execGrouping.c.

Function Documentation

◆ BuildTupleHashTable()

TupleHashTable BuildTupleHashTable ( PlanState parent,
TupleDesc  inputDesc,
int  numCols,
AttrNumber keyColIdx,
const Oid eqfuncoids,
FmgrInfo hashfunctions,
Oid collations,
long  nbuckets,
Size  additionalsize,
MemoryContext  tablecxt,
MemoryContext  tempcxt,
bool  use_variable_hash_iv 
)

Definition at line 255 of file execGrouping.c.

References BuildTupleHashTableExt().

265 {
266  return BuildTupleHashTableExt(parent,
267  inputDesc,
268  numCols, keyColIdx,
269  eqfuncoids,
270  hashfunctions,
271  collations,
272  nbuckets, additionalsize,
273  tablecxt,
274  tablecxt,
275  tempcxt,
276  use_variable_hash_iv);
277 }
TupleHashTable BuildTupleHashTableExt(PlanState *parent, TupleDesc inputDesc, int numCols, AttrNumber *keyColIdx, const Oid *eqfuncoids, FmgrInfo *hashfunctions, Oid *collations, long nbuckets, Size additionalsize, MemoryContext metacxt, MemoryContext tablecxt, MemoryContext tempcxt, bool use_variable_hash_iv)
Definition: execGrouping.c:157

◆ BuildTupleHashTableExt()

TupleHashTable BuildTupleHashTableExt ( PlanState parent,
TupleDesc  inputDesc,
int  numCols,
AttrNumber keyColIdx,
const Oid eqfuncoids,
FmgrInfo hashfunctions,
Oid collations,
long  nbuckets,
Size  additionalsize,
MemoryContext  metacxt,
MemoryContext  tablecxt,
MemoryContext  tempcxt,
bool  use_variable_hash_iv 
)

Definition at line 157 of file execGrouping.c.

References Assert, CreateStandaloneExprContext(), CreateTupleDescCopy(), TupleHashTableData::cur_eq_func, TupleHashTableData::entrysize, ExecBuildGroupingEqual(), TupleHashTableData::exprcontext, TupleHashTableData::hash_iv, TupleHashTableData::hashtab, TupleHashTableData::in_hash_funcs, TupleHashTableData::inputslot, TupleHashTableData::keyColIdx, MakeSingleTupleTableSlot(), MemoryContextSwitchTo(), Min, murmurhash32(), TupleHashTableData::numCols, palloc(), ParallelWorkerNumber, TupleHashTableData::tab_collations, TupleHashTableData::tab_eq_func, TupleHashTableData::tab_hash_funcs, TupleHashTableData::tablecxt, TupleHashTableData::tableslot, TupleHashTableData::tempcxt, TTSOpsMinimalTuple, and work_mem.

Referenced by build_hash_table(), buildSubPlanHash(), and BuildTupleHashTable().

168 {
169  TupleHashTable hashtable;
170  Size entrysize = sizeof(TupleHashEntryData) + additionalsize;
171  MemoryContext oldcontext;
172  bool allow_jit;
173 
174  Assert(nbuckets > 0);
175 
176  /* Limit initial table size request to not more than work_mem */
177  nbuckets = Min(nbuckets, (long) ((work_mem * 1024L) / entrysize));
178 
179  oldcontext = MemoryContextSwitchTo(metacxt);
180 
181  hashtable = (TupleHashTable) palloc(sizeof(TupleHashTableData));
182 
183  hashtable->numCols = numCols;
184  hashtable->keyColIdx = keyColIdx;
185  hashtable->tab_hash_funcs = hashfunctions;
186  hashtable->tab_collations = collations;
187  hashtable->tablecxt = tablecxt;
188  hashtable->tempcxt = tempcxt;
189  hashtable->entrysize = entrysize;
190  hashtable->tableslot = NULL; /* will be made on first lookup */
191  hashtable->inputslot = NULL;
192  hashtable->in_hash_funcs = NULL;
193  hashtable->cur_eq_func = NULL;
194 
195  /*
196  * If parallelism is in use, even if the master backend is performing the
197  * scan itself, we don't want to create the hashtable exactly the same way
198  * in all workers. As hashtables are iterated over in keyspace-order,
199  * doing so in all processes in the same way is likely to lead to
200  * "unbalanced" hashtables when the table size initially is
201  * underestimated.
202  */
203  if (use_variable_hash_iv)
205  else
206  hashtable->hash_iv = 0;
207 
208  hashtable->hashtab = tuplehash_create(metacxt, nbuckets, hashtable);
209 
210  /*
211  * We copy the input tuple descriptor just for safety --- we assume all
212  * input tuples will have equivalent descriptors.
213  */
216 
217  /*
218  * If the old reset interface is used (i.e. BuildTupleHashTable, rather
219  * than BuildTupleHashTableExt), allowing JIT would lead to the generated
220  * functions to a) live longer than the query b) be re-generated each time
221  * the table is being reset. Therefore prevent JIT from being used in that
222  * case, by not providing a parent node (which prevents accessing the
223  * JitContext in the EState).
224  */
225  allow_jit = metacxt != tablecxt;
226 
227  /* build comparator for all columns */
228  /* XXX: should we support non-minimal tuples for the inputslot? */
229  hashtable->tab_eq_func = ExecBuildGroupingEqual(inputDesc, inputDesc,
231  numCols,
232  keyColIdx, eqfuncoids, collations,
233  allow_jit ? parent : NULL);
234 
235  /*
236  * While not pretty, it's ok to not shut down this context, but instead
237  * rely on the containing memory context being reset, as
238  * ExecBuildGroupingEqual() only builds a very simple expression calling
239  * functions (i.e. nothing that'd employ RegisterExprContextCallback()).
240  */
242 
243  MemoryContextSwitchTo(oldcontext);
244 
245  return hashtable;
246 }
ExprContext * exprcontext
Definition: execnodes.h:711
ExprContext * CreateStandaloneExprContext(void)
Definition: execUtils.c:316
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1208
ExprState * ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc, const TupleTableSlotOps *lops, const TupleTableSlotOps *rops, int numCols, const AttrNumber *keyColIdx, const Oid *eqfunctions, const Oid *collations, PlanState *parent)
Definition: execExpr.c:3329
TupleTableSlot * inputslot
Definition: execnodes.h:707
#define Min(x, y)
Definition: c.h:920
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
AttrNumber * keyColIdx
Definition: execnodes.h:698
ExprState * tab_eq_func
Definition: execnodes.h:700
ExprState * cur_eq_func
Definition: execnodes.h:709
FmgrInfo * tab_hash_funcs
Definition: execnodes.h:699
FmgrInfo * in_hash_funcs
Definition: execnodes.h:708
struct TupleHashEntryData TupleHashEntryData
MemoryContext tablecxt
Definition: execnodes.h:702
struct TupleHashTableData * TupleHashTable
Definition: execnodes.h:676
int ParallelWorkerNumber
Definition: parallel.c:110
int work_mem
Definition: globals.c:121
tuplehash_hash * hashtab
Definition: execnodes.h:696
#define Assert(condition)
Definition: c.h:738
TupleTableSlot * tableslot
Definition: execnodes.h:705
size_t Size
Definition: c.h:466
void * palloc(Size size)
Definition: mcxt.c:949
static uint32 murmurhash32(uint32 data)
Definition: hashutils.h:60
MemoryContext tempcxt
Definition: execnodes.h:703
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85

◆ execTuplesHashPrepare()

void execTuplesHashPrepare ( int  numCols,
const Oid eqOperators,
Oid **  eqFuncOids,
FmgrInfo **  hashFunctions 
)

Definition at line 99 of file execGrouping.c.

References Assert, elog, ERROR, fmgr_info(), get_op_hash_functions(), get_opcode(), i, and palloc().

Referenced by ExecInitRecursiveUnion(), ExecInitSetOp(), and find_hash_columns().

103 {
104  int i;
105 
106  *eqFuncOids = (Oid *) palloc(numCols * sizeof(Oid));
107  *hashFunctions = (FmgrInfo *) palloc(numCols * sizeof(FmgrInfo));
108 
109  for (i = 0; i < numCols; i++)
110  {
111  Oid eq_opr = eqOperators[i];
112  Oid eq_function;
113  Oid left_hash_function;
114  Oid right_hash_function;
115 
116  eq_function = get_opcode(eq_opr);
117  if (!get_op_hash_functions(eq_opr,
118  &left_hash_function, &right_hash_function))
119  elog(ERROR, "could not find hash function for hash operator %u",
120  eq_opr);
121  /* We're not supporting cross-type cases here */
122  Assert(left_hash_function == right_hash_function);
123  (*eqFuncOids)[i] = eq_function;
124  fmgr_info(right_hash_function, &(*hashFunctions)[i]);
125  }
126 }
Definition: fmgr.h:56
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:507
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1092
#define Assert(condition)
Definition: c.h:738
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
int i

◆ execTuplesMatchPrepare()

ExprState* execTuplesMatchPrepare ( TupleDesc  desc,
int  numCols,
const AttrNumber keyColIdx,
const Oid eqOperators,
const Oid collations,
PlanState parent 
)

Definition at line 62 of file execGrouping.c.

References ExecBuildGroupingEqual(), get_opcode(), i, and palloc().

Referenced by build_pertrans_for_aggref(), ExecInitAgg(), ExecInitGroup(), ExecInitSetOp(), ExecInitUnique(), ExecInitWindowAgg(), and hypothetical_dense_rank_final().

68 {
69  Oid *eqFunctions = (Oid *) palloc(numCols * sizeof(Oid));
70  int i;
71  ExprState *expr;
72 
73  if (numCols == 0)
74  return NULL;
75 
76  /* lookup equality functions */
77  for (i = 0; i < numCols; i++)
78  eqFunctions[i] = get_opcode(eqOperators[i]);
79 
80  /* build actual expression */
81  expr = ExecBuildGroupingEqual(desc, desc, NULL, NULL,
82  numCols, keyColIdx, eqFunctions, collations,
83  parent);
84 
85  return expr;
86 }
ExprState * ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc, const TupleTableSlotOps *lops, const TupleTableSlotOps *rops, int numCols, const AttrNumber *keyColIdx, const Oid *eqfunctions, const Oid *collations, PlanState *parent)
Definition: execExpr.c:3329
unsigned int Oid
Definition: postgres_ext.h:31
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1092
void * palloc(Size size)
Definition: mcxt.c:949
int i

◆ FindTupleHashEntry()

TupleHashEntry FindTupleHashEntry ( TupleHashTable  hashtable,
TupleTableSlot slot,
ExprState eqcomp,
FmgrInfo hashfunctions 
)

Definition at line 384 of file execGrouping.c.

References TupleHashTableData::cur_eq_func, TupleHashTableData::hashtab, TupleHashTableData::in_hash_funcs, TupleHashTableData::inputslot, sort-test::key, MemoryContextSwitchTo(), and TupleHashTableData::tempcxt.

Referenced by ExecHashSubPlan().

387 {
388  TupleHashEntry entry;
389  MemoryContext oldContext;
391 
392  /* Need to run the hash functions in short-lived context */
393  oldContext = MemoryContextSwitchTo(hashtable->tempcxt);
394 
395  /* Set up data needed by hash and match functions */
396  hashtable->inputslot = slot;
397  hashtable->in_hash_funcs = hashfunctions;
398  hashtable->cur_eq_func = eqcomp;
399 
400  /* Search the hash table */
401  key = NULL; /* flag to reference inputslot */
402  entry = tuplehash_lookup(hashtable->hashtab, key);
403  MemoryContextSwitchTo(oldContext);
404 
405  return entry;
406 }
TupleTableSlot * inputslot
Definition: execnodes.h:707
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
ExprState * cur_eq_func
Definition: execnodes.h:709
FmgrInfo * in_hash_funcs
Definition: execnodes.h:708
tuplehash_hash * hashtab
Definition: execnodes.h:696
MemoryContext tempcxt
Definition: execnodes.h:703

◆ LookupTupleHashEntry()

TupleHashEntry LookupTupleHashEntry ( TupleHashTable  hashtable,
TupleTableSlot slot,
bool isnew 
)

Definition at line 303 of file execGrouping.c.

References TupleHashTableData::cur_eq_func, hash(), TupleHashTableData::hashtab, TupleHashTableData::in_hash_funcs, TupleHashTableData::inputslot, LookupTupleHashEntry_internal(), MemoryContextSwitchTo(), TupleHashTableData::tab_eq_func, TupleHashTableData::tab_hash_funcs, TupleHashTableData::tempcxt, and TupleHashTableHash_internal().

Referenced by buildSubPlanHash(), ExecRecursiveUnion(), and setop_fill_hash_table().

305 {
306  TupleHashEntry entry;
307  MemoryContext oldContext;
308  uint32 hash;
309 
310  /* Need to run the hash functions in short-lived context */
311  oldContext = MemoryContextSwitchTo(hashtable->tempcxt);
312 
313  /* set up data needed by hash and match functions */
314  hashtable->inputslot = slot;
315  hashtable->in_hash_funcs = hashtable->tab_hash_funcs;
316  hashtable->cur_eq_func = hashtable->tab_eq_func;
317 
318  hash = TupleHashTableHash_internal(hashtable->hashtab, NULL);
319  entry = LookupTupleHashEntry_internal(hashtable, slot, isnew, hash);
320 
321  MemoryContextSwitchTo(oldContext);
322 
323  return entry;
324 }
TupleTableSlot * inputslot
Definition: execnodes.h:707
static uint32 TupleHashTableHash_internal(struct tuplehash_hash *tb, const MinimalTuple tuple)
Definition: execGrouping.c:417
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
ExprState * tab_eq_func
Definition: execnodes.h:700
ExprState * cur_eq_func
Definition: execnodes.h:709
FmgrInfo * tab_hash_funcs
Definition: execnodes.h:699
FmgrInfo * in_hash_funcs
Definition: execnodes.h:708
static TupleHashEntry LookupTupleHashEntry_internal(TupleHashTable hashtable, TupleTableSlot *slot, bool *isnew, uint32 hash)
Definition: execGrouping.c:487
unsigned int uint32
Definition: c.h:367
tuplehash_hash * hashtab
Definition: execnodes.h:696
MemoryContext tempcxt
Definition: execnodes.h:703
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:541

◆ LookupTupleHashEntry_internal()

static TupleHashEntry LookupTupleHashEntry_internal ( TupleHashTable  hashtable,
TupleTableSlot slot,
bool isnew,
uint32  hash 
)
static

Definition at line 487 of file execGrouping.c.

References TupleHashEntryData::additional, ExecCopySlotMinimalTuple(), TupleHashEntryData::firstTuple, TupleHashTableData::hashtab, sort-test::key, MemoryContextSwitchTo(), and TupleHashTableData::tablecxt.

Referenced by LookupTupleHashEntry(), and LookupTupleHashEntryHash().

489 {
490  TupleHashEntryData *entry;
491  bool found;
493 
494  key = NULL; /* flag to reference inputslot */
495 
496  if (isnew)
497  {
498  entry = tuplehash_insert_hash(hashtable->hashtab, key, hash, &found);
499 
500  if (found)
501  {
502  /* found pre-existing entry */
503  *isnew = false;
504  }
505  else
506  {
507  /* created new entry */
508  *isnew = true;
509  /* zero caller data */
510  entry->additional = NULL;
511  MemoryContextSwitchTo(hashtable->tablecxt);
512  /* Copy the first tuple into the table context */
513  entry->firstTuple = ExecCopySlotMinimalTuple(slot);
514  }
515  }
516  else
517  {
518  entry = tuplehash_lookup_hash(hashtable->hashtab, key, hash);
519  }
520 
521  return entry;
522 }
MinimalTuple firstTuple
Definition: execnodes.h:680
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static MinimalTuple ExecCopySlotMinimalTuple(TupleTableSlot *slot)
Definition: tuptable.h:463
MemoryContext tablecxt
Definition: execnodes.h:702
tuplehash_hash * hashtab
Definition: execnodes.h:696
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:541

◆ LookupTupleHashEntryHash()

TupleHashEntry LookupTupleHashEntryHash ( TupleHashTable  hashtable,
TupleTableSlot slot,
bool isnew,
uint32  hash 
)

Definition at line 353 of file execGrouping.c.

References TupleHashTableData::cur_eq_func, TupleHashTableData::in_hash_funcs, TupleHashTableData::inputslot, LookupTupleHashEntry_internal(), MemoryContextSwitchTo(), TupleHashTableData::tab_eq_func, TupleHashTableData::tab_hash_funcs, and TupleHashTableData::tempcxt.

Referenced by lookup_hash_entry().

355 {
356  TupleHashEntry entry;
357  MemoryContext oldContext;
358 
359  /* Need to run the hash functions in short-lived context */
360  oldContext = MemoryContextSwitchTo(hashtable->tempcxt);
361 
362  /* set up data needed by hash and match functions */
363  hashtable->inputslot = slot;
364  hashtable->in_hash_funcs = hashtable->tab_hash_funcs;
365  hashtable->cur_eq_func = hashtable->tab_eq_func;
366 
367  entry = LookupTupleHashEntry_internal(hashtable, slot, isnew, hash);
368 
369  MemoryContextSwitchTo(oldContext);
370 
371  return entry;
372 }
TupleTableSlot * inputslot
Definition: execnodes.h:707
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
ExprState * tab_eq_func
Definition: execnodes.h:700
ExprState * cur_eq_func
Definition: execnodes.h:709
FmgrInfo * tab_hash_funcs
Definition: execnodes.h:699
FmgrInfo * in_hash_funcs
Definition: execnodes.h:708
static TupleHashEntry LookupTupleHashEntry_internal(TupleHashTable hashtable, TupleTableSlot *slot, bool *isnew, uint32 hash)
Definition: execGrouping.c:487
MemoryContext tempcxt
Definition: execnodes.h:703
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:541

◆ ResetTupleHashTable()

void ResetTupleHashTable ( TupleHashTable  hashtable)

Definition at line 285 of file execGrouping.c.

References TupleHashTableData::hashtab.

Referenced by build_hash_tables(), buildSubPlanHash(), ExecReScanRecursiveUnion(), and ExecReScanSetOp().

286 {
287  tuplehash_reset(hashtable->hashtab);
288 }
tuplehash_hash * hashtab
Definition: execnodes.h:696

◆ TupleHashTableHash()

uint32 TupleHashTableHash ( TupleHashTable  hashtable,
TupleTableSlot slot 
)

Definition at line 330 of file execGrouping.c.

References hash(), TupleHashTableData::hashtab, TupleHashTableData::in_hash_funcs, TupleHashTableData::inputslot, MemoryContextSwitchTo(), TupleHashTableData::tab_hash_funcs, TupleHashTableData::tempcxt, and TupleHashTableHash_internal().

Referenced by lookup_hash_entries().

331 {
332  MemoryContext oldContext;
333  uint32 hash;
334 
335  hashtable->inputslot = slot;
336  hashtable->in_hash_funcs = hashtable->tab_hash_funcs;
337 
338  /* Need to run the hash functions in short-lived context */
339  oldContext = MemoryContextSwitchTo(hashtable->tempcxt);
340 
341  hash = TupleHashTableHash_internal(hashtable->hashtab, NULL);
342 
343  MemoryContextSwitchTo(oldContext);
344 
345  return hash;
346 }
TupleTableSlot * inputslot
Definition: execnodes.h:707
static uint32 TupleHashTableHash_internal(struct tuplehash_hash *tb, const MinimalTuple tuple)
Definition: execGrouping.c:417
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
FmgrInfo * tab_hash_funcs
Definition: execnodes.h:699
FmgrInfo * in_hash_funcs
Definition: execnodes.h:708
unsigned int uint32
Definition: c.h:367
tuplehash_hash * hashtab
Definition: execnodes.h:696
MemoryContext tempcxt
Definition: execnodes.h:703
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:541

◆ TupleHashTableHash_internal()

static uint32 TupleHashTableHash_internal ( struct tuplehash_hash *  tb,
const MinimalTuple  tuple 
)
static

Definition at line 417 of file execGrouping.c.

References DatumGetUInt32, ExecStoreMinimalTuple(), FunctionCall1Coll(), TupleHashTableData::hash_iv, i, TupleHashTableData::in_hash_funcs, TupleHashTableData::inputslot, TupleHashTableData::keyColIdx, murmurhash32(), TupleHashTableData::numCols, slot_getattr(), TupleHashTableData::tab_collations, TupleHashTableData::tab_hash_funcs, and TupleHashTableData::tableslot.

Referenced by LookupTupleHashEntry(), and TupleHashTableHash().

419 {
420  TupleHashTable hashtable = (TupleHashTable) tb->private_data;
421  int numCols = hashtable->numCols;
422  AttrNumber *keyColIdx = hashtable->keyColIdx;
423  uint32 hashkey = hashtable->hash_iv;
424  TupleTableSlot *slot;
425  FmgrInfo *hashfunctions;
426  int i;
427 
428  if (tuple == NULL)
429  {
430  /* Process the current input tuple for the table */
431  slot = hashtable->inputslot;
432  hashfunctions = hashtable->in_hash_funcs;
433  }
434  else
435  {
436  /*
437  * Process a tuple already stored in the table.
438  *
439  * (this case never actually occurs due to the way simplehash.h is
440  * used, as the hash-value is stored in the entries)
441  */
442  slot = hashtable->tableslot;
443  ExecStoreMinimalTuple(tuple, slot, false);
444  hashfunctions = hashtable->tab_hash_funcs;
445  }
446 
447  for (i = 0; i < numCols; i++)
448  {
449  AttrNumber att = keyColIdx[i];
450  Datum attr;
451  bool isNull;
452 
453  /* rotate hashkey left 1 bit at each step */
454  hashkey = (hashkey << 1) | ((hashkey & 0x80000000) ? 1 : 0);
455 
456  attr = slot_getattr(slot, att, &isNull);
457 
458  if (!isNull) /* treat nulls as having hash key 0 */
459  {
460  uint32 hkey;
461 
462  hkey = DatumGetUInt32(FunctionCall1Coll(&hashfunctions[i],
463  hashtable->tab_collations[i],
464  attr));
465  hashkey ^= hkey;
466  }
467  }
468 
469  /*
470  * The way hashes are combined above, among each other and with the IV,
471  * doesn't lead to good bit perturbation. As the IV's goal is to lead to
472  * achieve that, perform a round of hashing of the combined hash -
473  * resulting in near perfect perturbation.
474  */
475  return murmurhash32(hashkey);
476 }
#define DatumGetUInt32(X)
Definition: postgres.h:486
Definition: fmgr.h:56
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1416
TupleTableSlot * inputslot
Definition: execnodes.h:707
AttrNumber * keyColIdx
Definition: execnodes.h:698
FmgrInfo * tab_hash_funcs
Definition: execnodes.h:699
FmgrInfo * in_hash_funcs
Definition: execnodes.h:708
struct TupleHashTableData * TupleHashTable
Definition: execnodes.h:676
unsigned int uint32
Definition: c.h:367
uintptr_t Datum
Definition: postgres.h:367
Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
Definition: fmgr.c:1130
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:381
TupleTableSlot * tableslot
Definition: execnodes.h:705
int i
static uint32 murmurhash32(uint32 data)
Definition: hashutils.h:60
int16 AttrNumber
Definition: attnum.h:21

◆ TupleHashTableMatch()

static int TupleHashTableMatch ( struct tuplehash_hash *  tb,
const MinimalTuple  tuple1,
const MinimalTuple  tuple2 
)
static

Definition at line 528 of file execGrouping.c.

References Assert, TupleHashTableData::cur_eq_func, ExecQualAndReset(), ExecStoreMinimalTuple(), TupleHashTableData::exprcontext, TupleHashTableData::inputslot, and TupleHashTableData::tableslot.

529 {
530  TupleTableSlot *slot1;
531  TupleTableSlot *slot2;
532  TupleHashTable hashtable = (TupleHashTable) tb->private_data;
533  ExprContext *econtext = hashtable->exprcontext;
534 
535  /*
536  * We assume that simplehash.h will only ever call us with the first
537  * argument being an actual table entry, and the second argument being
538  * LookupTupleHashEntry's dummy TupleHashEntryData. The other direction
539  * could be supported too, but is not currently required.
540  */
541  Assert(tuple1 != NULL);
542  slot1 = hashtable->tableslot;
543  ExecStoreMinimalTuple(tuple1, slot1, false);
544  Assert(tuple2 == NULL);
545  slot2 = hashtable->inputslot;
546 
547  /* For crosstype comparisons, the inputslot must be first */
548  econtext->ecxt_innertuple = slot2;
549  econtext->ecxt_outertuple = slot1;
550  return !ExecQualAndReset(hashtable->cur_eq_func, econtext);
551 }
ExprContext * exprcontext
Definition: execnodes.h:711
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1416
TupleTableSlot * inputslot
Definition: execnodes.h:707
ExprState * cur_eq_func
Definition: execnodes.h:709
struct TupleHashTableData * TupleHashTable
Definition: execnodes.h:676
static bool ExecQualAndReset(ExprState *state, ExprContext *econtext)
Definition: executor.h:397
#define Assert(condition)
Definition: c.h:738
TupleTableSlot * tableslot
Definition: execnodes.h:705