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