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