PostgreSQL Source Code  git master
execExprInterp.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * execExprInterp.c
4  * Interpreted evaluation of an expression step list.
5  *
6  * This file provides either a "direct threaded" (for gcc, clang and
7  * compatible) or a "switch threaded" (for all compilers) implementation of
8  * expression evaluation. The former is amongst the fastest known methods
9  * of interpreting programs without resorting to assembly level work, or
10  * just-in-time compilation, but it requires support for computed gotos.
11  * The latter is amongst the fastest approaches doable in standard C.
12  *
13  * In either case we use ExprEvalStep->opcode to dispatch to the code block
14  * within ExecInterpExpr() that implements the specific opcode type.
15  *
16  * Switch-threading uses a plain switch() statement to perform the
17  * dispatch. This has the advantages of being plain C and allowing the
18  * compiler to warn if implementation of a specific opcode has been forgotten.
19  * The disadvantage is that dispatches will, as commonly implemented by
20  * compilers, happen from a single location, requiring more jumps and causing
21  * bad branch prediction.
22  *
23  * In direct threading, we use gcc's label-as-values extension - also adopted
24  * by some other compilers - to replace ExprEvalStep->opcode with the address
25  * of the block implementing the instruction. Dispatch to the next instruction
26  * is done by a "computed goto". This allows for better branch prediction
27  * (as the jumps are happening from different locations) and fewer jumps
28  * (as no preparatory jump to a common dispatch location is needed).
29  *
30  * When using direct threading, ExecReadyInterpretedExpr will replace
31  * each step's opcode field with the address of the relevant code block and
32  * ExprState->flags will contain EEO_FLAG_DIRECT_THREADED to remember that
33  * that's been done.
34  *
35  * For very simple instructions the overhead of the full interpreter
36  * "startup", as minimal as it is, is noticeable. Therefore
37  * ExecReadyInterpretedExpr will choose to implement certain simple
38  * opcode patterns using special fast-path routines (ExecJust*).
39  *
40  * Complex or uncommon instructions are not implemented in-line in
41  * ExecInterpExpr(), rather we call out to a helper function appearing later
42  * in this file. For one reason, there'd not be a noticeable performance
43  * benefit, but more importantly those complex routines are intended to be
44  * shared between different expression evaluation approaches. For instance
45  * a JIT compiler would generate calls to them. (This is why they are
46  * exported rather than being "static" in this file.)
47  *
48  *
49  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
50  * Portions Copyright (c) 1994, Regents of the University of California
51  *
52  * IDENTIFICATION
53  * src/backend/executor/execExprInterp.c
54  *
55  *-------------------------------------------------------------------------
56  */
57 #include "postgres.h"
58 
59 #include "access/heaptoast.h"
60 #include "catalog/pg_type.h"
61 #include "commands/sequence.h"
62 #include "executor/execExpr.h"
63 #include "executor/nodeSubplan.h"
64 #include "funcapi.h"
65 #include "miscadmin.h"
66 #include "nodes/nodeFuncs.h"
67 #include "parser/parsetree.h"
68 #include "pgstat.h"
69 #include "utils/array.h"
70 #include "utils/builtins.h"
71 #include "utils/date.h"
72 #include "utils/datum.h"
73 #include "utils/expandedrecord.h"
74 #include "utils/lsyscache.h"
75 #include "utils/memutils.h"
76 #include "utils/timestamp.h"
77 #include "utils/typcache.h"
78 #include "utils/xml.h"
79 
80 /*
81  * Use computed-goto-based opcode dispatch when computed gotos are available.
82  * But use a separate symbol so that it's easy to adjust locally in this file
83  * for development and testing.
84  */
85 #ifdef HAVE_COMPUTED_GOTO
86 #define EEO_USE_COMPUTED_GOTO
87 #endif /* HAVE_COMPUTED_GOTO */
88 
89 /*
90  * Macros for opcode dispatch.
91  *
92  * EEO_SWITCH - just hides the switch if not in use.
93  * EEO_CASE - labels the implementation of named expression step type.
94  * EEO_DISPATCH - jump to the implementation of the step type for 'op'.
95  * EEO_OPCODE - compute opcode required by used expression evaluation method.
96  * EEO_NEXT - increment 'op' and jump to correct next step type.
97  * EEO_JUMP - jump to the specified step number within the current expression.
98  */
99 #if defined(EEO_USE_COMPUTED_GOTO)
100 
101 /* struct for jump target -> opcode lookup table */
102 typedef struct ExprEvalOpLookup
103 {
104  const void *opcode;
105  ExprEvalOp op;
106 } ExprEvalOpLookup;
107 
108 /* to make dispatch_table accessible outside ExecInterpExpr() */
109 static const void **dispatch_table = NULL;
110 
111 /* jump target -> opcode lookup table */
112 static ExprEvalOpLookup reverse_dispatch_table[EEOP_LAST];
113 
114 #define EEO_SWITCH()
115 #define EEO_CASE(name) CASE_##name:
116 #define EEO_DISPATCH() goto *((void *) op->opcode)
117 #define EEO_OPCODE(opcode) ((intptr_t) dispatch_table[opcode])
118 
119 #else /* !EEO_USE_COMPUTED_GOTO */
120 
121 #define EEO_SWITCH() starteval: switch ((ExprEvalOp) op->opcode)
122 #define EEO_CASE(name) case name:
123 #define EEO_DISPATCH() goto starteval
124 #define EEO_OPCODE(opcode) (opcode)
125 
126 #endif /* EEO_USE_COMPUTED_GOTO */
127 
128 #define EEO_NEXT() \
129  do { \
130  op++; \
131  EEO_DISPATCH(); \
132  } while (0)
133 
134 #define EEO_JUMP(stepno) \
135  do { \
136  op = &state->steps[stepno]; \
137  EEO_DISPATCH(); \
138  } while (0)
139 
140 
141 static Datum ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull);
142 static void ExecInitInterpreter(void);
143 
144 /* support functions */
145 static void CheckVarSlotCompatibility(TupleTableSlot *slot, int attnum, Oid vartype);
147 static TupleDesc get_cached_rowtype(Oid type_id, int32 typmod,
148  ExprEvalRowtypeCache *rowcache,
149  bool *changed);
151  ExprContext *econtext, bool checkisnull);
152 
153 /* fast-path evaluation functions */
154 static Datum ExecJustInnerVar(ExprState *state, ExprContext *econtext, bool *isnull);
155 static Datum ExecJustOuterVar(ExprState *state, ExprContext *econtext, bool *isnull);
156 static Datum ExecJustScanVar(ExprState *state, ExprContext *econtext, bool *isnull);
157 static Datum ExecJustAssignInnerVar(ExprState *state, ExprContext *econtext, bool *isnull);
158 static Datum ExecJustAssignOuterVar(ExprState *state, ExprContext *econtext, bool *isnull);
159 static Datum ExecJustAssignScanVar(ExprState *state, ExprContext *econtext, bool *isnull);
160 static Datum ExecJustApplyFuncToCase(ExprState *state, ExprContext *econtext, bool *isnull);
161 static Datum ExecJustConst(ExprState *state, ExprContext *econtext, bool *isnull);
162 static Datum ExecJustInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull);
163 static Datum ExecJustOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull);
164 static Datum ExecJustScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull);
165 static Datum ExecJustAssignInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull);
166 static Datum ExecJustAssignOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull);
167 static Datum ExecJustAssignScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull);
168 
169 /* execution helper functions */
171  AggStatePerTrans pertrans,
172  AggStatePerGroup pergroup,
173  ExprContext *aggcontext,
174  int setno);
176  AggStatePerTrans pertrans,
177  AggStatePerGroup pergroup,
178  ExprContext *aggcontext,
179  int setno);
180 
181 /*
182  * ScalarArrayOpExprHashEntry
183  * Hash table entry type used during EEOP_HASHED_SCALARARRAYOP
184  */
186 {
188  uint32 status; /* hash status */
189  uint32 hash; /* hash value (cached) */
191 
192 #define SH_PREFIX saophash
193 #define SH_ELEMENT_TYPE ScalarArrayOpExprHashEntry
194 #define SH_KEY_TYPE Datum
195 #define SH_SCOPE static inline
196 #define SH_DECLARE
197 #include "lib/simplehash.h"
198 
199 static bool saop_hash_element_match(struct saophash_hash *tb, Datum key1,
200  Datum key2);
201 static uint32 saop_element_hash(struct saophash_hash *tb, Datum key);
202 
203 /*
204  * ScalarArrayOpExprHashTable
205  * Hash table for EEOP_HASHED_SCALARARRAYOP
206  */
208 {
209  saophash_hash *hashtab; /* underlying hash table */
210  struct ExprEvalStep *op;
212 
213 /* Define parameters for ScalarArrayOpExpr hash table code generation. */
214 #define SH_PREFIX saophash
215 #define SH_ELEMENT_TYPE ScalarArrayOpExprHashEntry
216 #define SH_KEY_TYPE Datum
217 #define SH_KEY key
218 #define SH_HASH_KEY(tb, key) saop_element_hash(tb, key)
219 #define SH_EQUAL(tb, a, b) saop_hash_element_match(tb, a, b)
220 #define SH_SCOPE static inline
221 #define SH_STORE_HASH
222 #define SH_GET_HASH(tb, a) a->hash
223 #define SH_DEFINE
224 #include "lib/simplehash.h"
225 
226 /*
227  * Prepare ExprState for interpreted execution.
228  */
229 void
231 {
232  /* Ensure one-time interpreter setup has been done */
234 
235  /* Simple validity checks on expression */
236  Assert(state->steps_len >= 1);
237  Assert(state->steps[state->steps_len - 1].opcode == EEOP_DONE);
238 
239  /*
240  * Don't perform redundant initialization. This is unreachable in current
241  * cases, but might be hit if there's additional expression evaluation
242  * methods that rely on interpreted execution to work.
243  */
245  return;
246 
247  /*
248  * First time through, check whether attribute matches Var. Might not be
249  * ok anymore, due to schema changes. We do that by setting up a callback
250  * that does checking on the first call, which then sets the evalfunc
251  * callback to the actual method of execution.
252  */
254 
255  /* DIRECT_THREADED should not already be set */
256  Assert((state->flags & EEO_FLAG_DIRECT_THREADED) == 0);
257 
258  /*
259  * There shouldn't be any errors before the expression is fully
260  * initialized, and even if so, it'd lead to the expression being
261  * abandoned. So we can set the flag now and save some code.
262  */
264 
265  /*
266  * Select fast-path evalfuncs for very simple expressions. "Starting up"
267  * the full interpreter is a measurable overhead for these, and these
268  * patterns occur often enough to be worth optimizing.
269  */
270  if (state->steps_len == 3)
271  {
272  ExprEvalOp step0 = state->steps[0].opcode;
273  ExprEvalOp step1 = state->steps[1].opcode;
274 
275  if (step0 == EEOP_INNER_FETCHSOME &&
276  step1 == EEOP_INNER_VAR)
277  {
278  state->evalfunc_private = (void *) ExecJustInnerVar;
279  return;
280  }
281  else if (step0 == EEOP_OUTER_FETCHSOME &&
282  step1 == EEOP_OUTER_VAR)
283  {
284  state->evalfunc_private = (void *) ExecJustOuterVar;
285  return;
286  }
287  else if (step0 == EEOP_SCAN_FETCHSOME &&
288  step1 == EEOP_SCAN_VAR)
289  {
290  state->evalfunc_private = (void *) ExecJustScanVar;
291  return;
292  }
293  else if (step0 == EEOP_INNER_FETCHSOME &&
294  step1 == EEOP_ASSIGN_INNER_VAR)
295  {
296  state->evalfunc_private = (void *) ExecJustAssignInnerVar;
297  return;
298  }
299  else if (step0 == EEOP_OUTER_FETCHSOME &&
300  step1 == EEOP_ASSIGN_OUTER_VAR)
301  {
302  state->evalfunc_private = (void *) ExecJustAssignOuterVar;
303  return;
304  }
305  else if (step0 == EEOP_SCAN_FETCHSOME &&
306  step1 == EEOP_ASSIGN_SCAN_VAR)
307  {
308  state->evalfunc_private = (void *) ExecJustAssignScanVar;
309  return;
310  }
311  else if (step0 == EEOP_CASE_TESTVAL &&
312  step1 == EEOP_FUNCEXPR_STRICT &&
313  state->steps[0].d.casetest.value)
314  {
315  state->evalfunc_private = (void *) ExecJustApplyFuncToCase;
316  return;
317  }
318  }
319  else if (state->steps_len == 2)
320  {
321  ExprEvalOp step0 = state->steps[0].opcode;
322 
323  if (step0 == EEOP_CONST)
324  {
325  state->evalfunc_private = (void *) ExecJustConst;
326  return;
327  }
328  else if (step0 == EEOP_INNER_VAR)
329  {
330  state->evalfunc_private = (void *) ExecJustInnerVarVirt;
331  return;
332  }
333  else if (step0 == EEOP_OUTER_VAR)
334  {
335  state->evalfunc_private = (void *) ExecJustOuterVarVirt;
336  return;
337  }
338  else if (step0 == EEOP_SCAN_VAR)
339  {
340  state->evalfunc_private = (void *) ExecJustScanVarVirt;
341  return;
342  }
343  else if (step0 == EEOP_ASSIGN_INNER_VAR)
344  {
346  return;
347  }
348  else if (step0 == EEOP_ASSIGN_OUTER_VAR)
349  {
351  return;
352  }
353  else if (step0 == EEOP_ASSIGN_SCAN_VAR)
354  {
356  return;
357  }
358  }
359 
360 #if defined(EEO_USE_COMPUTED_GOTO)
361 
362  /*
363  * In the direct-threaded implementation, replace each opcode with the
364  * address to jump to. (Use ExecEvalStepOp() to get back the opcode.)
365  */
366  for (int off = 0; off < state->steps_len; off++)
367  {
368  ExprEvalStep *op = &state->steps[off];
369 
370  op->opcode = EEO_OPCODE(op->opcode);
371  }
372 
374 #endif /* EEO_USE_COMPUTED_GOTO */
375 
376  state->evalfunc_private = (void *) ExecInterpExpr;
377 }
378 
379 
380 /*
381  * Evaluate expression identified by "state" in the execution context
382  * given by "econtext". *isnull is set to the is-null flag for the result,
383  * and the Datum value is the function result.
384  *
385  * As a special case, return the dispatch table's address if state is NULL.
386  * This is used by ExecInitInterpreter to set up the dispatch_table global.
387  * (Only applies when EEO_USE_COMPUTED_GOTO is defined.)
388  */
389 static Datum
391 {
392  ExprEvalStep *op;
393  TupleTableSlot *resultslot;
394  TupleTableSlot *innerslot;
395  TupleTableSlot *outerslot;
396  TupleTableSlot *scanslot;
397 
398  /*
399  * This array has to be in the same order as enum ExprEvalOp.
400  */
401 #if defined(EEO_USE_COMPUTED_GOTO)
402  static const void *const dispatch_table[] = {
403  &&CASE_EEOP_DONE,
404  &&CASE_EEOP_INNER_FETCHSOME,
405  &&CASE_EEOP_OUTER_FETCHSOME,
406  &&CASE_EEOP_SCAN_FETCHSOME,
407  &&CASE_EEOP_INNER_VAR,
408  &&CASE_EEOP_OUTER_VAR,
409  &&CASE_EEOP_SCAN_VAR,
410  &&CASE_EEOP_INNER_SYSVAR,
411  &&CASE_EEOP_OUTER_SYSVAR,
412  &&CASE_EEOP_SCAN_SYSVAR,
413  &&CASE_EEOP_WHOLEROW,
414  &&CASE_EEOP_ASSIGN_INNER_VAR,
415  &&CASE_EEOP_ASSIGN_OUTER_VAR,
416  &&CASE_EEOP_ASSIGN_SCAN_VAR,
417  &&CASE_EEOP_ASSIGN_TMP,
418  &&CASE_EEOP_ASSIGN_TMP_MAKE_RO,
419  &&CASE_EEOP_CONST,
420  &&CASE_EEOP_FUNCEXPR,
421  &&CASE_EEOP_FUNCEXPR_STRICT,
422  &&CASE_EEOP_FUNCEXPR_FUSAGE,
423  &&CASE_EEOP_FUNCEXPR_STRICT_FUSAGE,
424  &&CASE_EEOP_BOOL_AND_STEP_FIRST,
425  &&CASE_EEOP_BOOL_AND_STEP,
426  &&CASE_EEOP_BOOL_AND_STEP_LAST,
427  &&CASE_EEOP_BOOL_OR_STEP_FIRST,
428  &&CASE_EEOP_BOOL_OR_STEP,
429  &&CASE_EEOP_BOOL_OR_STEP_LAST,
430  &&CASE_EEOP_BOOL_NOT_STEP,
431  &&CASE_EEOP_QUAL,
432  &&CASE_EEOP_JUMP,
433  &&CASE_EEOP_JUMP_IF_NULL,
434  &&CASE_EEOP_JUMP_IF_NOT_NULL,
435  &&CASE_EEOP_JUMP_IF_NOT_TRUE,
436  &&CASE_EEOP_NULLTEST_ISNULL,
437  &&CASE_EEOP_NULLTEST_ISNOTNULL,
438  &&CASE_EEOP_NULLTEST_ROWISNULL,
439  &&CASE_EEOP_NULLTEST_ROWISNOTNULL,
440  &&CASE_EEOP_BOOLTEST_IS_TRUE,
441  &&CASE_EEOP_BOOLTEST_IS_NOT_TRUE,
442  &&CASE_EEOP_BOOLTEST_IS_FALSE,
443  &&CASE_EEOP_BOOLTEST_IS_NOT_FALSE,
444  &&CASE_EEOP_PARAM_EXEC,
445  &&CASE_EEOP_PARAM_EXTERN,
446  &&CASE_EEOP_PARAM_CALLBACK,
447  &&CASE_EEOP_CASE_TESTVAL,
448  &&CASE_EEOP_MAKE_READONLY,
449  &&CASE_EEOP_IOCOERCE,
450  &&CASE_EEOP_DISTINCT,
451  &&CASE_EEOP_NOT_DISTINCT,
452  &&CASE_EEOP_NULLIF,
453  &&CASE_EEOP_SQLVALUEFUNCTION,
454  &&CASE_EEOP_CURRENTOFEXPR,
455  &&CASE_EEOP_NEXTVALUEEXPR,
456  &&CASE_EEOP_ARRAYEXPR,
457  &&CASE_EEOP_ARRAYCOERCE,
458  &&CASE_EEOP_ROW,
459  &&CASE_EEOP_ROWCOMPARE_STEP,
460  &&CASE_EEOP_ROWCOMPARE_FINAL,
461  &&CASE_EEOP_MINMAX,
462  &&CASE_EEOP_FIELDSELECT,
463  &&CASE_EEOP_FIELDSTORE_DEFORM,
464  &&CASE_EEOP_FIELDSTORE_FORM,
465  &&CASE_EEOP_SBSREF_SUBSCRIPTS,
466  &&CASE_EEOP_SBSREF_OLD,
467  &&CASE_EEOP_SBSREF_ASSIGN,
468  &&CASE_EEOP_SBSREF_FETCH,
469  &&CASE_EEOP_DOMAIN_TESTVAL,
470  &&CASE_EEOP_DOMAIN_NOTNULL,
471  &&CASE_EEOP_DOMAIN_CHECK,
472  &&CASE_EEOP_CONVERT_ROWTYPE,
473  &&CASE_EEOP_SCALARARRAYOP,
474  &&CASE_EEOP_HASHED_SCALARARRAYOP,
475  &&CASE_EEOP_XMLEXPR,
476  &&CASE_EEOP_AGGREF,
477  &&CASE_EEOP_GROUPING_FUNC,
478  &&CASE_EEOP_WINDOW_FUNC,
479  &&CASE_EEOP_SUBPLAN,
480  &&CASE_EEOP_AGG_STRICT_DESERIALIZE,
481  &&CASE_EEOP_AGG_DESERIALIZE,
482  &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_ARGS,
483  &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_NULLS,
484  &&CASE_EEOP_AGG_PLAIN_PERGROUP_NULLCHECK,
485  &&CASE_EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL,
486  &&CASE_EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL,
487  &&CASE_EEOP_AGG_PLAIN_TRANS_BYVAL,
488  &&CASE_EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF,
489  &&CASE_EEOP_AGG_PLAIN_TRANS_STRICT_BYREF,
490  &&CASE_EEOP_AGG_PLAIN_TRANS_BYREF,
491  &&CASE_EEOP_AGG_ORDERED_TRANS_DATUM,
492  &&CASE_EEOP_AGG_ORDERED_TRANS_TUPLE,
493  &&CASE_EEOP_LAST
494  };
495 
496  StaticAssertStmt(EEOP_LAST + 1 == lengthof(dispatch_table),
497  "dispatch_table out of whack with ExprEvalOp");
498 
499  if (unlikely(state == NULL))
500  return PointerGetDatum(dispatch_table);
501 #else
502  Assert(state != NULL);
503 #endif /* EEO_USE_COMPUTED_GOTO */
504 
505  /* setup state */
506  op = state->steps;
507  resultslot = state->resultslot;
508  innerslot = econtext->ecxt_innertuple;
509  outerslot = econtext->ecxt_outertuple;
510  scanslot = econtext->ecxt_scantuple;
511 
512 #if defined(EEO_USE_COMPUTED_GOTO)
513  EEO_DISPATCH();
514 #endif
515 
516  EEO_SWITCH()
517  {
519  {
520  goto out;
521  }
522 
524  {
525  CheckOpSlotCompatibility(op, innerslot);
526 
527  slot_getsomeattrs(innerslot, op->d.fetch.last_var);
528 
529  EEO_NEXT();
530  }
531 
533  {
534  CheckOpSlotCompatibility(op, outerslot);
535 
536  slot_getsomeattrs(outerslot, op->d.fetch.last_var);
537 
538  EEO_NEXT();
539  }
540 
542  {
543  CheckOpSlotCompatibility(op, scanslot);
544 
545  slot_getsomeattrs(scanslot, op->d.fetch.last_var);
546 
547  EEO_NEXT();
548  }
549 
551  {
552  int attnum = op->d.var.attnum;
553 
554  /*
555  * Since we already extracted all referenced columns from the
556  * tuple with a FETCHSOME step, we can just grab the value
557  * directly out of the slot's decomposed-data arrays. But let's
558  * have an Assert to check that that did happen.
559  */
560  Assert(attnum >= 0 && attnum < innerslot->tts_nvalid);
561  *op->resvalue = innerslot->tts_values[attnum];
562  *op->resnull = innerslot->tts_isnull[attnum];
563 
564  EEO_NEXT();
565  }
566 
568  {
569  int attnum = op->d.var.attnum;
570 
571  /* See EEOP_INNER_VAR comments */
572 
573  Assert(attnum >= 0 && attnum < outerslot->tts_nvalid);
574  *op->resvalue = outerslot->tts_values[attnum];
575  *op->resnull = outerslot->tts_isnull[attnum];
576 
577  EEO_NEXT();
578  }
579 
581  {
582  int attnum = op->d.var.attnum;
583 
584  /* See EEOP_INNER_VAR comments */
585 
586  Assert(attnum >= 0 && attnum < scanslot->tts_nvalid);
587  *op->resvalue = scanslot->tts_values[attnum];
588  *op->resnull = scanslot->tts_isnull[attnum];
589 
590  EEO_NEXT();
591  }
592 
594  {
595  ExecEvalSysVar(state, op, econtext, innerslot);
596  EEO_NEXT();
597  }
598 
600  {
601  ExecEvalSysVar(state, op, econtext, outerslot);
602  EEO_NEXT();
603  }
604 
606  {
607  ExecEvalSysVar(state, op, econtext, scanslot);
608  EEO_NEXT();
609  }
610 
612  {
613  /* too complex for an inline implementation */
614  ExecEvalWholeRowVar(state, op, econtext);
615 
616  EEO_NEXT();
617  }
618 
620  {
621  int resultnum = op->d.assign_var.resultnum;
622  int attnum = op->d.assign_var.attnum;
623 
624  /*
625  * We do not need CheckVarSlotCompatibility here; that was taken
626  * care of at compilation time. But see EEOP_INNER_VAR comments.
627  */
628  Assert(attnum >= 0 && attnum < innerslot->tts_nvalid);
629  Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
630  resultslot->tts_values[resultnum] = innerslot->tts_values[attnum];
631  resultslot->tts_isnull[resultnum] = innerslot->tts_isnull[attnum];
632 
633  EEO_NEXT();
634  }
635 
637  {
638  int resultnum = op->d.assign_var.resultnum;
639  int attnum = op->d.assign_var.attnum;
640 
641  /*
642  * We do not need CheckVarSlotCompatibility here; that was taken
643  * care of at compilation time. But see EEOP_INNER_VAR comments.
644  */
645  Assert(attnum >= 0 && attnum < outerslot->tts_nvalid);
646  Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
647  resultslot->tts_values[resultnum] = outerslot->tts_values[attnum];
648  resultslot->tts_isnull[resultnum] = outerslot->tts_isnull[attnum];
649 
650  EEO_NEXT();
651  }
652 
654  {
655  int resultnum = op->d.assign_var.resultnum;
656  int attnum = op->d.assign_var.attnum;
657 
658  /*
659  * We do not need CheckVarSlotCompatibility here; that was taken
660  * care of at compilation time. But see EEOP_INNER_VAR comments.
661  */
662  Assert(attnum >= 0 && attnum < scanslot->tts_nvalid);
663  Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
664  resultslot->tts_values[resultnum] = scanslot->tts_values[attnum];
665  resultslot->tts_isnull[resultnum] = scanslot->tts_isnull[attnum];
666 
667  EEO_NEXT();
668  }
669 
671  {
672  int resultnum = op->d.assign_tmp.resultnum;
673 
674  Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
675  resultslot->tts_values[resultnum] = state->resvalue;
676  resultslot->tts_isnull[resultnum] = state->resnull;
677 
678  EEO_NEXT();
679  }
680 
682  {
683  int resultnum = op->d.assign_tmp.resultnum;
684 
685  Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
686  resultslot->tts_isnull[resultnum] = state->resnull;
687  if (!resultslot->tts_isnull[resultnum])
688  resultslot->tts_values[resultnum] =
690  else
691  resultslot->tts_values[resultnum] = state->resvalue;
692 
693  EEO_NEXT();
694  }
695 
697  {
698  *op->resnull = op->d.constval.isnull;
699  *op->resvalue = op->d.constval.value;
700 
701  EEO_NEXT();
702  }
703 
704  /*
705  * Function-call implementations. Arguments have previously been
706  * evaluated directly into fcinfo->args.
707  *
708  * As both STRICT checks and function-usage are noticeable performance
709  * wise, and function calls are a very hot-path (they also back
710  * operators!), it's worth having so many separate opcodes.
711  *
712  * Note: the reason for using a temporary variable "d", here and in
713  * other places, is that some compilers think "*op->resvalue = f();"
714  * requires them to evaluate op->resvalue into a register before
715  * calling f(), just in case f() is able to modify op->resvalue
716  * somehow. The extra line of code can save a useless register spill
717  * and reload across the function call.
718  */
720  {
721  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
722  Datum d;
723 
724  fcinfo->isnull = false;
725  d = op->d.func.fn_addr(fcinfo);
726  *op->resvalue = d;
727  *op->resnull = fcinfo->isnull;
728 
729  EEO_NEXT();
730  }
731 
733  {
734  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
735  NullableDatum *args = fcinfo->args;
736  int nargs = op->d.func.nargs;
737  Datum d;
738 
739  /* strict function, so check for NULL args */
740  for (int argno = 0; argno < nargs; argno++)
741  {
742  if (args[argno].isnull)
743  {
744  *op->resnull = true;
745  goto strictfail;
746  }
747  }
748  fcinfo->isnull = false;
749  d = op->d.func.fn_addr(fcinfo);
750  *op->resvalue = d;
751  *op->resnull = fcinfo->isnull;
752 
753  strictfail:
754  EEO_NEXT();
755  }
756 
758  {
759  /* not common enough to inline */
760  ExecEvalFuncExprFusage(state, op, econtext);
761 
762  EEO_NEXT();
763  }
764 
766  {
767  /* not common enough to inline */
768  ExecEvalFuncExprStrictFusage(state, op, econtext);
769 
770  EEO_NEXT();
771  }
772 
773  /*
774  * If any of its clauses is FALSE, an AND's result is FALSE regardless
775  * of the states of the rest of the clauses, so we can stop evaluating
776  * and return FALSE immediately. If none are FALSE and one or more is
777  * NULL, we return NULL; otherwise we return TRUE. This makes sense
778  * when you interpret NULL as "don't know": perhaps one of the "don't
779  * knows" would have been FALSE if we'd known its value. Only when
780  * all the inputs are known to be TRUE can we state confidently that
781  * the AND's result is TRUE.
782  */
784  {
785  *op->d.boolexpr.anynull = false;
786 
787  /*
788  * EEOP_BOOL_AND_STEP_FIRST resets anynull, otherwise it's the
789  * same as EEOP_BOOL_AND_STEP - so fall through to that.
790  */
791 
792  /* FALL THROUGH */
793  }
794 
796  {
797  if (*op->resnull)
798  {
799  *op->d.boolexpr.anynull = true;
800  }
801  else if (!DatumGetBool(*op->resvalue))
802  {
803  /* result is already set to FALSE, need not change it */
804  /* bail out early */
805  EEO_JUMP(op->d.boolexpr.jumpdone);
806  }
807 
808  EEO_NEXT();
809  }
810 
812  {
813  if (*op->resnull)
814  {
815  /* result is already set to NULL, need not change it */
816  }
817  else if (!DatumGetBool(*op->resvalue))
818  {
819  /* result is already set to FALSE, need not change it */
820 
821  /*
822  * No point jumping early to jumpdone - would be same target
823  * (as this is the last argument to the AND expression),
824  * except more expensive.
825  */
826  }
827  else if (*op->d.boolexpr.anynull)
828  {
829  *op->resvalue = (Datum) 0;
830  *op->resnull = true;
831  }
832  else
833  {
834  /* result is already set to TRUE, need not change it */
835  }
836 
837  EEO_NEXT();
838  }
839 
840  /*
841  * If any of its clauses is TRUE, an OR's result is TRUE regardless of
842  * the states of the rest of the clauses, so we can stop evaluating
843  * and return TRUE immediately. If none are TRUE and one or more is
844  * NULL, we return NULL; otherwise we return FALSE. This makes sense
845  * when you interpret NULL as "don't know": perhaps one of the "don't
846  * knows" would have been TRUE if we'd known its value. Only when all
847  * the inputs are known to be FALSE can we state confidently that the
848  * OR's result is FALSE.
849  */
851  {
852  *op->d.boolexpr.anynull = false;
853 
854  /*
855  * EEOP_BOOL_OR_STEP_FIRST resets anynull, otherwise it's the same
856  * as EEOP_BOOL_OR_STEP - so fall through to that.
857  */
858 
859  /* FALL THROUGH */
860  }
861 
863  {
864  if (*op->resnull)
865  {
866  *op->d.boolexpr.anynull = true;
867  }
868  else if (DatumGetBool(*op->resvalue))
869  {
870  /* result is already set to TRUE, need not change it */
871  /* bail out early */
872  EEO_JUMP(op->d.boolexpr.jumpdone);
873  }
874 
875  EEO_NEXT();
876  }
877 
879  {
880  if (*op->resnull)
881  {
882  /* result is already set to NULL, need not change it */
883  }
884  else if (DatumGetBool(*op->resvalue))
885  {
886  /* result is already set to TRUE, need not change it */
887 
888  /*
889  * No point jumping to jumpdone - would be same target (as
890  * this is the last argument to the AND expression), except
891  * more expensive.
892  */
893  }
894  else if (*op->d.boolexpr.anynull)
895  {
896  *op->resvalue = (Datum) 0;
897  *op->resnull = true;
898  }
899  else
900  {
901  /* result is already set to FALSE, need not change it */
902  }
903 
904  EEO_NEXT();
905  }
906 
908  {
909  /*
910  * Evaluation of 'not' is simple... if expr is false, then return
911  * 'true' and vice versa. It's safe to do this even on a
912  * nominally null value, so we ignore resnull; that means that
913  * NULL in produces NULL out, which is what we want.
914  */
916 
917  EEO_NEXT();
918  }
919 
921  {
922  /* simplified version of BOOL_AND_STEP for use by ExecQual() */
923 
924  /* If argument (also result) is false or null ... */
925  if (*op->resnull ||
926  !DatumGetBool(*op->resvalue))
927  {
928  /* ... bail out early, returning FALSE */
929  *op->resnull = false;
930  *op->resvalue = BoolGetDatum(false);
931  EEO_JUMP(op->d.qualexpr.jumpdone);
932  }
933 
934  /*
935  * Otherwise, leave the TRUE value in place, in case this is the
936  * last qual. Then, TRUE is the correct answer.
937  */
938 
939  EEO_NEXT();
940  }
941 
943  {
944  /* Unconditionally jump to target step */
945  EEO_JUMP(op->d.jump.jumpdone);
946  }
947 
949  {
950  /* Transfer control if current result is null */
951  if (*op->resnull)
952  EEO_JUMP(op->d.jump.jumpdone);
953 
954  EEO_NEXT();
955  }
956 
958  {
959  /* Transfer control if current result is non-null */
960  if (!*op->resnull)
961  EEO_JUMP(op->d.jump.jumpdone);
962 
963  EEO_NEXT();
964  }
965 
967  {
968  /* Transfer control if current result is null or false */
969  if (*op->resnull || !DatumGetBool(*op->resvalue))
970  EEO_JUMP(op->d.jump.jumpdone);
971 
972  EEO_NEXT();
973  }
974 
976  {
977  *op->resvalue = BoolGetDatum(*op->resnull);
978  *op->resnull = false;
979 
980  EEO_NEXT();
981  }
982 
984  {
985  *op->resvalue = BoolGetDatum(!*op->resnull);
986  *op->resnull = false;
987 
988  EEO_NEXT();
989  }
990 
992  {
993  /* out of line implementation: too large */
994  ExecEvalRowNull(state, op, econtext);
995 
996  EEO_NEXT();
997  }
998 
1000  {
1001  /* out of line implementation: too large */
1002  ExecEvalRowNotNull(state, op, econtext);
1003 
1004  EEO_NEXT();
1005  }
1006 
1007  /* BooleanTest implementations for all booltesttypes */
1008 
1010  {
1011  if (*op->resnull)
1012  {
1013  *op->resvalue = BoolGetDatum(false);
1014  *op->resnull = false;
1015  }
1016  /* else, input value is the correct output as well */
1017 
1018  EEO_NEXT();
1019  }
1020 
1022  {
1023  if (*op->resnull)
1024  {
1025  *op->resvalue = BoolGetDatum(true);
1026  *op->resnull = false;
1027  }
1028  else
1029  *op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue));
1030 
1031  EEO_NEXT();
1032  }
1033 
1035  {
1036  if (*op->resnull)
1037  {
1038  *op->resvalue = BoolGetDatum(false);
1039  *op->resnull = false;
1040  }
1041  else
1042  *op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue));
1043 
1044  EEO_NEXT();
1045  }
1046 
1048  {
1049  if (*op->resnull)
1050  {
1051  *op->resvalue = BoolGetDatum(true);
1052  *op->resnull = false;
1053  }
1054  /* else, input value is the correct output as well */
1055 
1056  EEO_NEXT();
1057  }
1058 
1060  {
1061  /* out of line implementation: too large */
1062  ExecEvalParamExec(state, op, econtext);
1063 
1064  EEO_NEXT();
1065  }
1066 
1068  {
1069  /* out of line implementation: too large */
1070  ExecEvalParamExtern(state, op, econtext);
1071  EEO_NEXT();
1072  }
1073 
1075  {
1076  /* allow an extension module to supply a PARAM_EXTERN value */
1077  op->d.cparam.paramfunc(state, op, econtext);
1078  EEO_NEXT();
1079  }
1080 
1082  {
1083  /*
1084  * Normally upper parts of the expression tree have setup the
1085  * values to be returned here, but some parts of the system
1086  * currently misuse {caseValue,domainValue}_{datum,isNull} to set
1087  * run-time data. So if no values have been set-up, use
1088  * ExprContext's. This isn't pretty, but also not *that* ugly,
1089  * and this is unlikely to be performance sensitive enough to
1090  * worry about an extra branch.
1091  */
1092  if (op->d.casetest.value)
1093  {
1094  *op->resvalue = *op->d.casetest.value;
1095  *op->resnull = *op->d.casetest.isnull;
1096  }
1097  else
1098  {
1099  *op->resvalue = econtext->caseValue_datum;
1100  *op->resnull = econtext->caseValue_isNull;
1101  }
1102 
1103  EEO_NEXT();
1104  }
1105 
1107  {
1108  /*
1109  * See EEOP_CASE_TESTVAL comment.
1110  */
1111  if (op->d.casetest.value)
1112  {
1113  *op->resvalue = *op->d.casetest.value;
1114  *op->resnull = *op->d.casetest.isnull;
1115  }
1116  else
1117  {
1118  *op->resvalue = econtext->domainValue_datum;
1119  *op->resnull = econtext->domainValue_isNull;
1120  }
1121 
1122  EEO_NEXT();
1123  }
1124 
1126  {
1127  /*
1128  * Force a varlena value that might be read multiple times to R/O
1129  */
1130  if (!*op->d.make_readonly.isnull)
1131  *op->resvalue =
1133  *op->resnull = *op->d.make_readonly.isnull;
1134 
1135  EEO_NEXT();
1136  }
1137 
1139  {
1140  /*
1141  * Evaluate a CoerceViaIO node. This can be quite a hot path, so
1142  * inline as much work as possible. The source value is in our
1143  * result variable.
1144  */
1145  char *str;
1146 
1147  /* call output function (similar to OutputFunctionCall) */
1148  if (*op->resnull)
1149  {
1150  /* output functions are not called on nulls */
1151  str = NULL;
1152  }
1153  else
1154  {
1155  FunctionCallInfo fcinfo_out;
1156 
1157  fcinfo_out = op->d.iocoerce.fcinfo_data_out;
1158  fcinfo_out->args[0].value = *op->resvalue;
1159  fcinfo_out->args[0].isnull = false;
1160 
1161  fcinfo_out->isnull = false;
1162  str = DatumGetCString(FunctionCallInvoke(fcinfo_out));
1163 
1164  /* OutputFunctionCall assumes result isn't null */
1165  Assert(!fcinfo_out->isnull);
1166  }
1167 
1168  /* call input function (similar to InputFunctionCall) */
1169  if (!op->d.iocoerce.finfo_in->fn_strict || str != NULL)
1170  {
1171  FunctionCallInfo fcinfo_in;
1172  Datum d;
1173 
1174  fcinfo_in = op->d.iocoerce.fcinfo_data_in;
1175  fcinfo_in->args[0].value = PointerGetDatum(str);
1176  fcinfo_in->args[0].isnull = *op->resnull;
1177  /* second and third arguments are already set up */
1178 
1179  fcinfo_in->isnull = false;
1180  d = FunctionCallInvoke(fcinfo_in);
1181  *op->resvalue = d;
1182 
1183  /* Should get null result if and only if str is NULL */
1184  if (str == NULL)
1185  {
1186  Assert(*op->resnull);
1187  Assert(fcinfo_in->isnull);
1188  }
1189  else
1190  {
1191  Assert(!*op->resnull);
1192  Assert(!fcinfo_in->isnull);
1193  }
1194  }
1195 
1196  EEO_NEXT();
1197  }
1198 
1200  {
1201  /*
1202  * IS DISTINCT FROM must evaluate arguments (already done into
1203  * fcinfo->args) to determine whether they are NULL; if either is
1204  * NULL then the result is determined. If neither is NULL, then
1205  * proceed to evaluate the comparison function, which is just the
1206  * type's standard equality operator. We need not care whether
1207  * that function is strict. Because the handling of nulls is
1208  * different, we can't just reuse EEOP_FUNCEXPR.
1209  */
1210  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
1211 
1212  /* check function arguments for NULLness */
1213  if (fcinfo->args[0].isnull && fcinfo->args[1].isnull)
1214  {
1215  /* Both NULL? Then is not distinct... */
1216  *op->resvalue = BoolGetDatum(false);
1217  *op->resnull = false;
1218  }
1219  else if (fcinfo->args[0].isnull || fcinfo->args[1].isnull)
1220  {
1221  /* Only one is NULL? Then is distinct... */
1222  *op->resvalue = BoolGetDatum(true);
1223  *op->resnull = false;
1224  }
1225  else
1226  {
1227  /* Neither null, so apply the equality function */
1228  Datum eqresult;
1229 
1230  fcinfo->isnull = false;
1231  eqresult = op->d.func.fn_addr(fcinfo);
1232  /* Must invert result of "="; safe to do even if null */
1233  *op->resvalue = BoolGetDatum(!DatumGetBool(eqresult));
1234  *op->resnull = fcinfo->isnull;
1235  }
1236 
1237  EEO_NEXT();
1238  }
1239 
1240  /* see EEOP_DISTINCT for comments, this is just inverted */
1242  {
1243  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
1244 
1245  if (fcinfo->args[0].isnull && fcinfo->args[1].isnull)
1246  {
1247  *op->resvalue = BoolGetDatum(true);
1248  *op->resnull = false;
1249  }
1250  else if (fcinfo->args[0].isnull || fcinfo->args[1].isnull)
1251  {
1252  *op->resvalue = BoolGetDatum(false);
1253  *op->resnull = false;
1254  }
1255  else
1256  {
1257  Datum eqresult;
1258 
1259  fcinfo->isnull = false;
1260  eqresult = op->d.func.fn_addr(fcinfo);
1261  *op->resvalue = eqresult;
1262  *op->resnull = fcinfo->isnull;
1263  }
1264 
1265  EEO_NEXT();
1266  }
1267 
1269  {
1270  /*
1271  * The arguments are already evaluated into fcinfo->args.
1272  */
1273  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
1274 
1275  /* if either argument is NULL they can't be equal */
1276  if (!fcinfo->args[0].isnull && !fcinfo->args[1].isnull)
1277  {
1278  Datum result;
1279 
1280  fcinfo->isnull = false;
1281  result = op->d.func.fn_addr(fcinfo);
1282 
1283  /* if the arguments are equal return null */
1284  if (!fcinfo->isnull && DatumGetBool(result))
1285  {
1286  *op->resvalue = (Datum) 0;
1287  *op->resnull = true;
1288 
1289  EEO_NEXT();
1290  }
1291  }
1292 
1293  /* Arguments aren't equal, so return the first one */
1294  *op->resvalue = fcinfo->args[0].value;
1295  *op->resnull = fcinfo->args[0].isnull;
1296 
1297  EEO_NEXT();
1298  }
1299 
1301  {
1302  /*
1303  * Doesn't seem worthwhile to have an inline implementation
1304  * efficiency-wise.
1305  */
1306  ExecEvalSQLValueFunction(state, op);
1307 
1308  EEO_NEXT();
1309  }
1310 
1312  {
1313  /* error invocation uses space, and shouldn't ever occur */
1314  ExecEvalCurrentOfExpr(state, op);
1315 
1316  EEO_NEXT();
1317  }
1318 
1320  {
1321  /*
1322  * Doesn't seem worthwhile to have an inline implementation
1323  * efficiency-wise.
1324  */
1325  ExecEvalNextValueExpr(state, op);
1326 
1327  EEO_NEXT();
1328  }
1329 
1331  {
1332  /* too complex for an inline implementation */
1333  ExecEvalArrayExpr(state, op);
1334 
1335  EEO_NEXT();
1336  }
1337 
1339  {
1340  /* too complex for an inline implementation */
1341  ExecEvalArrayCoerce(state, op, econtext);
1342 
1343  EEO_NEXT();
1344  }
1345 
1347  {
1348  /* too complex for an inline implementation */
1349  ExecEvalRow(state, op);
1350 
1351  EEO_NEXT();
1352  }
1353 
1355  {
1356  FunctionCallInfo fcinfo = op->d.rowcompare_step.fcinfo_data;
1357  Datum d;
1358 
1359  /* force NULL result if strict fn and NULL input */
1360  if (op->d.rowcompare_step.finfo->fn_strict &&
1361  (fcinfo->args[0].isnull || fcinfo->args[1].isnull))
1362  {
1363  *op->resnull = true;
1364  EEO_JUMP(op->d.rowcompare_step.jumpnull);
1365  }
1366 
1367  /* Apply comparison function */
1368  fcinfo->isnull = false;
1369  d = op->d.rowcompare_step.fn_addr(fcinfo);
1370  *op->resvalue = d;
1371 
1372  /* force NULL result if NULL function result */
1373  if (fcinfo->isnull)
1374  {
1375  *op->resnull = true;
1376  EEO_JUMP(op->d.rowcompare_step.jumpnull);
1377  }
1378  *op->resnull = false;
1379 
1380  /* If unequal, no need to compare remaining columns */
1381  if (DatumGetInt32(*op->resvalue) != 0)
1382  {
1383  EEO_JUMP(op->d.rowcompare_step.jumpdone);
1384  }
1385 
1386  EEO_NEXT();
1387  }
1388 
1390  {
1391  int32 cmpresult = DatumGetInt32(*op->resvalue);
1392  RowCompareType rctype = op->d.rowcompare_final.rctype;
1393 
1394  *op->resnull = false;
1395  switch (rctype)
1396  {
1397  /* EQ and NE cases aren't allowed here */
1398  case ROWCOMPARE_LT:
1399  *op->resvalue = BoolGetDatum(cmpresult < 0);
1400  break;
1401  case ROWCOMPARE_LE:
1402  *op->resvalue = BoolGetDatum(cmpresult <= 0);
1403  break;
1404  case ROWCOMPARE_GE:
1405  *op->resvalue = BoolGetDatum(cmpresult >= 0);
1406  break;
1407  case ROWCOMPARE_GT:
1408  *op->resvalue = BoolGetDatum(cmpresult > 0);
1409  break;
1410  default:
1411  Assert(false);
1412  break;
1413  }
1414 
1415  EEO_NEXT();
1416  }
1417 
1419  {
1420  /* too complex for an inline implementation */
1421  ExecEvalMinMax(state, op);
1422 
1423  EEO_NEXT();
1424  }
1425 
1427  {
1428  /* too complex for an inline implementation */
1429  ExecEvalFieldSelect(state, op, econtext);
1430 
1431  EEO_NEXT();
1432  }
1433 
1435  {
1436  /* too complex for an inline implementation */
1437  ExecEvalFieldStoreDeForm(state, op, econtext);
1438 
1439  EEO_NEXT();
1440  }
1441 
1443  {
1444  /* too complex for an inline implementation */
1445  ExecEvalFieldStoreForm(state, op, econtext);
1446 
1447  EEO_NEXT();
1448  }
1449 
1451  {
1452  /* Precheck SubscriptingRef subscript(s) */
1453  if (op->d.sbsref_subscript.subscriptfunc(state, op, econtext))
1454  {
1455  EEO_NEXT();
1456  }
1457  else
1458  {
1459  /* Subscript is null, short-circuit SubscriptingRef to NULL */
1460  EEO_JUMP(op->d.sbsref_subscript.jumpdone);
1461  }
1462  }
1463 
1467  {
1468  /* Perform a SubscriptingRef fetch or assignment */
1469  op->d.sbsref.subscriptfunc(state, op, econtext);
1470 
1471  EEO_NEXT();
1472  }
1473 
1475  {
1476  /* too complex for an inline implementation */
1477  ExecEvalConvertRowtype(state, op, econtext);
1478 
1479  EEO_NEXT();
1480  }
1481 
1483  {
1484  /* too complex for an inline implementation */
1485  ExecEvalScalarArrayOp(state, op);
1486 
1487  EEO_NEXT();
1488  }
1489 
1491  {
1492  /* too complex for an inline implementation */
1493  ExecEvalHashedScalarArrayOp(state, op, econtext);
1494 
1495  EEO_NEXT();
1496  }
1497 
1499  {
1500  /* too complex for an inline implementation */
1501  ExecEvalConstraintNotNull(state, op);
1502 
1503  EEO_NEXT();
1504  }
1505 
1507  {
1508  /* too complex for an inline implementation */
1509  ExecEvalConstraintCheck(state, op);
1510 
1511  EEO_NEXT();
1512  }
1513 
1515  {
1516  /* too complex for an inline implementation */
1517  ExecEvalXmlExpr(state, op);
1518 
1519  EEO_NEXT();
1520  }
1521 
1523  {
1524  /*
1525  * Returns a Datum whose value is the precomputed aggregate value
1526  * found in the given expression context.
1527  */
1528  int aggno = op->d.aggref.aggno;
1529 
1530  Assert(econtext->ecxt_aggvalues != NULL);
1531 
1532  *op->resvalue = econtext->ecxt_aggvalues[aggno];
1533  *op->resnull = econtext->ecxt_aggnulls[aggno];
1534 
1535  EEO_NEXT();
1536  }
1537 
1539  {
1540  /* too complex/uncommon for an inline implementation */
1541  ExecEvalGroupingFunc(state, op);
1542 
1543  EEO_NEXT();
1544  }
1545 
1547  {
1548  /*
1549  * Like Aggref, just return a precomputed value from the econtext.
1550  */
1551  WindowFuncExprState *wfunc = op->d.window_func.wfstate;
1552 
1553  Assert(econtext->ecxt_aggvalues != NULL);
1554 
1555  *op->resvalue = econtext->ecxt_aggvalues[wfunc->wfuncno];
1556  *op->resnull = econtext->ecxt_aggnulls[wfunc->wfuncno];
1557 
1558  EEO_NEXT();
1559  }
1560 
1562  {
1563  /* too complex for an inline implementation */
1564  ExecEvalSubPlan(state, op, econtext);
1565 
1566  EEO_NEXT();
1567  }
1568 
1569  /* evaluate a strict aggregate deserialization function */
1571  {
1572  /* Don't call a strict deserialization function with NULL input */
1573  if (op->d.agg_deserialize.fcinfo_data->args[0].isnull)
1574  EEO_JUMP(op->d.agg_deserialize.jumpnull);
1575 
1576  /* fallthrough */
1577  }
1578 
1579  /* evaluate aggregate deserialization function (non-strict portion) */
1581  {
1582  FunctionCallInfo fcinfo = op->d.agg_deserialize.fcinfo_data;
1583  AggState *aggstate = castNode(AggState, state->parent);
1584  MemoryContext oldContext;
1585 
1586  /*
1587  * We run the deserialization functions in per-input-tuple memory
1588  * context.
1589  */
1590  oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
1591  fcinfo->isnull = false;
1592  *op->resvalue = FunctionCallInvoke(fcinfo);
1593  *op->resnull = fcinfo->isnull;
1594  MemoryContextSwitchTo(oldContext);
1595 
1596  EEO_NEXT();
1597  }
1598 
1599  /*
1600  * Check that a strict aggregate transition / combination function's
1601  * input is not NULL.
1602  */
1603 
1605  {
1607  int nargs = op->d.agg_strict_input_check.nargs;
1608 
1609  for (int argno = 0; argno < nargs; argno++)
1610  {
1611  if (args[argno].isnull)
1612  EEO_JUMP(op->d.agg_strict_input_check.jumpnull);
1613  }
1614  EEO_NEXT();
1615  }
1616 
1618  {
1619  bool *nulls = op->d.agg_strict_input_check.nulls;
1620  int nargs = op->d.agg_strict_input_check.nargs;
1621 
1622  for (int argno = 0; argno < nargs; argno++)
1623  {
1624  if (nulls[argno])
1625  EEO_JUMP(op->d.agg_strict_input_check.jumpnull);
1626  }
1627  EEO_NEXT();
1628  }
1629 
1630  /*
1631  * Check for a NULL pointer to the per-group states.
1632  */
1633 
1635  {
1636  AggState *aggstate = castNode(AggState, state->parent);
1637  AggStatePerGroup pergroup_allaggs =
1638  aggstate->all_pergroups[op->d.agg_plain_pergroup_nullcheck.setoff];
1639 
1640  if (pergroup_allaggs == NULL)
1641  EEO_JUMP(op->d.agg_plain_pergroup_nullcheck.jumpnull);
1642 
1643  EEO_NEXT();
1644  }
1645 
1646  /*
1647  * Different types of aggregate transition functions are implemented
1648  * as different types of steps, to avoid incurring unnecessary
1649  * overhead. There's a step type for each valid combination of having
1650  * a by value / by reference transition type, [not] needing to the
1651  * initialize the transition value for the first row in a group from
1652  * input, and [not] strict transition function.
1653  *
1654  * Could optimize further by splitting off by-reference for
1655  * fixed-length types, but currently that doesn't seem worth it.
1656  */
1657 
1659  {
1660  AggState *aggstate = castNode(AggState, state->parent);
1661  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
1662  AggStatePerGroup pergroup =
1663  &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
1664 
1665  Assert(pertrans->transtypeByVal);
1666 
1667  if (pergroup->noTransValue)
1668  {
1669  /* If transValue has not yet been initialized, do so now. */
1670  ExecAggInitGroup(aggstate, pertrans, pergroup,
1671  op->d.agg_trans.aggcontext);
1672  /* copied trans value from input, done this round */
1673  }
1674  else if (likely(!pergroup->transValueIsNull))
1675  {
1676  /* invoke transition function, unless prevented by strictness */
1677  ExecAggPlainTransByVal(aggstate, pertrans, pergroup,
1678  op->d.agg_trans.aggcontext,
1679  op->d.agg_trans.setno);
1680  }
1681 
1682  EEO_NEXT();
1683  }
1684 
1685  /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
1687  {
1688  AggState *aggstate = castNode(AggState, state->parent);
1689  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
1690  AggStatePerGroup pergroup =
1691  &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
1692 
1693  Assert(pertrans->transtypeByVal);
1694 
1695  if (likely(!pergroup->transValueIsNull))
1696  ExecAggPlainTransByVal(aggstate, pertrans, pergroup,
1697  op->d.agg_trans.aggcontext,
1698  op->d.agg_trans.setno);
1699 
1700  EEO_NEXT();
1701  }
1702 
1703  /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
1705  {
1706  AggState *aggstate = castNode(AggState, state->parent);
1707  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
1708  AggStatePerGroup pergroup =
1709  &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
1710 
1711  Assert(pertrans->transtypeByVal);
1712 
1713  ExecAggPlainTransByVal(aggstate, pertrans, pergroup,
1714  op->d.agg_trans.aggcontext,
1715  op->d.agg_trans.setno);
1716 
1717  EEO_NEXT();
1718  }
1719 
1720  /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
1722  {
1723  AggState *aggstate = castNode(AggState, state->parent);
1724  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
1725  AggStatePerGroup pergroup =
1726  &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
1727 
1728  Assert(!pertrans->transtypeByVal);
1729 
1730  if (pergroup->noTransValue)
1731  ExecAggInitGroup(aggstate, pertrans, pergroup,
1732  op->d.agg_trans.aggcontext);
1733  else if (likely(!pergroup->transValueIsNull))
1734  ExecAggPlainTransByRef(aggstate, pertrans, pergroup,
1735  op->d.agg_trans.aggcontext,
1736  op->d.agg_trans.setno);
1737 
1738  EEO_NEXT();
1739  }
1740 
1741  /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
1743  {
1744  AggState *aggstate = castNode(AggState, state->parent);
1745  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
1746  AggStatePerGroup pergroup =
1747  &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
1748 
1749  Assert(!pertrans->transtypeByVal);
1750 
1751  if (likely(!pergroup->transValueIsNull))
1752  ExecAggPlainTransByRef(aggstate, pertrans, pergroup,
1753  op->d.agg_trans.aggcontext,
1754  op->d.agg_trans.setno);
1755  EEO_NEXT();
1756  }
1757 
1758  /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
1760  {
1761  AggState *aggstate = castNode(AggState, state->parent);
1762  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
1763  AggStatePerGroup pergroup =
1764  &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
1765 
1766  Assert(!pertrans->transtypeByVal);
1767 
1768  ExecAggPlainTransByRef(aggstate, pertrans, pergroup,
1769  op->d.agg_trans.aggcontext,
1770  op->d.agg_trans.setno);
1771 
1772  EEO_NEXT();
1773  }
1774 
1775  /* process single-column ordered aggregate datum */
1777  {
1778  /* too complex for an inline implementation */
1779  ExecEvalAggOrderedTransDatum(state, op, econtext);
1780 
1781  EEO_NEXT();
1782  }
1783 
1784  /* process multi-column ordered aggregate tuple */
1786  {
1787  /* too complex for an inline implementation */
1788  ExecEvalAggOrderedTransTuple(state, op, econtext);
1789 
1790  EEO_NEXT();
1791  }
1792 
1794  {
1795  /* unreachable */
1796  Assert(false);
1797  goto out;
1798  }
1799  }
1800 
1801 out:
1802  *isnull = state->resnull;
1803  return state->resvalue;
1804 }
1805 
1806 /*
1807  * Expression evaluation callback that performs extra checks before executing
1808  * the expression. Declared extern so other methods of execution can use it
1809  * too.
1810  */
1811 Datum
1813 {
1814  /*
1815  * First time through, check whether attribute matches Var. Might not be
1816  * ok anymore, due to schema changes.
1817  */
1818  CheckExprStillValid(state, econtext);
1819 
1820  /* skip the check during further executions */
1821  state->evalfunc = (ExprStateEvalFunc) state->evalfunc_private;
1822 
1823  /* and actually execute */
1824  return state->evalfunc(state, econtext, isNull);
1825 }
1826 
1827 /*
1828  * Check that an expression is still valid in the face of potential schema
1829  * changes since the plan has been created.
1830  */
1831 void
1833 {
1834  TupleTableSlot *innerslot;
1835  TupleTableSlot *outerslot;
1836  TupleTableSlot *scanslot;
1837 
1838  innerslot = econtext->ecxt_innertuple;
1839  outerslot = econtext->ecxt_outertuple;
1840  scanslot = econtext->ecxt_scantuple;
1841 
1842  for (int i = 0; i < state->steps_len; i++)
1843  {
1844  ExprEvalStep *op = &state->steps[i];
1845 
1846  switch (ExecEvalStepOp(state, op))
1847  {
1848  case EEOP_INNER_VAR:
1849  {
1850  int attnum = op->d.var.attnum;
1851 
1852  CheckVarSlotCompatibility(innerslot, attnum + 1, op->d.var.vartype);
1853  break;
1854  }
1855 
1856  case EEOP_OUTER_VAR:
1857  {
1858  int attnum = op->d.var.attnum;
1859 
1860  CheckVarSlotCompatibility(outerslot, attnum + 1, op->d.var.vartype);
1861  break;
1862  }
1863 
1864  case EEOP_SCAN_VAR:
1865  {
1866  int attnum = op->d.var.attnum;
1867 
1868  CheckVarSlotCompatibility(scanslot, attnum + 1, op->d.var.vartype);
1869  break;
1870  }
1871  default:
1872  break;
1873  }
1874  }
1875 }
1876 
1877 /*
1878  * Check whether a user attribute in a slot can be referenced by a Var
1879  * expression. This should succeed unless there have been schema changes
1880  * since the expression tree has been created.
1881  */
1882 static void
1884 {
1885  /*
1886  * What we have to check for here is the possibility of an attribute
1887  * having been dropped or changed in type since the plan tree was created.
1888  * Ideally the plan will get invalidated and not re-used, but just in
1889  * case, we keep these defenses. Fortunately it's sufficient to check
1890  * once on the first time through.
1891  *
1892  * Note: ideally we'd check typmod as well as typid, but that seems
1893  * impractical at the moment: in many cases the tupdesc will have been
1894  * generated by ExecTypeFromTL(), and that can't guarantee to generate an
1895  * accurate typmod in all cases, because some expression node types don't
1896  * carry typmod. Fortunately, for precisely that reason, there should be
1897  * no places with a critical dependency on the typmod of a value.
1898  *
1899  * System attributes don't require checking since their types never
1900  * change.
1901  */
1902  if (attnum > 0)
1903  {
1904  TupleDesc slot_tupdesc = slot->tts_tupleDescriptor;
1905  Form_pg_attribute attr;
1906 
1907  if (attnum > slot_tupdesc->natts) /* should never happen */
1908  elog(ERROR, "attribute number %d exceeds number of columns %d",
1909  attnum, slot_tupdesc->natts);
1910 
1911  attr = TupleDescAttr(slot_tupdesc, attnum - 1);
1912 
1913  if (attr->attisdropped)
1914  ereport(ERROR,
1915  (errcode(ERRCODE_UNDEFINED_COLUMN),
1916  errmsg("attribute %d of type %s has been dropped",
1917  attnum, format_type_be(slot_tupdesc->tdtypeid))));
1918 
1919  if (vartype != attr->atttypid)
1920  ereport(ERROR,
1921  (errcode(ERRCODE_DATATYPE_MISMATCH),
1922  errmsg("attribute %d of type %s has wrong type",
1923  attnum, format_type_be(slot_tupdesc->tdtypeid)),
1924  errdetail("Table has type %s, but query expects %s.",
1925  format_type_be(attr->atttypid),
1926  format_type_be(vartype))));
1927  }
1928 }
1929 
1930 /*
1931  * Verify that the slot is compatible with a EEOP_*_FETCHSOME operation.
1932  */
1933 static void
1935 {
1936 #ifdef USE_ASSERT_CHECKING
1937  /* there's nothing to check */
1938  if (!op->d.fetch.fixed)
1939  return;
1940 
1941  /*
1942  * Should probably fixed at some point, but for now it's easier to allow
1943  * buffer and heap tuples to be used interchangeably.
1944  */
1945  if (slot->tts_ops == &TTSOpsBufferHeapTuple &&
1946  op->d.fetch.kind == &TTSOpsHeapTuple)
1947  return;
1948  if (slot->tts_ops == &TTSOpsHeapTuple &&
1949  op->d.fetch.kind == &TTSOpsBufferHeapTuple)
1950  return;
1951 
1952  /*
1953  * At the moment we consider it OK if a virtual slot is used instead of a
1954  * specific type of slot, as a virtual slot never needs to be deformed.
1955  */
1956  if (slot->tts_ops == &TTSOpsVirtual)
1957  return;
1958 
1959  Assert(op->d.fetch.kind == slot->tts_ops);
1960 #endif
1961 }
1962 
1963 /*
1964  * get_cached_rowtype: utility function to lookup a rowtype tupdesc
1965  *
1966  * type_id, typmod: identity of the rowtype
1967  * rowcache: space for caching identity info
1968  * (rowcache->cacheptr must be initialized to NULL)
1969  * changed: if not NULL, *changed is set to true on any update
1970  *
1971  * The returned TupleDesc is not guaranteed pinned; caller must pin it
1972  * to use it across any operation that might incur cache invalidation.
1973  * (The TupleDesc is always refcounted, so just use IncrTupleDescRefCount.)
1974  *
1975  * NOTE: because composite types can change contents, we must be prepared
1976  * to re-do this during any node execution; cannot call just once during
1977  * expression initialization.
1978  */
1979 static TupleDesc
1980 get_cached_rowtype(Oid type_id, int32 typmod,
1982  bool *changed)
1983 {
1984  if (type_id != RECORDOID)
1985  {
1986  /*
1987  * It's a named composite type, so use the regular typcache. Do a
1988  * lookup first time through, or if the composite type changed. Note:
1989  * "tupdesc_id == 0" may look redundant, but it protects against the
1990  * admittedly-theoretical possibility that type_id was RECORDOID the
1991  * last time through, so that the cacheptr isn't TypeCacheEntry *.
1992  */
1993  TypeCacheEntry *typentry = (TypeCacheEntry *) rowcache->cacheptr;
1994 
1995  if (unlikely(typentry == NULL ||
1996  rowcache->tupdesc_id == 0 ||
1997  typentry->tupDesc_identifier != rowcache->tupdesc_id))
1998  {
1999  typentry = lookup_type_cache(type_id, TYPECACHE_TUPDESC);
2000  if (typentry->tupDesc == NULL)
2001  ereport(ERROR,
2002  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2003  errmsg("type %s is not composite",
2004  format_type_be(type_id))));
2005  rowcache->cacheptr = (void *) typentry;
2006  rowcache->tupdesc_id = typentry->tupDesc_identifier;
2007  if (changed)
2008  *changed = true;
2009  }
2010  return typentry->tupDesc;
2011  }
2012  else
2013  {
2014  /*
2015  * A RECORD type, once registered, doesn't change for the life of the
2016  * backend. So we don't need a typcache entry as such, which is good
2017  * because there isn't one. It's possible that the caller is asking
2018  * about a different type than before, though.
2019  */
2020  TupleDesc tupDesc = (TupleDesc) rowcache->cacheptr;
2021 
2022  if (unlikely(tupDesc == NULL ||
2023  rowcache->tupdesc_id != 0 ||
2024  type_id != tupDesc->tdtypeid ||
2025  typmod != tupDesc->tdtypmod))
2026  {
2027  tupDesc = lookup_rowtype_tupdesc(type_id, typmod);
2028  /* Drop pin acquired by lookup_rowtype_tupdesc */
2029  ReleaseTupleDesc(tupDesc);
2030  rowcache->cacheptr = (void *) tupDesc;
2031  rowcache->tupdesc_id = 0; /* not a valid value for non-RECORD */
2032  if (changed)
2033  *changed = true;
2034  }
2035  return tupDesc;
2036  }
2037 }
2038 
2039 
2040 /*
2041  * Fast-path functions, for very simple expressions
2042  */
2043 
2044 /* implementation of ExecJust(Inner|Outer|Scan)Var */
2047 {
2048  ExprEvalStep *op = &state->steps[1];
2049  int attnum = op->d.var.attnum + 1;
2050 
2051  CheckOpSlotCompatibility(&state->steps[0], slot);
2052 
2053  /*
2054  * Since we use slot_getattr(), we don't need to implement the FETCHSOME
2055  * step explicitly, and we also needn't Assert that the attnum is in range
2056  * --- slot_getattr() will take care of any problems.
2057  */
2058  return slot_getattr(slot, attnum, isnull);
2059 }
2060 
2061 /* Simple reference to inner Var */
2062 static Datum
2064 {
2065  return ExecJustVarImpl(state, econtext->ecxt_innertuple, isnull);
2066 }
2067 
2068 /* Simple reference to outer Var */
2069 static Datum
2071 {
2072  return ExecJustVarImpl(state, econtext->ecxt_outertuple, isnull);
2073 }
2074 
2075 /* Simple reference to scan Var */
2076 static Datum
2078 {
2079  return ExecJustVarImpl(state, econtext->ecxt_scantuple, isnull);
2080 }
2081 
2082 /* implementation of ExecJustAssign(Inner|Outer|Scan)Var */
2085 {
2086  ExprEvalStep *op = &state->steps[1];
2087  int attnum = op->d.assign_var.attnum + 1;
2088  int resultnum = op->d.assign_var.resultnum;
2089  TupleTableSlot *outslot = state->resultslot;
2090 
2091  CheckOpSlotCompatibility(&state->steps[0], inslot);
2092 
2093  /*
2094  * We do not need CheckVarSlotCompatibility here; that was taken care of
2095  * at compilation time.
2096  *
2097  * Since we use slot_getattr(), we don't need to implement the FETCHSOME
2098  * step explicitly, and we also needn't Assert that the attnum is in range
2099  * --- slot_getattr() will take care of any problems. Nonetheless, check
2100  * that resultnum is in range.
2101  */
2102  Assert(resultnum >= 0 && resultnum < outslot->tts_tupleDescriptor->natts);
2103  outslot->tts_values[resultnum] =
2104  slot_getattr(inslot, attnum, &outslot->tts_isnull[resultnum]);
2105  return 0;
2106 }
2107 
2108 /* Evaluate inner Var and assign to appropriate column of result tuple */
2109 static Datum
2111 {
2112  return ExecJustAssignVarImpl(state, econtext->ecxt_innertuple, isnull);
2113 }
2114 
2115 /* Evaluate outer Var and assign to appropriate column of result tuple */
2116 static Datum
2118 {
2119  return ExecJustAssignVarImpl(state, econtext->ecxt_outertuple, isnull);
2120 }
2121 
2122 /* Evaluate scan Var and assign to appropriate column of result tuple */
2123 static Datum
2125 {
2126  return ExecJustAssignVarImpl(state, econtext->ecxt_scantuple, isnull);
2127 }
2128 
2129 /* Evaluate CASE_TESTVAL and apply a strict function to it */
2130 static Datum
2132 {
2133  ExprEvalStep *op = &state->steps[0];
2134  FunctionCallInfo fcinfo;
2136  int nargs;
2137  Datum d;
2138 
2139  /*
2140  * XXX with some redesign of the CaseTestExpr mechanism, maybe we could
2141  * get rid of this data shuffling?
2142  */
2143  *op->resvalue = *op->d.casetest.value;
2144  *op->resnull = *op->d.casetest.isnull;
2145 
2146  op++;
2147 
2148  nargs = op->d.func.nargs;
2149  fcinfo = op->d.func.fcinfo_data;
2150  args = fcinfo->args;
2151 
2152  /* strict function, so check for NULL args */
2153  for (int argno = 0; argno < nargs; argno++)
2154  {
2155  if (args[argno].isnull)
2156  {
2157  *isnull = true;
2158  return (Datum) 0;
2159  }
2160  }
2161  fcinfo->isnull = false;
2162  d = op->d.func.fn_addr(fcinfo);
2163  *isnull = fcinfo->isnull;
2164  return d;
2165 }
2166 
2167 /* Simple Const expression */
2168 static Datum
2170 {
2171  ExprEvalStep *op = &state->steps[0];
2172 
2173  *isnull = op->d.constval.isnull;
2174  return op->d.constval.value;
2175 }
2176 
2177 /* implementation of ExecJust(Inner|Outer|Scan)VarVirt */
2180 {
2181  ExprEvalStep *op = &state->steps[0];
2182  int attnum = op->d.var.attnum;
2183 
2184  /*
2185  * As it is guaranteed that a virtual slot is used, there never is a need
2186  * to perform tuple deforming (nor would it be possible). Therefore
2187  * execExpr.c has not emitted an EEOP_*_FETCHSOME step. Verify, as much as
2188  * possible, that that determination was accurate.
2189  */
2190  Assert(TTS_IS_VIRTUAL(slot));
2191  Assert(TTS_FIXED(slot));
2192  Assert(attnum >= 0 && attnum < slot->tts_nvalid);
2193 
2194  *isnull = slot->tts_isnull[attnum];
2195 
2196  return slot->tts_values[attnum];
2197 }
2198 
2199 /* Like ExecJustInnerVar, optimized for virtual slots */
2200 static Datum
2202 {
2203  return ExecJustVarVirtImpl(state, econtext->ecxt_innertuple, isnull);
2204 }
2205 
2206 /* Like ExecJustOuterVar, optimized for virtual slots */
2207 static Datum
2209 {
2210  return ExecJustVarVirtImpl(state, econtext->ecxt_outertuple, isnull);
2211 }
2212 
2213 /* Like ExecJustScanVar, optimized for virtual slots */
2214 static Datum
2216 {
2217  return ExecJustVarVirtImpl(state, econtext->ecxt_scantuple, isnull);
2218 }
2219 
2220 /* implementation of ExecJustAssign(Inner|Outer|Scan)VarVirt */
2223 {
2224  ExprEvalStep *op = &state->steps[0];
2225  int attnum = op->d.assign_var.attnum;
2226  int resultnum = op->d.assign_var.resultnum;
2227  TupleTableSlot *outslot = state->resultslot;
2228 
2229  /* see ExecJustVarVirtImpl for comments */
2230 
2231  Assert(TTS_IS_VIRTUAL(inslot));
2232  Assert(TTS_FIXED(inslot));
2233  Assert(attnum >= 0 && attnum < inslot->tts_nvalid);
2234  Assert(resultnum >= 0 && resultnum < outslot->tts_tupleDescriptor->natts);
2235 
2236  outslot->tts_values[resultnum] = inslot->tts_values[attnum];
2237  outslot->tts_isnull[resultnum] = inslot->tts_isnull[attnum];
2238 
2239  return 0;
2240 }
2241 
2242 /* Like ExecJustAssignInnerVar, optimized for virtual slots */
2243 static Datum
2245 {
2246  return ExecJustAssignVarVirtImpl(state, econtext->ecxt_innertuple, isnull);
2247 }
2248 
2249 /* Like ExecJustAssignOuterVar, optimized for virtual slots */
2250 static Datum
2252 {
2253  return ExecJustAssignVarVirtImpl(state, econtext->ecxt_outertuple, isnull);
2254 }
2255 
2256 /* Like ExecJustAssignScanVar, optimized for virtual slots */
2257 static Datum
2259 {
2260  return ExecJustAssignVarVirtImpl(state, econtext->ecxt_scantuple, isnull);
2261 }
2262 
2263 #if defined(EEO_USE_COMPUTED_GOTO)
2264 /*
2265  * Comparator used when building address->opcode lookup table for
2266  * ExecEvalStepOp() in the threaded dispatch case.
2267  */
2268 static int
2269 dispatch_compare_ptr(const void *a, const void *b)
2270 {
2271  const ExprEvalOpLookup *la = (const ExprEvalOpLookup *) a;
2272  const ExprEvalOpLookup *lb = (const ExprEvalOpLookup *) b;
2273 
2274  if (la->opcode < lb->opcode)
2275  return -1;
2276  else if (la->opcode > lb->opcode)
2277  return 1;
2278  return 0;
2279 }
2280 #endif
2281 
2282 /*
2283  * Do one-time initialization of interpretation machinery.
2284  */
2285 static void
2287 {
2288 #if defined(EEO_USE_COMPUTED_GOTO)
2289  /* Set up externally-visible pointer to dispatch table */
2290  if (dispatch_table == NULL)
2291  {
2292  dispatch_table = (const void **)
2293  DatumGetPointer(ExecInterpExpr(NULL, NULL, NULL));
2294 
2295  /* build reverse lookup table */
2296  for (int i = 0; i < EEOP_LAST; i++)
2297  {
2298  reverse_dispatch_table[i].opcode = dispatch_table[i];
2299  reverse_dispatch_table[i].op = (ExprEvalOp) i;
2300  }
2301 
2302  /* make it bsearch()able */
2303  qsort(reverse_dispatch_table,
2304  EEOP_LAST /* nmembers */ ,
2305  sizeof(ExprEvalOpLookup),
2306  dispatch_compare_ptr);
2307  }
2308 #endif
2309 }
2310 
2311 /*
2312  * Function to return the opcode of an expression step.
2313  *
2314  * When direct-threading is in use, ExprState->opcode isn't easily
2315  * decipherable. This function returns the appropriate enum member.
2316  */
2317 ExprEvalOp
2319 {
2320 #if defined(EEO_USE_COMPUTED_GOTO)
2321  if (state->flags & EEO_FLAG_DIRECT_THREADED)
2322  {
2323  ExprEvalOpLookup key;
2324  ExprEvalOpLookup *res;
2325 
2326  key.opcode = (void *) op->opcode;
2327  res = bsearch(&key,
2328  reverse_dispatch_table,
2329  EEOP_LAST /* nmembers */ ,
2330  sizeof(ExprEvalOpLookup),
2331  dispatch_compare_ptr);
2332  Assert(res); /* unknown ops shouldn't get looked up */
2333  return res->op;
2334  }
2335 #endif
2336  return (ExprEvalOp) op->opcode;
2337 }
2338 
2339 
2340 /*
2341  * Out-of-line helper functions for complex instructions.
2342  */
2343 
2344 /*
2345  * Evaluate EEOP_FUNCEXPR_FUSAGE
2346  */
2347 void
2349  ExprContext *econtext)
2350 {
2351  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
2352  PgStat_FunctionCallUsage fcusage;
2353  Datum d;
2354 
2355  pgstat_init_function_usage(fcinfo, &fcusage);
2356 
2357  fcinfo->isnull = false;
2358  d = op->d.func.fn_addr(fcinfo);
2359  *op->resvalue = d;
2360  *op->resnull = fcinfo->isnull;
2361 
2362  pgstat_end_function_usage(&fcusage, true);
2363 }
2364 
2365 /*
2366  * Evaluate EEOP_FUNCEXPR_STRICT_FUSAGE
2367  */
2368 void
2370  ExprContext *econtext)
2371 {
2372 
2373  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
2374  PgStat_FunctionCallUsage fcusage;
2375  NullableDatum *args = fcinfo->args;
2376  int nargs = op->d.func.nargs;
2377  Datum d;
2378 
2379  /* strict function, so check for NULL args */
2380  for (int argno = 0; argno < nargs; argno++)
2381  {
2382  if (args[argno].isnull)
2383  {
2384  *op->resnull = true;
2385  return;
2386  }
2387  }
2388 
2389  pgstat_init_function_usage(fcinfo, &fcusage);
2390 
2391  fcinfo->isnull = false;
2392  d = op->d.func.fn_addr(fcinfo);
2393  *op->resvalue = d;
2394  *op->resnull = fcinfo->isnull;
2395 
2396  pgstat_end_function_usage(&fcusage, true);
2397 }
2398 
2399 /*
2400  * Evaluate a PARAM_EXEC parameter.
2401  *
2402  * PARAM_EXEC params (internal executor parameters) are stored in the
2403  * ecxt_param_exec_vals array, and can be accessed by array index.
2404  */
2405 void
2407 {
2408  ParamExecData *prm;
2409 
2410  prm = &(econtext->ecxt_param_exec_vals[op->d.param.paramid]);
2411  if (unlikely(prm->execPlan != NULL))
2412  {
2413  /* Parameter not evaluated yet, so go do it */
2414  ExecSetParamPlan(prm->execPlan, econtext);
2415  /* ExecSetParamPlan should have processed this param... */
2416  Assert(prm->execPlan == NULL);
2417  }
2418  *op->resvalue = prm->value;
2419  *op->resnull = prm->isnull;
2420 }
2421 
2422 /*
2423  * Evaluate a PARAM_EXTERN parameter.
2424  *
2425  * PARAM_EXTERN parameters must be sought in ecxt_param_list_info.
2426  */
2427 void
2429 {
2430  ParamListInfo paramInfo = econtext->ecxt_param_list_info;
2431  int paramId = op->d.param.paramid;
2432 
2433  if (likely(paramInfo &&
2434  paramId > 0 && paramId <= paramInfo->numParams))
2435  {
2436  ParamExternData *prm;
2437  ParamExternData prmdata;
2438 
2439  /* give hook a chance in case parameter is dynamic */
2440  if (paramInfo->paramFetch != NULL)
2441  prm = paramInfo->paramFetch(paramInfo, paramId, false, &prmdata);
2442  else
2443  prm = &paramInfo->params[paramId - 1];
2444 
2445  if (likely(OidIsValid(prm->ptype)))
2446  {
2447  /* safety check in case hook did something unexpected */
2448  if (unlikely(prm->ptype != op->d.param.paramtype))
2449  ereport(ERROR,
2450  (errcode(ERRCODE_DATATYPE_MISMATCH),
2451  errmsg("type of parameter %d (%s) does not match that when preparing the plan (%s)",
2452  paramId,
2453  format_type_be(prm->ptype),
2454  format_type_be(op->d.param.paramtype))));
2455  *op->resvalue = prm->value;
2456  *op->resnull = prm->isnull;
2457  return;
2458  }
2459  }
2460 
2461  ereport(ERROR,
2462  (errcode(ERRCODE_UNDEFINED_OBJECT),
2463  errmsg("no value found for parameter %d", paramId)));
2464 }
2465 
2466 /*
2467  * Evaluate a SQLValueFunction expression.
2468  */
2469 void
2471 {
2472  LOCAL_FCINFO(fcinfo, 0);
2474 
2475  *op->resnull = false;
2476 
2477  /*
2478  * Note: current_schema() can return NULL. current_user() etc currently
2479  * cannot, but might as well code those cases the same way for safety.
2480  */
2481  switch (svf->op)
2482  {
2483  case SVFOP_CURRENT_DATE:
2485  break;
2486  case SVFOP_CURRENT_TIME:
2487  case SVFOP_CURRENT_TIME_N:
2489  break;
2493  break;
2494  case SVFOP_LOCALTIME:
2495  case SVFOP_LOCALTIME_N:
2497  break;
2498  case SVFOP_LOCALTIMESTAMP:
2501  break;
2502  case SVFOP_CURRENT_ROLE:
2503  case SVFOP_CURRENT_USER:
2504  case SVFOP_USER:
2505  InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2506  *op->resvalue = current_user(fcinfo);
2507  *op->resnull = fcinfo->isnull;
2508  break;
2509  case SVFOP_SESSION_USER:
2510  InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2511  *op->resvalue = session_user(fcinfo);
2512  *op->resnull = fcinfo->isnull;
2513  break;
2514  case SVFOP_CURRENT_CATALOG:
2515  InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2516  *op->resvalue = current_database(fcinfo);
2517  *op->resnull = fcinfo->isnull;
2518  break;
2519  case SVFOP_CURRENT_SCHEMA:
2520  InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2521  *op->resvalue = current_schema(fcinfo);
2522  *op->resnull = fcinfo->isnull;
2523  break;
2524  }
2525 }
2526 
2527 /*
2528  * Raise error if a CURRENT OF expression is evaluated.
2529  *
2530  * The planner should convert CURRENT OF into a TidScan qualification, or some
2531  * other special handling in a ForeignScan node. So we have to be able to do
2532  * ExecInitExpr on a CurrentOfExpr, but we shouldn't ever actually execute it.
2533  * If we get here, we suppose we must be dealing with CURRENT OF on a foreign
2534  * table whose FDW doesn't handle it, and complain accordingly.
2535  */
2536 void
2538 {
2539  ereport(ERROR,
2540  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2541  errmsg("WHERE CURRENT OF is not supported for this table type")));
2542 }
2543 
2544 /*
2545  * Evaluate NextValueExpr.
2546  */
2547 void
2549 {
2550  int64 newval = nextval_internal(op->d.nextvalueexpr.seqid, false);
2551 
2552  switch (op->d.nextvalueexpr.seqtypid)
2553  {
2554  case INT2OID:
2555  *op->resvalue = Int16GetDatum((int16) newval);
2556  break;
2557  case INT4OID:
2558  *op->resvalue = Int32GetDatum((int32) newval);
2559  break;
2560  case INT8OID:
2561  *op->resvalue = Int64GetDatum((int64) newval);
2562  break;
2563  default:
2564  elog(ERROR, "unsupported sequence type %u",
2565  op->d.nextvalueexpr.seqtypid);
2566  }
2567  *op->resnull = false;
2568 }
2569 
2570 /*
2571  * Evaluate NullTest / IS NULL for rows.
2572  */
2573 void
2575 {
2576  ExecEvalRowNullInt(state, op, econtext, true);
2577 }
2578 
2579 /*
2580  * Evaluate NullTest / IS NOT NULL for rows.
2581  */
2582 void
2584 {
2585  ExecEvalRowNullInt(state, op, econtext, false);
2586 }
2587 
2588 /* Common code for IS [NOT] NULL on a row value */
2589 static void
2591  ExprContext *econtext, bool checkisnull)
2592 {
2593  Datum value = *op->resvalue;
2594  bool isnull = *op->resnull;
2595  HeapTupleHeader tuple;
2596  Oid tupType;
2597  int32 tupTypmod;
2598  TupleDesc tupDesc;
2599  HeapTupleData tmptup;
2600 
2601  *op->resnull = false;
2602 
2603  /* NULL row variables are treated just as NULL scalar columns */
2604  if (isnull)
2605  {
2606  *op->resvalue = BoolGetDatum(checkisnull);
2607  return;
2608  }
2609 
2610  /*
2611  * The SQL standard defines IS [NOT] NULL for a non-null rowtype argument
2612  * as:
2613  *
2614  * "R IS NULL" is true if every field is the null value.
2615  *
2616  * "R IS NOT NULL" is true if no field is the null value.
2617  *
2618  * This definition is (apparently intentionally) not recursive; so our
2619  * tests on the fields are primitive attisnull tests, not recursive checks
2620  * to see if they are all-nulls or no-nulls rowtypes.
2621  *
2622  * The standard does not consider the possibility of zero-field rows, but
2623  * here we consider them to vacuously satisfy both predicates.
2624  */
2625 
2626  tuple = DatumGetHeapTupleHeader(value);
2627 
2628  tupType = HeapTupleHeaderGetTypeId(tuple);
2629  tupTypmod = HeapTupleHeaderGetTypMod(tuple);
2630 
2631  /* Lookup tupdesc if first time through or if type changes */
2632  tupDesc = get_cached_rowtype(tupType, tupTypmod,
2633  &op->d.nulltest_row.rowcache, NULL);
2634 
2635  /*
2636  * heap_attisnull needs a HeapTuple not a bare HeapTupleHeader.
2637  */
2638  tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
2639  tmptup.t_data = tuple;
2640 
2641  for (int att = 1; att <= tupDesc->natts; att++)
2642  {
2643  /* ignore dropped columns */
2644  if (TupleDescAttr(tupDesc, att - 1)->attisdropped)
2645  continue;
2646  if (heap_attisnull(&tmptup, att, tupDesc))
2647  {
2648  /* null field disproves IS NOT NULL */
2649  if (!checkisnull)
2650  {
2651  *op->resvalue = BoolGetDatum(false);
2652  return;
2653  }
2654  }
2655  else
2656  {
2657  /* non-null field disproves IS NULL */
2658  if (checkisnull)
2659  {
2660  *op->resvalue = BoolGetDatum(false);
2661  return;
2662  }
2663  }
2664  }
2665 
2666  *op->resvalue = BoolGetDatum(true);
2667 }
2668 
2669 /*
2670  * Evaluate an ARRAY[] expression.
2671  *
2672  * The individual array elements (or subarrays) have already been evaluated
2673  * into op->d.arrayexpr.elemvalues[]/elemnulls[].
2674  */
2675 void
2677 {
2678  ArrayType *result;
2679  Oid element_type = op->d.arrayexpr.elemtype;
2680  int nelems = op->d.arrayexpr.nelems;
2681  int ndims = 0;
2682  int dims[MAXDIM];
2683  int lbs[MAXDIM];
2684 
2685  /* Set non-null as default */
2686  *op->resnull = false;
2687 
2688  if (!op->d.arrayexpr.multidims)
2689  {
2690  /* Elements are presumably of scalar type */
2691  Datum *dvalues = op->d.arrayexpr.elemvalues;
2692  bool *dnulls = op->d.arrayexpr.elemnulls;
2693 
2694  /* setup for 1-D array of the given length */
2695  ndims = 1;
2696  dims[0] = nelems;
2697  lbs[0] = 1;
2698 
2699  result = construct_md_array(dvalues, dnulls, ndims, dims, lbs,
2700  element_type,
2701  op->d.arrayexpr.elemlength,
2702  op->d.arrayexpr.elembyval,
2703  op->d.arrayexpr.elemalign);
2704  }
2705  else
2706  {
2707  /* Must be nested array expressions */
2708  int nbytes = 0;
2709  int nitems = 0;
2710  int outer_nelems = 0;
2711  int elem_ndims = 0;
2712  int *elem_dims = NULL;
2713  int *elem_lbs = NULL;
2714  bool firstone = true;
2715  bool havenulls = false;
2716  bool haveempty = false;
2717  char **subdata;
2718  bits8 **subbitmaps;
2719  int *subbytes;
2720  int *subnitems;
2721  int32 dataoffset;
2722  char *dat;
2723  int iitem;
2724 
2725  subdata = (char **) palloc(nelems * sizeof(char *));
2726  subbitmaps = (bits8 **) palloc(nelems * sizeof(bits8 *));
2727  subbytes = (int *) palloc(nelems * sizeof(int));
2728  subnitems = (int *) palloc(nelems * sizeof(int));
2729 
2730  /* loop through and get data area from each element */
2731  for (int elemoff = 0; elemoff < nelems; elemoff++)
2732  {
2733  Datum arraydatum;
2734  bool eisnull;
2735  ArrayType *array;
2736  int this_ndims;
2737 
2738  arraydatum = op->d.arrayexpr.elemvalues[elemoff];
2739  eisnull = op->d.arrayexpr.elemnulls[elemoff];
2740 
2741  /* temporarily ignore null subarrays */
2742  if (eisnull)
2743  {
2744  haveempty = true;
2745  continue;
2746  }
2747 
2748  array = DatumGetArrayTypeP(arraydatum);
2749 
2750  /* run-time double-check on element type */
2751  if (element_type != ARR_ELEMTYPE(array))
2752  ereport(ERROR,
2753  (errcode(ERRCODE_DATATYPE_MISMATCH),
2754  errmsg("cannot merge incompatible arrays"),
2755  errdetail("Array with element type %s cannot be "
2756  "included in ARRAY construct with element type %s.",
2757  format_type_be(ARR_ELEMTYPE(array)),
2758  format_type_be(element_type))));
2759 
2760  this_ndims = ARR_NDIM(array);
2761  /* temporarily ignore zero-dimensional subarrays */
2762  if (this_ndims <= 0)
2763  {
2764  haveempty = true;
2765  continue;
2766  }
2767 
2768  if (firstone)
2769  {
2770  /* Get sub-array details from first member */
2771  elem_ndims = this_ndims;
2772  ndims = elem_ndims + 1;
2773  if (ndims <= 0 || ndims > MAXDIM)
2774  ereport(ERROR,
2775  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2776  errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
2777  ndims, MAXDIM)));
2778 
2779  elem_dims = (int *) palloc(elem_ndims * sizeof(int));
2780  memcpy(elem_dims, ARR_DIMS(array), elem_ndims * sizeof(int));
2781  elem_lbs = (int *) palloc(elem_ndims * sizeof(int));
2782  memcpy(elem_lbs, ARR_LBOUND(array), elem_ndims * sizeof(int));
2783 
2784  firstone = false;
2785  }
2786  else
2787  {
2788  /* Check other sub-arrays are compatible */
2789  if (elem_ndims != this_ndims ||
2790  memcmp(elem_dims, ARR_DIMS(array),
2791  elem_ndims * sizeof(int)) != 0 ||
2792  memcmp(elem_lbs, ARR_LBOUND(array),
2793  elem_ndims * sizeof(int)) != 0)
2794  ereport(ERROR,
2795  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2796  errmsg("multidimensional arrays must have array "
2797  "expressions with matching dimensions")));
2798  }
2799 
2800  subdata[outer_nelems] = ARR_DATA_PTR(array);
2801  subbitmaps[outer_nelems] = ARR_NULLBITMAP(array);
2802  subbytes[outer_nelems] = ARR_SIZE(array) - ARR_DATA_OFFSET(array);
2803  nbytes += subbytes[outer_nelems];
2804  subnitems[outer_nelems] = ArrayGetNItems(this_ndims,
2805  ARR_DIMS(array));
2806  nitems += subnitems[outer_nelems];
2807  havenulls |= ARR_HASNULL(array);
2808  outer_nelems++;
2809  }
2810 
2811  /*
2812  * If all items were null or empty arrays, return an empty array;
2813  * otherwise, if some were and some weren't, raise error. (Note: we
2814  * must special-case this somehow to avoid trying to generate a 1-D
2815  * array formed from empty arrays. It's not ideal...)
2816  */
2817  if (haveempty)
2818  {
2819  if (ndims == 0) /* didn't find any nonempty array */
2820  {
2821  *op->resvalue = PointerGetDatum(construct_empty_array(element_type));
2822  return;
2823  }
2824  ereport(ERROR,
2825  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2826  errmsg("multidimensional arrays must have array "
2827  "expressions with matching dimensions")));
2828  }
2829 
2830  /* setup for multi-D array */
2831  dims[0] = outer_nelems;
2832  lbs[0] = 1;
2833  for (int i = 1; i < ndims; i++)
2834  {
2835  dims[i] = elem_dims[i - 1];
2836  lbs[i] = elem_lbs[i - 1];
2837  }
2838 
2839  /* check for subscript overflow */
2840  (void) ArrayGetNItems(ndims, dims);
2841  ArrayCheckBounds(ndims, dims, lbs);
2842 
2843  if (havenulls)
2844  {
2845  dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nitems);
2846  nbytes += dataoffset;
2847  }
2848  else
2849  {
2850  dataoffset = 0; /* marker for no null bitmap */
2851  nbytes += ARR_OVERHEAD_NONULLS(ndims);
2852  }
2853 
2854  result = (ArrayType *) palloc(nbytes);
2855  SET_VARSIZE(result, nbytes);
2856  result->ndim = ndims;
2857  result->dataoffset = dataoffset;
2858  result->elemtype = element_type;
2859  memcpy(ARR_DIMS(result), dims, ndims * sizeof(int));
2860  memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int));
2861 
2862  dat = ARR_DATA_PTR(result);
2863  iitem = 0;
2864  for (int i = 0; i < outer_nelems; i++)
2865  {
2866  memcpy(dat, subdata[i], subbytes[i]);
2867  dat += subbytes[i];
2868  if (havenulls)
2869  array_bitmap_copy(ARR_NULLBITMAP(result), iitem,
2870  subbitmaps[i], 0,
2871  subnitems[i]);
2872  iitem += subnitems[i];
2873  }
2874  }
2875 
2876  *op->resvalue = PointerGetDatum(result);
2877 }
2878 
2879 /*
2880  * Evaluate an ArrayCoerceExpr expression.
2881  *
2882  * Source array is in step's result variable.
2883  */
2884 void
2886 {
2887  Datum arraydatum;
2888 
2889  /* NULL array -> NULL result */
2890  if (*op->resnull)
2891  return;
2892 
2893  arraydatum = *op->resvalue;
2894 
2895  /*
2896  * If it's binary-compatible, modify the element type in the array header,
2897  * but otherwise leave the array as we received it.
2898  */
2899  if (op->d.arraycoerce.elemexprstate == NULL)
2900  {
2901  /* Detoast input array if necessary, and copy in any case */
2902  ArrayType *array = DatumGetArrayTypePCopy(arraydatum);
2903 
2904  ARR_ELEMTYPE(array) = op->d.arraycoerce.resultelemtype;
2905  *op->resvalue = PointerGetDatum(array);
2906  return;
2907  }
2908 
2909  /*
2910  * Use array_map to apply the sub-expression to each array element.
2911  */
2912  *op->resvalue = array_map(arraydatum,
2913  op->d.arraycoerce.elemexprstate,
2914  econtext,
2915  op->d.arraycoerce.resultelemtype,
2916  op->d.arraycoerce.amstate);
2917 }
2918 
2919 /*
2920  * Evaluate a ROW() expression.
2921  *
2922  * The individual columns have already been evaluated into
2923  * op->d.row.elemvalues[]/elemnulls[].
2924  */
2925 void
2927 {
2928  HeapTuple tuple;
2929 
2930  /* build tuple from evaluated field values */
2931  tuple = heap_form_tuple(op->d.row.tupdesc,
2932  op->d.row.elemvalues,
2933  op->d.row.elemnulls);
2934 
2935  *op->resvalue = HeapTupleGetDatum(tuple);
2936  *op->resnull = false;
2937 }
2938 
2939 /*
2940  * Evaluate GREATEST() or LEAST() expression (note this is *not* MIN()/MAX()).
2941  *
2942  * All of the to-be-compared expressions have already been evaluated into
2943  * op->d.minmax.values[]/nulls[].
2944  */
2945 void
2947 {
2948  Datum *values = op->d.minmax.values;
2949  bool *nulls = op->d.minmax.nulls;
2950  FunctionCallInfo fcinfo = op->d.minmax.fcinfo_data;
2951  MinMaxOp operator = op->d.minmax.op;
2952 
2953  /* set at initialization */
2954  Assert(fcinfo->args[0].isnull == false);
2955  Assert(fcinfo->args[1].isnull == false);
2956 
2957  /* default to null result */
2958  *op->resnull = true;
2959 
2960  for (int off = 0; off < op->d.minmax.nelems; off++)
2961  {
2962  /* ignore NULL inputs */
2963  if (nulls[off])
2964  continue;
2965 
2966  if (*op->resnull)
2967  {
2968  /* first nonnull input, adopt value */
2969  *op->resvalue = values[off];
2970  *op->resnull = false;
2971  }
2972  else
2973  {
2974  int cmpresult;
2975 
2976  /* apply comparison function */
2977  fcinfo->args[0].value = *op->resvalue;
2978  fcinfo->args[1].value = values[off];
2979 
2980  fcinfo->isnull = false;
2981  cmpresult = DatumGetInt32(FunctionCallInvoke(fcinfo));
2982  if (fcinfo->isnull) /* probably should not happen */
2983  continue;
2984 
2985  if (cmpresult > 0 && operator == IS_LEAST)
2986  *op->resvalue = values[off];
2987  else if (cmpresult < 0 && operator == IS_GREATEST)
2988  *op->resvalue = values[off];
2989  }
2990  }
2991 }
2992 
2993 /*
2994  * Evaluate a FieldSelect node.
2995  *
2996  * Source record is in step's result variable.
2997  */
2998 void
3000 {
3001  AttrNumber fieldnum = op->d.fieldselect.fieldnum;
3002  Datum tupDatum;
3003  HeapTupleHeader tuple;
3004  Oid tupType;
3005  int32 tupTypmod;
3006  TupleDesc tupDesc;
3007  Form_pg_attribute attr;
3008  HeapTupleData tmptup;
3009 
3010  /* NULL record -> NULL result */
3011  if (*op->resnull)
3012  return;
3013 
3014  tupDatum = *op->resvalue;
3015 
3016  /* We can special-case expanded records for speed */
3018  {
3020 
3021  Assert(erh->er_magic == ER_MAGIC);
3022 
3023  /* Extract record's TupleDesc */
3024  tupDesc = expanded_record_get_tupdesc(erh);
3025 
3026  /*
3027  * Find field's attr record. Note we don't support system columns
3028  * here: a datum tuple doesn't have valid values for most of the
3029  * interesting system columns anyway.
3030  */
3031  if (fieldnum <= 0) /* should never happen */
3032  elog(ERROR, "unsupported reference to system column %d in FieldSelect",
3033  fieldnum);
3034  if (fieldnum > tupDesc->natts) /* should never happen */
3035  elog(ERROR, "attribute number %d exceeds number of columns %d",
3036  fieldnum, tupDesc->natts);
3037  attr = TupleDescAttr(tupDesc, fieldnum - 1);
3038 
3039  /* Check for dropped column, and force a NULL result if so */
3040  if (attr->attisdropped)
3041  {
3042  *op->resnull = true;
3043  return;
3044  }
3045 
3046  /* Check for type mismatch --- possible after ALTER COLUMN TYPE? */
3047  /* As in CheckVarSlotCompatibility, we should but can't check typmod */
3048  if (op->d.fieldselect.resulttype != attr->atttypid)
3049  ereport(ERROR,
3050  (errcode(ERRCODE_DATATYPE_MISMATCH),
3051  errmsg("attribute %d has wrong type", fieldnum),
3052  errdetail("Table has type %s, but query expects %s.",
3053  format_type_be(attr->atttypid),
3054  format_type_be(op->d.fieldselect.resulttype))));
3055 
3056  /* extract the field */
3057  *op->resvalue = expanded_record_get_field(erh, fieldnum,
3058  op->resnull);
3059  }
3060  else
3061  {
3062  /* Get the composite datum and extract its type fields */
3063  tuple = DatumGetHeapTupleHeader(tupDatum);
3064 
3065  tupType = HeapTupleHeaderGetTypeId(tuple);
3066  tupTypmod = HeapTupleHeaderGetTypMod(tuple);
3067 
3068  /* Lookup tupdesc if first time through or if type changes */
3069  tupDesc = get_cached_rowtype(tupType, tupTypmod,
3070  &op->d.fieldselect.rowcache, NULL);
3071 
3072  /*
3073  * Find field's attr record. Note we don't support system columns
3074  * here: a datum tuple doesn't have valid values for most of the
3075  * interesting system columns anyway.
3076  */
3077  if (fieldnum <= 0) /* should never happen */
3078  elog(ERROR, "unsupported reference to system column %d in FieldSelect",
3079  fieldnum);
3080  if (fieldnum > tupDesc->natts) /* should never happen */
3081  elog(ERROR, "attribute number %d exceeds number of columns %d",
3082  fieldnum, tupDesc->natts);
3083  attr = TupleDescAttr(tupDesc, fieldnum - 1);
3084 
3085  /* Check for dropped column, and force a NULL result if so */
3086  if (attr->attisdropped)
3087  {
3088  *op->resnull = true;
3089  return;
3090  }
3091 
3092  /* Check for type mismatch --- possible after ALTER COLUMN TYPE? */
3093  /* As in CheckVarSlotCompatibility, we should but can't check typmod */
3094  if (op->d.fieldselect.resulttype != attr->atttypid)
3095  ereport(ERROR,
3096  (errcode(ERRCODE_DATATYPE_MISMATCH),
3097  errmsg("attribute %d has wrong type", fieldnum),
3098  errdetail("Table has type %s, but query expects %s.",
3099  format_type_be(attr->atttypid),
3100  format_type_be(op->d.fieldselect.resulttype))));
3101 
3102  /* heap_getattr needs a HeapTuple not a bare HeapTupleHeader */
3103  tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
3104  tmptup.t_data = tuple;
3105 
3106  /* extract the field */
3107  *op->resvalue = heap_getattr(&tmptup,
3108  fieldnum,
3109  tupDesc,
3110  op->resnull);
3111  }
3112 }
3113 
3114 /*
3115  * Deform source tuple, filling in the step's values/nulls arrays, before
3116  * evaluating individual new values as part of a FieldStore expression.
3117  * Subsequent steps will overwrite individual elements of the values/nulls
3118  * arrays with the new field values, and then FIELDSTORE_FORM will build the
3119  * new tuple value.
3120  *
3121  * Source record is in step's result variable.
3122  */
3123 void
3125 {
3126  TupleDesc tupDesc;
3127 
3128  /* Lookup tupdesc if first time through or if type changes */
3129  tupDesc = get_cached_rowtype(op->d.fieldstore.fstore->resulttype, -1,
3130  op->d.fieldstore.rowcache, NULL);
3131 
3132  /* Check that current tupdesc doesn't have more fields than we allocated */
3133  if (unlikely(tupDesc->natts > op->d.fieldstore.ncolumns))
3134  elog(ERROR, "too many columns in composite type %u",
3135  op->d.fieldstore.fstore->resulttype);
3136 
3137  if (*op->resnull)
3138  {
3139  /* Convert null input tuple into an all-nulls row */
3140  memset(op->d.fieldstore.nulls, true,
3141  op->d.fieldstore.ncolumns * sizeof(bool));
3142  }
3143  else
3144  {
3145  /*
3146  * heap_deform_tuple needs a HeapTuple not a bare HeapTupleHeader. We
3147  * set all the fields in the struct just in case.
3148  */
3149  Datum tupDatum = *op->resvalue;
3150  HeapTupleHeader tuphdr;
3151  HeapTupleData tmptup;
3152 
3153  tuphdr = DatumGetHeapTupleHeader(tupDatum);
3154  tmptup.t_len = HeapTupleHeaderGetDatumLength(tuphdr);
3155  ItemPointerSetInvalid(&(tmptup.t_self));
3156  tmptup.t_tableOid = InvalidOid;
3157  tmptup.t_data = tuphdr;
3158 
3159  heap_deform_tuple(&tmptup, tupDesc,
3160  op->d.fieldstore.values,
3161  op->d.fieldstore.nulls);
3162  }
3163 }
3164 
3165 /*
3166  * Compute the new composite datum after each individual field value of a
3167  * FieldStore expression has been evaluated.
3168  */
3169 void
3171 {
3172  TupleDesc tupDesc;
3173  HeapTuple tuple;
3174 
3175  /* Lookup tupdesc (should be valid already) */
3176  tupDesc = get_cached_rowtype(op->d.fieldstore.fstore->resulttype, -1,
3177  op->d.fieldstore.rowcache, NULL);
3178 
3179  tuple = heap_form_tuple(tupDesc,
3180  op->d.fieldstore.values,
3181  op->d.fieldstore.nulls);
3182 
3183  *op->resvalue = HeapTupleGetDatum(tuple);
3184  *op->resnull = false;
3185 }
3186 
3187 /*
3188  * Evaluate a rowtype coercion operation.
3189  * This may require rearranging field positions.
3190  *
3191  * Source record is in step's result variable.
3192  */
3193 void
3195 {
3196  HeapTuple result;
3197  Datum tupDatum;
3198  HeapTupleHeader tuple;
3199  HeapTupleData tmptup;
3200  TupleDesc indesc,
3201  outdesc;
3202  bool changed = false;
3203 
3204  /* NULL in -> NULL out */
3205  if (*op->resnull)
3206  return;
3207 
3208  tupDatum = *op->resvalue;
3209  tuple = DatumGetHeapTupleHeader(tupDatum);
3210 
3211  /*
3212  * Lookup tupdescs if first time through or if type changes. We'd better
3213  * pin them since type conversion functions could do catalog lookups and
3214  * hence cause cache invalidation.
3215  */
3216  indesc = get_cached_rowtype(op->d.convert_rowtype.inputtype, -1,
3217  op->d.convert_rowtype.incache,
3218  &changed);
3219  IncrTupleDescRefCount(indesc);
3220  outdesc = get_cached_rowtype(op->d.convert_rowtype.outputtype, -1,
3221  op->d.convert_rowtype.outcache,
3222  &changed);
3223  IncrTupleDescRefCount(outdesc);
3224 
3225  /*
3226  * We used to be able to assert that incoming tuples are marked with
3227  * exactly the rowtype of indesc. However, now that ExecEvalWholeRowVar
3228  * might change the tuples' marking to plain RECORD due to inserting
3229  * aliases, we can only make this weak test:
3230  */
3231  Assert(HeapTupleHeaderGetTypeId(tuple) == indesc->tdtypeid ||
3232  HeapTupleHeaderGetTypeId(tuple) == RECORDOID);
3233 
3234  /* if first time through, or after change, initialize conversion map */
3235  if (changed)
3236  {
3237  MemoryContext old_cxt;
3238 
3239  /* allocate map in long-lived memory context */
3240  old_cxt = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
3241 
3242  /* prepare map from old to new attribute numbers */
3243  op->d.convert_rowtype.map = convert_tuples_by_name(indesc, outdesc);
3244 
3245  MemoryContextSwitchTo(old_cxt);
3246  }
3247 
3248  /* Following steps need a HeapTuple not a bare HeapTupleHeader */
3249  tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
3250  tmptup.t_data = tuple;
3251 
3252  if (op->d.convert_rowtype.map != NULL)
3253  {
3254  /* Full conversion with attribute rearrangement needed */
3255  result = execute_attr_map_tuple(&tmptup, op->d.convert_rowtype.map);
3256  /* Result already has appropriate composite-datum header fields */
3257  *op->resvalue = HeapTupleGetDatum(result);
3258  }
3259  else
3260  {
3261  /*
3262  * The tuple is physically compatible as-is, but we need to insert the
3263  * destination rowtype OID in its composite-datum header field, so we
3264  * have to copy it anyway. heap_copy_tuple_as_datum() is convenient
3265  * for this since it will both make the physical copy and insert the
3266  * correct composite header fields. Note that we aren't expecting to
3267  * have to flatten any toasted fields: the input was a composite
3268  * datum, so it shouldn't contain any. So heap_copy_tuple_as_datum()
3269  * is overkill here, but its check for external fields is cheap.
3270  */
3271  *op->resvalue = heap_copy_tuple_as_datum(&tmptup, outdesc);
3272  }
3273 
3274  DecrTupleDescRefCount(indesc);
3275  DecrTupleDescRefCount(outdesc);
3276 }
3277 
3278 /*
3279  * Evaluate "scalar op ANY/ALL (array)".
3280  *
3281  * Source array is in our result area, scalar arg is already evaluated into
3282  * fcinfo->args[0].
3283  *
3284  * The operator always yields boolean, and we combine the results across all
3285  * array elements using OR and AND (for ANY and ALL respectively). Of course
3286  * we short-circuit as soon as the result is known.
3287  */
3288 void
3290 {
3291  FunctionCallInfo fcinfo = op->d.scalararrayop.fcinfo_data;
3292  bool useOr = op->d.scalararrayop.useOr;
3293  bool strictfunc = op->d.scalararrayop.finfo->fn_strict;
3294  ArrayType *arr;
3295  int nitems;
3296  Datum result;
3297  bool resultnull;
3298  int16 typlen;
3299  bool typbyval;
3300  char typalign;
3301  char *s;
3302  bits8 *bitmap;
3303  int bitmask;
3304 
3305  /*
3306  * If the array is NULL then we return NULL --- it's not very meaningful
3307  * to do anything else, even if the operator isn't strict.
3308  */
3309  if (*op->resnull)
3310  return;
3311 
3312  /* Else okay to fetch and detoast the array */
3313  arr = DatumGetArrayTypeP(*op->resvalue);
3314 
3315  /*
3316  * If the array is empty, we return either FALSE or TRUE per the useOr
3317  * flag. This is correct even if the scalar is NULL; since we would
3318  * evaluate the operator zero times, it matters not whether it would want
3319  * to return NULL.
3320  */
3321  nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
3322  if (nitems <= 0)
3323  {
3324  *op->resvalue = BoolGetDatum(!useOr);
3325  *op->resnull = false;
3326  return;
3327  }
3328 
3329  /*
3330  * If the scalar is NULL, and the function is strict, return NULL; no
3331  * point in iterating the loop.
3332  */
3333  if (fcinfo->args[0].isnull && strictfunc)
3334  {
3335  *op->resnull = true;
3336  return;
3337  }
3338 
3339  /*
3340  * We arrange to look up info about the element type only once per series
3341  * of calls, assuming the element type doesn't change underneath us.
3342  */
3343  if (op->d.scalararrayop.element_type != ARR_ELEMTYPE(arr))
3344  {
3346  &op->d.scalararrayop.typlen,
3347  &op->d.scalararrayop.typbyval,
3348  &op->d.scalararrayop.typalign);
3349  op->d.scalararrayop.element_type = ARR_ELEMTYPE(arr);
3350  }
3351 
3352  typlen = op->d.scalararrayop.typlen;
3353  typbyval = op->d.scalararrayop.typbyval;
3354  typalign = op->d.scalararrayop.typalign;
3355 
3356  /* Initialize result appropriately depending on useOr */
3357  result = BoolGetDatum(!useOr);
3358  resultnull = false;
3359 
3360  /* Loop over the array elements */
3361  s = (char *) ARR_DATA_PTR(arr);
3362  bitmap = ARR_NULLBITMAP(arr);
3363  bitmask = 1;
3364 
3365  for (int i = 0; i < nitems; i++)
3366  {
3367  Datum elt;
3368  Datum thisresult;
3369 
3370  /* Get array element, checking for NULL */
3371  if (bitmap && (*bitmap & bitmask) == 0)
3372  {
3373  fcinfo->args[1].value = (Datum) 0;
3374  fcinfo->args[1].isnull = true;
3375  }
3376  else
3377  {
3378  elt = fetch_att(s, typbyval, typlen);
3379  s = att_addlength_pointer(s, typlen, s);
3380  s = (char *) att_align_nominal(s, typalign);
3381  fcinfo->args[1].value = elt;
3382  fcinfo->args[1].isnull = false;
3383  }
3384 
3385  /* Call comparison function */
3386  if (fcinfo->args[1].isnull && strictfunc)
3387  {
3388  fcinfo->isnull = true;
3389  thisresult = (Datum) 0;
3390  }
3391  else
3392  {
3393  fcinfo->isnull = false;
3394  thisresult = op->d.scalararrayop.fn_addr(fcinfo);
3395  }
3396 
3397  /* Combine results per OR or AND semantics */
3398  if (fcinfo->isnull)
3399  resultnull = true;
3400  else if (useOr)
3401  {
3402  if (DatumGetBool(thisresult))
3403  {
3404  result = BoolGetDatum(true);
3405  resultnull = false;
3406  break; /* needn't look at any more elements */
3407  }
3408  }
3409  else
3410  {
3411  if (!DatumGetBool(thisresult))
3412  {
3413  result = BoolGetDatum(false);
3414  resultnull = false;
3415  break; /* needn't look at any more elements */
3416  }
3417  }
3418 
3419  /* advance bitmap pointer if any */
3420  if (bitmap)
3421  {
3422  bitmask <<= 1;
3423  if (bitmask == 0x100)
3424  {
3425  bitmap++;
3426  bitmask = 1;
3427  }
3428  }
3429  }
3430 
3431  *op->resvalue = result;
3432  *op->resnull = resultnull;
3433 }
3434 
3435 /*
3436  * Hash function for scalar array hash op elements.
3437  *
3438  * We use the element type's default hash opclass, and the column collation
3439  * if the type is collation-sensitive.
3440  */
3441 static uint32
3442 saop_element_hash(struct saophash_hash *tb, Datum key)
3443 {
3445  FunctionCallInfo fcinfo = elements_tab->op->d.hashedscalararrayop.hash_fcinfo_data;
3446  Datum hash;
3447 
3448  fcinfo->args[0].value = key;
3449  fcinfo->args[0].isnull = false;
3450 
3451  hash = elements_tab->op->d.hashedscalararrayop.hash_fn_addr(fcinfo);
3452 
3453  return DatumGetUInt32(hash);
3454 }
3455 
3456 /*
3457  * Matching function for scalar array hash op elements, to be used in hashtable
3458  * lookups.
3459  */
3460 static bool
3461 saop_hash_element_match(struct saophash_hash *tb, Datum key1, Datum key2)
3462 {
3463  Datum result;
3464 
3466  FunctionCallInfo fcinfo = elements_tab->op->d.hashedscalararrayop.fcinfo_data;
3467 
3468  fcinfo->args[0].value = key1;
3469  fcinfo->args[0].isnull = false;
3470  fcinfo->args[1].value = key2;
3471  fcinfo->args[1].isnull = false;
3472 
3473  result = elements_tab->op->d.hashedscalararrayop.fn_addr(fcinfo);
3474 
3475  return DatumGetBool(result);
3476 }
3477 
3478 /*
3479  * Evaluate "scalar op ANY (const array)".
3480  *
3481  * Similar to ExecEvalScalarArrayOp, but optimized for faster repeat lookups
3482  * by building a hashtable on the first lookup. This hashtable will be reused
3483  * by subsequent lookups. Unlike ExecEvalScalarArrayOp, this version only
3484  * supports OR semantics.
3485  *
3486  * Source array is in our result area, scalar arg is already evaluated into
3487  * fcinfo->args[0].
3488  *
3489  * The operator always yields boolean.
3490  */
3491 void
3493 {
3495  FunctionCallInfo fcinfo = op->d.hashedscalararrayop.fcinfo_data;
3496  bool inclause = op->d.hashedscalararrayop.inclause;
3497  bool strictfunc = op->d.hashedscalararrayop.finfo->fn_strict;
3498  Datum scalar = fcinfo->args[0].value;
3499  bool scalar_isnull = fcinfo->args[0].isnull;
3500  Datum result;
3501  bool resultnull;
3502  bool hashfound;
3503 
3504  /* We don't setup a hashed scalar array op if the array const is null. */
3505  Assert(!*op->resnull);
3506 
3507  /*
3508  * If the scalar is NULL, and the function is strict, return NULL; no
3509  * point in executing the search.
3510  */
3511  if (fcinfo->args[0].isnull && strictfunc)
3512  {
3513  *op->resnull = true;
3514  return;
3515  }
3516 
3517  /* Build the hash table on first evaluation */
3518  if (elements_tab == NULL)
3519  {
3520  int16 typlen;
3521  bool typbyval;
3522  char typalign;
3523  int nitems;
3524  bool has_nulls = false;
3525  char *s;
3526  bits8 *bitmap;
3527  int bitmask;
3528  MemoryContext oldcontext;
3529  ArrayType *arr;
3530 
3531  arr = DatumGetArrayTypeP(*op->resvalue);
3532  nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
3533 
3535  &typlen,
3536  &typbyval,
3537  &typalign);
3538 
3539  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
3540 
3541  elements_tab = (ScalarArrayOpExprHashTable *)
3543  op->d.hashedscalararrayop.elements_tab = elements_tab;
3544  elements_tab->op = op;
3545 
3546  /*
3547  * Create the hash table sizing it according to the number of elements
3548  * in the array. This does assume that the array has no duplicates.
3549  * If the array happens to contain many duplicate values then it'll
3550  * just mean that we sized the table a bit on the large side.
3551  */
3552  elements_tab->hashtab = saophash_create(CurrentMemoryContext, nitems,
3553  elements_tab);
3554 
3555  MemoryContextSwitchTo(oldcontext);
3556 
3557  s = (char *) ARR_DATA_PTR(arr);
3558  bitmap = ARR_NULLBITMAP(arr);
3559  bitmask = 1;
3560  for (int i = 0; i < nitems; i++)
3561  {
3562  /* Get array element, checking for NULL. */
3563  if (bitmap && (*bitmap & bitmask) == 0)
3564  {
3565  has_nulls = true;
3566  }
3567  else
3568  {
3569  Datum element;
3570 
3571  element = fetch_att(s, typbyval, typlen);
3572  s = att_addlength_pointer(s, typlen, s);
3573  s = (char *) att_align_nominal(s, typalign);
3574 
3575  saophash_insert(elements_tab->hashtab, element, &hashfound);
3576  }
3577 
3578  /* Advance bitmap pointer if any. */
3579  if (bitmap)
3580  {
3581  bitmask <<= 1;
3582  if (bitmask == 0x100)
3583  {
3584  bitmap++;
3585  bitmask = 1;
3586  }
3587  }
3588  }
3589 
3590  /*
3591  * Remember if we had any nulls so that we know if we need to execute
3592  * non-strict functions with a null lhs value if no match is found.
3593  */
3594  op->d.hashedscalararrayop.has_nulls = has_nulls;
3595  }
3596 
3597  /* Check the hash to see if we have a match. */
3598  hashfound = NULL != saophash_lookup(elements_tab->hashtab, scalar);
3599 
3600  /* the result depends on if the clause is an IN or NOT IN clause */
3601  if (inclause)
3602  result = BoolGetDatum(hashfound); /* IN */
3603  else
3604  result = BoolGetDatum(!hashfound); /* NOT IN */
3605 
3606  resultnull = false;
3607 
3608  /*
3609  * If we didn't find a match in the array, we still might need to handle
3610  * the possibility of null values. We didn't put any NULLs into the
3611  * hashtable, but instead marked if we found any when building the table
3612  * in has_nulls.
3613  */
3614  if (!hashfound && op->d.hashedscalararrayop.has_nulls)
3615  {
3616  if (strictfunc)
3617  {
3618 
3619  /*
3620  * We have nulls in the array so a non-null lhs and no match must
3621  * yield NULL.
3622  */
3623  result = (Datum) 0;
3624  resultnull = true;
3625  }
3626  else
3627  {
3628  /*
3629  * Execute function will null rhs just once.
3630  *
3631  * The hash lookup path will have scribbled on the lhs argument so
3632  * we need to set it up also (even though we entered this function
3633  * with it already set).
3634  */
3635  fcinfo->args[0].value = scalar;
3636  fcinfo->args[0].isnull = scalar_isnull;
3637  fcinfo->args[1].value = (Datum) 0;
3638  fcinfo->args[1].isnull = true;
3639 
3640  result = op->d.hashedscalararrayop.fn_addr(fcinfo);
3641  resultnull = fcinfo->isnull;
3642 
3643  /*
3644  * Reverse the result for NOT IN clauses since the above function
3645  * is the equality function and we need not-equals.
3646  */
3647  if (!inclause)
3648  result = !result;
3649  }
3650  }
3651 
3652  *op->resvalue = result;
3653  *op->resnull = resultnull;
3654 }
3655 
3656 /*
3657  * Evaluate a NOT NULL domain constraint.
3658  */
3659 void
3661 {
3662  if (*op->resnull)
3663  ereport(ERROR,
3664  (errcode(ERRCODE_NOT_NULL_VIOLATION),
3665  errmsg("domain %s does not allow null values",
3666  format_type_be(op->d.domaincheck.resulttype)),
3667  errdatatype(op->d.domaincheck.resulttype)));
3668 }
3669 
3670 /*
3671  * Evaluate a CHECK domain constraint.
3672  */
3673 void
3675 {
3676  if (!*op->d.domaincheck.checknull &&
3677  !DatumGetBool(*op->d.domaincheck.checkvalue))
3678  ereport(ERROR,
3679  (errcode(ERRCODE_CHECK_VIOLATION),
3680  errmsg("value for domain %s violates check constraint \"%s\"",
3681  format_type_be(op->d.domaincheck.resulttype),
3682  op->d.domaincheck.constraintname),
3683  errdomainconstraint(op->d.domaincheck.resulttype,
3684  op->d.domaincheck.constraintname)));
3685 }
3686 
3687 /*
3688  * Evaluate the various forms of XmlExpr.
3689  *
3690  * Arguments have been evaluated into named_argvalue/named_argnull
3691  * and/or argvalue/argnull arrays.
3692  */
3693 void
3695 {
3696  XmlExpr *xexpr = op->d.xmlexpr.xexpr;
3697  Datum value;
3698 
3699  *op->resnull = true; /* until we get a result */
3700  *op->resvalue = (Datum) 0;
3701 
3702  switch (xexpr->op)
3703  {
3704  case IS_XMLCONCAT:
3705  {
3706  Datum *argvalue = op->d.xmlexpr.argvalue;
3707  bool *argnull = op->d.xmlexpr.argnull;
3708  List *values = NIL;
3709 
3710  for (int i = 0; i < list_length(xexpr->args); i++)
3711  {
3712  if (!argnull[i])
3713  values = lappend(values, DatumGetPointer(argvalue[i]));
3714  }
3715 
3716  if (values != NIL)
3717  {
3718  *op->resvalue = PointerGetDatum(xmlconcat(values));
3719  *op->resnull = false;
3720  }
3721  }
3722  break;
3723 
3724  case IS_XMLFOREST:
3725  {
3726  Datum *argvalue = op->d.xmlexpr.named_argvalue;
3727  bool *argnull = op->d.xmlexpr.named_argnull;
3729  ListCell *lc;
3730  ListCell *lc2;
3731  int i;
3732 
3733  initStringInfo(&buf);
3734 
3735  i = 0;
3736  forboth(lc, xexpr->named_args, lc2, xexpr->arg_names)
3737  {
3738  Expr *e = (Expr *) lfirst(lc);
3739  char *argname = strVal(lfirst(lc2));
3740 
3741  if (!argnull[i])
3742  {
3743  value = argvalue[i];
3744  appendStringInfo(&buf, "<%s>%s</%s>",
3745  argname,
3747  exprType((Node *) e), true),
3748  argname);
3749  *op->resnull = false;
3750  }
3751  i++;
3752  }
3753 
3754  if (!*op->resnull)
3755  {
3756  text *result;
3757 
3758  result = cstring_to_text_with_len(buf.data, buf.len);
3759  *op->resvalue = PointerGetDatum(result);
3760  }
3761 
3762  pfree(buf.data);
3763  }
3764  break;
3765 
3766  case IS_XMLELEMENT:
3767  *op->resvalue = PointerGetDatum(xmlelement(xexpr,
3768  op->d.xmlexpr.named_argvalue,
3769  op->d.xmlexpr.named_argnull,
3770  op->d.xmlexpr.argvalue,
3771  op->d.xmlexpr.argnull));
3772  *op->resnull = false;
3773  break;
3774 
3775  case IS_XMLPARSE:
3776  {
3777  Datum *argvalue = op->d.xmlexpr.argvalue;
3778  bool *argnull = op->d.xmlexpr.argnull;
3779  text *data;
3780  bool preserve_whitespace;
3781 
3782  /* arguments are known to be text, bool */
3783  Assert(list_length(xexpr->args) == 2);
3784 
3785  if (argnull[0])
3786  return;
3787  value = argvalue[0];
3788  data = DatumGetTextPP(value);
3789 
3790  if (argnull[1]) /* probably can't happen */
3791  return;
3792  value = argvalue[1];
3793  preserve_whitespace = DatumGetBool(value);
3794 
3795  *op->resvalue = PointerGetDatum(xmlparse(data,
3796  xexpr->xmloption,
3797  preserve_whitespace));
3798  *op->resnull = false;
3799  }
3800  break;
3801 
3802  case IS_XMLPI:
3803  {
3804  text *arg;
3805  bool isnull;
3806 
3807  /* optional argument is known to be text */
3808  Assert(list_length(xexpr->args) <= 1);
3809 
3810  if (xexpr->args)
3811  {
3812  isnull = op->d.xmlexpr.argnull[0];
3813  if (isnull)
3814  arg = NULL;
3815  else
3816  arg = DatumGetTextPP(op->d.xmlexpr.argvalue[0]);
3817  }
3818  else
3819  {
3820  arg = NULL;
3821  isnull = false;
3822  }
3823 
3824  *op->resvalue = PointerGetDatum(xmlpi(xexpr->name,
3825  arg,
3826  isnull,
3827  op->resnull));
3828  }
3829  break;
3830 
3831  case IS_XMLROOT:
3832  {
3833  Datum *argvalue = op->d.xmlexpr.argvalue;
3834  bool *argnull = op->d.xmlexpr.argnull;
3835  xmltype *data;
3836  text *version;
3837  int standalone;
3838 
3839  /* arguments are known to be xml, text, int */
3840  Assert(list_length(xexpr->args) == 3);
3841 
3842  if (argnull[0])
3843  return;
3844  data = DatumGetXmlP(argvalue[0]);
3845 
3846  if (argnull[1])
3847  version = NULL;
3848  else
3849  version = DatumGetTextPP(argvalue[1]);
3850 
3851  Assert(!argnull[2]); /* always present */
3852  standalone = DatumGetInt32(argvalue[2]);
3853 
3854  *op->resvalue = PointerGetDatum(xmlroot(data,
3855  version,
3856  standalone));
3857  *op->resnull = false;
3858  }
3859  break;
3860 
3861  case IS_XMLSERIALIZE:
3862  {
3863  Datum *argvalue = op->d.xmlexpr.argvalue;
3864  bool *argnull = op->d.xmlexpr.argnull;
3865 
3866  /* argument type is known to be xml */
3867  Assert(list_length(xexpr->args) == 1);
3868 
3869  if (argnull[0])
3870  return;
3871  value = argvalue[0];
3872 
3874  xexpr->xmloption));
3875  *op->resnull = false;
3876  }
3877  break;
3878 
3879  case IS_DOCUMENT:
3880  {
3881  Datum *argvalue = op->d.xmlexpr.argvalue;
3882  bool *argnull = op->d.xmlexpr.argnull;
3883 
3884  /* optional argument is known to be xml */
3885  Assert(list_length(xexpr->args) == 1);
3886 
3887  if (argnull[0])
3888  return;
3889  value = argvalue[0];
3890 
3891  *op->resvalue =
3893  *op->resnull = false;
3894  }
3895  break;
3896 
3897  default:
3898  elog(ERROR, "unrecognized XML operation");
3899  break;
3900  }
3901 }
3902 
3903 /*
3904  * ExecEvalGroupingFunc
3905  *
3906  * Computes a bitmask with a bit for each (unevaluated) argument expression
3907  * (rightmost arg is least significant bit).
3908  *
3909  * A bit is set if the corresponding expression is NOT part of the set of
3910  * grouping expressions in the current grouping set.
3911  */
3912 void
3914 {
3915  AggState *aggstate = castNode(AggState, state->parent);
3916  int result = 0;
3917  Bitmapset *grouped_cols = aggstate->grouped_cols;
3918  ListCell *lc;
3919 
3920  foreach(lc, op->d.grouping_func.clauses)
3921  {
3922  int attnum = lfirst_int(lc);
3923 
3924  result <<= 1;
3925 
3926  if (!bms_is_member(attnum, grouped_cols))
3927  result |= 1;
3928  }
3929 
3930  *op->resvalue = Int32GetDatum(result);
3931  *op->resnull = false;
3932 }
3933 
3934 /*
3935  * Hand off evaluation of a subplan to nodeSubplan.c
3936  */
3937 void
3939 {
3940  SubPlanState *sstate = op->d.subplan.sstate;
3941 
3942  /* could potentially be nested, so make sure there's enough stack */
3944 
3945  *op->resvalue = ExecSubPlan(sstate, econtext, op->resnull);
3946 }
3947 
3948 /*
3949  * Evaluate a wholerow Var expression.
3950  *
3951  * Returns a Datum whose value is the value of a whole-row range variable
3952  * with respect to given expression context.
3953  */
3954 void
3956 {
3957  Var *variable = op->d.wholerow.var;
3958  TupleTableSlot *slot;
3959  TupleDesc output_tupdesc;
3960  MemoryContext oldcontext;
3961  HeapTupleHeader dtuple;
3962  HeapTuple tuple;
3963 
3964  /* This was checked by ExecInitExpr */
3965  Assert(variable->varattno == InvalidAttrNumber);
3966 
3967  /* Get the input slot we want */
3968  switch (variable->varno)
3969  {
3970  case INNER_VAR:
3971  /* get the tuple from the inner node */
3972  slot = econtext->ecxt_innertuple;
3973  break;
3974 
3975  case OUTER_VAR:
3976  /* get the tuple from the outer node */
3977  slot = econtext->ecxt_outertuple;
3978  break;
3979 
3980  /* INDEX_VAR is handled by default case */
3981 
3982  default:
3983  /* get the tuple from the relation being scanned */
3984  slot = econtext->ecxt_scantuple;
3985  break;
3986  }
3987 
3988  /* Apply the junkfilter if any */
3989  if (op->d.wholerow.junkFilter != NULL)
3990  slot = ExecFilterJunk(op->d.wholerow.junkFilter, slot);
3991 
3992  /*
3993  * If first time through, obtain tuple descriptor and check compatibility.
3994  *
3995  * XXX: It'd be great if this could be moved to the expression
3996  * initialization phase, but due to using slots that's currently not
3997  * feasible.
3998  */
3999  if (op->d.wholerow.first)
4000  {
4001  /* optimistically assume we don't need slow path */
4002  op->d.wholerow.slow = false;
4003 
4004  /*
4005  * If the Var identifies a named composite type, we must check that
4006  * the actual tuple type is compatible with it.
4007  */
4008  if (variable->vartype != RECORDOID)
4009  {
4010  TupleDesc var_tupdesc;
4011  TupleDesc slot_tupdesc;
4012 
4013  /*
4014  * We really only care about numbers of attributes and data types.
4015  * Also, we can ignore type mismatch on columns that are dropped
4016  * in the destination type, so long as (1) the physical storage
4017  * matches or (2) the actual column value is NULL. Case (1) is
4018  * helpful in some cases involving out-of-date cached plans, while
4019  * case (2) is expected behavior in situations such as an INSERT
4020  * into a table with dropped columns (the planner typically
4021  * generates an INT4 NULL regardless of the dropped column type).
4022  * If we find a dropped column and cannot verify that case (1)
4023  * holds, we have to use the slow path to check (2) for each row.
4024  *
4025  * If vartype is a domain over composite, just look through that
4026  * to the base composite type.
4027  */
4028  var_tupdesc = lookup_rowtype_tupdesc_domain(variable->vartype,
4029  -1, false);
4030 
4031  slot_tupdesc = slot->tts_tupleDescriptor;
4032 
4033  if (var_tupdesc->natts != slot_tupdesc->natts)
4034  ereport(ERROR,
4035  (errcode(ERRCODE_DATATYPE_MISMATCH),
4036  errmsg("table row type and query-specified row type do not match"),
4037  errdetail_plural("Table row contains %d attribute, but query expects %d.",
4038  "Table row contains %d attributes, but query expects %d.",
4039  slot_tupdesc->natts,
4040  slot_tupdesc->natts,
4041  var_tupdesc->natts)));
4042 
4043  for (int i = 0; i < var_tupdesc->natts; i++)
4044  {
4045  Form_pg_attribute vattr = TupleDescAttr(var_tupdesc, i);
4046  Form_pg_attribute sattr = TupleDescAttr(slot_tupdesc, i);
4047 
4048  if (vattr->atttypid == sattr->atttypid)
4049  continue; /* no worries */
4050  if (!vattr->attisdropped)
4051  ereport(ERROR,
4052  (errcode(ERRCODE_DATATYPE_MISMATCH),
4053  errmsg("table row type and query-specified row type do not match"),
4054  errdetail("Table has type %s at ordinal position %d, but query expects %s.",
4055  format_type_be(sattr->atttypid),
4056  i + 1,
4057  format_type_be(vattr->atttypid))));
4058 
4059  if (vattr->attlen != sattr->attlen ||
4060  vattr->attalign != sattr->attalign)
4061  op->d.wholerow.slow = true; /* need to check for nulls */
4062  }
4063 
4064  /*
4065  * Use the variable's declared rowtype as the descriptor for the
4066  * output values, modulo possibly assigning new column names
4067  * below. In particular, we *must* absorb any attisdropped
4068  * markings.
4069  */
4070  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
4071  output_tupdesc = CreateTupleDescCopy(var_tupdesc);
4072  MemoryContextSwitchTo(oldcontext);
4073 
4074  ReleaseTupleDesc(var_tupdesc);
4075  }
4076  else
4077  {
4078  /*
4079  * In the RECORD case, we use the input slot's rowtype as the
4080  * descriptor for the output values, modulo possibly assigning new
4081  * column names below.
4082  */
4083  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
4084  output_tupdesc = CreateTupleDescCopy(slot->tts_tupleDescriptor);
4085  MemoryContextSwitchTo(oldcontext);
4086  }
4087 
4088  /*
4089  * Construct a tuple descriptor for the composite values we'll
4090  * produce, and make sure its record type is "blessed". The main
4091  * reason to do this is to be sure that operations such as
4092  * row_to_json() will see the desired column names when they look up
4093  * the descriptor from the type information embedded in the composite
4094  * values.
4095  *
4096  * We already got the correct physical datatype info above, but now we
4097  * should try to find the source RTE and adopt its column aliases, in
4098  * case they are different from the original rowtype's names. For
4099  * example, in "SELECT foo(t) FROM tab t(x,y)", the first two columns
4100  * in the composite output should be named "x" and "y" regardless of
4101  * tab's column names.
4102  *
4103  * If we can't locate the RTE, assume the column names we've got are
4104  * OK. (As of this writing, the only cases where we can't locate the
4105  * RTE are in execution of trigger WHEN clauses, and then the Var will
4106  * have the trigger's relation's rowtype, so its names are fine.)
4107  * Also, if the creator of the RTE didn't bother to fill in an eref
4108  * field, assume our column names are OK. (This happens in COPY, and
4109  * perhaps other places.)
4110  */
4111  if (econtext->ecxt_estate &&
4112  variable->varno <= econtext->ecxt_estate->es_range_table_size)
4113  {
4114  RangeTblEntry *rte = exec_rt_fetch(variable->varno,
4115  econtext->ecxt_estate);
4116 
4117  if (rte->eref)
4118  ExecTypeSetColNames(output_tupdesc, rte->eref->colnames);
4119  }
4120 
4121  /* Bless the tupdesc if needed, and save it in the execution state */
4122  op->d.wholerow.tupdesc = BlessTupleDesc(output_tupdesc);
4123 
4124  op->d.wholerow.first = false;
4125  }
4126 
4127  /*
4128  * Make sure all columns of the slot are accessible in the slot's
4129  * Datum/isnull arrays.
4130  */
4131  slot_getallattrs(slot);
4132 
4133  if (op->d.wholerow.slow)
4134  {
4135  /* Check to see if any dropped attributes are non-null */
4136  TupleDesc tupleDesc = slot->tts_tupleDescriptor;
4137  TupleDesc var_tupdesc = op->d.wholerow.tupdesc;
4138 
4139  Assert(var_tupdesc->natts == tupleDesc->natts);
4140 
4141  for (int i = 0; i < var_tupdesc->natts; i++)
4142  {
4143  Form_pg_attribute vattr = TupleDescAttr(var_tupdesc, i);
4144  Form_pg_attribute sattr = TupleDescAttr(tupleDesc, i);
4145 
4146  if (!vattr->attisdropped)
4147  continue; /* already checked non-dropped cols */
4148  if (slot->tts_isnull[i])
4149  continue; /* null is always okay */
4150  if (vattr->attlen != sattr->attlen ||
4151  vattr->attalign != sattr->attalign)
4152  ereport(ERROR,
4153  (errcode(ERRCODE_DATATYPE_MISMATCH),
4154  errmsg("table row type and query-specified row type do not match"),
4155  errdetail("Physical storage mismatch on dropped attribute at ordinal position %d.",
4156  i + 1)));
4157  }
4158  }
4159 
4160  /*
4161  * Build a composite datum, making sure any toasted fields get detoasted.
4162  *
4163  * (Note: it is critical that we not change the slot's state here.)
4164  */
4166  slot->tts_values,
4167  slot->tts_isnull);
4168  dtuple = tuple->t_data;
4169 
4170  /*
4171  * Label the datum with the composite type info we identified before.
4172  *
4173  * (Note: we could skip doing this by passing op->d.wholerow.tupdesc to
4174  * the tuple build step; but that seems a tad risky so let's not.)
4175  */
4176  HeapTupleHeaderSetTypeId(dtuple, op->d.wholerow.tupdesc->tdtypeid);
4177  HeapTupleHeaderSetTypMod(dtuple, op->d.wholerow.tupdesc->tdtypmod);
4178 
4179  *op->resvalue = PointerGetDatum(dtuple);
4180  *op->resnull = false;
4181 }
4182 
4183 void
4185  TupleTableSlot *slot)
4186 {
4187  Datum d;
4188 
4189  /* slot_getsysattr has sufficient defenses against bad attnums */
4190  d = slot_getsysattr(slot,
4191  op->d.var.attnum,
4192  op->resnull);
4193  *op->resvalue = d;
4194  /* this ought to be unreachable, but it's cheap enough to check */
4195  if (unlikely(*op->resnull))
4196  elog(ERROR, "failed to fetch attribute from slot");
4197 }
4198 
4199 /*
4200  * Transition value has not been initialized. This is the first non-NULL input
4201  * value for a group. We use it as the initial value for transValue.
4202  */
4203 void
4206 {
4207  FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
4208  MemoryContext oldContext;
4209 
4210  /*
4211  * We must copy the datum into aggcontext if it is pass-by-ref. We do not
4212  * need to pfree the old transValue, since it's NULL. (We already checked
4213  * that the agg's input type is binary-compatible with its transtype, so
4214  * straight copy here is OK.)
4215  */
4216  oldContext = MemoryContextSwitchTo(aggcontext->ecxt_per_tuple_memory);
4217  pergroup->transValue = datumCopy(fcinfo->args[1].value,
4218  pertrans->transtypeByVal,
4219  pertrans->transtypeLen);
4220  pergroup->transValueIsNull = false;
4221  pergroup->noTransValue = false;
4222  MemoryContextSwitchTo(oldContext);
4223 }
4224 
4225 /*
4226  * Ensure that the current transition value is a child of the aggcontext,
4227  * rather than the per-tuple context.
4228  *
4229  * NB: This can change the current memory context.
4230  */
4231 Datum
4233  Datum newValue, bool newValueIsNull,
4234  Datum oldValue, bool oldValueIsNull)
4235 {
4236  Assert(newValue != oldValue);
4237 
4238  if (!newValueIsNull)
4239  {
4241  if (DatumIsReadWriteExpandedObject(newValue,
4242  false,
4243  pertrans->transtypeLen) &&
4244  MemoryContextGetParent(DatumGetEOHP(newValue)->eoh_context) == CurrentMemoryContext)
4245  /* do nothing */ ;
4246  else
4247  newValue = datumCopy(newValue,
4248  pertrans->transtypeByVal,
4249  pertrans->transtypeLen);
4250  }
4251  else
4252  {
4253  /*
4254  * Ensure that AggStatePerGroup->transValue ends up being 0, so
4255  * callers can safely compare newValue/oldValue without having to
4256  * check their respective nullness.
4257  */
4258  newValue = (Datum) 0;
4259  }
4260 
4261  if (!oldValueIsNull)
4262  {
4263  if (DatumIsReadWriteExpandedObject(oldValue,
4264  false,
4265  pertrans->transtypeLen))
4266  DeleteExpandedObject(oldValue);
4267  else
4268  pfree(DatumGetPointer(oldValue));
4269  }
4270 
4271  return newValue;
4272 }
4273 
4274 /*
4275  * Invoke ordered transition function, with a datum argument.
4276  */
4277 void
4279  ExprContext *econtext)
4280 {
4281  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
4282  int setno = op->d.agg_trans.setno;
4283 
4284  tuplesort_putdatum(pertrans->sortstates[setno],
4285  *op->resvalue, *op->resnull);
4286 }
4287 
4288 /*
4289  * Invoke ordered transition function, with a tuple argument.
4290  */
4291 void
4293  ExprContext *econtext)
4294 {
4295  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
4296  int setno = op->d.agg_trans.setno;
4297 
4298  ExecClearTuple(pertrans->sortslot);
4299  pertrans->sortslot->tts_nvalid = pertrans->numInputs;
4300  ExecStoreVirtualTuple(pertrans->sortslot);
4301  tuplesort_puttupleslot(pertrans->sortstates[setno], pertrans->sortslot);
4302 }
4303 
4304 /* implementation of transition function invocation for byval types */
4305 static pg_attribute_always_inline void
4307  AggStatePerGroup pergroup,
4309 {
4310  FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
4311  MemoryContext oldContext;
4312  Datum newVal;
4313 
4314  /* cf. select_current_set() */
4315  aggstate->curaggcontext = aggcontext;
4316  aggstate->current_set = setno;
4317 
4318  /* set up aggstate->curpertrans for AggGetAggref() */
4319  aggstate->curpertrans = pertrans;
4320 
4321  /* invoke transition function in per-tuple context */
4322  oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
4323 
4324  fcinfo->args[0].value = pergroup->transValue;
4325  fcinfo->args[0].isnull = pergroup->transValueIsNull;
4326  fcinfo->isnull = false; /* just in case transfn doesn't set it */
4327 
4328  newVal = FunctionCallInvoke(fcinfo);
4329 
4330  pergroup->transValue = newVal;
4331  pergroup->transValueIsNull = fcinfo->isnull;
4332 
4333  MemoryContextSwitchTo(oldContext);
4334 }
4335 
4336 /* implementation of transition function invocation for byref types */
4337 static pg_attribute_always_inline void
4339  AggStatePerGroup pergroup,
4341 {
4342  FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
4343  MemoryContext oldContext;
4344  Datum newVal;
4345 
4346  /* cf. select_current_set() */
4347  aggstate->curaggcontext = aggcontext;
4348  aggstate->current_set = setno;
4349 
4350  /* set up aggstate->curpertrans for AggGetAggref() */
4351  aggstate->curpertrans = pertrans;
4352 
4353  /* invoke transition function in per-tuple context */
4354  oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
4355 
4356  fcinfo->args[0].value = pergroup->transValue;
4357  fcinfo->args[0].isnull = pergroup->transValueIsNull;
4358  fcinfo->isnull = false; /* just in case transfn doesn't set it */
4359 
4360  newVal = FunctionCallInvoke(fcinfo);
4361 
4362  /*
4363  * For pass-by-ref datatype, must copy the new value into aggcontext and
4364  * free the prior transValue. But if transfn returned a pointer to its
4365  * first input, we don't need to do anything. Also, if transfn returned a
4366  * pointer to a R/W expanded object that is already a child of the
4367  * aggcontext, assume we can adopt that value without copying it.
4368  *
4369  * It's safe to compare newVal with pergroup->transValue without regard
4370  * for either being NULL, because ExecAggTransReparent() takes care to set
4371  * transValue to 0 when NULL. Otherwise we could end up accidentally not
4372  * reparenting, when the transValue has the same numerical value as
4373  * newValue, despite being NULL. This is a somewhat hot path, making it
4374  * undesirable to instead solve this with another branch for the common
4375  * case of the transition function returning its (modified) input
4376  * argument.
4377  */
4378  if (DatumGetPointer(newVal) != DatumGetPointer(pergroup->transValue))
4379  newVal = ExecAggTransReparent(aggstate, pertrans,
4380  newVal, fcinfo->isnull,
4381  pergroup->transValue,
4382  pergroup->transValueIsNull);
4383 
4384  pergroup->transValue = newVal;
4385  pergroup->transValueIsNull = fcinfo->isnull;
4386 
4387  MemoryContextSwitchTo(oldContext);
4388 }
SubPlanState * sstate
Definition: execExpr.h:624
#define TTS_FIXED(slot)
Definition: tuptable.h:109
AggStatePerTrans pertrans
Definition: execExpr.h:665
struct ScalarArrayOpExprHashEntry ScalarArrayOpExprHashEntry
signed short int16
Definition: c.h:428
struct ExprEvalStep::@49::@86 subplan
#define DatumGetUInt32(X)
Definition: postgres.h:530
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:125
#define NIL
Definition: pg_list.h:65
void ExecEvalScalarArrayOp(ExprState *state, ExprEvalStep *op)
Datum value
Definition: params.h:92
Datum ExecInterpExprStillValid(ExprState *state, ExprContext *econtext, bool *isNull)
struct ExprEvalStep::@49::@52 wholerow
void IncrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:366
struct ExprEvalStep::@49::@80 scalararrayop
void * execPlan
Definition: params.h:148
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111
Datum * ecxt_aggvalues
Definition: execnodes.h:245
Datum MakeExpandedObjectReadOnlyInternal(Datum d)
Definition: expandeddatum.c:95
#define HeapTupleHeaderSetTypeId(tup, typeid)
Definition: htup_details.h:462
struct PlanState * parent
Definition: execnodes.h:108
#define likely(x)
Definition: c.h:272
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
RowCompareType rctype
Definition: execExpr.h:476
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:303
void tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull)
Definition: tuplesort.c:1805
Datum current_schema(PG_FUNCTION_ARGS)
Definition: name.c:279
struct ExprEvalStep * op
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:148
static Datum ExecJustOuterVar(ExprState *state, ExprContext *econtext, bool *isnull)
static pg_attribute_always_inline void ExecAggPlainTransByVal(AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroup, ExprContext *aggcontext, int setno)
char * name
Definition: primnodes.h:1228
#define ARR_SIZE(a)
Definition: array.h:282
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: postgres.h:335
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1824
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
static Datum ExecJustScanVar(ExprState *state, ExprContext *econtext, bool *isnull)
Datum * resvalue
Definition: execExpr.h:273
List * colnames
Definition: primnodes.h:43
#define DatumGetInt32(X)
Definition: postgres.h:516
MemoryContext MemoryContextGetParent(MemoryContext context)
Definition: mcxt.c:446
struct ExprEvalStep::@49::@50 fetch
static Datum ExecJustInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
bool has_nulls
Definition: execExpr.h:576
#define castNode(_type_, nodeptr)
Definition: nodes.h:608
static Datum expanded_record_get_field(ExpandedRecordHeader *erh, int fnumber, bool *isnull)
#define EEO_SWITCH()
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2218
#define PointerGetDatum(X)
Definition: postgres.h:600
static uint32 saop_element_hash(struct saophash_hash *tb, Datum key)
struct ExprEvalStep * steps
Definition: execnodes.h:85
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
ExprEvalRowtypeCache rowcache
Definition: execExpr.h:373
void ExecReadyInterpretedExpr(ExprState *state)
struct ExprEvalStep::@49::@57 boolexpr
struct ExprEvalStep::@49::@58 qualexpr
const TupleTableSlotOps TTSOpsBufferHeapTuple
Definition: execTuples.c:86
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:234
#define EEO_JUMP(stepno)
void array_bitmap_copy(bits8 *destbitmap, int destoffset, const bits8 *srcbitmap, int srcoffset, int nitems)
Definition: arrayfuncs.c:4721
int64 nextval_internal(Oid relid, bool check_permissions)
Definition: sequence.c:579
int32 dataoffset
Definition: array.h:89
#define DatumGetTextPP(X)
Definition: fmgr.h:292
void ExecEvalParamExec(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
bool * resnull
Definition: execExpr.h:274
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:76
#define TimeTzADTPGetDatum(X)
Definition: date.h:59
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define Int16GetDatum(X)
Definition: postgres.h:495
ExprEvalOp
Definition: execExpr.h:64
xmltype * xmlconcat(List *args)
Definition: xml.c:512
bool * nulls
Definition: execExpr.h:484
static void slot_getsomeattrs(TupleTableSlot *slot, int attnum)
Definition: tuptable.h:341
struct ExprEvalStep::@49::@72 rowcompare_final
Definition: nodes.h:539
#define strVal(v)
Definition: value.h:54
const TupleTableSlotOps *const tts_ops
Definition: tuptable.h:122
TimestampTz GetSQLCurrentTimestamp(int32 typmod)
Definition: timestamp.c:1598
int errcode(int sqlerrcode)
Definition: elog.c:698
struct ExprEvalStep::@49::@74 fieldselect
void ExecEvalConvertRowtype(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
#define DateADTGetDatum(X)
Definition: date.h:57
AttrNumber varattno
Definition: primnodes.h:191
bool useOr
Definition: execExpr.h:563
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
Datum * tts_values
Definition: tuptable.h:126
struct ExprEvalStep::@49::@83 aggref
XmlExpr * xexpr
Definition: execExpr.h:592
struct ExprEvalStep::@49::@67 nextvalueexpr
MinMaxOp op
Definition: execExpr.h:487
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
static pg_attribute_always_inline Datum ExecJustAssignVarImpl(ExprState *state, TupleTableSlot *inslot, bool *isnull)
#define lengthof(array)
Definition: c.h:734
ArrayType * construct_empty_array(Oid elmtype)
Definition: arrayfuncs.c:3439
void ExecEvalHashedScalarArrayOp(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
static bool saop_hash_element_match(struct saophash_hash *tb, Datum key1, Datum key2)
DateADT GetSQLCurrentDate(void)
Definition: date.c:301
TupleTableSlot * resultslot
Definition: execnodes.h:80
ExprContext * aggcontext
Definition: execExpr.h:666
unsigned int Oid
Definition: postgres_ext.h:31
Datum domainValue_datum
Definition: execnodes.h:257
List * arg_names
Definition: primnodes.h:1230
Definition: primnodes.h:186
#define DatumGetXmlP(X)
Definition: xml.h:50
int current_set
Definition: execnodes.h:2299
#define OidIsValid(objectId)
Definition: c.h:710
void ExecEvalFieldStoreForm(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:295
FunctionCallInfo transfn_fcinfo
Definition: nodeAgg.h:162
Datum(* ExprStateEvalFunc)(struct ExprState *expression, struct ExprContext *econtext, bool *isNull)
Definition: execnodes.h:53
#define ARR_OVERHEAD_WITHNULLS(ndims, nitems)
Definition: array.h:305
struct ExprEvalStep::@49::@71 rowcompare_step
Datum ExecAggTransReparent(AggState *aggstate, AggStatePerTrans pertrans, Datum newValue, bool newValueIsNull, Datum oldValue, bool oldValueIsNull)
struct ExprEvalStep::@49::@51 var
SQLValueFunctionOp op
Definition: primnodes.h:1189
TupleDesc lookup_rowtype_tupdesc_domain(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1880
ExprContext * tmpcontext
Definition: execnodes.h:2289
signed int int32
Definition: c.h:429
Datum current_database(PG_FUNCTION_ARGS)
Definition: misc.c:173
xmltype * xmlpi(const char *target, text *arg, bool arg_is_null, bool *result_is_null)
Definition: xml.c:770
struct ExprEvalStep::@49::@62 cparam
void ExecEvalRow(ExprState *state, ExprEvalStep *op)
#define ARR_DATA_OFFSET(a)
Definition: array.h:309
#define ARR_LBOUND(a)
Definition: array.h:289
HeapTupleHeader t_data
Definition: htup.h:68
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:467
struct ExprEvalStep::@49::@89 agg_plain_pergroup_nullcheck
static pg_attribute_always_inline Datum ExecJustVarVirtImpl(ExprState *state, TupleTableSlot *slot, bool *isnull)
Oid element_type
Definition: execExpr.h:562
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:918
void ExecEvalMinMax(ExprState *state, ExprEvalStep *op)
struct ExprEvalStep::@49::@88 agg_strict_input_check
static void CheckVarSlotCompatibility(TupleTableSlot *slot, int attnum, Oid vartype)
ParamFetchHook paramFetch
Definition: params.h:112
void pfree(void *pointer)
Definition: mcxt.c:1169
ExprEvalOp ExecEvalStepOp(ExprState *state, ExprEvalStep *op)
MinMaxOp
Definition: primnodes.h:1139
char typalign
Definition: pg_type.h:176
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
static Datum ExecJustConst(ExprState *state, ExprContext *econtext, bool *isnull)
struct ExprEvalStep::@49::@85 window_func
bool inclause
Definition: execExpr.h:577
#define ERROR
Definition: elog.h:46
int errdomainconstraint(Oid datatypeOid, const char *conname)
Definition: domains.c:384
bool resnull
Definition: execnodes.h:72
#define DatumGetCString(X)
Definition: postgres.h:610
void pgstat_init_function_usage(FunctionCallInfo fcinfo, PgStat_FunctionCallUsage *fcu)
Definition: pgstat.c:1908
union ExprEvalStep::@49 d
Oid elemtype
Definition: array.h:90
#define lfirst_int(lc)
Definition: pg_list.h:170
void ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
Definition: nodeSubplan.c:1058
bool isnull
Definition: params.h:150
Oid vartype
Definition: primnodes.h:193
Datum caseValue_datum
Definition: execnodes.h:251
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
text * xmltotext_with_xmloption(xmltype *data, XmlOptionType xmloption_arg)
Definition: xml.c:615
int32 tdtypmod
Definition: tupdesc.h:83
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
#define ARR_DIMS(a)
Definition: array.h:287
struct ExprEvalStep::@49::@61 param
TimeTzADT * GetSQLCurrentTime(int32 typmod)
Definition: date.c:334
ItemPointerData t_self
Definition: htup.h:65
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:354
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2082
struct ExprEvalStep::@49::@55 constval
static TupleDesc get_cached_rowtype(Oid type_id, int32 typmod, ExprEvalRowtypeCache *rowcache, bool *changed)
struct ExprEvalStep::@49::@90 agg_trans
uint32 t_len
Definition: htup.h:64
#define EEO_NEXT()
#define ARR_DATA_PTR(a)
Definition: array.h:315
void ExecEvalRowNull(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
int resultnum
Definition: execExpr.h:320
struct ExprEvalStep::@49::@79 convert_rowtype
TupleConversionMap * convert_tuples_by_name(TupleDesc indesc, TupleDesc outdesc)
Definition: tupconvert.c:102
Tuplesortstate ** sortstates
Definition: nodeAgg.h:154
ExprStateEvalFunc evalfunc
Definition: execnodes.h:91
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:172
static void ExecEvalRowNullInt(ExprState *state, ExprEvalStep *op, ExprContext *econtext, bool checkisnull)
static char * buf
Definition: pg_test_fsync.c:68
Datum current_user(PG_FUNCTION_ARGS)
Definition: name.c:263
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:202
HeapTuple toast_build_flattened_tuple(TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptoast.c:563
bool * tts_isnull
Definition: tuptable.h:128
void ExecEvalSysVar(ExprState *state, ExprEvalStep *op, ExprContext *econtext, TupleTableSlot *slot)
static Datum ExecJustInnerVar(ExprState *state, ExprContext *econtext, bool *isnull)
void check_stack_depth(void)
Definition: postgres.c:3469
void ExecAggInitGroup(AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroup, ExprContext *aggcontext)
#define MAXDIM
Definition: array.h:75
int errdetail(const char *fmt,...)
Definition: elog.c:1042
xmltype * xmlroot(xmltype *data, text *version, int standalone)
Definition: xml.c:822
#define DatumGetBool(X)
Definition: postgres.h:437
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:228
void ExecEvalGroupingFunc(ExprState *state, ExprEvalStep *op)
ParamExecData * ecxt_param_exec_vals
Definition: execnodes.h:237
struct EState * ecxt_estate
Definition: execnodes.h:262
static Datum ExecJustAssignOuterVar(ExprState *state, ExprContext *econtext, bool *isnull)
static RangeTblEntry * exec_rt_fetch(Index rti, EState *estate)
Definition: executor.h:571
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
unsigned int uint32
Definition: c.h:441
Oid t_tableOid
Definition: htup.h:66
#define ARR_HASNULL(a)
Definition: array.h:284
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1697
HeapTuple execute_attr_map_tuple(HeapTuple tuple, TupleConversionMap *map)
Definition: tupconvert.c:139
#define EEO_DISPATCH()
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:176
Datum value
Definition: postgres.h:422
Bitmapset * grouped_cols
Definition: execnodes.h:2300
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:131
#define TimestampGetDatum(X)
Definition: timestamp.h:31
static pg_attribute_always_inline Datum ExecJustAssignVarVirtImpl(ExprState *state, TupleTableSlot *inslot, bool *isnull)
void * evalfunc_private
Definition: execnodes.h:97
Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc)
Definition: heaptuple.c:984
static TupleDesc expanded_record_get_tupdesc(ExpandedRecordHeader *erh)
struct ExprEvalStep::@49::@76 sbsref_subscript
TimeADT GetSQLLocalTime(int32 typmod)
Definition: date.c:354
List * lappend(List *list, void *datum)
Definition: list.c:336
struct ExprEvalStep::@49::@78 domaincheck
static Datum ExecJustApplyFuncToCase(ExprState *state, ExprContext *econtext, bool *isnull)
static chr element(struct vars *v, const chr *startp, const chr *endp)
Definition: regc_locale.c:376
bool xml_is_document(xmltype *arg)
Definition: xml.c:888
static Datum ExecJustAssignOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
struct ExprEvalStep::@49::@84 grouping_func
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
Index varno
Definition: primnodes.h:189
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
void ArrayCheckBounds(int ndim, const int *dims, const int *lb)
Definition: arrayutils.c:128
bool domainValue_isNull
Definition: execnodes.h:259
XmlExprOp op
Definition: primnodes.h:1227
Datum session_user(PG_FUNCTION_ARGS)
Definition: name.c:269
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
struct ExprEvalStep::@49::@69 arraycoerce
ExprContext * curaggcontext
Definition: execnodes.h:2291
bool * ecxt_aggnulls
Definition: execnodes.h:247
uint8 bits8
Definition: c.h:448
struct ExprEvalStep::@49::@53 assign_var
struct ExprEvalStep::@49::@75 fieldstore
#define TTS_IS_VIRTUAL(slot)
Definition: tuptable.h:228
uintptr_t Datum
Definition: postgres.h:411
TupleTableSlot * ExecFilterJunk(JunkFilter *junkfilter, TupleTableSlot *slot)
Definition: execJunk.c:247
#define ER_MAGIC
void ExecEvalXmlExpr(ExprState *state, ExprEvalStep *op)
#define HeapTupleHeaderSetTypMod(tup, typmod)
Definition: htup_details.h:472
struct ScalarArrayOpExprHashTable ScalarArrayOpExprHashTable
void ExecEvalWholeRowVar(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
#define DatumGetArrayTypePCopy(X)
Definition: array.h:255
#define TimeADTGetDatum(X)
Definition: date.h:58
int errdatatype(Oid datatypeOid)
Definition: domains.c:360
struct ExprEvalStep::@49::@63 casetest
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:457
struct ExprEvalStep::@49::@56 func
Timestamp GetSQLLocalTimestamp(int32 typmod)
Definition: timestamp.c:1612
struct ExprEvalStep::@49::@64 make_readonly
#define BoolGetDatum(X)
Definition: postgres.h:446
void ExecEvalParamExtern(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
void DeleteExpandedObject(Datum d)
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:339
#define InvalidOid
Definition: postgres_ext.h:36
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:381
int16 attnum
Definition: pg_attribute.h:83
List * named_args
Definition: primnodes.h:1229
static struct @143 value
#define ereport(elevel,...)
Definition: elog.h:157
RowCompareType
Definition: primnodes.h:1102
static Datum ExecJustScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
#define INNER_VAR
Definition: primnodes.h:175
List * args
Definition: primnodes.h:1231
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
Datum array_map(Datum arrayd, ExprState *exprstate, ExprContext *econtext, Oid retType, ArrayMapState *amstate)
Definition: arrayfuncs.c:3158
struct ExprEvalStep::@49::@82 xmlexpr
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:230
static Datum ExecJustAssignScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
struct ExprEvalStep::@49::@81 hashedscalararrayop
Definition: regguts.h:317
intptr_t opcode
Definition: execExpr.h:270
static Datum ExecJustAssignScanVar(ExprState *state, ExprContext *econtext, bool *isnull)
#define DatumIsReadWriteExpandedObject(d, isnull, typlen)
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:384
XmlOptionType xmloption
Definition: primnodes.h:1232
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static void ExecInitInterpreter(void)
struct ExprEvalStep::@49::@60 nulltest_row
static int list_length(const List *l)
Definition: pg_list.h:149
#define newval
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1134
void ExecEvalArrayCoerce(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:226
Index es_range_table_size
Definition: execnodes.h:561
void ExecEvalFuncExprStrictFusage(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
uint64 tupDesc_identifier
Definition: typcache.h:90
struct ExprEvalStep::@49::@77 sbsref
static pg_attribute_always_inline Datum ExecJustVarImpl(ExprState *state, TupleTableSlot *slot, bool *isnull)
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
struct ExprEvalStep::@49::@66 sqlvaluefunction
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:233
#define ARR_NDIM(a)
Definition: array.h:283
Datum * argvalue
Definition: execExpr.h:597
#define InvalidAttrNumber
Definition: attnum.h:23
static Datum ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
void ExecEvalAggOrderedTransDatum(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
struct ExprEvalStep::@49::@59 jump
struct ExprEvalStep::@49::@70 row
#define DatumGetPointer(X)
Definition: postgres.h:593
void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu, bool finalize)
Definition: pgstat.c:1979
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1249
void ExecEvalFieldStoreDeForm(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
static Datum values[MAXATTR]
Definition: bootstrap.c:166
struct ExprEvalStep::@49::@68 arrayexpr
Oid tdtypeid
Definition: tupdesc.h:82
void ExecEvalConstraintCheck(ExprState *state, ExprEvalStep *op)
#define Int32GetDatum(X)
Definition: postgres.h:523
e
Definition: preproc-init.c:82
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:172
static Datum ExecJustOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
bool caseValue_isNull
Definition: execnodes.h:253
struct TupleDescData * TupleDesc
Definition: tupdesc.h:89
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define fetch_att(T, attbyval, attlen)
Definition: tupmacs.h:75
bool * argnull
Definition: execExpr.h:598
int steps_len
Definition: execnodes.h:104
void ExecEvalRowNotNull(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
void ExecEvalConstraintNotNull(ExprState *state, ExprEvalStep *op)
#define elog(elevel,...)
Definition: elog.h:232
static Datum ExecJustAssignInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
Datum ExecSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:62
int i
xmltype * xmlelement(XmlExpr *xexpr, Datum *named_argvalue, bool *named_argnull, Datum *argvalue, bool *argnull)
Definition: xml.c:628
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:84
Datum value
Definition: params.h:149
static Datum ExecJustAssignInnerVar(ExprState *state, ExprContext *econtext, bool *isnull)
uint8 flags
Definition: execnodes.h:65
static pg_attribute_always_inline void ExecAggPlainTransByRef(AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroup, ExprContext *aggcontext, int setno)
#define pg_attribute_always_inline
Definition: c.h:196
void * arg
AggStatePerTrans curpertrans
Definition: execnodes.h:2294
bool isnull
Definition: execExpr.h:337
#define unlikely(x)
Definition: c.h:273
int16 typlen
Definition: execExpr.h:564
SQLValueFunction * svf
Definition: execExpr.h:420
Definition: c.h:621
void ExecEvalSubPlan(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
struct ScalarArrayOpExprHashTable * elements_tab
Definition: execExpr.h:578
void ExecEvalFuncExprFusage(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
struct ExprEvalStep::@49::@54 assign_tmp
AttrNumber tts_nvalid
Definition: tuptable.h:121
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
struct ExprEvalStep::@49::@65 iocoerce
struct ExprEvalStep::@49::@87 agg_deserialize
TupleTableSlot * sortslot
Definition: nodeAgg.h:136
Alias * eref
Definition: parsenodes.h:1141
#define qsort(a, b, c, d)
Definition: port.h:504
void CheckExprStillValid(ExprState *state, ExprContext *econtext)
#define EEO_OPCODE(opcode)
static Datum slot_getsysattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:402
void ExecEvalCurrentOfExpr(ExprState *state, ExprEvalStep *op)
static void CheckOpSlotCompatibility(ExprEvalStep *op, TupleTableSlot *slot)
ArrayType * construct_md_array(Datum *elems, bool *nulls, int ndims, int *dims, int *lbs, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3353
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Definition: xml.c:2134
ParamListInfo ecxt_param_list_info
Definition: execnodes.h:238
Definition: pg_list.h:50
void ExecEvalNextValueExpr(ExprState *state, ExprEvalStep *op)
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:719
bool isnull
Definition: params.h:93
#define EEO_CASE(name)
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
TupleDesc tupDesc
Definition: typcache.h:89
#define ARR_ELEMTYPE(a)
Definition: array.h:285
Datum resvalue
Definition: execnodes.h:74
#define EEO_FLAG_INTERPRETER_INITIALIZED
Definition: execExpr.h:27
int16 AttrNumber
Definition: attnum.h:21
#define ARR_NULLBITMAP(a)
Definition: array.h:293
#define OUTER_VAR
Definition: primnodes.h:176
void ExecTypeSetColNames(TupleDesc typeInfo, List *namesList)
Definition: execTuples.c:2036
AttrNumber fieldnum
Definition: execExpr.h:496
#define TYPECACHE_TUPDESC
Definition: typcache.h:144
void ExecEvalAggOrderedTransTuple(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
struct ExprEvalStep::@49::@73 minmax
bool typbyval
Definition: execExpr.h:565
void ExecEvalFieldSelect(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
xmltype * xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
Definition: xml.c:752
int ndim
Definition: array.h:88
#define EEO_FLAG_DIRECT_THREADED
Definition: execExpr.h:29
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1552
void tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot)
Definition: tuplesort.c:1684
void ExecEvalSQLValueFunction(ExprState *state, ExprEvalStep *op)
AggStatePerGroup * all_pergroups
Definition: execnodes.h:2345
void ExecEvalArrayExpr(ExprState *state, ExprEvalStep *op)
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:451
#define DatumGetArrayTypeP(X)
Definition: array.h:254