PostgreSQL Source Code  git master
execExprInterp.c File Reference
#include "postgres.h"
#include "access/heaptoast.h"
#include "catalog/pg_type.h"
#include "commands/sequence.h"
#include "executor/execExpr.h"
#include "executor/nodeSubplan.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "parser/parsetree.h"
#include "pgstat.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datum.h"
#include "utils/expandedrecord.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/timestamp.h"
#include "utils/typcache.h"
#include "utils/xml.h"
#include "lib/simplehash.h"
Include dependency graph for execExprInterp.c:

Go to the source code of this file.

Data Structures

struct  ScalarArrayOpExprHashEntry
 
struct  ScalarArrayOpExprHashTable
 

Macros

#define EEO_SWITCH()   starteval: switch ((ExprEvalOp) op->opcode)
 
#define EEO_CASE(name)   case name:
 
#define EEO_DISPATCH()   goto starteval
 
#define EEO_OPCODE(opcode)   (opcode)
 
#define EEO_NEXT()
 
#define EEO_JUMP(stepno)
 
#define SH_PREFIX   saophash
 
#define SH_ELEMENT_TYPE   ScalarArrayOpExprHashEntry
 
#define SH_KEY_TYPE   Datum
 
#define SH_SCOPE   static inline
 
#define SH_DECLARE
 
#define SH_PREFIX   saophash
 
#define SH_ELEMENT_TYPE   ScalarArrayOpExprHashEntry
 
#define SH_KEY_TYPE   Datum
 
#define SH_KEY   key
 
#define SH_HASH_KEY(tb, key)   saop_element_hash(tb, key)
 
#define SH_EQUAL(tb, a, b)   saop_hash_element_match(tb, a, b)
 
#define SH_SCOPE   static inline
 
#define SH_STORE_HASH
 
#define SH_GET_HASH(tb, a)   a->hash
 
#define SH_DEFINE
 

Typedefs

typedef struct ScalarArrayOpExprHashEntry ScalarArrayOpExprHashEntry
 
typedef struct ScalarArrayOpExprHashTable ScalarArrayOpExprHashTable
 

Functions

static Datum ExecInterpExpr (ExprState *state, ExprContext *econtext, bool *isnull)
 
static void ExecInitInterpreter (void)
 
static void CheckVarSlotCompatibility (TupleTableSlot *slot, int attnum, Oid vartype)
 
static void CheckOpSlotCompatibility (ExprEvalStep *op, TupleTableSlot *slot)
 
static TupleDesc get_cached_rowtype (Oid type_id, int32 typmod, ExprEvalRowtypeCache *rowcache, bool *changed)
 
static void ExecEvalRowNullInt (ExprState *state, ExprEvalStep *op, ExprContext *econtext, bool checkisnull)
 
static Datum ExecJustInnerVar (ExprState *state, ExprContext *econtext, bool *isnull)
 
static Datum ExecJustOuterVar (ExprState *state, ExprContext *econtext, bool *isnull)
 
static Datum ExecJustScanVar (ExprState *state, ExprContext *econtext, bool *isnull)
 
static Datum ExecJustAssignInnerVar (ExprState *state, ExprContext *econtext, bool *isnull)
 
static Datum ExecJustAssignOuterVar (ExprState *state, ExprContext *econtext, bool *isnull)
 
static Datum ExecJustAssignScanVar (ExprState *state, ExprContext *econtext, bool *isnull)
 
static Datum ExecJustApplyFuncToCase (ExprState *state, ExprContext *econtext, bool *isnull)
 
static Datum ExecJustConst (ExprState *state, ExprContext *econtext, bool *isnull)
 
static Datum ExecJustInnerVarVirt (ExprState *state, ExprContext *econtext, bool *isnull)
 
static Datum ExecJustOuterVarVirt (ExprState *state, ExprContext *econtext, bool *isnull)
 
static Datum ExecJustScanVarVirt (ExprState *state, ExprContext *econtext, bool *isnull)
 
static Datum ExecJustAssignInnerVarVirt (ExprState *state, ExprContext *econtext, bool *isnull)
 
static Datum ExecJustAssignOuterVarVirt (ExprState *state, ExprContext *econtext, bool *isnull)
 
static Datum ExecJustAssignScanVarVirt (ExprState *state, ExprContext *econtext, bool *isnull)
 
static pg_attribute_always_inline void ExecAggPlainTransByVal (AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroup, ExprContext *aggcontext, int setno)
 
static pg_attribute_always_inline void ExecAggPlainTransByRef (AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroup, ExprContext *aggcontext, int setno)
 
static bool saop_hash_element_match (struct saophash_hash *tb, Datum key1, Datum key2)
 
static uint32 saop_element_hash (struct saophash_hash *tb, Datum key)
 
void ExecReadyInterpretedExpr (ExprState *state)
 
Datum ExecInterpExprStillValid (ExprState *state, ExprContext *econtext, bool *isNull)
 
void CheckExprStillValid (ExprState *state, ExprContext *econtext)
 
static pg_attribute_always_inline Datum ExecJustVarImpl (ExprState *state, TupleTableSlot *slot, bool *isnull)
 
static pg_attribute_always_inline Datum ExecJustAssignVarImpl (ExprState *state, TupleTableSlot *inslot, bool *isnull)
 
static pg_attribute_always_inline Datum ExecJustVarVirtImpl (ExprState *state, TupleTableSlot *slot, bool *isnull)
 
static pg_attribute_always_inline Datum ExecJustAssignVarVirtImpl (ExprState *state, TupleTableSlot *inslot, bool *isnull)
 
ExprEvalOp ExecEvalStepOp (ExprState *state, ExprEvalStep *op)
 
void ExecEvalFuncExprFusage (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
void ExecEvalFuncExprStrictFusage (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
void ExecEvalParamExec (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
void ExecEvalParamExtern (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
void ExecEvalSQLValueFunction (ExprState *state, ExprEvalStep *op)
 
void ExecEvalCurrentOfExpr (ExprState *state, ExprEvalStep *op)
 
void ExecEvalNextValueExpr (ExprState *state, ExprEvalStep *op)
 
void ExecEvalRowNull (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
void ExecEvalRowNotNull (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
void ExecEvalArrayExpr (ExprState *state, ExprEvalStep *op)
 
void ExecEvalArrayCoerce (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
void ExecEvalRow (ExprState *state, ExprEvalStep *op)
 
void ExecEvalMinMax (ExprState *state, ExprEvalStep *op)
 
void ExecEvalFieldSelect (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
void ExecEvalFieldStoreDeForm (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
void ExecEvalFieldStoreForm (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
void ExecEvalConvertRowtype (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
void ExecEvalScalarArrayOp (ExprState *state, ExprEvalStep *op)
 
void ExecEvalHashedScalarArrayOp (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
void ExecEvalConstraintNotNull (ExprState *state, ExprEvalStep *op)
 
void ExecEvalConstraintCheck (ExprState *state, ExprEvalStep *op)
 
void ExecEvalXmlExpr (ExprState *state, ExprEvalStep *op)
 
void ExecEvalGroupingFunc (ExprState *state, ExprEvalStep *op)
 
void ExecEvalSubPlan (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
void ExecEvalWholeRowVar (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
void ExecEvalSysVar (ExprState *state, ExprEvalStep *op, ExprContext *econtext, TupleTableSlot *slot)
 
void ExecAggInitGroup (AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroup, ExprContext *aggcontext)
 
Datum ExecAggTransReparent (AggState *aggstate, AggStatePerTrans pertrans, Datum newValue, bool newValueIsNull, Datum oldValue, bool oldValueIsNull)
 
void ExecEvalAggOrderedTransDatum (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
void ExecEvalAggOrderedTransTuple (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 

Macro Definition Documentation

◆ EEO_CASE

#define EEO_CASE (   name)    case name:

Definition at line 122 of file execExprInterp.c.

Referenced by ExecInterpExpr().

◆ EEO_DISPATCH

#define EEO_DISPATCH ( )    goto starteval

Definition at line 123 of file execExprInterp.c.

Referenced by ExecInterpExpr().

◆ EEO_JUMP

#define EEO_JUMP (   stepno)
Value:
do { \
op = &state->steps[stepno]; \
EEO_DISPATCH(); \
} while (0)
Definition: regguts.h:317

Definition at line 134 of file execExprInterp.c.

Referenced by ExecInterpExpr().

◆ EEO_NEXT

#define EEO_NEXT ( )
Value:
do { \
op++; \
EEO_DISPATCH(); \
} while (0)

Definition at line 128 of file execExprInterp.c.

Referenced by ExecInterpExpr().

◆ EEO_OPCODE

#define EEO_OPCODE (   opcode)    (opcode)

Definition at line 124 of file execExprInterp.c.

Referenced by ExecReadyInterpretedExpr().

◆ EEO_SWITCH

#define EEO_SWITCH ( )    starteval: switch ((ExprEvalOp) op->opcode)

Definition at line 121 of file execExprInterp.c.

Referenced by ExecInterpExpr().

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 196 of file execExprInterp.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 223 of file execExprInterp.c.

◆ SH_ELEMENT_TYPE [1/2]

#define SH_ELEMENT_TYPE   ScalarArrayOpExprHashEntry

Definition at line 215 of file execExprInterp.c.

◆ SH_ELEMENT_TYPE [2/2]

#define SH_ELEMENT_TYPE   ScalarArrayOpExprHashEntry

Definition at line 215 of file execExprInterp.c.

◆ SH_EQUAL

#define SH_EQUAL (   tb,
  a,
 
)    saop_hash_element_match(tb, a, b)

Definition at line 219 of file execExprInterp.c.

◆ SH_GET_HASH

#define SH_GET_HASH (   tb,
 
)    a->hash

Definition at line 222 of file execExprInterp.c.

◆ SH_HASH_KEY

#define SH_HASH_KEY (   tb,
  key 
)    saop_element_hash(tb, key)

Definition at line 218 of file execExprInterp.c.

◆ SH_KEY

#define SH_KEY   key

Definition at line 217 of file execExprInterp.c.

◆ SH_KEY_TYPE [1/2]

#define SH_KEY_TYPE   Datum

Definition at line 216 of file execExprInterp.c.

◆ SH_KEY_TYPE [2/2]

#define SH_KEY_TYPE   Datum

Definition at line 216 of file execExprInterp.c.

◆ SH_PREFIX [1/2]

#define SH_PREFIX   saophash

Definition at line 214 of file execExprInterp.c.

◆ SH_PREFIX [2/2]

#define SH_PREFIX   saophash

Definition at line 214 of file execExprInterp.c.

◆ SH_SCOPE [1/2]

#define SH_SCOPE   static inline

Definition at line 220 of file execExprInterp.c.

◆ SH_SCOPE [2/2]

#define SH_SCOPE   static inline

Definition at line 220 of file execExprInterp.c.

◆ SH_STORE_HASH

#define SH_STORE_HASH

Definition at line 221 of file execExprInterp.c.

Typedef Documentation

◆ ScalarArrayOpExprHashEntry

◆ ScalarArrayOpExprHashTable

Function Documentation

◆ CheckExprStillValid()

void CheckExprStillValid ( ExprState state,
ExprContext econtext 
)

Definition at line 1827 of file execExprInterp.c.

References attnum, CheckVarSlotCompatibility(), ExprEvalStep::d, ExprContext::ecxt_innertuple, ExprContext::ecxt_outertuple, ExprContext::ecxt_scantuple, EEOP_INNER_VAR, EEOP_OUTER_VAR, EEOP_SCAN_VAR, ExecEvalStepOp(), i, ExprEvalStep::op, ExprState::steps, ExprState::steps_len, and ExprEvalStep::var.

Referenced by ExecInterpExprStillValid(), and ExecRunCompiledExpr().

1828 {
1829  TupleTableSlot *innerslot;
1830  TupleTableSlot *outerslot;
1831  TupleTableSlot *scanslot;
1832 
1833  innerslot = econtext->ecxt_innertuple;
1834  outerslot = econtext->ecxt_outertuple;
1835  scanslot = econtext->ecxt_scantuple;
1836 
1837  for (int i = 0; i < state->steps_len; i++)
1838  {
1839  ExprEvalStep *op = &state->steps[i];
1840 
1841  switch (ExecEvalStepOp(state, op))
1842  {
1843  case EEOP_INNER_VAR:
1844  {
1845  int attnum = op->d.var.attnum;
1846 
1847  CheckVarSlotCompatibility(innerslot, attnum + 1, op->d.var.vartype);
1848  break;
1849  }
1850 
1851  case EEOP_OUTER_VAR:
1852  {
1853  int attnum = op->d.var.attnum;
1854 
1855  CheckVarSlotCompatibility(outerslot, attnum + 1, op->d.var.vartype);
1856  break;
1857  }
1858 
1859  case EEOP_SCAN_VAR:
1860  {
1861  int attnum = op->d.var.attnum;
1862 
1863  CheckVarSlotCompatibility(scanslot, attnum + 1, op->d.var.vartype);
1864  break;
1865  }
1866  default:
1867  break;
1868  }
1869  }
1870 }
struct ExprEvalStep * steps
Definition: execnodes.h:85
struct ExprEvalStep::@49::@51 var
static void CheckVarSlotCompatibility(TupleTableSlot *slot, int attnum, Oid vartype)
ExprEvalOp ExecEvalStepOp(ExprState *state, ExprEvalStep *op)
union ExprEvalStep::@49 d
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:228
int16 attnum
Definition: pg_attribute.h:83
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:230
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:226
int steps_len
Definition: execnodes.h:104
int i

◆ CheckOpSlotCompatibility()

static void CheckOpSlotCompatibility ( ExprEvalStep op,
TupleTableSlot slot 
)
static

Definition at line 1929 of file execExprInterp.c.

References Assert, ExprEvalStep::d, ExprEvalStep::fetch, TupleTableSlot::tts_ops, TTSOpsBufferHeapTuple, TTSOpsHeapTuple, and TTSOpsVirtual.

Referenced by ExecInterpExpr(), ExecJustAssignVarImpl(), and ExecJustVarImpl().

1930 {
1931 #ifdef USE_ASSERT_CHECKING
1932  /* there's nothing to check */
1933  if (!op->d.fetch.fixed)
1934  return;
1935 
1936  /*
1937  * Should probably fixed at some point, but for now it's easier to allow
1938  * buffer and heap tuples to be used interchangeably.
1939  */
1940  if (slot->tts_ops == &TTSOpsBufferHeapTuple &&
1941  op->d.fetch.kind == &TTSOpsHeapTuple)
1942  return;
1943  if (slot->tts_ops == &TTSOpsHeapTuple &&
1944  op->d.fetch.kind == &TTSOpsBufferHeapTuple)
1945  return;
1946 
1947  /*
1948  * At the moment we consider it OK if a virtual slot is used instead of a
1949  * specific type of slot, as a virtual slot never needs to be deformed.
1950  */
1951  if (slot->tts_ops == &TTSOpsVirtual)
1952  return;
1953 
1954  Assert(op->d.fetch.kind == slot->tts_ops);
1955 #endif
1956 }
struct ExprEvalStep::@49::@50 fetch
const TupleTableSlotOps TTSOpsBufferHeapTuple
Definition: execTuples.c:86
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
const TupleTableSlotOps *const tts_ops
Definition: tuptable.h:122
union ExprEvalStep::@49 d
#define Assert(condition)
Definition: c.h:804
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:84

◆ CheckVarSlotCompatibility()

static void CheckVarSlotCompatibility ( TupleTableSlot slot,
int  attnum,
Oid  vartype 
)
static

Definition at line 1878 of file execExprInterp.c.

References elog, ereport, errcode(), errdetail(), errmsg(), ERROR, format_type_be(), TupleDescData::natts, TupleDescData::tdtypeid, TupleTableSlot::tts_tupleDescriptor, and TupleDescAttr.

Referenced by CheckExprStillValid().

1879 {
1880  /*
1881  * What we have to check for here is the possibility of an attribute
1882  * having been dropped or changed in type since the plan tree was created.
1883  * Ideally the plan will get invalidated and not re-used, but just in
1884  * case, we keep these defenses. Fortunately it's sufficient to check
1885  * once on the first time through.
1886  *
1887  * Note: ideally we'd check typmod as well as typid, but that seems
1888  * impractical at the moment: in many cases the tupdesc will have been
1889  * generated by ExecTypeFromTL(), and that can't guarantee to generate an
1890  * accurate typmod in all cases, because some expression node types don't
1891  * carry typmod. Fortunately, for precisely that reason, there should be
1892  * no places with a critical dependency on the typmod of a value.
1893  *
1894  * System attributes don't require checking since their types never
1895  * change.
1896  */
1897  if (attnum > 0)
1898  {
1899  TupleDesc slot_tupdesc = slot->tts_tupleDescriptor;
1900  Form_pg_attribute attr;
1901 
1902  if (attnum > slot_tupdesc->natts) /* should never happen */
1903  elog(ERROR, "attribute number %d exceeds number of columns %d",
1904  attnum, slot_tupdesc->natts);
1905 
1906  attr = TupleDescAttr(slot_tupdesc, attnum - 1);
1907 
1908  if (attr->attisdropped)
1909  ereport(ERROR,
1910  (errcode(ERRCODE_UNDEFINED_COLUMN),
1911  errmsg("attribute %d of type %s has been dropped",
1912  attnum, format_type_be(slot_tupdesc->tdtypeid))));
1913 
1914  if (vartype != attr->atttypid)
1915  ereport(ERROR,
1916  (errcode(ERRCODE_DATATYPE_MISMATCH),
1917  errmsg("attribute %d of type %s has wrong type",
1918  attnum, format_type_be(slot_tupdesc->tdtypeid)),
1919  errdetail("Table has type %s, but query expects %s.",
1920  format_type_be(attr->atttypid),
1921  format_type_be(vartype))));
1922  }
1923 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int errcode(int sqlerrcode)
Definition: elog.c:698
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
#define ERROR
Definition: elog.h:46
int errdetail(const char *fmt,...)
Definition: elog.c:1042
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:203
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
int16 attnum
Definition: pg_attribute.h:83
#define ereport(elevel,...)
Definition: elog.h:157
Oid tdtypeid
Definition: tupdesc.h:82
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232

◆ ExecAggInitGroup()

void ExecAggInitGroup ( AggState aggstate,
AggStatePerTrans  pertrans,
AggStatePerGroup  pergroup,
ExprContext aggcontext 
)

Definition at line 4179 of file execExprInterp.c.

References FunctionCallInfoBaseData::args, datumCopy(), ExprContext::ecxt_per_tuple_memory, MemoryContextSwitchTo(), AggStatePerGroupData::noTransValue, AggStatePerTransData::transfn_fcinfo, AggStatePerTransData::transtypeByVal, AggStatePerTransData::transtypeLen, AggStatePerGroupData::transValue, AggStatePerGroupData::transValueIsNull, and NullableDatum::value.

Referenced by ExecInterpExpr().

4181 {
4182  FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
4183  MemoryContext oldContext;
4184 
4185  /*
4186  * We must copy the datum into aggcontext if it is pass-by-ref. We do not
4187  * need to pfree the old transValue, since it's NULL. (We already checked
4188  * that the agg's input type is binary-compatible with its transtype, so
4189  * straight copy here is OK.)
4190  */
4191  oldContext = MemoryContextSwitchTo(aggcontext->ecxt_per_tuple_memory);
4192  pergroup->transValue = datumCopy(fcinfo->args[1].value,
4193  pertrans->transtypeByVal,
4194  pertrans->transtypeLen);
4195  pergroup->transValueIsNull = false;
4196  pergroup->noTransValue = false;
4197  MemoryContextSwitchTo(oldContext);
4198 }
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:234
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
FunctionCallInfo transfn_fcinfo
Definition: nodeAgg.h:162
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
Datum value
Definition: postgres.h:422
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:131

◆ ExecAggPlainTransByRef()

static pg_attribute_always_inline void ExecAggPlainTransByRef ( AggState aggstate,
AggStatePerTrans  pertrans,
AggStatePerGroup  pergroup,
ExprContext aggcontext,
int  setno 
)
static

Definition at line 4313 of file execExprInterp.c.

References ExprEvalStep::aggcontext, FunctionCallInfoBaseData::args, AggState::curaggcontext, AggState::curpertrans, AggState::current_set, DatumGetPointer, ExprContext::ecxt_per_tuple_memory, ExecAggTransReparent(), FunctionCallInvoke, FunctionCallInfoBaseData::isnull, NullableDatum::isnull, MemoryContextSwitchTo(), ExprEvalStep::pertrans, ExprEvalStep::setno, AggState::tmpcontext, AggStatePerTransData::transfn_fcinfo, AggStatePerGroupData::transValue, AggStatePerGroupData::transValueIsNull, and NullableDatum::value.

Referenced by ExecInterpExpr().

4316 {
4317  FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
4318  MemoryContext oldContext;
4319  Datum newVal;
4320 
4321  /* cf. select_current_set() */
4322  aggstate->curaggcontext = aggcontext;
4323  aggstate->current_set = setno;
4324 
4325  /* set up aggstate->curpertrans for AggGetAggref() */
4326  aggstate->curpertrans = pertrans;
4327 
4328  /* invoke transition function in per-tuple context */
4329  oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
4330 
4331  fcinfo->args[0].value = pergroup->transValue;
4332  fcinfo->args[0].isnull = pergroup->transValueIsNull;
4333  fcinfo->isnull = false; /* just in case transfn doesn't set it */
4334 
4335  newVal = FunctionCallInvoke(fcinfo);
4336 
4337  /*
4338  * For pass-by-ref datatype, must copy the new value into aggcontext and
4339  * free the prior transValue. But if transfn returned a pointer to its
4340  * first input, we don't need to do anything. Also, if transfn returned a
4341  * pointer to a R/W expanded object that is already a child of the
4342  * aggcontext, assume we can adopt that value without copying it.
4343  *
4344  * It's safe to compare newVal with pergroup->transValue without regard
4345  * for either being NULL, because ExecAggTransReparent() takes care to set
4346  * transValue to 0 when NULL. Otherwise we could end up accidentally not
4347  * reparenting, when the transValue has the same numerical value as
4348  * newValue, despite being NULL. This is a somewhat hot path, making it
4349  * undesirable to instead solve this with another branch for the common
4350  * case of the transition function returning its (modified) input
4351  * argument.
4352  */
4353  if (DatumGetPointer(newVal) != DatumGetPointer(pergroup->transValue))
4354  newVal = ExecAggTransReparent(aggstate, pertrans,
4355  newVal, fcinfo->isnull,
4356  pergroup->transValue,
4357  pergroup->transValueIsNull);
4358 
4359  pergroup->transValue = newVal;
4360  pergroup->transValueIsNull = fcinfo->isnull;
4361 
4362  MemoryContextSwitchTo(oldContext);
4363 }
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:234
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int current_set
Definition: execnodes.h:2294
FunctionCallInfo transfn_fcinfo
Definition: nodeAgg.h:162
Datum ExecAggTransReparent(AggState *aggstate, AggStatePerTrans pertrans, Datum newValue, bool newValueIsNull, Datum oldValue, bool oldValueIsNull)
ExprContext * tmpcontext
Definition: execnodes.h:2284
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:172
Datum value
Definition: postgres.h:422
ExprContext * curaggcontext
Definition: execnodes.h:2286
uintptr_t Datum
Definition: postgres.h:411
#define DatumGetPointer(X)
Definition: postgres.h:593
AggStatePerTrans curpertrans
Definition: execnodes.h:2289

◆ ExecAggPlainTransByVal()

static pg_attribute_always_inline void ExecAggPlainTransByVal ( AggState aggstate,
AggStatePerTrans  pertrans,
AggStatePerGroup  pergroup,
ExprContext aggcontext,
int  setno 
)
static

Definition at line 4281 of file execExprInterp.c.

References ExprEvalStep::aggcontext, FunctionCallInfoBaseData::args, AggState::curaggcontext, AggState::curpertrans, AggState::current_set, ExprContext::ecxt_per_tuple_memory, FunctionCallInvoke, FunctionCallInfoBaseData::isnull, NullableDatum::isnull, MemoryContextSwitchTo(), ExprEvalStep::pertrans, pg_attribute_always_inline, ExprEvalStep::setno, AggState::tmpcontext, AggStatePerTransData::transfn_fcinfo, AggStatePerGroupData::transValue, AggStatePerGroupData::transValueIsNull, and NullableDatum::value.

Referenced by ExecInterpExpr().

4284 {
4285  FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
4286  MemoryContext oldContext;
4287  Datum newVal;
4288 
4289  /* cf. select_current_set() */
4290  aggstate->curaggcontext = aggcontext;
4291  aggstate->current_set = setno;
4292 
4293  /* set up aggstate->curpertrans for AggGetAggref() */
4294  aggstate->curpertrans = pertrans;
4295 
4296  /* invoke transition function in per-tuple context */
4297  oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
4298 
4299  fcinfo->args[0].value = pergroup->transValue;
4300  fcinfo->args[0].isnull = pergroup->transValueIsNull;
4301  fcinfo->isnull = false; /* just in case transfn doesn't set it */
4302 
4303  newVal = FunctionCallInvoke(fcinfo);
4304 
4305  pergroup->transValue = newVal;
4306  pergroup->transValueIsNull = fcinfo->isnull;
4307 
4308  MemoryContextSwitchTo(oldContext);
4309 }
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:234
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int current_set
Definition: execnodes.h:2294
FunctionCallInfo transfn_fcinfo
Definition: nodeAgg.h:162
ExprContext * tmpcontext
Definition: execnodes.h:2284
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:172
Datum value
Definition: postgres.h:422
ExprContext * curaggcontext
Definition: execnodes.h:2286
uintptr_t Datum
Definition: postgres.h:411
AggStatePerTrans curpertrans
Definition: execnodes.h:2289

◆ ExecAggTransReparent()

Datum ExecAggTransReparent ( AggState aggstate,
AggStatePerTrans  pertrans,
Datum  newValue,
bool  newValueIsNull,
Datum  oldValue,
bool  oldValueIsNull 
)

Definition at line 4207 of file execExprInterp.c.

References Assert, AggState::curaggcontext, CurrentMemoryContext, datumCopy(), DatumGetEOHP(), DatumGetPointer, DatumIsReadWriteExpandedObject, DeleteExpandedObject(), ExprContext::ecxt_per_tuple_memory, MemoryContextGetParent(), MemoryContextSwitchTo(), pfree(), AggStatePerTransData::transtypeByVal, and AggStatePerTransData::transtypeLen.

Referenced by advance_transition_function(), and ExecAggPlainTransByRef().

4210 {
4211  Assert(newValue != oldValue);
4212 
4213  if (!newValueIsNull)
4214  {
4216  if (DatumIsReadWriteExpandedObject(newValue,
4217  false,
4218  pertrans->transtypeLen) &&
4219  MemoryContextGetParent(DatumGetEOHP(newValue)->eoh_context) == CurrentMemoryContext)
4220  /* do nothing */ ;
4221  else
4222  newValue = datumCopy(newValue,
4223  pertrans->transtypeByVal,
4224  pertrans->transtypeLen);
4225  }
4226  else
4227  {
4228  /*
4229  * Ensure that AggStatePerGroup->transValue ends up being 0, so
4230  * callers can safely compare newValue/oldValue without having to
4231  * check their respective nullness.
4232  */
4233  newValue = (Datum) 0;
4234  }
4235 
4236  if (!oldValueIsNull)
4237  {
4238  if (DatumIsReadWriteExpandedObject(oldValue,
4239  false,
4240  pertrans->transtypeLen))
4241  DeleteExpandedObject(oldValue);
4242  else
4243  pfree(DatumGetPointer(oldValue));
4244  }
4245 
4246  return newValue;
4247 }
MemoryContext MemoryContextGetParent(MemoryContext context)
Definition: mcxt.c:446
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:234
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void pfree(void *pointer)
Definition: mcxt.c:1169
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:131
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
ExprContext * curaggcontext
Definition: execnodes.h:2286
uintptr_t Datum
Definition: postgres.h:411
void DeleteExpandedObject(Datum d)
#define Assert(condition)
Definition: c.h:804
#define DatumIsReadWriteExpandedObject(d, isnull, typlen)
#define DatumGetPointer(X)
Definition: postgres.h:593

◆ ExecEvalAggOrderedTransDatum()

void ExecEvalAggOrderedTransDatum ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 4253 of file execExprInterp.c.

References ExprEvalStep::agg_trans, ExprEvalStep::d, ExprEvalStep::pertrans, ExprEvalStep::resnull, ExprEvalStep::resvalue, ExprEvalStep::setno, AggStatePerTransData::sortstates, and tuplesort_putdatum().

Referenced by ExecInterpExpr().

4255 {
4256  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
4257  int setno = op->d.agg_trans.setno;
4258 
4259  tuplesort_putdatum(pertrans->sortstates[setno],
4260  *op->resvalue, *op->resnull);
4261 }
void tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull)
Definition: tuplesort.c:1805
Datum * resvalue
Definition: execExpr.h:273
bool * resnull
Definition: execExpr.h:274
union ExprEvalStep::@49 d
struct ExprEvalStep::@49::@90 agg_trans
Tuplesortstate ** sortstates
Definition: nodeAgg.h:154

◆ ExecEvalAggOrderedTransTuple()

void ExecEvalAggOrderedTransTuple ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 4267 of file execExprInterp.c.

References ExprEvalStep::agg_trans, ExprEvalStep::d, ExecClearTuple(), ExecStoreVirtualTuple(), AggStatePerTransData::numInputs, ExprEvalStep::pertrans, pg_attribute_always_inline, ExprEvalStep::setno, AggStatePerTransData::sortslot, AggStatePerTransData::sortstates, TupleTableSlot::tts_nvalid, and tuplesort_puttupleslot().

Referenced by ExecInterpExpr().

4269 {
4270  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
4271  int setno = op->d.agg_trans.setno;
4272 
4273  ExecClearTuple(pertrans->sortslot);
4274  pertrans->sortslot->tts_nvalid = pertrans->numInputs;
4275  ExecStoreVirtualTuple(pertrans->sortslot);
4276  tuplesort_puttupleslot(pertrans->sortstates[setno], pertrans->sortslot);
4277 }
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
union ExprEvalStep::@49 d
struct ExprEvalStep::@49::@90 agg_trans
Tuplesortstate ** sortstates
Definition: nodeAgg.h:154
AttrNumber tts_nvalid
Definition: tuptable.h:121
TupleTableSlot * sortslot
Definition: nodeAgg.h:136
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1552
void tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot)
Definition: tuplesort.c:1684

◆ ExecEvalArrayCoerce()

void ExecEvalArrayCoerce ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 2873 of file execExprInterp.c.

References ARR_ELEMTYPE, array_map(), ExprEvalStep::arraycoerce, ExprEvalStep::d, DatumGetArrayTypePCopy, PointerGetDatum, ExprEvalStep::resnull, and ExprEvalStep::resvalue.

Referenced by ExecInterpExpr().

2874 {
2875  Datum arraydatum;
2876 
2877  /* NULL array -> NULL result */
2878  if (*op->resnull)
2879  return;
2880 
2881  arraydatum = *op->resvalue;
2882 
2883  /*
2884  * If it's binary-compatible, modify the element type in the array header,
2885  * but otherwise leave the array as we received it.
2886  */
2887  if (op->d.arraycoerce.elemexprstate == NULL)
2888  {
2889  /* Detoast input array if necessary, and copy in any case */
2890  ArrayType *array = DatumGetArrayTypePCopy(arraydatum);
2891 
2892  ARR_ELEMTYPE(array) = op->d.arraycoerce.resultelemtype;
2893  *op->resvalue = PointerGetDatum(array);
2894  return;
2895  }
2896 
2897  /*
2898  * Use array_map to apply the sub-expression to each array element.
2899  */
2900  *op->resvalue = array_map(arraydatum,
2901  op->d.arraycoerce.elemexprstate,
2902  econtext,
2903  op->d.arraycoerce.resultelemtype,
2904  op->d.arraycoerce.amstate);
2905 }
Datum * resvalue
Definition: execExpr.h:273
#define PointerGetDatum(X)
Definition: postgres.h:600
bool * resnull
Definition: execExpr.h:274
union ExprEvalStep::@49 d
struct ExprEvalStep::@49::@69 arraycoerce
uintptr_t Datum
Definition: postgres.h:411
#define DatumGetArrayTypePCopy(X)
Definition: array.h:255
Datum array_map(Datum arrayd, ExprState *exprstate, ExprContext *econtext, Oid retType, ArrayMapState *amstate)
Definition: arrayfuncs.c:3158
#define ARR_ELEMTYPE(a)
Definition: array.h:285

◆ ExecEvalArrayExpr()

void ExecEvalArrayExpr ( ExprState state,
ExprEvalStep op 
)

Definition at line 2668 of file execExprInterp.c.

References ARR_DATA_OFFSET, ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_LBOUND, ARR_NDIM, ARR_NULLBITMAP, ARR_OVERHEAD_NONULLS, ARR_OVERHEAD_WITHNULLS, ARR_SIZE, array_bitmap_copy(), ExprEvalStep::arrayexpr, ArrayGetNItems(), construct_empty_array(), construct_md_array(), ExprEvalStep::d, ArrayType::dataoffset, DatumGetArrayTypeP, ExprEvalStep::element_type, ArrayType::elemtype, ereport, errcode(), errdetail(), errmsg(), ERROR, format_type_be(), i, MAXDIM, ArrayType::ndim, ExprEvalStep::nelems, palloc(), PointerGetDatum, ExprEvalStep::resnull, ExprEvalStep::resvalue, and SET_VARSIZE.

Referenced by ExecInterpExpr().

2669 {
2670  ArrayType *result;
2671  Oid element_type = op->d.arrayexpr.elemtype;
2672  int nelems = op->d.arrayexpr.nelems;
2673  int ndims = 0;
2674  int dims[MAXDIM];
2675  int lbs[MAXDIM];
2676 
2677  /* Set non-null as default */
2678  *op->resnull = false;
2679 
2680  if (!op->d.arrayexpr.multidims)
2681  {
2682  /* Elements are presumably of scalar type */
2683  Datum *dvalues = op->d.arrayexpr.elemvalues;
2684  bool *dnulls = op->d.arrayexpr.elemnulls;
2685 
2686  /* setup for 1-D array of the given length */
2687  ndims = 1;
2688  dims[0] = nelems;
2689  lbs[0] = 1;
2690 
2691  result = construct_md_array(dvalues, dnulls, ndims, dims, lbs,
2692  element_type,
2693  op->d.arrayexpr.elemlength,
2694  op->d.arrayexpr.elembyval,
2695  op->d.arrayexpr.elemalign);
2696  }
2697  else
2698  {
2699  /* Must be nested array expressions */
2700  int nbytes = 0;
2701  int nitems = 0;
2702  int outer_nelems = 0;
2703  int elem_ndims = 0;
2704  int *elem_dims = NULL;
2705  int *elem_lbs = NULL;
2706  bool firstone = true;
2707  bool havenulls = false;
2708  bool haveempty = false;
2709  char **subdata;
2710  bits8 **subbitmaps;
2711  int *subbytes;
2712  int *subnitems;
2713  int32 dataoffset;
2714  char *dat;
2715  int iitem;
2716 
2717  subdata = (char **) palloc(nelems * sizeof(char *));
2718  subbitmaps = (bits8 **) palloc(nelems * sizeof(bits8 *));
2719  subbytes = (int *) palloc(nelems * sizeof(int));
2720  subnitems = (int *) palloc(nelems * sizeof(int));
2721 
2722  /* loop through and get data area from each element */
2723  for (int elemoff = 0; elemoff < nelems; elemoff++)
2724  {
2725  Datum arraydatum;
2726  bool eisnull;
2727  ArrayType *array;
2728  int this_ndims;
2729 
2730  arraydatum = op->d.arrayexpr.elemvalues[elemoff];
2731  eisnull = op->d.arrayexpr.elemnulls[elemoff];
2732 
2733  /* temporarily ignore null subarrays */
2734  if (eisnull)
2735  {
2736  haveempty = true;
2737  continue;
2738  }
2739 
2740  array = DatumGetArrayTypeP(arraydatum);
2741 
2742  /* run-time double-check on element type */
2743  if (element_type != ARR_ELEMTYPE(array))
2744  ereport(ERROR,
2745  (errcode(ERRCODE_DATATYPE_MISMATCH),
2746  errmsg("cannot merge incompatible arrays"),
2747  errdetail("Array with element type %s cannot be "
2748  "included in ARRAY construct with element type %s.",
2749  format_type_be(ARR_ELEMTYPE(array)),
2750  format_type_be(element_type))));
2751 
2752  this_ndims = ARR_NDIM(array);
2753  /* temporarily ignore zero-dimensional subarrays */
2754  if (this_ndims <= 0)
2755  {
2756  haveempty = true;
2757  continue;
2758  }
2759 
2760  if (firstone)
2761  {
2762  /* Get sub-array details from first member */
2763  elem_ndims = this_ndims;
2764  ndims = elem_ndims + 1;
2765  if (ndims <= 0 || ndims > MAXDIM)
2766  ereport(ERROR,
2767  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2768  errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
2769  ndims, MAXDIM)));
2770 
2771  elem_dims = (int *) palloc(elem_ndims * sizeof(int));
2772  memcpy(elem_dims, ARR_DIMS(array), elem_ndims * sizeof(int));
2773  elem_lbs = (int *) palloc(elem_ndims * sizeof(int));
2774  memcpy(elem_lbs, ARR_LBOUND(array), elem_ndims * sizeof(int));
2775 
2776  firstone = false;
2777  }
2778  else
2779  {
2780  /* Check other sub-arrays are compatible */
2781  if (elem_ndims != this_ndims ||
2782  memcmp(elem_dims, ARR_DIMS(array),
2783  elem_ndims * sizeof(int)) != 0 ||
2784  memcmp(elem_lbs, ARR_LBOUND(array),
2785  elem_ndims * sizeof(int)) != 0)
2786  ereport(ERROR,
2787  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2788  errmsg("multidimensional arrays must have array "
2789  "expressions with matching dimensions")));
2790  }
2791 
2792  subdata[outer_nelems] = ARR_DATA_PTR(array);
2793  subbitmaps[outer_nelems] = ARR_NULLBITMAP(array);
2794  subbytes[outer_nelems] = ARR_SIZE(array) - ARR_DATA_OFFSET(array);
2795  nbytes += subbytes[outer_nelems];
2796  subnitems[outer_nelems] = ArrayGetNItems(this_ndims,
2797  ARR_DIMS(array));
2798  nitems += subnitems[outer_nelems];
2799  havenulls |= ARR_HASNULL(array);
2800  outer_nelems++;
2801  }
2802 
2803  /*
2804  * If all items were null or empty arrays, return an empty array;
2805  * otherwise, if some were and some weren't, raise error. (Note: we
2806  * must special-case this somehow to avoid trying to generate a 1-D
2807  * array formed from empty arrays. It's not ideal...)
2808  */
2809  if (haveempty)
2810  {
2811  if (ndims == 0) /* didn't find any nonempty array */
2812  {
2813  *op->resvalue = PointerGetDatum(construct_empty_array(element_type));
2814  return;
2815  }
2816  ereport(ERROR,
2817  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2818  errmsg("multidimensional arrays must have array "
2819  "expressions with matching dimensions")));
2820  }
2821 
2822  /* setup for multi-D array */
2823  dims[0] = outer_nelems;
2824  lbs[0] = 1;
2825  for (int i = 1; i < ndims; i++)
2826  {
2827  dims[i] = elem_dims[i - 1];
2828  lbs[i] = elem_lbs[i - 1];
2829  }
2830 
2831  if (havenulls)
2832  {
2833  dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nitems);
2834  nbytes += dataoffset;
2835  }
2836  else
2837  {
2838  dataoffset = 0; /* marker for no null bitmap */
2839  nbytes += ARR_OVERHEAD_NONULLS(ndims);
2840  }
2841 
2842  result = (ArrayType *) palloc(nbytes);
2843  SET_VARSIZE(result, nbytes);
2844  result->ndim = ndims;
2845  result->dataoffset = dataoffset;
2846  result->elemtype = element_type;
2847  memcpy(ARR_DIMS(result), dims, ndims * sizeof(int));
2848  memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int));
2849 
2850  dat = ARR_DATA_PTR(result);
2851  iitem = 0;
2852  for (int i = 0; i < outer_nelems; i++)
2853  {
2854  memcpy(dat, subdata[i], subbytes[i]);
2855  dat += subbytes[i];
2856  if (havenulls)
2857  array_bitmap_copy(ARR_NULLBITMAP(result), iitem,
2858  subbitmaps[i], 0,
2859  subnitems[i]);
2860  iitem += subnitems[i];
2861  }
2862  }
2863 
2864  *op->resvalue = PointerGetDatum(result);
2865 }
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:303
#define ARR_SIZE(a)
Definition: array.h:282
Datum * resvalue
Definition: execExpr.h:273
#define PointerGetDatum(X)
Definition: postgres.h:600
void array_bitmap_copy(bits8 *destbitmap, int destoffset, const bits8 *srcbitmap, int srcoffset, int nitems)
Definition: arrayfuncs.c:4719
int32 dataoffset
Definition: array.h:89
bool * resnull
Definition: execExpr.h:274
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
int errcode(int sqlerrcode)
Definition: elog.c:698
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
ArrayType * construct_empty_array(Oid elmtype)
Definition: arrayfuncs.c:3437
unsigned int Oid
Definition: postgres_ext.h:31
#define ARR_OVERHEAD_WITHNULLS(ndims, nitems)
Definition: array.h:305
signed int int32
Definition: c.h:429
#define ARR_DATA_OFFSET(a)
Definition: array.h:309
#define ARR_LBOUND(a)
Definition: array.h:289
#define ERROR
Definition: elog.h:46
union ExprEvalStep::@49 d
Oid elemtype
Definition: array.h:90
#define ARR_DIMS(a)
Definition: array.h:287
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define MAXDIM
Definition: array.h:75
int errdetail(const char *fmt,...)
Definition: elog.c:1042
#define ARR_HASNULL(a)
Definition: array.h:284
uint8 bits8
Definition: c.h:448
uintptr_t Datum
Definition: postgres.h:411
#define ereport(elevel,...)
Definition: elog.h:157
#define ARR_NDIM(a)
Definition: array.h:283
struct ExprEvalStep::@49::@68 arrayexpr
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
ArrayType * construct_md_array(Datum *elems, bool *nulls, int ndims, int *dims, int *lbs, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3353
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define ARR_NULLBITMAP(a)
Definition: array.h:293
int ndim
Definition: array.h:88
#define DatumGetArrayTypeP(X)
Definition: array.h:254

◆ ExecEvalConstraintCheck()

void ExecEvalConstraintCheck ( ExprState state,
ExprEvalStep op 
)

Definition at line 3649 of file execExprInterp.c.

References ExprEvalStep::d, DatumGetBool, ExprEvalStep::domaincheck, ereport, errcode(), errdomainconstraint(), errmsg(), ERROR, and format_type_be().

Referenced by ExecInterpExpr().

3650 {
3651  if (!*op->d.domaincheck.checknull &&
3652  !DatumGetBool(*op->d.domaincheck.checkvalue))
3653  ereport(ERROR,
3654  (errcode(ERRCODE_CHECK_VIOLATION),
3655  errmsg("value for domain %s violates check constraint \"%s\"",
3656  format_type_be(op->d.domaincheck.resulttype),
3657  op->d.domaincheck.constraintname),
3658  errdomainconstraint(op->d.domaincheck.resulttype,
3659  op->d.domaincheck.constraintname)));
3660 }
int errcode(int sqlerrcode)
Definition: elog.c:698
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
#define ERROR
Definition: elog.h:46
int errdomainconstraint(Oid datatypeOid, const char *conname)
Definition: domains.c:384
union ExprEvalStep::@49 d
#define DatumGetBool(X)
Definition: postgres.h:437
struct ExprEvalStep::@49::@78 domaincheck
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ ExecEvalConstraintNotNull()

void ExecEvalConstraintNotNull ( ExprState state,
ExprEvalStep op 
)

Definition at line 3635 of file execExprInterp.c.

References ExprEvalStep::d, ExprEvalStep::domaincheck, ereport, errcode(), errdatatype(), errmsg(), ERROR, format_type_be(), and ExprEvalStep::resnull.

Referenced by ExecInterpExpr().

3636 {
3637  if (*op->resnull)
3638  ereport(ERROR,
3639  (errcode(ERRCODE_NOT_NULL_VIOLATION),
3640  errmsg("domain %s does not allow null values",
3641  format_type_be(op->d.domaincheck.resulttype)),
3642  errdatatype(op->d.domaincheck.resulttype)));
3643 }
bool * resnull
Definition: execExpr.h:274
int errcode(int sqlerrcode)
Definition: elog.c:698
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
#define ERROR
Definition: elog.h:46
union ExprEvalStep::@49 d
struct ExprEvalStep::@49::@78 domaincheck
int errdatatype(Oid datatypeOid)
Definition: domains.c:360
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ ExecEvalConvertRowtype()

void ExecEvalConvertRowtype ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 3182 of file execExprInterp.c.

References Assert, ExprEvalStep::convert_rowtype, convert_tuples_by_name(), ExprEvalStep::d, DatumGetHeapTupleHeader, DecrTupleDescRefCount(), ExprContext::ecxt_per_query_memory, execute_attr_map_tuple(), get_cached_rowtype(), heap_copy_tuple_as_datum(), HeapTupleGetDatum, HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetTypeId, IncrTupleDescRefCount(), MemoryContextSwitchTo(), ExprEvalStep::resnull, ExprEvalStep::resvalue, HeapTupleData::t_data, HeapTupleData::t_len, and TupleDescData::tdtypeid.

Referenced by ExecInterpExpr().

3183 {
3184  HeapTuple result;
3185  Datum tupDatum;
3186  HeapTupleHeader tuple;
3187  HeapTupleData tmptup;
3188  TupleDesc indesc,
3189  outdesc;
3190  bool changed = false;
3191 
3192  /* NULL in -> NULL out */
3193  if (*op->resnull)
3194  return;
3195 
3196  tupDatum = *op->resvalue;
3197  tuple = DatumGetHeapTupleHeader(tupDatum);
3198 
3199  /*
3200  * Lookup tupdescs if first time through or if type changes. We'd better
3201  * pin them since type conversion functions could do catalog lookups and
3202  * hence cause cache invalidation.
3203  */
3204  indesc = get_cached_rowtype(op->d.convert_rowtype.inputtype, -1,
3205  op->d.convert_rowtype.incache,
3206  &changed);
3207  IncrTupleDescRefCount(indesc);
3208  outdesc = get_cached_rowtype(op->d.convert_rowtype.outputtype, -1,
3209  op->d.convert_rowtype.outcache,
3210  &changed);
3211  IncrTupleDescRefCount(outdesc);
3212 
3213  /*
3214  * We used to be able to assert that incoming tuples are marked with
3215  * exactly the rowtype of indesc. However, now that ExecEvalWholeRowVar
3216  * might change the tuples' marking to plain RECORD due to inserting
3217  * aliases, we can only make this weak test:
3218  */
3219  Assert(HeapTupleHeaderGetTypeId(tuple) == indesc->tdtypeid ||
3220  HeapTupleHeaderGetTypeId(tuple) == RECORDOID);
3221 
3222  /* if first time through, or after change, initialize conversion map */
3223  if (changed)
3224  {
3225  MemoryContext old_cxt;
3226 
3227  /* allocate map in long-lived memory context */
3228  old_cxt = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
3229 
3230  /* prepare map from old to new attribute numbers */
3231  op->d.convert_rowtype.map = convert_tuples_by_name(indesc, outdesc);
3232 
3233  MemoryContextSwitchTo(old_cxt);
3234  }
3235 
3236  /* Following steps need a HeapTuple not a bare HeapTupleHeader */
3237  tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
3238  tmptup.t_data = tuple;
3239 
3240  if (op->d.convert_rowtype.map != NULL)
3241  {
3242  /* Full conversion with attribute rearrangement needed */
3243  result = execute_attr_map_tuple(&tmptup, op->d.convert_rowtype.map);
3244  /* Result already has appropriate composite-datum header fields */
3245  *op->resvalue = HeapTupleGetDatum(result);
3246  }
3247  else
3248  {
3249  /*
3250  * The tuple is physically compatible as-is, but we need to insert the
3251  * destination rowtype OID in its composite-datum header field, so we
3252  * have to copy it anyway. heap_copy_tuple_as_datum() is convenient
3253  * for this since it will both make the physical copy and insert the
3254  * correct composite header fields. Note that we aren't expecting to
3255  * have to flatten any toasted fields: the input was a composite
3256  * datum, so it shouldn't contain any. So heap_copy_tuple_as_datum()
3257  * is overkill here, but its check for external fields is cheap.
3258  */
3259  *op->resvalue = heap_copy_tuple_as_datum(&tmptup, outdesc);
3260  }
3261 
3262  DecrTupleDescRefCount(indesc);
3263  DecrTupleDescRefCount(outdesc);
3264 }
void IncrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:366
Datum * resvalue
Definition: execExpr.h:273
bool * resnull
Definition: execExpr.h:274
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:295
HeapTupleHeader t_data
Definition: htup.h:68
union ExprEvalStep::@49 d
static TupleDesc get_cached_rowtype(Oid type_id, int32 typmod, ExprEvalRowtypeCache *rowcache, bool *changed)
uint32 t_len
Definition: htup.h:64
struct ExprEvalStep::@49::@79 convert_rowtype
TupleConversionMap * convert_tuples_by_name(TupleDesc indesc, TupleDesc outdesc)
Definition: tupconvert.c:102
HeapTuple execute_attr_map_tuple(HeapTuple tuple, TupleConversionMap *map)
Definition: tupconvert.c:139
Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc)
Definition: heaptuple.c:984
uintptr_t Datum
Definition: postgres.h:411
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:457
#define Assert(condition)
Definition: c.h:804
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:384
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:233
Oid tdtypeid
Definition: tupdesc.h:82
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:451

◆ ExecEvalCurrentOfExpr()

void ExecEvalCurrentOfExpr ( ExprState state,
ExprEvalStep op 
)

Definition at line 2529 of file execExprInterp.c.

References ereport, errcode(), errmsg(), and ERROR.

Referenced by ExecInterpExpr().

2530 {
2531  ereport(ERROR,
2532  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2533  errmsg("WHERE CURRENT OF is not supported for this table type")));
2534 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ ExecEvalFieldSelect()

void ExecEvalFieldSelect ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 2987 of file execExprInterp.c.

References Assert, ExprEvalStep::d, DatumGetEOHP(), DatumGetHeapTupleHeader, DatumGetPointer, elog, ER_MAGIC, ExpandedRecordHeader::er_magic, ereport, errcode(), errdetail(), errmsg(), ERROR, expanded_record_get_field(), expanded_record_get_tupdesc(), ExprEvalStep::fieldnum, ExprEvalStep::fieldselect, format_type_be(), get_cached_rowtype(), heap_getattr, HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, TupleDescData::natts, ExprEvalStep::resnull, ExprEvalStep::resvalue, HeapTupleData::t_data, HeapTupleData::t_len, TupleDescAttr, and VARATT_IS_EXTERNAL_EXPANDED.

Referenced by ExecInterpExpr().

2988 {
2989  AttrNumber fieldnum = op->d.fieldselect.fieldnum;
2990  Datum tupDatum;
2991  HeapTupleHeader tuple;
2992  Oid tupType;
2993  int32 tupTypmod;
2994  TupleDesc tupDesc;
2995  Form_pg_attribute attr;
2996  HeapTupleData tmptup;
2997 
2998  /* NULL record -> NULL result */
2999  if (*op->resnull)
3000  return;
3001 
3002  tupDatum = *op->resvalue;
3003 
3004  /* We can special-case expanded records for speed */
3006  {
3008 
3009  Assert(erh->er_magic == ER_MAGIC);
3010 
3011  /* Extract record's TupleDesc */
3012  tupDesc = expanded_record_get_tupdesc(erh);
3013 
3014  /*
3015  * Find field's attr record. Note we don't support system columns
3016  * here: a datum tuple doesn't have valid values for most of the
3017  * interesting system columns anyway.
3018  */
3019  if (fieldnum <= 0) /* should never happen */
3020  elog(ERROR, "unsupported reference to system column %d in FieldSelect",
3021  fieldnum);
3022  if (fieldnum > tupDesc->natts) /* should never happen */
3023  elog(ERROR, "attribute number %d exceeds number of columns %d",
3024  fieldnum, tupDesc->natts);
3025  attr = TupleDescAttr(tupDesc, fieldnum - 1);
3026 
3027  /* Check for dropped column, and force a NULL result if so */
3028  if (attr->attisdropped)
3029  {
3030  *op->resnull = true;
3031  return;
3032  }
3033 
3034  /* Check for type mismatch --- possible after ALTER COLUMN TYPE? */
3035  /* As in CheckVarSlotCompatibility, we should but can't check typmod */
3036  if (op->d.fieldselect.resulttype != attr->atttypid)
3037  ereport(ERROR,
3038  (errcode(ERRCODE_DATATYPE_MISMATCH),
3039  errmsg("attribute %d has wrong type", fieldnum),
3040  errdetail("Table has type %s, but query expects %s.",
3041  format_type_be(attr->atttypid),
3042  format_type_be(op->d.fieldselect.resulttype))));
3043 
3044  /* extract the field */
3045  *op->resvalue = expanded_record_get_field(erh, fieldnum,
3046  op->resnull);
3047  }
3048  else
3049  {
3050  /* Get the composite datum and extract its type fields */
3051  tuple = DatumGetHeapTupleHeader(tupDatum);
3052 
3053  tupType = HeapTupleHeaderGetTypeId(tuple);
3054  tupTypmod = HeapTupleHeaderGetTypMod(tuple);
3055 
3056  /* Lookup tupdesc if first time through or if type changes */
3057  tupDesc = get_cached_rowtype(tupType, tupTypmod,
3058  &op->d.fieldselect.rowcache, NULL);
3059 
3060  /*
3061  * Find field's attr record. Note we don't support system columns
3062  * here: a datum tuple doesn't have valid values for most of the
3063  * interesting system columns anyway.
3064  */
3065  if (fieldnum <= 0) /* should never happen */
3066  elog(ERROR, "unsupported reference to system column %d in FieldSelect",
3067  fieldnum);
3068  if (fieldnum > tupDesc->natts) /* should never happen */
3069  elog(ERROR, "attribute number %d exceeds number of columns %d",
3070  fieldnum, tupDesc->natts);
3071  attr = TupleDescAttr(tupDesc, fieldnum - 1);
3072 
3073  /* Check for dropped column, and force a NULL result if so */
3074  if (attr->attisdropped)
3075  {
3076  *op->resnull = true;
3077  return;
3078  }
3079 
3080  /* Check for type mismatch --- possible after ALTER COLUMN TYPE? */
3081  /* As in CheckVarSlotCompatibility, we should but can't check typmod */
3082  if (op->d.fieldselect.resulttype != attr->atttypid)
3083  ereport(ERROR,
3084  (errcode(ERRCODE_DATATYPE_MISMATCH),
3085  errmsg("attribute %d has wrong type", fieldnum),
3086  errdetail("Table has type %s, but query expects %s.",
3087  format_type_be(attr->atttypid),
3088  format_type_be(op->d.fieldselect.resulttype))));
3089 
3090  /* heap_getattr needs a HeapTuple not a bare HeapTupleHeader */
3091  tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
3092  tmptup.t_data = tuple;
3093 
3094  /* extract the field */
3095  *op->resvalue = heap_getattr(&tmptup,
3096  fieldnum,
3097  tupDesc,
3098  op->resnull);
3099  }
3100 }
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: postgres.h:335
Datum * resvalue
Definition: execExpr.h:273
static Datum expanded_record_get_field(ExpandedRecordHeader *erh, int fnumber, bool *isnull)
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
bool * resnull
Definition: execExpr.h:274
int errcode(int sqlerrcode)
Definition: elog.c:698
struct ExprEvalStep::@49::@74 fieldselect
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:295
signed int int32
Definition: c.h:429
HeapTupleHeader t_data
Definition: htup.h:68
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:467
#define ERROR
Definition: elog.h:46
union ExprEvalStep::@49 d
static TupleDesc get_cached_rowtype(Oid type_id, int32 typmod, ExprEvalRowtypeCache *rowcache, bool *changed)
uint32 t_len
Definition: htup.h:64
int errdetail(const char *fmt,...)
Definition: elog.c:1042
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:203
static TupleDesc expanded_record_get_tupdesc(ExpandedRecordHeader *erh)
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
uintptr_t Datum
Definition: postgres.h:411
#define ER_MAGIC
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:457
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
#define DatumGetPointer(X)
Definition: postgres.h:593
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
int16 AttrNumber
Definition: attnum.h:21
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:451

◆ ExecEvalFieldStoreDeForm()

void ExecEvalFieldStoreDeForm ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 3112 of file execExprInterp.c.

References ExprEvalStep::d, DatumGetHeapTupleHeader, elog, ERROR, ExprEvalStep::fieldstore, get_cached_rowtype(), heap_deform_tuple(), HeapTupleHeaderGetDatumLength, InvalidOid, ItemPointerSetInvalid, TupleDescData::natts, ExprEvalStep::resnull, ExprEvalStep::resvalue, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, and unlikely.

Referenced by ExecInterpExpr().

3113 {
3114  TupleDesc tupDesc;
3115 
3116  /* Lookup tupdesc if first time through or if type changes */
3117  tupDesc = get_cached_rowtype(op->d.fieldstore.fstore->resulttype, -1,
3118  op->d.fieldstore.rowcache, NULL);
3119 
3120  /* Check that current tupdesc doesn't have more fields than we allocated */
3121  if (unlikely(tupDesc->natts > op->d.fieldstore.ncolumns))
3122  elog(ERROR, "too many columns in composite type %u",
3123  op->d.fieldstore.fstore->resulttype);
3124 
3125  if (*op->resnull)
3126  {
3127  /* Convert null input tuple into an all-nulls row */
3128  memset(op->d.fieldstore.nulls, true,
3129  op->d.fieldstore.ncolumns * sizeof(bool));
3130  }
3131  else
3132  {
3133  /*
3134  * heap_deform_tuple needs a HeapTuple not a bare HeapTupleHeader. We
3135  * set all the fields in the struct just in case.
3136  */
3137  Datum tupDatum = *op->resvalue;
3138  HeapTupleHeader tuphdr;
3139  HeapTupleData tmptup;
3140 
3141  tuphdr = DatumGetHeapTupleHeader(tupDatum);
3142  tmptup.t_len = HeapTupleHeaderGetDatumLength(tuphdr);
3143  ItemPointerSetInvalid(&(tmptup.t_self));
3144  tmptup.t_tableOid = InvalidOid;
3145  tmptup.t_data = tuphdr;
3146 
3147  heap_deform_tuple(&tmptup, tupDesc,
3148  op->d.fieldstore.values,
3149  op->d.fieldstore.nulls);
3150  }
3151 }
Datum * resvalue
Definition: execExpr.h:273
bool * resnull
Definition: execExpr.h:274
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:295
HeapTupleHeader t_data
Definition: htup.h:68
#define ERROR
Definition: elog.h:46
union ExprEvalStep::@49 d
ItemPointerData t_self
Definition: htup.h:65
static TupleDesc get_cached_rowtype(Oid type_id, int32 typmod, ExprEvalRowtypeCache *rowcache, bool *changed)
uint32 t_len
Definition: htup.h:64
Oid t_tableOid
Definition: htup.h:66
struct ExprEvalStep::@49::@75 fieldstore
uintptr_t Datum
Definition: postgres.h:411
#define InvalidOid
Definition: postgres_ext.h:36
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1249
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:172
#define elog(elevel,...)
Definition: elog.h:232
#define unlikely(x)
Definition: c.h:273
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:451

◆ ExecEvalFieldStoreForm()

void ExecEvalFieldStoreForm ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 3158 of file execExprInterp.c.

References ExprEvalStep::d, ExprEvalStep::fieldstore, get_cached_rowtype(), heap_form_tuple(), HeapTupleGetDatum, ExprEvalStep::resnull, and ExprEvalStep::resvalue.

Referenced by ExecInterpExpr().

3159 {
3160  TupleDesc tupDesc;
3161  HeapTuple tuple;
3162 
3163  /* Lookup tupdesc (should be valid already) */
3164  tupDesc = get_cached_rowtype(op->d.fieldstore.fstore->resulttype, -1,
3165  op->d.fieldstore.rowcache, NULL);
3166 
3167  tuple = heap_form_tuple(tupDesc,
3168  op->d.fieldstore.values,
3169  op->d.fieldstore.nulls);
3170 
3171  *op->resvalue = HeapTupleGetDatum(tuple);
3172  *op->resnull = false;
3173 }
Datum * resvalue
Definition: execExpr.h:273
bool * resnull
Definition: execExpr.h:274
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
union ExprEvalStep::@49 d
static TupleDesc get_cached_rowtype(Oid type_id, int32 typmod, ExprEvalRowtypeCache *rowcache, bool *changed)
struct ExprEvalStep::@49::@75 fieldstore
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221

◆ ExecEvalFuncExprFusage()

void ExecEvalFuncExprFusage ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 2340 of file execExprInterp.c.

References ExprEvalStep::d, ExprEvalStep::func, FunctionCallInfoBaseData::isnull, pgstat_end_function_usage(), pgstat_init_function_usage(), ExprEvalStep::resnull, and ExprEvalStep::resvalue.

Referenced by ExecInterpExpr().

2342 {
2343  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
2344  PgStat_FunctionCallUsage fcusage;
2345  Datum d;
2346 
2347  pgstat_init_function_usage(fcinfo, &fcusage);
2348 
2349  fcinfo->isnull = false;
2350  d = op->d.func.fn_addr(fcinfo);
2351  *op->resvalue = d;
2352  *op->resnull = fcinfo->isnull;
2353 
2354  pgstat_end_function_usage(&fcusage, true);
2355 }
Datum * resvalue
Definition: execExpr.h:273
bool * resnull
Definition: execExpr.h:274
void pgstat_init_function_usage(FunctionCallInfo fcinfo, PgStat_FunctionCallUsage *fcu)
Definition: pgstat.c:1910
union ExprEvalStep::@49 d
uintptr_t Datum
Definition: postgres.h:411
struct ExprEvalStep::@49::@56 func
void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu, bool finalize)
Definition: pgstat.c:1981

◆ ExecEvalFuncExprStrictFusage()

void ExecEvalFuncExprStrictFusage ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 2361 of file execExprInterp.c.

References FunctionCallInfoBaseData::args, generate_unaccent_rules::args, ExprEvalStep::d, ExprEvalStep::func, FunctionCallInfoBaseData::isnull, ExprEvalStep::isnull, ExprEvalStep::nargs, pgstat_end_function_usage(), pgstat_init_function_usage(), ExprEvalStep::resnull, and ExprEvalStep::resvalue.

Referenced by ExecInterpExpr().

2363 {
2364 
2365  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
2366  PgStat_FunctionCallUsage fcusage;
2367  NullableDatum *args = fcinfo->args;
2368  int nargs = op->d.func.nargs;
2369  Datum d;
2370 
2371  /* strict function, so check for NULL args */
2372  for (int argno = 0; argno < nargs; argno++)
2373  {
2374  if (args[argno].isnull)
2375  {
2376  *op->resnull = true;
2377  return;
2378  }
2379  }
2380 
2381  pgstat_init_function_usage(fcinfo, &fcusage);
2382 
2383  fcinfo->isnull = false;
2384  d = op->d.func.fn_addr(fcinfo);
2385  *op->resvalue = d;
2386  *op->resnull = fcinfo->isnull;
2387 
2388  pgstat_end_function_usage(&fcusage, true);
2389 }
Datum * resvalue
Definition: execExpr.h:273
bool * resnull
Definition: execExpr.h:274
void pgstat_init_function_usage(FunctionCallInfo fcinfo, PgStat_FunctionCallUsage *fcu)
Definition: pgstat.c:1910
union ExprEvalStep::@49 d
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
uintptr_t Datum
Definition: postgres.h:411
struct ExprEvalStep::@49::@56 func
void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu, bool finalize)
Definition: pgstat.c:1981

◆ ExecEvalGroupingFunc()

void ExecEvalGroupingFunc ( ExprState state,
ExprEvalStep op 
)

Definition at line 3888 of file execExprInterp.c.

References attnum, bms_is_member(), castNode, ExprEvalStep::d, AggState::grouped_cols, ExprEvalStep::grouping_func, Int32GetDatum, lfirst_int, ExprState::parent, ExprEvalStep::resnull, and ExprEvalStep::resvalue.

Referenced by ExecInterpExpr().

3889 {
3890  AggState *aggstate = castNode(AggState, state->parent);
3891  int result = 0;
3892  Bitmapset *grouped_cols = aggstate->grouped_cols;
3893  ListCell *lc;
3894 
3895  foreach(lc, op->d.grouping_func.clauses)
3896  {
3897  int attnum = lfirst_int(lc);
3898 
3899  result <<= 1;
3900 
3901  if (!bms_is_member(attnum, grouped_cols))
3902  result |= 1;
3903  }
3904 
3905  *op->resvalue = Int32GetDatum(result);
3906  *op->resnull = false;
3907 }
struct PlanState * parent
Definition: execnodes.h:108
Datum * resvalue
Definition: execExpr.h:273
#define castNode(_type_, nodeptr)
Definition: nodes.h:608
bool * resnull
Definition: execExpr.h:274
union ExprEvalStep::@49 d
#define lfirst_int(lc)
Definition: pg_list.h:170
Bitmapset * grouped_cols
Definition: execnodes.h:2295
struct ExprEvalStep::@49::@84 grouping_func
int16 attnum
Definition: pg_attribute.h:83
#define Int32GetDatum(X)
Definition: postgres.h:523
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ ExecEvalHashedScalarArrayOp()

void ExecEvalHashedScalarArrayOp ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 3480 of file execExprInterp.c.

References FunctionCallInfoBaseData::args, ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_NDIM, ARR_NULLBITMAP, ArrayGetNItems(), Assert, att_addlength_pointer, att_align_nominal, BoolGetDatum, CurrentMemoryContext, ExprEvalStep::d, DatumGetArrayTypeP, DatumGetBool, ExprContext::ecxt_per_query_memory, element(), ExprEvalStep::elements_tab, fetch_att, get_typlenbyvalalign(), ExprEvalStep::has_nulls, ExprEvalStep::hashedscalararrayop, ScalarArrayOpExprHashTable::hashtab, i, FunctionCallInfoBaseData::isnull, NullableDatum::isnull, MemoryContextSwitchTo(), ScalarArrayOpExprHashTable::op, ExprEvalStep::op, palloc(), ExprEvalStep::resnull, ExprEvalStep::resvalue, typalign, ExprEvalStep::typbyval, ExprEvalStep::typlen, and NullableDatum::value.

Referenced by ExecInterpExpr().

3481 {
3482  ScalarArrayOpExprHashTable *elements_tab = op->d.hashedscalararrayop.elements_tab;
3483  FunctionCallInfo fcinfo = op->d.hashedscalararrayop.fcinfo_data;
3484  bool strictfunc = op->d.hashedscalararrayop.finfo->fn_strict;
3485  Datum scalar = fcinfo->args[0].value;
3486  bool scalar_isnull = fcinfo->args[0].isnull;
3487  Datum result;
3488  bool resultnull;
3489  bool hashfound;
3490 
3491  /* We don't setup a hashed scalar array op if the array const is null. */
3492  Assert(!*op->resnull);
3493 
3494  /*
3495  * If the scalar is NULL, and the function is strict, return NULL; no
3496  * point in executing the search.
3497  */
3498  if (fcinfo->args[0].isnull && strictfunc)
3499  {
3500  *op->resnull = true;
3501  return;
3502  }
3503 
3504  /* Build the hash table on first evaluation */
3505  if (elements_tab == NULL)
3506  {
3507  int16 typlen;
3508  bool typbyval;
3509  char typalign;
3510  int nitems;
3511  bool has_nulls = false;
3512  char *s;
3513  bits8 *bitmap;
3514  int bitmask;
3515  MemoryContext oldcontext;
3516  ArrayType *arr;
3517 
3518  arr = DatumGetArrayTypeP(*op->resvalue);
3519  nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
3520 
3522  &typlen,
3523  &typbyval,
3524  &typalign);
3525 
3526  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
3527 
3528  elements_tab = (ScalarArrayOpExprHashTable *)
3530  op->d.hashedscalararrayop.elements_tab = elements_tab;
3531  elements_tab->op = op;
3532 
3533  /*
3534  * Create the hash table sizing it according to the number of elements
3535  * in the array. This does assume that the array has no duplicates.
3536  * If the array happens to contain many duplicate values then it'll
3537  * just mean that we sized the table a bit on the large side.
3538  */
3539  elements_tab->hashtab = saophash_create(CurrentMemoryContext, nitems,
3540  elements_tab);
3541 
3542  MemoryContextSwitchTo(oldcontext);
3543 
3544  s = (char *) ARR_DATA_PTR(arr);
3545  bitmap = ARR_NULLBITMAP(arr);
3546  bitmask = 1;
3547  for (int i = 0; i < nitems; i++)
3548  {
3549  /* Get array element, checking for NULL. */
3550  if (bitmap && (*bitmap & bitmask) == 0)
3551  {
3552  has_nulls = true;
3553  }
3554  else
3555  {
3556  Datum element;
3557 
3558  element = fetch_att(s, typbyval, typlen);
3559  s = att_addlength_pointer(s, typlen, s);
3560  s = (char *) att_align_nominal(s, typalign);
3561 
3562  saophash_insert(elements_tab->hashtab, element, &hashfound);
3563  }
3564 
3565  /* Advance bitmap pointer if any. */
3566  if (bitmap)
3567  {
3568  bitmask <<= 1;
3569  if (bitmask == 0x100)
3570  {
3571  bitmap++;
3572  bitmask = 1;
3573  }
3574  }
3575  }
3576 
3577  /*
3578  * Remember if we had any nulls so that we know if we need to execute
3579  * non-strict functions with a null lhs value if no match is found.
3580  */
3581  op->d.hashedscalararrayop.has_nulls = has_nulls;
3582  }
3583 
3584  /* Check the hash to see if we have a match. */
3585  hashfound = NULL != saophash_lookup(elements_tab->hashtab, scalar);
3586 
3587  result = BoolGetDatum(hashfound);
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 (!DatumGetBool(result) && 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.fn_addr(fcinfo);
3623  resultnull = fcinfo->isnull;
3624  }
3625  }
3626 
3627  *op->resvalue = result;
3628  *op->resnull = resultnull;
3629 }
signed short int16
Definition: c.h:428
struct ExprEvalStep * op
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:148
Datum * resvalue
Definition: execExpr.h:273
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2218
bool * resnull
Definition: execExpr.h:274
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
char typalign
Definition: pg_type.h:176
union ExprEvalStep::@49 d
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
#define ARR_DIMS(a)
Definition: array.h:287
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define DatumGetBool(X)
Definition: postgres.h:437
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:176
Datum value
Definition: postgres.h:422
static chr element(struct vars *v, const chr *startp, const chr *endp)
Definition: regc_locale.c:376
uint8 bits8
Definition: c.h:448
uintptr_t Datum
Definition: postgres.h:411
#define BoolGetDatum(X)
Definition: postgres.h:446
#define Assert(condition)
Definition: c.h:804
struct ExprEvalStep::@49::@81 hashedscalararrayop
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:233
#define ARR_NDIM(a)
Definition: array.h:283
void * palloc(Size size)
Definition: mcxt.c:1062
#define fetch_att(T, attbyval, attlen)
Definition: tupmacs.h:75
int i
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define ARR_NULLBITMAP(a)
Definition: array.h:293
#define DatumGetArrayTypeP(X)
Definition: array.h:254

◆ ExecEvalMinMax()

void ExecEvalMinMax ( ExprState state,
ExprEvalStep op 
)

Definition at line 2934 of file execExprInterp.c.

References FunctionCallInfoBaseData::args, Assert, ExprEvalStep::d, DatumGetInt32, FunctionCallInvoke, IS_GREATEST, IS_LEAST, FunctionCallInfoBaseData::isnull, NullableDatum::isnull, ExprEvalStep::minmax, ExprEvalStep::nulls, ExprEvalStep::resnull, ExprEvalStep::resvalue, NullableDatum::value, and values.

Referenced by ExecInterpExpr().

2935 {
2936  Datum *values = op->d.minmax.values;
2937  bool *nulls = op->d.minmax.nulls;
2938  FunctionCallInfo fcinfo = op->d.minmax.fcinfo_data;
2939  MinMaxOp operator = op->d.minmax.op;
2940 
2941  /* set at initialization */
2942  Assert(fcinfo->args[0].isnull == false);
2943  Assert(fcinfo->args[1].isnull == false);
2944 
2945  /* default to null result */
2946  *op->resnull = true;
2947 
2948  for (int off = 0; off < op->d.minmax.nelems; off++)
2949  {
2950  /* ignore NULL inputs */
2951  if (nulls[off])
2952  continue;
2953 
2954  if (*op->resnull)
2955  {
2956  /* first nonnull input, adopt value */
2957  *op->resvalue = values[off];
2958  *op->resnull = false;
2959  }
2960  else
2961  {
2962  int cmpresult;
2963 
2964  /* apply comparison function */
2965  fcinfo->args[0].value = *op->resvalue;
2966  fcinfo->args[1].value = values[off];
2967 
2968  fcinfo->isnull = false;
2969  cmpresult = DatumGetInt32(FunctionCallInvoke(fcinfo));
2970  if (fcinfo->isnull) /* probably should not happen */
2971  continue;
2972 
2973  if (cmpresult > 0 && operator == IS_LEAST)
2974  *op->resvalue = values[off];
2975  else if (cmpresult < 0 && operator == IS_GREATEST)
2976  *op->resvalue = values[off];
2977  }
2978  }
2979 }
Datum * resvalue
Definition: execExpr.h:273
#define DatumGetInt32(X)
Definition: postgres.h:516
bool * resnull
Definition: execExpr.h:274
MinMaxOp
Definition: primnodes.h:1129
union ExprEvalStep::@49 d
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:172
Datum value
Definition: postgres.h:422
uintptr_t Datum
Definition: postgres.h:411
#define Assert(condition)
Definition: c.h:804
static Datum values[MAXATTR]
Definition: bootstrap.c:166
struct ExprEvalStep::@49::@73 minmax

◆ ExecEvalNextValueExpr()

void ExecEvalNextValueExpr ( ExprState state,
ExprEvalStep op 
)

Definition at line 2540 of file execExprInterp.c.

References ExprEvalStep::d, elog, ERROR, Int16GetDatum, Int32GetDatum, Int64GetDatum(), newval, nextval_internal(), ExprEvalStep::nextvalueexpr, ExprEvalStep::resnull, and ExprEvalStep::resvalue.

Referenced by ExecInterpExpr().

2541 {
2542  int64 newval = nextval_internal(op->d.nextvalueexpr.seqid, false);
2543 
2544  switch (op->d.nextvalueexpr.seqtypid)
2545  {
2546  case INT2OID:
2547  *op->resvalue = Int16GetDatum((int16) newval);
2548  break;
2549  case INT4OID:
2550  *op->resvalue = Int32GetDatum((int32) newval);
2551  break;
2552  case INT8OID:
2553  *op->resvalue = Int64GetDatum((int64) newval);
2554  break;
2555  default:
2556  elog(ERROR, "unsupported sequence type %u",
2557  op->d.nextvalueexpr.seqtypid);
2558  }
2559  *op->resnull = false;
2560 }
signed short int16
Definition: c.h:428
Datum * resvalue
Definition: execExpr.h:273
int64 nextval_internal(Oid relid, bool check_permissions)
Definition: sequence.c:579
bool * resnull
Definition: execExpr.h:274
#define Int16GetDatum(X)
Definition: postgres.h:495
struct ExprEvalStep::@49::@67 nextvalueexpr
signed int int32
Definition: c.h:429
#define ERROR
Definition: elog.h:46
union ExprEvalStep::@49 d
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1697
#define newval
#define Int32GetDatum(X)
Definition: postgres.h:523
#define elog(elevel,...)
Definition: elog.h:232

◆ ExecEvalParamExec()

void ExecEvalParamExec ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 2398 of file execExprInterp.c.

References Assert, ExprEvalStep::d, ExprContext::ecxt_param_exec_vals, ParamExecData::execPlan, ExecSetParamPlan(), ParamExecData::isnull, ExprEvalStep::param, ExprEvalStep::resnull, ExprEvalStep::resvalue, unlikely, and ParamExecData::value.

Referenced by ExecInterpExpr().

2399 {
2400  ParamExecData *prm;
2401 
2402  prm = &(econtext->ecxt_param_exec_vals[op->d.param.paramid]);
2403  if (unlikely(prm->execPlan != NULL))
2404  {
2405  /* Parameter not evaluated yet, so go do it */
2406  ExecSetParamPlan(prm->execPlan, econtext);
2407  /* ExecSetParamPlan should have processed this param... */
2408  Assert(prm->execPlan == NULL);
2409  }
2410  *op->resvalue = prm->value;
2411  *op->resnull = prm->isnull;
2412 }
void * execPlan
Definition: params.h:148
Datum * resvalue
Definition: execExpr.h:273
bool * resnull
Definition: execExpr.h:274
union ExprEvalStep::@49 d
void ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
Definition: nodeSubplan.c:1058
bool isnull
Definition: params.h:150
struct ExprEvalStep::@49::@61 param
ParamExecData * ecxt_param_exec_vals
Definition: execnodes.h:237
#define Assert(condition)
Definition: c.h:804
Datum value
Definition: params.h:149
#define unlikely(x)
Definition: c.h:273

◆ ExecEvalParamExtern()

void ExecEvalParamExtern ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 2420 of file execExprInterp.c.

References ExprEvalStep::d, ExprContext::ecxt_param_list_info, ereport, errcode(), errmsg(), ERROR, format_type_be(), ParamExternData::isnull, likely, OidIsValid, ExprEvalStep::param, ParamListInfoData::paramFetch, ParamListInfoData::params, ParamExternData::ptype, ExprEvalStep::resnull, ExprEvalStep::resvalue, unlikely, and ParamExternData::value.

Referenced by ExecInterpExpr().

2421 {
2422  ParamListInfo paramInfo = econtext->ecxt_param_list_info;
2423  int paramId = op->d.param.paramid;
2424 
2425  if (likely(paramInfo &&
2426  paramId > 0 && paramId <= paramInfo->numParams))
2427  {
2428  ParamExternData *prm;
2429  ParamExternData prmdata;
2430 
2431  /* give hook a chance in case parameter is dynamic */
2432  if (paramInfo->paramFetch != NULL)
2433  prm = paramInfo->paramFetch(paramInfo, paramId, false, &prmdata);
2434  else
2435  prm = &paramInfo->params[paramId - 1];
2436 
2437  if (likely(OidIsValid(prm->ptype)))
2438  {
2439  /* safety check in case hook did something unexpected */
2440  if (unlikely(prm->ptype != op->d.param.paramtype))
2441  ereport(ERROR,
2442  (errcode(ERRCODE_DATATYPE_MISMATCH),
2443  errmsg("type of parameter %d (%s) does not match that when preparing the plan (%s)",
2444  paramId,
2445  format_type_be(prm->ptype),
2446  format_type_be(op->d.param.paramtype))));
2447  *op->resvalue = prm->value;
2448  *op->resnull = prm->isnull;
2449  return;
2450  }
2451  }
2452 
2453  ereport(ERROR,
2454  (errcode(ERRCODE_UNDEFINED_OBJECT),
2455  errmsg("no value found for parameter %d", paramId)));
2456 }
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:125
Datum value
Definition: params.h:92
#define likely(x)
Definition: c.h:272
Datum * resvalue
Definition: execExpr.h:273
bool * resnull
Definition: execExpr.h:274
int errcode(int sqlerrcode)
Definition: elog.c:698
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
#define OidIsValid(objectId)
Definition: c.h:710
ParamFetchHook paramFetch
Definition: params.h:112
#define ERROR
Definition: elog.h:46
union ExprEvalStep::@49 d
struct ExprEvalStep::@49::@61 param
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define unlikely(x)
Definition: c.h:273
ParamListInfo ecxt_param_list_info
Definition: execnodes.h:238
bool isnull
Definition: params.h:93

◆ ExecEvalRow()

void ExecEvalRow ( ExprState state,
ExprEvalStep op 
)

Definition at line 2914 of file execExprInterp.c.

References ExprEvalStep::d, heap_form_tuple(), HeapTupleGetDatum, ExprEvalStep::resnull, ExprEvalStep::resvalue, and ExprEvalStep::row.

Referenced by ExecInterpExpr().

2915 {
2916  HeapTuple tuple;
2917 
2918  /* build tuple from evaluated field values */
2919  tuple = heap_form_tuple(op->d.row.tupdesc,
2920  op->d.row.elemvalues,
2921  op->d.row.elemnulls);
2922 
2923  *op->resvalue = HeapTupleGetDatum(tuple);
2924  *op->resnull = false;
2925 }
Datum * resvalue
Definition: execExpr.h:273
bool * resnull
Definition: execExpr.h:274
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
union ExprEvalStep::@49 d
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
struct ExprEvalStep::@49::@70 row

◆ ExecEvalRowNotNull()

void ExecEvalRowNotNull ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 2575 of file execExprInterp.c.

References ExecEvalRowNullInt().

Referenced by ExecInterpExpr().

2576 {
2577  ExecEvalRowNullInt(state, op, econtext, false);
2578 }
static void ExecEvalRowNullInt(ExprState *state, ExprEvalStep *op, ExprContext *econtext, bool checkisnull)

◆ ExecEvalRowNull()

void ExecEvalRowNull ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 2566 of file execExprInterp.c.

References ExecEvalRowNullInt().

Referenced by ExecInterpExpr().

2567 {
2568  ExecEvalRowNullInt(state, op, econtext, true);
2569 }
static void ExecEvalRowNullInt(ExprState *state, ExprEvalStep *op, ExprContext *econtext, bool checkisnull)

◆ ExecEvalRowNullInt()

static void ExecEvalRowNullInt ( ExprState state,
ExprEvalStep op,
ExprContext econtext,
bool  checkisnull 
)
static

Definition at line 2582 of file execExprInterp.c.

References BoolGetDatum, ExprEvalStep::d, DatumGetHeapTupleHeader, get_cached_rowtype(), heap_attisnull(), HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, ExprEvalStep::isnull, TupleDescData::natts, ExprEvalStep::nulltest_row, ExprEvalStep::resnull, ExprEvalStep::resvalue, HeapTupleData::t_data, HeapTupleData::t_len, TupleDescAttr, and value.

Referenced by ExecEvalRowNotNull(), and ExecEvalRowNull().

2584 {
2585  Datum value = *op->resvalue;
2586  bool isnull = *op->resnull;
2587  HeapTupleHeader tuple;
2588  Oid tupType;
2589  int32 tupTypmod;
2590  TupleDesc tupDesc;
2591  HeapTupleData tmptup;
2592 
2593  *op->resnull = false;
2594 
2595  /* NULL row variables are treated just as NULL scalar columns */
2596  if (isnull)
2597  {
2598  *op->resvalue = BoolGetDatum(checkisnull);
2599  return;
2600  }
2601 
2602  /*
2603  * The SQL standard defines IS [NOT] NULL for a non-null rowtype argument
2604  * as:
2605  *
2606  * "R IS NULL" is true if every field is the null value.
2607  *
2608  * "R IS NOT NULL" is true if no field is the null value.
2609  *
2610  * This definition is (apparently intentionally) not recursive; so our
2611  * tests on the fields are primitive attisnull tests, not recursive checks
2612  * to see if they are all-nulls or no-nulls rowtypes.
2613  *
2614  * The standard does not consider the possibility of zero-field rows, but
2615  * here we consider them to vacuously satisfy both predicates.
2616  */
2617 
2618  tuple = DatumGetHeapTupleHeader(value);
2619 
2620  tupType = HeapTupleHeaderGetTypeId(tuple);
2621  tupTypmod = HeapTupleHeaderGetTypMod(tuple);
2622 
2623  /* Lookup tupdesc if first time through or if type changes */
2624  tupDesc = get_cached_rowtype(tupType, tupTypmod,
2625  &op->d.nulltest_row.rowcache, NULL);
2626 
2627  /*
2628  * heap_attisnull needs a HeapTuple not a bare HeapTupleHeader.
2629  */
2630  tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
2631  tmptup.t_data = tuple;
2632 
2633  for (int att = 1; att <= tupDesc->natts; att++)
2634  {
2635  /* ignore dropped columns */
2636  if (TupleDescAttr(tupDesc, att - 1)->attisdropped)
2637  continue;
2638  if (heap_attisnull(&tmptup, att, tupDesc))
2639  {
2640  /* null field disproves IS NOT NULL */
2641  if (!checkisnull)
2642  {
2643  *op->resvalue = BoolGetDatum(false);
2644  return;
2645  }
2646  }
2647  else
2648  {
2649  /* non-null field disproves IS NULL */
2650  if (checkisnull)
2651  {
2652  *op->resvalue = BoolGetDatum(false);
2653  return;
2654  }
2655  }
2656  }
2657 
2658  *op->resvalue = BoolGetDatum(true);
2659 }
static struct @142 value
Datum * resvalue
Definition: execExpr.h:273
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
bool * resnull
Definition: execExpr.h:274
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
unsigned int Oid
Definition: postgres_ext.h:31
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:295
signed int int32
Definition: c.h:429
HeapTupleHeader t_data
Definition: htup.h:68
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:467
union ExprEvalStep::@49 d
static TupleDesc get_cached_rowtype(Oid type_id, int32 typmod, ExprEvalRowtypeCache *rowcache, bool *changed)
uint32 t_len
Definition: htup.h:64
uintptr_t Datum
Definition: postgres.h:411
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:457
#define BoolGetDatum(X)
Definition: postgres.h:446
struct ExprEvalStep::@49::@60 nulltest_row
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:451

◆ ExecEvalScalarArrayOp()

void ExecEvalScalarArrayOp ( ExprState state,
ExprEvalStep op 
)

Definition at line 3277 of file execExprInterp.c.

References FunctionCallInfoBaseData::args, ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_NDIM, ARR_NULLBITMAP, ArrayGetNItems(), att_addlength_pointer, att_align_nominal, BoolGetDatum, ExprEvalStep::d, DatumGetArrayTypeP, DatumGetBool, fetch_att, get_typlenbyvalalign(), i, FunctionCallInfoBaseData::isnull, NullableDatum::isnull, ExprEvalStep::resnull, ExprEvalStep::resvalue, ExprEvalStep::scalararrayop, typalign, ExprEvalStep::typbyval, ExprEvalStep::typlen, ExprEvalStep::useOr, and NullableDatum::value.

Referenced by ExecInterpExpr().

3278 {
3279  FunctionCallInfo fcinfo = op->d.scalararrayop.fcinfo_data;
3280  bool useOr = op->d.scalararrayop.useOr;
3281  bool strictfunc = op->d.scalararrayop.finfo->fn_strict;
3282  ArrayType *arr;
3283  int nitems;
3284  Datum result;
3285  bool resultnull;
3286  int16 typlen;
3287  bool typbyval;
3288  char typalign;
3289  char *s;
3290  bits8 *bitmap;
3291  int bitmask;
3292 
3293  /*
3294  * If the array is NULL then we return NULL --- it's not very meaningful
3295  * to do anything else, even if the operator isn't strict.
3296  */
3297  if (*op->resnull)
3298  return;
3299 
3300  /* Else okay to fetch and detoast the array */
3301  arr = DatumGetArrayTypeP(*op->resvalue);
3302 
3303  /*
3304  * If the array is empty, we return either FALSE or TRUE per the useOr
3305  * flag. This is correct even if the scalar is NULL; since we would
3306  * evaluate the operator zero times, it matters not whether it would want
3307  * to return NULL.
3308  */
3309  nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
3310  if (nitems <= 0)
3311  {
3312  *op->resvalue = BoolGetDatum(!useOr);
3313  *op->resnull = false;
3314  return;
3315  }
3316 
3317  /*
3318  * If the scalar is NULL, and the function is strict, return NULL; no
3319  * point in iterating the loop.
3320  */
3321  if (fcinfo->args[0].isnull && strictfunc)
3322  {
3323  *op->resnull = true;
3324  return;
3325  }
3326 
3327  /*
3328  * We arrange to look up info about the element type only once per series
3329  * of calls, assuming the element type doesn't change underneath us.
3330  */
3331  if (op->d.scalararrayop.element_type != ARR_ELEMTYPE(arr))
3332  {
3334  &op->d.scalararrayop.typlen,
3335  &op->d.scalararrayop.typbyval,
3336  &op->d.scalararrayop.typalign);
3337  op->d.scalararrayop.element_type = ARR_ELEMTYPE(arr);
3338  }
3339 
3340  typlen = op->d.scalararrayop.typlen;
3341  typbyval = op->d.scalararrayop.typbyval;
3342  typalign = op->d.scalararrayop.typalign;
3343 
3344  /* Initialize result appropriately depending on useOr */
3345  result = BoolGetDatum(!useOr);
3346  resultnull = false;
3347 
3348  /* Loop over the array elements */
3349  s = (char *) ARR_DATA_PTR(arr);
3350  bitmap = ARR_NULLBITMAP(arr);
3351  bitmask = 1;
3352 
3353  for (int i = 0; i < nitems; i++)
3354  {
3355  Datum elt;
3356  Datum thisresult;
3357 
3358  /* Get array element, checking for NULL */
3359  if (bitmap && (*bitmap & bitmask) == 0)
3360  {
3361  fcinfo->args[1].value = (Datum) 0;
3362  fcinfo->args[1].isnull = true;
3363  }
3364  else
3365  {
3366  elt = fetch_att(s, typbyval, typlen);
3367  s = att_addlength_pointer(s, typlen, s);
3368  s = (char *) att_align_nominal(s, typalign);
3369  fcinfo->args[1].value = elt;
3370  fcinfo->args[1].isnull = false;
3371  }
3372 
3373  /* Call comparison function */
3374  if (fcinfo->args[1].isnull && strictfunc)
3375  {
3376  fcinfo->isnull = true;
3377  thisresult = (Datum) 0;
3378  }
3379  else
3380  {
3381  fcinfo->isnull = false;
3382  thisresult = op->d.scalararrayop.fn_addr(fcinfo);
3383  }
3384 
3385  /* Combine results per OR or AND semantics */
3386  if (fcinfo->isnull)
3387  resultnull = true;
3388  else if (useOr)
3389  {
3390  if (DatumGetBool(thisresult))
3391  {
3392  result = BoolGetDatum(true);
3393  resultnull = false;
3394  break; /* needn't look at any more elements */
3395  }
3396  }
3397  else
3398  {
3399  if (!DatumGetBool(thisresult))
3400  {
3401  result = BoolGetDatum(false);
3402  resultnull = false;
3403  break; /* needn't look at any more elements */
3404  }
3405  }
3406 
3407  /* advance bitmap pointer if any */
3408  if (bitmap)
3409  {
3410  bitmask <<= 1;
3411  if (bitmask == 0x100)
3412  {
3413  bitmap++;
3414  bitmask = 1;
3415  }
3416  }
3417  }
3418 
3419  *op->resvalue = result;
3420  *op->resnull = resultnull;
3421 }
signed short int16
Definition: c.h:428
struct ExprEvalStep::@49::@80 scalararrayop
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:148
Datum * resvalue
Definition: execExpr.h:273
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2218
bool * resnull
Definition: execExpr.h:274
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
char typalign
Definition: pg_type.h:176
union ExprEvalStep::@49 d
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
#define ARR_DIMS(a)
Definition: array.h:287
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define DatumGetBool(X)
Definition: postgres.h:437
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:176
Datum value
Definition: postgres.h:422
uint8 bits8
Definition: c.h:448
uintptr_t Datum
Definition: postgres.h:411
#define BoolGetDatum(X)
Definition: postgres.h:446
#define ARR_NDIM(a)
Definition: array.h:283
#define fetch_att(T, attbyval, attlen)
Definition: tupmacs.h:75
int i
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define ARR_NULLBITMAP(a)
Definition: array.h:293
#define DatumGetArrayTypeP(X)
Definition: array.h:254

◆ ExecEvalSQLValueFunction()

void ExecEvalSQLValueFunction ( ExprState state,
ExprEvalStep op 
)

Definition at line 2462 of file execExprInterp.c.

References current_database(), current_schema(), current_user(), ExprEvalStep::d, DateADTGetDatum, GetSQLCurrentDate(), GetSQLCurrentTime(), GetSQLCurrentTimestamp(), GetSQLLocalTime(), GetSQLLocalTimestamp(), InitFunctionCallInfoData, InvalidOid, LOCAL_FCINFO, SQLValueFunction::op, ExprEvalStep::resnull, ExprEvalStep::resvalue, session_user(), ExprEvalStep::sqlvaluefunction, ExprEvalStep::svf, SVFOP_CURRENT_CATALOG, SVFOP_CURRENT_DATE, SVFOP_CURRENT_ROLE, SVFOP_CURRENT_SCHEMA, SVFOP_CURRENT_TIME, SVFOP_CURRENT_TIME_N, SVFOP_CURRENT_TIMESTAMP, SVFOP_CURRENT_TIMESTAMP_N, SVFOP_CURRENT_USER, SVFOP_LOCALTIME, SVFOP_LOCALTIME_N, SVFOP_LOCALTIMESTAMP, SVFOP_LOCALTIMESTAMP_N, SVFOP_SESSION_USER, SVFOP_USER, TimeADTGetDatum, TimestampGetDatum, TimestampTzGetDatum, TimeTzADTPGetDatum, and SQLValueFunction::typmod.

Referenced by ExecInterpExpr().

2463 {
2464  LOCAL_FCINFO(fcinfo, 0);
2465  SQLValueFunction *svf = op->d.sqlvaluefunction.svf;
2466 
2467  *op->resnull = false;
2468 
2469  /*
2470  * Note: current_schema() can return NULL. current_user() etc currently
2471  * cannot, but might as well code those cases the same way for safety.
2472  */
2473  switch (svf->op)
2474  {
2475  case SVFOP_CURRENT_DATE:
2477  break;
2478  case SVFOP_CURRENT_TIME:
2479  case SVFOP_CURRENT_TIME_N:
2481  break;
2485  break;
2486  case SVFOP_LOCALTIME:
2487  case SVFOP_LOCALTIME_N:
2489  break;
2490  case SVFOP_LOCALTIMESTAMP:
2493  break;
2494  case SVFOP_CURRENT_ROLE:
2495  case SVFOP_CURRENT_USER:
2496  case SVFOP_USER:
2497  InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2498  *op->resvalue = current_user(fcinfo);
2499  *op->resnull = fcinfo->isnull;
2500  break;
2501  case SVFOP_SESSION_USER:
2502  InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2503  *op->resvalue = session_user(fcinfo);
2504  *op->resnull = fcinfo->isnull;
2505  break;
2506  case SVFOP_CURRENT_CATALOG:
2507  InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2508  *op->resvalue = current_database(fcinfo);
2509  *op->resnull = fcinfo->isnull;
2510  break;
2511  case SVFOP_CURRENT_SCHEMA:
2512  InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2513  *op->resvalue = current_schema(fcinfo);
2514  *op->resnull = fcinfo->isnull;
2515  break;
2516  }
2517 }
Datum current_schema(PG_FUNCTION_ARGS)
Definition: name.c:279
Datum * resvalue
Definition: execExpr.h:273
bool * resnull
Definition: execExpr.h:274
#define TimeTzADTPGetDatum(X)
Definition: date.h:59
TimestampTz GetSQLCurrentTimestamp(int32 typmod)
Definition: timestamp.c:1598
#define DateADTGetDatum(X)
Definition: date.h:57
DateADT GetSQLCurrentDate(void)
Definition: date.c:301
SQLValueFunctionOp op
Definition: primnodes.h:1179
Datum current_database(PG_FUNCTION_ARGS)
Definition: misc.c:173
union ExprEvalStep::@49 d
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
TimeTzADT * GetSQLCurrentTime(int32 typmod)
Definition: date.c:334
Datum current_user(PG_FUNCTION_ARGS)
Definition: name.c:263
#define TimestampGetDatum(X)
Definition: timestamp.h:31
TimeADT GetSQLLocalTime(int32 typmod)
Definition: date.c:354
Datum session_user(PG_FUNCTION_ARGS)
Definition: name.c:269
#define TimeADTGetDatum(X)
Definition: date.h:58
Timestamp GetSQLLocalTimestamp(int32 typmod)
Definition: timestamp.c:1612
#define InvalidOid
Definition: postgres_ext.h:36
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
struct ExprEvalStep::@49::@66 sqlvaluefunction

◆ ExecEvalStepOp()

ExprEvalOp ExecEvalStepOp ( ExprState state,
ExprEvalStep op 
)

Definition at line 2310 of file execExprInterp.c.

References Assert, EEO_FLAG_DIRECT_THREADED, EEOP_LAST, ExprState::flags, sort-test::key, and ExprEvalStep::opcode.

Referenced by CheckExprStillValid(), and llvm_compile_expr().

2311 {
2312 #if defined(EEO_USE_COMPUTED_GOTO)
2313  if (state->flags & EEO_FLAG_DIRECT_THREADED)
2314  {
2315  ExprEvalOpLookup key;
2316  ExprEvalOpLookup *res;
2317 
2318  key.opcode = (void *) op->opcode;
2319  res = bsearch(&key,
2320  reverse_dispatch_table,
2321  EEOP_LAST /* nmembers */ ,
2322  sizeof(ExprEvalOpLookup),
2323  dispatch_compare_ptr);
2324  Assert(res); /* unknown ops shouldn't get looked up */
2325  return res->op;
2326  }
2327 #endif
2328  return (ExprEvalOp) op->opcode;
2329 }
ExprEvalOp
Definition: execExpr.h:64
#define Assert(condition)
Definition: c.h:804
intptr_t opcode
Definition: execExpr.h:270
uint8 flags
Definition: execnodes.h:65
#define EEO_FLAG_DIRECT_THREADED
Definition: execExpr.h:29

◆ ExecEvalSubPlan()

void ExecEvalSubPlan ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 3913 of file execExprInterp.c.

References check_stack_depth(), ExprEvalStep::d, ExecSubPlan(), ExprEvalStep::resnull, ExprEvalStep::resvalue, ExprEvalStep::sstate, and ExprEvalStep::subplan.

Referenced by ExecInterpExpr().

3914 {
3915  SubPlanState *sstate = op->d.subplan.sstate;
3916 
3917  /* could potentially be nested, so make sure there's enough stack */
3919 
3920  *op->resvalue = ExecSubPlan(sstate, econtext, op->resnull);
3921 }
struct ExprEvalStep::@49::@86 subplan
Datum * resvalue
Definition: execExpr.h:273
bool * resnull
Definition: execExpr.h:274
union ExprEvalStep::@49 d
void check_stack_depth(void)
Definition: postgres.c:3459
Datum ExecSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:62

◆ ExecEvalSysVar()

void ExecEvalSysVar ( ExprState state,
ExprEvalStep op,
ExprContext econtext,
TupleTableSlot slot 
)

Definition at line 4159 of file execExprInterp.c.

References ExprEvalStep::d, elog, ERROR, ExprEvalStep::resnull, ExprEvalStep::resvalue, slot_getsysattr(), unlikely, and ExprEvalStep::var.

Referenced by ExecInterpExpr().

4161 {
4162  Datum d;
4163 
4164  /* slot_getsysattr has sufficient defenses against bad attnums */
4165  d = slot_getsysattr(slot,
4166  op->d.var.attnum,
4167  op->resnull);
4168  *op->resvalue = d;
4169  /* this ought to be unreachable, but it's cheap enough to check */
4170  if (unlikely(*op->resnull))
4171  elog(ERROR, "failed to fetch attribute from slot");
4172 }
Datum * resvalue
Definition: execExpr.h:273
bool * resnull
Definition: execExpr.h:274
struct ExprEvalStep::@49::@51 var
#define ERROR
Definition: elog.h:46
union ExprEvalStep::@49 d
uintptr_t Datum
Definition: postgres.h:411
#define elog(elevel,...)
Definition: elog.h:232
#define unlikely(x)
Definition: c.h:273
static Datum slot_getsysattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:402

◆ ExecEvalWholeRowVar()

void ExecEvalWholeRowVar ( ExprState state,
ExprEvalStep op,
ExprContext econtext 
)

Definition at line 3930 of file execExprInterp.c.

References Assert, BlessTupleDesc(), Alias::colnames, CreateTupleDescCopy(), ExprEvalStep::d, ExprContext::ecxt_estate, ExprContext::ecxt_innertuple, ExprContext::ecxt_outertuple, ExprContext::ecxt_per_query_memory, ExprContext::ecxt_scantuple, RangeTblEntry::eref, ereport, errcode(), errdetail(), errdetail_plural(), errmsg(), ERROR, EState::es_range_table_size, exec_rt_fetch(), ExecFilterJunk(), ExecTypeSetColNames(), format_type_be(), HeapTupleHeaderSetTypeId, HeapTupleHeaderSetTypMod, i, INNER_VAR, InvalidAttrNumber, lookup_rowtype_tupdesc_domain(), MemoryContextSwitchTo(), TupleDescData::natts, OUTER_VAR, PointerGetDatum, ReleaseTupleDesc, ExprEvalStep::resnull, ExprEvalStep::resvalue, slot_getallattrs(), HeapTupleData::t_data, toast_build_flattened_tuple(), TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, TupleTableSlot::tts_values, TupleDescAttr, Var::varattno, Var::varno, Var::vartype, and ExprEvalStep::wholerow.

Referenced by ExecInterpExpr().

3931 {
3932  Var *variable = op->d.wholerow.var;
3933  TupleTableSlot *slot;
3934  TupleDesc output_tupdesc;
3935  MemoryContext oldcontext;
3936  HeapTupleHeader dtuple;
3937  HeapTuple tuple;
3938 
3939  /* This was checked by ExecInitExpr */
3940  Assert(variable->varattno == InvalidAttrNumber);
3941 
3942  /* Get the input slot we want */
3943  switch (variable->varno)
3944  {
3945  case INNER_VAR:
3946  /* get the tuple from the inner node */
3947  slot = econtext->ecxt_innertuple;
3948  break;
3949 
3950  case OUTER_VAR:
3951  /* get the tuple from the outer node */
3952  slot = econtext->ecxt_outertuple;
3953  break;
3954 
3955  /* INDEX_VAR is handled by default case */
3956 
3957  default:
3958  /* get the tuple from the relation being scanned */
3959  slot = econtext->ecxt_scantuple;
3960  break;
3961  }
3962 
3963  /* Apply the junkfilter if any */
3964  if (op->d.wholerow.junkFilter != NULL)
3965  slot = ExecFilterJunk(op->d.wholerow.junkFilter, slot);
3966 
3967  /*
3968  * If first time through, obtain tuple descriptor and check compatibility.
3969  *
3970  * XXX: It'd be great if this could be moved to the expression
3971  * initialization phase, but due to using slots that's currently not
3972  * feasible.
3973  */
3974  if (op->d.wholerow.first)
3975  {
3976  /* optimistically assume we don't need slow path */
3977  op->d.wholerow.slow = false;
3978 
3979  /*
3980  * If the Var identifies a named composite type, we must check that
3981  * the actual tuple type is compatible with it.
3982  */
3983  if (variable->vartype != RECORDOID)
3984  {
3985  TupleDesc var_tupdesc;
3986  TupleDesc slot_tupdesc;
3987 
3988  /*
3989  * We really only care about numbers of attributes and data types.
3990  * Also, we can ignore type mismatch on columns that are dropped
3991  * in the destination type, so long as (1) the physical storage
3992  * matches or (2) the actual column value is NULL. Case (1) is
3993  * helpful in some cases involving out-of-date cached plans, while
3994  * case (2) is expected behavior in situations such as an INSERT
3995  * into a table with dropped columns (the planner typically
3996  * generates an INT4 NULL regardless of the dropped column type).
3997  * If we find a dropped column and cannot verify that case (1)
3998  * holds, we have to use the slow path to check (2) for each row.
3999  *
4000  * If vartype is a domain over composite, just look through that
4001  * to the base composite type.
4002  */
4003  var_tupdesc = lookup_rowtype_tupdesc_domain(variable->vartype,
4004  -1, false);
4005 
4006  slot_tupdesc = slot->tts_tupleDescriptor;
4007 
4008  if (var_tupdesc->natts != slot_tupdesc->natts)
4009  ereport(ERROR,
4010  (errcode(ERRCODE_DATATYPE_MISMATCH),
4011  errmsg("table row type and query-specified row type do not match"),
4012  errdetail_plural("Table row contains %d attribute, but query expects %d.",
4013  "Table row contains %d attributes, but query expects %d.",
4014  slot_tupdesc->natts,
4015  slot_tupdesc->natts,
4016  var_tupdesc->natts)));
4017 
4018  for (int i = 0; i < var_tupdesc->natts; i++)
4019  {
4020  Form_pg_attribute vattr = TupleDescAttr(var_tupdesc, i);
4021  Form_pg_attribute sattr = TupleDescAttr(slot_tupdesc, i);
4022 
4023  if (vattr->atttypid == sattr->atttypid)
4024  continue; /* no worries */
4025  if (!vattr->attisdropped)
4026  ereport(ERROR,
4027  (errcode(ERRCODE_DATATYPE_MISMATCH),
4028  errmsg("table row type and query-specified row type do not match"),
4029  errdetail("Table has type %s at ordinal position %d, but query expects %s.",
4030  format_type_be(sattr->atttypid),
4031  i + 1,
4032  format_type_be(vattr->atttypid))));
4033 
4034  if (vattr->attlen != sattr->attlen ||
4035  vattr->attalign != sattr->attalign)
4036  op->d.wholerow.slow = true; /* need to check for nulls */
4037  }
4038 
4039  /*
4040  * Use the variable's declared rowtype as the descriptor for the
4041  * output values, modulo possibly assigning new column names
4042  * below. In particular, we *must* absorb any attisdropped
4043  * markings.
4044  */
4045  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
4046  output_tupdesc = CreateTupleDescCopy(var_tupdesc);
4047  MemoryContextSwitchTo(oldcontext);
4048 
4049  ReleaseTupleDesc(var_tupdesc);
4050  }
4051  else
4052  {
4053  /*
4054  * In the RECORD case, we use the input slot's rowtype as the
4055  * descriptor for the output values, modulo possibly assigning new
4056  * column names below.
4057  */
4058  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
4059  output_tupdesc = CreateTupleDescCopy(slot->tts_tupleDescriptor);
4060  MemoryContextSwitchTo(oldcontext);
4061  }
4062 
4063  /*
4064  * Construct a tuple descriptor for the composite values we'll
4065  * produce, and make sure its record type is "blessed". The main
4066  * reason to do this is to be sure that operations such as
4067  * row_to_json() will see the desired column names when they look up
4068  * the descriptor from the type information embedded in the composite
4069  * values.
4070  *
4071  * We already got the correct physical datatype info above, but now we
4072  * should try to find the source RTE and adopt its column aliases, in
4073  * case they are different from the original rowtype's names. For
4074  * example, in "SELECT foo(t) FROM tab t(x,y)", the first two columns
4075  * in the composite output should be named "x" and "y" regardless of
4076  * tab's column names.
4077  *
4078  * If we can't locate the RTE, assume the column names we've got are
4079  * OK. (As of this writing, the only cases where we can't locate the
4080  * RTE are in execution of trigger WHEN clauses, and then the Var will
4081  * have the trigger's relation's rowtype, so its names are fine.)
4082  * Also, if the creator of the RTE didn't bother to fill in an eref
4083  * field, assume our column names are OK. (This happens in COPY, and
4084  * perhaps other places.)
4085  */
4086  if (econtext->ecxt_estate &&
4087  variable->varno <= econtext->ecxt_estate->es_range_table_size)
4088  {
4089  RangeTblEntry *rte = exec_rt_fetch(variable->varno,
4090  econtext->ecxt_estate);
4091 
4092  if (rte->eref)
4093  ExecTypeSetColNames(output_tupdesc, rte->eref->colnames);
4094  }
4095 
4096  /* Bless the tupdesc if needed, and save it in the execution state */
4097  op->d.wholerow.tupdesc = BlessTupleDesc(output_tupdesc);
4098 
4099  op->d.wholerow.first = false;
4100  }
4101 
4102  /*
4103  * Make sure all columns of the slot are accessible in the slot's
4104  * Datum/isnull arrays.
4105  */
4106  slot_getallattrs(slot);
4107 
4108  if (op->d.wholerow.slow)
4109  {
4110  /* Check to see if any dropped attributes are non-null */
4111  TupleDesc tupleDesc = slot->tts_tupleDescriptor;
4112  TupleDesc var_tupdesc = op->d.wholerow.tupdesc;
4113 
4114  Assert(var_tupdesc->natts == tupleDesc->natts);
4115 
4116  for (int i = 0; i < var_tupdesc->natts; i++)
4117  {
4118  Form_pg_attribute vattr = TupleDescAttr(var_tupdesc, i);
4119  Form_pg_attribute sattr = TupleDescAttr(tupleDesc, i);
4120 
4121  if (!vattr->attisdropped)
4122  continue; /* already checked non-dropped cols */
4123  if (slot->tts_isnull[i])
4124  continue; /* null is always okay */
4125  if (vattr->attlen != sattr->attlen ||
4126  vattr->attalign != sattr->attalign)
4127  ereport(ERROR,
4128  (errcode(ERRCODE_DATATYPE_MISMATCH),
4129  errmsg("table row type and query-specified row type do not match"),
4130  errdetail("Physical storage mismatch on dropped attribute at ordinal position %d.",
4131  i + 1)));
4132  }
4133  }
4134 
4135  /*
4136  * Build a composite datum, making sure any toasted fields get detoasted.
4137  *
4138  * (Note: it is critical that we not change the slot's state here.)
4139  */
4141  slot->tts_values,
4142  slot->tts_isnull);
4143  dtuple = tuple->t_data;
4144 
4145  /*
4146  * Label the datum with the composite type info we identified before.
4147  *
4148  * (Note: we could skip doing this by passing op->d.wholerow.tupdesc to
4149  * the tuple build step; but that seems a tad risky so let's not.)
4150  */
4151  HeapTupleHeaderSetTypeId(dtuple, op->d.wholerow.tupdesc->tdtypeid);
4152  HeapTupleHeaderSetTypMod(dtuple, op->d.wholerow.tupdesc->tdtypmod);
4153 
4154  *op->resvalue = PointerGetDatum(dtuple);
4155  *op->resnull = false;
4156 }
struct ExprEvalStep::@49::@52 wholerow
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111
#define HeapTupleHeaderSetTypeId(tup, typeid)
Definition: htup_details.h:462
Datum * resvalue
Definition: execExpr.h:273
List * colnames
Definition: primnodes.h:43
#define PointerGetDatum(X)
Definition: postgres.h:600
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
bool * resnull
Definition: execExpr.h:274
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
AttrNumber varattno
Definition: primnodes.h:191
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
Datum * tts_values
Definition: tuptable.h:126
Definition: primnodes.h:186
TupleDesc lookup_rowtype_tupdesc_domain(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1882
HeapTupleHeader t_data
Definition: htup.h:68
#define ERROR
Definition: elog.h:46
union ExprEvalStep::@49 d
Oid vartype
Definition: primnodes.h:193
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:354
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2082
HeapTuple toast_build_flattened_tuple(TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptoast.c:563
bool * tts_isnull
Definition: tuptable.h:128
int errdetail(const char *fmt,...)
Definition: elog.c:1042
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:228
struct EState * ecxt_estate
Definition: execnodes.h:262
static RangeTblEntry * exec_rt_fetch(Index rti, EState *estate)
Definition: executor.h:570
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:203
Index varno
Definition: primnodes.h:189
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
TupleTableSlot * ExecFilterJunk(JunkFilter *junkfilter, TupleTableSlot *slot)
Definition: execJunk.c:247
#define HeapTupleHeaderSetTypMod(tup, typmod)
Definition: htup_details.h:472
#define ereport(elevel,...)
Definition: elog.h:157
#define INNER_VAR
Definition: primnodes.h:175
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:230
#define Assert(condition)
Definition: c.h:804
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1134
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:226
Index es_range_table_size
Definition: execnodes.h:559
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:233
#define InvalidAttrNumber
Definition: attnum.h:23
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
Alias * eref
Definition: parsenodes.h:1141
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
#define OUTER_VAR
Definition: primnodes.h:176
void ExecTypeSetColNames(TupleDesc typeInfo, List *namesList)
Definition: execTuples.c:2036

◆ ExecEvalXmlExpr()

void ExecEvalXmlExpr ( ExprState state,
ExprEvalStep op 
)

Definition at line 3669 of file execExprInterp.c.

References appendStringInfo(), arg, XmlExpr::arg_names, ExprEvalStep::argnull, XmlExpr::args, ExprEvalStep::argvalue, Assert, BoolGetDatum, buf, cstring_to_text_with_len(), ExprEvalStep::d, StringInfoData::data, DatumGetBool, DatumGetInt32, DatumGetPointer, DatumGetTextPP, DatumGetXmlP, elog, ERROR, exprType(), forboth, i, initStringInfo(), IS_DOCUMENT, IS_XMLCONCAT, IS_XMLELEMENT, IS_XMLFOREST, IS_XMLPARSE, IS_XMLPI, IS_XMLROOT, IS_XMLSERIALIZE, ExprEvalStep::isnull, lappend(), StringInfoData::len, lfirst, list_length(), map_sql_value_to_xml_value(), XmlExpr::name, XmlExpr::named_args, NIL, XmlExpr::op, pfree(), PointerGetDatum, ExprEvalStep::resnull, ExprEvalStep::resvalue, strVal, value, values, ExprEvalStep::xexpr, xml_is_document(), xmlconcat(), xmlelement(), ExprEvalStep::xmlexpr, XmlExpr::xmloption, xmlparse(), xmlpi(), xmlroot(), and xmltotext_with_xmloption().

Referenced by ExecInterpExpr().

3670 {
3671  XmlExpr *xexpr = op->d.xmlexpr.xexpr;
3672  Datum value;
3673 
3674  *op->resnull = true; /* until we get a result */
3675  *op->resvalue = (Datum) 0;
3676 
3677  switch (xexpr->op)
3678  {
3679  case IS_XMLCONCAT:
3680  {
3681  Datum *argvalue = op->d.xmlexpr.argvalue;
3682  bool *argnull = op->d.xmlexpr.argnull;
3683  List *values = NIL;
3684 
3685  for (int i = 0; i < list_length(xexpr->args); i++)
3686  {
3687  if (!argnull[i])
3688  values = lappend(values, DatumGetPointer(argvalue[i]));
3689  }
3690 
3691  if (values != NIL)
3692  {
3693  *op->resvalue = PointerGetDatum(xmlconcat(values));
3694  *op->resnull = false;
3695  }
3696  }
3697  break;
3698 
3699  case IS_XMLFOREST:
3700  {
3701  Datum *argvalue = op->d.xmlexpr.named_argvalue;
3702  bool *argnull = op->d.xmlexpr.named_argnull;
3704  ListCell *lc;
3705  ListCell *lc2;
3706  int i;
3707 
3708  initStringInfo(&buf);
3709 
3710  i = 0;
3711  forboth(lc, xexpr->named_args, lc2, xexpr->arg_names)
3712  {
3713  Expr *e = (Expr *) lfirst(lc);
3714  char *argname = strVal(lfirst(lc2));
3715 
3716  if (!argnull[i])
3717  {
3718  value = argvalue[i];
3719  appendStringInfo(&buf, "<%s>%s</%s>",
3720  argname,
3722  exprType((Node *) e), true),
3723  argname);
3724  *op->resnull = false;
3725  }
3726  i++;
3727  }
3728 
3729  if (!*op->resnull)
3730  {
3731  text *result;
3732 
3733  result = cstring_to_text_with_len(buf.data, buf.len);
3734  *op->resvalue = PointerGetDatum(result);
3735  }
3736 
3737  pfree(buf.data);
3738  }
3739  break;
3740 
3741  case IS_XMLELEMENT:
3742  *op->resvalue = PointerGetDatum(xmlelement(xexpr,
3743  op->d.xmlexpr.named_argvalue,
3744  op->d.xmlexpr.named_argnull,
3745  op->d.xmlexpr.argvalue,
3746  op->d.xmlexpr.argnull));
3747  *op->resnull = false;
3748  break;
3749 
3750  case IS_XMLPARSE:
3751  {
3752  Datum *argvalue = op->d.xmlexpr.argvalue;
3753  bool *argnull = op->d.xmlexpr.argnull;
3754  text *data;
3755  bool preserve_whitespace;
3756 
3757  /* arguments are known to be text, bool */
3758  Assert(list_length(xexpr->args) == 2);
3759 
3760  if (argnull[0])
3761  return;
3762  value = argvalue[0];
3763  data = DatumGetTextPP(value);
3764 
3765  if (argnull[1]) /* probably can't happen */
3766  return;
3767  value = argvalue[1];
3768  preserve_whitespace = DatumGetBool(value);
3769 
3770  *op->resvalue = PointerGetDatum(xmlparse(data,
3771  xexpr->xmloption,
3772  preserve_whitespace));
3773  *op->resnull = false;
3774  }
3775  break;
3776 
3777  case IS_XMLPI:
3778  {
3779  text *arg;
3780  bool isnull;
3781 
3782  /* optional argument is known to be text */
3783  Assert(list_length(xexpr->args) <= 1);
3784 
3785  if (xexpr->args)
3786  {
3787  isnull = op->d.xmlexpr.argnull[0];
3788  if (isnull)
3789  arg = NULL;
3790  else
3791  arg = DatumGetTextPP(op->d.xmlexpr.argvalue[0]);
3792  }
3793  else
3794  {
3795  arg = NULL;
3796  isnull = false;
3797  }
3798 
3799  *op->resvalue = PointerGetDatum(xmlpi(xexpr->name,
3800  arg,
3801  isnull,
3802  op->resnull));
3803  }
3804  break;
3805 
3806  case IS_XMLROOT:
3807  {
3808  Datum *argvalue = op->d.xmlexpr.argvalue;
3809  bool *argnull = op->d.xmlexpr.argnull;
3810  xmltype *data;
3811  text *version;
3812  int standalone;
3813 
3814  /* arguments are known to be xml, text, int */
3815  Assert(list_length(xexpr->args) == 3);
3816 
3817  if (argnull[0])
3818  return;
3819  data = DatumGetXmlP(argvalue[0]);
3820 
3821  if (argnull[1])
3822  version = NULL;
3823  else
3824  version = DatumGetTextPP(argvalue[1]);
3825 
3826  Assert(!argnull[2]); /* always present */
3827  standalone = DatumGetInt32(argvalue[2]);
3828 
3829  *op->resvalue = PointerGetDatum(xmlroot(data,
3830  version,
3831  standalone));
3832  *op->resnull = false;
3833  }
3834  break;
3835 
3836  case IS_XMLSERIALIZE:
3837  {
3838  Datum *argvalue = op->d.xmlexpr.argvalue;
3839  bool *argnull = op->d.xmlexpr.argnull;
3840 
3841  /* argument type is known to be xml */
3842  Assert(list_length(xexpr->args) == 1);
3843 
3844  if (argnull[0])
3845  return;
3846  value = argvalue[0];
3847 
3849  xexpr->xmloption));
3850  *op->resnull = false;
3851  }
3852  break;
3853 
3854  case IS_DOCUMENT:
3855  {
3856  Datum *argvalue = op->d.xmlexpr.argvalue;
3857  bool *argnull = op->d.xmlexpr.argnull;
3858 
3859  /* optional argument is known to be xml */
3860  Assert(list_length(xexpr->args) == 1);
3861 
3862  if (argnull[0])
3863  return;
3864  value = argvalue[0];
3865 
3866  *op->resvalue =
3868  *op->resnull = false;
3869  }
3870  break;
3871 
3872  default:
3873  elog(ERROR, "unrecognized XML operation");
3874  break;
3875  }
3876 }
#define NIL
Definition: pg_list.h:65
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
char * name
Definition: primnodes.h:1218
static struct @142 value
Datum * resvalue
Definition: execExpr.h:273
#define DatumGetInt32(X)
Definition: postgres.h:516
#define PointerGetDatum(X)
Definition: postgres.h:600
#define DatumGetTextPP(X)
Definition: fmgr.h:292
bool * resnull
Definition: execExpr.h:274
xmltype * xmlconcat(List *args)
Definition: xml.c:512
Definition: nodes.h:539
#define strVal(v)
Definition: value.h:54
List * arg_names
Definition: primnodes.h:1220
#define DatumGetXmlP(X)
Definition: xml.h:50
xmltype * xmlpi(const char *target, text *arg, bool arg_is_null, bool *result_is_null)
Definition: xml.c:770
void pfree(void *pointer)
Definition: mcxt.c:1169
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ERROR
Definition: elog.h:46
union ExprEvalStep::@49 d
text * xmltotext_with_xmloption(xmltype *data, XmlOptionType xmloption_arg)
Definition: xml.c:615
static char * buf
Definition: pg_test_fsync.c:68
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:202
xmltype * xmlroot(xmltype *data, text *version, int standalone)
Definition: xml.c:822
#define DatumGetBool(X)
Definition: postgres.h:437
List * lappend(List *list, void *datum)
Definition: list.c:336
bool xml_is_document(xmltype *arg)
Definition: xml.c:888
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
XmlExprOp op
Definition: primnodes.h:1217
uintptr_t Datum
Definition: postgres.h:411
#define BoolGetDatum(X)
Definition: postgres.h:446
List * named_args
Definition: primnodes.h:1219
List * args
Definition: primnodes.h:1221
struct ExprEvalStep::@49::@82 xmlexpr
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
XmlOptionType xmloption
Definition: primnodes.h:1222
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:149
#define DatumGetPointer(X)
Definition: postgres.h:593
static Datum values[MAXATTR]
Definition: bootstrap.c:166
e
Definition: preproc-init.c:82
#define elog(elevel,...)
Definition: elog.h:232
int i
xmltype * xmlelement(XmlExpr *xexpr, Datum *named_argvalue, bool *named_argnull, Datum *argvalue, bool *argnull)
Definition: xml.c:628
void * arg
Definition: c.h:621
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Definition: xml.c:2134
Definition: pg_list.h:50
xmltype * xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
Definition: xml.c:752

◆ ExecInitInterpreter()

static void ExecInitInterpreter ( void  )
static

Definition at line 2278 of file execExprInterp.c.

References DatumGetPointer, EEOP_LAST, ExecInterpExpr(), i, and qsort.

Referenced by ExecReadyInterpretedExpr().

2279 {
2280 #if defined(EEO_USE_COMPUTED_GOTO)
2281  /* Set up externally-visible pointer to dispatch table */
2282  if (dispatch_table == NULL)
2283  {
2284  dispatch_table = (const void **)
2285  DatumGetPointer(ExecInterpExpr(NULL, NULL, NULL));
2286 
2287  /* build reverse lookup table */
2288  for (int i = 0; i < EEOP_LAST; i++)
2289  {
2290  reverse_dispatch_table[i].opcode = dispatch_table[i];
2291  reverse_dispatch_table[i].op = (ExprEvalOp) i;
2292  }
2293 
2294  /* make it bsearch()able */
2295  qsort(reverse_dispatch_table,
2296  EEOP_LAST /* nmembers */ ,
2297  sizeof(ExprEvalOpLookup),
2298  dispatch_compare_ptr);
2299  }
2300 #endif
2301 }
ExprEvalOp
Definition: execExpr.h:64
static Datum ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
#define DatumGetPointer(X)
Definition: postgres.h:593
int i
#define qsort(a, b, c, d)
Definition: port.h:504

◆ ExecInterpExpr()

static Datum ExecInterpExpr ( ExprState state,
ExprContext econtext,
bool isnull 
)
static

Definition at line 390 of file execExprInterp.c.

References ExprEvalStep::agg_deserialize, ExprEvalStep::agg_plain_pergroup_nullcheck, ExprEvalStep::agg_strict_input_check, ExprEvalStep::agg_trans, ExprEvalStep::aggno, ExprEvalStep::aggref, AggState::all_pergroups, FunctionCallInfoBaseData::args, generate_unaccent_rules::args, Assert, ExprEvalStep::assign_tmp, ExprEvalStep::assign_var, attnum, ExprEvalStep::boolexpr, BoolGetDatum, ExprEvalStep::casetest, ExprContext::caseValue_datum, ExprContext::caseValue_isNull, castNode, CheckOpSlotCompatibility(), ExprEvalStep::constval, ExprEvalStep::cparam, ExprEvalStep::d, DatumGetBool, DatumGetCString, DatumGetInt32, ExprContext::domainValue_datum, ExprContext::domainValue_isNull, ExprContext::ecxt_aggnulls, ExprContext::ecxt_aggvalues, ExprContext::ecxt_innertuple, ExprContext::ecxt_outertuple, ExprContext::ecxt_per_tuple_memory, ExprContext::ecxt_scantuple, EEO_CASE, EEO_DISPATCH, EEO_JUMP, EEO_NEXT, EEO_SWITCH, EEOP_AGG_DESERIALIZE, EEOP_AGG_ORDERED_TRANS_DATUM, EEOP_AGG_ORDERED_TRANS_TUPLE, EEOP_AGG_PLAIN_PERGROUP_NULLCHECK, EEOP_AGG_PLAIN_TRANS_BYREF, EEOP_AGG_PLAIN_TRANS_BYVAL, EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF, EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL, EEOP_AGG_PLAIN_TRANS_STRICT_BYREF, EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL, EEOP_AGG_STRICT_DESERIALIZE, EEOP_AGG_STRICT_INPUT_CHECK_ARGS, EEOP_AGG_STRICT_INPUT_CHECK_NULLS, EEOP_AGGREF, EEOP_ARRAYCOERCE, EEOP_ARRAYEXPR, EEOP_ASSIGN_INNER_VAR, EEOP_ASSIGN_OUTER_VAR, EEOP_ASSIGN_SCAN_VAR, EEOP_ASSIGN_TMP, EEOP_ASSIGN_TMP_MAKE_RO, EEOP_BOOL_AND_STEP, EEOP_BOOL_AND_STEP_FIRST, EEOP_BOOL_AND_STEP_LAST, EEOP_BOOL_NOT_STEP, EEOP_BOOL_OR_STEP, EEOP_BOOL_OR_STEP_FIRST, EEOP_BOOL_OR_STEP_LAST, EEOP_BOOLTEST_IS_FALSE, EEOP_BOOLTEST_IS_NOT_FALSE, EEOP_BOOLTEST_IS_NOT_TRUE, EEOP_BOOLTEST_IS_TRUE, EEOP_CASE_TESTVAL, EEOP_CONST, EEOP_CONVERT_ROWTYPE, EEOP_CURRENTOFEXPR, EEOP_DISTINCT, EEOP_DOMAIN_CHECK, EEOP_DOMAIN_NOTNULL, EEOP_DOMAIN_TESTVAL, EEOP_DONE, EEOP_FIELDSELECT, EEOP_FIELDSTORE_DEFORM, EEOP_FIELDSTORE_FORM, EEOP_FUNCEXPR, EEOP_FUNCEXPR_FUSAGE, EEOP_FUNCEXPR_STRICT, EEOP_FUNCEXPR_STRICT_FUSAGE, EEOP_GROUPING_FUNC, EEOP_HASHED_SCALARARRAYOP, EEOP_INNER_FETCHSOME, EEOP_INNER_SYSVAR, EEOP_INNER_VAR, EEOP_IOCOERCE, EEOP_JUMP, EEOP_JUMP_IF_NOT_NULL, EEOP_JUMP_IF_NOT_TRUE, EEOP_JUMP_IF_NULL, EEOP_LAST, EEOP_MAKE_READONLY, EEOP_MINMAX, EEOP_NEXTVALUEEXPR, EEOP_NOT_DISTINCT, EEOP_NULLIF, EEOP_NULLTEST_ISNOTNULL, EEOP_NULLTEST_ISNULL, EEOP_NULLTEST_ROWISNOTNULL, EEOP_NULLTEST_ROWISNULL, EEOP_OUTER_FETCHSOME, EEOP_OUTER_SYSVAR, EEOP_OUTER_VAR, EEOP_PARAM_CALLBACK, EEOP_PARAM_EXEC, EEOP_PARAM_EXTERN, EEOP_QUAL, EEOP_ROW, EEOP_ROWCOMPARE_FINAL, EEOP_ROWCOMPARE_STEP, EEOP_SBSREF_ASSIGN, EEOP_SBSREF_FETCH, EEOP_SBSREF_OLD, EEOP_SBSREF_SUBSCRIPTS, EEOP_SCALARARRAYOP, EEOP_SCAN_FETCHSOME, EEOP_SCAN_SYSVAR, EEOP_SCAN_VAR, EEOP_SQLVALUEFUNCTION, EEOP_SUBPLAN, EEOP_WHOLEROW, EEOP_WINDOW_FUNC, EEOP_XMLEXPR, ExecAggInitGroup(), ExecAggPlainTransByRef(), ExecAggPlainTransByVal(), ExecEvalAggOrderedTransDatum(), ExecEvalAggOrderedTransTuple(), ExecEvalArrayCoerce(), ExecEvalArrayExpr(), ExecEvalConstraintCheck(), ExecEvalConstraintNotNull(), ExecEvalConvertRowtype(), ExecEvalCurrentOfExpr(), ExecEvalFieldSelect(), ExecEvalFieldStoreDeForm(), ExecEvalFieldStoreForm(), ExecEvalFuncExprFusage(), ExecEvalFuncExprStrictFusage(), ExecEvalGroupingFunc(), ExecEvalHashedScalarArrayOp(), ExecEvalMinMax(), ExecEvalNextValueExpr(), ExecEvalParamExec(), ExecEvalParamExtern(), ExecEvalRow(), ExecEvalRowNotNull(), ExecEvalRowNull(), ExecEvalScalarArrayOp(), ExecEvalSQLValueFunction(), ExecEvalSubPlan(), ExecEvalSysVar(), ExecEvalWholeRowVar(), ExecEvalXmlExpr(), ExprEvalStep::fetch, ExprEvalStep::func, FunctionCallInvoke, ExprEvalStep::iocoerce, FunctionCallInfoBaseData::isnull, NullableDatum::isnull, ExprEvalStep::jump, lengthof, likely, ExprEvalStep::make_readonly, MakeExpandedObjectReadOnlyInternal(), MemoryContextSwitchTo(), ExprEvalStep::nargs, AggStatePerGroupData::noTransValue, ExprEvalStep::nulls, ExprEvalStep::op, ExprState::parent, ExprEvalStep::pertrans, PointerGetDatum, ExprEvalStep::qualexpr, ExprEvalStep::rctype, ExprState::resnull, ExprEvalStep::resnull, ExprEvalStep::resultnum, ExprState::resultslot, ExprState::resvalue, ExprEvalStep::resvalue, ExprEvalStep::rowcompare_final, ROWCOMPARE_GE, ROWCOMPARE_GT, ROWCOMPARE_LE, ROWCOMPARE_LT, ExprEvalStep::rowcompare_step, ExprEvalStep::sbsref, ExprEvalStep::sbsref_subscript, slot_getsomeattrs(), StaticAssertStmt, ExprState::steps, generate_unaccent_rules::str, AggState::tmpcontext, AggStatePerTransData::transtypeByVal, AggStatePerGroupData::transValueIsNull, TupleTableSlot::tts_isnull, TupleTableSlot::tts_values, unlikely, NullableDatum::value, ExprEvalStep::var, WindowFuncExprState::wfuncno, and ExprEvalStep::window_func.

Referenced by ExecInitInterpreter(), and ExecReadyInterpretedExpr().

391 {
392  ExprEvalStep *op;
393  TupleTableSlot *resultslot;
394  TupleTableSlot *innerslot;
395  TupleTableSlot *outerslot;
396  TupleTableSlot *scanslot;
397 
398  /*
399  * This array has to be in the same order as enum ExprEvalOp.
400  */
401 #if defined(EEO_USE_COMPUTED_GOTO)
402  static const void *const dispatch_table[] = {
403  &&CASE_EEOP_DONE,
404  &&CASE_EEOP_INNER_FETCHSOME,
405  &&CASE_EEOP_OUTER_FETCHSOME,
406  &&CASE_EEOP_SCAN_FETCHSOME,
407  &&CASE_EEOP_INNER_VAR,
408  &&CASE_EEOP_OUTER_VAR,
409  &&CASE_EEOP_SCAN_VAR,
410  &&CASE_EEOP_INNER_SYSVAR,
411  &&CASE_EEOP_OUTER_SYSVAR,
412  &&CASE_EEOP_SCAN_SYSVAR,
413  &&CASE_EEOP_WHOLEROW,
414  &&CASE_EEOP_ASSIGN_INNER_VAR,
415  &&CASE_EEOP_ASSIGN_OUTER_VAR,
416  &&CASE_EEOP_ASSIGN_SCAN_VAR,
417  &&CASE_EEOP_ASSIGN_TMP,
418  &&CASE_EEOP_ASSIGN_TMP_MAKE_RO,
419  &&CASE_EEOP_CONST,
420  &&CASE_EEOP_FUNCEXPR,
421  &&CASE_EEOP_FUNCEXPR_STRICT,
422  &&CASE_EEOP_FUNCEXPR_FUSAGE,
423  &&CASE_EEOP_FUNCEXPR_STRICT_FUSAGE,
424  &&CASE_EEOP_BOOL_AND_STEP_FIRST,
425  &&CASE_EEOP_BOOL_AND_STEP,
426  &&CASE_EEOP_BOOL_AND_STEP_LAST,
427  &&CASE_EEOP_BOOL_OR_STEP_FIRST,
428  &&CASE_EEOP_BOOL_OR_STEP,
429  &&CASE_EEOP_BOOL_OR_STEP_LAST,
430  &&CASE_EEOP_BOOL_NOT_STEP,
431  &&CASE_EEOP_QUAL,
432  &&CASE_EEOP_JUMP,
433  &&CASE_EEOP_JUMP_IF_NULL,
434  &&CASE_EEOP_JUMP_IF_NOT_NULL,
435  &&CASE_EEOP_JUMP_IF_NOT_TRUE,
436  &&CASE_EEOP_NULLTEST_ISNULL,
437  &&CASE_EEOP_NULLTEST_ISNOTNULL,
438  &&CASE_EEOP_NULLTEST_ROWISNULL,
439  &&CASE_EEOP_NULLTEST_ROWISNOTNULL,
440  &&CASE_EEOP_BOOLTEST_IS_TRUE,
441  &&CASE_EEOP_BOOLTEST_IS_NOT_TRUE,
442  &&CASE_EEOP_BOOLTEST_IS_FALSE,
443  &&CASE_EEOP_BOOLTEST_IS_NOT_FALSE,
444  &&CASE_EEOP_PARAM_EXEC,
445  &&CASE_EEOP_PARAM_EXTERN,
446  &&CASE_EEOP_PARAM_CALLBACK,
447  &&CASE_EEOP_CASE_TESTVAL,
448  &&CASE_EEOP_MAKE_READONLY,
449  &&CASE_EEOP_IOCOERCE,
450  &&CASE_EEOP_DISTINCT,
451  &&CASE_EEOP_NOT_DISTINCT,
452  &&CASE_EEOP_NULLIF,
453  &&CASE_EEOP_SQLVALUEFUNCTION,
454  &&CASE_EEOP_CURRENTOFEXPR,
455  &&CASE_EEOP_NEXTVALUEEXPR,
456  &&CASE_EEOP_ARRAYEXPR,
457  &&CASE_EEOP_ARRAYCOERCE,
458  &&CASE_EEOP_ROW,
459  &&CASE_EEOP_ROWCOMPARE_STEP,
460  &&CASE_EEOP_ROWCOMPARE_FINAL,
461  &&CASE_EEOP_MINMAX,
462  &&CASE_EEOP_FIELDSELECT,
463  &&CASE_EEOP_FIELDSTORE_DEFORM,
464  &&CASE_EEOP_FIELDSTORE_FORM,
465  &&CASE_EEOP_SBSREF_SUBSCRIPTS,
466  &&CASE_EEOP_SBSREF_OLD,
467  &&CASE_EEOP_SBSREF_ASSIGN,
468  &&CASE_EEOP_SBSREF_FETCH,
469  &&CASE_EEOP_DOMAIN_TESTVAL,
470  &&CASE_EEOP_DOMAIN_NOTNULL,
471  &&CASE_EEOP_DOMAIN_CHECK,
472  &&CASE_EEOP_CONVERT_ROWTYPE,
473  &&CASE_EEOP_SCALARARRAYOP,
474  &&CASE_EEOP_HASHED_SCALARARRAYOP,
475  &&CASE_EEOP_XMLEXPR,
476  &&CASE_EEOP_AGGREF,
477  &&CASE_EEOP_GROUPING_FUNC,
478  &&CASE_EEOP_WINDOW_FUNC,
479  &&CASE_EEOP_SUBPLAN,
480  &&CASE_EEOP_AGG_STRICT_DESERIALIZE,
481  &&CASE_EEOP_AGG_DESERIALIZE,
482  &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_ARGS,
483  &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_NULLS,
484  &&CASE_EEOP_AGG_PLAIN_PERGROUP_NULLCHECK,
485  &&CASE_EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL,
486  &&CASE_EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL,
487  &&CASE_EEOP_AGG_PLAIN_TRANS_BYVAL,
488  &&CASE_EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF,
489  &&CASE_EEOP_AGG_PLAIN_TRANS_STRICT_BYREF,
490  &&CASE_EEOP_AGG_PLAIN_TRANS_BYREF,
491  &&CASE_EEOP_AGG_ORDERED_TRANS_DATUM,
492  &&CASE_EEOP_AGG_ORDERED_TRANS_TUPLE,
493  &&CASE_EEOP_LAST
494  };
495 
496  StaticAssertStmt(EEOP_LAST + 1 == lengthof(dispatch_table),
497  "dispatch_table out of whack with ExprEvalOp");
498 
499  if (unlikely(state == NULL))
500  return PointerGetDatum(dispatch_table);
501 #else
502  Assert(state != NULL);
503 #endif /* EEO_USE_COMPUTED_GOTO */
504 
505  /* setup state */
506  op = state->steps;
507  resultslot = state->resultslot;
508  innerslot = econtext->ecxt_innertuple;
509  outerslot = econtext->ecxt_outertuple;
510  scanslot = econtext->ecxt_scantuple;
511 
512 #if defined(EEO_USE_COMPUTED_GOTO)
513  EEO_DISPATCH();
514 #endif
515 
516  EEO_SWITCH()
517  {
519  {
520  goto out;
521  }
522 
524  {
525  CheckOpSlotCompatibility(op, innerslot);
526 
527  slot_getsomeattrs(innerslot, op->d.fetch.last_var);
528 
529  EEO_NEXT();
530  }
531 
533  {
534  CheckOpSlotCompatibility(op, outerslot);
535 
536  slot_getsomeattrs(outerslot, op->d.fetch.last_var);
537 
538  EEO_NEXT();
539  }
540 
542  {
543  CheckOpSlotCompatibility(op, scanslot);
544 
545  slot_getsomeattrs(scanslot, op->d.fetch.last_var);
546 
547  EEO_NEXT();
548  }
549 
551  {
552  int attnum = op->d.var.attnum;
553 
554  /*
555  * Since we already extracted all referenced columns from the
556  * tuple with a FETCHSOME step, we can just grab the value
557  * directly out of the slot's decomposed-data arrays. But let's
558  * have an Assert to check that that did happen.
559  */
560  Assert(attnum >= 0 && attnum < innerslot->tts_nvalid);
561  *op->resvalue = innerslot->tts_values[attnum];
562  *op->resnull = innerslot->tts_isnull[attnum];
563 
564  EEO_NEXT();
565  }
566 
568  {
569  int attnum = op->d.var.attnum;
570 
571  /* See EEOP_INNER_VAR comments */
572 
573  Assert(attnum >= 0 && attnum < outerslot->tts_nvalid);
574  *op->resvalue = outerslot->tts_values[attnum];
575  *op->resnull = outerslot->tts_isnull[attnum];
576 
577  EEO_NEXT();
578  }
579 
581  {
582  int attnum = op->d.var.attnum;
583 
584  /* See EEOP_INNER_VAR comments */
585 
586  Assert(attnum >= 0 && attnum < scanslot->tts_nvalid);
587  *op->resvalue = scanslot->tts_values[attnum];
588  *op->resnull = scanslot->tts_isnull[attnum];
589 
590  EEO_NEXT();
591  }
592 
594  {
595  ExecEvalSysVar(state, op, econtext, innerslot);
596  EEO_NEXT();
597  }
598 
600  {
601  ExecEvalSysVar(state, op, econtext, outerslot);
602  EEO_NEXT();
603  }
604 
606  {
607  ExecEvalSysVar(state, op, econtext, scanslot);
608  EEO_NEXT();
609  }
610 
612  {
613  /* too complex for an inline implementation */
614  ExecEvalWholeRowVar(state, op, econtext);
615 
616  EEO_NEXT();
617  }
618 
620  {
621  int resultnum = op->d.assign_var.resultnum;
622  int attnum = op->d.assign_var.attnum;
623 
624  /*
625  * We do not need CheckVarSlotCompatibility here; that was taken
626  * care of at compilation time. But see EEOP_INNER_VAR comments.
627  */
628  Assert(attnum >= 0 && attnum < innerslot->tts_nvalid);
629  resultslot->tts_values[resultnum] = innerslot->tts_values[attnum];
630  resultslot->tts_isnull[resultnum] = innerslot->tts_isnull[attnum];
631 
632  EEO_NEXT();
633  }
634 
636  {
637  int resultnum = op->d.assign_var.resultnum;
638  int attnum = op->d.assign_var.attnum;
639 
640  /*
641  * We do not need CheckVarSlotCompatibility here; that was taken
642  * care of at compilation time. But see EEOP_INNER_VAR comments.
643  */
644  Assert(attnum >= 0 && attnum < outerslot->tts_nvalid);
645  resultslot->tts_values[resultnum] = outerslot->tts_values[attnum];
646  resultslot->tts_isnull[resultnum] = outerslot->tts_isnull[attnum];
647 
648  EEO_NEXT();
649  }
650 
652  {
653  int resultnum = op->d.assign_var.resultnum;
654  int attnum = op->d.assign_var.attnum;
655 
656  /*
657  * We do not need CheckVarSlotCompatibility here; that was taken
658  * care of at compilation time. But see EEOP_INNER_VAR comments.
659  */
660  Assert(attnum >= 0 && attnum < scanslot->tts_nvalid);
661  resultslot->tts_values[resultnum] = scanslot->tts_values[attnum];
662  resultslot->tts_isnull[resultnum] = scanslot->tts_isnull[attnum];
663 
664  EEO_NEXT();
665  }
666 
668  {
669  int resultnum = op->d.assign_tmp.resultnum;
670 
671  resultslot->tts_values[resultnum] = state->resvalue;
672  resultslot->tts_isnull[resultnum] = state->resnull;
673 
674  EEO_NEXT();
675  }
676 
678  {
679  int resultnum = op->d.assign_tmp.resultnum;
680 
681  resultslot->tts_isnull[resultnum] = state->resnull;
682  if (!resultslot->tts_isnull[resultnum])
683  resultslot->tts_values[resultnum] =
685  else
686  resultslot->tts_values[resultnum] = state->resvalue;
687 
688  EEO_NEXT();
689  }
690 
692  {
693  *op->resnull = op->d.constval.isnull;
694  *op->resvalue = op->d.constval.value;
695 
696  EEO_NEXT();
697  }
698 
699  /*
700  * Function-call implementations. Arguments have previously been
701  * evaluated directly into fcinfo->args.
702  *
703  * As both STRICT checks and function-usage are noticeable performance
704  * wise, and function calls are a very hot-path (they also back
705  * operators!), it's worth having so many separate opcodes.
706  *
707  * Note: the reason for using a temporary variable "d", here and in
708  * other places, is that some compilers think "*op->resvalue = f();"
709  * requires them to evaluate op->resvalue into a register before
710  * calling f(), just in case f() is able to modify op->resvalue
711  * somehow. The extra line of code can save a useless register spill
712  * and reload across the function call.
713  */
715  {
716  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
717  Datum d;
718 
719  fcinfo->isnull = false;
720  d = op->d.func.fn_addr(fcinfo);
721  *op->resvalue = d;
722  *op->resnull = fcinfo->isnull;
723 
724  EEO_NEXT();
725  }
726 
728  {
729  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
730  NullableDatum *args = fcinfo->args;
731  int nargs = op->d.func.nargs;
732  Datum d;
733 
734  /* strict function, so check for NULL args */
735  for (int argno = 0; argno < nargs; argno++)
736  {
737  if (args[argno].isnull)
738  {
739  *op->resnull = true;
740  goto strictfail;
741  }
742  }
743  fcinfo->isnull = false;
744  d = op->d.func.fn_addr(fcinfo);
745  *op->resvalue = d;
746  *op->resnull = fcinfo->isnull;
747 
748  strictfail:
749  EEO_NEXT();
750  }
751 
753  {
754  /* not common enough to inline */
755  ExecEvalFuncExprFusage(state, op, econtext);
756 
757  EEO_NEXT();
758  }
759 
761  {
762  /* not common enough to inline */
763  ExecEvalFuncExprStrictFusage(state, op, econtext);
764 
765  EEO_NEXT();
766  }
767 
768  /*
769  * If any of its clauses is FALSE, an AND's result is FALSE regardless
770  * of the states of the rest of the clauses, so we can stop evaluating
771  * and return FALSE immediately. If none are FALSE and one or more is
772  * NULL, we return NULL; otherwise we return TRUE. This makes sense
773  * when you interpret NULL as "don't know": perhaps one of the "don't
774  * knows" would have been FALSE if we'd known its value. Only when
775  * all the inputs are known to be TRUE can we state confidently that
776  * the AND's result is TRUE.
777  */
779  {
780  *op->d.boolexpr.anynull = false;
781 
782  /*
783  * EEOP_BOOL_AND_STEP_FIRST resets anynull, otherwise it's the
784  * same as EEOP_BOOL_AND_STEP - so fall through to that.
785  */
786 
787  /* FALL THROUGH */
788  }
789 
791  {
792  if (*op->resnull)
793  {
794  *op->d.boolexpr.anynull = true;
795  }
796  else if (!DatumGetBool(*op->resvalue))
797  {
798  /* result is already set to FALSE, need not change it */
799  /* bail out early */
800  EEO_JUMP(op->d.boolexpr.jumpdone);
801  }
802 
803  EEO_NEXT();
804  }
805 
807  {
808  if (*op->resnull)
809  {
810  /* result is already set to NULL, need not change it */
811  }
812  else if (!DatumGetBool(*op->resvalue))
813  {
814  /* result is already set to FALSE, need not change it */
815 
816  /*
817  * No point jumping early to jumpdone - would be same target
818  * (as this is the last argument to the AND expression),
819  * except more expensive.
820  */
821  }
822  else if (*op->d.boolexpr.anynull)
823  {
824  *op->resvalue = (Datum) 0;
825  *op->resnull = true;
826  }
827  else
828  {
829  /* result is already set to TRUE, need not change it */
830  }
831 
832  EEO_NEXT();
833  }
834 
835  /*
836  * If any of its clauses is TRUE, an OR's result is TRUE regardless of
837  * the states of the rest of the clauses, so we can stop evaluating
838  * and return TRUE immediately. If none are TRUE and one or more is
839  * NULL, we return NULL; otherwise we return FALSE. This makes sense
840  * when you interpret NULL as "don't know": perhaps one of the "don't
841  * knows" would have been TRUE if we'd known its value. Only when all
842  * the inputs are known to be FALSE can we state confidently that the
843  * OR's result is FALSE.
844  */
846  {
847  *op->d.boolexpr.anynull = false;
848 
849  /*
850  * EEOP_BOOL_OR_STEP_FIRST resets anynull, otherwise it's the same
851  * as EEOP_BOOL_OR_STEP - so fall through to that.
852  */
853 
854  /* FALL THROUGH */
855  }
856 
858  {
859  if (*op->resnull)
860  {
861  *op->d.boolexpr.anynull = true;
862  }
863  else if (DatumGetBool(*op->resvalue))
864  {
865  /* result is already set to TRUE, need not change it */
866  /* bail out early */
867  EEO_JUMP(op->d.boolexpr.jumpdone);
868  }
869 
870  EEO_NEXT();
871  }
872 
874  {
875  if (*op->resnull)
876  {
877  /* result is already set to NULL, need not change it */
878  }
879  else if (DatumGetBool(*op->resvalue))
880  {
881  /* result is already set to TRUE, need not change it */
882 
883  /*
884  * No point jumping to jumpdone - would be same target (as
885  * this is the last argument to the AND expression), except
886  * more expensive.
887  */
888  }
889  else if (*op->d.boolexpr.anynull)
890  {
891  *op->resvalue = (Datum) 0;
892  *op->resnull = true;
893  }
894  else
895  {
896  /* result is already set to FALSE, need not change it */
897  }
898 
899  EEO_NEXT();
900  }
901 
903  {
904  /*
905  * Evaluation of 'not' is simple... if expr is false, then return
906  * 'true' and vice versa. It's safe to do this even on a
907  * nominally null value, so we ignore resnull; that means that
908  * NULL in produces NULL out, which is what we want.
909  */
911 
912  EEO_NEXT();
913  }
914 
916  {
917  /* simplified version of BOOL_AND_STEP for use by ExecQual() */
918 
919  /* If argument (also result) is false or null ... */
920  if (*op->resnull ||
921  !DatumGetBool(*op->resvalue))
922  {
923  /* ... bail out early, returning FALSE */
924  *op->resnull = false;
925  *op->resvalue = BoolGetDatum(false);
926  EEO_JUMP(op->d.qualexpr.jumpdone);
927  }
928 
929  /*
930  * Otherwise, leave the TRUE value in place, in case this is the
931  * last qual. Then, TRUE is the correct answer.
932  */
933 
934  EEO_NEXT();
935  }
936 
938  {
939  /* Unconditionally jump to target step */
940  EEO_JUMP(op->d.jump.jumpdone);
941  }
942 
944  {
945  /* Transfer control if current result is null */
946  if (*op->resnull)
947  EEO_JUMP(op->d.jump.jumpdone);
948 
949  EEO_NEXT();
950  }
951 
953  {
954  /* Transfer control if current result is non-null */
955  if (!*op->resnull)
956  EEO_JUMP(op->d.jump.jumpdone);
957 
958  EEO_NEXT();
959  }
960 
962  {
963  /* Transfer control if current result is null or false */
964  if (*op->resnull || !DatumGetBool(*op->resvalue))
965  EEO_JUMP(op->d.jump.jumpdone);
966 
967  EEO_NEXT();
968  }
969 
971  {
972  *op->resvalue = BoolGetDatum(*op->resnull);
973  *op->resnull = false;
974 
975  EEO_NEXT();
976  }
977 
979  {
980  *op->resvalue = BoolGetDatum(!*op->resnull);
981  *op->resnull = false;
982 
983  EEO_NEXT();
984  }
985 
987  {
988  /* out of line implementation: too large */
989  ExecEvalRowNull(state, op, econtext);
990 
991  EEO_NEXT();
992  }
993 
995  {
996  /* out of line implementation: too large */
997  ExecEvalRowNotNull(state, op, econtext);
998 
999  EEO_NEXT();
1000  }
1001 
1002  /* BooleanTest implementations for all booltesttypes */
1003 
1005  {
1006  if (*op->resnull)
1007  {
1008  *op->resvalue = BoolGetDatum(false);
1009  *op->resnull = false;
1010  }
1011  /* else, input value is the correct output as well */
1012 
1013  EEO_NEXT();
1014  }
1015 
1017  {
1018  if (*op->resnull)
1019  {
1020  *op->resvalue = BoolGetDatum(true);
1021  *op->resnull = false;
1022  }
1023  else
1024  *op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue));
1025 
1026  EEO_NEXT();
1027  }
1028 
1030  {
1031  if (*op->resnull)
1032  {
1033  *op->resvalue = BoolGetDatum(false);
1034  *op->resnull = false;
1035  }
1036  else
1037  *op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue));
1038 
1039  EEO_NEXT();
1040  }
1041 
1043  {
1044  if (*op->resnull)
1045  {
1046  *op->resvalue = BoolGetDatum(true);
1047  *op->resnull = false;
1048  }
1049  /* else, input value is the correct output as well */
1050 
1051  EEO_NEXT();
1052  }
1053 
1055  {
1056  /* out of line implementation: too large */
1057  ExecEvalParamExec(state, op, econtext);
1058 
1059  EEO_NEXT();
1060  }
1061 
1063  {
1064  /* out of line implementation: too large */
1065  ExecEvalParamExtern(state, op, econtext);
1066  EEO_NEXT();
1067  }
1068 
1070  {
1071  /* allow an extension module to supply a PARAM_EXTERN value */
1072  op->d.cparam.paramfunc(state, op, econtext);
1073  EEO_NEXT();
1074  }
1075 
1077  {
1078  /*
1079  * Normally upper parts of the expression tree have setup the
1080  * values to be returned here, but some parts of the system
1081  * currently misuse {caseValue,domainValue}_{datum,isNull} to set
1082  * run-time data. So if no values have been set-up, use
1083  * ExprContext's. This isn't pretty, but also not *that* ugly,
1084  * and this is unlikely to be performance sensitive enough to
1085  * worry about an extra branch.
1086  */
1087  if (op->d.casetest.value)
1088  {
1089  *op->resvalue = *op->d.casetest.value;
1090  *op->resnull = *op->d.casetest.isnull;
1091  }
1092  else
1093  {
1094  *op->resvalue = econtext->caseValue_datum;
1095  *op->resnull = econtext->caseValue_isNull;
1096  }
1097 
1098  EEO_NEXT();
1099  }
1100 
1102  {
1103  /*
1104  * See EEOP_CASE_TESTVAL comment.
1105  */
1106  if (op->d.casetest.value)
1107  {
1108  *op->resvalue = *op->d.casetest.value;
1109  *op->resnull = *op->d.casetest.isnull;
1110  }
1111  else
1112  {
1113  *op->resvalue = econtext->domainValue_datum;
1114  *op->resnull = econtext->domainValue_isNull;
1115  }
1116 
1117  EEO_NEXT();
1118  }
1119 
1121  {
1122  /*
1123  * Force a varlena value that might be read multiple times to R/O
1124  */
1125  if (!*op->d.make_readonly.isnull)
1126  *op->resvalue =
1128  *op->resnull = *op->d.make_readonly.isnull;
1129 
1130  EEO_NEXT();
1131  }
1132 
1134  {
1135  /*
1136  * Evaluate a CoerceViaIO node. This can be quite a hot path, so
1137  * inline as much work as possible. The source value is in our
1138  * result variable.
1139  */
1140  char *str;
1141 
1142  /* call output function (similar to OutputFunctionCall) */
1143  if (*op->resnull)
1144  {
1145  /* output functions are not called on nulls */
1146  str = NULL;
1147  }
1148  else
1149  {
1150  FunctionCallInfo fcinfo_out;
1151 
1152  fcinfo_out = op->d.iocoerce.fcinfo_data_out;
1153  fcinfo_out->args[0].value = *op->resvalue;
1154  fcinfo_out->args[0].isnull = false;
1155 
1156  fcinfo_out->isnull = false;
1157  str = DatumGetCString(FunctionCallInvoke(fcinfo_out));
1158 
1159  /* OutputFunctionCall assumes result isn't null */
1160  Assert(!fcinfo_out->isnull);
1161  }
1162 
1163  /* call input function (similar to InputFunctionCall) */
1164  if (!op->d.iocoerce.finfo_in->fn_strict || str != NULL)
1165  {
1166  FunctionCallInfo fcinfo_in;
1167  Datum d;
1168 
1169  fcinfo_in = op->d.iocoerce.fcinfo_data_in;
1170  fcinfo_in->args[0].value = PointerGetDatum(str);
1171  fcinfo_in->args[0].isnull = *op->resnull;
1172  /* second and third arguments are already set up */
1173 
1174  fcinfo_in->isnull = false;
1175  d = FunctionCallInvoke(fcinfo_in);
1176  *op->resvalue = d;
1177 
1178  /* Should get null result if and only if str is NULL */
1179  if (str == NULL)
1180  {
1181  Assert(*op->resnull);
1182  Assert(fcinfo_in->isnull);
1183  }
1184  else
1185  {
1186  Assert(!*op->resnull);
1187  Assert(!fcinfo_in->isnull);
1188  }
1189  }
1190 
1191  EEO_NEXT();
1192  }
1193 
1195  {
1196  /*
1197  * IS DISTINCT FROM must evaluate arguments (already done into
1198  * fcinfo->args) to determine whether they are NULL; if either is
1199  * NULL then the result is determined. If neither is NULL, then
1200  * proceed to evaluate the comparison function, which is just the
1201  * type's standard equality operator. We need not care whether
1202  * that function is strict. Because the handling of nulls is
1203  * different, we can't just reuse EEOP_FUNCEXPR.
1204  */
1205  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
1206 
1207  /* check function arguments for NULLness */
1208  if (fcinfo->args[0].isnull && fcinfo->args[1].isnull)
1209  {
1210  /* Both NULL? Then is not distinct... */
1211  *op->resvalue = BoolGetDatum(false);
1212  *op->resnull = false;
1213  }
1214  else if (fcinfo->args[0].isnull || fcinfo->args[1].isnull)
1215  {
1216  /* Only one is NULL? Then is distinct... */
1217  *op->resvalue = BoolGetDatum(true);
1218  *op->resnull = false;
1219  }
1220  else
1221  {
1222  /* Neither null, so apply the equality function */
1223  Datum eqresult;
1224 
1225  fcinfo->isnull = false;
1226  eqresult = op->d.func.fn_addr(fcinfo);
1227  /* Must invert result of "="; safe to do even if null */
1228  *op->resvalue = BoolGetDatum(!DatumGetBool(eqresult));
1229  *op->resnull = fcinfo->isnull;
1230  }
1231 
1232  EEO_NEXT();
1233  }
1234 
1235  /* see EEOP_DISTINCT for comments, this is just inverted */
1237  {
1238  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
1239 
1240  if (fcinfo->args[0].isnull && fcinfo->args[1].isnull)
1241  {
1242  *op->resvalue = BoolGetDatum(true);
1243  *op->resnull = false;
1244  }
1245  else if (fcinfo->args[0].isnull || fcinfo->args[1].isnull)
1246  {
1247  *op->resvalue = BoolGetDatum(false);
1248  *op->resnull = false;
1249  }
1250  else
1251  {
1252  Datum eqresult;
1253 
1254  fcinfo->isnull = false;
1255  eqresult = op->d.func.fn_addr(fcinfo);
1256  *op->resvalue = eqresult;
1257  *op->resnull = fcinfo->isnull;
1258  }
1259 
1260  EEO_NEXT();
1261  }
1262 
1264  {
1265  /*
1266  * The arguments are already evaluated into fcinfo->args.
1267  */
1268  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
1269 
1270  /* if either argument is NULL they can't be equal */
1271  if (!fcinfo->args[0].isnull && !fcinfo->args[1].isnull)
1272  {
1273  Datum result;
1274 
1275  fcinfo->isnull = false;
1276  result = op->d.func.fn_addr(fcinfo);
1277 
1278  /* if the arguments are equal return null */
1279  if (!fcinfo->isnull && DatumGetBool(result))
1280  {
1281  *op->resvalue = (Datum) 0;
1282  *op->resnull = true;
1283 
1284  EEO_NEXT();
1285  }
1286  }
1287 
1288  /* Arguments aren't equal, so return the first one */
1289  *op->resvalue = fcinfo->args[0].value;
1290  *op->resnull = fcinfo->args[0].isnull;
1291 
1292  EEO_NEXT();
1293  }
1294 
1296  {
1297  /*
1298  * Doesn't seem worthwhile to have an inline implementation
1299  * efficiency-wise.
1300  */
1301  ExecEvalSQLValueFunction(state, op);
1302 
1303  EEO_NEXT();
1304  }
1305 
1307  {
1308  /* error invocation uses space, and shouldn't ever occur */
1309  ExecEvalCurrentOfExpr(state, op);
1310 
1311  EEO_NEXT();
1312  }
1313 
1315  {
1316  /*
1317  * Doesn't seem worthwhile to have an inline implementation
1318  * efficiency-wise.
1319  */
1320  ExecEvalNextValueExpr(state, op);
1321 
1322  EEO_NEXT();
1323  }
1324 
1326  {
1327  /* too complex for an inline implementation */
1328  ExecEvalArrayExpr(state, op);
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 */
1416  ExecEvalMinMax(state, op);
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 */
1480  ExecEvalScalarArrayOp(state, op);
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 */
1496  ExecEvalConstraintNotNull(state, op);
1497 
1498  EEO_NEXT();
1499  }
1500 
1502  {
1503  /* too complex for an inline implementation */
1504  ExecEvalConstraintCheck(state, op);
1505 
1506  EEO_NEXT();
1507  }
1508 
1510  {
1511  /* too complex for an inline implementation */
1512  ExecEvalXmlExpr(state, op);
1513 
1514  EEO_NEXT();
1515  }
1516 
1518  {
1519  /*
1520  * Returns a Datum whose value is the precomputed aggregate value
1521  * found in the given expression context.
1522  */
1523  int aggno = op->d.aggref.aggno;
1524 
1525  Assert(econtext->ecxt_aggvalues != NULL);
1526 
1527  *op->resvalue = econtext->ecxt_aggvalues[aggno];
1528  *op->resnull = econtext->ecxt_aggnulls[aggno];
1529 
1530  EEO_NEXT();
1531  }
1532 
1534  {
1535  /* too complex/uncommon for an inline implementation */
1536  ExecEvalGroupingFunc(state, op);
1537 
1538  EEO_NEXT();
1539  }
1540 
1542  {
1543  /*
1544  * Like Aggref, just return a precomputed value from the econtext.
1545  */
1546  WindowFuncExprState *wfunc = op->d.window_func.wfstate;
1547 
1548  Assert(econtext->ecxt_aggvalues != NULL);
1549 
1550  *op->resvalue = econtext->ecxt_aggvalues[wfunc->wfuncno];
1551  *op->resnull = econtext->ecxt_aggnulls[wfunc->wfuncno];
1552 
1553  EEO_NEXT();
1554  }
1555 
1557  {
1558  /* too complex for an inline implementation */
1559  ExecEvalSubPlan(state, op, econtext);
1560 
1561  EEO_NEXT();
1562  }
1563 
1564  /* evaluate a strict aggregate deserialization function */
1566  {
1567  /* Don't call a strict deserialization function with NULL input */
1568  if (op->d.agg_deserialize.fcinfo_data->args[0].isnull)
1569  EEO_JUMP(op->d.agg_deserialize.jumpnull);
1570 
1571  /* fallthrough */
1572  }
1573 
1574  /* evaluate aggregate deserialization function (non-strict portion) */
1576  {
1577  FunctionCallInfo fcinfo = op->d.agg_deserialize.fcinfo_data;
1578  AggState *aggstate = castNode(AggState, state->parent);
1579  MemoryContext oldContext;
1580 
1581  /*
1582  * We run the deserialization functions in per-input-tuple memory
1583  * context.
1584  */
1585  oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
1586  fcinfo->isnull = false;
1587  *op->resvalue = FunctionCallInvoke(fcinfo);
1588  *op->resnull = fcinfo->isnull;
1589  MemoryContextSwitchTo(oldContext);
1590 
1591  EEO_NEXT();
1592  }
1593 
1594  /*
1595  * Check that a strict aggregate transition / combination function's
1596  * input is not NULL.
1597  */
1598 
1600  {
1601  NullableDatum *args = op->d.agg_strict_input_check.args;
1602  int nargs = op->d.agg_strict_input_check.nargs;
1603 
1604  for (int argno = 0; argno < nargs; argno++)
1605  {
1606  if (args[argno].isnull)
1607  EEO_JUMP(op->d.agg_strict_input_check.jumpnull);
1608  }
1609  EEO_NEXT();
1610  }
1611 
1613  {
1614  bool *nulls = op->d.agg_strict_input_check.nulls;
1615  int nargs = op->d.agg_strict_input_check.nargs;
1616 
1617  for (int argno = 0; argno < nargs; argno++)
1618  {
1619  if (nulls[argno])
1620  EEO_JUMP(op->d.agg_strict_input_check.jumpnull);
1621  }
1622  EEO_NEXT();
1623  }
1624 
1625  /*
1626  * Check for a NULL pointer to the per-group states.
1627  */
1628 
1630  {
1631  AggState *aggstate = castNode(AggState, state->parent);
1632  AggStatePerGroup pergroup_allaggs =
1633  aggstate->all_pergroups[op->d.agg_plain_pergroup_nullcheck.setoff];
1634 
1635  if (pergroup_allaggs == NULL)
1636  EEO_JUMP(op->d.agg_plain_pergroup_nullcheck.jumpnull);
1637 
1638  EEO_NEXT();
1639  }
1640 
1641  /*
1642  * Different types of aggregate transition functions are implemented
1643  * as different types of steps, to avoid incurring unnecessary
1644  * overhead. There's a step type for each valid combination of having
1645  * a by value / by reference transition type, [not] needing to the
1646  * initialize the transition value for the first row in a group from
1647  * input, and [not] strict transition function.
1648  *
1649  * Could optimize further by splitting off by-reference for
1650  * fixed-length types, but currently that doesn't seem worth it.
1651  */
1652 
1654  {
1655  AggState *aggstate = castNode(AggState, state->parent);
1656  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
1657  AggStatePerGroup pergroup =
1658  &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
1659 
1660  Assert(pertrans->transtypeByVal);
1661 
1662  if (pergroup->noTransValue)
1663  {
1664  /* If transValue has not yet been initialized, do so now. */
1665  ExecAggInitGroup(aggstate, pertrans, pergroup,
1666  op->d.agg_trans.aggcontext);
1667  /* copied trans value from input, done this round */
1668  }
1669  else if (likely(!pergroup->transValueIsNull))
1670  {
1671  /* invoke transition function, unless prevented by strictness */
1672  ExecAggPlainTransByVal(aggstate, pertrans, pergroup,
1673  op->d.agg_trans.aggcontext,
1674  op->d.agg_trans.setno);
1675  }
1676 
1677  EEO_NEXT();
1678  }
1679 
1680  /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
1682  {
1683  AggState *aggstate = castNode(AggState, state->parent);
1684  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
1685  AggStatePerGroup pergroup =
1686  &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
1687 
1688  Assert(pertrans->transtypeByVal);
1689 
1690  if (likely(!pergroup->transValueIsNull))
1691  ExecAggPlainTransByVal(aggstate, pertrans, pergroup,
1692  op->d.agg_trans.aggcontext,
1693  op->d.agg_trans.setno);
1694 
1695  EEO_NEXT();
1696  }
1697 
1698  /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
1700  {
1701  AggState *aggstate = castNode(AggState, state->parent);
1702  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
1703  AggStatePerGroup pergroup =
1704  &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
1705 
1706  Assert(pertrans->transtypeByVal);
1707 
1708  ExecAggPlainTransByVal(aggstate, pertrans, pergroup,
1709  op->d.agg_trans.aggcontext,
1710  op->d.agg_trans.setno);
1711 
1712  EEO_NEXT();
1713  }
1714 
1715  /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
1717  {
1718  AggState *aggstate = castNode(AggState, state->parent);
1719  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
1720  AggStatePerGroup pergroup =
1721  &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
1722 
1723  Assert(!pertrans->transtypeByVal);
1724 
1725  if (pergroup->noTransValue)
1726  ExecAggInitGroup(aggstate, pertrans, pergroup,
1727  op->d.agg_trans.aggcontext);
1728  else if (likely(!pergroup->transValueIsNull))
1729  ExecAggPlainTransByRef(aggstate, pertrans, pergroup,
1730  op->d.agg_trans.aggcontext,
1731  op->d.agg_trans.setno);
1732 
1733  EEO_NEXT();
1734  }
1735 
1736  /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
1738  {
1739  AggState *aggstate = castNode(AggState, state->parent);
1740  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
1741  AggStatePerGroup pergroup =
1742  &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
1743 
1744  Assert(!pertrans->transtypeByVal);
1745 
1746  if (likely(!pergroup->transValueIsNull))
1747  ExecAggPlainTransByRef(aggstate, pertrans, pergroup,
1748  op->d.agg_trans.aggcontext,
1749  op->d.agg_trans.setno);
1750  EEO_NEXT();
1751  }
1752 
1753  /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
1755  {
1756  AggState *aggstate = castNode(AggState, state->parent);
1757  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
1758  AggStatePerGroup pergroup =
1759  &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
1760 
1761  Assert(!pertrans->transtypeByVal);
1762 
1763  ExecAggPlainTransByRef(aggstate, pertrans, pergroup,
1764  op->d.agg_trans.aggcontext,
1765  op->d.agg_trans.setno);
1766 
1767  EEO_NEXT();
1768  }
1769 
1770  /* process single-column ordered aggregate datum */
1772  {
1773  /* too complex for an inline implementation */
1774  ExecEvalAggOrderedTransDatum(state, op, econtext);
1775 
1776  EEO_NEXT();
1777  }
1778 
1779  /* process multi-column ordered aggregate tuple */
1781  {
1782  /* too complex for an inline implementation */
1783  ExecEvalAggOrderedTransTuple(state, op, econtext);
1784 
1785  EEO_NEXT();
1786  }
1787 
1789  {
1790  /* unreachable */
1791  Assert(false);
1792  goto out;
1793  }
1794  }
1795 
1796 out:
1797  *isnull = state->resnull;
1798  return state->resvalue;
1799 }
void ExecEvalScalarArrayOp(ExprState *state, ExprEvalStep *op)
Datum * ecxt_aggvalues
Definition: execnodes.h:245
Datum MakeExpandedObjectReadOnlyInternal(Datum d)
Definition: expandeddatum.c:95
struct PlanState * parent
Definition: execnodes.h:108
#define likely(x)
Definition: c.h:272
static pg_attribute_always_inline void ExecAggPlainTransByVal(AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroup, ExprContext *aggcontext, int setno)
Datum * resvalue
Definition: