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