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