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