PostgreSQL Source Code  git master
execGrouping.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * execGrouping.c
4  * executor utility routines for grouping, hashing, and aggregation
5  *
6  * Note: we currently assume that equality and hashing functions are not
7  * collation-sensitive, so the code in this file has no support for passing
8  * collation settings through from callers. That may have to change someday.
9  *
10  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  *
14  * IDENTIFICATION
15  * src/backend/executor/execGrouping.c
16  *
17  *-------------------------------------------------------------------------
18  */
19 #include "postgres.h"
20 
21 #include "access/hash.h"
22 #include "access/parallel.h"
23 #include "executor/executor.h"
24 #include "miscadmin.h"
25 #include "utils/lsyscache.h"
26 #include "utils/hashutils.h"
27 #include "utils/memutils.h"
28 
29 static uint32 TupleHashTableHash(struct tuplehash_hash *tb, const MinimalTuple tuple);
30 static int TupleHashTableMatch(struct tuplehash_hash *tb, const MinimalTuple tuple1, const MinimalTuple tuple2);
31 
32 /*
33  * Define parameters for tuple hash table code generation. The interface is
34  * *also* declared in execnodes.h (to generate the types, which are externally
35  * visible).
36  */
37 #define SH_PREFIX tuplehash
38 #define SH_ELEMENT_TYPE TupleHashEntryData
39 #define SH_KEY_TYPE MinimalTuple
40 #define SH_KEY firstTuple
41 #define SH_HASH_KEY(tb, key) TupleHashTableHash(tb, key)
42 #define SH_EQUAL(tb, a, b) TupleHashTableMatch(tb, a, b) == 0
43 #define SH_SCOPE extern
44 #define SH_STORE_HASH
45 #define SH_GET_HASH(tb, a) a->hash
46 #define SH_DEFINE
47 #include "lib/simplehash.h"
48 
49 
50 /*****************************************************************************
51  * Utility routines for grouping tuples together
52  *****************************************************************************/
53 
54 /*
55  * execTuplesMatchPrepare
56  * Build expression that can be evaluated using ExecQual(), returning
57  * whether an ExprContext's inner/outer tuples are NOT DISTINCT
58  */
59 ExprState *
61  int numCols,
62  AttrNumber *keyColIdx,
63  Oid *eqOperators,
64  PlanState *parent)
65 {
66  Oid *eqFunctions = (Oid *) palloc(numCols * sizeof(Oid));
67  int i;
68  ExprState *expr;
69 
70  if (numCols == 0)
71  return NULL;
72 
73  /* lookup equality functions */
74  for (i = 0; i < numCols; i++)
75  eqFunctions[i] = get_opcode(eqOperators[i]);
76 
77  /* build actual expression */
78  expr = ExecBuildGroupingEqual(desc, desc, numCols, keyColIdx, eqFunctions,
79  parent);
80 
81  return expr;
82 }
83 
84 /*
85  * execTuplesHashPrepare
86  * Look up the equality and hashing functions needed for a TupleHashTable.
87  *
88  * This is similar to execTuplesMatchPrepare, but we also need to find the
89  * hash functions associated with the equality operators. *eqFunctions and
90  * *hashFunctions receive the palloc'd result arrays.
91  *
92  * Note: we expect that the given operators are not cross-type comparisons.
93  */
94 void
96  Oid *eqOperators,
97  Oid **eqFuncOids,
98  FmgrInfo **hashFunctions)
99 {
100  int i;
101 
102  *eqFuncOids = (Oid *) palloc(numCols * sizeof(Oid));
103  *hashFunctions = (FmgrInfo *) palloc(numCols * sizeof(FmgrInfo));
104 
105  for (i = 0; i < numCols; i++)
106  {
107  Oid eq_opr = eqOperators[i];
108  Oid eq_function;
109  Oid left_hash_function;
110  Oid right_hash_function;
111 
112  eq_function = get_opcode(eq_opr);
113  if (!get_op_hash_functions(eq_opr,
114  &left_hash_function, &right_hash_function))
115  elog(ERROR, "could not find hash function for hash operator %u",
116  eq_opr);
117  /* We're not supporting cross-type cases here */
118  Assert(left_hash_function == right_hash_function);
119  (*eqFuncOids)[i] = eq_function;
120  fmgr_info(right_hash_function, &(*hashFunctions)[i]);
121  }
122 }
123 
124 
125 /*****************************************************************************
126  * Utility routines for all-in-memory hash tables
127  *
128  * These routines build hash tables for grouping tuples together (eg, for
129  * hash aggregation). There is one entry for each not-distinct set of tuples
130  * presented.
131  *****************************************************************************/
132 
133 /*
134  * Construct an empty TupleHashTable
135  *
136  * numCols, keyColIdx: identify the tuple fields to use as lookup key
137  * eqfunctions: equality comparison functions to use
138  * hashfunctions: datatype-specific hashing functions to use
139  * nbuckets: initial estimate of hashtable size
140  * additionalsize: size of data stored in ->additional
141  * tablecxt: memory context in which to store table and table entries
142  * tempcxt: short-lived context for evaluation hash and comparison functions
143  *
144  * The function arrays may be made with execTuplesHashPrepare(). Note they
145  * are not cross-type functions, but expect to see the table datatype(s)
146  * on both sides.
147  *
148  * Note that keyColIdx, eqfunctions, and hashfunctions must be allocated in
149  * storage that will live as long as the hashtable does.
150  */
153  TupleDesc inputDesc,
154  int numCols, AttrNumber *keyColIdx,
155  Oid *eqfuncoids,
156  FmgrInfo *hashfunctions,
157  long nbuckets, Size additionalsize,
158  MemoryContext tablecxt, MemoryContext tempcxt,
159  bool use_variable_hash_iv)
160 {
161  TupleHashTable hashtable;
162  Size entrysize = sizeof(TupleHashEntryData) + additionalsize;
163  MemoryContext oldcontext;
164 
165  Assert(nbuckets > 0);
166 
167  /* Limit initial table size request to not more than work_mem */
168  nbuckets = Min(nbuckets, (long) ((work_mem * 1024L) / entrysize));
169 
170  hashtable = (TupleHashTable)
171  MemoryContextAlloc(tablecxt, sizeof(TupleHashTableData));
172 
173  hashtable->numCols = numCols;
174  hashtable->keyColIdx = keyColIdx;
175  hashtable->tab_hash_funcs = hashfunctions;
176  hashtable->tablecxt = tablecxt;
177  hashtable->tempcxt = tempcxt;
178  hashtable->entrysize = entrysize;
179  hashtable->tableslot = NULL; /* will be made on first lookup */
180  hashtable->inputslot = NULL;
181  hashtable->in_hash_funcs = NULL;
182  hashtable->cur_eq_func = NULL;
183 
184  /*
185  * If parallelism is in use, even if the master backend is performing the
186  * scan itself, we don't want to create the hashtable exactly the same way
187  * in all workers. As hashtables are iterated over in keyspace-order,
188  * doing so in all processes in the same way is likely to lead to
189  * "unbalanced" hashtables when the table size initially is
190  * underestimated.
191  */
192  if (use_variable_hash_iv)
194  else
195  hashtable->hash_iv = 0;
196 
197  hashtable->hashtab = tuplehash_create(tablecxt, nbuckets, hashtable);
198 
199  oldcontext = MemoryContextSwitchTo(hashtable->tablecxt);
200 
201  /*
202  * We copy the input tuple descriptor just for safety --- we assume all
203  * input tuples will have equivalent descriptors.
204  */
205  hashtable->tableslot = MakeSingleTupleTableSlot(CreateTupleDescCopy(inputDesc));
206 
207  /* build comparator for all columns */
208  hashtable->tab_eq_func = ExecBuildGroupingEqual(inputDesc, inputDesc,
209  numCols,
210  keyColIdx, eqfuncoids,
211  parent);
212 
213  MemoryContextSwitchTo(oldcontext);
214 
215  hashtable->exprcontext = CreateExprContext(parent->state);
216 
217  return hashtable;
218 }
219 
220 /*
221  * Find or create a hashtable entry for the tuple group containing the
222  * given tuple. The tuple must be the same type as the hashtable entries.
223  *
224  * If isnew is NULL, we do not create new entries; we return NULL if no
225  * match is found.
226  *
227  * If isnew isn't NULL, then a new entry is created if no existing entry
228  * matches. On return, *isnew is true if the entry is newly created,
229  * false if it existed already. ->additional_data in the new entry has
230  * been zeroed.
231  */
234  bool *isnew)
235 {
236  TupleHashEntryData *entry;
237  MemoryContext oldContext;
238  bool found;
239  MinimalTuple key;
240 
241  /* Need to run the hash functions in short-lived context */
242  oldContext = MemoryContextSwitchTo(hashtable->tempcxt);
243 
244  /* set up data needed by hash and match functions */
245  hashtable->inputslot = slot;
246  hashtable->in_hash_funcs = hashtable->tab_hash_funcs;
247  hashtable->cur_eq_func = hashtable->tab_eq_func;
248 
249  key = NULL; /* flag to reference inputslot */
250 
251  if (isnew)
252  {
253  entry = tuplehash_insert(hashtable->hashtab, key, &found);
254 
255  if (found)
256  {
257  /* found pre-existing entry */
258  *isnew = false;
259  }
260  else
261  {
262  /* created new entry */
263  *isnew = true;
264  /* zero caller data */
265  entry->additional = NULL;
266  MemoryContextSwitchTo(hashtable->tablecxt);
267  /* Copy the first tuple into the table context */
268  entry->firstTuple = ExecCopySlotMinimalTuple(slot);
269  }
270  }
271  else
272  {
273  entry = tuplehash_lookup(hashtable->hashtab, key);
274  }
275 
276  MemoryContextSwitchTo(oldContext);
277 
278  return entry;
279 }
280 
281 /*
282  * Search for a hashtable entry matching the given tuple. No entry is
283  * created if there's not a match. This is similar to the non-creating
284  * case of LookupTupleHashEntry, except that it supports cross-type
285  * comparisons, in which the given tuple is not of the same type as the
286  * table entries. The caller must provide the hash functions to use for
287  * the input tuple, as well as the equality functions, since these may be
288  * different from the table's internal functions.
289  */
292  ExprState *eqcomp,
293  FmgrInfo *hashfunctions)
294 {
295  TupleHashEntry entry;
296  MemoryContext oldContext;
297  MinimalTuple key;
298 
299  /* Need to run the hash functions in short-lived context */
300  oldContext = MemoryContextSwitchTo(hashtable->tempcxt);
301 
302  /* Set up data needed by hash and match functions */
303  hashtable->inputslot = slot;
304  hashtable->in_hash_funcs = hashfunctions;
305  hashtable->cur_eq_func = eqcomp;
306 
307  /* Search the hash table */
308  key = NULL; /* flag to reference inputslot */
309  entry = tuplehash_lookup(hashtable->hashtab, key);
310  MemoryContextSwitchTo(oldContext);
311 
312  return entry;
313 }
314 
315 /*
316  * Compute the hash value for a tuple
317  *
318  * The passed-in key is a pointer to TupleHashEntryData. In an actual hash
319  * table entry, the firstTuple field points to a tuple (in MinimalTuple
320  * format). LookupTupleHashEntry sets up a dummy TupleHashEntryData with a
321  * NULL firstTuple field --- that cues us to look at the inputslot instead.
322  * This convention avoids the need to materialize virtual input tuples unless
323  * they actually need to get copied into the table.
324  *
325  * Also, the caller must select an appropriate memory context for running
326  * the hash functions. (dynahash.c doesn't change CurrentMemoryContext.)
327  */
328 static uint32
329 TupleHashTableHash(struct tuplehash_hash *tb, const MinimalTuple tuple)
330 {
331  TupleHashTable hashtable = (TupleHashTable) tb->private_data;
332  int numCols = hashtable->numCols;
333  AttrNumber *keyColIdx = hashtable->keyColIdx;
334  uint32 hashkey = hashtable->hash_iv;
335  TupleTableSlot *slot;
336  FmgrInfo *hashfunctions;
337  int i;
338 
339  if (tuple == NULL)
340  {
341  /* Process the current input tuple for the table */
342  slot = hashtable->inputslot;
343  hashfunctions = hashtable->in_hash_funcs;
344  }
345  else
346  {
347  /*
348  * Process a tuple already stored in the table.
349  *
350  * (this case never actually occurs due to the way simplehash.h is
351  * used, as the hash-value is stored in the entries)
352  */
353  slot = hashtable->tableslot;
354  ExecStoreMinimalTuple(tuple, slot, false);
355  hashfunctions = hashtable->tab_hash_funcs;
356  }
357 
358  for (i = 0; i < numCols; i++)
359  {
360  AttrNumber att = keyColIdx[i];
361  Datum attr;
362  bool isNull;
363 
364  /* rotate hashkey left 1 bit at each step */
365  hashkey = (hashkey << 1) | ((hashkey & 0x80000000) ? 1 : 0);
366 
367  attr = slot_getattr(slot, att, &isNull);
368 
369  if (!isNull) /* treat nulls as having hash key 0 */
370  {
371  uint32 hkey;
372 
373  hkey = DatumGetUInt32(FunctionCall1(&hashfunctions[i],
374  attr));
375  hashkey ^= hkey;
376  }
377  }
378 
379  /*
380  * The way hashes are combined above, among each other and with the IV,
381  * doesn't lead to good bit perturbation. As the IV's goal is to lead to
382  * achieve that, perform a round of hashing of the combined hash -
383  * resulting in near perfect perturbation.
384  */
385  return murmurhash32(hashkey);
386 }
387 
388 /*
389  * See whether two tuples (presumably of the same hash value) match
390  *
391  * As above, the passed pointers are pointers to TupleHashEntryData.
392  */
393 static int
394 TupleHashTableMatch(struct tuplehash_hash *tb, const MinimalTuple tuple1, const MinimalTuple tuple2)
395 {
396  TupleTableSlot *slot1;
397  TupleTableSlot *slot2;
398  TupleHashTable hashtable = (TupleHashTable) tb->private_data;
399  ExprContext *econtext = hashtable->exprcontext;
400 
401  /*
402  * We assume that simplehash.h will only ever call us with the first
403  * argument being an actual table entry, and the second argument being
404  * LookupTupleHashEntry's dummy TupleHashEntryData. The other direction
405  * could be supported too, but is not currently required.
406  */
407  Assert(tuple1 != NULL);
408  slot1 = hashtable->tableslot;
409  ExecStoreMinimalTuple(tuple1, slot1, false);
410  Assert(tuple2 == NULL);
411  slot2 = hashtable->inputslot;
412 
413  /* For crosstype comparisons, the inputslot must be first */
414  econtext->ecxt_innertuple = slot2;
415  econtext->ecxt_outertuple = slot1;
416  return !ExecQualAndReset(hashtable->cur_eq_func, econtext);
417 }
ExprContext * exprcontext
Definition: execnodes.h:681
#define DatumGetUInt32(X)
Definition: postgres.h:471
Definition: fmgr.h:56
void execTuplesHashPrepare(int numCols, Oid *eqOperators, Oid **eqFuncOids, FmgrInfo **hashFunctions)
Definition: execGrouping.c:95
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:112
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:507
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:420
ExprState * ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc, int numCols, AttrNumber *keyColIdx, Oid *eqfunctions, PlanState *parent)
Definition: execExpr.c:3216
TupleTableSlot * inputslot
Definition: execnodes.h:677
MinimalTuple firstTuple
Definition: execnodes.h:651
#define Min(x, y)
Definition: c.h:857
MinimalTuple ExecCopySlotMinimalTuple(TupleTableSlot *slot)
Definition: execTuples.c:613
TupleHashEntry FindTupleHashEntry(TupleHashTable hashtable, TupleTableSlot *slot, ExprState *eqcomp, FmgrInfo *hashfunctions)
Definition: execGrouping.c:291
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
AttrNumber * keyColIdx
Definition: execnodes.h:669
ExprState * tab_eq_func
Definition: execnodes.h:671
ExprState * cur_eq_func
Definition: execnodes.h:679
EState * state
Definition: execnodes.h:914
unsigned int Oid
Definition: postgres_ext.h:31
FmgrInfo * tab_hash_funcs
Definition: execnodes.h:670
static int TupleHashTableMatch(struct tuplehash_hash *tb, const MinimalTuple tuple1, const MinimalTuple tuple2)
Definition: execGrouping.c:394
FmgrInfo * in_hash_funcs
Definition: execnodes.h:678
#define ERROR
Definition: elog.h:43
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
struct TupleHashEntryData TupleHashEntryData
MemoryContext tablecxt
Definition: execnodes.h:672
struct TupleHashTableData * TupleHashTable
Definition: execnodes.h:647
int ParallelWorkerNumber
Definition: parallel.c:103
unsigned int uint32
Definition: c.h:325
TupleHashEntry LookupTupleHashEntry(TupleHashTable hashtable, TupleTableSlot *slot, bool *isnew)
Definition: execGrouping.c:233
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc)
Definition: execTuples.c:232
static bool ExecQualAndReset(ExprState *state, ExprContext *econtext)
Definition: executor.h:388
uintptr_t Datum
Definition: postgres.h:367
int work_mem
Definition: globals.c:120
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1079
tuplehash_hash * hashtab
Definition: execnodes.h:667
ExprState * execTuplesMatchPrepare(TupleDesc desc, int numCols, AttrNumber *keyColIdx, Oid *eqOperators, PlanState *parent)
Definition: execGrouping.c:60
#define Assert(condition)
Definition: c.h:699
TupleTableSlot * tableslot
Definition: execnodes.h:675
size_t Size
Definition: c.h:433
static uint32 TupleHashTableHash(struct tuplehash_hash *tb, const MinimalTuple tuple)
Definition: execGrouping.c:329
void * palloc(Size size)
Definition: mcxt.c:924
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:771
TupleHashTable BuildTupleHashTable(PlanState *parent, TupleDesc inputDesc, int numCols, AttrNumber *keyColIdx, Oid *eqfuncoids, FmgrInfo *hashfunctions, long nbuckets, Size additionalsize, MemoryContext tablecxt, MemoryContext tempcxt, bool use_variable_hash_iv)
Definition: execGrouping.c:152
int i
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:608
Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: heaptuple.c:1518
ExprContext * CreateExprContext(EState *estate)
Definition: execUtils.c:228
static uint32 murmurhash32(uint32 data)
Definition: hashutils.h:41
MemoryContext tempcxt
Definition: execnodes.h:673
#define elog
Definition: elog.h:219
int16 AttrNumber
Definition: attnum.h:21