PostgreSQL Source Code  git master
execExprInterp.c File Reference
#include "postgres.h"
#include "access/heaptoast.h"
#include "access/xact.h"
#include "catalog/pg_proc.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/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/parsetree.h"
#include "parser/parse_expr.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/json.h"
#include "utils/jsonb.h"
#include "utils/jsonfuncs.h"
#include "utils/jsonpath.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/resowner.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
 
struct  ExecEvalJsonExprContext
 

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
 
typedef Datum(* JsonFunc) (ExprEvalStep *op, ExprContext *econtext, Datum item, bool *resnull, void *p, bool *error)
 

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 ExecEvalJsonIsPredicate (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)
 
void ExecEvalJsonConstructor (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 
static Datum ExecEvalJsonBehavior (ExprContext *econtext, JsonBehavior *behavior, ExprState *default_estate, bool *is_null)
 
static Datum ExecEvalJsonExprCoercion (ExprEvalStep *op, ExprContext *econtext, Datum res, bool *isNull, void *p, bool *error)
 
int EvalJsonPathVar (void *cxt, char *varName, int varNameLen, JsonbValue *val, JsonbValue *baseObject)
 
Datum ExecPrepareJsonItemCoercion (JsonbValue *item, JsonReturning *returning, struct JsonCoercionsState *coercions, struct JsonCoercionState **pcoercion)
 
static Datum ExecEvalJsonExprSubtrans (JsonFunc func, ExprEvalStep *op, ExprContext *econtext, Datum res, bool *resnull, void *p, bool *error, bool subtrans)
 
static Datum ExecEvalJsonExpr (ExprEvalStep *op, ExprContext *econtext, Datum item, bool *resnull, void *pcxt, bool *error)
 
bool ExecEvalJsonNeedsSubTransaction (JsonExpr *jsexpr, struct JsonCoercionsState *coercions)
 
void ExecEvalJson (ExprState *state, ExprEvalStep *op, ExprContext *econtext)
 

Macro Definition Documentation

◆ EEO_CASE

#define EEO_CASE (   name)    case name:

Definition at line 131 of file execExprInterp.c.

◆ EEO_DISPATCH

#define EEO_DISPATCH ( )    goto starteval

Definition at line 132 of file execExprInterp.c.

◆ EEO_JUMP

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

Definition at line 143 of file execExprInterp.c.

◆ EEO_NEXT

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

Definition at line 137 of file execExprInterp.c.

◆ EEO_OPCODE

#define EEO_OPCODE (   opcode)    (opcode)

Definition at line 133 of file execExprInterp.c.

◆ EEO_SWITCH

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

Definition at line 130 of file execExprInterp.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 205 of file execExprInterp.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 232 of file execExprInterp.c.

◆ SH_ELEMENT_TYPE [1/2]

#define SH_ELEMENT_TYPE   ScalarArrayOpExprHashEntry

Definition at line 224 of file execExprInterp.c.

◆ SH_ELEMENT_TYPE [2/2]

#define SH_ELEMENT_TYPE   ScalarArrayOpExprHashEntry

Definition at line 224 of file execExprInterp.c.

◆ SH_EQUAL

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

Definition at line 228 of file execExprInterp.c.

◆ SH_GET_HASH

#define SH_GET_HASH (   tb,
  a 
)    a->hash

Definition at line 231 of file execExprInterp.c.

◆ SH_HASH_KEY

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

Definition at line 227 of file execExprInterp.c.

◆ SH_KEY

#define SH_KEY   key

Definition at line 226 of file execExprInterp.c.

◆ SH_KEY_TYPE [1/2]

#define SH_KEY_TYPE   Datum

Definition at line 225 of file execExprInterp.c.

◆ SH_KEY_TYPE [2/2]

#define SH_KEY_TYPE   Datum

Definition at line 225 of file execExprInterp.c.

◆ SH_PREFIX [1/2]

#define SH_PREFIX   saophash

Definition at line 223 of file execExprInterp.c.

◆ SH_PREFIX [2/2]

#define SH_PREFIX   saophash

Definition at line 223 of file execExprInterp.c.

◆ SH_SCOPE [1/2]

#define SH_SCOPE   static inline

Definition at line 229 of file execExprInterp.c.

◆ SH_SCOPE [2/2]

#define SH_SCOPE   static inline

Definition at line 229 of file execExprInterp.c.

◆ SH_STORE_HASH

#define SH_STORE_HASH

Definition at line 230 of file execExprInterp.c.

Typedef Documentation

◆ JsonFunc

typedef Datum(* JsonFunc) (ExprEvalStep *op, ExprContext *econtext, Datum item, bool *resnull, void *p, bool *error)

Definition at line 4815 of file execExprInterp.c.

◆ ScalarArrayOpExprHashEntry

◆ ScalarArrayOpExprHashTable

Function Documentation

◆ CheckExprStillValid()

void CheckExprStillValid ( ExprState state,
ExprContext econtext 
)

Definition at line 1864 of file execExprInterp.c.

1865 {
1866  TupleTableSlot *innerslot;
1867  TupleTableSlot *outerslot;
1868  TupleTableSlot *scanslot;
1869 
1870  innerslot = econtext->ecxt_innertuple;
1871  outerslot = econtext->ecxt_outertuple;
1872  scanslot = econtext->ecxt_scantuple;
1873 
1874  for (int i = 0; i < state->steps_len; i++)
1875  {
1876  ExprEvalStep *op = &state->steps[i];
1877 
1878  switch (ExecEvalStepOp(state, op))
1879  {
1880  case EEOP_INNER_VAR:
1881  {
1882  int attnum = op->d.var.attnum;
1883 
1884  CheckVarSlotCompatibility(innerslot, attnum + 1, op->d.var.vartype);
1885  break;
1886  }
1887 
1888  case EEOP_OUTER_VAR:
1889  {
1890  int attnum = op->d.var.attnum;
1891 
1892  CheckVarSlotCompatibility(outerslot, attnum + 1, op->d.var.vartype);
1893  break;
1894  }
1895 
1896  case EEOP_SCAN_VAR:
1897  {
1898  int attnum = op->d.var.attnum;
1899 
1900  CheckVarSlotCompatibility(scanslot, attnum + 1, op->d.var.vartype);
1901  break;
1902  }
1903  default:
1904  break;
1905  }
1906  }
1907 }
static void CheckVarSlotCompatibility(TupleTableSlot *slot, int attnum, Oid vartype)
ExprEvalOp ExecEvalStepOp(ExprState *state, ExprEvalStep *op)
@ EEOP_INNER_VAR
Definition: execExpr.h:76
@ EEOP_SCAN_VAR
Definition: execExpr.h:78
@ EEOP_OUTER_VAR
Definition: execExpr.h:77
int i
Definition: isn.c:73
int16 attnum
Definition: pg_attribute.h:83
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:234
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:232
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:236
struct ExprEvalStep::@52::@54 var
union ExprEvalStep::@52 d

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

Referenced by ExecInterpExprStillValid(), and ExecRunCompiledExpr().

◆ CheckOpSlotCompatibility()

static void CheckOpSlotCompatibility ( ExprEvalStep op,
TupleTableSlot slot 
)
static

Definition at line 1966 of file execExprInterp.c.

1967 {
1968 #ifdef USE_ASSERT_CHECKING
1969  /* there's nothing to check */
1970  if (!op->d.fetch.fixed)
1971  return;
1972 
1973  /*
1974  * Should probably fixed at some point, but for now it's easier to allow
1975  * buffer and heap tuples to be used interchangeably.
1976  */
1977  if (slot->tts_ops == &TTSOpsBufferHeapTuple &&
1978  op->d.fetch.kind == &TTSOpsHeapTuple)
1979  return;
1980  if (slot->tts_ops == &TTSOpsHeapTuple &&
1981  op->d.fetch.kind == &TTSOpsBufferHeapTuple)
1982  return;
1983 
1984  /*
1985  * At the moment we consider it OK if a virtual slot is used instead of a
1986  * specific type of slot, as a virtual slot never needs to be deformed.
1987  */
1988  if (slot->tts_ops == &TTSOpsVirtual)
1989  return;
1990 
1991  Assert(op->d.fetch.kind == slot->tts_ops);
1992 #endif
1993 }
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
const TupleTableSlotOps TTSOpsBufferHeapTuple
Definition: execTuples.c:86
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:84
Assert(fmt[strlen(fmt) - 1] !='\n')
struct ExprEvalStep::@52::@53 fetch
const TupleTableSlotOps *const tts_ops
Definition: tuptable.h:122

References Assert(), ExprEvalStep::op, TupleTableSlot::tts_ops, TTSOpsBufferHeapTuple, TTSOpsHeapTuple, and TTSOpsVirtual.

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

◆ CheckVarSlotCompatibility()

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

Definition at line 1915 of file execExprInterp.c.

1916 {
1917  /*
1918  * What we have to check for here is the possibility of an attribute
1919  * having been dropped or changed in type since the plan tree was created.
1920  * Ideally the plan will get invalidated and not re-used, but just in
1921  * case, we keep these defenses. Fortunately it's sufficient to check
1922  * once on the first time through.
1923  *
1924  * Note: ideally we'd check typmod as well as typid, but that seems
1925  * impractical at the moment: in many cases the tupdesc will have been
1926  * generated by ExecTypeFromTL(), and that can't guarantee to generate an
1927  * accurate typmod in all cases, because some expression node types don't
1928  * carry typmod. Fortunately, for precisely that reason, there should be
1929  * no places with a critical dependency on the typmod of a value.
1930  *
1931  * System attributes don't require checking since their types never
1932  * change.
1933  */
1934  if (attnum > 0)
1935  {
1936  TupleDesc slot_tupdesc = slot->tts_tupleDescriptor;
1937  Form_pg_attribute attr;
1938 
1939  if (attnum > slot_tupdesc->natts) /* should never happen */
1940  elog(ERROR, "attribute number %d exceeds number of columns %d",
1941  attnum, slot_tupdesc->natts);
1942 
1943  attr = TupleDescAttr(slot_tupdesc, attnum - 1);
1944 
1945  if (attr->attisdropped)
1946  ereport(ERROR,
1947  (errcode(ERRCODE_UNDEFINED_COLUMN),
1948  errmsg("attribute %d of type %s has been dropped",
1949  attnum, format_type_be(slot_tupdesc->tdtypeid))));
1950 
1951  if (vartype != attr->atttypid)
1952  ereport(ERROR,
1953  (errcode(ERRCODE_DATATYPE_MISMATCH),
1954  errmsg("attribute %d of type %s has wrong type",
1955  attnum, format_type_be(slot_tupdesc->tdtypeid)),
1956  errdetail("Table has type %s, but query expects %s.",
1957  format_type_be(attr->atttypid),
1958  format_type_be(vartype))));
1959  }
1960 }
int errdetail(const char *fmt,...)
Definition: elog.c:1037
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
#define ereport(elevel,...)
Definition: elog.h:143
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
Oid tdtypeid
Definition: tupdesc.h:82
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

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

Referenced by CheckExprStillValid().

◆ EvalJsonPathVar()

int EvalJsonPathVar ( void *  cxt,
char *  varName,
int  varNameLen,
JsonbValue val,
JsonbValue baseObject 
)

Definition at line 4675 of file execExprInterp.c.

4677 {
4678  JsonPathVariableEvalContext *var = NULL;
4679  List *vars = cxt;
4680  ListCell *lc;
4681  int id = 1;
4682 
4683  if (!varName)
4684  return list_length(vars);
4685 
4686  foreach(lc, vars)
4687  {
4688  var = lfirst(lc);
4689 
4690  if (!strncmp(var->name, varName, varNameLen))
4691  break;
4692 
4693  var = NULL;
4694  id++;
4695  }
4696 
4697  if (!var)
4698  return -1;
4699 
4700  if (!var->evaluated)
4701  {
4702  MemoryContext oldcxt = var->mcxt ?
4703  MemoryContextSwitchTo(var->mcxt) : NULL;
4704 
4705  var->value = ExecEvalExpr(var->estate, var->econtext, &var->isnull);
4706  var->evaluated = true;
4707 
4708  if (oldcxt)
4709  MemoryContextSwitchTo(oldcxt);
4710  }
4711 
4712  if (var->isnull)
4713  {
4714  val->type = jbvNull;
4715  return 0;
4716  }
4717 
4718  JsonItemFromDatum(var->value, var->typid, var->typmod, val);
4719 
4720  *baseObject = *val;
4721  return id;
4722 }
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:320
long val
Definition: informix.c:664
@ jbvNull
Definition: jsonb.h:236
void JsonItemFromDatum(Datum val, Oid typid, int32 typmod, JsonbValue *res)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
struct ExprState * estate
Definition: jsonpath.h:266
struct ExprContext * econtext
Definition: jsonpath.h:265
Definition: pg_list.h:51
Definition: regcomp.c:238

References ExecEvalExpr(), jbvNull, JsonItemFromDatum(), lfirst, list_length(), MemoryContextSwitchTo(), val, and ExprEvalStep::var.

Referenced by JsonPathExists(), JsonPathQuery(), JsonPathValue(), and JsonTableResetContextItem().

◆ ExecAggInitGroup()

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

Definition at line 4319 of file execExprInterp.c.

4321 {
4322  FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
4323  MemoryContext oldContext;
4324 
4325  /*
4326  * We must copy the datum into aggcontext if it is pass-by-ref. We do not
4327  * need to pfree the old transValue, since it's NULL. (We already checked
4328  * that the agg's input type is binary-compatible with its transtype, so
4329  * straight copy here is OK.)
4330  */
4331  oldContext = MemoryContextSwitchTo(aggcontext->ecxt_per_tuple_memory);
4332  pergroup->transValue = datumCopy(fcinfo->args[1].value,
4333  pertrans->transtypeByVal,
4334  pertrans->transtypeLen);
4335  pergroup->transValueIsNull = false;
4336  pergroup->noTransValue = false;
4337  MemoryContextSwitchTo(oldContext);
4338 }
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:132
FunctionCallInfo transfn_fcinfo
Definition: nodeAgg.h:162
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:240
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
Datum value
Definition: postgres.h:422

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

Referenced by ExecInterpExpr().

◆ ExecAggPlainTransByRef()

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

Definition at line 4453 of file execExprInterp.c.

4456 {
4457  FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
4458  MemoryContext oldContext;
4459  Datum newVal;
4460 
4461  /* cf. select_current_set() */
4462  aggstate->curaggcontext = aggcontext;
4463  aggstate->current_set = setno;
4464 
4465  /* set up aggstate->curpertrans for AggGetAggref() */
4466  aggstate->curpertrans = pertrans;
4467 
4468  /* invoke transition function in per-tuple context */
4469  oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
4470 
4471  fcinfo->args[0].value = pergroup->transValue;
4472  fcinfo->args[0].isnull = pergroup->transValueIsNull;
4473  fcinfo->isnull = false; /* just in case transfn doesn't set it */
4474 
4475  newVal = FunctionCallInvoke(fcinfo);
4476 
4477  /*
4478  * For pass-by-ref datatype, must copy the new value into aggcontext and
4479  * free the prior transValue. But if transfn returned a pointer to its
4480  * first input, we don't need to do anything. Also, if transfn returned a
4481  * pointer to a R/W expanded object that is already a child of the
4482  * aggcontext, assume we can adopt that value without copying it.
4483  *
4484  * It's safe to compare newVal with pergroup->transValue without regard
4485  * for either being NULL, because ExecAggTransReparent() takes care to set
4486  * transValue to 0 when NULL. Otherwise we could end up accidentally not
4487  * reparenting, when the transValue has the same numerical value as
4488  * newValue, despite being NULL. This is a somewhat hot path, making it
4489  * undesirable to instead solve this with another branch for the common
4490  * case of the transition function returning its (modified) input
4491  * argument.
4492  */
4493  if (DatumGetPointer(newVal) != DatumGetPointer(pergroup->transValue))
4494  newVal = ExecAggTransReparent(aggstate, pertrans,
4495  newVal, fcinfo->isnull,
4496  pergroup->transValue,
4497  pergroup->transValueIsNull);
4498 
4499  pergroup->transValue = newVal;
4500  pergroup->transValueIsNull = fcinfo->isnull;
4501 
4502  MemoryContextSwitchTo(oldContext);
4503 }
Datum ExecAggTransReparent(AggState *aggstate, AggStatePerTrans pertrans, Datum newValue, bool newValueIsNull, Datum oldValue, bool oldValueIsNull)
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:172
uintptr_t Datum
Definition: postgres.h:411
#define DatumGetPointer(X)
Definition: postgres.h:593
ExprContext * tmpcontext
Definition: execnodes.h:2339
ExprContext * curaggcontext
Definition: execnodes.h:2341
AggStatePerTrans curpertrans
Definition: execnodes.h:2344
int current_set
Definition: execnodes.h:2349

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().

◆ ExecAggPlainTransByVal()

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

Definition at line 4421 of file execExprInterp.c.

4424 {
4425  FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
4426  MemoryContext oldContext;
4427  Datum newVal;
4428 
4429  /* cf. select_current_set() */
4430  aggstate->curaggcontext = aggcontext;
4431  aggstate->current_set = setno;
4432 
4433  /* set up aggstate->curpertrans for AggGetAggref() */
4434  aggstate->curpertrans = pertrans;
4435 
4436  /* invoke transition function in per-tuple context */
4437  oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
4438 
4439  fcinfo->args[0].value = pergroup->transValue;
4440  fcinfo->args[0].isnull = pergroup->transValueIsNull;
4441  fcinfo->isnull = false; /* just in case transfn doesn't set it */
4442 
4443  newVal = FunctionCallInvoke(fcinfo);
4444 
4445  pergroup->transValue = newVal;
4446  pergroup->transValueIsNull = fcinfo->isnull;
4447 
4448  MemoryContextSwitchTo(oldContext);
4449 }

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, ExprEvalStep::setno, AggState::tmpcontext, AggStatePerTransData::transfn_fcinfo, AggStatePerGroupData::transValue, AggStatePerGroupData::transValueIsNull, and NullableDatum::value.

Referenced by ExecInterpExpr().

◆ ExecAggTransReparent()

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

Definition at line 4347 of file execExprInterp.c.

4350 {
4351  Assert(newValue != oldValue);
4352 
4353  if (!newValueIsNull)
4354  {
4356  if (DatumIsReadWriteExpandedObject(newValue,
4357  false,
4358  pertrans->transtypeLen) &&
4359  MemoryContextGetParent(DatumGetEOHP(newValue)->eoh_context) == CurrentMemoryContext)
4360  /* do nothing */ ;
4361  else
4362  newValue = datumCopy(newValue,
4363  pertrans->transtypeByVal,
4364  pertrans->transtypeLen);
4365  }
4366  else
4367  {
4368  /*
4369  * Ensure that AggStatePerGroup->transValue ends up being 0, so
4370  * callers can safely compare newValue/oldValue without having to
4371  * check their respective nullness.
4372  */
4373  newValue = (Datum) 0;
4374  }
4375 
4376  if (!oldValueIsNull)
4377  {
4378  if (DatumIsReadWriteExpandedObject(oldValue,
4379  false,
4380  pertrans->transtypeLen))
4381  DeleteExpandedObject(oldValue);
4382  else
4383  pfree(DatumGetPointer(oldValue));
4384  }
4385 
4386  return newValue;
4387 }
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
void DeleteExpandedObject(Datum d)
#define DatumIsReadWriteExpandedObject(d, isnull, typlen)
void pfree(void *pointer)
Definition: mcxt.c:1175
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
MemoryContext MemoryContextGetParent(MemoryContext context)
Definition: mcxt.c:446

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

Referenced by advance_transition_function(), and ExecAggPlainTransByRef().

◆ ExecEvalAggOrderedTransDatum()

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

Definition at line 4393 of file execExprInterp.c.

4395 {
4396  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
4397  int setno = op->d.agg_trans.setno;
4398 
4399  tuplesort_putdatum(pertrans->sortstates[setno],
4400  *op->resvalue, *op->resnull);
4401 }
Tuplesortstate ** sortstates
Definition: nodeAgg.h:154
struct ExprEvalStep::@52::@93 agg_trans
Datum * resvalue
Definition: execExpr.h:277
bool * resnull
Definition: execExpr.h:278
void tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull)
Definition: tuplesort.c:1961

References ExprEvalStep::op, ExprEvalStep::pertrans, ExprEvalStep::setno, AggStatePerTransData::sortstates, and tuplesort_putdatum().

Referenced by ExecInterpExpr().

◆ ExecEvalAggOrderedTransTuple()

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

Definition at line 4407 of file execExprInterp.c.

4409 {
4410  AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
4411  int setno = op->d.agg_trans.setno;
4412 
4413  ExecClearTuple(pertrans->sortslot);
4414  pertrans->sortslot->tts_nvalid = pertrans->numInputs;
4415  ExecStoreVirtualTuple(pertrans->sortslot);
4416  tuplesort_puttupleslot(pertrans->sortstates[setno], pertrans->sortslot);
4417 }
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1552
TupleTableSlot * sortslot
Definition: nodeAgg.h:136
AttrNumber tts_nvalid
Definition: tuptable.h:121
void tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot)
Definition: tuplesort.c:1840
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425

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

Referenced by ExecInterpExpr().

◆ ExecEvalArrayCoerce()

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

Definition at line 2917 of file execExprInterp.c.

2918 {
2919  Datum arraydatum;
2920 
2921  /* NULL array -> NULL result */
2922  if (*op->resnull)
2923  return;
2924 
2925  arraydatum = *op->resvalue;
2926 
2927  /*
2928  * If it's binary-compatible, modify the element type in the array header,
2929  * but otherwise leave the array as we received it.
2930  */
2931  if (op->d.arraycoerce.elemexprstate == NULL)
2932  {
2933  /* Detoast input array if necessary, and copy in any case */
2934  ArrayType *array = DatumGetArrayTypePCopy(arraydatum);
2935 
2936  ARR_ELEMTYPE(array) = op->d.arraycoerce.resultelemtype;
2937  *op->resvalue = PointerGetDatum(array);
2938  return;
2939  }
2940 
2941  /*
2942  * Use array_map to apply the sub-expression to each array element.
2943  */
2944  *op->resvalue = array_map(arraydatum,
2945  op->d.arraycoerce.elemexprstate,
2946  econtext,
2947  op->d.arraycoerce.resultelemtype,
2948  op->d.arraycoerce.amstate);
2949 }
#define DatumGetArrayTypePCopy(X)
Definition: array.h:255
#define ARR_ELEMTYPE(a)
Definition: array.h:285
Datum array_map(Datum arrayd, ExprState *exprstate, ExprContext *econtext, Oid retType, ArrayMapState *amstate)
Definition: arrayfuncs.c:3159
#define PointerGetDatum(X)
Definition: postgres.h:600
struct ExprEvalStep::@52::@72 arraycoerce

References ARR_ELEMTYPE, array_map(), DatumGetArrayTypePCopy, ExprEvalStep::op, and PointerGetDatum.

Referenced by ExecInterpExpr().

◆ ExecEvalArrayExpr()

void ExecEvalArrayExpr ( ExprState state,
ExprEvalStep op 
)

Definition at line 2708 of file execExprInterp.c.

2709 {
2710  ArrayType *result;
2711  Oid element_type = op->d.arrayexpr.elemtype;
2712  int nelems = op->d.arrayexpr.nelems;
2713  int ndims = 0;
2714  int dims[MAXDIM];
2715  int lbs[MAXDIM];
2716 
2717  /* Set non-null as default */
2718  *op->resnull = false;
2719 
2720  if (!op->d.arrayexpr.multidims)
2721  {
2722  /* Elements are presumably of scalar type */
2723  Datum *dvalues = op->d.arrayexpr.elemvalues;
2724  bool *dnulls = op->d.arrayexpr.elemnulls;
2725 
2726  /* setup for 1-D array of the given length */
2727  ndims = 1;
2728  dims[0] = nelems;
2729  lbs[0] = 1;
2730 
2731  result = construct_md_array(dvalues, dnulls, ndims, dims, lbs,
2732  element_type,
2733  op->d.arrayexpr.elemlength,
2734  op->d.arrayexpr.elembyval,
2735  op->d.arrayexpr.elemalign);
2736  }
2737  else
2738  {
2739  /* Must be nested array expressions */
2740  int nbytes = 0;
2741  int nitems = 0;
2742  int outer_nelems = 0;
2743  int elem_ndims = 0;
2744  int *elem_dims = NULL;
2745  int *elem_lbs = NULL;
2746  bool firstone = true;
2747  bool havenulls = false;
2748  bool haveempty = false;
2749  char **subdata;
2750  bits8 **subbitmaps;
2751  int *subbytes;
2752  int *subnitems;
2753  int32 dataoffset;
2754  char *dat;
2755  int iitem;
2756 
2757  subdata = (char **) palloc(nelems * sizeof(char *));
2758  subbitmaps = (bits8 **) palloc(nelems * sizeof(bits8 *));
2759  subbytes = (int *) palloc(nelems * sizeof(int));
2760  subnitems = (int *) palloc(nelems * sizeof(int));
2761 
2762  /* loop through and get data area from each element */
2763  for (int elemoff = 0; elemoff < nelems; elemoff++)
2764  {
2765  Datum arraydatum;
2766  bool eisnull;
2767  ArrayType *array;
2768  int this_ndims;
2769 
2770  arraydatum = op->d.arrayexpr.elemvalues[elemoff];
2771  eisnull = op->d.arrayexpr.elemnulls[elemoff];
2772 
2773  /* temporarily ignore null subarrays */
2774  if (eisnull)
2775  {
2776  haveempty = true;
2777  continue;
2778  }
2779 
2780  array = DatumGetArrayTypeP(arraydatum);
2781 
2782  /* run-time double-check on element type */
2783  if (element_type != ARR_ELEMTYPE(array))
2784  ereport(ERROR,
2785  (errcode(ERRCODE_DATATYPE_MISMATCH),
2786  errmsg("cannot merge incompatible arrays"),
2787  errdetail("Array with element type %s cannot be "
2788  "included in ARRAY construct with element type %s.",
2789  format_type_be(ARR_ELEMTYPE(array)),
2790  format_type_be(element_type))));
2791 
2792  this_ndims = ARR_NDIM(array);
2793  /* temporarily ignore zero-dimensional subarrays */
2794  if (this_ndims <= 0)
2795  {
2796  haveempty = true;
2797  continue;
2798  }
2799 
2800  if (firstone)
2801  {
2802  /* Get sub-array details from first member */
2803  elem_ndims = this_ndims;
2804  ndims = elem_ndims + 1;
2805  if (ndims <= 0 || ndims > MAXDIM)
2806  ereport(ERROR,
2807  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2808  errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
2809  ndims, MAXDIM)));
2810 
2811  elem_dims = (int *) palloc(elem_ndims * sizeof(int));
2812  memcpy(elem_dims, ARR_DIMS(array), elem_ndims * sizeof(int));
2813  elem_lbs = (int *) palloc(elem_ndims * sizeof(int));
2814  memcpy(elem_lbs, ARR_LBOUND(array), elem_ndims * sizeof(int));
2815 
2816  firstone = false;
2817  }
2818  else
2819  {
2820  /* Check other sub-arrays are compatible */
2821  if (elem_ndims != this_ndims ||
2822  memcmp(elem_dims, ARR_DIMS(array),
2823  elem_ndims * sizeof(int)) != 0 ||
2824  memcmp(elem_lbs, ARR_LBOUND(array),
2825  elem_ndims * sizeof(int)) != 0)
2826  ereport(ERROR,
2827  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2828  errmsg("multidimensional arrays must have array "
2829  "expressions with matching dimensions")));
2830  }
2831 
2832  subdata[outer_nelems] = ARR_DATA_PTR(array);
2833  subbitmaps[outer_nelems] = ARR_NULLBITMAP(array);
2834  subbytes[outer_nelems] = ARR_SIZE(array) - ARR_DATA_OFFSET(array);
2835  nbytes += subbytes[outer_nelems];
2836  subnitems[outer_nelems] = ArrayGetNItems(this_ndims,
2837  ARR_DIMS(array));
2838  nitems += subnitems[outer_nelems];
2839  havenulls |= ARR_HASNULL(array);
2840  outer_nelems++;
2841  }
2842 
2843  /*
2844  * If all items were null or empty arrays, return an empty array;
2845  * otherwise, if some were and some weren't, raise error. (Note: we
2846  * must special-case this somehow to avoid trying to generate a 1-D
2847  * array formed from empty arrays. It's not ideal...)
2848  */
2849  if (haveempty)
2850  {
2851  if (ndims == 0) /* didn't find any nonempty array */
2852  {
2853  *op->resvalue = PointerGetDatum(construct_empty_array(element_type));
2854  return;
2855  }
2856  ereport(ERROR,
2857  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2858  errmsg("multidimensional arrays must have array "
2859  "expressions with matching dimensions")));
2860  }
2861 
2862  /* setup for multi-D array */
2863  dims[0] = outer_nelems;
2864  lbs[0] = 1;
2865  for (int i = 1; i < ndims; i++)
2866  {
2867  dims[i] = elem_dims[i - 1];
2868  lbs[i] = elem_lbs[i - 1];
2869  }
2870 
2871  /* check for subscript overflow */
2872  (void) ArrayGetNItems(ndims, dims);
2873  ArrayCheckBounds(ndims, dims, lbs);
2874 
2875  if (havenulls)
2876  {
2877  dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nitems);
2878  nbytes += dataoffset;
2879  }
2880  else
2881  {
2882  dataoffset = 0; /* marker for no null bitmap */
2883  nbytes += ARR_OVERHEAD_NONULLS(ndims);
2884  }
2885 
2886  result = (ArrayType *) palloc(nbytes);
2887  SET_VARSIZE(result, nbytes);
2888  result->ndim = ndims;
2889  result->dataoffset = dataoffset;
2890  result->elemtype = element_type;
2891  memcpy(ARR_DIMS(result), dims, ndims * sizeof(int));
2892  memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int));
2893 
2894  dat = ARR_DATA_PTR(result);
2895  iitem = 0;
2896  for (int i = 0; i < outer_nelems; i++)
2897  {
2898  memcpy(dat, subdata[i], subbytes[i]);
2899  dat += subbytes[i];
2900  if (havenulls)
2901  array_bitmap_copy(ARR_NULLBITMAP(result), iitem,
2902  subbitmaps[i], 0,
2903  subnitems[i]);
2904  iitem += subnitems[i];
2905  }
2906  }
2907 
2908  *op->resvalue = PointerGetDatum(result);
2909 }
#define ARR_NDIM(a)
Definition: array.h:283
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define MAXDIM
Definition: array.h:75
#define ARR_NULLBITMAP(a)
Definition: array.h:293
#define ARR_OVERHEAD_WITHNULLS(ndims, nitems)
Definition: array.h:305
#define DatumGetArrayTypeP(X)
Definition: array.h:254
#define ARR_SIZE(a)
Definition: array.h:282
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:303
#define ARR_DATA_OFFSET(a)
Definition: array.h:309
#define ARR_DIMS(a)
Definition: array.h:287
#define ARR_HASNULL(a)
Definition: array.h:284
#define ARR_LBOUND(a)
Definition: array.h:289
ArrayType * construct_empty_array(Oid elmtype)
Definition: arrayfuncs.c:3526
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:3440
void array_bitmap_copy(bits8 *destbitmap, int destoffset, const bits8 *srcbitmap, int srcoffset, int nitems)
Definition: arrayfuncs.c:4912
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:76
void ArrayCheckBounds(int ndim, const int *dims, const int *lb)
Definition: arrayutils.c:128
signed int int32
Definition: c.h:429
uint8 bits8
Definition: c.h:448
void * palloc(Size size)
Definition: mcxt.c:1068
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
unsigned int Oid
Definition: postgres_ext.h:31
Oid elemtype
Definition: array.h:90
int ndim
Definition: array.h:88
int32 dataoffset
Definition: array.h:89
struct ExprEvalStep::@52::@71 arrayexpr

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(), ArrayCheckBounds(), ArrayGetNItems(), construct_empty_array(), construct_md_array(), ArrayType::dataoffset, DatumGetArrayTypeP, ExprEvalStep::element_type, ArrayType::elemtype, ereport, errcode(), errdetail(), errmsg(), ERROR, format_type_be(), i, MAXDIM, ArrayType::ndim, ExprEvalStep::nelems, ExprEvalStep::op, palloc(), PointerGetDatum, and SET_VARSIZE.

Referenced by ExecInterpExpr().

◆ ExecEvalConstraintCheck()

void ExecEvalConstraintCheck ( ExprState state,
ExprEvalStep op 
)

Definition at line 3706 of file execExprInterp.c.

3707 {
3708  if (!*op->d.domaincheck.checknull &&
3709  !DatumGetBool(*op->d.domaincheck.checkvalue))
3710  ereport(ERROR,
3711  (errcode(ERRCODE_CHECK_VIOLATION),
3712  errmsg("value for domain %s violates check constraint \"%s\"",
3713  format_type_be(op->d.domaincheck.resulttype),
3714  op->d.domaincheck.constraintname),
3715  errdomainconstraint(op->d.domaincheck.resulttype,
3716  op->d.domaincheck.constraintname)));
3717 }
int errdomainconstraint(Oid datatypeOid, const char *conname)
Definition: domains.c:384
#define DatumGetBool(X)
Definition: postgres.h:437
struct ExprEvalStep::@52::@81 domaincheck

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

Referenced by ExecInterpExpr().

◆ ExecEvalConstraintNotNull()

void ExecEvalConstraintNotNull ( ExprState state,
ExprEvalStep op 
)

Definition at line 3692 of file execExprInterp.c.

3693 {
3694  if (*op->resnull)
3695  ereport(ERROR,
3696  (errcode(ERRCODE_NOT_NULL_VIOLATION),
3697  errmsg("domain %s does not allow null values",
3698  format_type_be(op->d.domaincheck.resulttype)),
3699  errdatatype(op->d.domaincheck.resulttype)));
3700 }
int errdatatype(Oid datatypeOid)
Definition: domains.c:360

References ereport, errcode(), errdatatype(), errmsg(), ERROR, format_type_be(), and ExprEvalStep::op.

Referenced by ExecInterpExpr().

◆ ExecEvalConvertRowtype()

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

Definition at line 3226 of file execExprInterp.c.

3227 {
3228  HeapTuple result;
3229  Datum tupDatum;
3230  HeapTupleHeader tuple;
3231  HeapTupleData tmptup;
3232  TupleDesc indesc,
3233  outdesc;
3234  bool changed = false;
3235 
3236  /* NULL in -> NULL out */
3237  if (*op->resnull)
3238  return;
3239 
3240  tupDatum = *op->resvalue;
3241  tuple = DatumGetHeapTupleHeader(tupDatum);
3242 
3243  /*
3244  * Lookup tupdescs if first time through or if type changes. We'd better
3245  * pin them since type conversion functions could do catalog lookups and
3246  * hence cause cache invalidation.
3247  */
3248  indesc = get_cached_rowtype(op->d.convert_rowtype.inputtype, -1,
3249  op->d.convert_rowtype.incache,
3250  &changed);
3251  IncrTupleDescRefCount(indesc);
3252  outdesc = get_cached_rowtype(op->d.convert_rowtype.outputtype, -1,
3253  op->d.convert_rowtype.outcache,
3254  &changed);
3255  IncrTupleDescRefCount(outdesc);
3256 
3257  /*
3258  * We used to be able to assert that incoming tuples are marked with
3259  * exactly the rowtype of indesc. However, now that ExecEvalWholeRowVar
3260  * might change the tuples' marking to plain RECORD due to inserting
3261  * aliases, we can only make this weak test:
3262  */
3263  Assert(HeapTupleHeaderGetTypeId(tuple) == indesc->tdtypeid ||
3264  HeapTupleHeaderGetTypeId(tuple) == RECORDOID);
3265 
3266  /* if first time through, or after change, initialize conversion map */
3267  if (changed)
3268  {
3269  MemoryContext old_cxt;
3270 
3271  /* allocate map in long-lived memory context */
3272  old_cxt = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
3273 
3274  /* prepare map from old to new attribute numbers */
3275  op->d.convert_rowtype.map = convert_tuples_by_name(indesc, outdesc);
3276 
3277  MemoryContextSwitchTo(old_cxt);
3278  }
3279 
3280  /* Following steps need a HeapTuple not a bare HeapTupleHeader */
3281  tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
3282  tmptup.t_data = tuple;
3283 
3284  if (op->d.convert_rowtype.map != NULL)
3285  {
3286  /* Full conversion with attribute rearrangement needed */
3287  result = execute_attr_map_tuple(&tmptup, op->d.convert_rowtype.map);
3288  /* Result already has appropriate composite-datum header fields */
3289  *op->resvalue = HeapTupleGetDatum(result);
3290  }
3291  else
3292  {
3293  /*
3294  * The tuple is physically compatible as-is, but we need to insert the
3295  * destination rowtype OID in its composite-datum header field, so we
3296  * have to copy it anyway. heap_copy_tuple_as_datum() is convenient
3297  * for this since it will both make the physical copy and insert the
3298  * correct composite header fields. Note that we aren't expecting to
3299  * have to flatten any toasted fields: the input was a composite
3300  * datum, so it shouldn't contain any. So heap_copy_tuple_as_datum()
3301  * is overkill here, but its check for external fields is cheap.
3302  */
3303  *op->resvalue = heap_copy_tuple_as_datum(&tmptup, outdesc);
3304  }
3305 
3306  DecrTupleDescRefCount(indesc);
3307  DecrTupleDescRefCount(outdesc);
3308 }
static TupleDesc get_cached_rowtype(Oid type_id, int32 typmod, ExprEvalRowtypeCache *rowcache, bool *changed)
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:295
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc)
Definition: heaptuple.c:984
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:452
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:446
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:239
struct ExprEvalStep::@52::@82 convert_rowtype
uint32 t_len
Definition: htup.h:64
HeapTupleHeader t_data
Definition: htup.h:68
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
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:384
void IncrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:366

References Assert(), convert_tuples_by_name(), 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::op, HeapTupleData::t_data, HeapTupleData::t_len, and TupleDescData::tdtypeid.

Referenced by ExecInterpExpr().

◆ ExecEvalCurrentOfExpr()

void ExecEvalCurrentOfExpr ( ExprState state,
ExprEvalStep op 
)

Definition at line 2569 of file execExprInterp.c.

2570 {
2571  ereport(ERROR,
2572  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2573  errmsg("WHERE CURRENT OF is not supported for this table type")));
2574 }

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

Referenced by ExecInterpExpr().

◆ ExecEvalFieldSelect()

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

Definition at line 3031 of file execExprInterp.c.

3032 {
3033  AttrNumber fieldnum = op->d.fieldselect.fieldnum;
3034  Datum tupDatum;
3035  HeapTupleHeader tuple;
3036  Oid tupType;
3037  int32 tupTypmod;
3038  TupleDesc tupDesc;
3039  Form_pg_attribute attr;
3040  HeapTupleData tmptup;
3041 
3042  /* NULL record -> NULL result */
3043  if (*op->resnull)
3044  return;
3045 
3046  tupDatum = *op->resvalue;
3047 
3048  /* We can special-case expanded records for speed */
3050  {
3052 
3053  Assert(erh->er_magic == ER_MAGIC);
3054 
3055  /* Extract record's TupleDesc */
3056  tupDesc = expanded_record_get_tupdesc(erh);
3057 
3058  /*
3059  * Find field's attr record. Note we don't support system columns
3060  * here: a datum tuple doesn't have valid values for most of the
3061  * interesting system columns anyway.
3062  */
3063  if (fieldnum <= 0) /* should never happen */
3064  elog(ERROR, "unsupported reference to system column %d in FieldSelect",
3065  fieldnum);
3066  if (fieldnum > tupDesc->natts) /* should never happen */
3067  elog(ERROR, "attribute number %d exceeds number of columns %d",
3068  fieldnum, tupDesc->natts);
3069  attr = TupleDescAttr(tupDesc, fieldnum - 1);
3070 
3071  /* Check for dropped column, and force a NULL result if so */
3072  if (attr->attisdropped)
3073  {
3074  *op->resnull = true;
3075  return;
3076  }
3077 
3078  /* Check for type mismatch --- possible after ALTER COLUMN TYPE? */
3079  /* As in CheckVarSlotCompatibility, we should but can't check typmod */
3080  if (op->d.fieldselect.resulttype != attr->atttypid)
3081  ereport(ERROR,
3082  (errcode(ERRCODE_DATATYPE_MISMATCH),
3083  errmsg("attribute %d has wrong type", fieldnum),
3084  errdetail("Table has type %s, but query expects %s.",
3085  format_type_be(attr->atttypid),
3086  format_type_be(op->d.fieldselect.resulttype))));
3087 
3088  /* extract the field */
3089  *op->resvalue = expanded_record_get_field(erh, fieldnum,
3090  op->resnull);
3091  }
3092  else
3093  {
3094  /* Get the composite datum and extract its type fields */
3095  tuple = DatumGetHeapTupleHeader(tupDatum);
3096 
3097  tupType = HeapTupleHeaderGetTypeId(tuple);
3098  tupTypmod = HeapTupleHeaderGetTypMod(tuple);
3099 
3100  /* Lookup tupdesc if first time through or if type changes */
3101  tupDesc = get_cached_rowtype(tupType, tupTypmod,
3102  &op->d.fieldselect.rowcache, NULL);
3103 
3104  /*
3105  * Find field's attr record. Note we don't support system columns
3106  * here: a datum tuple doesn't have valid values for most of the
3107  * interesting system columns anyway.
3108  */
3109  if (fieldnum <= 0) /* should never happen */
3110  elog(ERROR, "unsupported reference to system column %d in FieldSelect",
3111  fieldnum);
3112  if (fieldnum > tupDesc->natts) /* should never happen */
3113  elog(ERROR, "attribute number %d exceeds number of columns %d",
3114  fieldnum, tupDesc->natts);
3115  attr = TupleDescAttr(tupDesc, fieldnum - 1);
3116 
3117  /* Check for dropped column, and force a NULL result if so */
3118  if (attr->attisdropped)
3119  {
3120  *op->resnull = true;
3121  return;
3122  }
3123 
3124  /* Check for type mismatch --- possible after ALTER COLUMN TYPE? */
3125  /* As in CheckVarSlotCompatibility, we should but can't check typmod */
3126  if (op->d.fieldselect.resulttype != attr->atttypid)
3127  ereport(ERROR,
3128  (errcode(ERRCODE_DATATYPE_MISMATCH),
3129  errmsg("attribute %d has wrong type", fieldnum),
3130  errdetail("Table has type %s, but query expects %s.",
3131  format_type_be(attr->atttypid),
3132  format_type_be(op->d.fieldselect.resulttype))));
3133 
3134  /* heap_getattr needs a HeapTuple not a bare HeapTupleHeader */
3135  tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
3136  tmptup.t_data = tuple;
3137 
3138  /* extract the field */
3139  *op->resvalue = heap_getattr(&tmptup,
3140  fieldnum,
3141  tupDesc,
3142  op->resnull);
3143  }
3144 }
int16 AttrNumber
Definition: attnum.h:21
static Datum expanded_record_get_field(ExpandedRecordHeader *erh, int fnumber, bool *isnull)
#define ER_MAGIC
static TupleDesc expanded_record_get_tupdesc(ExpandedRecordHeader *erh)
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: htup_details.h:788
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:462
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: postgres.h:335
struct ExprEvalStep::@52::@77 fieldselect

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

Referenced by ExecInterpExpr().

◆ ExecEvalFieldStoreDeForm()

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

Definition at line 3156 of file execExprInterp.c.

3157 {
3158  TupleDesc tupDesc;
3159 
3160  /* Lookup tupdesc if first time through or if type changes */
3161  tupDesc = get_cached_rowtype(op->d.fieldstore.fstore->resulttype, -1,
3162  op->d.fieldstore.rowcache, NULL);
3163 
3164  /* Check that current tupdesc doesn't have more fields than we allocated */
3165  if (unlikely(tupDesc->natts > op->d.fieldstore.ncolumns))
3166  elog(ERROR, "too many columns in composite type %u",
3167  op->d.fieldstore.fstore->resulttype);
3168 
3169  if (*op->resnull)
3170  {
3171  /* Convert null input tuple into an all-nulls row */
3172  memset(op->d.fieldstore.nulls, true,
3173  op->d.fieldstore.ncolumns * sizeof(bool));
3174  }
3175  else
3176  {
3177  /*
3178  * heap_deform_tuple needs a HeapTuple not a bare HeapTupleHeader. We
3179  * set all the fields in the struct just in case.
3180  */
3181  Datum tupDatum = *op->resvalue;
3182  HeapTupleHeader tuphdr;
3183  HeapTupleData tmptup;
3184 
3185  tuphdr = DatumGetHeapTupleHeader(tupDatum);
3186  tmptup.t_len = HeapTupleHeaderGetDatumLength(tuphdr);
3187  ItemPointerSetInvalid(&(tmptup.t_self));
3188  tmptup.t_tableOid = InvalidOid;
3189  tmptup.t_data = tuphdr;
3190 
3191  heap_deform_tuple(&tmptup, tupDesc,
3192  op->d.fieldstore.values,
3193  op->d.fieldstore.nulls);
3194  }
3195 }
#define unlikely(x)
Definition: c.h:273
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1249
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:172
#define InvalidOid
Definition: postgres_ext.h:36
struct ExprEvalStep::@52::@78 fieldstore
ItemPointerData t_self
Definition: htup.h:65
Oid t_tableOid
Definition: htup.h:66

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

Referenced by ExecInterpExpr().

◆ ExecEvalFieldStoreForm()

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

Definition at line 3202 of file execExprInterp.c.

3203 {
3204  TupleDesc tupDesc;
3205  HeapTuple tuple;
3206 
3207  /* Lookup tupdesc (should be valid already) */
3208  tupDesc = get_cached_rowtype(op->d.fieldstore.fstore->resulttype, -1,
3209  op->d.fieldstore.rowcache, NULL);
3210 
3211  tuple = heap_form_tuple(tupDesc,
3212  op->d.fieldstore.values,
3213  op->d.fieldstore.nulls);
3214 
3215  *op->resvalue = HeapTupleGetDatum(tuple);
3216  *op->resnull = false;
3217 }
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020

References get_cached_rowtype(), heap_form_tuple(), HeapTupleGetDatum, and ExprEvalStep::op.

Referenced by ExecInterpExpr().

◆ ExecEvalFuncExprFusage()

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

Definition at line 2380 of file execExprInterp.c.

2382 {
2383  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
2384  PgStat_FunctionCallUsage fcusage;
2385  Datum d;
2386 
2387  pgstat_init_function_usage(fcinfo, &fcusage);
2388 
2389  fcinfo->isnull = false;
2390  d = op->d.func.fn_addr(fcinfo);
2391  *op->resvalue = d;
2392  *op->resnull = fcinfo->isnull;
2393 
2394  pgstat_end_function_usage(&fcusage, true);
2395 }
void pgstat_init_function_usage(FunctionCallInfo fcinfo, PgStat_FunctionCallUsage *fcu)
void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu, bool finalize)
struct ExprEvalStep::@52::@59 func

References ExprEvalStep::d, FunctionCallInfoBaseData::isnull, ExprEvalStep::op, pgstat_end_function_usage(), and pgstat_init_function_usage().

Referenced by ExecInterpExpr().

◆ ExecEvalFuncExprStrictFusage()

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

Definition at line 2401 of file execExprInterp.c.

2403 {
2404 
2405  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
2406  PgStat_FunctionCallUsage fcusage;
2407  NullableDatum *args = fcinfo->args;
2408  int nargs = op->d.func.nargs;
2409  Datum d;
2410 
2411  /* strict function, so check for NULL args */
2412  for (int argno = 0; argno < nargs; argno++)
2413  {
2414  if (args[argno].isnull)
2415  {
2416  *op->resnull = true;
2417  return;
2418  }
2419  }
2420 
2421  pgstat_init_function_usage(fcinfo, &fcusage);
2422 
2423  fcinfo->isnull = false;
2424  d = op->d.func.fn_addr(fcinfo);
2425  *op->resvalue = d;
2426  *op->resnull = fcinfo->isnull;
2427 
2428  pgstat_end_function_usage(&fcusage, true);
2429 }

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

Referenced by ExecInterpExpr().

◆ ExecEvalGroupingFunc()

void ExecEvalGroupingFunc ( ExprState state,
ExprEvalStep op 
)

Definition at line 4030 of file execExprInterp.c.

4031 {
4032  AggState *aggstate = castNode(AggState, state->parent);
4033  int result = 0;
4034  Bitmapset *grouped_cols = aggstate->grouped_cols;
4035  ListCell *lc;
4036 
4037  foreach(lc, op->d.grouping_func.clauses)
4038  {
4039  int attnum = lfirst_int(lc);
4040 
4041  result <<= 1;
4042 
4043  if (!bms_is_member(attnum, grouped_cols))
4044  result |= 1;
4045  }
4046 
4047  *op->resvalue = Int32GetDatum(result);
4048  *op->resnull = false;
4049 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
#define castNode(_type_, nodeptr)
Definition: nodes.h:642
#define lfirst_int(lc)
Definition: pg_list.h:170
#define Int32GetDatum(X)
Definition: postgres.h:523
Bitmapset * grouped_cols
Definition: execnodes.h:2350
struct ExprEvalStep::@52::@87 grouping_func

References attnum, bms_is_member(), castNode, AggState::grouped_cols, Int32GetDatum, lfirst_int, and ExprEvalStep::op.

Referenced by ExecInterpExpr().

◆ ExecEvalHashedScalarArrayOp()

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

Definition at line 3524 of file execExprInterp.c.

3525 {
3526  ScalarArrayOpExprHashTable *elements_tab = op->d.hashedscalararrayop.elements_tab;
3527  FunctionCallInfo fcinfo = op->d.hashedscalararrayop.fcinfo_data;
3528  bool inclause = op->d.hashedscalararrayop.inclause;
3529  bool strictfunc = op->d.hashedscalararrayop.finfo->fn_strict;
3530  Datum scalar = fcinfo->args[0].value;
3531  bool scalar_isnull = fcinfo->args[0].isnull;
3532  Datum result;
3533  bool resultnull;
3534  bool hashfound;
3535 
3536  /* We don't setup a hashed scalar array op if the array const is null. */
3537  Assert(!*op->resnull);
3538 
3539  /*
3540  * If the scalar is NULL, and the function is strict, return NULL; no
3541  * point in executing the search.
3542  */
3543  if (fcinfo->args[0].isnull && strictfunc)
3544  {
3545  *op->resnull = true;
3546  return;
3547  }
3548 
3549  /* Build the hash table on first evaluation */
3550  if (elements_tab == NULL)
3551  {
3552  int16 typlen;
3553  bool typbyval;
3554  char typalign;
3555  int nitems;
3556  bool has_nulls = false;
3557  char *s;
3558  bits8 *bitmap;
3559  int bitmask;
3560  MemoryContext oldcontext;
3561  ArrayType *arr;
3562 
3563  arr = DatumGetArrayTypeP(*op->resvalue);
3564  nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
3565 
3567  &typlen,
3568  &typbyval,
3569  &typalign);
3570 
3571  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
3572 
3573  elements_tab = (ScalarArrayOpExprHashTable *)
3575  op->d.hashedscalararrayop.elements_tab = elements_tab;
3576  elements_tab->op = op;
3577 
3578  /*
3579  * Create the hash table sizing it according to the number of elements
3580  * in the array. This does assume that the array has no duplicates.
3581  * If the array happens to contain many duplicate values then it'll
3582  * just mean that we sized the table a bit on the large side.
3583  */
3584  elements_tab->hashtab = saophash_create(CurrentMemoryContext, nitems,
3585  elements_tab);
3586 
3587  MemoryContextSwitchTo(oldcontext);
3588 
3589  s = (char *) ARR_DATA_PTR(arr);
3590  bitmap = ARR_NULLBITMAP(arr);
3591  bitmask = 1;
3592  for (int i = 0; i < nitems; i++)
3593  {
3594  /* Get array element, checking for NULL. */
3595  if (bitmap && (*bitmap & bitmask) == 0)
3596  {
3597  has_nulls = true;
3598  }
3599  else
3600  {
3601  Datum element;
3602 
3603  element = fetch_att(s, typbyval, typlen);
3604  s = att_addlength_pointer(s, typlen, s);
3605  s = (char *) att_align_nominal(s, typalign);
3606 
3607  saophash_insert(elements_tab->hashtab, element, &hashfound);
3608  }
3609 
3610  /* Advance bitmap pointer if any. */
3611  if (bitmap)
3612  {
3613  bitmask <<= 1;
3614  if (bitmask == 0x100)
3615  {
3616  bitmap++;
3617  bitmask = 1;
3618  }
3619  }
3620  }
3621 
3622  /*
3623  * Remember if we had any nulls so that we know if we need to execute
3624  * non-strict functions with a null lhs value if no match is found.
3625  */
3626  op->d.hashedscalararrayop.has_nulls = has_nulls;
3627  }
3628 
3629  /* Check the hash to see if we have a match. */
3630  hashfound = NULL != saophash_lookup(elements_tab->hashtab, scalar);
3631 
3632  /* the result depends on if the clause is an IN or NOT IN clause */
3633  if (inclause)
3634  result = BoolGetDatum(hashfound); /* IN */
3635  else
3636  result = BoolGetDatum(!hashfound); /* NOT IN */
3637 
3638  resultnull = false;
3639 
3640  /*
3641  * If we didn't find a match in the array, we still might need to handle
3642  * the possibility of null values. We didn't put any NULLs into the
3643  * hashtable, but instead marked if we found any when building the table
3644  * in has_nulls.
3645  */
3646  if (!hashfound && op->d.hashedscalararrayop.has_nulls)
3647  {
3648  if (strictfunc)
3649  {
3650 
3651  /*
3652  * We have nulls in the array so a non-null lhs and no match must
3653  * yield NULL.
3654  */
3655  result = (Datum) 0;
3656  resultnull = true;
3657  }
3658  else
3659  {
3660  /*
3661  * Execute function will null rhs just once.
3662  *
3663  * The hash lookup path will have scribbled on the lhs argument so
3664  * we need to set it up also (even though we entered this function
3665  * with it already set).
3666  */
3667  fcinfo->args[0].value = scalar;
3668  fcinfo->args[0].isnull = scalar_isnull;
3669  fcinfo->args[1].value = (Datum) 0;
3670  fcinfo->args[1].isnull = true;
3671 
3672  result = op->d.hashedscalararrayop.fn_addr(fcinfo);
3673  resultnull = fcinfo->isnull;
3674 
3675  /*
3676  * Reverse the result for NOT IN clauses since the above function
3677  * is the equality function and we need not-equals.
3678  */
3679  if (!inclause)
3680  result = !result;
3681  }
3682  }
3683 
3684  *op->resvalue = result;
3685  *op->resnull = resultnull;
3686 }
signed short int16
Definition: c.h:428
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2228
char typalign
Definition: pg_type.h:176
#define BoolGetDatum(X)
Definition: postgres.h:446
static chr element(struct vars *v, const chr *startp, const chr *endp)
Definition: regc_locale.c:376
struct ExprEvalStep::@52::@84 hashedscalararrayop
struct ExprEvalStep * op
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:148
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:176
#define fetch_att(T, attbyval, attlen)
Definition: tupmacs.h:75

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

Referenced by ExecInterpExpr().

◆ ExecEvalJson()

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

Definition at line 5066 of file execExprInterp.c.

5067 {
5069  JsonExpr *jexpr = op->d.jsonexpr.jsexpr;
5070  Datum item;
5071  Datum res = (Datum) 0;
5072  JsonPath *path;
5073  ListCell *lc;
5074  bool error = false;
5075  bool needSubtrans;
5076  bool throwErrors = jexpr->on_error->btype == JSON_BEHAVIOR_ERROR;
5077 
5078  *op->resnull = true; /* until we get a result */
5079  *op->resvalue = (Datum) 0;
5080 
5081  if (op->d.jsonexpr.formatted_expr->isnull || op->d.jsonexpr.pathspec->isnull)
5082  {
5083  /* execute domain checks for NULLs */
5084  (void) ExecEvalJsonExprCoercion(op, econtext, res, op->resnull,
5085  NULL, NULL);
5086 
5087  Assert(*op->resnull);
5088  return;
5089  }
5090 
5091  item = op->d.jsonexpr.formatted_expr->value;
5092  path = DatumGetJsonPathP(op->d.jsonexpr.pathspec->value);
5093 
5094  /* reset JSON path variable contexts */
5095  foreach(lc, op->d.jsonexpr.args)
5096  {
5098 
5099  var->econtext = econtext;
5100  var->evaluated = false;
5101  }
5102 
5103  needSubtrans = ExecEvalJsonNeedsSubTransaction(jexpr, &op->d.jsonexpr.coercions);
5104 
5105  cxt.path = path;
5106  cxt.error = throwErrors ? NULL : &error;
5107  cxt.coercionInSubtrans = !needSubtrans && !throwErrors;
5108  Assert(!needSubtrans || cxt.error);
5109 
5110  res = ExecEvalJsonExprSubtrans(ExecEvalJsonExpr, op, econtext, item,
5111  op->resnull, &cxt, cxt.error,
5112  needSubtrans);
5113 
5114  if (error)
5115  {
5116  /* Execute ON ERROR behavior */
5117  res = ExecEvalJsonBehavior(econtext, jexpr->on_error,
5118  op->d.jsonexpr.default_on_error,
5119  op->resnull);
5120 
5121  /* result is already coerced in DEFAULT behavior case */
5122  if (jexpr->on_error->btype != JSON_BEHAVIOR_DEFAULT)
5123  res = ExecEvalJsonExprCoercion(op, econtext, res,
5124  op->resnull,
5125  NULL, NULL);
5126  }
5127 
5128  *op->resvalue = res;
5129 }
static Datum ExecEvalJsonExprCoercion(ExprEvalStep *op, ExprContext *econtext, Datum res, bool *isNull, void *p, bool *error)
static Datum ExecEvalJsonExpr(ExprEvalStep *op, ExprContext *econtext, Datum item, bool *resnull, void *pcxt, bool *error)
bool ExecEvalJsonNeedsSubTransaction(JsonExpr *jsexpr, struct JsonCoercionsState *coercions)
static Datum ExecEvalJsonBehavior(ExprContext *econtext, JsonBehavior *behavior, ExprState *default_estate, bool *is_null)
static Datum ExecEvalJsonExprSubtrans(JsonFunc func, ExprEvalStep *op, ExprContext *econtext, Datum res, bool *resnull, void *p, bool *error, bool subtrans)
#define DatumGetJsonPathP(d)
Definition: jsonpath.h:35
@ JSON_BEHAVIOR_ERROR
Definition: primnodes.h:1291
@ JSON_BEHAVIOR_DEFAULT
Definition: primnodes.h:1298
static void error(void)
Definition: sql-dyntest.c:147
struct ExprEvalStep::@52::@96 jsonexpr
JsonBehaviorType btype
Definition: primnodes.h:1409
JsonBehavior * on_error
Definition: primnodes.h:1462

References Assert(), JsonBehavior::btype, ExecEvalJsonExprContext::coercionInSubtrans, ExprEvalStep::d, DatumGetJsonPathP, JsonPathVariableEvalContext::econtext, ExecEvalJsonExprContext::error, error(), JsonPathVariableEvalContext::evaluated, ExecEvalJsonBehavior(), ExecEvalJsonExpr(), ExecEvalJsonExprCoercion(), ExecEvalJsonExprSubtrans(), ExecEvalJsonNeedsSubTransaction(), JSON_BEHAVIOR_DEFAULT, JSON_BEHAVIOR_ERROR, ExprEvalStep::jsonexpr, lfirst, JsonExpr::on_error, ExecEvalJsonExprContext::path, res, ExprEvalStep::resnull, and ExprEvalStep::resvalue.

Referenced by ExecInterpExpr().

◆ ExecEvalJsonBehavior()

static Datum ExecEvalJsonBehavior ( ExprContext econtext,
JsonBehavior behavior,
ExprState default_estate,
bool is_null 
)
static

Definition at line 4588 of file execExprInterp.c.

4590 {
4591  *is_null = false;
4592 
4593  switch (behavior->btype)
4594  {
4597 
4600 
4601  case JSON_BEHAVIOR_TRUE:
4602  return BoolGetDatum(true);
4603 
4604  case JSON_BEHAVIOR_FALSE:
4605  return BoolGetDatum(false);
4606 
4607  case JSON_BEHAVIOR_NULL:
4608  case JSON_BEHAVIOR_UNKNOWN:
4609  case JSON_BEHAVIOR_EMPTY:
4610  *is_null = true;
4611  return (Datum) 0;
4612 
4613  case JSON_BEHAVIOR_DEFAULT:
4614  return ExecEvalExpr(default_estate, econtext, is_null);
4615 
4616  default:
4617  elog(ERROR, "unrecognized SQL/JSON behavior %d", behavior->btype);
4618  return (Datum) 0;
4619  }
4620 }
Jsonb * JsonbMakeEmptyArray(void)
Definition: jsonb.c:2225
Jsonb * JsonbMakeEmptyObject(void)
Definition: jsonb.c:2241
#define JsonbPGetDatum(p)
Definition: jsonb.h:73
@ JSON_BEHAVIOR_TRUE
Definition: primnodes.h:1293
@ JSON_BEHAVIOR_EMPTY
Definition: primnodes.h:1292
@ JSON_BEHAVIOR_FALSE
Definition: primnodes.h:1294
@ JSON_BEHAVIOR_NULL
Definition: primnodes.h:1290
@ JSON_BEHAVIOR_EMPTY_OBJECT
Definition: primnodes.h:1297
@ JSON_BEHAVIOR_UNKNOWN
Definition: primnodes.h:1295
@ JSON_BEHAVIOR_EMPTY_ARRAY
Definition: primnodes.h:1296

References BoolGetDatum, JsonBehavior::btype, elog, ERROR, ExecEvalExpr(), JSON_BEHAVIOR_DEFAULT, JSON_BEHAVIOR_EMPTY, JSON_BEHAVIOR_EMPTY_ARRAY, JSON_BEHAVIOR_EMPTY_OBJECT, JSON_BEHAVIOR_FALSE, JSON_BEHAVIOR_NULL, JSON_BEHAVIOR_TRUE, JSON_BEHAVIOR_UNKNOWN, JsonbMakeEmptyArray(), JsonbMakeEmptyObject(), and JsonbPGetDatum.

Referenced by ExecEvalJson(), and ExecEvalJsonExpr().

◆ ExecEvalJsonConstructor()

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

Definition at line 4509 of file execExprInterp.c.

4511 {
4512  Datum res;
4513  JsonConstructorExpr *ctor = op->d.json_constructor.constructor;
4514  bool is_jsonb = ctor->returning->format->format_type == JS_FORMAT_JSONB;
4515  bool isnull = false;
4516 
4517  if (ctor->type == JSCTOR_JSON_ARRAY)
4518  res = (is_jsonb ?
4521  op->d.json_constructor.arg_values,
4522  op->d.json_constructor.arg_nulls,
4523  op->d.json_constructor.arg_types,
4524  op->d.json_constructor.constructor->absent_on_null);
4525  else if (ctor->type == JSCTOR_JSON_OBJECT)
4526  res = (is_jsonb ?
4529  op->d.json_constructor.arg_values,
4530  op->d.json_constructor.arg_nulls,
4531  op->d.json_constructor.arg_types,
4532  op->d.json_constructor.constructor->absent_on_null,
4533  op->d.json_constructor.constructor->unique);
4534  else if (ctor->type == JSCTOR_JSON_SCALAR)
4535  {
4536  if (op->d.json_constructor.arg_nulls[0])
4537  {
4538  res = (Datum) 0;
4539  isnull = true;
4540  }
4541  else
4542  {
4543  Datum value = op->d.json_constructor.arg_values[0];
4544  int category = op->d.json_constructor.arg_type_cache[0].category;
4545  Oid outfuncid = op->d.json_constructor.arg_type_cache[0].outfuncid;
4546 
4547  if (is_jsonb)
4548  res = to_jsonb_worker(value, category, outfuncid);
4549  else
4550  res = to_json_worker(value, category, outfuncid);
4551  }
4552  }
4553  else if (ctor->type == JSCTOR_JSON_PARSE)
4554  {
4555  if (op->d.json_constructor.arg_nulls[0])
4556  {
4557  res = (Datum) 0;
4558  isnull = true;
4559  }
4560  else
4561  {
4562  Datum value = op->d.json_constructor.arg_values[0];
4563  text *js = DatumGetTextP(value);
4564 
4565  if (is_jsonb)
4566  res = jsonb_from_text(js, true);
4567  else
4568  {
4569  (void) json_validate(js, true, true);
4570  res = value;
4571  }
4572  }
4573  }
4574  else
4575  {
4576  res = (Datum) 0;
4577  elog(ERROR, "invalid JsonConstructorExpr type %d", ctor->type);
4578  }
4579 
4580  *op->resvalue = res;
4581  *op->resnull = isnull;
4582 }
#define DatumGetTextP(X)
Definition: fmgr.h:332
static struct @151 value
Datum json_build_array_worker(int nargs, Datum *args, bool *nulls, Oid *types, bool absent_on_null)
Definition: json.c:1352
Datum json_build_object_worker(int nargs, Datum *args, bool *nulls, Oid *types, bool absent_on_null, bool unique_keys)
Definition: json.c:1238
bool json_validate(text *json, bool check_unique_keys, bool throw_error)
Definition: json.c:1677
Datum to_json_worker(Datum val, JsonTypeCategory tcategory, Oid outfuncoid)
Definition: json.c:748
Datum jsonb_from_text(text *js, bool unique_keys)
Definition: jsonb.c:146
Datum to_jsonb_worker(Datum val, JsonbTypeCategory tcategory, Oid outfuncoid)
Definition: jsonb.c:1119
Datum jsonb_build_array_worker(int nargs, Datum *args, bool *nulls, Oid *types, bool absent_on_null)
Definition: jsonb.c:1271
Datum jsonb_build_object_worker(int nargs, Datum *args, bool *nulls, Oid *types, bool absent_on_null, bool unique_keys)
Definition: jsonb.c:1186
@ JS_FORMAT_JSONB
Definition: primnodes.h:1277
@ JSCTOR_JSON_PARSE
Definition: primnodes.h:1356
@ JSCTOR_JSON_OBJECT
Definition: primnodes.h:1350
@ JSCTOR_JSON_SCALAR
Definition: primnodes.h:1354
@ JSCTOR_JSON_ARRAY
Definition: primnodes.h:1351
struct ExprEvalStep::@52::@94 json_constructor
JsonReturning * returning
Definition: primnodes.h:1370
JsonConstructorType type
Definition: primnodes.h:1366
JsonFormatType format_type
Definition: primnodes.h:1319
JsonFormat * format
Definition: primnodes.h:1331
Definition: c.h:622

References ExprEvalStep::category, DatumGetTextP, elog, ERROR, JsonReturning::format, JsonFormat::format_type, ExprEvalStep::isnull, JS_FORMAT_JSONB, JSCTOR_JSON_ARRAY, JSCTOR_JSON_OBJECT, JSCTOR_JSON_PARSE, JSCTOR_JSON_SCALAR, json_build_array_worker(), json_build_object_worker(), json_validate(), jsonb_build_array_worker(), jsonb_build_object_worker(), jsonb_from_text(), ExprEvalStep::op, ExprEvalStep::outfuncid, res, JsonConstructorExpr::returning, to_json_worker(), to_jsonb_worker(), JsonConstructorExpr::type, and value.

Referenced by ExecInterpExpr().

◆ ExecEvalJsonExpr()

static Datum ExecEvalJsonExpr ( ExprEvalStep op,
ExprContext econtext,
Datum  item,
bool resnull,
void *  pcxt,
bool error 
)
static

Definition at line 4892 of file execExprInterp.c.

4895 {
4896  ExecEvalJsonExprContext *cxt = pcxt;
4897  JsonPath *path = cxt->path;
4898  JsonExpr *jexpr = op->d.jsonexpr.jsexpr;
4899  ExprState *estate = NULL;
4900  bool empty = false;
4901  Datum res = (Datum) 0;
4902 
4903  switch (jexpr->op)
4904  {
4905  case JSON_QUERY_OP:
4906  res = JsonPathQuery(item, path, jexpr->wrapper, &empty, error,
4907  op->d.jsonexpr.args);
4908  if (error && *error)
4909  {
4910  *resnull = true;
4911  return (Datum) 0;
4912  }
4913  *resnull = !DatumGetPointer(res);
4914  break;
4915 
4916  case JSON_VALUE_OP:
4917  {
4918  struct JsonCoercionState *jcstate;
4919  JsonbValue *jbv = JsonPathValue(item, path, &empty, error,
4920  op->d.jsonexpr.args);
4921 
4922  if (error && *error)
4923  return (Datum) 0;
4924 
4925  if (!jbv) /* NULL or empty */
4926  break;
4927 
4928  Assert(!empty);
4929 
4930  *resnull = false;
4931 
4932  /* coerce scalar item to the output type */
4933  if (jexpr->returning->typid == JSONOID ||
4934  jexpr->returning->typid == JSONBOID)
4935  {
4936  /* Use result coercion from json[b] to the output type */
4938  break;
4939  }
4940 
4941  /* Use coercion from SQL/JSON item type to the output type */
4943  op->d.jsonexpr.jsexpr->returning,
4944  &op->d.jsonexpr.coercions,
4945  &jcstate);
4946 
4947  if (jcstate->coercion &&
4948  (jcstate->coercion->via_io ||
4949  jcstate->coercion->via_populate))
4950  {
4951  if (error)
4952  {
4953  *error = true;
4954  return (Datum) 0;
4955  }
4956 
4957  /*
4958  * Coercion via I/O means here that the cast to the target
4959  * type simply does not exist.
4960  */
4961  ereport(ERROR,
4962 
4963  /*
4964  * XXX Standard says about a separate error code
4965  * ERRCODE_SQL_JSON_ITEM_CANNOT_BE_CAST_TO_TARGET_TYPE but
4966  * does not define its number.
4967  */
4968  (errcode(ERRCODE_SQL_JSON_SCALAR_REQUIRED),
4969  errmsg("SQL/JSON item cannot be cast to target type")));
4970  }
4971  else if (!jcstate->estate)
4972  return res; /* no coercion */
4973 
4974  /* coerce using specific expression */
4975  estate = jcstate->estate;
4976  op->d.jsonexpr.coercion_expr->value = res;
4977  op->d.jsonexpr.coercion_expr->isnull = *resnull;
4978  break;
4979  }
4980 
4981  case JSON_EXISTS_OP:
4982  {
4983  bool exists = JsonPathExists(item, path,
4984  op->d.jsonexpr.args,
4985  error);
4986 
4987  *resnull = error && *error;
4988  res = BoolGetDatum(exists);
4989 
4990  if (!op->d.jsonexpr.result_expr)
4991  return res;
4992 
4993  /* coerce using result expression */
4994  estate = op->d.jsonexpr.result_expr;
4995  op->d.jsonexpr.res_expr->value = res;
4996  op->d.jsonexpr.res_expr->isnull = *resnull;
4997  break;
4998  }
4999 
5000  case JSON_TABLE_OP:
5001  *resnull = false;
5002  return item;
5003 
5004  default:
5005  elog(ERROR, "unrecognized SQL/JSON expression op %d", jexpr->op);
5006  return (Datum) 0;
5007  }
5008 
5009  if (empty)
5010  {
5011  Assert(jexpr->on_empty); /* it is not JSON_EXISTS */
5012 
5013  if (jexpr->on_empty->btype == JSON_BEHAVIOR_ERROR)
5014  {
5015  if (error)
5016  {
5017  *error = true;
5018  return (Datum) 0;
5019  }
5020 
5021  ereport(ERROR,
5022  (errcode(ERRCODE_NO_SQL_JSON_ITEM),
5023  errmsg("no SQL/JSON item")));
5024  }
5025 
5026  if (jexpr->on_empty->btype == JSON_BEHAVIOR_DEFAULT)
5027 
5028  /*
5029  * Execute DEFAULT expression as a coercion expression, because
5030  * its result is already coerced to the target type.
5031  */
5032  estate = op->d.jsonexpr.default_on_empty;
5033  else
5034  /* Execute ON EMPTY behavior */
5035  res = ExecEvalJsonBehavior(econtext, jexpr->on_empty,
5036  op->d.jsonexpr.default_on_empty,
5037  resnull);
5038  }
5039 
5041  res, resnull, estate, error,
5042  cxt->coercionInSubtrans);
5043 }
Datum ExecPrepareJsonItemCoercion(JsonbValue *item, JsonReturning *returning, struct JsonCoercionsState *coercions, struct JsonCoercionState **pcoercion)
Jsonb * JsonbValueToJsonb(JsonbValue *val)
Definition: jsonb_util.c:94
Datum JsonPathQuery(Datum jb, JsonPath *jp, JsonWrapper wrapper, bool *empty, bool *error, List *vars)
bool JsonPathExists(Datum jb, JsonPath *jp, List *vars, bool *error)
JsonbValue * JsonPathValue(Datum jb, JsonPath *jp, bool *empty, bool *error, List *vars)
@ JSON_QUERY_OP
Definition: primnodes.h:1252
@ JSON_TABLE_OP
Definition: primnodes.h:1254
@ JSON_EXISTS_OP
Definition: primnodes.h:1253
@ JSON_VALUE_OP
Definition: primnodes.h:1251
JsonBehavior * on_empty
Definition: primnodes.h:1461
JsonReturning * returning
Definition: primnodes.h:1460
JsonWrapper wrapper
Definition: primnodes.h:1464
JsonExprOp op
Definition: primnodes.h:1453

References Assert(), BoolGetDatum, JsonBehavior::btype, ExecEvalJsonExprContext::coercionInSubtrans, ExprEvalStep::d, DatumGetPointer, elog, ereport, errcode(), errmsg(), ERROR, error(), ExecEvalJsonBehavior(), ExecEvalJsonExprCoercion(), ExecEvalJsonExprSubtrans(), ExecPrepareJsonItemCoercion(), JSON_BEHAVIOR_DEFAULT, JSON_BEHAVIOR_ERROR, JSON_EXISTS_OP, JSON_QUERY_OP, JSON_TABLE_OP, JSON_VALUE_OP, JsonbPGetDatum, JsonbValueToJsonb(), ExprEvalStep::jsonexpr, JsonPathExists(), JsonPathQuery(), JsonPathValue(), JsonExpr::on_empty, JsonExpr::op, ExecEvalJsonExprContext::path, res, JsonExpr::returning, JsonReturning::typid, and JsonExpr::wrapper.

Referenced by ExecEvalJson().

◆ ExecEvalJsonExprCoercion()

static Datum ExecEvalJsonExprCoercion ( ExprEvalStep op,
ExprContext econtext,
Datum  res,
bool isNull,
void *  p,
bool error 
)
static

Definition at line 4626 of file execExprInterp.c.

4628 {
4629  ExprState *estate = p;
4630 
4631  if (estate) /* coerce using specified expression */
4632  return ExecEvalExpr(estate, econtext, isNull);
4633 
4634  if (op->d.jsonexpr.jsexpr->op != JSON_EXISTS_OP)
4635  {
4636  JsonCoercion *coercion = op->d.jsonexpr.jsexpr->result_coercion;
4637  JsonExpr *jexpr = op->d.jsonexpr.jsexpr;
4638  Jsonb *jb = *isNull ? NULL : DatumGetJsonbP(res);
4639 
4640  if ((coercion && coercion->via_io) ||
4641  (jexpr->omit_quotes && !*isNull &&
4642  JB_ROOT_IS_SCALAR(jb)))
4643  {
4644  /* strip quotes and call typinput function */
4645  char *str = *isNull ? NULL : JsonbUnquote(jb);
4646 
4647  return InputFunctionCall(&op->d.jsonexpr.input.func, str,
4648  op->d.jsonexpr.input.typioparam,
4649  jexpr->returning->typmod);
4650  }
4651  else if (coercion && coercion->via_populate)
4652  return json_populate_type(res, JSONBOID,
4653  jexpr->returning->typid,
4654  jexpr->returning->typmod,
4655  &op->d.jsonexpr.cache,
4656  econtext->ecxt_per_query_memory,
4657  isNull);
4658  }
4659 
4660  if (op->d.jsonexpr.result_expr)
4661  {
4662  op->d.jsonexpr.res_expr->value = res;
4663  op->d.jsonexpr.res_expr->isnull = *isNull;
4664 
4665  res = ExecEvalExpr(op->d.jsonexpr.result_expr, econtext, isNull);
4666  }
4667 
4668  return res;
4669 }
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1515
char * JsonbUnquote(Jsonb *jb)
Definition: jsonb.c:2256
#define JB_ROOT_IS_SCALAR(jbp_)
Definition: jsonb.h:228
#define DatumGetJsonbP(d)
Definition: jsonb.h:71
Datum json_populate_type(Datum json_val, Oid json_type, Oid typid, int32 typmod, void **cache, MemoryContext mcxt, bool *isnull)
Definition: jsonfuncs.c:3137
bool via_populate
Definition: primnodes.h:1421
bool omit_quotes
Definition: primnodes.h:1465
Definition: jsonb.h:221

References ExprEvalStep::coercion, DatumGetJsonbP, ExprContext::ecxt_per_query_memory, ExprEvalStep::estate, ExecEvalExpr(), InputFunctionCall(), JB_ROOT_IS_SCALAR, JSON_EXISTS_OP, json_populate_type(), JsonbUnquote(), JsonExpr::omit_quotes, ExprEvalStep::op, res, JsonExpr::returning, generate_unaccent_rules::str, JsonReturning::typid, JsonReturning::typmod, JsonCoercion::via_io, and JsonCoercion::via_populate.

Referenced by ExecEvalJson(), and ExecEvalJsonExpr().

◆ ExecEvalJsonExprSubtrans()

static Datum ExecEvalJsonExprSubtrans ( JsonFunc  func,
ExprEvalStep op,
ExprContext econtext,
Datum  res,
bool resnull,
void *  p,
bool error,
bool  subtrans 
)
static

Definition at line 4819 of file execExprInterp.c.

4823 {
4824  MemoryContext oldcontext;
4825  ResourceOwner oldowner;
4826 
4827  if (!subtrans)
4828  /* No need to use subtransactions. */
4829  return func(op, econtext, res, resnull, p, error);
4830 
4831  /*
4832  * We should catch exceptions of category ERRCODE_DATA_EXCEPTION and
4833  * execute the corresponding ON ERROR behavior then.
4834  */
4835  oldcontext = CurrentMemoryContext;
4836  oldowner = CurrentResourceOwner;
4837 
4838  Assert(error);
4839 
4841  /* Want to execute expressions inside function's memory context */
4842  MemoryContextSwitchTo(oldcontext);
4843 
4844  PG_TRY();
4845  {
4846  res = func(op, econtext, res, resnull, p, error);
4847 
4848  /* Commit the inner transaction, return to outer xact context */
4850  MemoryContextSwitchTo(oldcontext);
4851  CurrentResourceOwner = oldowner;
4852  }
4853  PG_CATCH();
4854  {
4855  ErrorData *edata;
4856  int ecategory;
4857 
4858  /* Save error info in oldcontext */
4859  MemoryContextSwitchTo(oldcontext);
4860  edata = CopyErrorData();
4861  FlushErrorState();
4862 
4863  /* Abort the inner transaction */
4865  MemoryContextSwitchTo(oldcontext);
4866  CurrentResourceOwner = oldowner;
4867 
4868  ecategory = ERRCODE_TO_CATEGORY(edata->sqlerrcode);
4869 
4870  if (ecategory != ERRCODE_DATA_EXCEPTION && /* jsonpath and other data
4871  * errors */
4872  ecategory != ERRCODE_INTEGRITY_CONSTRAINT_VIOLATION) /* domain errors */
4873  ReThrowError(edata);
4874 
4875  res = (Datum) 0;
4876  *error = true;
4877  }
4878  PG_END_TRY();
4879 
4880  return res;
4881 }
void ReThrowError(ErrorData *edata)
Definition: elog.c:1733
void FlushErrorState(void)
Definition: elog.c:1649
ErrorData * CopyErrorData(void)
Definition: elog.c:1555
#define PG_END_TRY()
Definition: elog.h:324
#define PG_TRY()
Definition: elog.h:299
#define ERRCODE_TO_CATEGORY(ec)
Definition: elog.h:55
#define PG_CATCH()
Definition: elog.h:309
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
int sqlerrcode
Definition: elog.h:367
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4490
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4595
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4561

References Assert(), BeginInternalSubTransaction(), CopyErrorData(), CurrentMemoryContext, CurrentResourceOwner, ERRCODE_TO_CATEGORY, error(), FlushErrorState(), MemoryContextSwitchTo(), PG_CATCH, PG_END_TRY, PG_TRY, ReleaseCurrentSubTransaction(), res, ReThrowError(), RollbackAndReleaseCurrentSubTransaction(), and ErrorData::sqlerrcode.

Referenced by ExecEvalJson(), and ExecEvalJsonExpr().

◆ ExecEvalJsonIsPredicate()

void ExecEvalJsonIsPredicate ( ExprState state,
ExprEvalStep op 
)

Definition at line 3936 of file execExprInterp.c.

3937 {
3938  JsonIsPredicate *pred = op->d.is_json.pred;
3939  Datum js = *op->resvalue;
3940  Oid exprtype;
3941  bool res;
3942 
3943  if (*op->resnull)
3944  {
3945  *op->resvalue = BoolGetDatum(false);
3946  return;
3947  }
3948 
3949  exprtype = exprType(pred->expr);
3950 
3951  if (exprtype == TEXTOID || exprtype == JSONOID)
3952  {
3953  text *json = DatumGetTextP(js);
3954 
3955  if (pred->item_type == JS_TYPE_ANY)
3956  res = true;
3957  else
3958  {
3959  switch (json_get_first_token(json, false))
3960  {
3962  res = pred->item_type == JS_TYPE_OBJECT;
3963  break;
3965  res = pred->item_type == JS_TYPE_ARRAY;
3966  break;
3967  case JSON_TOKEN_STRING:
3968  case JSON_TOKEN_NUMBER:
3969  case JSON_TOKEN_TRUE:
3970  case JSON_TOKEN_FALSE:
3971  case JSON_TOKEN_NULL:
3972  res = pred->item_type == JS_TYPE_SCALAR;
3973  break;
3974  default:
3975  res = false;
3976  break;
3977  }
3978  }
3979 
3980  /*
3981  * Do full parsing pass only for uniqueness check or for JSON text
3982  * validation.
3983  */
3984  if (res && (pred->unique_keys || exprtype == TEXTOID))
3985  res = json_validate(json, pred->unique_keys, false);
3986  }
3987  else if (exprtype == JSONBOID)
3988  {
3989  if (pred->item_type == JS_TYPE_ANY)
3990  res = true;
3991  else
3992  {
3993  Jsonb *jb = DatumGetJsonbP(js);
3994 
3995  switch (pred->item_type)
3996  {
3997  case JS_TYPE_OBJECT:
3998  res = JB_ROOT_IS_OBJECT(jb);
3999  break;
4000  case JS_TYPE_ARRAY:
4001  res = JB_ROOT_IS_ARRAY(jb) && !JB_ROOT_IS_SCALAR(jb);
4002  break;
4003  case JS_TYPE_SCALAR:
4004  res = JB_ROOT_IS_ARRAY(jb) && JB_ROOT_IS_SCALAR(jb);
4005  break;
4006  default:
4007  res = false;
4008  break;
4009  }
4010  }
4011 
4012  /* Key uniqueness check is redundant for jsonb */
4013  }
4014  else
4015  res = false;
4016 
4017  *op->resvalue = BoolGetDatum(res);
4018 }
@ JSON_TOKEN_FALSE
Definition: jsonapi.h:31
@ JSON_TOKEN_TRUE
Definition: jsonapi.h:30
@ JSON_TOKEN_NULL
Definition: jsonapi.h:32
@ JSON_TOKEN_OBJECT_START
Definition: jsonapi.h:24
@ JSON_TOKEN_NUMBER
Definition: jsonapi.h:23
@ JSON_TOKEN_STRING
Definition: jsonapi.h:22
@ JSON_TOKEN_ARRAY_START
Definition: jsonapi.h:26
#define JB_ROOT_IS_OBJECT(jbp_)
Definition: jsonb.h:229
#define JB_ROOT_IS_ARRAY(jbp_)
Definition: jsonb.h:230
JsonTokenType json_get_first_token(text *json, bool throw_error)
Definition: jsonfuncs.c:5571
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
@ JS_TYPE_ANY
Definition: primnodes.h:1382
@ JS_TYPE_ARRAY
Definition: primnodes.h:1384
@ JS_TYPE_OBJECT
Definition: primnodes.h:1383
@ JS_TYPE_SCALAR
Definition: primnodes.h:1385
struct ExprEvalStep::@52::@95 is_json
JsonValueType item_type
Definition: primnodes.h:1397

References BoolGetDatum, DatumGetJsonbP, DatumGetTextP, JsonIsPredicate::expr, exprType(), JsonIsPredicate::item_type, JB_ROOT_IS_ARRAY, JB_ROOT_IS_OBJECT, JB_ROOT_IS_SCALAR, JS_TYPE_ANY, JS_TYPE_ARRAY, JS_TYPE_OBJECT, JS_TYPE_SCALAR, json_get_first_token(), JSON_TOKEN_ARRAY_START, JSON_TOKEN_FALSE, JSON_TOKEN_NULL, JSON_TOKEN_NUMBER, JSON_TOKEN_OBJECT_START, JSON_TOKEN_STRING, JSON_TOKEN_TRUE, json_validate(), ExprEvalStep::op, ExprEvalStep::pred, res, and JsonIsPredicate::unique_keys.

Referenced by ExecInterpExpr().

◆ ExecEvalJsonNeedsSubTransaction()

bool ExecEvalJsonNeedsSubTransaction ( JsonExpr jsexpr,
struct JsonCoercionsState *  coercions 
)

Definition at line 5046 of file execExprInterp.c.

5048 {
5049  if (jsexpr->on_error->btype == JSON_BEHAVIOR_ERROR)
5050  return false;
5051 
5052  if (jsexpr->op == JSON_EXISTS_OP && !jsexpr->result_coercion)
5053  return false;
5054 
5055  if (!coercions)
5056  return true;
5057 
5058  return false;
5059 }
JsonCoercion * result_coercion
Definition: primnodes.h:1455

References JsonBehavior::btype, JSON_BEHAVIOR_ERROR, JSON_EXISTS_OP, JsonExpr::on_error, JsonExpr::op, and JsonExpr::result_coercion.

Referenced by ExecEvalJson(), and max_parallel_hazard_walker().

◆ ExecEvalMinMax()

void ExecEvalMinMax ( ExprState state,
ExprEvalStep op 
)

Definition at line 2978 of file execExprInterp.c.

2979 {
2980  Datum *values = op->d.minmax.values;
2981  bool *nulls = op->d.minmax.nulls;
2982  FunctionCallInfo fcinfo = op->d.minmax.fcinfo_data;
2983  MinMaxOp operator = op->d.minmax.op;
2984 
2985  /* set at initialization */
2986  Assert(fcinfo->args[0].isnull == false);
2987  Assert(fcinfo->args[1].isnull == false);
2988 
2989  /* default to null result */
2990  *op->resnull = true;
2991 
2992  for (int off = 0; off < op->d.minmax.nelems; off++)
2993  {
2994  /* ignore NULL inputs */
2995  if (nulls[off])
2996  continue;
2997 
2998  if (*op->resnull)
2999  {
3000  /* first nonnull input, adopt value */
3001  *op->resvalue = values[off];
3002  *op->resnull = false;
3003  }
3004  else
3005  {
3006  int cmpresult;
3007 
3008  /* apply comparison function */
3009  fcinfo->args[0].value = *op->resvalue;
3010  fcinfo->args[1].value = values[off];
3011 
3012  fcinfo->isnull = false;
3013  cmpresult = DatumGetInt32(FunctionCallInvoke(fcinfo));
3014  if (fcinfo->isnull) /* probably should not happen */
3015  continue;
3016 
3017  if (cmpresult > 0 && operator == IS_LEAST)
3018  *op->resvalue = values[off];
3019  else if (cmpresult < 0 && operator == IS_GREATEST)
3020  *op->resvalue = values[off];
3021  }
3022  }
3023 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define DatumGetInt32(X)
Definition: postgres.h:516
MinMaxOp
Definition: primnodes.h:1147
@ IS_LEAST
Definition: primnodes.h:1149
@ IS_GREATEST
Definition: primnodes.h:1148
struct ExprEvalStep::@52::@76 minmax

References FunctionCallInfoBaseData::args, Assert(), DatumGetInt32, FunctionCallInvoke, IS_GREATEST, IS_LEAST, FunctionCallInfoBaseData::isnull, NullableDatum::isnull, ExprEvalStep::nulls, ExprEvalStep::op, NullableDatum::value, and values.

Referenced by ExecInterpExpr().

◆ ExecEvalNextValueExpr()

void ExecEvalNextValueExpr ( ExprState state,
ExprEvalStep op 
)

Definition at line 2580 of file execExprInterp.c.

2581 {
2582  int64 newval = nextval_internal(op->d.nextvalueexpr.seqid, false);
2583 
2584  switch (op->d.nextvalueexpr.seqtypid)
2585  {
2586  case INT2OID:
2587  *op->resvalue = Int16GetDatum((int16) newval);
2588  break;
2589  case INT4OID:
2590  *op->resvalue = Int32GetDatum((int32) newval);
2591  break;
2592  case INT8OID:
2593  *op->resvalue = Int64GetDatum((int64) newval);
2594  break;
2595  default:
2596  elog(ERROR, "unsupported sequence type %u",
2597  op->d.nextvalueexpr.seqtypid);
2598  }
2599  *op->resnull = false;
2600 }
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1683
#define newval
#define Int16GetDatum(X)
Definition: postgres.h:495
int64 nextval_internal(Oid relid, bool check_permissions)
Definition: sequence.c:621
struct ExprEvalStep::@52::@70 nextvalueexpr

References elog, ERROR, Int16GetDatum, Int32GetDatum, Int64GetDatum(), newval, nextval_internal(), and ExprEvalStep::op.

Referenced by ExecInterpExpr().

◆ ExecEvalParamExec()

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

Definition at line 2438 of file execExprInterp.c.

2439 {
2440  ParamExecData *prm;
2441 
2442  prm = &(econtext->ecxt_param_exec_vals[op->d.param.paramid]);
2443  if (unlikely(prm->execPlan != NULL))
2444  {
2445  /* Parameter not evaluated yet, so go do it */
2446  ExecSetParamPlan(prm->execPlan, econtext);
2447  /* ExecSetParamPlan should have processed this param... */
2448  Assert(prm->execPlan == NULL);
2449  }
2450  *op->resvalue = prm->value;
2451  *op->resnull = prm->isnull;
2452 }
void ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
Definition: nodeSubplan.c:1058
ParamExecData * ecxt_param_exec_vals
Definition: execnodes.h:243
struct ExprEvalStep::@52::@64 param
bool isnull
Definition: params.h:150
Datum value
Definition: params.h:149
void * execPlan
Definition: params.h:148

References Assert(), ExprContext::ecxt_param_exec_vals, ParamExecData::execPlan, ExecSetParamPlan(), ParamExecData::isnull, ExprEvalStep::op, unlikely, and ParamExecData::value.

Referenced by ExecInterpExpr().

◆ ExecEvalParamExtern()

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

Definition at line 2460 of file execExprInterp.c.

2461 {
2462  ParamListInfo paramInfo = econtext->ecxt_param_list_info;
2463  int paramId = op->d.param.paramid;
2464 
2465  if (likely(paramInfo &&
2466  paramId > 0 && paramId <= paramInfo->numParams))
2467  {
2468  ParamExternData *prm;
2469  ParamExternData prmdata;
2470 
2471  /* give hook a chance in case parameter is dynamic */
2472  if (paramInfo->paramFetch != NULL)
2473  prm = paramInfo->paramFetch(paramInfo, paramId, false, &prmdata);
2474  else
2475  prm = &paramInfo->params[paramId - 1];
2476 
2477  if (likely(OidIsValid(prm->ptype)))
2478  {
2479  /* safety check in case hook did something unexpected */
2480  if (unlikely(prm->ptype != op->d.param.paramtype))
2481  ereport(ERROR,
2482  (errcode(ERRCODE_DATATYPE_MISMATCH),
2483  errmsg("type of parameter %d (%s) does not match that when preparing the plan (%s)",
2484  paramId,
2485  format_type_be(prm->ptype),
2486  format_type_be(op->d.param.paramtype))));
2487  *op->resvalue = prm->value;
2488  *op->resnull = prm->isnull;
2489  return;
2490  }
2491  }
2492 
2493  ereport(ERROR,
2494  (errcode(ERRCODE_UNDEFINED_OBJECT),
2495  errmsg("no value found for parameter %d", paramId)));
2496 }
#define likely(x)
Definition: c.h:272
#define OidIsValid(objectId)
Definition: c.h:710
ParamListInfo ecxt_param_list_info
Definition: execnodes.h:244
bool isnull
Definition: params.h:93
Datum value
Definition: params.h:92
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:125
ParamFetchHook paramFetch
Definition: params.h:112

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

Referenced by ExecInterpExpr().

◆ ExecEvalRow()

void ExecEvalRow ( ExprState state,
ExprEvalStep op 
)

Definition at line 2958 of file execExprInterp.c.

2959 {
2960  HeapTuple tuple;
2961 
2962  /* build tuple from evaluated field values */
2963  tuple = heap_form_tuple(op->d.row.tupdesc,
2964  op->d.row.elemvalues,
2965  op->d.row.elemnulls);
2966 
2967  *op->resvalue = HeapTupleGetDatum(tuple);
2968  *op->resnull = false;
2969 }
struct ExprEvalStep::@52::@73 row

References heap_form_tuple(), HeapTupleGetDatum, and ExprEvalStep::op.

Referenced by ExecInterpExpr().

◆ ExecEvalRowNotNull()

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

Definition at line 2615 of file execExprInterp.c.

2616 {
2617  ExecEvalRowNullInt(state, op, econtext, false);
2618 }
static void ExecEvalRowNullInt(ExprState *state, ExprEvalStep *op, ExprContext *econtext, bool checkisnull)

References ExecEvalRowNullInt(), and ExprEvalStep::op.

Referenced by ExecInterpExpr().

◆ ExecEvalRowNull()

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

Definition at line 2606 of file execExprInterp.c.

2607 {
2608  ExecEvalRowNullInt(state, op, econtext, true);
2609 }

References ExecEvalRowNullInt(), and ExprEvalStep::op.

Referenced by ExecInterpExpr().

◆ ExecEvalRowNullInt()

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

Definition at line 2622 of file execExprInterp.c.

2624 {
2625  Datum value = *op->resvalue;
2626  bool isnull = *op->resnull;
2627  HeapTupleHeader tuple;
2628  Oid tupType;
2629  int32 tupTypmod;
2630  TupleDesc tupDesc;
2631  HeapTupleData tmptup;
2632 
2633  *op->resnull = false;
2634 
2635  /* NULL row variables are treated just as NULL scalar columns */
2636  if (isnull)
2637  {
2638  *op->resvalue = BoolGetDatum(checkisnull);
2639  return;
2640  }
2641 
2642  /*
2643  * The SQL standard defines IS [NOT] NULL for a non-null rowtype argument
2644  * as:
2645  *
2646  * "R IS NULL" is true if every field is the null value.
2647  *
2648  * "R IS NOT NULL" is true if no field is the null value.
2649  *
2650  * This definition is (apparently intentionally) not recursive; so our
2651  * tests on the fields are primitive attisnull tests, not recursive checks
2652  * to see if they are all-nulls or no-nulls rowtypes.
2653  *
2654  * The standard does not consider the possibility of zero-field rows, but
2655  * here we consider them to vacuously satisfy both predicates.
2656  */
2657 
2658  tuple = DatumGetHeapTupleHeader(value);
2659 
2660  tupType = HeapTupleHeaderGetTypeId(tuple);
2661  tupTypmod = HeapTupleHeaderGetTypMod(tuple);
2662 
2663  /* Lookup tupdesc if first time through or if type changes */
2664  tupDesc = get_cached_rowtype(tupType, tupTypmod,
2665  &op->d.nulltest_row.rowcache, NULL);
2666 
2667  /*
2668  * heap_attisnull needs a HeapTuple not a bare HeapTupleHeader.
2669  */
2670  tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
2671  tmptup.t_data = tuple;
2672 
2673  for (int att = 1; att <= tupDesc->natts; att++)
2674  {
2675  /* ignore dropped columns */
2676  if (TupleDescAttr(tupDesc, att - 1)->attisdropped)
2677  continue;
2678  if (heap_attisnull(&tmptup, att, tupDesc))
2679  {
2680  /* null field disproves IS NOT NULL */
2681  if (!checkisnull)
2682  {
2683  *op->resvalue = BoolGetDatum(false);
2684  return;
2685  }
2686  }
2687  else
2688  {
2689  /* non-null field disproves IS NULL */
2690  if (checkisnull)
2691  {
2692  *op->resvalue = BoolGetDatum(false);
2693  return;
2694  }
2695  }
2696  }
2697 
2698  *op->resvalue = BoolGetDatum(true);
2699 }
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
struct ExprEvalStep::@52::@63 nulltest_row

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

Referenced by ExecEvalRowNotNull(), and ExecEvalRowNull().

◆ ExecEvalScalarArrayOp()

void ExecEvalScalarArrayOp ( ExprState state,
ExprEvalStep op 
)

Definition at line 3321 of file execExprInterp.c.

3322 {
3323  FunctionCallInfo fcinfo = op->d.scalararrayop.fcinfo_data;
3324  bool useOr = op->d.scalararrayop.useOr;
3325  bool strictfunc = op->d.scalararrayop.finfo->fn_strict;
3326  ArrayType *arr;
3327  int nitems;
3328  Datum result;
3329  bool resultnull;
3330  int16 typlen;
3331  bool typbyval;
3332  char typalign;
3333  char *s;
3334  bits8 *bitmap;
3335  int bitmask;
3336 
3337  /*
3338  * If the array is NULL then we return NULL --- it's not very meaningful
3339  * to do anything else, even if the operator isn't strict.
3340  */
3341  if (*op->resnull)
3342  return;
3343 
3344  /* Else okay to fetch and detoast the array */
3345  arr = DatumGetArrayTypeP(*op->resvalue);
3346 
3347  /*
3348  * If the array is empty, we return either FALSE or TRUE per the useOr
3349  * flag. This is correct even if the scalar is NULL; since we would
3350  * evaluate the operator zero times, it matters not whether it would want
3351  * to return NULL.
3352  */
3353  nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
3354  if (nitems <= 0)
3355  {
3356  *op->resvalue = BoolGetDatum(!useOr);
3357  *op->resnull = false;
3358  return;
3359  }
3360 
3361  /*
3362  * If the scalar is NULL, and the function is strict, return NULL; no
3363  * point in iterating the loop.
3364  */
3365  if (fcinfo->args[0].isnull && strictfunc)
3366  {
3367  *op->resnull = true;
3368  return;
3369  }
3370 
3371  /*
3372  * We arrange to look up info about the element type only once per series
3373  * of calls, assuming the element type doesn't change underneath us.
3374  */
3375  if (op->d.scalararrayop.element_type != ARR_ELEMTYPE(arr))
3376  {
3378  &op->d.scalararrayop.typlen,
3379  &op->d.scalararrayop.typbyval,
3380  &op->d.scalararrayop.typalign);
3381  op->d.scalararrayop.element_type = ARR_ELEMTYPE(arr);
3382  }
3383 
3384  typlen = op->d.scalararrayop.typlen;
3385  typbyval = op->d.scalararrayop.typbyval;
3386  typalign = op->d.scalararrayop.typalign;
3387 
3388  /* Initialize result appropriately depending on useOr */
3389  result = BoolGetDatum(!useOr);
3390  resultnull = false;
3391 
3392  /* Loop over the array elements */
3393  s = (char *) ARR_DATA_PTR(arr);
3394  bitmap = ARR_NULLBITMAP(arr);
3395  bitmask = 1;
3396 
3397  for (int i = 0; i < nitems; i++)
3398  {
3399  Datum elt;
3400  Datum thisresult;
3401 
3402  /* Get array element, checking for NULL */
3403  if (bitmap && (*bitmap & bitmask) == 0)
3404  {
3405  fcinfo->args[1].value = (Datum) 0;
3406  fcinfo->args[1].isnull = true;
3407  }
3408  else
3409  {
3410  elt = fetch_att(s, typbyval, typlen);
3411  s = att_addlength_pointer(s, typlen, s);
3412  s = (char *) att_align_nominal(s, typalign);
3413  fcinfo->args[1].value = elt;
3414  fcinfo->args[1].isnull = false;
3415  }
3416 
3417  /* Call comparison function */
3418  if (fcinfo->args[1].isnull && strictfunc)
3419  {
3420  fcinfo->isnull = true;
3421  thisresult = (Datum) 0;
3422  }
3423  else
3424  {
3425  fcinfo->isnull = false;
3426  thisresult = op->d.scalararrayop.fn_addr(fcinfo);
3427  }
3428 
3429  /* Combine results per OR or AND semantics */
3430  if (fcinfo->isnull)
3431  resultnull = true;
3432  else if (useOr)
3433  {
3434  if (DatumGetBool(thisresult))
3435  {
3436  result = BoolGetDatum(true);
3437  resultnull = false;
3438  break; /* needn't look at any more elements */
3439  }
3440  }
3441  else
3442  {
3443  if (!DatumGetBool(thisresult))
3444  {
3445  result = BoolGetDatum(false);
3446  resultnull = false;
3447  break; /* needn't look at any more elements */
3448  }
3449  }
3450 
3451  /* advance bitmap pointer if any */
3452  if (bitmap)
3453  {
3454  bitmask <<= 1;
3455  if (bitmask == 0x100)
3456  {
3457  bitmap++;
3458  bitmask = 1;
3459  }
3460  }
3461  }
3462 
3463  *op->resvalue = result;
3464  *op->resnull = resultnull;
3465 }
struct ExprEvalStep::@52::@83 scalararrayop

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

Referenced by ExecInterpExpr().

◆ ExecEvalSQLValueFunction()

void ExecEvalSQLValueFunction ( ExprState state,
ExprEvalStep op 
)

Definition at line 2502 of file execExprInterp.c.

2503 {
2504  LOCAL_FCINFO(fcinfo, 0);
2505  SQLValueFunction *svf = op->d.sqlvaluefunction.svf;
2506 
2507  *op->resnull = false;
2508 
2509  /*
2510  * Note: current_schema() can return NULL. current_user() etc currently
2511  * cannot, but might as well code those cases the same way for safety.
2512  */
2513  switch (svf->op)
2514  {
2515  case SVFOP_CURRENT_DATE:
2517  break;
2518  case SVFOP_CURRENT_TIME:
2519  case SVFOP_CURRENT_TIME_N:
2521  break;
2525  break;
2526  case SVFOP_LOCALTIME:
2527  case SVFOP_LOCALTIME_N:
2529  break;
2530  case SVFOP_LOCALTIMESTAMP:
2533  break;
2534  case SVFOP_CURRENT_ROLE:
2535  case SVFOP_CURRENT_USER:
2536  case SVFOP_USER:
2537  InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2538  *op->resvalue = current_user(fcinfo);
2539  *op->resnull = fcinfo->isnull;
2540  break;
2541  case SVFOP_SESSION_USER:
2542  InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2543  *op->resvalue = session_user(fcinfo);
2544  *op->resnull = fcinfo->isnull;
2545  break;
2546  case SVFOP_CURRENT_CATALOG:
2547  InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2548  *op->resvalue = current_database(fcinfo);
2549  *op->resnull = fcinfo->isnull;
2550  break;
2551  case SVFOP_CURRENT_SCHEMA:
2552  InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2553  *op->resvalue = current_schema(fcinfo);
2554  *op->resnull = fcinfo->isnull;
2555  break;
2556  }
2557 }
Datum current_database(PG_FUNCTION_ARGS)
Definition: misc.c:174
Timestamp GetSQLLocalTimestamp(int32 typmod)
Definition: timestamp.c:1606
TimestampTz GetSQLCurrentTimestamp(int32 typmod)
Definition: timestamp.c:1592
TimeTzADT * GetSQLCurrentTime(int32 typmod)
Definition: date.c:335
TimeADT GetSQLLocalTime(int32 typmod)
Definition: date.c:355
DateADT GetSQLCurrentDate(void)
Definition: date.c:302
#define TimeTzADTPGetDatum(X)
Definition: date.h:59
#define DateADTGetDatum(X)
Definition: date.h:57
#define TimeADTGetDatum(X)
Definition: date.h:58
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
Datum current_user(PG_FUNCTION_ARGS)
Definition: name.c:263
Datum session_user(PG_FUNCTION_ARGS)
Definition: name.c:269
Datum current_schema(PG_FUNCTION_ARGS)
Definition: name.c:279
@ SVFOP_CURRENT_CATALOG
Definition: primnodes.h:1189
@ SVFOP_LOCALTIME_N
Definition: primnodes.h:1182
@ SVFOP_CURRENT_TIMESTAMP
Definition: primnodes.h:1179
@ SVFOP_LOCALTIME
Definition: primnodes.h:1181
@ SVFOP_CURRENT_TIMESTAMP_N
Definition: primnodes.h:1180
@ SVFOP_CURRENT_ROLE
Definition: primnodes.h:1185
@ SVFOP_USER
Definition: primnodes.h:1187
@ SVFOP_CURRENT_SCHEMA
Definition: primnodes.h:1190
@ SVFOP_LOCALTIMESTAMP_N
Definition: primnodes.h:1184
@ SVFOP_CURRENT_DATE
Definition: primnodes.h:1176
@ SVFOP_CURRENT_TIME_N
Definition: primnodes.h:1178
@ SVFOP_CURRENT_TIME
Definition: primnodes.h:1177
@ SVFOP_LOCALTIMESTAMP
Definition: primnodes.h:1183
@ SVFOP_CURRENT_USER
Definition: primnodes.h:1186
@ SVFOP_SESSION_USER
Definition: primnodes.h:1188
struct ExprEvalStep::@52::@69 sqlvaluefunction
SQLValueFunctionOp op
Definition: primnodes.h:1196
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
#define TimestampGetDatum(X)
Definition: timestamp.h:31

References current_database(), current_schema(), current_user(), DateADTGetDatum, GetSQLCurrentDate(), GetSQLCurrentTime(), GetSQLCurrentTimestamp(), GetSQLLocalTime(), GetSQLLocalTimestamp(), InitFunctionCallInfoData, InvalidOid, LOCAL_FCINFO, ExprEvalStep::op, SQLValueFunction::op, session_user(), 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().

◆ ExecEvalStepOp()

ExprEvalOp ExecEvalStepOp ( ExprState state,
ExprEvalStep op 
)

Definition at line 2350 of file execExprInterp.c.

2351 {
2352 #if defined(EEO_USE_COMPUTED_GOTO)
2353  if (state->flags & EEO_FLAG_DIRECT_THREADED)
2354  {
2355  ExprEvalOpLookup key;
2356  ExprEvalOpLookup *res;
2357 
2358  key.opcode = (void *) op->opcode;
2359  res = bsearch(&key,
2360  reverse_dispatch_table,
2361  EEOP_LAST /* nmembers */ ,
2362  sizeof(ExprEvalOpLookup),
2363  dispatch_compare_ptr);
2364  Assert(res); /* unknown ops shouldn't get looked up */
2365  return res->op;
2366  }
2367 #endif
2368  return (ExprEvalOp) op->opcode;
2369 }
#define EEO_FLAG_DIRECT_THREADED
Definition: execExpr.h:30
ExprEvalOp
Definition: execExpr.h:66
@ EEOP_LAST
Definition: execExpr.h:263
intptr_t opcode
Definition: execExpr.h:274

References Assert(), EEO_FLAG_DIRECT_THREADED, EEOP_LAST, sort-test::key, ExprEvalStep::op, and res.

Referenced by CheckExprStillValid().

◆ ExecEvalSubPlan()

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

Definition at line 4055 of file execExprInterp.c.

4056 {
4057  SubPlanState *sstate = op->d.subplan.sstate;
4058 
4059  /* could potentially be nested, so make sure there's enough stack */
4061 
4062  *op->resvalue = ExecSubPlan(sstate, econtext, op->resnull);
4063 }
Datum ExecSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:62
void check_stack_depth(void)
Definition: postgres.c:3500
struct ExprEvalStep::@52::@89 subplan

References check_stack_depth(), ExecSubPlan(), ExprEvalStep::op, and ExprEvalStep::sstate.

Referenced by ExecInterpExpr().

◆ ExecEvalSysVar()

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

Definition at line 4299 of file execExprInterp.c.

4301 {
4302  Datum d;
4303 
4304  /* slot_getsysattr has sufficient defenses against bad attnums */
4305  d = slot_getsysattr(slot,
4306  op->d.var.attnum,
4307  op->resnull);
4308  *op->resvalue = d;
4309  /* this ought to be unreachable, but it's cheap enough to check */
4310  if (unlikely(*op->resnull))
4311  elog(ERROR, "failed to fetch attribute from slot");
4312 }
static Datum slot_getsysattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:402

References ExprEvalStep::d, elog, ERROR, ExprEvalStep::op, slot_getsysattr(), and unlikely.

Referenced by ExecInterpExpr().

◆ ExecEvalWholeRowVar()

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

Definition at line 4072 of file execExprInterp.c.

4073 {
4074  Var *variable = op->d.wholerow.var;
4075  TupleTableSlot *slot;
4076  TupleDesc output_tupdesc;
4077  MemoryContext oldcontext;
4078  HeapTupleHeader dtuple;
4079  HeapTuple tuple;
4080 
4081  /* This was checked by ExecInitExpr */
4082  Assert(variable->varattno == InvalidAttrNumber);
4083 
4084  /* Get the input slot we want */
4085  switch (variable->varno)
4086  {
4087  case INNER_VAR:
4088  /* get the tuple from the inner node */
4089  slot = econtext->ecxt_innertuple;
4090  break;
4091 
4092  case OUTER_VAR:
4093  /* get the tuple from the outer node */
4094  slot = econtext->ecxt_outertuple;
4095  break;
4096 
4097  /* INDEX_VAR is handled by default case */
4098 
4099  default:
4100  /* get the tuple from the relation being scanned */
4101  slot = econtext->ecxt_scantuple;
4102  break;
4103  }
4104 
4105  /* Apply the junkfilter if any */
4106  if (op->d.wholerow.junkFilter != NULL)
4107  slot = ExecFilterJunk(op->d.wholerow.junkFilter, slot);
4108 
4109  /*
4110  * If first time through, obtain tuple descriptor and check compatibility.
4111  *
4112  * XXX: It'd be great if this could be moved to the expression
4113  * initialization phase, but due to using slots that's currently not
4114  * feasible.
4115  */
4116  if (op->d.wholerow.first)
4117  {
4118  /* optimistically assume we don't need slow path */
4119  op->d.wholerow.slow = false;
4120 
4121  /*
4122  * If the Var identifies a named composite type, we must check that
4123  * the actual tuple type is compatible with it.
4124  */
4125  if (variable->vartype != RECORDOID)
4126  {
4127  TupleDesc var_tupdesc;
4128  TupleDesc slot_tupdesc;
4129 
4130  /*
4131  * We really only care about numbers of attributes and data types.
4132  * Also, we can ignore type mismatch on columns that are dropped
4133  * in the destination type, so long as (1) the physical storage
4134  * matches or (2) the actual column value is NULL. Case (1) is
4135  * helpful in some cases involving out-of-date cached plans, while
4136  * case (2) is expected behavior in situations such as an INSERT
4137  * into a table with dropped columns (the planner typically
4138  * generates an INT4 NULL regardless of the dropped column type).
4139  * If we find a dropped column and cannot verify that case (1)
4140  * holds, we have to use the slow path to check (2) for each row.
4141  *
4142  * If vartype is a domain over composite, just look through that
4143  * to the base composite type.
4144  */
4145  var_tupdesc = lookup_rowtype_tupdesc_domain(variable->vartype,
4146  -1, false);
4147 
4148  slot_tupdesc = slot->tts_tupleDescriptor;
4149 
4150  if (var_tupdesc->natts != slot_tupdesc->natts)
4151  ereport(ERROR,
4152  (errcode(ERRCODE_DATATYPE_MISMATCH),
4153  errmsg("table row type and query-specified row type do not match"),
4154  errdetail_plural("Table row contains %d attribute, but query expects %d.",
4155  "Table row contains %d attributes, but query expects %d.",
4156  slot_tupdesc->natts,
4157  slot_tupdesc->natts,
4158  var_tupdesc->natts)));
4159 
4160  for (int i = 0; i < var_tupdesc->natts; i++)
4161  {
4162  Form_pg_attribute vattr = TupleDescAttr(var_tupdesc, i);
4163  Form_pg_attribute sattr = TupleDescAttr(slot_tupdesc, i);
4164 
4165  if (vattr->atttypid == sattr->atttypid)
4166  continue; /* no worries */
4167  if (!vattr->attisdropped)
4168  ereport(ERROR,
4169  (errcode(ERRCODE_DATATYPE_MISMATCH),
4170  errmsg("table row type and query-specified row type do not match"),
4171  errdetail("Table has type %s at ordinal position %d, but query expects %s.",
4172  format_type_be(sattr->atttypid),
4173  i + 1,
4174  format_type_be(vattr->atttypid))));
4175 
4176  if (vattr->attlen != sattr->attlen ||
4177  vattr->attalign != sattr->attalign)
4178  op->d.wholerow.slow = true; /* need to check for nulls */
4179  }
4180 
4181  /*
4182  * Use the variable's declared rowtype as the descriptor for the
4183  * output values. In particular, we *must* absorb any
4184  * attisdropped markings.
4185  */
4186  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
4187  output_tupdesc = CreateTupleDescCopy(var_tupdesc);
4188  MemoryContextSwitchTo(oldcontext);
4189 
4190  ReleaseTupleDesc(var_tupdesc);
4191  }
4192  else
4193  {
4194  /*
4195  * In the RECORD case, we use the input slot's rowtype as the
4196  * descriptor for the output values, modulo possibly assigning new
4197  * column names below.
4198  */
4199  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
4200  output_tupdesc = CreateTupleDescCopy(slot->tts_tupleDescriptor);
4201  MemoryContextSwitchTo(oldcontext);
4202 
4203  /*
4204  * It's possible that the input slot is a relation scan slot and
4205  * so is marked with that relation's rowtype. But we're supposed
4206  * to be returning RECORD, so reset to that.
4207  */
4208  output_tupdesc->tdtypeid = RECORDOID;
4209  output_tupdesc->tdtypmod = -1;
4210 
4211  /*
4212  * We already got the correct physical datatype info above, but
4213  * now we should try to find the source RTE and adopt its column
4214  * aliases, since it's unlikely that the input slot has the
4215  * desired names.
4216  *
4217  * If we can't locate the RTE, assume the column names we've got
4218  * are OK. (As of this writing, the only cases where we can't
4219  * locate the RTE are in execution of trigger WHEN clauses, and
4220  * then the Var will have the trigger's relation's rowtype, so its
4221  * names are fine.) Also, if the creator of the RTE didn't bother
4222  * to fill in an eref field, assume our column names are OK. (This
4223  * happens in COPY, and perhaps other places.)
4224  */
4225  if (econtext->ecxt_estate &&
4226  variable->varno <= econtext->ecxt_estate->es_range_table_size)
4227  {
4228  RangeTblEntry *rte = exec_rt_fetch(variable->varno,
4229  econtext->ecxt_estate);
4230 
4231  if (rte->eref)
4232  ExecTypeSetColNames(output_tupdesc, rte->eref->colnames);
4233  }
4234  }
4235 
4236  /* Bless the tupdesc if needed, and save it in the execution state */
4237  op->d.wholerow.tupdesc = BlessTupleDesc(output_tupdesc);
4238 
4239  op->d.wholerow.first = false;
4240  }
4241 
4242  /*
4243  * Make sure all columns of the slot are accessible in the slot's
4244  * Datum/isnull arrays.
4245  */
4246  slot_getallattrs(slot);
4247 
4248  if (op->d.wholerow.slow)
4249  {
4250  /* Check to see if any dropped attributes are non-null */
4251  TupleDesc tupleDesc = slot->tts_tupleDescriptor;
4252  TupleDesc var_tupdesc = op->d.wholerow.tupdesc;
4253 
4254  Assert(var_tupdesc->natts == tupleDesc->natts);
4255 
4256  for (int i = 0; i < var_tupdesc->natts; i++)
4257  {
4258  Form_pg_attribute vattr = TupleDescAttr(var_tupdesc, i);
4259  Form_pg_attribute sattr = TupleDescAttr(tupleDesc, i);
4260 
4261  if (!vattr->attisdropped)
4262  continue; /* already checked non-dropped cols */
4263  if (slot->tts_isnull[i])
4264  continue; /* null is always okay */
4265  if (vattr->attlen != sattr->attlen ||
4266  vattr->attalign != sattr->attalign)
4267  ereport(ERROR,
4268  (errcode(ERRCODE_DATATYPE_MISMATCH),
4269  errmsg("table row type and query-specified row type do not match"),
4270  errdetail("Physical storage mismatch on dropped attribute at ordinal position %d.",
4271  i + 1)));
4272  }
4273  }
4274 
4275  /*
4276  * Build a composite datum, making sure any toasted fields get detoasted.
4277  *
4278  * (Note: it is critical that we not change the slot's state here.)
4279  */
4281  slot->tts_values,
4282  slot->tts_isnull);
4283  dtuple = tuple->t_data;
4284 
4285  /*
4286  * Label the datum with the composite type info we identified before.
4287  *
4288  * (Note: we could skip doing this by passing op->d.wholerow.tupdesc to
4289  * the tuple build step; but that seems a tad risky so let's not.)
4290  */
4291  HeapTupleHeaderSetTypeId(dtuple, op->d.wholerow.tupdesc->tdtypeid);
4292  HeapTupleHeaderSetTypMod(dtuple, op->d.wholerow.tupdesc->tdtypmod);
4293 
4294  *op->resvalue = PointerGetDatum(dtuple);
4295  *op->resnull = false;
4296 }
#define InvalidAttrNumber
Definition: attnum.h:23
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1129
TupleTableSlot * ExecFilterJunk(JunkFilter *junkfilter, TupleTableSlot *slot)
Definition: execJunk.c:247
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2071
void ExecTypeSetColNames(TupleDesc typeInfo, List *namesList)
Definition: execTuples.c:2030
static RangeTblEntry * exec_rt_fetch(Index rti, EState *estate)
Definition: executor.h:575
HeapTuple toast_build_flattened_tuple(TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptoast.c:563
#define HeapTupleHeaderSetTypMod(tup, typmod)
Definition: htup_details.h:467
#define HeapTupleHeaderSetTypeId(tup, typeid)
Definition: htup_details.h:457
#define OUTER_VAR
Definition: primnodes.h:185
#define INNER_VAR
Definition: primnodes.h:184
List * colnames
Definition: primnodes.h:43
Index es_range_table_size
Definition: execnodes.h:593
struct EState * ecxt_estate
Definition: execnodes.h:268
struct ExprEvalStep::@52::@55 wholerow
Alias * eref
Definition: parsenodes.h:1161
int32 tdtypmod
Definition: tupdesc.h:83
bool * tts_isnull
Definition: tuptable.h:128
Datum * tts_values
Definition: tuptable.h:126
Definition: primnodes.h:196
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:354
TupleDesc lookup_rowtype_tupdesc_domain(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1886

References Assert(), BlessTupleDesc(), Alias::colnames, CreateTupleDescCopy(), 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, ExprEvalStep::op, OUTER_VAR, PointerGetDatum, ReleaseTupleDesc, slot_getallattrs(), HeapTupleData::t_data, TupleDescData::tdtypeid, TupleDescData::tdtypmod, toast_build_flattened_tuple(), TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, TupleTableSlot::tts_values, and TupleDescAttr.

Referenced by ExecInterpExpr().

◆ ExecEvalXmlExpr()

void ExecEvalXmlExpr ( ExprState state,
ExprEvalStep op 
)

Definition at line 3726 of file execExprInterp.c.

3727 {
3728  XmlExpr *xexpr = op->d.xmlexpr.xexpr;
3729  Datum value;
3730 
3731  *op->resnull = true; /* until we get a result */
3732  *op->resvalue = (Datum) 0;
3733 
3734  switch (xexpr->op)
3735  {
3736  case IS_XMLCONCAT:
3737  {
3738  Datum *argvalue = op->d.xmlexpr.argvalue;
3739  bool *argnull = op->d.xmlexpr.argnull;
3740  List *values = NIL;
3741 
3742  for (int i = 0; i < list_length(xexpr->args); i++)
3743  {
3744  if (!argnull[i])
3745  values = lappend(values, DatumGetPointer(argvalue[i]));
3746  }
3747 
3748  if (values != NIL)
3749  {
3751  *op->resnull = false;
3752  }
3753  }
3754  break;
3755 
3756  case IS_XMLFOREST:
3757  {
3758  Datum *argvalue = op->d.xmlexpr.named_argvalue;
3759  bool *argnull = op->d.xmlexpr.named_argnull;
3761  ListCell *lc;
3762  ListCell *lc2;
3763  int i;
3764 
3765  initStringInfo(&buf);
3766 
3767  i = 0;
3768  forboth(lc, xexpr->named_args, lc2, xexpr->arg_names)
3769  {
3770  Expr *e = (Expr *) lfirst(lc);
3771  char *argname = strVal(lfirst(lc2));
3772 
3773  if (!argnull[i])
3774  {
3775  value = argvalue[i];
3776  appendStringInfo(&buf, "<%s>%s</%s>",
3777  argname,
3779  exprType((Node *) e), true),
3780  argname);
3781  *op->resnull = false;
3782  }
3783  i++;
3784  }
3785 
3786  if (!*op->resnull)
3787  {
3788  text *result;
3789 
3790  result = cstring_to_text_with_len(buf.data, buf.len);
3791  *op->resvalue = PointerGetDatum(result);
3792  }
3793 
3794  pfree(buf.data);
3795  }
3796  break;
3797 
3798  case IS_XMLELEMENT:
3799  *op->resvalue = PointerGetDatum(xmlelement(xexpr,
3800  op->d.xmlexpr.named_argvalue,
3801  op->d.xmlexpr.named_argnull,
3802  op->d.xmlexpr.argvalue,
3803  op->d.xmlexpr.argnull));
3804  *op->resnull = false;
3805  break;
3806 
3807  case IS_XMLPARSE:
3808  {
3809  Datum *argvalue = op->d.xmlexpr.argvalue;
3810  bool *argnull = op->d.xmlexpr.argnull;
3811  text *data;
3812  bool preserve_whitespace;
3813 
3814  /* arguments are known to be text, bool */
3815  Assert(list_length(xexpr->args) == 2);
3816 
3817  if (argnull[0])
3818  return;
3819  value = argvalue[0];
3821 
3822  if (argnull[1]) /* probably can't happen */
3823  return;
3824  value = argvalue[1];
3825  preserve_whitespace = DatumGetBool(value);
3826 
3828  xexpr->xmloption,
3829  preserve_whitespace));
3830  *op->resnull = false;
3831  }
3832  break;
3833 
3834  case IS_XMLPI:
3835  {
3836  text *arg;
3837  bool isnull;
3838 
3839  /* optional argument is known to be text */
3840  Assert(list_length(xexpr->args) <= 1);
3841 
3842  if (xexpr->args)
3843  {
3844  isnull = op->d.xmlexpr.argnull[0];
3845  if (isnull)
3846  arg = NULL;
3847  else
3848  arg = DatumGetTextPP(op->d.xmlexpr.argvalue[0]);
3849  }
3850  else
3851  {
3852  arg = NULL;
3853  isnull = false;
3854  }
3855 
3856  *op->resvalue = PointerGetDatum(xmlpi(xexpr->name,
3857  arg,
3858  isnull,
3859  op->resnull));
3860  }
3861  break;
3862 
3863  case IS_XMLROOT:
3864  {
3865  Datum *argvalue = op->d.xmlexpr.argvalue;
3866  bool *argnull = op->d.xmlexpr.argnull;
3867  xmltype *data;
3868  text *version;
3869  int standalone;
3870 
3871  /* arguments are known to be xml, text, int */
3872  Assert(list_length(xexpr->args) == 3);
3873 
3874  if (argnull[0])
3875  return;
3876  data = DatumGetXmlP(argvalue[0]);
3877 
3878  if (argnull[1])
3879  version = NULL;
3880  else
3881  version = DatumGetTextPP(argvalue[1]);
3882 
3883  Assert(!argnull[2]); /* always present */
3884  standalone = DatumGetInt32(argvalue[2]);
3885 
3887  version,
3888  standalone));
3889  *op->resnull = false;
3890  }
3891  break;
3892 
3893  case IS_XMLSERIALIZE:
3894  {
3895  Datum *argvalue = op->d.xmlexpr.argvalue;
3896  bool *argnull = op->d.xmlexpr.argnull;
3897 
3898  /* argument type is known to be xml */
3899  Assert(list_length(xexpr->args) == 1);
3900 
3901  if (argnull[0])
3902  return;
3903  value = argvalue[0];
3904 
3906  xexpr->xmloption));
3907  *op->resnull = false;
3908  }
3909  break;
3910 
3911  case IS_DOCUMENT:
3912  {
3913  Datum *argvalue = op->d.xmlexpr.argvalue;
3914  bool *argnull = op->d.xmlexpr.argnull;
3915 
3916  /* optional argument is known to be xml */
3917  Assert(list_length(xexpr->args) == 1);
3918 
3919  if (argnull[0])
3920  return;
3921  value = argvalue[0];
3922 
3923  *op->resvalue =
3925  *op->resnull = false;
3926  }
3927  break;
3928 
3929  default:
3930  elog(ERROR, "unrecognized XML operation");
3931  break;
3932  }
3933 }
#define DatumGetTextPP(X)
Definition: fmgr.h:292
List * lappend(List *list, void *datum)
Definition: list.c:336
void * arg
const void * data
#define NIL
Definition: pg_list.h:65
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
static char * buf
Definition: pg_test_fsync.c:67
e
Definition: preproc-init.c:82
@ IS_DOCUMENT
Definition: primnodes.h:1222
@ IS_XMLFOREST
Definition: primnodes.h:1217
@ IS_XMLCONCAT
Definition: primnodes.h:1215
@ IS_XMLPI
Definition: primnodes.h:1219
@ IS_XMLPARSE
Definition: primnodes.h:1218
@ IS_XMLSERIALIZE
Definition: primnodes.h:1221
@ IS_XMLROOT
Definition: primnodes.h:1220
@ IS_XMLELEMENT
Definition: primnodes.h:1216
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
struct ExprEvalStep::@52::@85 xmlexpr
Definition: nodes.h:574
List * args
Definition: primnodes.h:1238
char * name
Definition: primnodes.h:1235
List * arg_names
Definition: primnodes.h:1237
List * named_args
Definition: primnodes.h:1236
XmlExprOp op
Definition: primnodes.h:1234
XmlOptionType xmloption
Definition: primnodes.h:1239
#define strVal(v)
Definition: value.h:72
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:200
xmltype * xmlconcat(List *args)
Definition: xml.c:511
xmltype * xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
Definition: xml.c:751
text * xmltotext_with_xmloption(xmltype *data, XmlOptionType xmloption_arg)
Definition: xml.c:614
bool xml_is_document(xmltype *arg)
Definition: xml.c:887
xmltype * xmlpi(const char *target, text *arg, bool arg_is_null, bool *result_is_null)
Definition: xml.c:769
char * map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Definition: xml.c:2133
xmltype * xmlelement(XmlExpr *xexpr, Datum *named_argvalue, bool *named_argnull, Datum *argvalue, bool *argnull)
Definition: xml.c:627
xmltype * xmlroot(xmltype *data, text *version, int standalone)
Definition: xml.c:821
#define DatumGetXmlP(X)
Definition: xml.h:50

References appendStringInfo(), arg, XmlExpr::arg_names, ExprEvalStep::argnull, XmlExpr::args, ExprEvalStep::argvalue, Assert(), BoolGetDatum, buf, cstring_to_text_with_len(), 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(), lfirst, list_length(), map_sql_value_to_xml_value(), XmlExpr::name, XmlExpr::named_args, NIL, ExprEvalStep::op, XmlExpr::op, pfree(), PointerGetDatum, strVal, value, values, ExprEvalStep::xexpr, xml_is_document(), xmlconcat(), xmlelement(), XmlExpr::xmloption, xmlparse(), xmlpi(), xmlroot(), and xmltotext_with_xmloption().

Referenced by ExecInterpExpr().

◆ ExecInitInterpreter()

static void ExecInitInterpreter ( void  )
static

Definition at line 2318 of file execExprInterp.c.

2319 {
2320 #if defined(EEO_USE_COMPUTED_GOTO)
2321  /* Set up externally-visible pointer to dispatch table */
2322  if (dispatch_table == NULL)
2323  {
2324  dispatch_table = (const void **)
2325  DatumGetPointer(ExecInterpExpr(NULL, NULL, NULL));
2326 
2327  /* build reverse lookup table */
2328  for (int i = 0; i < EEOP_LAST; i++)
2329  {
2330  reverse_dispatch_table[i].opcode = dispatch_table[i];
2331  reverse_dispatch_table[i].op = (ExprEvalOp) i;
2332  }
2333 
2334  /* make it bsearch()able */
2335  qsort(reverse_dispatch_table,
2336  EEOP_LAST /* nmembers */ ,
2337  sizeof(ExprEvalOpLookup),
2338  dispatch_compare_ptr);
2339  }
2340 #endif
2341 }
static Datum ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
#define qsort(a, b, c, d)
Definition: port.h:495

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

Referenced by ExecReadyInterpretedExpr().

◆ ExecInterpExpr()

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

Definition at line 399 of file execExprInterp.c.

400 {
401  ExprEvalStep *op;
402  TupleTableSlot *resultslot;
403  TupleTableSlot *innerslot;
404  TupleTableSlot *outerslot;
405  TupleTableSlot *scanslot;
406 
407  /*
408  * This array has to be in the same order as enum ExprEvalOp.
409  */
410 #if defined(EEO_USE_COMPUTED_GOTO)
411  static const void *const dispatch_table[] = {
412  &&CASE_EEOP_DONE,
413  &&CASE_EEOP_INNER_FETCHSOME,
414  &&CASE_EEOP_OUTER_FETCHSOME,
415  &&CASE_EEOP_SCAN_FETCHSOME,
416  &&CASE_EEOP_INNER_VAR,
417  &&CASE_EEOP_OUTER_VAR,
418  &&CASE_EEOP_SCAN_VAR,
419  &&CASE_EEOP_INNER_SYSVAR,
420  &&CASE_EEOP_OUTER_SYSVAR,
421  &&CASE_EEOP_SCAN_SYSVAR,
422  &&CASE_EEOP_WHOLEROW,
423  &&CASE_EEOP_ASSIGN_INNER_VAR,
424  &&CASE_EEOP_ASSIGN_OUTER_VAR,
425  &&CASE_EEOP_ASSIGN_SCAN_VAR,
426  &&CASE_EEOP_ASSIGN_TMP,
427  &&CASE_EEOP_ASSIGN_TMP_MAKE_RO,
428  &&CASE_EEOP_CONST,
429  &&CASE_EEOP_FUNCEXPR,
430  &&CASE_EEOP_FUNCEXPR_STRICT,
431  &&CASE_EEOP_FUNCEXPR_FUSAGE,
432  &&CASE_EEOP_FUNCEXPR_STRICT_FUSAGE,
433  &&CASE_EEOP_BOOL_AND_STEP_FIRST,
434  &&CASE_EEOP_BOOL_AND_STEP,
435  &&CASE_EEOP_BOOL_AND_STEP_LAST,
436  &&CASE_EEOP_BOOL_OR_STEP_FIRST,
437  &&CASE_EEOP_BOOL_OR_STEP,
438  &&CASE_EEOP_BOOL_OR_STEP_LAST,
439  &&CASE_EEOP_BOOL_NOT_STEP,
440  &&CASE_EEOP_QUAL,
441  &&CASE_EEOP_JUMP,
442  &&CASE_EEOP_JUMP_IF_NULL,
443  &&CASE_EEOP_JUMP_IF_NOT_NULL,
444  &&CASE_EEOP_JUMP_IF_NOT_TRUE,
445  &&CASE_EEOP_NULLTEST_ISNULL,
446  &&CASE_EEOP_NULLTEST_ISNOTNULL,
447  &&CASE_EEOP_NULLTEST_ROWISNULL,
448  &&CASE_EEOP_NULLTEST_ROWISNOTNULL,
449  &&CASE_EEOP_BOOLTEST_IS_TRUE,
450  &&CASE_EEOP_BOOLTEST_IS_NOT_TRUE,
451  &&CASE_EEOP_BOOLTEST_IS_FALSE,
452  &&CASE_EEOP_BOOLTEST_IS_NOT_FALSE,
453  &&CASE_EEOP_PARAM_EXEC,
454  &&CASE_EEOP_PARAM_EXTERN,
455  &&CASE_EEOP_PARAM_CALLBACK,
456  &&CASE_EEOP_CASE_TESTVAL,
457  &&CASE_EEOP_MAKE_READONLY,
458  &&CASE_EEOP_IOCOERCE,
459  &&CASE_EEOP_DISTINCT,
460  &&CASE_EEOP_NOT_DISTINCT,
461  &&CASE_EEOP_NULLIF,
462  &&CASE_EEOP_SQLVALUEFUNCTION,
463  &&CASE_EEOP_CURRENTOFEXPR,
464  &&CASE_EEOP_NEXTVALUEEXPR,
465  &&CASE_EEOP_ARRAYEXPR,
466  &&CASE_EEOP_ARRAYCOERCE,
467  &&CASE_EEOP_ROW,
468  &&CASE_EEOP_ROWCOMPARE_STEP,
469  &&CASE_EEOP_ROWCOMPARE_FINAL,
470  &&CASE_EEOP_MINMAX,
471  &&CASE_EEOP_FIELDSELECT,
472  &&CASE_EEOP_FIELDSTORE_DEFORM,
473  &&CASE_EEOP_FIELDSTORE_FORM,
474  &&CASE_EEOP_SBSREF_SUBSCRIPTS,
475  &&CASE_EEOP_SBSREF_OLD,
476  &&CASE_EEOP_SBSREF_ASSIGN,
477  &&CASE_EEOP_SBSREF_FETCH,
478  &&CASE_EEOP_DOMAIN_TESTVAL,
479  &&CASE_EEOP_DOMAIN_NOTNULL,
480  &&CASE_EEOP_DOMAIN_CHECK,
481  &&CASE_EEOP_CONVERT_ROWTYPE,
482  &&CASE_EEOP_SCALARARRAYOP,
483  &&CASE_EEOP_HASHED_SCALARARRAYOP,
484  &&CASE_EEOP_XMLEXPR,
485  &&CASE_EEOP_AGGREF,
486  &&CASE_EEOP_GROUPING_FUNC,
487  &&CASE_EEOP_WINDOW_FUNC,
488  &&CASE_EEOP_SUBPLAN,
489  &&CASE_EEOP_JSON_CONSTRUCTOR,
490  &&CASE_EEOP_IS_JSON,
491  &&CASE_EEOP_JSONEXPR,
492  &&CASE_EEOP_AGG_STRICT_DESERIALIZE,
493  &&CASE_EEOP_AGG_DESERIALIZE,
494  &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_ARGS,
495  &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_NULLS,
496  &&CASE_EEOP_AGG_PLAIN_PERGROUP_NULLCHECK,
497  &&CASE_EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL,
498  &&CASE_EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL,
499  &&CASE_EEOP_AGG_PLAIN_TRANS_BYVAL,
500  &&CASE_EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF,
501  &&CASE_EEOP_AGG_PLAIN_TRANS_STRICT_BYREF,
502  &&CASE_EEOP_AGG_PLAIN_TRANS_BYREF,
503  &&CASE_EEOP_AGG_ORDERED_TRANS_DATUM,
504  &&CASE_EEOP_AGG_ORDERED_TRANS_TUPLE,
505  &&CASE_EEOP_LAST
506  };
507 
508  StaticAssertStmt(EEOP_LAST + 1 == lengthof(dispatch_table),
509  "dispatch_table out of whack with ExprEvalOp");
510 
511  if (unlikely(state == NULL))
512  return PointerGetDatum(dispatch_table);
513 #else
514  Assert(state != NULL);
515 #endif /* EEO_USE_COMPUTED_GOTO */
516 
517  /* setup state */
518  op = state->steps;
519  resultslot = state->resultslot;
520  innerslot = econtext->ecxt_innertuple;
521  outerslot = econtext->ecxt_outertuple;
522  scanslot = econtext->ecxt_scantuple;
523 
524 #if defined(EEO_USE_COMPUTED_GOTO)
525  EEO_DISPATCH();
526 #endif
527 
528  EEO_SWITCH()
529  {
531  {
532  goto out;
533  }
534 
536  {
537  CheckOpSlotCompatibility(op, innerslot);
538 
539  slot_getsomeattrs(innerslot, op->d.fetch.last_var);
540 
541  EEO_NEXT();
542  }
543 
545  {
546  CheckOpSlotCompatibility(op, outerslot);
547 
548  slot_getsomeattrs(outerslot, op->d.fetch.last_var);
549 
550  EEO_NEXT();
551  }
552 
554  {
555  CheckOpSlotCompatibility(op, scanslot);
556 
557  slot_getsomeattrs(scanslot, op->d.fetch.last_var);
558 
559  EEO_NEXT();
560  }
561 
563  {
564  int attnum = op->d.var.attnum;
565 
566  /*
567  * Since we already extracted all referenced columns from the
568  * tuple with a FETCHSOME step, we can just grab the value
569  * directly out of the slot's decomposed-data arrays. But let's
570  * have an Assert to check that that did happen.
571  */
572  Assert(attnum >= 0 && attnum < innerslot->tts_nvalid);
573  *op->resvalue = innerslot->tts_values[attnum];
574  *op->resnull = innerslot->tts_isnull[attnum];
575 
576  EEO_NEXT();
577  }
578 
580  {
581  int attnum = op->d.var.attnum;
582 
583  /* See EEOP_INNER_VAR comments */
584 
585  Assert(attnum >= 0 && attnum < outerslot->tts_nvalid);
586  *op->resvalue = outerslot->tts_values[attnum];
587  *op->resnull = outerslot->tts_isnull[attnum];
588 
589  EEO_NEXT();
590  }
591 
593  {
594  int attnum = op->d.var.attnum;
595 
596  /* See EEOP_INNER_VAR comments */
597 
598  Assert(attnum >= 0 && attnum < scanslot->tts_nvalid);
599  *op->resvalue = scanslot->tts_values[attnum];
600  *op->resnull = scanslot->tts_isnull[attnum];
601 
602  EEO_NEXT();
603  }
604 
606  {
607  ExecEvalSysVar(state, op, econtext, innerslot);
608  EEO_NEXT();
609  }
610 
612  {
613  ExecEvalSysVar(state, op, econtext, outerslot);
614  EEO_NEXT();
615  }
616 
618  {
619  ExecEvalSysVar(state, op, econtext, scanslot);
620  EEO_NEXT();
621  }
622 
624  {
625  /* too complex for an inline implementation */
626  ExecEvalWholeRowVar(state, op, econtext);
627 
628  EEO_NEXT();
629  }
630 
632  {
633  int resultnum = op->d.assign_var.resultnum;
634  int attnum = op->d.assign_var.attnum;
635 
636  /*
637  * We do not need CheckVarSlotCompatibility here; that was taken
638  * care of at compilation time. But see EEOP_INNER_VAR comments.
639  */
640  Assert(attnum >= 0 && attnum < innerslot->tts_nvalid);
641  Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
642  resultslot->tts_values[resultnum] = innerslot->tts_values[attnum];
643  resultslot->tts_isnull[resultnum] = innerslot->tts_isnull[attnum];
644 
645  EEO_NEXT();
646  }
647 
649  {
650  int resultnum = op->d.assign_var.resultnum;
651  int attnum = op->d.assign_var.attnum;
652 
653  /*
654  * We do not need CheckVarSlotCompatibility here; that was taken
655  * care of at compilation time. But see EEOP_INNER_VAR comments.
656  */
657  Assert(attnum >= 0 && attnum < outerslot->tts_nvalid);
658  Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
659  resultslot->tts_values[resultnum] = outerslot->tts_values[attnum];
660  resultslot->tts_isnull[resultnum] = outerslot->tts_isnull[attnum];
661 
662  EEO_NEXT();
663  }
664 
666  {
667  int resultnum = op->d.assign_var.resultnum;
668  int attnum = op->d.assign_var.attnum;
669 
670  /*
671  * We do not need CheckVarSlotCompatibility here; that was taken
672  * care of at compilation time. But see EEOP_INNER_VAR comments.
673  */
674  Assert(attnum >= 0 && attnum < scanslot->tts_nvalid);
675  Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
676  resultslot->tts_values[resultnum] = scanslot->tts_values[attnum];
677  resultslot->tts_isnull[resultnum] = scanslot->tts_isnull[attnum];
678 
679  EEO_NEXT();
680  }
681 
683  {
684  int resultnum = op->d.assign_tmp.resultnum;
685 
686  Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
687  resultslot->tts_values[resultnum] = state->resvalue;
688  resultslot->tts_isnull[resultnum] = state->resnull;
689 
690  EEO_NEXT();
691  }
692 
694  {
695  int resultnum = op->d.assign_tmp.resultnum;
696 
697  Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
698  resultslot->tts_isnull[resultnum] = state->resnull;
699  if (!resultslot->tts_isnull[resultnum])
700  resultslot->tts_values[resultnum] =
702  else
703  resultslot->tts_values[resultnum] = state->resvalue;
704 
705  EEO_NEXT();
706  }
707 
709  {
710  *op->resnull = op->d.constval.isnull;
711  *op->resvalue = op->d.constval.value;
712 
713  EEO_NEXT();
714  }
715 
716  /*
717  * Function-call implementations. Arguments have previously been
718  * evaluated directly into fcinfo->args.
719  *
720  * As both STRICT checks and function-usage are noticeable performance
721  * wise, and function calls are a very hot-path (they also back
722  * operators!), it's worth having so many separate opcodes.
723  *
724  * Note: the reason for using a temporary variable "d", here and in
725  * other places, is that some compilers think "*op->resvalue = f();"
726  * requires them to evaluate op->resvalue into a register before
727  * calling f(), just in case f() is able to modify op->resvalue
728  * somehow. The extra line of code can save a useless register spill
729  * and reload across the function call.
730  */
732  {
733  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
734  Datum d;
735 
736  fcinfo->isnull = false;
737  d = op->d.func.fn_addr(fcinfo);
738  *op->resvalue = d;
739  *op->resnull = fcinfo->isnull;
740 
741  EEO_NEXT();
742  }
743 
745  {
746  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
747  NullableDatum *args = fcinfo->args;
748  int nargs = op->d.func.nargs;
749  Datum d;
750 
751  /* strict function, so check for NULL args */
752  for (int argno = 0; argno < nargs; argno++)
753  {
754  if (args[argno].isnull)
755  {
756  *op->resnull = true;
757  goto strictfail;
758  }
759  }
760  fcinfo->isnull = false;
761  d = op->d.func.fn_addr(fcinfo);
762  *op->resvalue = d;
763  *op->resnull = fcinfo->isnull;
764 
765  strictfail:
766  EEO_NEXT();
767  }
768 
770  {
771  /* not common enough to inline */
772  ExecEvalFuncExprFusage(state, op, econtext);
773 
774  EEO_NEXT();
775  }
776 
778  {
779  /* not common enough to inline */
780  ExecEvalFuncExprStrictFusage(state, op, econtext);
781 
782  EEO_NEXT();
783  }
784 
785  /*
786  * If any of its clauses is FALSE, an AND's result is FALSE regardless
787  * of the states of the rest of the clauses, so we can stop evaluating
788  * and return FALSE immediately. If none are FALSE and one or more is
789  * NULL, we return NULL; otherwise we return TRUE. This makes sense
790  * when you interpret NULL as "don't know": perhaps one of the "don't
791  * knows" would have been FALSE if we'd known its value. Only when
792  * all the inputs are known to be TRUE can we state confidently that
793  * the AND's result is TRUE.
794  */
796  {
797  *op->d.boolexpr.anynull = false;
798 
799  /*
800  * EEOP_BOOL_AND_STEP_FIRST resets anynull, otherwise it's the
801  * same as EEOP_BOOL_AND_STEP - so fall through to that.
802  */
803 
804  /* FALL THROUGH */
805  }
806 
808  {
809  if (*op->resnull)
810  {
811  *op->d.boolexpr.anynull = true;
812  }
813  else if (!DatumGetBool(*op->resvalue))
814  {
815  /* result is already set to FALSE, need not change it */
816  /* bail out early */
817  EEO_JUMP(op->d.boolexpr.jumpdone);
818  }
819 
820  EEO_NEXT();
821  }
822 
824  {
825  if (*op->resnull)
826  {
827  /* result is already set to NULL, need not change it */
828  }
829  else if (!DatumGetBool(*op->resvalue))
830  {
831  /* result is already set to FALSE, need not change it */
832 
833  /*
834  * No point jumping early to jumpdone - would be same target
835  * (as this is the last argument to the AND expression),
836  * except more expensive.
837  */
838  }
839  else if (*op->d.boolexpr.anynull)
840  {
841  *op->resvalue = (Datum) 0;
842  *op->resnull = true;
843  }
844  else
845  {
846  /* result is already set to TRUE, need not change it */
847  }
848 
849  EEO_NEXT();
850  }
851 
852  /*
853  * If any of its clauses is TRUE, an OR's result is TRUE regardless of
854  * the states of the rest of the clauses, so we can stop evaluating
855  * and return TRUE immediately. If none are TRUE and one or more is
856  * NULL, we return NULL; otherwise we return FALSE. This makes sense
857  * when you interpret NULL as "don't know": perhaps one of the "don't
858  * knows" would have been TRUE if we'd known its value. Only when all
859  * the inputs are known to be FALSE can we state confidently that the
860  * OR's result is FALSE.
861  */
863  {
864  *op->d.boolexpr.anynull = false;
865 
866  /*
867  * EEOP_BOOL_OR_STEP_FIRST resets anynull, otherwise it's the same
868  * as EEOP_BOOL_OR_STEP - so fall through to that.
869  */
870 
871  /* FALL THROUGH */
872  }
873 
875  {
876  if (*op->resnull)
877  {
878  *op->d.boolexpr.anynull = true;
879  }
880  else if (DatumGetBool(*op->resvalue))
881  {
882  /* result is already set to TRUE, need not change it */
883  /* bail out early */
884  EEO_JUMP(op->d.boolexpr.jumpdone);
885  }
886 
887  EEO_NEXT();
888  }
889 
891  {
892  if (*op->resnull)
893  {
894  /* result is already set to NULL, need not change it */
895  }
896  else if (DatumGetBool(*op->resvalue))
897  {
898  /* result is already set to TRUE, need not change it */
899 
900  /*
901  * No point jumping to jumpdone - would be same target (as
902  * this is the last argument to the AND expression), except
903  * more expensive.
904  */
905  }
906  else if (*op->d.boolexpr.anynull)
907  {
908  *op->resvalue = (Datum) 0;
909  *op->resnull = true;
910  }
911  else
912  {
913  /* result is already set to FALSE, need not change it */
914  }
915 
916  EEO_NEXT();
917  }
918 
920  {
921  /*
922  * Evaluation of 'not' is simple... if expr is false, then return
923  * 'true' and vice versa. It's safe to do this even on a
924  * nominally null value, so we ignore resnull; that means that
925  * NULL in produces NULL out, which is what we want.
926  */
928 
929  EEO_NEXT();
930  }
931 
933  {
934  /* simplified version of BOOL_AND_STEP for use by ExecQual() */
935 
936  /* If argument (also result) is false or null ... */
937  if (*op->resnull ||
938  !DatumGetBool(*op->resvalue))
939  {
940  /* ... bail out early, returning FALSE */
941  *op->resnull = false;
942  *op->resvalue = BoolGetDatum(false);
943  EEO_JUMP(op->d.qualexpr.jumpdone);
944  }
945 
946  /*
947  * Otherwise, leave the TRUE value in place, in case this is the
948  * last qual. Then, TRUE is the correct answer.
949  */
950 
951  EEO_NEXT();
952  }
953 
955  {
956  /* Unconditionally jump to target step */
957  EEO_JUMP(op->d.jump.jumpdone);
958  }
959 
961  {
962  /* Transfer control if current result is null */
963  if (*op->resnull)
964  EEO_JUMP(op->d.jump.jumpdone);
965 
966  EEO_NEXT();
967  }
968 
970  {
971  /* Transfer control if current result is non-null */
972  if (!*op->resnull)
973  EEO_JUMP(op->d.jump.jumpdone);
974 
975  EEO_NEXT();
976  }
977 
979  {
980  /* Transfer control if current result is null or false */
981  if (*op->resnull || !DatumGetBool(*op->resvalue))
982  EEO_JUMP(op->d.jump.jumpdone);
983 
984  EEO_NEXT();
985  }
986 
988  {
989  *op->resvalue = BoolGetDatum(*op->resnull);
990  *op->resnull = false;
991 
992  EEO_NEXT();
993  }
994 
996  {
997  *op->resvalue = BoolGetDatum(!*op->resnull);
998  *op->resnull = false;
999 
1000  EEO_NEXT();
1001  }
1002 
1004  {
1005  /* out of line implementation: too large */
1006  ExecEvalRowNull(state, op, econtext);
1007 
1008  EEO_NEXT();
1009  }
1010 
1012  {
1013  /* out of line implementation: too large */
1014  ExecEvalRowNotNull(state, op, econtext);
1015 
1016  EEO_NEXT();
1017  }
1018 
1019  /* BooleanTest implementations for all booltesttypes */
1020 
1022  {
1023  if (*op->resnull)
1024  {
1025  *op->resvalue = BoolGetDatum(false);
1026  *op->resnull = false;
1027  }
1028  /* else, input value is the correct output as well */
1029 
1030  EEO_NEXT();
1031  }
1032 
1034  {
1035  if (*op->resnull)
1036  {
1037  *op->resvalue = BoolGetDatum(true);
1038  *op->resnull = false;
1039  }
1040  else
1041  *op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue));
1042 
1043  EEO_NEXT();
1044  }
1045 
1047  {
1048  if (*op->resnull)
1049  {
1050  *op->resvalue = BoolGetDatum(false);
1051  *op->resnull = false;
1052  }
1053  else
1054  *op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue));
1055 
1056  EEO_NEXT();
1057  }
1058 
1060  {
1061  if (*op->resnull)
1062  {
1063  *op->resvalue = BoolGetDatum(true);
1064  *op->resnull = false;
1065  }
1066  /* else, input value is the correct output as well */
1067 
1068  EEO_NEXT();
1069  }
1070 
1072  {
1073  /* out of line implementation: too large */
1074  ExecEvalParamExec(state, op, econtext);
1075 
1076  EEO_NEXT();
1077  }
1078 
1080  {
1081  /* out of line implementation: too large */
1082  ExecEvalParamExtern(state, op, econtext);
1083  EEO_NEXT();
1084  }
1085 
1087  {
1088  /* allow an extension module to supply a PARAM_EXTERN value */
1089  op->d.cparam.paramfunc(state, op, econtext);
1090  EEO_NEXT();
1091  }
1092 
1094  {
1095  /*
1096  * Normally upper parts of the expression tree have setup the
1097  * values to be returned here, but some parts of the system
1098  * currently misuse {caseValue,domainValue}_{datum,isNull} to set
1099  * run-time data. So if no values have been set-up, use
1100  * ExprContext's. This isn't pretty, but also not *that* ugly,
1101  * and this is unlikely to be performance sensitive enough to
1102  * worry about an extra branch.
1103  */
1104  if (op->d.casetest.value)
1105  {
1106  *op->resvalue = *op->d.casetest.value;
1107  *op->resnull = *op->d.casetest.isnull;
1108  }
1109  else
1110  {
1111  *op->resvalue = econtext->caseValue_datum;
1112  *op->resnull = econtext->caseValue_isNull;
1113  }
1114 
1115  EEO_NEXT();
1116  }
1117 
1119  {
1120  /*
1121  * See EEOP_CASE_TESTVAL comment.
1122  */
1123  if (op->d.casetest.value)
1124  {
1125  *op->resvalue = *op->d.casetest.value;
1126  *op->resnull = *op->d.casetest.isnull;
1127  }
1128  else
1129  {
1130  *op->resvalue = econtext->domainValue_datum;
1131  *op->resnull = econtext->domainValue_isNull;
1132  }
1133 
1134  EEO_NEXT();
1135  }
1136 
1138  {
1139  /*
1140  * Force a varlena value that might be read multiple times to R/O
1141  */
1142  if (!*op->d.make_readonly.isnull)
1143  *op->resvalue =
1145  *op->resnull = *op->d.make_readonly.isnull;
1146 
1147  EEO_NEXT();
1148  }
1149 
1151  {
1152  /*
1153  * Evaluate a CoerceViaIO node. This can be quite a hot path, so
1154  * inline as much work as possible. The source value is in our
1155  * result variable.
1156  */
1157  char *str;
1158 
1159  /* call output function (similar to OutputFunctionCall) */
1160  if (*op->resnull)
1161  {
1162  /* output functions are not called on nulls */
1163  str = NULL;
1164  }
1165  else
1166  {
1167  FunctionCallInfo fcinfo_out;
1168 
1169  fcinfo_out = op->d.iocoerce.fcinfo_data_out;
1170  fcinfo_out->args[0].value = *op->resvalue;
1171  fcinfo_out->args[0].isnull = false;
1172 
1173  fcinfo_out->isnull = false;
1174  str = DatumGetCString(FunctionCallInvoke(fcinfo_out));
1175 
1176  /* OutputFunctionCall assumes result isn't null */
1177  Assert(!fcinfo_out->isnull);
1178  }
1179 
1180  /* call input function (similar to InputFunctionCall) */
1181  if (!op->d.iocoerce.finfo_in->fn_strict || str != NULL)
1182  {
1183  FunctionCallInfo fcinfo_in;
1184  Datum d;
1185 
1186  fcinfo_in = op->d.iocoerce.fcinfo_data_in;
1187  fcinfo_in->args[0].value = PointerGetDatum(str);
1188  fcinfo_in->args[0].isnull = *op->resnull;
1189  /* second and third arguments are already set up */
1190 
1191  fcinfo_in->isnull = false;
1192  d = FunctionCallInvoke(fcinfo_in);
1193  *op->resvalue = d;
1194 
1195  /* Should get null result if and only if str is NULL */
1196  if (str == NULL)
1197  {
1198  Assert(*op->resnull);
1199  Assert(fcinfo_in->isnull);
1200  }
1201  else
1202  {
1203  Assert(!*op->resnull);
1204  Assert(!fcinfo_in->isnull);
1205  }
1206  }
1207 
1208  EEO_NEXT();
1209  }
1210 
1212  {
1213  /*
1214  * IS DISTINCT FROM must evaluate arguments (already done into
1215  * fcinfo->args) to determine whether they are NULL; if either is
1216  * NULL then the result is determined. If neither is NULL, then
1217  * proceed to evaluate the comparison function, which is just the
1218  * type's standard equality operator. We need not care whether
1219  * that function is strict. Because the handling of nulls is
1220  * different, we can't just reuse EEOP_FUNCEXPR.
1221  */
1222  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
1223 
1224  /* check function arguments for NULLness */
1225  if (fcinfo->args[0].isnull && fcinfo->args[1].isnull)
1226  {
1227  /* Both NULL? Then is not distinct... */
1228  *op->resvalue = BoolGetDatum(false);
1229  *op->resnull = false;
1230  }
1231  else if (fcinfo->args[0].isnull || fcinfo->args[1].isnull)
1232  {
1233  /* Only one is NULL? Then is distinct... */
1234  *op->resvalue = BoolGetDatum(true);
1235  *op->resnull = false;
1236  }
1237  else
1238  {
1239  /* Neither null, so apply the equality function */
1240  Datum eqresult;
1241 
1242  fcinfo->isnull = false;
1243  eqresult = op->d.func.fn_addr(fcinfo);
1244  /* Must invert result of "="; safe to do even if null */
1245  *op->resvalue = BoolGetDatum(!DatumGetBool(eqresult));
1246  *op->resnull = fcinfo->isnull;
1247  }
1248 
1249  EEO_NEXT();
1250  }
1251 
1252  /* see EEOP_DISTINCT for comments, this is just inverted */
1254  {
1255  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
1256 
1257  if (fcinfo->args[0].isnull && fcinfo->args[1].isnull)
1258  {
1259  *op->resvalue = BoolGetDatum(true);
1260  *op->resnull = false;
1261  }
1262  else if (fcinfo->args[0].isnull || fcinfo->args[1].isnull)
1263  {
1264  *op->resvalue = BoolGetDatum(false);
1265  *op->resnull = false;
1266  }
1267  else
1268  {
1269  Datum eqresult;
1270 
1271  fcinfo->isnull = false;
1272  eqresult = op->d.func.fn_addr(fcinfo);
1273  *op->resvalue = eqresult;
1274  *op->resnull = fcinfo->isnull;
1275  }
1276 
1277  EEO_NEXT();
1278  }
1279 
1281  {
1282  /*
1283  * The arguments are already evaluated into fcinfo->args.
1284  */
1285  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
1286 
1287  /* if either argument is NULL they can't be equal */
1288  if (!fcinfo->args[0].isnull && !fcinfo->args[1].isnull)
1289  {
1290  Datum result;
1291 
1292  fcinfo->isnull = false;
1293  result = op->d.func.fn_addr(fcinfo);
1294 
1295  /* if the arguments are equal return null */
1296  if (!fcinfo->isnull && DatumGetBool(result))
1297  {
1298  *op->resvalue = (Datum) 0;
1299  *op->resnull = true;
1300 
1301  EEO_NEXT();
1302  }
1303  }
1304 
1305  /* Arguments aren't equal, so return the first one */
1306  *op->resvalue = fcinfo->args[0].value;
1307  *op->resnull = fcinfo->args[0].isnull;
1308 
1309  EEO_NEXT();
1310  }
1311 
1313  {
1314  /*
1315  * Doesn't seem worthwhile to have an inline implementation
1316  * efficiency-wise.
1317  */
1319 
1320  EEO_NEXT();
1321  }
1322 
1324  {
1325  /* error invocation uses space, and shouldn't ever occur */
1327 
1328  EEO_NEXT();
1329  }
1330 
1332  {
1333  /*
1334  * Doesn't seem worthwhile to have an inline implementation
1335  * efficiency-wise.
1336  */
1338 
1339  EEO_NEXT();
1340  }
1341 
1343  {
1344  /* too complex for an inline implementation */
1345  ExecEvalArrayExpr(state, op);
1346 
1347  EEO_NEXT();
1348  }
1349 
1351  {
1352  /* too complex for an inline implementation */
1353  ExecEvalArrayCoerce(state, op, econtext);
1354 
1355  EEO_NEXT();
1356  }
1357 
1359  {
1360  /* too complex for an inline implementation */
1361  ExecEvalRow(state, op);
1362 
1363  EEO_NEXT();
1364  }
1365 
1367  {
1368  FunctionCallInfo fcinfo = op->d.rowcompare_step.fcinfo_data;
1369  Datum d;
1370 
1371  /* force NULL result if strict fn and NULL input */
1372  if (op->d.rowcompare_step.finfo->fn_strict &&
1373  (fcinfo->args[0].isnull || fcinfo->args[1].isnull))
1374  {
1375  *op->resnull = true;
1376  EEO_JUMP(op->d.rowcompare_step.jumpnull);
1377  }
1378 
1379  /* Apply comparison function */
1380  fcinfo->isnull = false;
1381  d = op->d.rowcompare_step.fn_addr(fcinfo);
1382  *op->resvalue = d;
1383 
1384  /* force NULL result if NULL function result */
1385  if (fcinfo->isnull)
1386  {
1387  *op->resnull = true;
1388  EEO_JUMP(op->d.rowcompare_step.jumpnull);
1389  }
1390  *op->resnull = false;
1391 
1392  /* If unequal, no need to compare remaining columns */
1393  if (DatumGetInt32(*op->resvalue) != 0)
1394  {
1395  EEO_JUMP(op->d.rowcompare_step.jumpdone);
1396  }
1397 
1398  EEO_NEXT();
1399  }
1400 
1402  {
1403  int32 cmpresult = DatumGetInt32(*op->resvalue);
1404  RowCompareType rctype = op->d.rowcompare_final.rctype;
1405 
1406  *op->resnull = false;
1407  switch (rctype)
1408  {
1409  /* EQ and NE cases aren't allowed here */
1410  case ROWCOMPARE_LT:
1411  *op->resvalue = BoolGetDatum(cmpresult < 0);
1412  break;
1413  case ROWCOMPARE_LE:
1414  *op->resvalue = BoolGetDatum(cmpresult <= 0);
1415  break;
1416  case ROWCOMPARE_GE:
1417  *op->resvalue = BoolGetDatum(cmpresult >= 0);
1418  break;
1419  case ROWCOMPARE_GT:
1420  *op->resvalue = BoolGetDatum(cmpresult > 0);
1421  break;
1422  default:
1423  Assert(false);
1424  break;
1425  }
1426 
1427  EEO_NEXT();
1428  }
1429 
1431  {
1432  /* too complex for an inline implementation */
1433  ExecEvalMinMax(state, op);
1434 
1435