PostgreSQL Source Code git master
execExpr.c File Reference
#include "postgres.h"
#include "access/nbtree.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "executor/execExpr.h"
#include "executor/nodeSubplan.h"
#include "funcapi.h"
#include "jit/jit.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/subscripting.h"
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "utils/acl.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/jsonfuncs.h"
#include "utils/jsonpath.h"
#include "utils/lsyscache.h"
#include "utils/typcache.h"
Include dependency graph for execExpr.c:

Go to the source code of this file.

Data Structures

struct  ExprSetupInfo
 

Typedefs

typedef struct ExprSetupInfo ExprSetupInfo
 

Functions

static void ExecReadyExpr (ExprState *state)
 
static void ExecInitExprRec (Expr *node, ExprState *state, Datum *resv, bool *resnull)
 
static void ExecInitFunc (ExprEvalStep *scratch, Expr *node, List *args, Oid funcid, Oid inputcollid, ExprState *state)
 
static void ExecInitSubPlanExpr (SubPlan *subplan, ExprState *state, Datum *resv, bool *resnull)
 
static void ExecCreateExprSetupSteps (ExprState *state, Node *node)
 
static void ExecPushExprSetupSteps (ExprState *state, ExprSetupInfo *info)
 
static bool expr_setup_walker (Node *node, ExprSetupInfo *info)
 
static bool ExecComputeSlotInfo (ExprState *state, ExprEvalStep *op)
 
static void ExecInitWholeRowVar (ExprEvalStep *scratch, Var *variable, ExprState *state)
 
static void ExecInitSubscriptingRef (ExprEvalStep *scratch, SubscriptingRef *sbsref, ExprState *state, Datum *resv, bool *resnull)
 
static bool isAssignmentIndirectionExpr (Expr *expr)
 
static void ExecInitCoerceToDomain (ExprEvalStep *scratch, CoerceToDomain *ctest, ExprState *state, Datum *resv, bool *resnull)
 
static void ExecBuildAggTransCall (ExprState *state, AggState *aggstate, ExprEvalStep *scratch, FunctionCallInfo fcinfo, AggStatePerTrans pertrans, int transno, int setno, int setoff, bool ishash, bool nullcheck)
 
static void ExecInitJsonExpr (JsonExpr *jsexpr, ExprState *state, Datum *resv, bool *resnull, ExprEvalStep *scratch)
 
static void ExecInitJsonCoercion (ExprState *state, JsonReturning *returning, ErrorSaveContext *escontext, bool omit_quotes, bool exists_coerce, Datum *resv, bool *resnull)
 
ExprStateExecInitExpr (Expr *node, PlanState *parent)
 
ExprStateExecInitExprWithParams (Expr *node, ParamListInfo ext_params)
 
ExprStateExecInitQual (List *qual, PlanState *parent)
 
ExprStateExecInitCheck (List *qual, PlanState *parent)
 
ListExecInitExprList (List *nodes, PlanState *parent)
 
ProjectionInfoExecBuildProjectionInfo (List *targetList, ExprContext *econtext, TupleTableSlot *slot, PlanState *parent, TupleDesc inputDesc)
 
ProjectionInfoExecBuildUpdateProjection (List *targetList, bool evalTargetList, List *targetColnos, TupleDesc relDesc, ExprContext *econtext, TupleTableSlot *slot, PlanState *parent)
 
ExprStateExecPrepareExpr (Expr *node, EState *estate)
 
ExprStateExecPrepareQual (List *qual, EState *estate)
 
ExprStateExecPrepareCheck (List *qual, EState *estate)
 
ListExecPrepareExprList (List *nodes, EState *estate)
 
bool ExecCheck (ExprState *state, ExprContext *econtext)
 
void ExprEvalPushStep (ExprState *es, const ExprEvalStep *s)
 
ExprStateExecBuildAggTrans (AggState *aggstate, AggStatePerPhase phase, bool doSort, bool doHash, bool nullcheck)
 
ExprStateExecBuildHash32FromAttrs (TupleDesc desc, const TupleTableSlotOps *ops, FmgrInfo *hashfunctions, Oid *collations, int numCols, AttrNumber *keyColIdx, PlanState *parent, uint32 init_value)
 
ExprStateExecBuildHash32Expr (TupleDesc desc, const TupleTableSlotOps *ops, const Oid *hashfunc_oids, const List *collations, const List *hash_exprs, const bool *opstrict, PlanState *parent, uint32 init_value, bool keep_nulls)
 
ExprStateExecBuildGroupingEqual (TupleDesc ldesc, TupleDesc rdesc, const TupleTableSlotOps *lops, const TupleTableSlotOps *rops, int numCols, const AttrNumber *keyColIdx, const Oid *eqfunctions, const Oid *collations, PlanState *parent)
 
ExprStateExecBuildParamSetEqual (TupleDesc desc, const TupleTableSlotOps *lops, const TupleTableSlotOps *rops, const Oid *eqfunctions, const Oid *collations, const List *param_exprs, PlanState *parent)
 

Typedef Documentation

◆ ExprSetupInfo

typedef struct ExprSetupInfo ExprSetupInfo

Function Documentation

◆ ExecBuildAggTrans()

ExprState * ExecBuildAggTrans ( AggState aggstate,
AggStatePerPhase  phase,
bool  doSort,
bool  doHash,
bool  nullcheck 
)

Definition at line 3661 of file execExpr.c.

3663{
3665 PlanState *parent = &aggstate->ss.ps;
3666 ExprEvalStep scratch = {0};
3667 bool isCombine = DO_AGGSPLIT_COMBINE(aggstate->aggsplit);
3668 ExprSetupInfo deform = {0, 0, 0, 0, 0, NIL};
3669
3670 state->expr = (Expr *) aggstate;
3671 state->parent = parent;
3672
3673 scratch.resvalue = &state->resvalue;
3674 scratch.resnull = &state->resnull;
3675
3676 /*
3677 * First figure out which slots, and how many columns from each, we're
3678 * going to need.
3679 */
3680 for (int transno = 0; transno < aggstate->numtrans; transno++)
3681 {
3682 AggStatePerTrans pertrans = &aggstate->pertrans[transno];
3683
3685 &deform);
3686 expr_setup_walker((Node *) pertrans->aggref->args,
3687 &deform);
3688 expr_setup_walker((Node *) pertrans->aggref->aggorder,
3689 &deform);
3691 &deform);
3692 expr_setup_walker((Node *) pertrans->aggref->aggfilter,
3693 &deform);
3694 }
3695 ExecPushExprSetupSteps(state, &deform);
3696
3697 /*
3698 * Emit instructions for each transition value / grouping set combination.
3699 */
3700 for (int transno = 0; transno < aggstate->numtrans; transno++)
3701 {
3702 AggStatePerTrans pertrans = &aggstate->pertrans[transno];
3703 FunctionCallInfo trans_fcinfo = pertrans->transfn_fcinfo;
3704 List *adjust_bailout = NIL;
3705 NullableDatum *strictargs = NULL;
3706 bool *strictnulls = NULL;
3707 int argno;
3708 ListCell *bail;
3709
3710 /*
3711 * If filter present, emit. Do so before evaluating the input, to
3712 * avoid potentially unneeded computations, or even worse, unintended
3713 * side-effects. When combining, all the necessary filtering has
3714 * already been done.
3715 */
3716 if (pertrans->aggref->aggfilter && !isCombine)
3717 {
3718 /* evaluate filter expression */
3720 &state->resvalue, &state->resnull);
3721 /* and jump out if false */
3722 scratch.opcode = EEOP_JUMP_IF_NOT_TRUE;
3723 scratch.d.jump.jumpdone = -1; /* adjust later */
3724 ExprEvalPushStep(state, &scratch);
3725 adjust_bailout = lappend_int(adjust_bailout,
3726 state->steps_len - 1);
3727 }
3728
3729 /*
3730 * Evaluate arguments to aggregate/combine function.
3731 */
3732 argno = 0;
3733 if (isCombine)
3734 {
3735 /*
3736 * Combining two aggregate transition values. Instead of directly
3737 * coming from a tuple the input is a, potentially deserialized,
3738 * transition value.
3739 */
3740 TargetEntry *source_tle;
3741
3742 Assert(pertrans->numSortCols == 0);
3743 Assert(list_length(pertrans->aggref->args) == 1);
3744
3745 strictargs = trans_fcinfo->args + 1;
3746 source_tle = (TargetEntry *) linitial(pertrans->aggref->args);
3747
3748 /*
3749 * deserialfn_oid will be set if we must deserialize the input
3750 * state before calling the combine function.
3751 */
3752 if (!OidIsValid(pertrans->deserialfn_oid))
3753 {
3754 /*
3755 * Start from 1, since the 0th arg will be the transition
3756 * value
3757 */
3758 ExecInitExprRec(source_tle->expr, state,
3759 &trans_fcinfo->args[argno + 1].value,
3760 &trans_fcinfo->args[argno + 1].isnull);
3761 }
3762 else
3763 {
3764 FunctionCallInfo ds_fcinfo = pertrans->deserialfn_fcinfo;
3765
3766 /* evaluate argument */
3767 ExecInitExprRec(source_tle->expr, state,
3768 &ds_fcinfo->args[0].value,
3769 &ds_fcinfo->args[0].isnull);
3770
3771 /* Dummy second argument for type-safety reasons */
3772 ds_fcinfo->args[1].value = PointerGetDatum(NULL);
3773 ds_fcinfo->args[1].isnull = false;
3774
3775 /*
3776 * Don't call a strict deserialization function with NULL
3777 * input
3778 */
3779 if (pertrans->deserialfn.fn_strict)
3781 else
3782 scratch.opcode = EEOP_AGG_DESERIALIZE;
3783
3784 scratch.d.agg_deserialize.fcinfo_data = ds_fcinfo;
3785 scratch.d.agg_deserialize.jumpnull = -1; /* adjust later */
3786 scratch.resvalue = &trans_fcinfo->args[argno + 1].value;
3787 scratch.resnull = &trans_fcinfo->args[argno + 1].isnull;
3788
3789 ExprEvalPushStep(state, &scratch);
3790 /* don't add an adjustment unless the function is strict */
3791 if (pertrans->deserialfn.fn_strict)
3792 adjust_bailout = lappend_int(adjust_bailout,
3793 state->steps_len - 1);
3794
3795 /* restore normal settings of scratch fields */
3796 scratch.resvalue = &state->resvalue;
3797 scratch.resnull = &state->resnull;
3798 }
3799 argno++;
3800
3801 Assert(pertrans->numInputs == argno);
3802 }
3803 else if (!pertrans->aggsortrequired)
3804 {
3805 ListCell *arg;
3806
3807 /*
3808 * Normal transition function without ORDER BY / DISTINCT or with
3809 * ORDER BY / DISTINCT but the planner has given us pre-sorted
3810 * input.
3811 */
3812 strictargs = trans_fcinfo->args + 1;
3813
3814 foreach(arg, pertrans->aggref->args)
3815 {
3816 TargetEntry *source_tle = (TargetEntry *) lfirst(arg);
3817
3818 /*
3819 * Don't initialize args for any ORDER BY clause that might
3820 * exist in a presorted aggregate.
3821 */
3822 if (argno == pertrans->numTransInputs)
3823 break;
3824
3825 /*
3826 * Start from 1, since the 0th arg will be the transition
3827 * value
3828 */
3829 ExecInitExprRec(source_tle->expr, state,
3830 &trans_fcinfo->args[argno + 1].value,
3831 &trans_fcinfo->args[argno + 1].isnull);
3832 argno++;
3833 }
3834 Assert(pertrans->numTransInputs == argno);
3835 }
3836 else if (pertrans->numInputs == 1)
3837 {
3838 /*
3839 * Non-presorted DISTINCT and/or ORDER BY case, with a single
3840 * column sorted on.
3841 */
3842 TargetEntry *source_tle =
3843 (TargetEntry *) linitial(pertrans->aggref->args);
3844
3845 Assert(list_length(pertrans->aggref->args) == 1);
3846
3847 ExecInitExprRec(source_tle->expr, state,
3848 &state->resvalue,
3849 &state->resnull);
3850 strictnulls = &state->resnull;
3851 argno++;
3852
3853 Assert(pertrans->numInputs == argno);
3854 }
3855 else
3856 {
3857 /*
3858 * Non-presorted DISTINCT and/or ORDER BY case, with multiple
3859 * columns sorted on.
3860 */
3861 Datum *values = pertrans->sortslot->tts_values;
3862 bool *nulls = pertrans->sortslot->tts_isnull;
3863 ListCell *arg;
3864
3865 strictnulls = nulls;
3866
3867 foreach(arg, pertrans->aggref->args)
3868 {
3869 TargetEntry *source_tle = (TargetEntry *) lfirst(arg);
3870
3871 ExecInitExprRec(source_tle->expr, state,
3872 &values[argno], &nulls[argno]);
3873 argno++;
3874 }
3875 Assert(pertrans->numInputs == argno);
3876 }
3877
3878 /*
3879 * For a strict transfn, nothing happens when there's a NULL input; we
3880 * just keep the prior transValue. This is true for both plain and
3881 * sorted/distinct aggregates.
3882 */
3883 if (trans_fcinfo->flinfo->fn_strict && pertrans->numTransInputs > 0)
3884 {
3885 if (strictnulls)
3887 else
3889 scratch.d.agg_strict_input_check.nulls = strictnulls;
3890 scratch.d.agg_strict_input_check.args = strictargs;
3891 scratch.d.agg_strict_input_check.jumpnull = -1; /* adjust later */
3892 scratch.d.agg_strict_input_check.nargs = pertrans->numTransInputs;
3893 ExprEvalPushStep(state, &scratch);
3894 adjust_bailout = lappend_int(adjust_bailout,
3895 state->steps_len - 1);
3896 }
3897
3898 /* Handle DISTINCT aggregates which have pre-sorted input */
3899 if (pertrans->numDistinctCols > 0 && !pertrans->aggsortrequired)
3900 {
3901 if (pertrans->numDistinctCols > 1)
3903 else
3905
3906 scratch.d.agg_presorted_distinctcheck.pertrans = pertrans;
3907 scratch.d.agg_presorted_distinctcheck.jumpdistinct = -1; /* adjust later */
3908 ExprEvalPushStep(state, &scratch);
3909 adjust_bailout = lappend_int(adjust_bailout,
3910 state->steps_len - 1);
3911 }
3912
3913 /*
3914 * Call transition function (once for each concurrently evaluated
3915 * grouping set). Do so for both sort and hash based computations, as
3916 * applicable.
3917 */
3918 if (doSort)
3919 {
3920 int processGroupingSets = Max(phase->numsets, 1);
3921 int setoff = 0;
3922
3923 for (int setno = 0; setno < processGroupingSets; setno++)
3924 {
3925 ExecBuildAggTransCall(state, aggstate, &scratch, trans_fcinfo,
3926 pertrans, transno, setno, setoff, false,
3927 nullcheck);
3928 setoff++;
3929 }
3930 }
3931
3932 if (doHash)
3933 {
3934 int numHashes = aggstate->num_hashes;
3935 int setoff;
3936
3937 /* in MIXED mode, there'll be preceding transition values */
3938 if (aggstate->aggstrategy != AGG_HASHED)
3939 setoff = aggstate->maxsets;
3940 else
3941 setoff = 0;
3942
3943 for (int setno = 0; setno < numHashes; setno++)
3944 {
3945 ExecBuildAggTransCall(state, aggstate, &scratch, trans_fcinfo,
3946 pertrans, transno, setno, setoff, true,
3947 nullcheck);
3948 setoff++;
3949 }
3950 }
3951
3952 /* adjust early bail out jump target(s) */
3953 foreach(bail, adjust_bailout)
3954 {
3955 ExprEvalStep *as = &state->steps[lfirst_int(bail)];
3956
3957 if (as->opcode == EEOP_JUMP_IF_NOT_TRUE)
3958 {
3959 Assert(as->d.jump.jumpdone == -1);
3960 as->d.jump.jumpdone = state->steps_len;
3961 }
3962 else if (as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_ARGS ||
3964 {
3966 as->d.agg_strict_input_check.jumpnull = state->steps_len;
3967 }
3968 else if (as->opcode == EEOP_AGG_STRICT_DESERIALIZE)
3969 {
3970 Assert(as->d.agg_deserialize.jumpnull == -1);
3971 as->d.agg_deserialize.jumpnull = state->steps_len;
3972 }
3975 {
3978 }
3979 else
3980 Assert(false);
3981 }
3982 }
3983
3984 scratch.resvalue = NULL;
3985 scratch.resnull = NULL;
3986 scratch.opcode = EEOP_DONE;
3987 ExprEvalPushStep(state, &scratch);
3988
3990
3991 return state;
3992}
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define Max(x, y)
Definition: c.h:955
#define Assert(condition)
Definition: c.h:815
#define OidIsValid(objectId)
Definition: c.h:732
static void ExecPushExprSetupSteps(ExprState *state, ExprSetupInfo *info)
Definition: execExpr.c:2881
static void ExecInitExprRec(Expr *node, ExprState *state, Datum *resv, bool *resnull)
Definition: execExpr.c:919
static void ExecBuildAggTransCall(ExprState *state, AggState *aggstate, ExprEvalStep *scratch, FunctionCallInfo fcinfo, AggStatePerTrans pertrans, int transno, int setno, int setoff, bool ishash, bool nullcheck)
Definition: execExpr.c:4000
void ExprEvalPushStep(ExprState *es, const ExprEvalStep *s)
Definition: execExpr.c:2670
static bool expr_setup_walker(Node *node, ExprSetupInfo *info)
Definition: execExpr.c:2967
static void ExecReadyExpr(ExprState *state)
Definition: execExpr.c:902
@ EEOP_AGG_STRICT_DESERIALIZE
Definition: execExpr.h:271
@ EEOP_DONE
Definition: execExpr.h:69
@ EEOP_AGG_PRESORTED_DISTINCT_MULTI
Definition: execExpr.h:283
@ EEOP_AGG_STRICT_INPUT_CHECK_NULLS
Definition: execExpr.h:274
@ EEOP_AGG_STRICT_INPUT_CHECK_ARGS
Definition: execExpr.h:273
@ EEOP_AGG_DESERIALIZE
Definition: execExpr.h:272
@ EEOP_JUMP_IF_NOT_TRUE
Definition: execExpr.h:151
@ EEOP_AGG_PRESORTED_DISTINCT_SINGLE
Definition: execExpr.h:282
List * lappend_int(List *list, int datum)
Definition: list.c:357
#define DO_AGGSPLIT_COMBINE(as)
Definition: nodes.h:385
@ AGG_HASHED
Definition: nodes.h:356
#define makeNode(_type_)
Definition: nodes.h:155
void * arg
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define lfirst_int(lc)
Definition: pg_list.h:173
#define linitial(l)
Definition: pg_list.h:178
#define bail(...)
Definition: pg_regress.c:167
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
TupleTableSlot * sortslot
Definition: nodeAgg.h:141
Aggref * aggref
Definition: nodeAgg.h:44
FmgrInfo deserialfn
Definition: nodeAgg.h:92
FunctionCallInfo deserialfn_fcinfo
Definition: nodeAgg.h:175
FunctionCallInfo transfn_fcinfo
Definition: nodeAgg.h:170
ScanState ss
Definition: execnodes.h:2509
AggStatePerTrans pertrans
Definition: execnodes.h:2519
AggStrategy aggstrategy
Definition: execnodes.h:2513
int numtrans
Definition: execnodes.h:2512
AggSplit aggsplit
Definition: execnodes.h:2514
int num_hashes
Definition: execnodes.h:2550
int maxsets
Definition: execnodes.h:2539
List * aggdistinct
Definition: primnodes.h:490
List * aggdirectargs
Definition: primnodes.h:481
List * args
Definition: primnodes.h:484
Expr * aggfilter
Definition: primnodes.h:493
List * aggorder
Definition: primnodes.h:487
struct ExprEvalStep::@55::@66 jump
AggStatePerTrans pertrans
Definition: execExpr.h:722
intptr_t opcode
Definition: execExpr.h:299
NullableDatum * args
Definition: execExpr.h:706
bool * nulls
Definition: execExpr.h:522
Datum * resvalue
Definition: execExpr.h:302
int jumpnull
Definition: execExpr.h:506
FunctionCallInfo fcinfo_data
Definition: execExpr.h:381
int jumpdistinct
Definition: execExpr.h:724
struct ExprEvalStep::@55::@97 agg_deserialize
int jumpdone
Definition: execExpr.h:366
union ExprEvalStep::@55 d
struct ExprEvalStep::@55::@98 agg_strict_input_check
struct ExprEvalStep::@55::@100 agg_presorted_distinctcheck
bool * resnull
Definition: execExpr.h:303
bool fn_strict
Definition: fmgr.h:61
FmgrInfo * flinfo
Definition: fmgr.h:87
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
Definition: pg_list.h:54
Definition: nodes.h:129
Datum value
Definition: postgres.h:80
bool isnull
Definition: postgres.h:82
PlanState ps
Definition: execnodes.h:1588
Expr * expr
Definition: primnodes.h:2245
bool * tts_isnull
Definition: tuptable.h:127
Datum * tts_values
Definition: tuptable.h:125
Definition: regguts.h:323

References ExprEvalStep::agg_deserialize, AGG_HASHED, ExprEvalStep::agg_presorted_distinctcheck, ExprEvalStep::agg_strict_input_check, Aggref::aggdirectargs, Aggref::aggdistinct, Aggref::aggfilter, Aggref::aggorder, AggStatePerTransData::aggref, AggStatePerTransData::aggsortrequired, AggState::aggsplit, AggState::aggstrategy, arg, ExprEvalStep::args, FunctionCallInfoBaseData::args, Aggref::args, Assert, bail, ExprEvalStep::d, AggStatePerTransData::deserialfn, AggStatePerTransData::deserialfn_fcinfo, AggStatePerTransData::deserialfn_oid, DO_AGGSPLIT_COMBINE, EEOP_AGG_DESERIALIZE, EEOP_AGG_PRESORTED_DISTINCT_MULTI, EEOP_AGG_PRESORTED_DISTINCT_SINGLE, EEOP_AGG_STRICT_DESERIALIZE, EEOP_AGG_STRICT_INPUT_CHECK_ARGS, EEOP_AGG_STRICT_INPUT_CHECK_NULLS, EEOP_DONE, EEOP_JUMP_IF_NOT_TRUE, ExecBuildAggTransCall(), ExecInitExprRec(), ExecPushExprSetupSteps(), ExecReadyExpr(), TargetEntry::expr, expr_setup_walker(), ExprEvalPushStep(), ExprEvalStep::fcinfo_data, FunctionCallInfoBaseData::flinfo, FmgrInfo::fn_strict, NullableDatum::isnull, ExprEvalStep::jump, ExprEvalStep::jumpdistinct, ExprEvalStep::jumpdone, ExprEvalStep::jumpnull, lappend_int(), lfirst, lfirst_int, linitial, list_length(), makeNode, Max, AggState::maxsets, ExprEvalStep::nargs, NIL, ExprEvalStep::nulls, AggState::num_hashes, AggStatePerTransData::numDistinctCols, AggStatePerTransData::numInputs, AggStatePerPhaseData::numsets, AggStatePerTransData::numSortCols, AggState::numtrans, AggStatePerTransData::numTransInputs, OidIsValid, ExprEvalStep::opcode, ExprEvalStep::pertrans, AggState::pertrans, PointerGetDatum(), ScanState::ps, ExprEvalStep::resnull, ExprEvalStep::resvalue, AggStatePerTransData::sortslot, AggState::ss, AggStatePerTransData::transfn_fcinfo, TupleTableSlot::tts_isnull, TupleTableSlot::tts_values, NullableDatum::value, and values.

Referenced by ExecInitAgg(), and hashagg_recompile_expressions().

◆ ExecBuildAggTransCall()

static void ExecBuildAggTransCall ( ExprState state,
AggState aggstate,
ExprEvalStep scratch,
FunctionCallInfo  fcinfo,
AggStatePerTrans  pertrans,
int  transno,
int  setno,
int  setoff,
bool  ishash,
bool  nullcheck 
)
static

Definition at line 4000 of file execExpr.c.

4005{
4006 ExprContext *aggcontext;
4007 int adjust_jumpnull = -1;
4008
4009 if (ishash)
4010 aggcontext = aggstate->hashcontext;
4011 else
4012 aggcontext = aggstate->aggcontexts[setno];
4013
4014 /* add check for NULL pointer? */
4015 if (nullcheck)
4016 {
4018 scratch->d.agg_plain_pergroup_nullcheck.setoff = setoff;
4019 /* adjust later */
4021 ExprEvalPushStep(state, scratch);
4022 adjust_jumpnull = state->steps_len - 1;
4023 }
4024
4025 /*
4026 * Determine appropriate transition implementation.
4027 *
4028 * For non-ordered aggregates and ORDER BY / DISTINCT aggregates with
4029 * presorted input:
4030 *
4031 * If the initial value for the transition state doesn't exist in the
4032 * pg_aggregate table then we will let the first non-NULL value returned
4033 * from the outer procNode become the initial value. (This is useful for
4034 * aggregates like max() and min().) The noTransValue flag signals that we
4035 * need to do so. If true, generate a
4036 * EEOP_AGG_INIT_STRICT_PLAIN_TRANS{,_BYVAL} step. This step also needs to
4037 * do the work described next:
4038 *
4039 * If the function is strict, but does have an initial value, choose
4040 * EEOP_AGG_STRICT_PLAIN_TRANS{,_BYVAL}, which skips the transition
4041 * function if the transition value has become NULL (because a previous
4042 * transition function returned NULL). This step also needs to do the work
4043 * described next:
4044 *
4045 * Otherwise we call EEOP_AGG_PLAIN_TRANS{,_BYVAL}, which does not have to
4046 * perform either of the above checks.
4047 *
4048 * Having steps with overlapping responsibilities is not nice, but
4049 * aggregations are very performance sensitive, making this worthwhile.
4050 *
4051 * For ordered aggregates:
4052 *
4053 * Only need to choose between the faster path for a single ordered
4054 * column, and the one between multiple columns. Checking strictness etc
4055 * is done when finalizing the aggregate. See
4056 * process_ordered_aggregate_{single, multi} and
4057 * advance_transition_function.
4058 */
4059 if (!pertrans->aggsortrequired)
4060 {
4061 if (pertrans->transtypeByVal)
4062 {
4063 if (fcinfo->flinfo->fn_strict &&
4064 pertrans->initValueIsNull)
4066 else if (fcinfo->flinfo->fn_strict)
4068 else
4070 }
4071 else
4072 {
4073 if (fcinfo->flinfo->fn_strict &&
4074 pertrans->initValueIsNull)
4076 else if (fcinfo->flinfo->fn_strict)
4078 else
4080 }
4081 }
4082 else if (pertrans->numInputs == 1)
4084 else
4086
4087 scratch->d.agg_trans.pertrans = pertrans;
4088 scratch->d.agg_trans.setno = setno;
4089 scratch->d.agg_trans.setoff = setoff;
4090 scratch->d.agg_trans.transno = transno;
4091 scratch->d.agg_trans.aggcontext = aggcontext;
4092 ExprEvalPushStep(state, scratch);
4093
4094 /* fix up jumpnull */
4095 if (adjust_jumpnull != -1)
4096 {
4097 ExprEvalStep *as = &state->steps[adjust_jumpnull];
4098
4102 }
4103}
@ EEOP_AGG_PLAIN_PERGROUP_NULLCHECK
Definition: execExpr.h:275
@ EEOP_AGG_PLAIN_TRANS_BYREF
Definition: execExpr.h:281
@ EEOP_AGG_PLAIN_TRANS_BYVAL
Definition: execExpr.h:278
@ EEOP_AGG_ORDERED_TRANS_DATUM
Definition: execExpr.h:284
@ EEOP_AGG_PLAIN_TRANS_STRICT_BYREF
Definition: execExpr.h:280
@ EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL
Definition: execExpr.h:276
@ EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL
Definition: execExpr.h:277
@ EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF
Definition: execExpr.h:279
@ EEOP_AGG_ORDERED_TRANS_TUPLE
Definition: execExpr.h:285
ExprContext * hashcontext
Definition: execnodes.h:2520
ExprContext ** aggcontexts
Definition: execnodes.h:2521
struct ExprEvalStep::@55::@99 agg_plain_pergroup_nullcheck
struct ExprEvalStep::@55::@101 agg_trans
ExprContext * aggcontext
Definition: execExpr.h:723

References ExprEvalStep::agg_plain_pergroup_nullcheck, ExprEvalStep::agg_trans, ExprEvalStep::aggcontext, AggState::aggcontexts, AggStatePerTransData::aggsortrequired, Assert, ExprEvalStep::d, EEOP_AGG_ORDERED_TRANS_DATUM, EEOP_AGG_ORDERED_TRANS_TUPLE, EEOP_AGG_PLAIN_PERGROUP_NULLCHECK, EEOP_AGG_PLAIN_TRANS_BYREF, EEOP_AGG_PLAIN_TRANS_BYVAL, EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF, EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL, EEOP_AGG_PLAIN_TRANS_STRICT_BYREF, EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL, ExprEvalPushStep(), FunctionCallInfoBaseData::flinfo, FmgrInfo::fn_strict, AggState::hashcontext, AggStatePerTransData::initValueIsNull, ExprEvalStep::jumpnull, AggStatePerTransData::numInputs, ExprEvalStep::opcode, ExprEvalStep::pertrans, ExprEvalStep::setno, ExprEvalStep::setoff, ExprEvalStep::transno, and AggStatePerTransData::transtypeByVal.

Referenced by ExecBuildAggTrans().

◆ ExecBuildGroupingEqual()

ExprState * ExecBuildGroupingEqual ( TupleDesc  ldesc,
TupleDesc  rdesc,
const TupleTableSlotOps lops,
const TupleTableSlotOps rops,
int  numCols,
const AttrNumber keyColIdx,
const Oid eqfunctions,
const Oid collations,
PlanState parent 
)

Definition at line 4446 of file execExpr.c.

4453{
4455 ExprEvalStep scratch = {0};
4456 int maxatt = -1;
4457 List *adjust_jumps = NIL;
4458 ListCell *lc;
4459
4460 /*
4461 * When no columns are actually compared, the result's always true. See
4462 * special case in ExecQual().
4463 */
4464 if (numCols == 0)
4465 return NULL;
4466
4467 state->expr = NULL;
4468 state->flags = EEO_FLAG_IS_QUAL;
4469 state->parent = parent;
4470
4471 scratch.resvalue = &state->resvalue;
4472 scratch.resnull = &state->resnull;
4473
4474 /* compute max needed attribute */
4475 for (int natt = 0; natt < numCols; natt++)
4476 {
4477 int attno = keyColIdx[natt];
4478
4479 if (attno > maxatt)
4480 maxatt = attno;
4481 }
4482 Assert(maxatt >= 0);
4483
4484 /* push deform steps */
4485 scratch.opcode = EEOP_INNER_FETCHSOME;
4486 scratch.d.fetch.last_var = maxatt;
4487 scratch.d.fetch.fixed = false;
4488 scratch.d.fetch.known_desc = ldesc;
4489 scratch.d.fetch.kind = lops;
4490 if (ExecComputeSlotInfo(state, &scratch))
4491 ExprEvalPushStep(state, &scratch);
4492
4493 scratch.opcode = EEOP_OUTER_FETCHSOME;
4494 scratch.d.fetch.last_var = maxatt;
4495 scratch.d.fetch.fixed = false;
4496 scratch.d.fetch.known_desc = rdesc;
4497 scratch.d.fetch.kind = rops;
4498 if (ExecComputeSlotInfo(state, &scratch))
4499 ExprEvalPushStep(state, &scratch);
4500
4501 /*
4502 * Start comparing at the last field (least significant sort key). That's
4503 * the most likely to be different if we are dealing with sorted input.
4504 */
4505 for (int natt = numCols; --natt >= 0;)
4506 {
4507 int attno = keyColIdx[natt];
4508 Form_pg_attribute latt = TupleDescAttr(ldesc, attno - 1);
4509 Form_pg_attribute ratt = TupleDescAttr(rdesc, attno - 1);
4510 Oid foid = eqfunctions[natt];
4511 Oid collid = collations[natt];
4512 FmgrInfo *finfo;
4513 FunctionCallInfo fcinfo;
4514 AclResult aclresult;
4515
4516 /* Check permission to call function */
4517 aclresult = object_aclcheck(ProcedureRelationId, foid, GetUserId(), ACL_EXECUTE);
4518 if (aclresult != ACLCHECK_OK)
4519 aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(foid));
4520
4522
4523 /* Set up the primary fmgr lookup information */
4524 finfo = palloc0(sizeof(FmgrInfo));
4525 fcinfo = palloc0(SizeForFunctionCallInfo(2));
4526 fmgr_info(foid, finfo);
4527 fmgr_info_set_expr(NULL, finfo);
4528 InitFunctionCallInfoData(*fcinfo, finfo, 2,
4529 collid, NULL, NULL);
4530
4531 /* left arg */
4532 scratch.opcode = EEOP_INNER_VAR;
4533 scratch.d.var.attnum = attno - 1;
4534 scratch.d.var.vartype = latt->atttypid;
4536 scratch.resvalue = &fcinfo->args[0].value;
4537 scratch.resnull = &fcinfo->args[0].isnull;
4538 ExprEvalPushStep(state, &scratch);
4539
4540 /* right arg */
4541 scratch.opcode = EEOP_OUTER_VAR;
4542 scratch.d.var.attnum = attno - 1;
4543 scratch.d.var.vartype = ratt->atttypid;
4545 scratch.resvalue = &fcinfo->args[1].value;
4546 scratch.resnull = &fcinfo->args[1].isnull;
4547 ExprEvalPushStep(state, &scratch);
4548
4549 /* evaluate distinctness */
4550 scratch.opcode = EEOP_NOT_DISTINCT;
4551 scratch.d.func.finfo = finfo;
4552 scratch.d.func.fcinfo_data = fcinfo;
4553 scratch.d.func.fn_addr = finfo->fn_addr;
4554 scratch.d.func.nargs = 2;
4555 scratch.resvalue = &state->resvalue;
4556 scratch.resnull = &state->resnull;
4557 ExprEvalPushStep(state, &scratch);
4558
4559 /* then emit EEOP_QUAL to detect if result is false (or null) */
4560 scratch.opcode = EEOP_QUAL;
4561 scratch.d.qualexpr.jumpdone = -1;
4562 scratch.resvalue = &state->resvalue;
4563 scratch.resnull = &state->resnull;
4564 ExprEvalPushStep(state, &scratch);
4565 adjust_jumps = lappend_int(adjust_jumps,
4566 state->steps_len - 1);
4567 }
4568
4569 /* adjust jump targets */
4570 foreach(lc, adjust_jumps)
4571 {
4572 ExprEvalStep *as = &state->steps[lfirst_int(lc)];
4573
4574 Assert(as->opcode == EEOP_QUAL);
4575 Assert(as->d.qualexpr.jumpdone == -1);
4576 as->d.qualexpr.jumpdone = state->steps_len;
4577 }
4578
4579 scratch.resvalue = NULL;
4580 scratch.resnull = NULL;
4581 scratch.opcode = EEOP_DONE;
4582 ExprEvalPushStep(state, &scratch);
4583
4585
4586 return state;
4587}
AclResult
Definition: acl.h:182
@ ACLCHECK_OK
Definition: acl.h:183
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2622
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition: aclchk.c:3804
Oid collid
static bool ExecComputeSlotInfo(ExprState *state, ExprEvalStep *op)
Definition: execExpr.c:3046
@ EEOP_NOT_DISTINCT
Definition: execExpr.h:184
@ EEOP_INNER_VAR
Definition: execExpr.h:79
@ EEOP_QUAL
Definition: execExpr.h:143
@ EEOP_INNER_FETCHSOME
Definition: execExpr.h:72
@ EEOP_OUTER_FETCHSOME
Definition: execExpr.h:73
@ EEOP_OUTER_VAR
Definition: execExpr.h:80
#define EEO_FLAG_IS_QUAL
Definition: execnodes.h:76
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
#define SizeForFunctionCallInfo(nargs)
Definition: fmgr.h:102
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
#define fmgr_info_set_expr(expr, finfo)
Definition: fmgr.h:135
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1608
void * palloc0(Size size)
Definition: mcxt.c:1347
Oid GetUserId(void)
Definition: miscinit.c:517
#define InvokeFunctionExecuteHook(objectId)
Definition: objectaccess.h:213
@ OBJECT_FUNCTION
Definition: parsenodes.h:2331
#define ACL_EXECUTE
Definition: parsenodes.h:83
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
unsigned int Oid
Definition: postgres_ext.h:32
@ VAR_RETURNING_DEFAULT
Definition: primnodes.h:255
int last_var
Definition: execExpr.h:317
struct ExprEvalStep::@55::@57 var
VarReturningType varreturningtype
Definition: execExpr.h:333
PGFunction fn_addr
Definition: execExpr.h:383
TupleDesc known_desc
Definition: execExpr.h:321
const TupleTableSlotOps * kind
Definition: execExpr.h:323
bool fixed
Definition: execExpr.h:319
struct ExprEvalStep::@55::@65 qualexpr
struct ExprEvalStep::@55::@63 func
FmgrInfo * finfo
Definition: execExpr.h:380
struct ExprEvalStep::@55::@56 fetch
Definition: fmgr.h:57
PGFunction fn_addr
Definition: fmgr.h:58
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:153

References ACL_EXECUTE, aclcheck_error(), ACLCHECK_OK, FunctionCallInfoBaseData::args, Assert, ExprEvalStep::attnum, collid, ExprEvalStep::d, EEO_FLAG_IS_QUAL, EEOP_DONE, EEOP_INNER_FETCHSOME, EEOP_INNER_VAR, EEOP_NOT_DISTINCT, EEOP_OUTER_FETCHSOME, EEOP_OUTER_VAR, EEOP_QUAL, ExecComputeSlotInfo(), ExecReadyExpr(), ExprEvalPushStep(), ExprEvalStep::fcinfo_data, ExprEvalStep::fetch, ExprEvalStep::finfo, ExprEvalStep::fixed, fmgr_info(), fmgr_info_set_expr, ExprEvalStep::fn_addr, FmgrInfo::fn_addr, ExprEvalStep::func, get_func_name(), GetUserId(), InitFunctionCallInfoData, InvokeFunctionExecuteHook, NullableDatum::isnull, ExprEvalStep::jumpdone, ExprEvalStep::kind, ExprEvalStep::known_desc, lappend_int(), ExprEvalStep::last_var, lfirst_int, makeNode, ExprEvalStep::nargs, NIL, object_aclcheck(), OBJECT_FUNCTION, ExprEvalStep::opcode, palloc0(), ExprEvalStep::qualexpr, ExprEvalStep::resnull, ExprEvalStep::resvalue, SizeForFunctionCallInfo, TupleDescAttr(), NullableDatum::value, ExprEvalStep::var, VAR_RETURNING_DEFAULT, ExprEvalStep::varreturningtype, and ExprEvalStep::vartype.

Referenced by BuildTupleHashTable(), ExecInitSubPlan(), and execTuplesMatchPrepare().

◆ ExecBuildHash32Expr()

ExprState * ExecBuildHash32Expr ( TupleDesc  desc,
const TupleTableSlotOps ops,
const Oid hashfunc_oids,
const List collations,
const List hash_exprs,
const bool *  opstrict,
PlanState parent,
uint32  init_value,
bool  keep_nulls 
)

Definition at line 4281 of file execExpr.c.

4285{
4287 ExprEvalStep scratch = {0};
4288 NullableDatum *iresult = NULL;
4289 List *adjust_jumps = NIL;
4290 ListCell *lc;
4291 ListCell *lc2;
4292 intptr_t strict_opcode;
4293 intptr_t opcode;
4294 int num_exprs = list_length(hash_exprs);
4295
4296 Assert(num_exprs == list_length(collations));
4297
4298 state->parent = parent;
4299
4300 /* Insert setup steps as needed. */
4301 ExecCreateExprSetupSteps(state, (Node *) hash_exprs);
4302
4303 /*
4304 * Make a place to store intermediate hash values between subsequent
4305 * hashing of individual expressions. We only need this if there is more
4306 * than one expression to hash or an initial value plus one expression.
4307 */
4308 if ((int64) num_exprs + (init_value != 0) > 1)
4309 iresult = palloc(sizeof(NullableDatum));
4310
4311 if (init_value == 0)
4312 {
4313 /*
4314 * No initial value, so we can assign the result of the hash function
4315 * for the first hash_expr without having to concern ourselves with
4316 * combining the result with any initial value.
4317 */
4318 strict_opcode = EEOP_HASHDATUM_FIRST_STRICT;
4319 opcode = EEOP_HASHDATUM_FIRST;
4320 }
4321 else
4322 {
4323 /*
4324 * Set up operation to set the initial value. Normally we store this
4325 * in the intermediate hash value location, but if there are no exprs
4326 * to hash, store it in the ExprState's result field.
4327 */
4329 scratch.d.hashdatum_initvalue.init_value = UInt32GetDatum(init_value);
4330 scratch.resvalue = num_exprs > 0 ? &iresult->value : &state->resvalue;
4331 scratch.resnull = num_exprs > 0 ? &iresult->isnull : &state->resnull;
4332
4333 ExprEvalPushStep(state, &scratch);
4334
4335 /*
4336 * When using an initial value use the NEXT32/NEXT32_STRICT ops as the
4337 * FIRST/FIRST_STRICT ops would overwrite the stored initial value.
4338 */
4339 strict_opcode = EEOP_HASHDATUM_NEXT32_STRICT;
4340 opcode = EEOP_HASHDATUM_NEXT32;
4341 }
4342
4343 forboth(lc, hash_exprs, lc2, collations)
4344 {
4345 Expr *expr = (Expr *) lfirst(lc);
4346 FmgrInfo *finfo;
4347 FunctionCallInfo fcinfo;
4348 int i = foreach_current_index(lc);
4349 Oid funcid;
4350 Oid inputcollid = lfirst_oid(lc2);
4351
4352 funcid = hashfunc_oids[i];
4353
4354 /* Allocate hash function lookup data. */
4355 finfo = palloc0(sizeof(FmgrInfo));
4356 fcinfo = palloc0(SizeForFunctionCallInfo(1));
4357
4358 fmgr_info(funcid, finfo);
4359
4360 /*
4361 * Build the steps to evaluate the hash function's argument have it so
4362 * the value of that is stored in the 0th argument of the hash func.
4363 */
4364 ExecInitExprRec(expr,
4365 state,
4366 &fcinfo->args[0].value,
4367 &fcinfo->args[0].isnull);
4368
4369 if (i == num_exprs - 1)
4370 {
4371 /* the result for hashing the final expr is stored in the state */
4372 scratch.resvalue = &state->resvalue;
4373 scratch.resnull = &state->resnull;
4374 }
4375 else
4376 {
4377 Assert(iresult != NULL);
4378
4379 /* intermediate values are stored in an intermediate result */
4380 scratch.resvalue = &iresult->value;
4381 scratch.resnull = &iresult->isnull;
4382 }
4383
4384 /*
4385 * NEXT32 opcodes need to look at the intermediate result. We might
4386 * as well just set this for all ops. FIRSTs won't look at it.
4387 */
4388 scratch.d.hashdatum.iresult = iresult;
4389
4390 /* Initialize function call parameter structure too */
4391 InitFunctionCallInfoData(*fcinfo, finfo, 1, inputcollid, NULL, NULL);
4392
4393 scratch.d.hashdatum.finfo = finfo;
4394 scratch.d.hashdatum.fcinfo_data = fcinfo;
4395 scratch.d.hashdatum.fn_addr = finfo->fn_addr;
4396
4397 scratch.opcode = opstrict[i] && !keep_nulls ? strict_opcode : opcode;
4398 scratch.d.hashdatum.jumpdone = -1;
4399
4400 ExprEvalPushStep(state, &scratch);
4401 adjust_jumps = lappend_int(adjust_jumps, state->steps_len - 1);
4402
4403 /*
4404 * For subsequent keys we must combine the hash value with the
4405 * previous hashes.
4406 */
4407 strict_opcode = EEOP_HASHDATUM_NEXT32_STRICT;
4408 opcode = EEOP_HASHDATUM_NEXT32;
4409 }
4410
4411 /* adjust jump targets */
4412 foreach(lc, adjust_jumps)
4413 {
4414 ExprEvalStep *as = &state->steps[lfirst_int(lc)];
4415
4420 Assert(as->d.hashdatum.jumpdone == -1);
4421 as->d.hashdatum.jumpdone = state->steps_len;
4422 }
4423
4424 scratch.resvalue = NULL;
4425 scratch.resnull = NULL;
4426 scratch.opcode = EEOP_DONE;
4427 ExprEvalPushStep(state, &scratch);
4428
4430
4431 return state;
4432}
int64_t int64
Definition: c.h:485
static void ExecCreateExprSetupSteps(ExprState *state, Node *node)
Definition: execExpr.c:2865
@ EEOP_HASHDATUM_NEXT32_STRICT
Definition: execExpr.h:252
@ EEOP_HASHDATUM_FIRST_STRICT
Definition: execExpr.h:250
@ EEOP_HASHDATUM_NEXT32
Definition: execExpr.h:251
@ EEOP_HASHDATUM_FIRST
Definition: execExpr.h:249
@ EEOP_HASHDATUM_SET_INITVAL
Definition: execExpr.h:248
int i
Definition: isn.c:72
void * palloc(Size size)
Definition: mcxt.c:1317
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
#define foreach_current_index(var_or_cell)
Definition: pg_list.h:403
#define lfirst_oid(lc)
Definition: pg_list.h:174
static Datum UInt32GetDatum(uint32 X)
Definition: postgres.h:237
NullableDatum * iresult
Definition: execExpr.h:601
struct ExprEvalStep::@55::@86 hashdatum_initvalue
struct ExprEvalStep::@55::@87 hashdatum
Datum init_value
Definition: execExpr.h:589

References FunctionCallInfoBaseData::args, Assert, ExprEvalStep::d, EEOP_DONE, EEOP_HASHDATUM_FIRST, EEOP_HASHDATUM_FIRST_STRICT, EEOP_HASHDATUM_NEXT32, EEOP_HASHDATUM_NEXT32_STRICT, EEOP_HASHDATUM_SET_INITVAL, ExecCreateExprSetupSteps(), ExecInitExprRec(), ExecReadyExpr(), ExprEvalPushStep(), ExprEvalStep::fcinfo_data, ExprEvalStep::finfo, fmgr_info(), ExprEvalStep::fn_addr, FmgrInfo::fn_addr, forboth, foreach_current_index, ExprEvalStep::hashdatum, ExprEvalStep::hashdatum_initvalue, i, ExprEvalStep::init_value, InitFunctionCallInfoData, ExprEvalStep::iresult, NullableDatum::isnull, ExprEvalStep::jumpdone, lappend_int(), lfirst, lfirst_int, lfirst_oid, list_length(), makeNode, NIL, ExprEvalStep::opcode, palloc(), palloc0(), ExprEvalStep::resnull, ExprEvalStep::resvalue, SizeForFunctionCallInfo, UInt32GetDatum(), and NullableDatum::value.

Referenced by ExecInitHashJoin().

◆ ExecBuildHash32FromAttrs()

ExprState * ExecBuildHash32FromAttrs ( TupleDesc  desc,
const TupleTableSlotOps ops,
FmgrInfo hashfunctions,
Oid collations,
int  numCols,
AttrNumber keyColIdx,
PlanState parent,
uint32  init_value 
)

Definition at line 4122 of file execExpr.c.

4126{
4128 ExprEvalStep scratch = {0};
4129 NullableDatum *iresult = NULL;
4130 intptr_t opcode;
4131 AttrNumber last_attnum = 0;
4132
4133 Assert(numCols >= 0);
4134
4135 state->parent = parent;
4136
4137 /*
4138 * Make a place to store intermediate hash values between subsequent
4139 * hashing of individual columns. We only need this if there is more than
4140 * one column to hash or an initial value plus one column.
4141 */
4142 if ((int64) numCols + (init_value != 0) > 1)
4143 iresult = palloc(sizeof(NullableDatum));
4144
4145 /* find the highest attnum so we deform the tuple to that point */
4146 for (int i = 0; i < numCols; i++)
4147 last_attnum = Max(last_attnum, keyColIdx[i]);
4148
4149 scratch.opcode = EEOP_INNER_FETCHSOME;
4150 scratch.d.fetch.last_var = last_attnum;
4151 scratch.d.fetch.fixed = false;
4152 scratch.d.fetch.kind = ops;
4153 scratch.d.fetch.known_desc = desc;
4154 if (ExecComputeSlotInfo(state, &scratch))
4155 ExprEvalPushStep(state, &scratch);
4156
4157 if (init_value == 0)
4158 {
4159 /*
4160 * No initial value, so we can assign the result of the hash function
4161 * for the first attribute without having to concern ourselves with
4162 * combining the result with any initial value.
4163 */
4164 opcode = EEOP_HASHDATUM_FIRST;
4165 }
4166 else
4167 {
4168 /*
4169 * Set up operation to set the initial value. Normally we store this
4170 * in the intermediate hash value location, but if there are no
4171 * columns to hash, store it in the ExprState's result field.
4172 */
4174 scratch.d.hashdatum_initvalue.init_value = UInt32GetDatum(init_value);
4175 scratch.resvalue = numCols > 0 ? &iresult->value : &state->resvalue;
4176 scratch.resnull = numCols > 0 ? &iresult->isnull : &state->resnull;
4177
4178 ExprEvalPushStep(state, &scratch);
4179
4180 /*
4181 * When using an initial value use the NEXT32 ops as the FIRST ops
4182 * would overwrite the stored initial value.
4183 */
4184 opcode = EEOP_HASHDATUM_NEXT32;
4185 }
4186
4187 for (int i = 0; i < numCols; i++)
4188 {
4189 FmgrInfo *finfo;
4190 FunctionCallInfo fcinfo;
4191 Oid inputcollid = collations[i];
4192 AttrNumber attnum = keyColIdx[i] - 1;
4193
4194 finfo = &hashfunctions[i];
4195 fcinfo = palloc0(SizeForFunctionCallInfo(1));
4196
4197 /* Initialize function call parameter structure too */
4198 InitFunctionCallInfoData(*fcinfo, finfo, 1, inputcollid, NULL, NULL);
4199
4200 /*
4201 * Fetch inner Var for this attnum and store it in the 1st arg of the
4202 * hash func.
4203 */
4204 scratch.opcode = EEOP_INNER_VAR;
4205 scratch.resvalue = &fcinfo->args[0].value;
4206 scratch.resnull = &fcinfo->args[0].isnull;
4207 scratch.d.var.attnum = attnum;
4208 scratch.d.var.vartype = TupleDescAttr(desc, attnum)->atttypid;
4210
4211 ExprEvalPushStep(state, &scratch);
4212
4213 /* Call the hash function */
4214 scratch.opcode = opcode;
4215
4216 if (i == numCols - 1)
4217 {
4218 /*
4219 * The result for hashing the final column is stored in the
4220 * ExprState.
4221 */
4222 scratch.resvalue = &state->resvalue;
4223 scratch.resnull = &state->resnull;
4224 }
4225 else
4226 {
4227 Assert(iresult != NULL);
4228
4229 /* intermediate values are stored in an intermediate result */
4230 scratch.resvalue = &iresult->value;
4231 scratch.resnull = &iresult->isnull;
4232 }
4233
4234 /*
4235 * NEXT32 opcodes need to look at the intermediate result. We might
4236 * as well just set this for all ops. FIRSTs won't look at it.
4237 */
4238 scratch.d.hashdatum.iresult = iresult;
4239
4240 scratch.d.hashdatum.finfo = finfo;
4241 scratch.d.hashdatum.fcinfo_data = fcinfo;
4242 scratch.d.hashdatum.fn_addr = finfo->fn_addr;
4243 scratch.d.hashdatum.jumpdone = -1;
4244
4245 ExprEvalPushStep(state, &scratch);
4246
4247 /* subsequent attnums must be combined with the previous */
4248 opcode = EEOP_HASHDATUM_NEXT32;
4249 }
4250
4251 scratch.resvalue = NULL;
4252 scratch.resnull = NULL;
4253 scratch.opcode = EEOP_DONE;
4254 ExprEvalPushStep(state, &scratch);
4255
4257
4258 return state;
4259}
int16 AttrNumber
Definition: attnum.h:21
int16 attnum
Definition: pg_attribute.h:74

References FunctionCallInfoBaseData::args, Assert, attnum, ExprEvalStep::attnum, ExprEvalStep::d, EEOP_DONE, EEOP_HASHDATUM_FIRST, EEOP_HASHDATUM_NEXT32, EEOP_HASHDATUM_SET_INITVAL, EEOP_INNER_FETCHSOME, EEOP_INNER_VAR, ExecComputeSlotInfo(), ExecReadyExpr(), ExprEvalPushStep(), ExprEvalStep::fcinfo_data, ExprEvalStep::fetch, ExprEvalStep::finfo, ExprEvalStep::fixed, ExprEvalStep::fn_addr, FmgrInfo::fn_addr, ExprEvalStep::hashdatum, ExprEvalStep::hashdatum_initvalue, i, ExprEvalStep::init_value, InitFunctionCallInfoData, ExprEvalStep::iresult, NullableDatum::isnull, ExprEvalStep::jumpdone, ExprEvalStep::kind, ExprEvalStep::known_desc, ExprEvalStep::last_var, makeNode, Max, ExprEvalStep::opcode, palloc(), palloc0(), ExprEvalStep::resnull, ExprEvalStep::resvalue, SizeForFunctionCallInfo, TupleDescAttr(), UInt32GetDatum(), NullableDatum::value, ExprEvalStep::var, VAR_RETURNING_DEFAULT, ExprEvalStep::varreturningtype, and ExprEvalStep::vartype.

Referenced by BuildTupleHashTable(), and ExecInitSubPlan().

◆ ExecBuildParamSetEqual()

ExprState * ExecBuildParamSetEqual ( TupleDesc  desc,
const TupleTableSlotOps lops,
const TupleTableSlotOps rops,
const Oid eqfunctions,
const Oid collations,
const List param_exprs,
PlanState parent 
)

Definition at line 4605 of file execExpr.c.

4612{
4614 ExprEvalStep scratch = {0};
4615 int maxatt = list_length(param_exprs);
4616 List *adjust_jumps = NIL;
4617 ListCell *lc;
4618
4619 state->expr = NULL;
4620 state->flags = EEO_FLAG_IS_QUAL;
4621 state->parent = parent;
4622
4623 scratch.resvalue = &state->resvalue;
4624 scratch.resnull = &state->resnull;
4625
4626 /* push deform steps */
4627 scratch.opcode = EEOP_INNER_FETCHSOME;
4628 scratch.d.fetch.last_var = maxatt;
4629 scratch.d.fetch.fixed = false;
4630 scratch.d.fetch.known_desc = desc;
4631 scratch.d.fetch.kind = lops;
4632 if (ExecComputeSlotInfo(state, &scratch))
4633 ExprEvalPushStep(state, &scratch);
4634
4635 scratch.opcode = EEOP_OUTER_FETCHSOME;
4636 scratch.d.fetch.last_var = maxatt;
4637 scratch.d.fetch.fixed = false;
4638 scratch.d.fetch.known_desc = desc;
4639 scratch.d.fetch.kind = rops;
4640 if (ExecComputeSlotInfo(state, &scratch))
4641 ExprEvalPushStep(state, &scratch);
4642
4643 for (int attno = 0; attno < maxatt; attno++)
4644 {
4645 Form_pg_attribute att = TupleDescAttr(desc, attno);
4646 Oid foid = eqfunctions[attno];
4647 Oid collid = collations[attno];
4648 FmgrInfo *finfo;
4649 FunctionCallInfo fcinfo;
4650 AclResult aclresult;
4651
4652 /* Check permission to call function */
4653 aclresult = object_aclcheck(ProcedureRelationId, foid, GetUserId(), ACL_EXECUTE);
4654 if (aclresult != ACLCHECK_OK)
4655 aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(foid));
4656
4658
4659 /* Set up the primary fmgr lookup information */
4660 finfo = palloc0(sizeof(FmgrInfo));
4661 fcinfo = palloc0(SizeForFunctionCallInfo(2));
4662 fmgr_info(foid, finfo);
4663 fmgr_info_set_expr(NULL, finfo);
4664 InitFunctionCallInfoData(*fcinfo, finfo, 2,
4665 collid, NULL, NULL);
4666
4667 /* left arg */
4668 scratch.opcode = EEOP_INNER_VAR;
4669 scratch.d.var.attnum = attno;
4670 scratch.d.var.vartype = att->atttypid;
4672 scratch.resvalue = &fcinfo->args[0].value;
4673 scratch.resnull = &fcinfo->args[0].isnull;
4674 ExprEvalPushStep(state, &scratch);
4675
4676 /* right arg */
4677 scratch.opcode = EEOP_OUTER_VAR;
4678 scratch.d.var.attnum = attno;
4679 scratch.d.var.vartype = att->atttypid;
4681 scratch.resvalue = &fcinfo->args[1].value;
4682 scratch.resnull = &fcinfo->args[1].isnull;
4683 ExprEvalPushStep(state, &scratch);
4684
4685 /* evaluate distinctness */
4686 scratch.opcode = EEOP_NOT_DISTINCT;
4687 scratch.d.func.finfo = finfo;
4688 scratch.d.func.fcinfo_data = fcinfo;
4689 scratch.d.func.fn_addr = finfo->fn_addr;
4690 scratch.d.func.nargs = 2;
4691 scratch.resvalue = &state->resvalue;
4692 scratch.resnull = &state->resnull;
4693 ExprEvalPushStep(state, &scratch);
4694
4695 /* then emit EEOP_QUAL to detect if result is false (or null) */
4696 scratch.opcode = EEOP_QUAL;
4697 scratch.d.qualexpr.jumpdone = -1;
4698 scratch.resvalue = &state->resvalue;
4699 scratch.resnull = &state->resnull;
4700 ExprEvalPushStep(state, &scratch);
4701 adjust_jumps = lappend_int(adjust_jumps,
4702 state->steps_len - 1);
4703 }
4704
4705 /* adjust jump targets */
4706 foreach(lc, adjust_jumps)
4707 {
4708 ExprEvalStep *as = &state->steps[lfirst_int(lc)];
4709
4710 Assert(as->opcode == EEOP_QUAL);
4711 Assert(as->d.qualexpr.jumpdone == -1);
4712 as->d.qualexpr.jumpdone = state->steps_len;
4713 }
4714
4715 scratch.resvalue = NULL;
4716 scratch.resnull = NULL;
4717 scratch.opcode = EEOP_DONE;
4718 ExprEvalPushStep(state, &scratch);
4719
4721
4722 return state;
4723}

References ACL_EXECUTE, aclcheck_error(), ACLCHECK_OK, FunctionCallInfoBaseData::args, Assert, ExprEvalStep::attnum, collid, ExprEvalStep::d, EEO_FLAG_IS_QUAL, EEOP_DONE, EEOP_INNER_FETCHSOME, EEOP_INNER_VAR, EEOP_NOT_DISTINCT, EEOP_OUTER_FETCHSOME, EEOP_OUTER_VAR, EEOP_QUAL, ExecComputeSlotInfo(), ExecReadyExpr(), ExprEvalPushStep(), ExprEvalStep::fcinfo_data, ExprEvalStep::fetch, ExprEvalStep::finfo, ExprEvalStep::fixed, fmgr_info(), fmgr_info_set_expr, ExprEvalStep::fn_addr, FmgrInfo::fn_addr, ExprEvalStep::func, get_func_name(), GetUserId(), InitFunctionCallInfoData, InvokeFunctionExecuteHook, NullableDatum::isnull, ExprEvalStep::jumpdone, ExprEvalStep::kind, ExprEvalStep::known_desc, lappend_int(), ExprEvalStep::last_var, lfirst_int, list_length(), makeNode, ExprEvalStep::nargs, NIL, object_aclcheck(), OBJECT_FUNCTION, ExprEvalStep::opcode, palloc0(), ExprEvalStep::qualexpr, ExprEvalStep::resnull, ExprEvalStep::resvalue, SizeForFunctionCallInfo, TupleDescAttr(), NullableDatum::value, ExprEvalStep::var, VAR_RETURNING_DEFAULT, ExprEvalStep::varreturningtype, and ExprEvalStep::vartype.

Referenced by ExecInitMemoize().

◆ ExecBuildProjectionInfo()

ProjectionInfo * ExecBuildProjectionInfo ( List targetList,
ExprContext econtext,
TupleTableSlot slot,
PlanState parent,
TupleDesc  inputDesc 
)

Definition at line 370 of file execExpr.c.

375{
378 ExprEvalStep scratch = {0};
379 ListCell *lc;
380
381 projInfo->pi_exprContext = econtext;
382 /* We embed ExprState into ProjectionInfo instead of doing extra palloc */
383 projInfo->pi_state.type = T_ExprState;
384 state = &projInfo->pi_state;
385 state->expr = (Expr *) targetList;
386 state->parent = parent;
387 state->ext_params = NULL;
388
389 state->resultslot = slot;
390
391 /* Insert setup steps as needed */
392 ExecCreateExprSetupSteps(state, (Node *) targetList);
393
394 /* Now compile each tlist column */
395 foreach(lc, targetList)
396 {
398 Var *variable = NULL;
399 AttrNumber attnum = 0;
400 bool isSafeVar = false;
401
402 /*
403 * If tlist expression is a safe non-system Var, use the fast-path
404 * ASSIGN_*_VAR opcodes. "Safe" means that we don't need to apply
405 * CheckVarSlotCompatibility() during plan startup. If a source slot
406 * was provided, we make the equivalent tests here; if a slot was not
407 * provided, we assume that no check is needed because we're dealing
408 * with a non-relation-scan-level expression.
409 */
410 if (tle->expr != NULL &&
411 IsA(tle->expr, Var) &&
412 ((Var *) tle->expr)->varattno > 0)
413 {
414 /* Non-system Var, but how safe is it? */
415 variable = (Var *) tle->expr;
416 attnum = variable->varattno;
417
418 if (inputDesc == NULL)
419 isSafeVar = true; /* can't check, just assume OK */
420 else if (attnum <= inputDesc->natts)
421 {
422 Form_pg_attribute attr = TupleDescAttr(inputDesc, attnum - 1);
423
424 /*
425 * If user attribute is dropped or has a type mismatch, don't
426 * use ASSIGN_*_VAR. Instead let the normal expression
427 * machinery handle it (which'll possibly error out).
428 */
429 if (!attr->attisdropped && variable->vartype == attr->atttypid)
430 {
431 isSafeVar = true;
432 }
433 }
434 }
435
436 if (isSafeVar)
437 {
438 /* Fast-path: just generate an EEOP_ASSIGN_*_VAR step */
439 switch (variable->varno)
440 {
441 case INNER_VAR:
442 /* get the tuple from the inner node */
444 break;
445
446 case OUTER_VAR:
447 /* get the tuple from the outer node */
449 break;
450
451 /* INDEX_VAR is handled by default case */
452
453 default:
454
455 /*
456 * Get the tuple from the relation being scanned, or the
457 * old/new tuple slot, if old/new values were requested.
458 */
459 switch (variable->varreturningtype)
460 {
463 break;
465 scratch.opcode = EEOP_ASSIGN_OLD_VAR;
466 state->flags |= EEO_FLAG_HAS_OLD;
467 break;
469 scratch.opcode = EEOP_ASSIGN_NEW_VAR;
470 state->flags |= EEO_FLAG_HAS_NEW;
471 break;
472 }
473 break;
474 }
475
476 scratch.d.assign_var.attnum = attnum - 1;
477 scratch.d.assign_var.resultnum = tle->resno - 1;
478 ExprEvalPushStep(state, &scratch);
479 }
480 else
481 {
482 /*
483 * Otherwise, compile the column expression normally.
484 *
485 * We can't tell the expression to evaluate directly into the
486 * result slot, as the result slot (and the exprstate for that
487 * matter) can change between executions. We instead evaluate
488 * into the ExprState's resvalue/resnull and then move.
489 */
491 &state->resvalue, &state->resnull);
492
493 /*
494 * Column might be referenced multiple times in upper nodes, so
495 * force value to R/O - but only if it could be an expanded datum.
496 */
497 if (get_typlen(exprType((Node *) tle->expr)) == -1)
499 else
500 scratch.opcode = EEOP_ASSIGN_TMP;
501 scratch.d.assign_tmp.resultnum = tle->resno - 1;
502 ExprEvalPushStep(state, &scratch);
503 }
504 }
505
506 scratch.opcode = EEOP_DONE;
507 ExprEvalPushStep(state, &scratch);
508
510
511 return projInfo;
512}
@ EEOP_ASSIGN_TMP
Definition: execExpr.h:107
@ EEOP_ASSIGN_SCAN_VAR
Definition: execExpr.h:102
@ EEOP_ASSIGN_OUTER_VAR
Definition: execExpr.h:101
@ EEOP_ASSIGN_OLD_VAR
Definition: execExpr.h:103
@ EEOP_ASSIGN_TMP_MAKE_RO
Definition: execExpr.h:109
@ EEOP_ASSIGN_INNER_VAR
Definition: execExpr.h:100
@ EEOP_ASSIGN_NEW_VAR
Definition: execExpr.h:104
#define EEO_FLAG_HAS_OLD
Definition: execnodes.h:78
#define EEO_FLAG_HAS_NEW
Definition: execnodes.h:80
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
int16 get_typlen(Oid typid)
Definition: lsyscache.c:2197
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define lfirst_node(type, lc)
Definition: pg_list.h:176
@ VAR_RETURNING_OLD
Definition: primnodes.h:256
@ VAR_RETURNING_NEW
Definition: primnodes.h:257
#define OUTER_VAR
Definition: primnodes.h:242
#define INNER_VAR
Definition: primnodes.h:241
struct ExprEvalStep::@55::@60 assign_tmp
int resultnum
Definition: execExpr.h:350
struct ExprEvalStep::@55::@59 assign_var
NodeTag type
Definition: execnodes.h:88
ExprState pi_state
Definition: execnodes.h:380
ExprContext * pi_exprContext
Definition: execnodes.h:382
AttrNumber resno
Definition: primnodes.h:2247
Definition: primnodes.h:261

References ExprEvalStep::assign_tmp, ExprEvalStep::assign_var, attnum, ExprEvalStep::attnum, ExprEvalStep::d, EEO_FLAG_HAS_NEW, EEO_FLAG_HAS_OLD, EEOP_ASSIGN_INNER_VAR, EEOP_ASSIGN_NEW_VAR, EEOP_ASSIGN_OLD_VAR, EEOP_ASSIGN_OUTER_VAR, EEOP_ASSIGN_SCAN_VAR, EEOP_ASSIGN_TMP, EEOP_ASSIGN_TMP_MAKE_RO, EEOP_DONE, ExecCreateExprSetupSteps(), ExecInitExprRec(), ExecReadyExpr(), TargetEntry::expr, ExprEvalPushStep(), exprType(), get_typlen(), if(), INNER_VAR, IsA, lfirst_node, makeNode, ExprEvalStep::opcode, OUTER_VAR, ProjectionInfo::pi_exprContext, ProjectionInfo::pi_state, TargetEntry::resno, ExprEvalStep::resultnum, TupleDescAttr(), ExprState::type, VAR_RETURNING_DEFAULT, VAR_RETURNING_NEW, and VAR_RETURNING_OLD.

Referenced by ExecAssignProjectionInfo(), ExecInitInsertProjection(), ExecInitMerge(), ExecInitModifyTable(), ExecInitPartitionInfo(), and ExecInitSubPlan().

◆ ExecBuildUpdateProjection()

ProjectionInfo * ExecBuildUpdateProjection ( List targetList,
bool  evalTargetList,
List targetColnos,
TupleDesc  relDesc,
ExprContext econtext,
TupleTableSlot slot,
PlanState parent 
)

Definition at line 547 of file execExpr.c.

554{
557 int nAssignableCols;
558 bool sawJunk;
559 Bitmapset *assignedCols;
560 ExprSetupInfo deform = {0, 0, 0, 0, 0, NIL};
561 ExprEvalStep scratch = {0};
562 int outerattnum;
563 ListCell *lc,
564 *lc2;
565
566 projInfo->pi_exprContext = econtext;
567 /* We embed ExprState into ProjectionInfo instead of doing extra palloc */
568 projInfo->pi_state.type = T_ExprState;
569 state = &projInfo->pi_state;
570 if (evalTargetList)
571 state->expr = (Expr *) targetList;
572 else
573 state->expr = NULL; /* not used */
574 state->parent = parent;
575 state->ext_params = NULL;
576
577 state->resultslot = slot;
578
579 /*
580 * Examine the targetList to see how many non-junk columns there are, and
581 * to verify that the non-junk columns come before the junk ones.
582 */
583 nAssignableCols = 0;
584 sawJunk = false;
585 foreach(lc, targetList)
586 {
588
589 if (tle->resjunk)
590 sawJunk = true;
591 else
592 {
593 if (sawJunk)
594 elog(ERROR, "subplan target list is out of order");
595 nAssignableCols++;
596 }
597 }
598
599 /* We should have one targetColnos entry per non-junk column */
600 if (nAssignableCols != list_length(targetColnos))
601 elog(ERROR, "targetColnos does not match subplan target list");
602
603 /*
604 * Build a bitmapset of the columns in targetColnos. (We could just use
605 * list_member_int() tests, but that risks O(N^2) behavior with many
606 * columns.)
607 */
608 assignedCols = NULL;
609 foreach(lc, targetColnos)
610 {
611 AttrNumber targetattnum = lfirst_int(lc);
612
613 assignedCols = bms_add_member(assignedCols, targetattnum);
614 }
615
616 /*
617 * We need to insert EEOP_*_FETCHSOME steps to ensure the input tuples are
618 * sufficiently deconstructed. The scan tuple must be deconstructed at
619 * least as far as the last old column we need.
620 */
621 for (int attnum = relDesc->natts; attnum > 0; attnum--)
622 {
623 CompactAttribute *attr = TupleDescCompactAttr(relDesc, attnum - 1);
624
625 if (attr->attisdropped)
626 continue;
627 if (bms_is_member(attnum, assignedCols))
628 continue;
629 deform.last_scan = attnum;
630 break;
631 }
632
633 /*
634 * If we're actually evaluating the tlist, incorporate its input
635 * requirements too; otherwise, we'll just need to fetch the appropriate
636 * number of columns of the "outer" tuple.
637 */
638 if (evalTargetList)
639 expr_setup_walker((Node *) targetList, &deform);
640 else
641 deform.last_outer = nAssignableCols;
642
644
645 /*
646 * Now generate code to evaluate the tlist's assignable expressions or
647 * fetch them from the outer tuple, incidentally validating that they'll
648 * be of the right data type. The checks above ensure that the forboth()
649 * will iterate over exactly the non-junk columns. Note that we don't
650 * bother evaluating any remaining resjunk columns.
651 */
652 outerattnum = 0;
653 forboth(lc, targetList, lc2, targetColnos)
654 {
656 AttrNumber targetattnum = lfirst_int(lc2);
658
659 Assert(!tle->resjunk);
660
661 /*
662 * Apply sanity checks comparable to ExecCheckPlanOutput().
663 */
664 if (targetattnum <= 0 || targetattnum > relDesc->natts)
666 (errcode(ERRCODE_DATATYPE_MISMATCH),
667 errmsg("table row type and query-specified row type do not match"),
668 errdetail("Query has too many columns.")));
669 attr = TupleDescAttr(relDesc, targetattnum - 1);
670
671 if (attr->attisdropped)
673 (errcode(ERRCODE_DATATYPE_MISMATCH),
674 errmsg("table row type and query-specified row type do not match"),
675 errdetail("Query provides a value for a dropped column at ordinal position %d.",
676 targetattnum)));
677 if (exprType((Node *) tle->expr) != attr->atttypid)
679 (errcode(ERRCODE_DATATYPE_MISMATCH),
680 errmsg("table row type and query-specified row type do not match"),
681 errdetail("Table has type %s at ordinal position %d, but query expects %s.",
682 format_type_be(attr->atttypid),
683 targetattnum,
684 format_type_be(exprType((Node *) tle->expr)))));
685
686 /* OK, generate code to perform the assignment. */
687 if (evalTargetList)
688 {
689 /*
690 * We must evaluate the TLE's expression and assign it. We do not
691 * bother jumping through hoops for "safe" Vars like
692 * ExecBuildProjectionInfo does; this is a relatively less-used
693 * path and it doesn't seem worth expending code for that.
694 */
696 &state->resvalue, &state->resnull);
697 /* Needn't worry about read-only-ness here, either. */
698 scratch.opcode = EEOP_ASSIGN_TMP;
699 scratch.d.assign_tmp.resultnum = targetattnum - 1;
700 ExprEvalPushStep(state, &scratch);
701 }
702 else
703 {
704 /* Just assign from the outer tuple. */
706 scratch.d.assign_var.attnum = outerattnum;
707 scratch.d.assign_var.resultnum = targetattnum - 1;
708 ExprEvalPushStep(state, &scratch);
709 }
710 outerattnum++;
711 }
712
713 /*
714 * Now generate code to copy over any old columns that were not assigned
715 * to, and to ensure that dropped columns are set to NULL.
716 */
717 for (int attnum = 1; attnum <= relDesc->natts; attnum++)
718 {
719 CompactAttribute *attr = TupleDescCompactAttr(relDesc, attnum - 1);
720
721 if (attr->attisdropped)
722 {
723 /* Put a null into the ExprState's resvalue/resnull ... */
724 scratch.opcode = EEOP_CONST;
725 scratch.resvalue = &state->resvalue;
726 scratch.resnull = &state->resnull;
727 scratch.d.constval.value = (Datum) 0;
728 scratch.d.constval.isnull = true;
729 ExprEvalPushStep(state, &scratch);
730 /* ... then assign it to the result slot */
731 scratch.opcode = EEOP_ASSIGN_TMP;
732 scratch.d.assign_tmp.resultnum = attnum - 1;
733 ExprEvalPushStep(state, &scratch);
734 }
735 else if (!bms_is_member(attnum, assignedCols))
736 {
737 /* Certainly the right type, so needn't check */
739 scratch.d.assign_var.attnum = attnum - 1;
740 scratch.d.assign_var.resultnum = attnum - 1;
741 ExprEvalPushStep(state, &scratch);
742 }
743 }
744
745 scratch.opcode = EEOP_DONE;
746 ExprEvalPushStep(state, &scratch);
747
749
750 return projInfo;
751}
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
@ EEOP_CONST
Definition: execExpr.h:112
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
bool attisdropped
Definition: tupdesc.h:76
struct ExprEvalStep::@55::@62 constval
Datum value
Definition: execExpr.h:373
bool isnull
Definition: execExpr.h:374
AttrNumber last_scan
Definition: execExpr.c:64
AttrNumber last_outer
Definition: execExpr.c:63
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:168

References Assert, ExprEvalStep::assign_tmp, ExprEvalStep::assign_var, CompactAttribute::attisdropped, attnum, ExprEvalStep::attnum, bms_add_member(), bms_is_member(), ExprEvalStep::constval, ExprEvalStep::d, EEOP_ASSIGN_OUTER_VAR, EEOP_ASSIGN_SCAN_VAR, EEOP_ASSIGN_TMP, EEOP_CONST, EEOP_DONE, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, ExecInitExprRec(), ExecPushExprSetupSteps(), ExecReadyExpr(), TargetEntry::expr, expr_setup_walker(), ExprEvalPushStep(), exprType(), forboth, format_type_be(), ExprEvalStep::isnull, ExprSetupInfo::last_outer, ExprSetupInfo::last_scan, lfirst_int, lfirst_node, list_length(), makeNode, TupleDescData::natts, NIL, ExprEvalStep::opcode, ProjectionInfo::pi_exprContext, ProjectionInfo::pi_state, ExprEvalStep::resnull, ExprEvalStep::resultnum, ExprEvalStep::resvalue, TupleDescAttr(), TupleDescCompactAttr(), ExprState::type, and ExprEvalStep::value.

Referenced by ExecInitMerge(), ExecInitModifyTable(), ExecInitPartitionInfo(), and ExecInitUpdateProjection().

◆ ExecCheck()

bool ExecCheck ( ExprState state,
ExprContext econtext 
)

Definition at line 872 of file execExpr.c.

873{
874 Datum ret;
875 bool isnull;
876
877 /* short-circuit (here and in ExecInitCheck) for empty restriction list */
878 if (state == NULL)
879 return true;
880
881 /* verify that expression was not compiled using ExecInitQual */
882 Assert(!(state->flags & EEO_FLAG_IS_QUAL));
883
884 ret = ExecEvalExprSwitchContext(state, econtext, &isnull);
885
886 if (isnull)
887 return true;
888
889 return DatumGetBool(ret);
890}
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:361
static bool DatumGetBool(Datum X)
Definition: postgres.h:95

References Assert, DatumGetBool(), EEO_FLAG_IS_QUAL, and ExecEvalExprSwitchContext().

Referenced by ATRewriteTable(), check_default_partition_contents(), domain_check_input(), ExecPartitionCheck(), and ExecRelCheck().

◆ ExecComputeSlotInfo()

static bool ExecComputeSlotInfo ( ExprState state,
ExprEvalStep op 
)
static

Definition at line 3046 of file execExpr.c.

3047{
3048 PlanState *parent = state->parent;
3049 TupleDesc desc = NULL;
3050 const TupleTableSlotOps *tts_ops = NULL;
3051 bool isfixed = false;
3052 ExprEvalOp opcode = op->opcode;
3053
3054 Assert(opcode == EEOP_INNER_FETCHSOME ||
3055 opcode == EEOP_OUTER_FETCHSOME ||
3056 opcode == EEOP_SCAN_FETCHSOME ||
3057 opcode == EEOP_OLD_FETCHSOME ||
3058 opcode == EEOP_NEW_FETCHSOME);
3059
3060 if (op->d.fetch.known_desc != NULL)
3061 {
3062 desc = op->d.fetch.known_desc;
3063 tts_ops = op->d.fetch.kind;
3064 isfixed = op->d.fetch.kind != NULL;
3065 }
3066 else if (!parent)
3067 {
3068 isfixed = false;
3069 }
3070 else if (opcode == EEOP_INNER_FETCHSOME)
3071 {
3072 PlanState *is = innerPlanState(parent);
3073
3074 if (parent->inneropsset && !parent->inneropsfixed)
3075 {
3076 isfixed = false;
3077 }
3078 else if (parent->inneropsset && parent->innerops)
3079 {
3080 isfixed = true;
3081 tts_ops = parent->innerops;
3082 desc = ExecGetResultType(is);
3083 }
3084 else if (is)
3085 {
3086 tts_ops = ExecGetResultSlotOps(is, &isfixed);
3087 desc = ExecGetResultType(is);
3088 }
3089 }
3090 else if (opcode == EEOP_OUTER_FETCHSOME)
3091 {
3092 PlanState *os = outerPlanState(parent);
3093
3094 if (parent->outeropsset && !parent->outeropsfixed)
3095 {
3096 isfixed = false;
3097 }
3098 else if (parent->outeropsset && parent->outerops)
3099 {
3100 isfixed = true;
3101 tts_ops = parent->outerops;
3102 desc = ExecGetResultType(os);
3103 }
3104 else if (os)
3105 {
3106 tts_ops = ExecGetResultSlotOps(os, &isfixed);
3107 desc = ExecGetResultType(os);
3108 }
3109 }
3110 else if (opcode == EEOP_SCAN_FETCHSOME ||
3111 opcode == EEOP_OLD_FETCHSOME ||
3112 opcode == EEOP_NEW_FETCHSOME)
3113 {
3114 desc = parent->scandesc;
3115
3116 if (parent->scanops)
3117 tts_ops = parent->scanops;
3118
3119 if (parent->scanopsset)
3120 isfixed = parent->scanopsfixed;
3121 }
3122
3123 if (isfixed && desc != NULL && tts_ops != NULL)
3124 {
3125 op->d.fetch.fixed = true;
3126 op->d.fetch.kind = tts_ops;
3127 op->d.fetch.known_desc = desc;
3128 }
3129 else
3130 {
3131 op->d.fetch.fixed = false;
3132 op->d.fetch.kind = NULL;
3133 op->d.fetch.known_desc = NULL;
3134 }
3135
3136 /* if the slot is known to always virtual we never need to deform */
3137 if (op->d.fetch.fixed && op->d.fetch.kind == &TTSOpsVirtual)
3138 return false;
3139
3140 return true;
3141}
ExprEvalOp
Definition: execExpr.h:67
@ EEOP_NEW_FETCHSOME
Definition: execExpr.h:76
@ EEOP_OLD_FETCHSOME
Definition: execExpr.h:75
@ EEOP_SCAN_FETCHSOME
Definition: execExpr.h:74
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:84
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:495
const TupleTableSlotOps * ExecGetResultSlotOps(PlanState *planstate, bool *isfixed)
Definition: execUtils.c:504
#define outerPlanState(node)
Definition: execnodes.h:1237
#define innerPlanState(node)
Definition: execnodes.h:1236
bool inneropsset
Definition: execnodes.h:1225
bool outeropsset
Definition: execnodes.h:1224
const TupleTableSlotOps * outerops
Definition: execnodes.h:1216
const TupleTableSlotOps * innerops
Definition: execnodes.h:1217
const TupleTableSlotOps * scanops
Definition: execnodes.h:1215
bool outeropsfixed
Definition: execnodes.h:1220
bool scanopsset
Definition: execnodes.h:1223
TupleDesc scandesc
Definition: execnodes.h:1190
bool scanopsfixed
Definition: execnodes.h:1219
bool inneropsfixed
Definition: execnodes.h:1221

References Assert, ExprEvalStep::d, EEOP_INNER_FETCHSOME, EEOP_NEW_FETCHSOME, EEOP_OLD_FETCHSOME, EEOP_OUTER_FETCHSOME, EEOP_SCAN_FETCHSOME, ExecGetResultSlotOps(), ExecGetResultType(), ExprEvalStep::fetch, ExprEvalStep::fixed, PlanState::innerops, PlanState::inneropsfixed, PlanState::inneropsset, innerPlanState, ExprEvalStep::kind, ExprEvalStep::known_desc, ExprEvalStep::opcode, PlanState::outerops, PlanState::outeropsfixed, PlanState::outeropsset, outerPlanState, PlanState::scandesc, PlanState::scanops, PlanState::scanopsfixed, PlanState::scanopsset, and TTSOpsVirtual.

Referenced by ExecBuildGroupingEqual(), ExecBuildHash32FromAttrs(), ExecBuildParamSetEqual(), and ExecPushExprSetupSteps().

◆ ExecCreateExprSetupSteps()

static void ExecCreateExprSetupSteps ( ExprState state,
Node node 
)
static

Definition at line 2865 of file execExpr.c.

2866{
2867 ExprSetupInfo info = {0, 0, 0, 0, 0, NIL};
2868
2869 /* Prescan to find out what we need. */
2870 expr_setup_walker(node, &info);
2871
2872 /* And generate those steps. */
2874}

References ExecPushExprSetupSteps(), expr_setup_walker(), and NIL.

Referenced by ExecBuildHash32Expr(), ExecBuildProjectionInfo(), ExecInitExpr(), ExecInitExprWithParams(), and ExecInitQual().

◆ ExecInitCheck()

ExprState * ExecInitCheck ( List qual,
PlanState parent 
)

Definition at line 315 of file execExpr.c.

316{
317 /* short-circuit (here and in ExecCheck) for empty restriction list */
318 if (qual == NIL)
319 return NULL;
320
321 Assert(IsA(qual, List));
322
323 /*
324 * Just convert the implicit-AND list to an explicit AND (if there's more
325 * than one entry), and compile normally. Unlike ExecQual, we can't
326 * short-circuit on NULL results, so the regular AND behavior is needed.
327 */
328 return ExecInitExpr(make_ands_explicit(qual), parent);
329}
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:143
Expr * make_ands_explicit(List *andclauses)
Definition: makefuncs.c:752

References Assert, ExecInitExpr(), IsA, make_ands_explicit(), and NIL.

Referenced by ExecPrepareCheck().

◆ ExecInitCoerceToDomain()

static void ExecInitCoerceToDomain ( ExprEvalStep scratch,
CoerceToDomain ctest,
ExprState state,
Datum resv,
bool *  resnull 
)
static

Definition at line 3506 of file execExpr.c.

3508{
3509 DomainConstraintRef *constraint_ref;
3510 Datum *domainval = NULL;
3511 bool *domainnull = NULL;
3512 ListCell *l;
3513
3514 scratch->d.domaincheck.resulttype = ctest->resulttype;
3515 /* we'll allocate workspace only if needed */
3516 scratch->d.domaincheck.checkvalue = NULL;
3517 scratch->d.domaincheck.checknull = NULL;
3518 scratch->d.domaincheck.escontext = state->escontext;
3519
3520 /*
3521 * Evaluate argument - it's fine to directly store it into resv/resnull,
3522 * if there's constraint failures there'll be errors, otherwise it's what
3523 * needs to be returned.
3524 */
3525 ExecInitExprRec(ctest->arg, state, resv, resnull);
3526
3527 /*
3528 * Note: if the argument is of varlena type, it could be a R/W expanded
3529 * object. We want to return the R/W pointer as the final result, but we
3530 * have to pass a R/O pointer as the value to be tested by any functions
3531 * in check expressions. We don't bother to emit a MAKE_READONLY step
3532 * unless there's actually at least one check expression, though. Until
3533 * we've tested that, domainval/domainnull are NULL.
3534 */
3535
3536 /*
3537 * Collect the constraints associated with the domain.
3538 *
3539 * Note: before PG v10 we'd recheck the set of constraints during each
3540 * evaluation of the expression. Now we bake them into the ExprState
3541 * during executor initialization. That means we don't need typcache.c to
3542 * provide compiled exprs.
3543 */
3544 constraint_ref = (DomainConstraintRef *)
3545 palloc(sizeof(DomainConstraintRef));
3547 constraint_ref,
3549 false);
3550
3551 /*
3552 * Compile code to check each domain constraint. NOTNULL constraints can
3553 * just be applied on the resv/resnull value, but for CHECK constraints we
3554 * need more pushups.
3555 */
3556 foreach(l, constraint_ref->constraints)
3557 {
3559 Datum *save_innermost_domainval;
3560 bool *save_innermost_domainnull;
3561
3562 scratch->d.domaincheck.constraintname = con->name;
3563
3564 switch (con->constrainttype)
3565 {
3567 scratch->opcode = EEOP_DOMAIN_NOTNULL;
3568 ExprEvalPushStep(state, scratch);
3569 break;
3571 /* Allocate workspace for CHECK output if we didn't yet */
3572 if (scratch->d.domaincheck.checkvalue == NULL)
3573 {
3574 scratch->d.domaincheck.checkvalue =
3575 (Datum *) palloc(sizeof(Datum));
3576 scratch->d.domaincheck.checknull =
3577 (bool *) palloc(sizeof(bool));
3578 }
3579
3580 /*
3581 * If first time through, determine where CoerceToDomainValue
3582 * nodes should read from.
3583 */
3584 if (domainval == NULL)
3585 {
3586 /*
3587 * Since value might be read multiple times, force to R/O
3588 * - but only if it could be an expanded datum.
3589 */
3590 if (get_typlen(ctest->resulttype) == -1)
3591 {
3592 ExprEvalStep scratch2 = {0};
3593
3594 /* Yes, so make output workspace for MAKE_READONLY */
3595 domainval = (Datum *) palloc(sizeof(Datum));
3596 domainnull = (bool *) palloc(sizeof(bool));
3597
3598 /* Emit MAKE_READONLY */
3599 scratch2.opcode = EEOP_MAKE_READONLY;
3600 scratch2.resvalue = domainval;
3601 scratch2.resnull = domainnull;
3602 scratch2.d.make_readonly.value = resv;
3603 scratch2.d.make_readonly.isnull = resnull;
3604 ExprEvalPushStep(state, &scratch2);
3605 }
3606 else
3607 {
3608 /* No, so it's fine to read from resv/resnull */
3609 domainval = resv;
3610 domainnull = resnull;
3611 }
3612 }
3613
3614 /*
3615 * Set up value to be returned by CoerceToDomainValue nodes.
3616 * We must save and restore innermost_domainval/null fields,
3617 * in case this node is itself within a check expression for
3618 * another domain.
3619 */
3620 save_innermost_domainval = state->innermost_domainval;
3621 save_innermost_domainnull = state->innermost_domainnull;
3622 state->innermost_domainval = domainval;
3623 state->innermost_domainnull = domainnull;
3624
3625 /* evaluate check expression value */
3627 scratch->d.domaincheck.checkvalue,
3628 scratch->d.domaincheck.checknull);
3629
3630 state->innermost_domainval = save_innermost_domainval;
3631 state->innermost_domainnull = save_innermost_domainnull;
3632
3633 /* now test result */
3634 scratch->opcode = EEOP_DOMAIN_CHECK;
3635 ExprEvalPushStep(state, scratch);
3636
3637 break;
3638 default:
3639 elog(ERROR, "unrecognized constraint type: %d",
3640 (int) con->constrainttype);
3641 break;
3642 }
3643 }
3644}
@ EEOP_DOMAIN_CHECK
Definition: execExpr.h:245
@ EEOP_DOMAIN_NOTNULL
Definition: execExpr.h:242
@ EEOP_MAKE_READONLY
Definition: execExpr.h:178
@ DOM_CONSTRAINT_CHECK
Definition: execnodes.h:1026
@ DOM_CONSTRAINT_NOTNULL
Definition: execnodes.h:1025
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
DomainConstraintType constrainttype
Definition: execnodes.h:1032
struct ExprEvalStep::@55::@71 make_readonly
bool * checknull
Definition: execExpr.h:580
struct ExprEvalStep::@55::@85 domaincheck
Oid resulttype
Definition: execExpr.h:535
Datum * checkvalue
Definition: execExpr.h:579
ErrorSaveContext * escontext
Definition: execExpr.h:583
char * constraintname
Definition: execExpr.h:577
void InitDomainConstraintRef(Oid type_id, DomainConstraintRef *ref, MemoryContext refctx, bool need_exprstate)
Definition: typcache.c:1400

References CoerceToDomain::arg, DomainConstraintState::check_expr, ExprEvalStep::checknull, ExprEvalStep::checkvalue, ExprEvalStep::constraintname, DomainConstraintRef::constraints, DomainConstraintState::constrainttype, CurrentMemoryContext, ExprEvalStep::d, DOM_CONSTRAINT_CHECK, DOM_CONSTRAINT_NOTNULL, ExprEvalStep::domaincheck, EEOP_DOMAIN_CHECK, EEOP_DOMAIN_NOTNULL, EEOP_MAKE_READONLY, elog, ERROR, ExprEvalStep::escontext, ExecInitExprRec(), ExprEvalPushStep(), get_typlen(), InitDomainConstraintRef(), ExprEvalStep::isnull, lfirst, ExprEvalStep::make_readonly, DomainConstraintState::name, ExprEvalStep::opcode, palloc(), ExprEvalStep::resnull, ExprEvalStep::resulttype, CoerceToDomain::resulttype, ExprEvalStep::resvalue, and ExprEvalStep::value.

Referenced by ExecInitExprRec().

◆ ExecInitExpr()

ExprState * ExecInitExpr ( Expr node,
PlanState parent 
)

Definition at line 143 of file execExpr.c.

144{
146 ExprEvalStep scratch = {0};
147
148 /* Special case: NULL expression produces a NULL ExprState pointer */
149 if (node == NULL)
150 return NULL;
151
152 /* Initialize ExprState with empty step list */
154 state->expr = node;
155 state->parent = parent;
156 state->ext_params = NULL;
157
158 /* Insert setup steps as needed */
160
161 /* Compile the expression proper */
162 ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
163
164 /* Finally, append a DONE step */
165 scratch.opcode = EEOP_DONE;
166 ExprEvalPushStep(state, &scratch);
167
169
170 return state;
171}

References EEOP_DONE, ExecCreateExprSetupSteps(), ExecInitExprRec(), ExecReadyExpr(), ExprEvalPushStep(), makeNode, and ExprEvalStep::opcode.

Referenced by ATRewriteTable(), BeginCopyFrom(), evaluate_expr(), ExecIndexBuildScanKeys(), ExecInitCheck(), ExecInitExprList(), ExecInitExprRec(), ExecInitLimit(), ExecInitMemoize(), ExecInitProjectSet(), ExecInitSampleScan(), ExecInitSubPlan(), ExecInitTableFuncScan(), ExecInitTableFunctionResult(), ExecInitWindowAgg(), ExecPrepareExpr(), get_cast_hashentry(), get_qual_for_range(), InitPartitionPruneContext(), MakeTidOpExpr(), MJExamineQuals(), operator_predicate_proof(), prep_domain_constraints(), slot_fill_defaults(), and TidExprListCreate().

◆ ExecInitExprList()

List * ExecInitExprList ( List nodes,
PlanState parent 
)

Definition at line 335 of file execExpr.c.

336{
337 List *result = NIL;
338 ListCell *lc;
339
340 foreach(lc, nodes)
341 {
342 Expr *e = lfirst(lc);
343
344 result = lappend(result, ExecInitExpr(e, parent));
345 }
346
347 return result;
348}
List * lappend(List *list, void *datum)
Definition: list.c:339
e
Definition: preproc-init.c:82

References ExecInitExpr(), lappend(), lfirst, and NIL.

Referenced by ExecInitAgg(), ExecInitExprRec(), ExecInitFunctionResultSet(), ExecInitIndexScan(), ExecInitSampleScan(), ExecInitTableFuncScan(), ExecInitTableFunctionResult(), ExecInitValuesScan(), prepare_query_params(), and ValuesNext().

◆ ExecInitExprRec()

static void ExecInitExprRec ( Expr node,
ExprState state,
Datum resv,
bool *  resnull 
)
static

Definition at line 919 of file execExpr.c.

921{
922 ExprEvalStep scratch = {0};
923
924 /* Guard against stack overflow due to overly complex expressions */
926
927 /* Step's output location is always what the caller gave us */
928 Assert(resv != NULL && resnull != NULL);
929 scratch.resvalue = resv;
930 scratch.resnull = resnull;
931
932 /* cases should be ordered as they are in enum NodeTag */
933 switch (nodeTag(node))
934 {
935 case T_Var:
936 {
937 Var *variable = (Var *) node;
938
939 if (variable->varattno == InvalidAttrNumber)
940 {
941 /* whole-row Var */
943 }
944 else if (variable->varattno <= 0)
945 {
946 /* system column */
947 scratch.d.var.attnum = variable->varattno;
948 scratch.d.var.vartype = variable->vartype;
949 scratch.d.var.varreturningtype = variable->varreturningtype;
950 switch (variable->varno)
951 {
952 case INNER_VAR:
953 scratch.opcode = EEOP_INNER_SYSVAR;
954 break;
955 case OUTER_VAR:
956 scratch.opcode = EEOP_OUTER_SYSVAR;
957 break;
958
959 /* INDEX_VAR is handled by default case */
960
961 default:
962 switch (variable->varreturningtype)
963 {
965 scratch.opcode = EEOP_SCAN_SYSVAR;
966 break;
968 scratch.opcode = EEOP_OLD_SYSVAR;
969 state->flags |= EEO_FLAG_HAS_OLD;
970 break;
972 scratch.opcode = EEOP_NEW_SYSVAR;
973 state->flags |= EEO_FLAG_HAS_NEW;
974 break;
975 }
976 break;
977 }
978 }
979 else
980 {
981 /* regular user column */
982 scratch.d.var.attnum = variable->varattno - 1;
983 scratch.d.var.vartype = variable->vartype;
984 scratch.d.var.varreturningtype = variable->varreturningtype;
985 switch (variable->varno)
986 {
987 case INNER_VAR:
988 scratch.opcode = EEOP_INNER_VAR;
989 break;
990 case OUTER_VAR:
991 scratch.opcode = EEOP_OUTER_VAR;
992 break;
993
994 /* INDEX_VAR is handled by default case */
995
996 default:
997 switch (variable->varreturningtype)
998 {
1000 scratch.opcode = EEOP_SCAN_VAR;
1001 break;
1002 case VAR_RETURNING_OLD:
1003 scratch.opcode = EEOP_OLD_VAR;
1004 state->flags |= EEO_FLAG_HAS_OLD;
1005 break;
1006 case VAR_RETURNING_NEW:
1007 scratch.opcode = EEOP_NEW_VAR;
1008 state->flags |= EEO_FLAG_HAS_NEW;
1009 break;
1010 }
1011 break;
1012 }
1013 }
1014
1015 ExprEvalPushStep(state, &scratch);
1016 break;
1017 }
1018
1019 case T_Const:
1020 {
1021 Const *con = (Const *) node;
1022
1023 scratch.opcode = EEOP_CONST;
1024 scratch.d.constval.value = con->constvalue;
1025 scratch.d.constval.isnull = con->constisnull;
1026
1027 ExprEvalPushStep(state, &scratch);
1028 break;
1029 }
1030
1031 case T_Param:
1032 {
1033 Param *param = (Param *) node;
1034 ParamListInfo params;
1035
1036 switch (param->paramkind)
1037 {
1038 case PARAM_EXEC:
1039 scratch.opcode = EEOP_PARAM_EXEC;
1040 scratch.d.param.paramid = param->paramid;
1041 scratch.d.param.paramtype = param->paramtype;
1042 ExprEvalPushStep(state, &scratch);
1043 break;
1044 case PARAM_EXTERN:
1045
1046 /*
1047 * If we have a relevant ParamCompileHook, use it;
1048 * otherwise compile a standard EEOP_PARAM_EXTERN
1049 * step. ext_params, if supplied, takes precedence
1050 * over info from the parent node's EState (if any).
1051 */
1052 if (state->ext_params)
1053 params = state->ext_params;
1054 else if (state->parent &&
1055 state->parent->state)
1056 params = state->parent->state->es_param_list_info;
1057 else
1058 params = NULL;
1059 if (params && params->paramCompile)
1060 {
1061 params->paramCompile(params, param, state,
1062 resv, resnull);
1063 }
1064 else
1065 {
1066 scratch.opcode = EEOP_PARAM_EXTERN;
1067 scratch.d.param.paramid = param->paramid;
1068 scratch.d.param.paramtype = param->paramtype;
1069 ExprEvalPushStep(state, &scratch);
1070 }
1071 break;
1072 default:
1073 elog(ERROR, "unrecognized paramkind: %d",
1074 (int) param->paramkind);
1075 break;
1076 }
1077 break;
1078 }
1079
1080 case T_Aggref:
1081 {
1082 Aggref *aggref = (Aggref *) node;
1083
1084 scratch.opcode = EEOP_AGGREF;
1085 scratch.d.aggref.aggno = aggref->aggno;
1086
1087 if (state->parent && IsA(state->parent, AggState))
1088 {
1089 AggState *aggstate = (AggState *) state->parent;
1090
1091 aggstate->aggs = lappend(aggstate->aggs, aggref);
1092 }
1093 else
1094 {
1095 /* planner messed up */
1096 elog(ERROR, "Aggref found in non-Agg plan node");
1097 }
1098
1099 ExprEvalPushStep(state, &scratch);
1100 break;
1101 }
1102
1103 case T_GroupingFunc:
1104 {
1105 GroupingFunc *grp_node = (GroupingFunc *) node;
1106 Agg *agg;
1107
1108 if (!state->parent || !IsA(state->parent, AggState) ||
1109 !IsA(state->parent->plan, Agg))
1110 elog(ERROR, "GroupingFunc found in non-Agg plan node");
1111
1112 scratch.opcode = EEOP_GROUPING_FUNC;
1113
1114 agg = (Agg *) (state->parent->plan);
1115
1116 if (agg->groupingSets)
1117 scratch.d.grouping_func.clauses = grp_node->cols;
1118 else
1119 scratch.d.grouping_func.clauses = NIL;
1120
1121 ExprEvalPushStep(state, &scratch);
1122 break;
1123 }
1124
1125 case T_WindowFunc:
1126 {
1127 WindowFunc *wfunc = (WindowFunc *) node;
1129
1130 wfstate->wfunc = wfunc;
1131
1132 if (state->parent && IsA(state->parent, WindowAggState))
1133 {
1134 WindowAggState *winstate = (WindowAggState *) state->parent;
1135 int nfuncs;
1136
1137 winstate->funcs = lappend(winstate->funcs, wfstate);
1138 nfuncs = ++winstate->numfuncs;
1139 if (wfunc->winagg)
1140 winstate->numaggs++;
1141
1142 /* for now initialize agg using old style expressions */
1143 wfstate->args = ExecInitExprList(wfunc->args,
1144 state->parent);
1145 wfstate->aggfilter = ExecInitExpr(wfunc->aggfilter,
1146 state->parent);
1147
1148 /*
1149 * Complain if the windowfunc's arguments contain any
1150 * windowfuncs; nested window functions are semantically
1151 * nonsensical. (This should have been caught earlier,
1152 * but we defend against it here anyway.)
1153 */
1154 if (nfuncs != winstate->numfuncs)
1155 ereport(ERROR,
1156 (errcode(ERRCODE_WINDOWING_ERROR),
1157 errmsg("window function calls cannot be nested")));
1158 }
1159 else
1160 {
1161 /* planner messed up */
1162 elog(ERROR, "WindowFunc found in non-WindowAgg plan node");
1163 }
1164
1165 scratch.opcode = EEOP_WINDOW_FUNC;
1166 scratch.d.window_func.wfstate = wfstate;
1167 ExprEvalPushStep(state, &scratch);
1168 break;
1169 }
1170
1171 case T_MergeSupportFunc:
1172 {
1173 /* must be in a MERGE, else something messed up */
1174 if (!state->parent ||
1175 !IsA(state->parent, ModifyTableState) ||
1176 ((ModifyTableState *) state->parent)->operation != CMD_MERGE)
1177 elog(ERROR, "MergeSupportFunc found in non-merge plan node");
1178
1180 ExprEvalPushStep(state, &scratch);
1181 break;
1182 }
1183
1184 case T_SubscriptingRef:
1185 {
1186 SubscriptingRef *sbsref = (SubscriptingRef *) node;
1187
1188 ExecInitSubscriptingRef(&scratch, sbsref, state, resv, resnull);
1189 break;
1190 }
1191
1192 case T_FuncExpr:
1193 {
1194 FuncExpr *func = (FuncExpr *) node;
1195
1196 ExecInitFunc(&scratch, node,
1197 func->args, func->funcid, func->inputcollid,
1198 state);
1199 ExprEvalPushStep(state, &scratch);
1200 break;
1201 }
1202
1203 case T_OpExpr:
1204 {
1205 OpExpr *op = (OpExpr *) node;
1206
1207 ExecInitFunc(&scratch, node,
1208 op->args, op->opfuncid, op->inputcollid,
1209 state);
1210 ExprEvalPushStep(state, &scratch);
1211 break;
1212 }
1213
1214 case T_DistinctExpr:
1215 {
1216 DistinctExpr *op = (DistinctExpr *) node;
1217
1218 ExecInitFunc(&scratch, node,
1219 op->args, op->opfuncid, op->inputcollid,
1220 state);
1221
1222 /*
1223 * Change opcode of call instruction to EEOP_DISTINCT.
1224 *
1225 * XXX: historically we've not called the function usage
1226 * pgstat infrastructure - that seems inconsistent given that
1227 * we do so for normal function *and* operator evaluation. If
1228 * we decided to do that here, we'd probably want separate
1229 * opcodes for FUSAGE or not.
1230 */
1231 scratch.opcode = EEOP_DISTINCT;
1232 ExprEvalPushStep(state, &scratch);
1233 break;
1234 }
1235
1236 case T_NullIfExpr:
1237 {
1238 NullIfExpr *op = (NullIfExpr *) node;
1239
1240 ExecInitFunc(&scratch, node,
1241 op->args, op->opfuncid, op->inputcollid,
1242 state);
1243
1244 /*
1245 * If first argument is of varlena type, we'll need to ensure
1246 * that the value passed to the comparison function is a
1247 * read-only pointer.
1248 */
1249 scratch.d.func.make_ro =
1250 (get_typlen(exprType((Node *) linitial(op->args))) == -1);
1251
1252 /*
1253 * Change opcode of call instruction to EEOP_NULLIF.
1254 *
1255 * XXX: historically we've not called the function usage
1256 * pgstat infrastructure - that seems inconsistent given that
1257 * we do so for normal function *and* operator evaluation. If
1258 * we decided to do that here, we'd probably want separate
1259 * opcodes for FUSAGE or not.
1260 */
1261 scratch.opcode = EEOP_NULLIF;
1262 ExprEvalPushStep(state, &scratch);
1263 break;
1264 }
1265
1266 case T_ScalarArrayOpExpr:
1267 {
1268 ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
1269 Expr *scalararg;
1270 Expr *arrayarg;
1271 FmgrInfo *finfo;
1272 FunctionCallInfo fcinfo;
1273 AclResult aclresult;
1274 Oid cmpfuncid;
1275
1276 /*
1277 * Select the correct comparison function. When we do hashed
1278 * NOT IN clauses, the opfuncid will be the inequality
1279 * comparison function and negfuncid will be set to equality.
1280 * We need to use the equality function for hash probes.
1281 */
1282 if (OidIsValid(opexpr->negfuncid))
1283 {
1284 Assert(OidIsValid(opexpr->hashfuncid));
1285 cmpfuncid = opexpr->negfuncid;
1286 }
1287 else
1288 cmpfuncid = opexpr->opfuncid;
1289
1290 Assert(list_length(opexpr->args) == 2);
1291 scalararg = (Expr *) linitial(opexpr->args);
1292 arrayarg = (Expr *) lsecond(opexpr->args);
1293
1294 /* Check permission to call function */
1295 aclresult = object_aclcheck(ProcedureRelationId, cmpfuncid,
1296 GetUserId(),
1297 ACL_EXECUTE);
1298 if (aclresult != ACLCHECK_OK)
1300 get_func_name(cmpfuncid));
1301 InvokeFunctionExecuteHook(cmpfuncid);
1302
1303 if (OidIsValid(opexpr->hashfuncid))
1304 {
1305 aclresult = object_aclcheck(ProcedureRelationId, opexpr->hashfuncid,
1306 GetUserId(),
1307 ACL_EXECUTE);
1308 if (aclresult != ACLCHECK_OK)
1310 get_func_name(opexpr->hashfuncid));
1311 InvokeFunctionExecuteHook(opexpr->hashfuncid);
1312 }
1313
1314 /* Set up the primary fmgr lookup information */
1315 finfo = palloc0(sizeof(FmgrInfo));
1316 fcinfo = palloc0(SizeForFunctionCallInfo(2));
1317 fmgr_info(cmpfuncid, finfo);
1318 fmgr_info_set_expr((Node *) node, finfo);
1319 InitFunctionCallInfoData(*fcinfo, finfo, 2,
1320 opexpr->inputcollid, NULL, NULL);
1321
1322 /*
1323 * If hashfuncid is set, we create a EEOP_HASHED_SCALARARRAYOP
1324 * step instead of a EEOP_SCALARARRAYOP. This provides much
1325 * faster lookup performance than the normal linear search
1326 * when the number of items in the array is anything but very
1327 * small.
1328 */
1329 if (OidIsValid(opexpr->hashfuncid))
1330 {
1331 /* Evaluate scalar directly into left function argument */
1332 ExecInitExprRec(scalararg, state,
1333 &fcinfo->args[0].value, &fcinfo->args[0].isnull);
1334
1335 /*
1336 * Evaluate array argument into our return value. There's
1337 * no danger in that, because the return value is
1338 * guaranteed to be overwritten by
1339 * EEOP_HASHED_SCALARARRAYOP, and will not be passed to
1340 * any other expression.
1341 */
1342 ExecInitExprRec(arrayarg, state, resv, resnull);
1343
1344 /* And perform the operation */
1346 scratch.d.hashedscalararrayop.inclause = opexpr->useOr;
1347 scratch.d.hashedscalararrayop.finfo = finfo;
1348 scratch.d.hashedscalararrayop.fcinfo_data = fcinfo;
1349 scratch.d.hashedscalararrayop.saop = opexpr;
1350
1351
1352 ExprEvalPushStep(state, &scratch);
1353 }
1354 else
1355 {
1356 /* Evaluate scalar directly into left function argument */
1357 ExecInitExprRec(scalararg, state,
1358 &fcinfo->args[0].value,
1359 &fcinfo->args[0].isnull);
1360
1361 /*
1362 * Evaluate array argument into our return value. There's
1363 * no danger in that, because the return value is
1364 * guaranteed to be overwritten by EEOP_SCALARARRAYOP, and
1365 * will not be passed to any other expression.
1366 */
1367 ExecInitExprRec(arrayarg, state, resv, resnull);
1368
1369 /* And perform the operation */
1370 scratch.opcode = EEOP_SCALARARRAYOP;
1372 scratch.d.scalararrayop.useOr = opexpr->useOr;
1373 scratch.d.scalararrayop.finfo = finfo;
1374 scratch.d.scalararrayop.fcinfo_data = fcinfo;
1375 scratch.d.scalararrayop.fn_addr = finfo->fn_addr;
1376 ExprEvalPushStep(state, &scratch);
1377 }
1378 break;
1379 }
1380
1381 case T_BoolExpr:
1382 {
1383 BoolExpr *boolexpr = (BoolExpr *) node;
1384 int nargs = list_length(boolexpr->args);
1385 List *adjust_jumps = NIL;
1386 int off;
1387 ListCell *lc;
1388
1389 /* allocate scratch memory used by all steps of AND/OR */
1390 if (boolexpr->boolop != NOT_EXPR)
1391 scratch.d.boolexpr.anynull = (bool *) palloc(sizeof(bool));
1392
1393 /*
1394 * For each argument evaluate the argument itself, then
1395 * perform the bool operation's appropriate handling.
1396 *
1397 * We can evaluate each argument into our result area, since
1398 * the short-circuiting logic means we only need to remember
1399 * previous NULL values.
1400 *
1401 * AND/OR is split into separate STEP_FIRST (one) / STEP (zero
1402 * or more) / STEP_LAST (one) steps, as each of those has to
1403 * perform different work. The FIRST/LAST split is valid
1404 * because AND/OR have at least two arguments.
1405 */
1406 off = 0;
1407 foreach(lc, boolexpr->args)
1408 {
1409 Expr *arg = (Expr *) lfirst(lc);
1410
1411 /* Evaluate argument into our output variable */
1412 ExecInitExprRec(arg, state, resv, resnull);
1413
1414 /* Perform the appropriate step type */
1415 switch (boolexpr->boolop)
1416 {
1417 case AND_EXPR:
1418 Assert(nargs >= 2);
1419
1420 if (off == 0)
1422 else if (off + 1 == nargs)
1424 else
1425 scratch.opcode = EEOP_BOOL_AND_STEP;
1426 break;
1427 case OR_EXPR:
1428 Assert(nargs >= 2);
1429
1430 if (off == 0)
1432 else if (off + 1 == nargs)
1434 else
1435 scratch.opcode = EEOP_BOOL_OR_STEP;
1436 break;
1437 case NOT_EXPR:
1438 Assert(nargs == 1);
1439
1440 scratch.opcode = EEOP_BOOL_NOT_STEP;
1441 break;
1442 default:
1443 elog(ERROR, "unrecognized boolop: %d",
1444 (int) boolexpr->boolop);
1445 break;
1446 }
1447
1448 scratch.d.boolexpr.jumpdone = -1;
1449 ExprEvalPushStep(state, &scratch);
1450 adjust_jumps = lappend_int(adjust_jumps,
1451 state->steps_len - 1);
1452 off++;
1453 }
1454
1455 /* adjust jump targets */
1456 foreach(lc, adjust_jumps)
1457 {
1458 ExprEvalStep *as = &state->steps[lfirst_int(lc)];
1459
1460 Assert(as->d.boolexpr.jumpdone == -1);
1461 as->d.boolexpr.jumpdone = state->steps_len;
1462 }
1463
1464 break;
1465 }
1466
1467 case T_SubPlan:
1468 {
1469 SubPlan *subplan = (SubPlan *) node;
1470
1471 /*
1472 * Real execution of a MULTIEXPR SubPlan has already been
1473 * done. What we have to do here is return a dummy NULL record
1474 * value in case this targetlist element is assigned
1475 * someplace.
1476 */
1477 if (subplan->subLinkType == MULTIEXPR_SUBLINK)
1478 {
1479 scratch.opcode = EEOP_CONST;
1480 scratch.d.constval.value = (Datum) 0;
1481 scratch.d.constval.isnull = true;
1482 ExprEvalPushStep(state, &scratch);
1483 break;
1484 }
1485
1486 ExecInitSubPlanExpr(subplan, state, resv, resnull);
1487 break;
1488 }
1489
1490 case T_FieldSelect:
1491 {
1492 FieldSelect *fselect = (FieldSelect *) node;
1493
1494 /* evaluate row/record argument into result area */
1495 ExecInitExprRec(fselect->arg, state, resv, resnull);
1496
1497 /* and extract field */
1498 scratch.opcode = EEOP_FIELDSELECT;
1499 scratch.d.fieldselect.fieldnum = fselect->fieldnum;
1500 scratch.d.fieldselect.resulttype = fselect->resulttype;
1501 scratch.d.fieldselect.rowcache.cacheptr = NULL;
1502
1503 ExprEvalPushStep(state, &scratch);
1504 break;
1505 }
1506
1507 case T_FieldStore:
1508 {
1509 FieldStore *fstore = (FieldStore *) node;
1510 TupleDesc tupDesc;
1511 ExprEvalRowtypeCache *rowcachep;
1512 Datum *values;
1513 bool *nulls;
1514 int ncolumns;
1515 ListCell *l1,
1516 *l2;
1517
1518 /* find out the number of columns in the composite type */
1519 tupDesc = lookup_rowtype_tupdesc(fstore->resulttype, -1);
1520 ncolumns = tupDesc->natts;
1521 ReleaseTupleDesc(tupDesc);
1522
1523 /* create workspace for column values */
1524 values = (Datum *) palloc(sizeof(Datum) * ncolumns);
1525 nulls = (bool *) palloc(sizeof(bool) * ncolumns);
1526
1527 /* create shared composite-type-lookup cache struct */
1528 rowcachep = palloc(sizeof(ExprEvalRowtypeCache));
1529 rowcachep->cacheptr = NULL;
1530
1531 /* emit code to evaluate the composite input value */
1532 ExecInitExprRec(fstore->arg, state, resv, resnull);
1533
1534 /* next, deform the input tuple into our workspace */
1536 scratch.d.fieldstore.fstore = fstore;
1537 scratch.d.fieldstore.rowcache = rowcachep;
1538 scratch.d.fieldstore.values = values;
1539 scratch.d.fieldstore.nulls = nulls;
1540 scratch.d.fieldstore.ncolumns = ncolumns;
1541 ExprEvalPushStep(state, &scratch);
1542
1543 /* evaluate new field values, store in workspace columns */
1544 forboth(l1, fstore->newvals, l2, fstore->fieldnums)
1545 {
1546 Expr *e = (Expr *) lfirst(l1);
1547 AttrNumber fieldnum = lfirst_int(l2);
1548 Datum *save_innermost_caseval;
1549 bool *save_innermost_casenull;
1550
1551 if (fieldnum <= 0 || fieldnum > ncolumns)
1552 elog(ERROR, "field number %d is out of range in FieldStore",
1553 fieldnum);
1554
1555 /*
1556 * Use the CaseTestExpr mechanism to pass down the old
1557 * value of the field being replaced; this is needed in
1558 * case the newval is itself a FieldStore or
1559 * SubscriptingRef that has to obtain and modify the old
1560 * value. It's safe to reuse the CASE mechanism because
1561 * there cannot be a CASE between here and where the value
1562 * would be needed, and a field assignment can't be within
1563 * a CASE either. (So saving and restoring
1564 * innermost_caseval is just paranoia, but let's do it
1565 * anyway.)
1566 *
1567 * Another non-obvious point is that it's safe to use the
1568 * field's values[]/nulls[] entries as both the caseval
1569 * source and the result address for this subexpression.
1570 * That's okay only because (1) both FieldStore and
1571 * SubscriptingRef evaluate their arg or refexpr inputs
1572 * first, and (2) any such CaseTestExpr is directly the
1573 * arg or refexpr input. So any read of the caseval will
1574 * occur before there's a chance to overwrite it. Also,
1575 * if multiple entries in the newvals/fieldnums lists
1576 * target the same field, they'll effectively be applied
1577 * left-to-right which is what we want.
1578 */
1579 save_innermost_caseval = state->innermost_caseval;
1580 save_innermost_casenull = state->innermost_casenull;
1581 state->innermost_caseval = &values[fieldnum - 1];
1582 state->innermost_casenull = &nulls[fieldnum - 1];
1583
1585 &values[fieldnum - 1],
1586 &nulls[fieldnum - 1]);
1587
1588 state->innermost_caseval = save_innermost_caseval;
1589 state->innermost_casenull = save_innermost_casenull;
1590 }
1591
1592 /* finally, form result tuple */
1593 scratch.opcode = EEOP_FIELDSTORE_FORM;
1594 scratch.d.fieldstore.fstore = fstore;
1595 scratch.d.fieldstore.rowcache = rowcachep;
1596 scratch.d.fieldstore.values = values;
1597 scratch.d.fieldstore.nulls = nulls;
1598 scratch.d.fieldstore.ncolumns = ncolumns;
1599 ExprEvalPushStep(state, &scratch);
1600 break;
1601 }
1602
1603 case T_RelabelType:
1604 {
1605 /* relabel doesn't need to do anything at runtime */
1606 RelabelType *relabel = (RelabelType *) node;
1607
1608 ExecInitExprRec(relabel->arg, state, resv, resnull);
1609 break;
1610 }
1611
1612 case T_CoerceViaIO:
1613 {
1614 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
1615 Oid iofunc;
1616 bool typisvarlena;
1617 Oid typioparam;
1618 FunctionCallInfo fcinfo_in;
1619
1620 /* evaluate argument into step's result area */
1621 ExecInitExprRec(iocoerce->arg, state, resv, resnull);
1622
1623 /*
1624 * Prepare both output and input function calls, to be
1625 * evaluated inside a single evaluation step for speed - this
1626 * can be a very common operation.
1627 *
1628 * We don't check permissions here as a type's input/output
1629 * function are assumed to be executable by everyone.
1630 */
1631 if (state->escontext == NULL)
1632 scratch.opcode = EEOP_IOCOERCE;
1633 else
1634 scratch.opcode = EEOP_IOCOERCE_SAFE;
1635
1636 /* lookup the source type's output function */
1637 scratch.d.iocoerce.finfo_out = palloc0(sizeof(FmgrInfo));
1639
1640 getTypeOutputInfo(exprType((Node *) iocoerce->arg),
1641 &iofunc, &typisvarlena);
1642 fmgr_info(iofunc, scratch.d.iocoerce.finfo_out);
1643 fmgr_info_set_expr((Node *) node, scratch.d.iocoerce.finfo_out);
1645 scratch.d.iocoerce.finfo_out,
1646 1, InvalidOid, NULL, NULL);
1647
1648 /* lookup the result type's input function */
1649 scratch.d.iocoerce.finfo_in = palloc0(sizeof(FmgrInfo));
1651
1652 getTypeInputInfo(iocoerce->resulttype,
1653 &iofunc, &typioparam);
1654 fmgr_info(iofunc, scratch.d.iocoerce.finfo_in);
1655 fmgr_info_set_expr((Node *) node, scratch.d.iocoerce.finfo_in);
1657 scratch.d.iocoerce.finfo_in,
1658 3, InvalidOid, NULL, NULL);
1659
1660 /*
1661 * We can preload the second and third arguments for the input
1662 * function, since they're constants.
1663 */
1664 fcinfo_in = scratch.d.iocoerce.fcinfo_data_in;
1665 fcinfo_in->args[1].value = ObjectIdGetDatum(typioparam);
1666 fcinfo_in->args[1].isnull = false;
1667 fcinfo_in->args[2].value = Int32GetDatum(-1);
1668 fcinfo_in->args[2].isnull = false;
1669
1670 fcinfo_in->context = (Node *) state->escontext;
1671
1672 ExprEvalPushStep(state, &scratch);
1673 break;
1674 }
1675
1676 case T_ArrayCoerceExpr:
1677 {
1678 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
1679 Oid resultelemtype;
1680 ExprState *elemstate;
1681
1682 /* evaluate argument into step's result area */
1683 ExecInitExprRec(acoerce->arg, state, resv, resnull);
1684
1685 resultelemtype = get_element_type(acoerce->resulttype);
1686 if (!OidIsValid(resultelemtype))
1687 ereport(ERROR,
1688 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1689 errmsg("target type is not an array")));
1690
1691 /*
1692 * Construct a sub-expression for the per-element expression;
1693 * but don't ready it until after we check it for triviality.
1694 * We assume it hasn't any Var references, but does have a
1695 * CaseTestExpr representing the source array element values.
1696 */
1697 elemstate = makeNode(ExprState);
1698 elemstate->expr = acoerce->elemexpr;
1699 elemstate->parent = state->parent;
1700 elemstate->ext_params = state->ext_params;
1701
1702 elemstate->innermost_caseval = (Datum *) palloc(sizeof(Datum));
1703 elemstate->innermost_casenull = (bool *) palloc(sizeof(bool));
1704
1705 ExecInitExprRec(acoerce->elemexpr, elemstate,
1706 &elemstate->resvalue, &elemstate->resnull);
1707
1708 if (elemstate->steps_len == 1 &&
1709 elemstate->steps[0].opcode == EEOP_CASE_TESTVAL)
1710 {
1711 /* Trivial, so we need no per-element work at runtime */
1712 elemstate = NULL;
1713 }
1714 else
1715 {
1716 /* Not trivial, so append a DONE step */
1717 scratch.opcode = EEOP_DONE;
1718 ExprEvalPushStep(elemstate, &scratch);
1719 /* and ready the subexpression */
1720 ExecReadyExpr(elemstate);
1721 }
1722
1723 scratch.opcode = EEOP_ARRAYCOERCE;
1724 scratch.d.arraycoerce.elemexprstate = elemstate;
1725 scratch.d.arraycoerce.resultelemtype = resultelemtype;
1726
1727 if (elemstate)
1728 {
1729 /* Set up workspace for array_map */
1730 scratch.d.arraycoerce.amstate =
1731 (ArrayMapState *) palloc0(sizeof(ArrayMapState));
1732 }
1733 else
1734 {
1735 /* Don't need workspace if there's no subexpression */
1736 scratch.d.arraycoerce.amstate = NULL;
1737 }
1738
1739 ExprEvalPushStep(state, &scratch);
1740 break;
1741 }
1742
1743 case T_ConvertRowtypeExpr:
1744 {
1746 ExprEvalRowtypeCache *rowcachep;
1747
1748 /* cache structs must be out-of-line for space reasons */
1749 rowcachep = palloc(2 * sizeof(ExprEvalRowtypeCache));
1750 rowcachep[0].cacheptr = NULL;
1751 rowcachep[1].cacheptr = NULL;
1752
1753 /* evaluate argument into step's result area */
1754 ExecInitExprRec(convert->arg, state, resv, resnull);
1755
1756 /* and push conversion step */
1757 scratch.opcode = EEOP_CONVERT_ROWTYPE;
1758 scratch.d.convert_rowtype.inputtype =
1759 exprType((Node *) convert->arg);
1760 scratch.d.convert_rowtype.outputtype = convert->resulttype;
1761 scratch.d.convert_rowtype.incache = &rowcachep[0];
1762 scratch.d.convert_rowtype.outcache = &rowcachep[1];
1763 scratch.d.convert_rowtype.map = NULL;
1764
1765 ExprEvalPushStep(state, &scratch);
1766 break;
1767 }
1768
1769 /* note that CaseWhen expressions are handled within this block */
1770 case T_CaseExpr:
1771 {
1772 CaseExpr *caseExpr = (CaseExpr *) node;
1773 List *adjust_jumps = NIL;
1774 Datum *caseval = NULL;
1775 bool *casenull = NULL;
1776 ListCell *lc;
1777
1778 /*
1779 * If there's a test expression, we have to evaluate it and
1780 * save the value where the CaseTestExpr placeholders can find
1781 * it.
1782 */
1783 if (caseExpr->arg != NULL)
1784 {
1785 /* Evaluate testexpr into caseval/casenull workspace */
1786 caseval = palloc(sizeof(Datum));
1787 casenull = palloc(sizeof(bool));
1788
1789 ExecInitExprRec(caseExpr->arg, state,
1790 caseval, casenull);
1791
1792 /*
1793 * Since value might be read multiple times, force to R/O
1794 * - but only if it could be an expanded datum.
1795 */
1796 if (get_typlen(exprType((Node *) caseExpr->arg)) == -1)
1797 {
1798 /* change caseval in-place */
1799 scratch.opcode = EEOP_MAKE_READONLY;
1800 scratch.resvalue = caseval;
1801 scratch.resnull = casenull;
1802 scratch.d.make_readonly.value = caseval;
1803 scratch.d.make_readonly.isnull = casenull;
1804 ExprEvalPushStep(state, &scratch);
1805 /* restore normal settings of scratch fields */
1806 scratch.resvalue = resv;
1807 scratch.resnull = resnull;
1808 }
1809 }
1810
1811 /*
1812 * Prepare to evaluate each of the WHEN clauses in turn; as
1813 * soon as one is true we return the value of the
1814 * corresponding THEN clause. If none are true then we return
1815 * the value of the ELSE clause, or NULL if there is none.
1816 */
1817 foreach(lc, caseExpr->args)
1818 {
1819 CaseWhen *when = (CaseWhen *) lfirst(lc);
1820 Datum *save_innermost_caseval;
1821 bool *save_innermost_casenull;
1822 int whenstep;
1823
1824 /*
1825 * Make testexpr result available to CaseTestExpr nodes
1826 * within the condition. We must save and restore prior
1827 * setting of innermost_caseval fields, in case this node
1828 * is itself within a larger CASE.
1829 *
1830 * If there's no test expression, we don't actually need
1831 * to save and restore these fields; but it's less code to
1832 * just do so unconditionally.
1833 */
1834 save_innermost_caseval = state->innermost_caseval;
1835 save_innermost_casenull = state->innermost_casenull;
1836 state->innermost_caseval = caseval;
1837 state->innermost_casenull = casenull;
1838
1839 /* evaluate condition into CASE's result variables */
1840 ExecInitExprRec(when->expr, state, resv, resnull);
1841
1842 state->innermost_caseval = save_innermost_caseval;
1843 state->innermost_casenull = save_innermost_casenull;
1844
1845 /* If WHEN result isn't true, jump to next CASE arm */
1846 scratch.opcode = EEOP_JUMP_IF_NOT_TRUE;
1847 scratch.d.jump.jumpdone = -1; /* computed later */
1848 ExprEvalPushStep(state, &scratch);
1849 whenstep = state->steps_len - 1;
1850
1851 /*
1852 * If WHEN result is true, evaluate THEN result, storing
1853 * it into the CASE's result variables.
1854 */
1855 ExecInitExprRec(when->result, state, resv, resnull);
1856
1857 /* Emit JUMP step to jump to end of CASE's code */
1858 scratch.opcode = EEOP_JUMP;
1859 scratch.d.jump.jumpdone = -1; /* computed later */
1860 ExprEvalPushStep(state, &scratch);
1861
1862 /*
1863 * Don't know address for that jump yet, compute once the
1864 * whole CASE expression is built.
1865 */
1866 adjust_jumps = lappend_int(adjust_jumps,
1867 state->steps_len - 1);
1868
1869 /*
1870 * But we can set WHEN test's jump target now, to make it
1871 * jump to the next WHEN subexpression or the ELSE.
1872 */
1873 state->steps[whenstep].d.jump.jumpdone = state->steps_len;
1874 }
1875
1876 /* transformCaseExpr always adds a default */
1877 Assert(caseExpr->defresult);
1878
1879 /* evaluate ELSE expr into CASE's result variables */
1880 ExecInitExprRec(caseExpr->defresult, state,
1881 resv, resnull);
1882
1883 /* adjust jump targets */
1884 foreach(lc, adjust_jumps)
1885 {
1886 ExprEvalStep *as = &state->steps[lfirst_int(lc)];
1887
1888 Assert(as->opcode == EEOP_JUMP);
1889 Assert(as->d.jump.jumpdone == -1);
1890 as->d.jump.jumpdone = state->steps_len;
1891 }
1892
1893 break;
1894 }
1895
1896 case T_CaseTestExpr:
1897 {
1898 /*
1899 * Read from location identified by innermost_caseval. Note
1900 * that innermost_caseval could be NULL, if this node isn't
1901 * actually within a CaseExpr, ArrayCoerceExpr, etc structure.
1902 * That can happen because some parts of the system abuse
1903 * CaseTestExpr to cause a read of a value externally supplied
1904 * in econtext->caseValue_datum. We'll take care of that
1905 * scenario at runtime.
1906 */
1907 scratch.opcode = EEOP_CASE_TESTVAL;
1908 scratch.d.casetest.value = state->innermost_caseval;
1909 scratch.d.casetest.isnull = state->innermost_casenull;
1910
1911 ExprEvalPushStep(state, &scratch);
1912 break;
1913 }
1914
1915 case T_ArrayExpr:
1916 {
1917 ArrayExpr *arrayexpr = (ArrayExpr *) node;
1918 int nelems = list_length(arrayexpr->elements);
1919 ListCell *lc;
1920 int elemoff;
1921
1922 /*
1923 * Evaluate by computing each element, and then forming the
1924 * array. Elements are computed into scratch arrays
1925 * associated with the ARRAYEXPR step.
1926 */
1927 scratch.opcode = EEOP_ARRAYEXPR;
1928 scratch.d.arrayexpr.elemvalues =
1929 (Datum *) palloc(sizeof(Datum) * nelems);
1930 scratch.d.arrayexpr.elemnulls =
1931 (bool *) palloc(sizeof(bool) * nelems);
1932 scratch.d.arrayexpr.nelems = nelems;
1933
1934 /* fill remaining fields of step */
1935 scratch.d.arrayexpr.multidims = arrayexpr->multidims;
1936 scratch.d.arrayexpr.elemtype = arrayexpr->element_typeid;
1937
1938 /* do one-time catalog lookup for type info */
1939 get_typlenbyvalalign(arrayexpr->element_typeid,
1940 &scratch.d.arrayexpr.elemlength,
1941 &scratch.d.arrayexpr.elembyval,
1942 &scratch.d.arrayexpr.elemalign);
1943
1944 /* prepare to evaluate all arguments */
1945 elemoff = 0;
1946 foreach(lc, arrayexpr->elements)
1947 {
1948 Expr *e = (Expr *) lfirst(lc);
1949
1951 &scratch.d.arrayexpr.elemvalues[elemoff],
1952 &scratch.d.arrayexpr.elemnulls[elemoff]);
1953 elemoff++;
1954 }
1955
1956 /* and then collect all into an array */
1957 ExprEvalPushStep(state, &scratch);
1958 break;
1959 }
1960
1961 case T_RowExpr:
1962 {
1963 RowExpr *rowexpr = (RowExpr *) node;
1964 int nelems = list_length(rowexpr->args);
1965 TupleDesc tupdesc;
1966 int i;
1967 ListCell *l;
1968
1969 /* Build tupdesc to describe result tuples */
1970 if (rowexpr->row_typeid == RECORDOID)
1971 {
1972 /* generic record, use types of given expressions */
1973 tupdesc = ExecTypeFromExprList(rowexpr->args);
1974 /* ... but adopt RowExpr's column aliases */
1975 ExecTypeSetColNames(tupdesc, rowexpr->colnames);
1976 /* Bless the tupdesc so it can be looked up later */
1977 BlessTupleDesc(tupdesc);
1978 }
1979 else
1980 {
1981 /* it's been cast to a named type, use that */
1982 tupdesc = lookup_rowtype_tupdesc_copy(rowexpr->row_typeid, -1);
1983 }
1984
1985 /*
1986 * In the named-type case, the tupdesc could have more columns
1987 * than are in the args list, since the type might have had
1988 * columns added since the ROW() was parsed. We want those
1989 * extra columns to go to nulls, so we make sure that the
1990 * workspace arrays are large enough and then initialize any
1991 * extra columns to read as NULLs.
1992 */
1993 Assert(nelems <= tupdesc->natts);
1994 nelems = Max(nelems, tupdesc->natts);
1995
1996 /*
1997 * Evaluate by first building datums for each field, and then
1998 * a final step forming the composite datum.
1999 */
2000 scratch.opcode = EEOP_ROW;
2001 scratch.d.row.tupdesc = tupdesc;
2002
2003 /* space for the individual field datums */
2004 scratch.d.row.elemvalues =
2005 (Datum *) palloc(sizeof(Datum) * nelems);
2006 scratch.d.row.elemnulls =
2007 (bool *) palloc(sizeof(bool) * nelems);
2008 /* as explained above, make sure any extra columns are null */
2009 memset(scratch.d.row.elemnulls, true, sizeof(bool) * nelems);
2010
2011 /* Set up evaluation, skipping any deleted columns */
2012 i = 0;
2013 foreach(l, rowexpr->args)
2014 {
2015 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
2016 Expr *e = (Expr *) lfirst(l);
2017
2018 if (!att->attisdropped)
2019 {
2020 /*
2021 * Guard against ALTER COLUMN TYPE on rowtype since
2022 * the RowExpr was created. XXX should we check
2023 * typmod too? Not sure we can be sure it'll be the
2024 * same.
2025 */
2026 if (exprType((Node *) e) != att->atttypid)
2027 ereport(ERROR,
2028 (errcode(ERRCODE_DATATYPE_MISMATCH),
2029 errmsg("ROW() column has type %s instead of type %s",
2031 format_type_be(att->atttypid))));
2032 }
2033 else
2034 {
2035 /*
2036 * Ignore original expression and insert a NULL. We
2037 * don't really care what type of NULL it is, so
2038 * always make an int4 NULL.
2039 */
2040 e = (Expr *) makeNullConst(INT4OID, -1, InvalidOid);
2041 }
2042
2043 /* Evaluate column expr into appropriate workspace slot */
2045 &scratch.d.row.elemvalues[i],
2046 &scratch.d.row.elemnulls[i]);
2047 i++;
2048 }
2049
2050 /* And finally build the row value */
2051 ExprEvalPushStep(state, &scratch);
2052 break;
2053 }
2054
2055 case T_RowCompareExpr:
2056 {
2057 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
2058 int nopers = list_length(rcexpr->opnos);
2059 List *adjust_jumps = NIL;
2060 ListCell *l_left_expr,
2061 *l_right_expr,
2062 *l_opno,
2063 *l_opfamily,
2064 *l_inputcollid;
2065 ListCell *lc;
2066
2067 /*
2068 * Iterate over each field, prepare comparisons. To handle
2069 * NULL results, prepare jumps to after the expression. If a
2070 * comparison yields a != 0 result, jump to the final step.
2071 */
2072 Assert(list_length(rcexpr->largs) == nopers);
2073 Assert(list_length(rcexpr->rargs) == nopers);
2074 Assert(list_length(rcexpr->opfamilies) == nopers);
2075 Assert(list_length(rcexpr->inputcollids) == nopers);
2076
2077 forfive(l_left_expr, rcexpr->largs,
2078 l_right_expr, rcexpr->rargs,
2079 l_opno, rcexpr->opnos,
2080 l_opfamily, rcexpr->opfamilies,
2081 l_inputcollid, rcexpr->inputcollids)
2082 {
2083 Expr *left_expr = (Expr *) lfirst(l_left_expr);
2084 Expr *right_expr = (Expr *) lfirst(l_right_expr);
2085 Oid opno = lfirst_oid(l_opno);
2086 Oid opfamily = lfirst_oid(l_opfamily);
2087 Oid inputcollid = lfirst_oid(l_inputcollid);
2088 int strategy;
2089 Oid lefttype;
2090 Oid righttype;
2091 Oid proc;
2092 FmgrInfo *finfo;
2093 FunctionCallInfo fcinfo;
2094
2095 get_op_opfamily_properties(opno, opfamily, false,
2096 &strategy,
2097 &lefttype,
2098 &righttype);
2099 proc = get_opfamily_proc(opfamily,
2100 lefttype,
2101 righttype,
2102 BTORDER_PROC);
2103 if (!OidIsValid(proc))
2104 elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
2105 BTORDER_PROC, lefttype, righttype, opfamily);
2106
2107 /* Set up the primary fmgr lookup information */
2108 finfo = palloc0(sizeof(FmgrInfo));
2109 fcinfo = palloc0(SizeForFunctionCallInfo(2));
2110 fmgr_info(proc, finfo);
2111 fmgr_info_set_expr((Node *) node, finfo);
2112 InitFunctionCallInfoData(*fcinfo, finfo, 2,
2113 inputcollid, NULL, NULL);
2114
2115 /*
2116 * If we enforced permissions checks on index support
2117 * functions, we'd need to make a check here. But the
2118 * index support machinery doesn't do that, and thus
2119 * neither does this code.
2120 */
2121
2122 /* evaluate left and right args directly into fcinfo */
2123 ExecInitExprRec(left_expr, state,
2124 &fcinfo->args[0].value, &fcinfo->args[0].isnull);
2125 ExecInitExprRec(right_expr, state,
2126 &fcinfo->args[1].value, &fcinfo->args[1].isnull);
2127
2128 scratch.opcode = EEOP_ROWCOMPARE_STEP;
2129 scratch.d.rowcompare_step.finfo = finfo;
2130 scratch.d.rowcompare_step.fcinfo_data = fcinfo;
2131 scratch.d.rowcompare_step.fn_addr = finfo->fn_addr;
2132 /* jump targets filled below */
2133 scratch.d.rowcompare_step.jumpnull = -1;
2134 scratch.d.rowcompare_step.jumpdone = -1;
2135
2136 ExprEvalPushStep(state, &scratch);
2137 adjust_jumps = lappend_int(adjust_jumps,
2138 state->steps_len - 1);
2139 }
2140
2141 /*
2142 * We could have a zero-column rowtype, in which case the rows
2143 * necessarily compare equal.
2144 */
2145 if (nopers == 0)
2146 {
2147 scratch.opcode = EEOP_CONST;
2148 scratch.d.constval.value = Int32GetDatum(0);
2149 scratch.d.constval.isnull = false;
2150 ExprEvalPushStep(state, &scratch);
2151 }
2152
2153 /* Finally, examine the last comparison result */
2154 scratch.opcode = EEOP_ROWCOMPARE_FINAL;
2155 scratch.d.rowcompare_final.cmptype = rcexpr->cmptype;
2156 ExprEvalPushStep(state, &scratch);
2157
2158 /* adjust jump targets */
2159 foreach(lc, adjust_jumps)
2160 {
2161 ExprEvalStep *as = &state->steps[lfirst_int(lc)];
2162
2164 Assert(as->d.rowcompare_step.jumpdone == -1);
2165 Assert(as->d.rowcompare_step.jumpnull == -1);
2166
2167 /* jump to comparison evaluation */
2168 as->d.rowcompare_step.jumpdone = state->steps_len - 1;
2169 /* jump to the following expression */
2170 as->d.rowcompare_step.jumpnull = state->steps_len;
2171 }
2172
2173 break;
2174 }
2175
2176 case T_CoalesceExpr:
2177 {
2178 CoalesceExpr *coalesce = (CoalesceExpr *) node;
2179 List *adjust_jumps = NIL;
2180 ListCell *lc;
2181
2182 /* We assume there's at least one arg */
2183 Assert(coalesce->args != NIL);
2184
2185 /*
2186 * Prepare evaluation of all coalesced arguments, after each
2187 * one push a step that short-circuits if not null.
2188 */
2189 foreach(lc, coalesce->args)
2190 {
2191 Expr *e = (Expr *) lfirst(lc);
2192
2193 /* evaluate argument, directly into result datum */
2194 ExecInitExprRec(e, state, resv, resnull);
2195
2196 /* if it's not null, skip to end of COALESCE expr */
2197 scratch.opcode = EEOP_JUMP_IF_NOT_NULL;
2198 scratch.d.jump.jumpdone = -1; /* adjust later */
2199 ExprEvalPushStep(state, &scratch);
2200
2201 adjust_jumps = lappend_int(adjust_jumps,
2202 state->steps_len - 1);
2203 }
2204
2205 /*
2206 * No need to add a constant NULL return - we only can get to
2207 * the end of the expression if a NULL already is being
2208 * returned.
2209 */
2210
2211 /* adjust jump targets */
2212 foreach(lc, adjust_jumps)
2213 {
2214 ExprEvalStep *as = &state->steps[lfirst_int(lc)];
2215
2217 Assert(as->d.jump.jumpdone == -1);
2218 as->d.jump.jumpdone = state->steps_len;
2219 }
2220
2221 break;
2222 }
2223
2224 case T_MinMaxExpr:
2225 {
2226 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
2227 int nelems = list_length(minmaxexpr->args);
2228 TypeCacheEntry *typentry;
2229 FmgrInfo *finfo;
2230 FunctionCallInfo fcinfo;
2231 ListCell *lc;
2232 int off;
2233
2234 /* Look up the btree comparison function for the datatype */
2235 typentry = lookup_type_cache(minmaxexpr->minmaxtype,
2237 if (!OidIsValid(typentry->cmp_proc))
2238 ereport(ERROR,
2239 (errcode(ERRCODE_UNDEFINED_FUNCTION),
2240 errmsg("could not identify a comparison function for type %s",
2241 format_type_be(minmaxexpr->minmaxtype))));
2242
2243 /*
2244 * If we enforced permissions checks on index support
2245 * functions, we'd need to make a check here. But the index
2246 * support machinery doesn't do that, and thus neither does
2247 * this code.
2248 */
2249
2250 /* Perform function lookup */
2251 finfo = palloc0(sizeof(FmgrInfo));
2252 fcinfo = palloc0(SizeForFunctionCallInfo(2));
2253 fmgr_info(typentry->cmp_proc, finfo);
2254 fmgr_info_set_expr((Node *) node, finfo);
2255 InitFunctionCallInfoData(*fcinfo, finfo, 2,
2256 minmaxexpr->inputcollid, NULL, NULL);
2257
2258 scratch.opcode = EEOP_MINMAX;
2259 /* allocate space to store arguments */
2260 scratch.d.minmax.values =
2261 (Datum *) palloc(sizeof(Datum) * nelems);
2262 scratch.d.minmax.nulls =
2263 (bool *) palloc(sizeof(bool) * nelems);
2264 scratch.d.minmax.nelems = nelems;
2265
2266 scratch.d.minmax.op = minmaxexpr->op;
2267 scratch.d.minmax.finfo = finfo;
2268 scratch.d.minmax.fcinfo_data = fcinfo;
2269
2270 /* evaluate expressions into minmax->values/nulls */
2271 off = 0;
2272 foreach(lc, minmaxexpr->args)
2273 {
2274 Expr *e = (Expr *) lfirst(lc);
2275
2277 &scratch.d.minmax.values[off],
2278 &scratch.d.minmax.nulls[off]);
2279 off++;
2280 }
2281
2282 /* and push the final comparison */
2283 ExprEvalPushStep(state, &scratch);
2284 break;
2285 }
2286
2287 case T_SQLValueFunction:
2288 {
2289 SQLValueFunction *svf = (SQLValueFunction *) node;
2290
2291 scratch.opcode = EEOP_SQLVALUEFUNCTION;
2292 scratch.d.sqlvaluefunction.svf = svf;
2293
2294 ExprEvalPushStep(state, &scratch);
2295 break;
2296 }
2297
2298 case T_XmlExpr:
2299 {
2300 XmlExpr *xexpr = (XmlExpr *) node;
2301 int nnamed = list_length(xexpr->named_args);
2302 int nargs = list_length(xexpr->args);
2303 int off;
2304 ListCell *arg;
2305
2306 scratch.opcode = EEOP_XMLEXPR;
2307 scratch.d.xmlexpr.xexpr = xexpr;
2308
2309 /* allocate space for storing all the arguments */
2310 if (nnamed)
2311 {
2312 scratch.d.xmlexpr.named_argvalue =
2313 (Datum *) palloc(sizeof(Datum) * nnamed);
2314 scratch.d.xmlexpr.named_argnull =
2315 (bool *) palloc(sizeof(bool) * nnamed);
2316 }
2317 else
2318 {
2319 scratch.d.xmlexpr.named_argvalue = NULL;
2320 scratch.d.xmlexpr.named_argnull = NULL;
2321 }
2322
2323 if (nargs)
2324 {
2325 scratch.d.xmlexpr.argvalue =
2326 (Datum *) palloc(sizeof(Datum) * nargs);
2327 scratch.d.xmlexpr.argnull =
2328 (bool *) palloc(sizeof(bool) * nargs);
2329 }
2330 else
2331 {
2332 scratch.d.xmlexpr.argvalue = NULL;
2333 scratch.d.xmlexpr.argnull = NULL;
2334 }
2335
2336 /* prepare argument execution */
2337 off = 0;
2338 foreach(arg, xexpr->named_args)
2339 {
2340 Expr *e = (Expr *) lfirst(arg);
2341
2343 &scratch.d.xmlexpr.named_argvalue[off],
2344 &scratch.d.xmlexpr.named_argnull[off]);
2345 off++;
2346 }
2347
2348 off = 0;
2349 foreach(arg, xexpr->args)
2350 {
2351 Expr *e = (Expr *) lfirst(arg);
2352
2354 &scratch.d.xmlexpr.argvalue[off],
2355 &scratch.d.xmlexpr.argnull[off]);
2356 off++;
2357 }
2358
2359 /* and evaluate the actual XML expression */
2360 ExprEvalPushStep(state, &scratch);
2361 break;
2362 }
2363
2364 case T_JsonValueExpr:
2365 {
2366 JsonValueExpr *jve = (JsonValueExpr *) node;
2367
2368 Assert(jve->raw_expr != NULL);
2369 ExecInitExprRec(jve->raw_expr, state, resv, resnull);
2370 Assert(jve->formatted_expr != NULL);
2371 ExecInitExprRec(jve->formatted_expr, state, resv, resnull);
2372 break;
2373 }
2374
2375 case T_JsonConstructorExpr:
2376 {
2378 List *args = ctor->args;
2379 ListCell *lc;
2380 int nargs = list_length(args);
2381 int argno = 0;
2382
2383 if (ctor->func)
2384 {
2385 ExecInitExprRec(ctor->func, state, resv, resnull);
2386 }
2387 else if ((ctor->type == JSCTOR_JSON_PARSE && !ctor->unique) ||
2388 ctor->type == JSCTOR_JSON_SERIALIZE)
2389 {
2390 /* Use the value of the first argument as result */
2391 ExecInitExprRec(linitial(args), state, resv, resnull);
2392 }
2393 else
2394 {
2395 JsonConstructorExprState *jcstate;
2396
2397 jcstate = palloc0(sizeof(JsonConstructorExprState));
2398
2399 scratch.opcode = EEOP_JSON_CONSTRUCTOR;
2400 scratch.d.json_constructor.jcstate = jcstate;
2401
2402 jcstate->constructor = ctor;
2403 jcstate->arg_values = (Datum *) palloc(sizeof(Datum) * nargs);
2404 jcstate->arg_nulls = (bool *) palloc(sizeof(bool) * nargs);
2405 jcstate->arg_types = (Oid *) palloc(sizeof(Oid) * nargs);
2406 jcstate->nargs = nargs;
2407
2408 foreach(lc, args)
2409 {
2410 Expr *arg = (Expr *) lfirst(lc);
2411
2412 jcstate->arg_types[argno] = exprType((Node *) arg);
2413
2414 if (IsA(arg, Const))
2415 {
2416 /* Don't evaluate const arguments every round */
2417 Const *con = (Const *) arg;
2418
2419 jcstate->arg_values[argno] = con->constvalue;
2420 jcstate->arg_nulls[argno] = con->constisnull;
2421 }
2422 else
2423 {
2425 &jcstate->arg_values[argno],
2426 &jcstate->arg_nulls[argno]);
2427 }
2428 argno++;
2429 }
2430
2431 /* prepare type cache for datum_to_json[b]() */
2432 if (ctor->type == JSCTOR_JSON_SCALAR)
2433 {
2434 bool is_jsonb =
2436
2437 jcstate->arg_type_cache =
2438 palloc(sizeof(*jcstate->arg_type_cache) * nargs);
2439
2440 for (int i = 0; i < nargs; i++)
2441 {
2442 JsonTypeCategory category;
2443 Oid outfuncid;
2444 Oid typid = jcstate->arg_types[i];
2445
2446 json_categorize_type(typid, is_jsonb,
2447 &category, &outfuncid);
2448
2449 jcstate->arg_type_cache[i].outfuncid = outfuncid;
2450 jcstate->arg_type_cache[i].category = (int) category;
2451 }
2452 }
2453
2454 ExprEvalPushStep(state, &scratch);
2455 }
2456
2457 if (ctor->coercion)
2458 {
2459 Datum *innermost_caseval = state->innermost_caseval;
2460 bool *innermost_isnull = state->innermost_casenull;
2461
2462 state->innermost_caseval = resv;
2463 state->innermost_casenull = resnull;
2464
2465 ExecInitExprRec(ctor->coercion, state, resv, resnull);
2466
2467 state->innermost_caseval = innermost_caseval;
2468 state->innermost_casenull = innermost_isnull;
2469 }
2470 }
2471 break;
2472
2473 case T_JsonIsPredicate:
2474 {
2475 JsonIsPredicate *pred = (JsonIsPredicate *) node;
2476
2477 ExecInitExprRec((Expr *) pred->expr, state, resv, resnull);
2478
2479 scratch.opcode = EEOP_IS_JSON;
2480 scratch.d.is_json.pred = pred;
2481
2482 ExprEvalPushStep(state, &scratch);
2483 break;
2484 }
2485
2486 case T_JsonExpr:
2487 {
2488 JsonExpr *jsexpr = castNode(JsonExpr, node);
2489
2490 /*
2491 * No need to initialize a full JsonExprState For
2492 * JSON_TABLE(), because the upstream caller tfuncFetchRows()
2493 * is only interested in the value of formatted_expr.
2494 */
2495 if (jsexpr->op == JSON_TABLE_OP)
2497 resv, resnull);
2498 else
2499 ExecInitJsonExpr(jsexpr, state, resv, resnull, &scratch);
2500 break;
2501 }
2502
2503 case T_NullTest:
2504 {
2505 NullTest *ntest = (NullTest *) node;
2506
2507 if (ntest->nulltesttype == IS_NULL)
2508 {
2509 if (ntest->argisrow)
2511 else
2512 scratch.opcode = EEOP_NULLTEST_ISNULL;
2513 }
2514 else if (ntest->nulltesttype == IS_NOT_NULL)
2515 {
2516 if (ntest->argisrow)
2518 else
2520 }
2521 else
2522 {
2523 elog(ERROR, "unrecognized nulltesttype: %d",
2524 (int) ntest->nulltesttype);
2525 }
2526 /* initialize cache in case it's a row test */
2527 scratch.d.nulltest_row.rowcache.cacheptr = NULL;
2528
2529 /* first evaluate argument into result variable */
2530 ExecInitExprRec(ntest->arg, state,
2531 resv, resnull);
2532
2533 /* then push the test of that argument */
2534 ExprEvalPushStep(state, &scratch);
2535 break;
2536 }
2537
2538 case T_BooleanTest:
2539 {
2540 BooleanTest *btest = (BooleanTest *) node;
2541
2542 /*
2543 * Evaluate argument, directly into result datum. That's ok,
2544 * because resv/resnull is definitely not used anywhere else,
2545 * and will get overwritten by the below EEOP_BOOLTEST_IS_*
2546 * step.
2547 */
2548 ExecInitExprRec(btest->arg, state, resv, resnull);
2549
2550 switch (btest->booltesttype)
2551 {
2552 case IS_TRUE:
2553 scratch.opcode = EEOP_BOOLTEST_IS_TRUE;
2554 break;
2555 case IS_NOT_TRUE:
2557 break;
2558 case IS_FALSE:
2560 break;
2561 case IS_NOT_FALSE:
2563 break;
2564 case IS_UNKNOWN:
2565 /* Same as scalar IS NULL test */
2566 scratch.opcode = EEOP_NULLTEST_ISNULL;
2567 break;
2568 case IS_NOT_UNKNOWN:
2569 /* Same as scalar IS NOT NULL test */
2571 break;
2572 default:
2573 elog(ERROR, "unrecognized booltesttype: %d",
2574 (int) btest->booltesttype);
2575 }
2576
2577 ExprEvalPushStep(state, &scratch);
2578 break;
2579 }
2580
2581 case T_CoerceToDomain:
2582 {
2583 CoerceToDomain *ctest = (CoerceToDomain *) node;
2584
2585 ExecInitCoerceToDomain(&scratch, ctest, state,
2586 resv, resnull);
2587 break;
2588 }
2589
2590 case T_CoerceToDomainValue:
2591 {
2592 /*
2593 * Read from location identified by innermost_domainval. Note
2594 * that innermost_domainval could be NULL, if we're compiling
2595 * a standalone domain check rather than one embedded in a
2596 * larger expression. In that case we must read from
2597 * econtext->domainValue_datum. We'll take care of that
2598 * scenario at runtime.
2599 */
2600 scratch.opcode = EEOP_DOMAIN_TESTVAL;
2601 /* we share instruction union variant with case testval */
2602 scratch.d.casetest.value = state->innermost_domainval;
2603 scratch.d.casetest.isnull = state->innermost_domainnull;
2604
2605 ExprEvalPushStep(state, &scratch);
2606 break;
2607 }
2608
2609 case T_CurrentOfExpr:
2610 {
2611 scratch.opcode = EEOP_CURRENTOFEXPR;
2612 ExprEvalPushStep(state, &scratch);
2613 break;
2614 }
2615
2616 case T_NextValueExpr:
2617 {
2618 NextValueExpr *nve = (NextValueExpr *) node;
2619
2620 scratch.opcode = EEOP_NEXTVALUEEXPR;
2621 scratch.d.nextvalueexpr.seqid = nve->seqid;
2622 scratch.d.nextvalueexpr.seqtypid = nve->typeId;
2623
2624 ExprEvalPushStep(state, &scratch);
2625 break;
2626 }
2627
2628 case T_ReturningExpr:
2629 {
2630 ReturningExpr *rexpr = (ReturningExpr *) node;
2631 int retstep;
2632
2633 /* Skip expression evaluation if OLD/NEW row doesn't exist */
2634 scratch.opcode = EEOP_RETURNINGEXPR;
2635 scratch.d.returningexpr.nullflag = rexpr->retold ?
2637 scratch.d.returningexpr.jumpdone = -1; /* set below */
2638 ExprEvalPushStep(state, &scratch);
2639 retstep = state->steps_len - 1;
2640
2641 /* Steps to evaluate expression to return */
2642 ExecInitExprRec(rexpr->retexpr, state, resv, resnull);
2643
2644 /* Jump target used if OLD/NEW row doesn't exist */
2645 state->steps[retstep].d.returningexpr.jumpdone = state->steps_len;
2646
2647 /* Update ExprState flags */
2648 if (rexpr->retold)
2649 state->flags |= EEO_FLAG_HAS_OLD;
2650 else
2651 state->flags |= EEO_FLAG_HAS_NEW;
2652
2653 break;
2654 }
2655
2656 default:
2657 elog(ERROR, "unrecognized node type: %d",
2658 (int) nodeTag(node));
2659 break;
2660 }
2661}
#define InvalidAttrNumber
Definition: attnum.h:23
static void ExecInitCoerceToDomain(ExprEvalStep *scratch, CoerceToDomain *ctest, ExprState *state, Datum *resv, bool *resnull)
Definition: execExpr.c:3506
static void ExecInitSubPlanExpr(SubPlan *subplan, ExprState *state, Datum *resv, bool *resnull)
Definition: execExpr.c:2805
static void ExecInitWholeRowVar(ExprEvalStep *scratch, Var *variable, ExprState *state)
Definition: execExpr.c:3148
static void ExecInitSubscriptingRef(ExprEvalStep *scratch, SubscriptingRef *sbsref, ExprState *state, Datum *resv, bool *resnull)
Definition: execExpr.c:3227
List * ExecInitExprList(List *nodes, PlanState *parent)
Definition: execExpr.c:335
static void ExecInitFunc(ExprEvalStep *scratch, Expr *node, List *args, Oid funcid, Oid inputcollid, ExprState *state)
Definition: execExpr.c:2696
static void ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state, Datum *resv, bool *resnull, ExprEvalStep *scratch)
Definition: execExpr.c:4729
@ EEOP_OLD_VAR
Definition: execExpr.h:82
@ EEOP_CONVERT_ROWTYPE
Definition: execExpr.h:255
@ EEOP_ARRAYEXPR
Definition: execExpr.h:190
@ EEOP_DOMAIN_TESTVAL
Definition: execExpr.h:239
@ EEOP_PARAM_EXTERN
Definition: execExpr.h:169
@ EEOP_IOCOERCE_SAFE
Definition: execExpr.h:182
@ EEOP_BOOL_AND_STEP
Definition: execExpr.h:131
@ EEOP_AGGREF
Definition: execExpr.h:264
@ EEOP_ROWCOMPARE_FINAL
Definition: execExpr.h:201
@ EEOP_IOCOERCE
Definition: execExpr.h:181
@ EEOP_RETURNINGEXPR
Definition: execExpr.h:189
@ EEOP_GROUPING_FUNC
Definition: execExpr.h:265
@ EEOP_BOOLTEST_IS_NOT_FALSE
Definition: execExpr.h:165
@ EEOP_NEXTVALUEEXPR
Definition: execExpr.h:188
@ EEOP_NEW_SYSVAR
Definition: execExpr.h:90
@ EEOP_SCAN_VAR
Definition: execExpr.h:81
@ EEOP_BOOL_NOT_STEP
Definition: execExpr.h:140
@ EEOP_NEW_VAR
Definition: execExpr.h:83
@ EEOP_SCAN_SYSVAR
Definition: execExpr.h:88
@ EEOP_SCALARARRAYOP
Definition: execExpr.h:256
@ EEOP_WINDOW_FUNC
Definition: execExpr.h:266
@ EEOP_NULLTEST_ROWISNOTNULL
Definition: execExpr.h:159
@ EEOP_ROW
Definition: execExpr.h:192
@ EEOP_FIELDSTORE_FORM
Definition: execExpr.h:220
@ EEOP_NULLIF
Definition: execExpr.h:185
@ EEOP_CURRENTOFEXPR
Definition: execExpr.h:187
@ EEOP_INNER_SYSVAR
Definition: execExpr.h:86
@ EEOP_BOOL_OR_STEP_LAST
Definition: execExpr.h:137
@ EEOP_BOOL_OR_STEP_FIRST
Definition: execExpr.h:135
@ EEOP_XMLEXPR
Definition: execExpr.h:258
@ EEOP_OUTER_SYSVAR
Definition: execExpr.h:87
@ EEOP_BOOL_OR_STEP
Definition: execExpr.h:136
@ EEOP_NULLTEST_ROWISNULL
Definition: execExpr.h:158
@ EEOP_BOOLTEST_IS_TRUE
Definition: execExpr.h:162
@ EEOP_NULLTEST_ISNOTNULL
Definition: execExpr.h:155
@ EEOP_ROWCOMPARE_STEP
Definition: execExpr.h:198
@ EEOP_MERGE_SUPPORT_FUNC
Definition: execExpr.h:267
@ EEOP_DISTINCT
Definition: execExpr.h:183
@ EEOP_BOOL_AND_STEP_FIRST
Definition: execExpr.h:130
@ EEOP_JUMP
Definition: execExpr.h:146
@ EEOP_OLD_SYSVAR
Definition: execExpr.h:89
@ EEOP_BOOL_AND_STEP_LAST
Definition: execExpr.h:132
@ EEOP_SQLVALUEFUNCTION
Definition: execExpr.h:186
@ EEOP_JUMP_IF_NOT_NULL
Definition: execExpr.h:150
@ EEOP_FIELDSTORE_DEFORM
Definition: execExpr.h:213
@ EEOP_BOOLTEST_IS_FALSE
Definition: execExpr.h:164
@ EEOP_BOOLTEST_IS_NOT_TRUE
Definition: execExpr.h:163
@ EEOP_PARAM_EXEC
Definition: execExpr.h:168
@ EEOP_JSON_CONSTRUCTOR
Definition: execExpr.h:259
@ EEOP_NULLTEST_ISNULL
Definition: execExpr.h:154
@ EEOP_MINMAX
Definition: execExpr.h:204
@ EEOP_ARRAYCOERCE
Definition: execExpr.h:191
@ EEOP_FIELDSELECT
Definition: execExpr.h:207
@ EEOP_CASE_TESTVAL
Definition: execExpr.h:175
@ EEOP_HASHED_SCALARARRAYOP
Definition: execExpr.h:257
@ EEOP_IS_JSON
Definition: execExpr.h:260
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2258
void ExecTypeSetColNames(TupleDesc typeInfo, List *namesList)
Definition: execTuples.c:2217
TupleDesc ExecTypeFromExprList(List *exprList)
Definition: execTuples.c:2184
#define EEO_FLAG_NEW_IS_NULL
Definition: execnodes.h:84
#define EEO_FLAG_OLD_IS_NULL
Definition: execnodes.h:82
void json_categorize_type(Oid typoid, bool is_jsonb, JsonTypeCategory *tcategory, Oid *outfuncoid)
Definition: jsonfuncs.c:5976
JsonTypeCategory
Definition: jsonfuncs.h:69
void get_op_opfamily_properties(Oid opno, Oid opfamily, bool ordering_op, int *strategy, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:136
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2759
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2907
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2271
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:796
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2874
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:341
#define BTORDER_PROC
Definition: nbtree.h:712
#define nodeTag(nodeptr)
Definition: nodes.h:133
@ CMD_MERGE
Definition: nodes.h:269
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
#define lsecond(l)
Definition: pg_list.h:183
#define forfive(cell1, list1, cell2, list2, cell3, list3, cell4, list4, cell5, list5)
Definition: pg_list.h:588
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217
#define InvalidOid
Definition: postgres_ext.h:37
@ IS_NOT_TRUE
Definition: primnodes.h:2007
@ IS_NOT_FALSE
Definition: primnodes.h:2007
@ IS_NOT_UNKNOWN
Definition: primnodes.h:2007
@ IS_TRUE
Definition: primnodes.h:2007
@ IS_UNKNOWN
Definition: primnodes.h:2007
@ IS_FALSE
Definition: primnodes.h:2007
@ MULTIEXPR_SUBLINK
Definition: primnodes.h:1018
@ JS_FORMAT_JSONB
Definition: primnodes.h:1671
@ AND_EXPR
Definition: primnodes.h:947
@ OR_EXPR
Definition: primnodes.h:947
@ NOT_EXPR
Definition: primnodes.h:947
@ PARAM_EXTERN
Definition: primnodes.h:383
@ PARAM_EXEC
Definition: primnodes.h:384
@ JSON_TABLE_OP
Definition: primnodes.h:1836
@ IS_NULL
Definition: primnodes.h:1983
@ IS_NOT_NULL
Definition: primnodes.h:1983
@ JSCTOR_JSON_SERIALIZE
Definition: primnodes.h:1727
@ JSCTOR_JSON_PARSE
Definition: primnodes.h:1725
@ JSCTOR_JSON_SCALAR
Definition: primnodes.h:1726
void check_stack_depth(void)
Definition: stack_depth.c:95
List * aggs
Definition: execnodes.h:2510
List * groupingSets
Definition: plannodes.h:1031
List * elements
Definition: primnodes.h:1396
BoolExprType boolop
Definition: primnodes.h:955
List * args
Definition: primnodes.h:956
BoolTestType booltesttype
Definition: primnodes.h:2014
Expr * arg
Definition: primnodes.h:2013
Expr * arg
Definition: primnodes.h:1329
Expr * defresult
Definition: primnodes.h:1331
List * args
Definition: primnodes.h:1330
List * args
Definition: primnodes.h:1523
Expr * arg
Definition: primnodes.h:1223
Oid resulttype
Definition: primnodes.h:1224
XmlExpr * xexpr
Definition: execExpr.h:644
JsonIsPredicate * pred
Definition: execExpr.h:741
uint8 nullflag
Definition: execExpr.h:365
bool * anynull
Definition: execExpr.h:391
ExprState * elemexprstate
Definition: execExpr.h:484
MinMaxOp op
Definition: execExpr.h:525
char elemalign
Definition: execExpr.h:477
struct ExprEvalStep::@55::@94 grouping_func
List * clauses
Definition: execExpr.h:668
WindowFuncExprState * wfstate
Definition: execExpr.h:675
TupleDesc tupdesc
Definition: execExpr.h:342
Oid seqtypid
Definition: execExpr.h:465
struct ExprEvalStep::@55::@81 fieldselect
FmgrInfo * finfo_out
Definition: execExpr.h:448
AttrNumber fieldnum
Definition: execExpr.h:534
struct ExprEvalStep::@55::@90 hashedscalararrayop
struct ExprEvalStep::@55::@92 json_constructor
Datum * values
Definition: execExpr.h:521
bool * elemnulls
Definition: execExpr.h:472
int ncolumns
Definition: execExpr.h:553
bool * named_argnull
Definition: execExpr.h:647
Oid outputtype
Definition: execExpr.h:608
struct ExprEvalStep::@55::@82 fieldstore
struct ExprEvalStep::@55::@102 is_json
bool inclause
Definition: execExpr.h:634
bool multidims
Definition: execExpr.h:478
int16 elemlength
Definition: execExpr.h:475
struct ExprEvalStep::@55::@78 rowcompare_step
TupleConversionMap * map
Definition: execExpr.h:612
bool * argnull
Definition: execExpr.h:650
struct ExprEvalStep::@55::@91 xmlexpr
struct ExprEvalStep::@55::@67 nulltest_row
Oid inputtype
Definition: execExpr.h:607
FunctionCallInfo fcinfo_data_in
Definition: execExpr.h:452
bool elembyval
Definition: execExpr.h:476
struct ExprEvalStep::@55::@77 row
Datum * elemvalues
Definition: execExpr.h:471
ExprEvalRowtypeCache * incache
Definition: execExpr.h:610
struct ExprEvalStep::@55::@74 nextvalueexpr
ScalarArrayOpExpr * saop
Definition: execExpr.h:638
Oid resultelemtype
Definition: execExpr.h:485
struct ExprEvalStep::@55::@93 aggref
FmgrInfo * finfo_in
Definition: execExpr.h:451
Datum * named_argvalue
Definition: execExpr.h:646
struct ExprEvalStep::@55::@95 window_func
struct ExprEvalStep::@55::@61 returningexpr
struct ExprEvalStep::@55::@72 iocoerce
Oid paramtype
Definition: execExpr.h:418
struct ExprEvalStep::@55::@80 minmax
struct ExprEvalStep::@55::@88 convert_rowtype
struct ExprEvalStep::@55::@79 rowcompare_final
bool useOr
Definition: execExpr.h:620
struct ExprEvalStep::@55::@70 casetest
bool make_ro
Definition: execExpr.h:385
struct ExprEvalStep::@55::@75 arrayexpr
struct ExprEvalStep::@55::@89 scalararrayop
CompareType cmptype
Definition: execExpr.h:514
FieldStore * fstore
Definition: execExpr.h:544
FunctionCallInfo fcinfo_data_out
Definition: execExpr.h:449
ExprEvalRowtypeCache * outcache
Definition: execExpr.h:611
Datum * argvalue
Definition: execExpr.h:649
struct ExprEvalStep::@55::@68 param
struct ExprEvalStep::@55::@73 sqlvaluefunction
struct ExprEvalStep::@55::@64 boolexpr
ExprEvalRowtypeCache rowcache
Definition: execExpr.h:411
struct JsonConstructorExprState * jcstate
Definition: execExpr.h:656
Oid elemtype
Definition: execExpr.h:474
Oid element_type
Definition: execExpr.h:619
struct ArrayMapState * amstate
Definition: execExpr.h:486
struct ExprEvalStep::@55::@76 arraycoerce
SQLValueFunction * svf
Definition: execExpr.h:458
Expr * expr
Definition: execnodes.h:120
Datum resvalue
Definition: execnodes.h:100
struct ExprEvalStep * steps
Definition: execnodes.h:111
bool resnull
Definition: execnodes.h:98
ParamListInfo ext_params
Definition: execnodes.h:135
struct PlanState * parent
Definition: execnodes.h:134
bool * innermost_casenull
Definition: execnodes.h:138
Datum * innermost_caseval
Definition: execnodes.h:137
int steps_len
Definition: execnodes.h:130
AttrNumber fieldnum
Definition: primnodes.h:1145
Expr * arg
Definition: primnodes.h:1144
List * newvals
Definition: primnodes.h:1176
Expr * arg
Definition: primnodes.h:1175
Oid funcid
Definition: primnodes.h:766
List * args
Definition: primnodes.h:784
fmNodePtr context
Definition: fmgr.h:88
JsonConstructorExpr * constructor
Definition: execExpr.h:814
struct JsonConstructorExprState::@105 * arg_type_cache
JsonReturning * returning
Definition: primnodes.h:1741
JsonConstructorType type
Definition: primnodes.h:1737
Node * formatted_expr
Definition: primnodes.h:1854
JsonExprOp op
Definition: primnodes.h:1848
JsonFormatType format_type
Definition: primnodes.h:1682
JsonFormat * format
Definition: primnodes.h:1694
Expr * formatted_expr
Definition: primnodes.h:1715
Expr * raw_expr
Definition: primnodes.h:1714
List * args
Definition: primnodes.h:1549
MinMaxOp op
Definition: primnodes.h:1547
NullTestType nulltesttype
Definition: primnodes.h:1990
Expr * arg
Definition: primnodes.h:1989
List * args
Definition: primnodes.h:852
ParamCompileHook paramCompile
Definition: params.h:114
int paramid
Definition: primnodes.h:393
Oid paramtype
Definition: primnodes.h:394
ParamKind paramkind
Definition: primnodes.h:392
Expr * arg
Definition: primnodes.h:1200
Expr * retexpr
Definition: primnodes.h:2183
CompareType cmptype
Definition: primnodes.h:1499
List * args
Definition: primnodes.h:1427
SubLinkType subLinkType
Definition: primnodes.h:1081
WindowFunc * wfunc
Definition: execnodes.h:898
ExprState * aggfilter
Definition: execnodes.h:900
List * args
Definition: primnodes.h:591
Expr * aggfilter
Definition: primnodes.h:593
List * args
Definition: primnodes.h:1639
List * named_args
Definition: primnodes.h:1635
Definition: type.h:89
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:212
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1920
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1954
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:386
#define TYPECACHE_CMP_PROC
Definition: typcache.h:140
static void convert(const int32 val, char *const buf)
Definition: zic.c:1992

References ACL_EXECUTE, aclcheck_error(), ACLCHECK_OK, WindowFuncExprState::aggfilter, WindowFunc::aggfilter, ExprEvalStep::aggno, ExprEvalStep::aggref, AggState::aggs, ExprEvalStep::amstate, AND_EXPR, ExprEvalStep::anynull, arg, FieldSelect::arg, FieldStore::arg, RelabelType::arg, CoerceViaIO::arg, ArrayCoerceExpr::arg, CaseExpr::arg, NullTest::arg, BooleanTest::arg, JsonConstructorExprState::arg_nulls, JsonConstructorExprState::arg_type_cache, JsonConstructorExprState::arg_types, JsonConstructorExprState::arg_values, ExprEvalStep::argnull, generate_unaccent_rules::args, FunctionCallInfoBaseData::args, WindowFuncExprState::args, WindowFunc::args, FuncExpr::args, OpExpr::args, ScalarArrayOpExpr::args, BoolExpr::args, CaseExpr::args, RowExpr::args, CoalesceExpr::args, MinMaxExpr::args, XmlExpr::args, JsonConstructorExpr::args, ExprEvalStep::argvalue, ExprEvalStep::arraycoerce, ExprEvalStep::arrayexpr, Assert, ExprEvalStep::attnum, BlessTupleDesc(), ExprEvalStep::boolexpr, BoolExpr::boolop, BooleanTest::booltesttype, BTORDER_PROC, ExprEvalRowtypeCache::cacheptr, ExprEvalStep::casetest, castNode, JsonConstructorExprState::category, check_stack_depth(), ExprEvalStep::clauses, CMD_MERGE, TypeCacheEntry::cmp_proc, ExprEvalStep::cmptype, RowCompareExpr::cmptype, JsonConstructorExpr::coercion, JsonConstructorExprState::constructor, ExprEvalStep::constval, FunctionCallInfoBaseData::context, convert(), ExprEvalStep::convert_rowtype, ExprEvalStep::d, CaseExpr::defresult, EEO_FLAG_HAS_NEW, EEO_FLAG_HAS_OLD, EEO_FLAG_NEW_IS_NULL, EEO_FLAG_OLD_IS_NULL, EEOP_AGGREF, EEOP_ARRAYCOERCE, EEOP_ARRAYEXPR, EEOP_BOOL_AND_STEP, EEOP_BOOL_AND_STEP_FIRST, EEOP_BOOL_AND_STEP_LAST, EEOP_BOOL_NOT_STEP, EEOP_BOOL_OR_STEP, EEOP_BOOL_OR_STEP_FIRST, EEOP_BOOL_OR_STEP_LAST, EEOP_BOOLTEST_IS_FALSE, EEOP_BOOLTEST_IS_NOT_FALSE, EEOP_BOOLTEST_IS_NOT_TRUE, EEOP_BOOLTEST_IS_TRUE, EEOP_CASE_TESTVAL, EEOP_CONST, EEOP_CONVERT_ROWTYPE, EEOP_CURRENTOFEXPR, EEOP_DISTINCT, EEOP_DOMAIN_TESTVAL, EEOP_DONE, EEOP_FIELDSELECT, EEOP_FIELDSTORE_DEFORM, EEOP_FIELDSTORE_FORM, EEOP_GROUPING_FUNC, EEOP_HASHED_SCALARARRAYOP, EEOP_INNER_SYSVAR, EEOP_INNER_VAR, EEOP_IOCOERCE, EEOP_IOCOERCE_SAFE, EEOP_IS_JSON, EEOP_JSON_CONSTRUCTOR, EEOP_JUMP, EEOP_JUMP_IF_NOT_NULL, EEOP_JUMP_IF_NOT_TRUE, EEOP_MAKE_READONLY, EEOP_MERGE_SUPPORT_FUNC, EEOP_MINMAX, EEOP_NEW_SYSVAR, EEOP_NEW_VAR, EEOP_NEXTVALUEEXPR, EEOP_NULLIF, EEOP_NULLTEST_ISNOTNULL, EEOP_NULLTEST_ISNULL, EEOP_NULLTEST_ROWISNOTNULL, EEOP_NULLTEST_ROWISNULL, EEOP_OLD_SYSVAR, EEOP_OLD_VAR, EEOP_OUTER_SYSVAR, EEOP_OUTER_VAR, EEOP_PARAM_EXEC, EEOP_PARAM_EXTERN, EEOP_RETURNINGEXPR, EEOP_ROW, EEOP_ROWCOMPARE_FINAL, EEOP_ROWCOMPARE_STEP, EEOP_SCALARARRAYOP, EEOP_SCAN_SYSVAR, EEOP_SCAN_VAR, EEOP_SQLVALUEFUNCTION, EEOP_WINDOW_FUNC, EEOP_XMLEXPR, ExprEvalStep::elemalign, ExprEvalStep::elembyval, ExprEvalStep::element_type, ArrayExpr::elements, ArrayCoerceExpr::elemexpr, ExprEvalStep::elemexprstate, ExprEvalStep::elemlength, ExprEvalStep::elemnulls, ExprEvalStep::elemtype, ExprEvalStep::elemvalues, elog, ereport, errcode(), errmsg(), ERROR, ExecInitCoerceToDomain(), ExecInitExpr(), ExecInitExprList(), ExecInitExprRec(), ExecInitFunc(), ExecInitJsonExpr(), ExecInitSubPlanExpr(), ExecInitSubscriptingRef(), ExecInitWholeRowVar(), ExecReadyExpr(), ExecTypeFromExprList(), ExecTypeSetColNames(), ExprState::expr, JsonIsPredicate::expr, ExprEvalPushStep(), exprType(), ExprState::ext_params, ExprEvalStep::fcinfo_data, ExprEvalStep::fcinfo_data_in, ExprEvalStep::fcinfo_data_out, ExprEvalStep::fieldnum, FieldSelect::fieldnum, ExprEvalStep::fieldselect, ExprEvalStep::fieldstore, ExprEvalStep::finfo, ExprEvalStep::finfo_in, ExprEvalStep::finfo_out, fmgr_info(), fmgr_info_set_expr, ExprEvalStep::fn_addr, FmgrInfo::fn_addr, forboth, forfive, JsonReturning::format, JsonFormat::format_type, format_type_be(), JsonValueExpr::formatted_expr, JsonExpr::formatted_expr, ExprEvalStep::fstore, ExprEvalStep::func, JsonConstructorExpr::func, FuncExpr::funcid, WindowAggState::funcs, get_element_type(), get_func_name(), get_op_opfamily_properties(), get_opfamily_proc(), get_typlen(), get_typlenbyvalalign(), getTypeInputInfo(), getTypeOutputInfo(), GetUserId(), ExprEvalStep::grouping_func, Agg::groupingSets, ExprEvalStep::hashedscalararrayop, i, ExprEvalStep::incache, ExprEvalStep::inclause, InitFunctionCallInfoData, INNER_VAR, ExprState::innermost_casenull, ExprState::innermost_caseval, ExprEvalStep::inputtype, Int32GetDatum(), InvalidAttrNumber, InvalidOid, InvokeFunctionExecuteHook, ExprEvalStep::iocoerce, IS_FALSE, ExprEvalStep::is_json, IS_NOT_FALSE, IS_NOT_NULL, IS_NOT_TRUE, IS_NOT_UNKNOWN, IS_NULL, IS_TRUE, IS_UNKNOWN, IsA, ExprEvalStep::isnull, NullableDatum::isnull, ExprEvalStep::jcstate, JS_FORMAT_JSONB, JSCTOR_JSON_PARSE, JSCTOR_JSON_SCALAR, JSCTOR_JSON_SERIALIZE, json_categorize_type(), ExprEvalStep::json_constructor, JSON_TABLE_OP, ExprEvalStep::jump, ExprEvalStep::jumpdone, ExprEvalStep::jumpnull, lappend(), lappend_int(), RowCompareExpr::largs, lfirst, lfirst_int, lfirst_oid, linitial, list_length(), lookup_rowtype_tupdesc(), lookup_rowtype_tupdesc_copy(), lookup_type_cache(), lsecond, ExprEvalStep::make_readonly, ExprEvalStep::make_ro, makeNode, makeNullConst(), ExprEvalStep::map, Max, ExprEvalStep::minmax, ExprEvalStep::multidims, MULTIEXPR_SUBLINK, ExprEvalStep::named_argnull, XmlExpr::named_args, ExprEvalStep::named_argvalue, JsonConstructorExprState::nargs, TupleDescData::natts, ExprEvalStep::ncolumns, ExprEvalStep::nelems, FieldStore::newvals, ExprEvalStep::nextvalueexpr, NIL, nodeTag, NOT_EXPR, ExprEvalStep::nullflag, ExprEvalStep::nulls, ExprEvalStep::nulltest_row, NullTest::nulltesttype, WindowAggState::numaggs, WindowAggState::numfuncs, object_aclcheck(), OBJECT_FUNCTION, ObjectIdGetDatum(), OidIsValid, ExprEvalStep::op, MinMaxExpr::op, JsonExpr::op, ExprEvalStep::opcode, OR_EXPR, ExprEvalStep::outcache, OUTER_VAR, JsonConstructorExprState::outfuncid, ExprEvalStep::outputtype, palloc(), palloc0(), ExprEvalStep::param, PARAM_EXEC, PARAM_EXTERN, ParamListInfoData::paramCompile, ExprEvalStep::paramid, Param::paramid, Param::paramkind, ExprEvalStep::paramtype, Param::paramtype, ExprState::parent, ExprEvalStep::pred, RowCompareExpr::rargs, JsonValueExpr::raw_expr, ReleaseTupleDesc, ExprEvalStep::resnull, ExprState::resnull, ExprEvalStep::resultelemtype, ExprEvalStep::resulttype, CoerceViaIO::resulttype, ArrayCoerceExpr::resulttype, ExprEvalStep::resvalue, ExprState::resvalue, ReturningExpr::retexpr, ReturningExpr::retold, JsonConstructorExpr::returning, ExprEvalStep::returningexpr, ExprEvalStep::row, ExprEvalStep::rowcache, ExprEvalStep::rowcompare_final, ExprEvalStep::rowcompare_step, ExprEvalStep::saop, ExprEvalStep::scalararrayop, ExprEvalStep::seqid, NextValueExpr::seqid, ExprEvalStep::seqtypid, SizeForFunctionCallInfo, ExprEvalStep::sqlvaluefunction, ExprState::steps, ExprState::steps_len, SubPlan::subLinkType, ExprEvalStep::svf, ExprEvalStep::tupdesc, TupleDescAttr(), JsonConstructorExpr::type, TYPECACHE_CMP_PROC, NextValueExpr::typeId, JsonConstructorExpr::unique, ExprEvalStep::useOr, ScalarArrayOpExpr::useOr, ExprEvalStep::value, NullableDatum::value, values, ExprEvalStep::values, ExprEvalStep::var, VAR_RETURNING_DEFAULT, VAR_RETURNING_NEW, VAR_RETURNING_OLD, ExprEvalStep::varreturningtype, ExprEvalStep::vartype, ExprEvalStep::wfstate, WindowFuncExprState::wfunc, ExprEvalStep::window_func, ExprEvalStep::xexpr, and ExprEvalStep::xmlexpr.

Referenced by ExecBuildAggTrans(), ExecBuildHash32Expr(), ExecBuildProjectionInfo(), ExecBuildUpdateProjection(), ExecInitCoerceToDomain(), ExecInitExpr(), ExecInitExprRec(), ExecInitExprWithParams(), ExecInitFunc(), ExecInitJsonExpr(), ExecInitQual(), ExecInitSubPlanExpr(), and ExecInitSubscriptingRef().

◆ ExecInitExprWithParams()

ExprState * ExecInitExprWithParams ( Expr node,
ParamListInfo  ext_params 
)

Definition at line 180 of file execExpr.c.

181{
183 ExprEvalStep scratch = {0};
184
185 /* Special case: NULL expression produces a NULL ExprState pointer */
186 if (node == NULL)
187 return NULL;
188
189 /* Initialize ExprState with empty step list */
191 state->expr = node;
192 state->parent = NULL;
193 state->ext_params = ext_params;
194
195 /* Insert setup steps as needed */
197
198 /* Compile the expression proper */
199 ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
200
201 /* Finally, append a DONE step */
202 scratch.opcode = EEOP_DONE;
203 ExprEvalPushStep(state, &scratch);
204
206
207 return state;
208}

References EEOP_DONE, ExecCreateExprSetupSteps(), ExecInitExprRec(), ExecReadyExpr(), ExprEvalPushStep(), makeNode, and ExprEvalStep::opcode.

Referenced by exec_eval_simple_expr(), and InitPartitionPruneContext().

◆ ExecInitFunc()

static void ExecInitFunc ( ExprEvalStep scratch,
Expr node,
List args,
Oid  funcid,
Oid  inputcollid,
ExprState state 
)
static

Definition at line 2696 of file execExpr.c.

2698{
2699 int nargs = list_length(args);
2700 AclResult aclresult;
2701 FmgrInfo *flinfo;
2702 FunctionCallInfo fcinfo;
2703 int argno;
2704 ListCell *lc;
2705
2706 /* Check permission to call function */
2707 aclresult = object_aclcheck(ProcedureRelationId, funcid, GetUserId(), ACL_EXECUTE);
2708 if (aclresult != ACLCHECK_OK)
2709 aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(funcid));
2711
2712 /*
2713 * Safety check on nargs. Under normal circumstances this should never
2714 * fail, as parser should check sooner. But possibly it might fail if
2715 * server has been compiled with FUNC_MAX_ARGS smaller than some functions
2716 * declared in pg_proc?
2717 */
2718 if (nargs > FUNC_MAX_ARGS)
2719 ereport(ERROR,
2720 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
2721 errmsg_plural("cannot pass more than %d argument to a function",
2722 "cannot pass more than %d arguments to a function",
2724 FUNC_MAX_ARGS)));
2725
2726 /* Allocate function lookup data and parameter workspace for this call */
2727 scratch->d.func.finfo = palloc0(sizeof(FmgrInfo));
2729 flinfo = scratch->d.func.finfo;
2730 fcinfo = scratch->d.func.fcinfo_data;
2731
2732 /* Set up the primary fmgr lookup information */
2733 fmgr_info(funcid, flinfo);
2734 fmgr_info_set_expr((Node *) node, flinfo);
2735
2736 /* Initialize function call parameter structure too */
2737 InitFunctionCallInfoData(*fcinfo, flinfo,
2738 nargs, inputcollid, NULL, NULL);
2739
2740 /* Keep extra copies of this info to save an indirection at runtime */
2741 scratch->d.func.fn_addr = flinfo->fn_addr;
2742 scratch->d.func.nargs = nargs;
2743
2744 /* We only support non-set functions here */
2745 if (flinfo->fn_retset)
2746 ereport(ERROR,
2747 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2748 errmsg("set-valued function called in context that cannot accept a set"),
2749 state->parent ?
2750 executor_errposition(state->parent->state,
2751 exprLocation((Node *) node)) : 0));
2752
2753 /* Build code to evaluate arguments directly into the fcinfo struct */
2754 argno = 0;
2755 foreach(lc, args)
2756 {
2757 Expr *arg = (Expr *) lfirst(lc);
2758
2759 if (IsA(arg, Const))
2760 {
2761 /*
2762 * Don't evaluate const arguments every round; especially
2763 * interesting for constants in comparisons.
2764 */
2765 Const *con = (Const *) arg;
2766
2767 fcinfo->args[argno].value = con->constvalue;
2768 fcinfo->args[argno].isnull = con->constisnull;
2769 }
2770 else
2771 {
2773 &fcinfo->args[argno].value,
2774 &fcinfo->args[argno].isnull);
2775 }
2776 argno++;
2777 }
2778
2779 /* Insert appropriate opcode depending on strictness and stats level */
2780 if (pgstat_track_functions <= flinfo->fn_stats)
2781 {
2782 if (flinfo->fn_strict && nargs > 0)
2783 scratch->opcode = EEOP_FUNCEXPR_STRICT;
2784 else
2785 scratch->opcode = EEOP_FUNCEXPR;
2786 }
2787 else
2788 {
2789 if (flinfo->fn_strict && nargs > 0)
2791 else
2792 scratch->opcode = EEOP_FUNCEXPR_FUSAGE;
2793 }
2794}
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1180
@ EEOP_FUNCEXPR_STRICT_FUSAGE
Definition: execExpr.h:122
@ EEOP_FUNCEXPR_STRICT
Definition: execExpr.h:120
@ EEOP_FUNCEXPR
Definition: execExpr.h:119
@ EEOP_FUNCEXPR_FUSAGE
Definition: execExpr.h:121
int executor_errposition(EState *estate, int location)
Definition: execUtils.c:915
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1388
#define FUNC_MAX_ARGS
bool fn_retset
Definition: fmgr.h:62

References ACL_EXECUTE, aclcheck_error(), ACLCHECK_OK, arg, generate_unaccent_rules::args, FunctionCallInfoBaseData::args, ExprEvalStep::d, EEOP_FUNCEXPR, EEOP_FUNCEXPR_FUSAGE, EEOP_FUNCEXPR_STRICT, EEOP_FUNCEXPR_STRICT_FUSAGE, ereport, errcode(), errmsg(), errmsg_plural(), ERROR, ExecInitExprRec(), executor_errposition(), exprLocation(), ExprEvalStep::fcinfo_data, ExprEvalStep::finfo, fmgr_info(), fmgr_info_set_expr, ExprEvalStep::fn_addr, FmgrInfo::fn_addr, FmgrInfo::fn_retset, FmgrInfo::fn_strict, ExprEvalStep::func, FUNC_MAX_ARGS, get_func_name(), GetUserId(), InitFunctionCallInfoData, InvokeFunctionExecuteHook, IsA, NullableDatum::isnull, lfirst, list_length(), ExprEvalStep::nargs, object_aclcheck(), OBJECT_FUNCTION, ExprEvalStep::opcode, palloc0(), SizeForFunctionCallInfo, and NullableDatum::value.

Referenced by ExecInitExprRec().

◆ ExecInitJsonCoercion()

static void ExecInitJsonCoercion ( ExprState state,
JsonReturning returning,
ErrorSaveContext escontext,
bool  omit_quotes,
bool  exists_coerce,
Datum resv,
bool *  resnull 
)
static

Definition at line 5031 of file execExpr.c.

5035{
5036 ExprEvalStep scratch = {0};
5037
5038 /* For json_populate_type() */
5040 scratch.resvalue = resv;
5041 scratch.resnull = resnull;
5042 scratch.d.jsonexpr_coercion.targettype = returning->typid;
5043 scratch.d.jsonexpr_coercion.targettypmod = returning->typmod;
5045 scratch.d.jsonexpr_coercion.escontext = escontext;
5046 scratch.d.jsonexpr_coercion.omit_quotes = omit_quotes;
5047 scratch.d.jsonexpr_coercion.exists_coerce = exists_coerce;
5048 scratch.d.jsonexpr_coercion.exists_cast_to_int = exists_coerce &&
5049 getBaseType(returning->typid) == INT4OID;
5050 scratch.d.jsonexpr_coercion.exists_check_domain = exists_coerce &&
5051 DomainHasConstraints(returning->typid);
5052 ExprEvalPushStep(state, &scratch);
5053}
@ EEOP_JSONEXPR_COERCION
Definition: execExpr.h:262
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2521
void * json_coercion_cache
Definition: execExpr.h:760
bool exists_coerce
Definition: execExpr.h:757
bool exists_cast_to_int
Definition: execExpr.h:758
struct ExprEvalStep::@55::@104 jsonexpr_coercion
bool exists_check_domain
Definition: execExpr.h:759
int32 targettypmod
Definition: execExpr.h:754
Oid targettype
Definition: execExpr.h:753
bool omit_quotes
Definition: execExpr.h:755
bool DomainHasConstraints(Oid type_id)
Definition: typcache.c:1487

References ExprEvalStep::d, DomainHasConstraints(), EEOP_JSONEXPR_COERCION, ExprEvalStep::escontext, ExprEvalStep::exists_cast_to_int, ExprEvalStep::exists_check_domain, ExprEvalStep::exists_coerce, ExprEvalPushStep(), getBaseType(), ExprEvalStep::json_coercion_cache, ExprEvalStep::jsonexpr_coercion, ExprEvalStep::omit_quotes, ExprEvalStep::opcode, ExprEvalStep::resnull, ExprEvalStep::resvalue, ExprEvalStep::targettype, ExprEvalStep::targettypmod, JsonReturning::typid, and JsonReturning::typmod.

Referenced by ExecInitJsonExpr().

◆ ExecInitJsonExpr()

static void ExecInitJsonExpr ( JsonExpr jsexpr,
ExprState state,
Datum resv,
bool *  resnull,
ExprEvalStep scratch 
)
static

Definition at line 4729 of file execExpr.c.

4732{
4733 JsonExprState *jsestate = palloc0(sizeof(JsonExprState));
4734 ListCell *argexprlc;
4735 ListCell *argnamelc;
4736 List *jumps_return_null = NIL;
4737 List *jumps_to_end = NIL;
4738 ListCell *lc;
4739 ErrorSaveContext *escontext;
4740 bool returning_domain =
4741 get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN;
4742
4743 Assert(jsexpr->on_error != NULL);
4744
4745 jsestate->jsexpr = jsexpr;
4746
4747 /*
4748 * Evaluate formatted_expr storing the result into
4749 * jsestate->formatted_expr.
4750 */
4752 &jsestate->formatted_expr.value,
4753 &jsestate->formatted_expr.isnull);
4754
4755 /* JUMP to return NULL if formatted_expr evaluates to NULL */
4756 jumps_return_null = lappend_int(jumps_return_null, state->steps_len);
4757 scratch->opcode = EEOP_JUMP_IF_NULL;
4758 scratch->resnull = &jsestate->formatted_expr.isnull;
4759 scratch->d.jump.jumpdone = -1; /* set below */
4760 ExprEvalPushStep(state, scratch);
4761
4762 /*
4763 * Evaluate pathspec expression storing the result into
4764 * jsestate->pathspec.
4765 */
4766 ExecInitExprRec((Expr *) jsexpr->path_spec, state,
4767 &jsestate->pathspec.value,
4768 &jsestate->pathspec.isnull);
4769
4770 /* JUMP to return NULL if path_spec evaluates to NULL */
4771 jumps_return_null = lappend_int(jumps_return_null, state->steps_len);
4772 scratch->opcode = EEOP_JUMP_IF_NULL;
4773 scratch->resnull = &jsestate->pathspec.isnull;
4774 scratch->d.jump.jumpdone = -1; /* set below */
4775 ExprEvalPushStep(state, scratch);
4776
4777 /* Steps to compute PASSING args. */
4778 jsestate->args = NIL;
4779 forboth(argexprlc, jsexpr->passing_values,
4780 argnamelc, jsexpr->passing_names)
4781 {
4782 Expr *argexpr = (Expr *) lfirst(argexprlc);
4783 String *argname = lfirst_node(String, argnamelc);
4784 JsonPathVariable *var = palloc(sizeof(*var));
4785
4786 var->name = argname->sval;
4787 var->namelen = strlen(var->name);
4788 var->typid = exprType((Node *) argexpr);
4789 var->typmod = exprTypmod((Node *) argexpr);
4790
4791 ExecInitExprRec((Expr *) argexpr, state, &var->value, &var->isnull);
4792
4793 jsestate->args = lappend(jsestate->args, var);
4794 }
4795
4796 /* Step for jsonpath evaluation; see ExecEvalJsonExprPath(). */
4797 scratch->opcode = EEOP_JSONEXPR_PATH;
4798 scratch->resvalue = resv;
4799 scratch->resnull = resnull;
4800 scratch->d.jsonexpr.jsestate = jsestate;
4801 ExprEvalPushStep(state, scratch);
4802
4803 /*
4804 * Step to return NULL after jumping to skip the EEOP_JSONEXPR_PATH step
4805 * when either formatted_expr or pathspec is NULL. Adjust jump target
4806 * addresses of JUMPs that we added above.
4807 */
4808 foreach(lc, jumps_return_null)
4809 {
4810 ExprEvalStep *as = &state->steps[lfirst_int(lc)];
4811
4812 as->d.jump.jumpdone =