Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * execExpr.c
4 : : * Expression evaluation infrastructure.
5 : : *
6 : : * During executor startup, we compile each expression tree (which has
7 : : * previously been processed by the parser and planner) into an ExprState,
8 : : * using ExecInitExpr() et al. This converts the tree into a flat array
9 : : * of ExprEvalSteps, which may be thought of as instructions in a program.
10 : : * At runtime, we'll execute steps, starting with the first, until we reach
11 : : * an EEOP_DONE_{RETURN|NO_RETURN} opcode.
12 : : *
13 : : * This file contains the "compilation" logic. It is independent of the
14 : : * specific execution technology we use (switch statement, computed goto,
15 : : * JIT compilation, etc).
16 : : *
17 : : * See src/backend/executor/README for some background, specifically the
18 : : * "Expression Trees and ExprState nodes", "Expression Initialization",
19 : : * and "Expression Evaluation" sections.
20 : : *
21 : : *
22 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
23 : : * Portions Copyright (c) 1994, Regents of the University of California
24 : : *
25 : : *
26 : : * IDENTIFICATION
27 : : * src/backend/executor/execExpr.c
28 : : *
29 : : *-------------------------------------------------------------------------
30 : : */
31 : : #include "postgres.h"
32 : :
33 : : #include "access/nbtree.h"
34 : : #include "catalog/objectaccess.h"
35 : : #include "catalog/pg_proc.h"
36 : : #include "catalog/pg_type.h"
37 : : #include "executor/execExpr.h"
38 : : #include "executor/nodeSubplan.h"
39 : : #include "funcapi.h"
40 : : #include "jit/jit.h"
41 : : #include "miscadmin.h"
42 : : #include "nodes/makefuncs.h"
43 : : #include "nodes/nodeFuncs.h"
44 : : #include "nodes/subscripting.h"
45 : : #include "optimizer/optimizer.h"
46 : : #include "pgstat.h"
47 : : #include "utils/acl.h"
48 : : #include "utils/array.h"
49 : : #include "utils/builtins.h"
50 : : #include "utils/jsonfuncs.h"
51 : : #include "utils/jsonpath.h"
52 : : #include "utils/lsyscache.h"
53 : : #include "utils/typcache.h"
54 : :
55 : :
56 : : typedef struct ExprSetupInfo
57 : : {
58 : : /*
59 : : * Highest attribute numbers fetched from inner/outer/scan/old/new tuple
60 : : * slots:
61 : : */
62 : : AttrNumber last_inner;
63 : : AttrNumber last_outer;
64 : : AttrNumber last_scan;
65 : : AttrNumber last_old;
66 : : AttrNumber last_new;
67 : : /* MULTIEXPR SubPlan nodes appearing in the expression: */
68 : : List *multiexpr_subplans;
69 : : } ExprSetupInfo;
70 : :
71 : : static void ExecReadyExpr(ExprState *state);
72 : : static void ExecInitExprRec(Expr *node, ExprState *state,
73 : : Datum *resv, bool *resnull);
74 : : static void ExecInitFunc(ExprEvalStep *scratch, Expr *node, List *args,
75 : : Oid funcid, Oid inputcollid,
76 : : ExprState *state);
77 : : static void ExecInitSubPlanExpr(SubPlan *subplan,
78 : : ExprState *state,
79 : : Datum *resv, bool *resnull);
80 : : static void ExecCreateExprSetupSteps(ExprState *state, Node *node);
81 : : static void ExecPushExprSetupSteps(ExprState *state, ExprSetupInfo *info);
82 : : static bool expr_setup_walker(Node *node, ExprSetupInfo *info);
83 : : static bool ExecComputeSlotInfo(ExprState *state, ExprEvalStep *op);
84 : : static void ExecInitWholeRowVar(ExprEvalStep *scratch, Var *variable,
85 : : ExprState *state);
86 : : static void ExecInitSubscriptingRef(ExprEvalStep *scratch,
87 : : SubscriptingRef *sbsref,
88 : : ExprState *state,
89 : : Datum *resv, bool *resnull);
90 : : static bool isAssignmentIndirectionExpr(Expr *expr);
91 : : static void ExecInitCoerceToDomain(ExprEvalStep *scratch, CoerceToDomain *ctest,
92 : : ExprState *state,
93 : : Datum *resv, bool *resnull);
94 : : static void ExecBuildAggTransCall(ExprState *state, AggState *aggstate,
95 : : ExprEvalStep *scratch,
96 : : FunctionCallInfo fcinfo, AggStatePerTrans pertrans,
97 : : int transno, int setno, int setoff, bool ishash,
98 : : bool nullcheck);
99 : : static void ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
100 : : Datum *resv, bool *resnull,
101 : : ExprEvalStep *scratch);
102 : : static void ExecInitJsonCoercion(ExprState *state, JsonReturning *returning,
103 : : ErrorSaveContext *escontext, bool omit_quotes,
104 : : bool exists_coerce,
105 : : Datum *resv, bool *resnull);
106 : :
107 : :
108 : : /*
109 : : * ExecInitExpr: prepare an expression tree for execution
110 : : *
111 : : * This function builds and returns an ExprState implementing the given
112 : : * Expr node tree. The return ExprState can then be handed to ExecEvalExpr
113 : : * for execution. Because the Expr tree itself is read-only as far as
114 : : * ExecInitExpr and ExecEvalExpr are concerned, several different executions
115 : : * of the same plan tree can occur concurrently. (But note that an ExprState
116 : : * does mutate at runtime, so it can't be re-used concurrently.)
117 : : *
118 : : * This must be called in a memory context that will last as long as repeated
119 : : * executions of the expression are needed. Typically the context will be
120 : : * the same as the per-query context of the associated ExprContext.
121 : : *
122 : : * Any Aggref, WindowFunc, or SubPlan nodes found in the tree are added to
123 : : * the lists of such nodes held by the parent PlanState.
124 : : *
125 : : * Note: there is no ExecEndExpr function; we assume that any resource
126 : : * cleanup needed will be handled by just releasing the memory context
127 : : * in which the state tree is built. Functions that require additional
128 : : * cleanup work can register a shutdown callback in the ExprContext.
129 : : *
130 : : * 'node' is the root of the expression tree to compile.
131 : : * 'parent' is the PlanState node that owns the expression.
132 : : *
133 : : * 'parent' may be NULL if we are preparing an expression that is not
134 : : * associated with a plan tree. (If so, it can't have aggs or subplans.)
135 : : * Such cases should usually come through ExecPrepareExpr, not directly here.
136 : : *
137 : : * Also, if 'node' is NULL, we just return NULL. This is convenient for some
138 : : * callers that may or may not have an expression that needs to be compiled.
139 : : * Note that a NULL ExprState pointer *cannot* be handed to ExecEvalExpr,
140 : : * although ExecQual and ExecCheck will accept one (and treat it as "true").
141 : : */
142 : : ExprState *
3395 andres@anarazel.de 143 :CBC 659047 : ExecInitExpr(Expr *node, PlanState *parent)
144 : : {
145 : : ExprState *state;
1398 andrew@dunslane.net 146 : 659047 : ExprEvalStep scratch = {0};
147 : :
148 : : /* Special case: NULL expression produces a NULL ExprState pointer */
149 [ + + ]: 659047 : if (node == NULL)
150 : 36540 : return NULL;
151 : :
152 : : /* Initialize ExprState with empty step list */
153 : 622507 : state = makeNode(ExprState);
154 : 622507 : state->expr = node;
155 : 622507 : state->parent = parent;
156 : 622507 : state->ext_params = NULL;
157 : :
158 : : /* Insert setup steps as needed */
1221 tgl@sss.pgh.pa.us 159 : 622507 : ExecCreateExprSetupSteps(state, (Node *) node);
160 : :
161 : : /* Compile the expression proper */
1398 andrew@dunslane.net 162 : 622507 : ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
163 : :
164 : : /* Finally, append a DONE step */
476 dgustafsson@postgres 165 : 622487 : scratch.opcode = EEOP_DONE_RETURN;
1398 andrew@dunslane.net 166 : 622487 : ExprEvalPushStep(state, &scratch);
167 : :
168 : 622487 : ExecReadyExpr(state);
169 : :
170 : 622487 : return state;
171 : : }
172 : :
173 : : /*
174 : : * ExecInitExprWithParams: prepare a standalone expression tree for execution
175 : : *
176 : : * This is the same as ExecInitExpr, except that there is no parent PlanState,
177 : : * and instead we may have a ParamListInfo describing PARAM_EXTERN Params.
178 : : */
179 : : ExprState *
3113 tgl@sss.pgh.pa.us 180 : 53702 : ExecInitExprWithParams(Expr *node, ParamListInfo ext_params)
181 : : {
182 : : ExprState *state;
1398 andrew@dunslane.net 183 : 53702 : ExprEvalStep scratch = {0};
184 : :
185 : : /* Special case: NULL expression produces a NULL ExprState pointer */
186 [ - + ]: 53702 : if (node == NULL)
1398 andrew@dunslane.net 187 :UBC 0 : return NULL;
188 : :
189 : : /* Initialize ExprState with empty step list */
1398 andrew@dunslane.net 190 :CBC 53702 : state = makeNode(ExprState);
191 : 53702 : state->expr = node;
192 : 53702 : state->parent = NULL;
193 : 53702 : state->ext_params = ext_params;
194 : :
195 : : /* Insert setup steps as needed */
1221 tgl@sss.pgh.pa.us 196 : 53702 : ExecCreateExprSetupSteps(state, (Node *) node);
197 : :
198 : : /* Compile the expression proper */
1398 andrew@dunslane.net 199 : 53702 : ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
200 : :
201 : : /* Finally, append a DONE step */
476 dgustafsson@postgres 202 : 53702 : scratch.opcode = EEOP_DONE_RETURN;
1398 andrew@dunslane.net 203 : 53702 : ExprEvalPushStep(state, &scratch);
204 : :
205 : 53702 : ExecReadyExpr(state);
206 : :
207 : 53702 : return state;
208 : : }
209 : :
210 : : /*
211 : : * ExecInitQual: prepare a qual for execution by ExecQual
212 : : *
213 : : * Prepares for the evaluation of a conjunctive boolean expression (qual list
214 : : * with implicit AND semantics) that returns true if none of the
215 : : * subexpressions are false.
216 : : *
217 : : * We must return true if the list is empty. Since that's a very common case,
218 : : * we optimize it a bit further by translating to a NULL ExprState pointer
219 : : * rather than setting up an ExprState that computes constant TRUE. (Some
220 : : * especially hot-spot callers of ExecQual detect this and avoid calling
221 : : * ExecQual at all.)
222 : : *
223 : : * If any of the subexpressions yield NULL, then the result of the conjunction
224 : : * is false. This makes ExecQual primarily useful for evaluating WHERE
225 : : * clauses, since SQL specifies that tuples with null WHERE results do not
226 : : * get selected.
227 : : */
228 : : ExprState *
3395 andres@anarazel.de 229 : 1139584 : ExecInitQual(List *qual, PlanState *parent)
230 : : {
231 : : ExprState *state;
3080 232 : 1139584 : ExprEvalStep scratch = {0};
3395 233 : 1139584 : List *adjust_jumps = NIL;
234 : :
235 : : /* short-circuit (here and in ExecQual) for empty restriction list */
236 [ + + ]: 1139584 : if (qual == NIL)
237 : 829372 : return NULL;
238 : :
239 [ - + ]: 310212 : Assert(IsA(qual, List));
240 : :
241 : 310212 : state = makeNode(ExprState);
242 : 310212 : state->expr = (Expr *) qual;
3113 tgl@sss.pgh.pa.us 243 : 310212 : state->parent = parent;
244 : 310212 : state->ext_params = NULL;
245 : :
246 : : /* mark expression as to be used with ExecQual() */
3395 andres@anarazel.de 247 : 310212 : state->flags = EEO_FLAG_IS_QUAL;
248 : :
249 : : /* Insert setup steps as needed */
1221 tgl@sss.pgh.pa.us 250 : 310212 : ExecCreateExprSetupSteps(state, (Node *) qual);
251 : :
252 : : /*
253 : : * ExecQual() needs to return false for an expression returning NULL. That
254 : : * allows us to short-circuit the evaluation the first time a NULL is
255 : : * encountered. As qual evaluation is a hot-path this warrants using a
256 : : * special opcode for qual evaluation that's simpler than BOOL_AND (which
257 : : * has more complex NULL handling).
258 : : */
3395 andres@anarazel.de 259 : 310212 : scratch.opcode = EEOP_QUAL;
260 : :
261 : : /*
262 : : * We can use ExprState's resvalue/resnull as target for each qual expr.
263 : : */
264 : 310212 : scratch.resvalue = &state->resvalue;
265 : 310212 : scratch.resnull = &state->resnull;
266 : :
908 nathan@postgresql.or 267 [ + - + + : 1004595 : foreach_ptr(Expr, node, qual)
+ + ]
268 : : {
269 : : /* first evaluate expression */
3113 tgl@sss.pgh.pa.us 270 : 384171 : ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
271 : :
272 : : /* then emit EEOP_QUAL to detect if it's false (or null) */
3395 andres@anarazel.de 273 : 384171 : scratch.d.qualexpr.jumpdone = -1;
274 : 384171 : ExprEvalPushStep(state, &scratch);
275 : 384171 : adjust_jumps = lappend_int(adjust_jumps,
276 : 384171 : state->steps_len - 1);
277 : : }
278 : :
279 : : /* adjust jump targets */
908 nathan@postgresql.or 280 [ + - + + : 1004595 : foreach_int(jump, adjust_jumps)
+ + ]
281 : : {
282 : 384171 : ExprEvalStep *as = &state->steps[jump];
283 : :
3395 andres@anarazel.de 284 [ - + ]: 384171 : Assert(as->opcode == EEOP_QUAL);
285 [ - + ]: 384171 : Assert(as->d.qualexpr.jumpdone == -1);
286 : 384171 : as->d.qualexpr.jumpdone = state->steps_len;
287 : : }
288 : :
289 : : /*
290 : : * At the end, we don't need to do anything more. The last qual expr must
291 : : * have yielded TRUE, and since its result is stored in the desired output
292 : : * location, we're done.
293 : : */
476 dgustafsson@postgres 294 : 310212 : scratch.opcode = EEOP_DONE_RETURN;
3395 andres@anarazel.de 295 : 310212 : ExprEvalPushStep(state, &scratch);
296 : :
297 : 310212 : ExecReadyExpr(state);
298 : :
299 : 310212 : return state;
300 : : }
301 : :
302 : : /*
303 : : * ExecInitCheck: prepare a check constraint for execution by ExecCheck
304 : : *
305 : : * This is much like ExecInitQual/ExecQual, except that a null result from
306 : : * the conjunction is treated as TRUE. This behavior is appropriate for
307 : : * evaluating CHECK constraints, since SQL specifies that NULL constraint
308 : : * conditions are not failures.
309 : : *
310 : : * Note that like ExecInitQual, this expects input in implicit-AND format.
311 : : * Users of ExecCheck that have expressions in normal explicit-AND format
312 : : * can just apply ExecInitExpr to produce suitable input for ExecCheck.
313 : : */
314 : : ExprState *
315 : 2522 : ExecInitCheck(List *qual, PlanState *parent)
316 : : {
317 : : /* short-circuit (here and in ExecCheck) for empty restriction list */
318 [ + + ]: 2522 : if (qual == NIL)
319 : 112 : return NULL;
320 : :
321 [ - + ]: 2410 : 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 : 2410 : return ExecInitExpr(make_ands_explicit(qual), parent);
329 : : }
330 : :
331 : : /*
332 : : * Call ExecInitExpr() on a list of expressions, return a list of ExprStates.
333 : : */
334 : : List *
335 : 303913 : ExecInitExprList(List *nodes, PlanState *parent)
336 : : {
337 : 303913 : List *result = NIL;
338 : : ListCell *lc;
339 : :
340 [ + + + + : 577199 : foreach(lc, nodes)
+ + ]
341 : : {
342 : 273286 : Expr *e = lfirst(lc);
343 : :
344 : 273286 : result = lappend(result, ExecInitExpr(e, parent));
345 : : }
346 : :
347 : 303913 : return result;
348 : : }
349 : :
350 : : /*
351 : : * ExecBuildProjectionInfo
352 : : *
353 : : * Build a ProjectionInfo node for evaluating the given tlist in the given
354 : : * econtext, and storing the result into the tuple slot. (Caller must have
355 : : * ensured that tuple slot has a descriptor matching the tlist!)
356 : : *
357 : : * inputDesc can be NULL, but if it is not, we check to see whether simple
358 : : * Vars in the tlist match the descriptor. It is important to provide
359 : : * inputDesc for relation-scan plan nodes, as a cross check that the relation
360 : : * hasn't been changed since the plan was made. At higher levels of a plan,
361 : : * there is no need to recheck.
362 : : *
363 : : * This is implemented by internally building an ExprState that performs the
364 : : * whole projection in one go.
365 : : *
366 : : * Caution: before PG v10, the targetList was a list of ExprStates; now it
367 : : * should be the planner-created targetlist, since we do the compilation here.
368 : : */
369 : : ProjectionInfo *
370 : 482987 : ExecBuildProjectionInfo(List *targetList,
371 : : ExprContext *econtext,
372 : : TupleTableSlot *slot,
373 : : PlanState *parent,
374 : : TupleDesc inputDesc)
375 : : {
376 : 482987 : ProjectionInfo *projInfo = makeNode(ProjectionInfo);
377 : : ExprState *state;
3080 378 : 482987 : ExprEvalStep scratch = {0};
379 : : ListCell *lc;
380 : :
3395 381 : 482987 : projInfo->pi_exprContext = econtext;
382 : : /* We embed ExprState into ProjectionInfo instead of doing extra palloc */
1805 peter@eisentraut.org 383 : 482987 : projInfo->pi_state.type = T_ExprState;
3395 andres@anarazel.de 384 : 482987 : state = &projInfo->pi_state;
385 : 482987 : state->expr = (Expr *) targetList;
3113 tgl@sss.pgh.pa.us 386 : 482987 : state->parent = parent;
387 : 482987 : state->ext_params = NULL;
388 : :
3395 andres@anarazel.de 389 : 482987 : state->resultslot = slot;
390 : :
391 : : /* Insert setup steps as needed */
1221 tgl@sss.pgh.pa.us 392 : 482987 : ExecCreateExprSetupSteps(state, (Node *) targetList);
393 : :
394 : : /* Now compile each tlist column */
3395 andres@anarazel.de 395 [ + + + + : 1790116 : foreach(lc, targetList)
+ + ]
396 : : {
3368 tgl@sss.pgh.pa.us 397 : 1307169 : TargetEntry *tle = lfirst_node(TargetEntry, lc);
3395 andres@anarazel.de 398 : 1307169 : Var *variable = NULL;
399 : 1307169 : AttrNumber attnum = 0;
400 : 1307169 : 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 [ + - ]: 1307169 : if (tle->expr != NULL &&
411 [ + + ]: 1307169 : IsA(tle->expr, Var) &&
412 [ + + ]: 811124 : ((Var *) tle->expr)->varattno > 0)
413 : : {
414 : : /* Non-system Var, but how safe is it? */
415 : 753256 : variable = (Var *) tle->expr;
416 : 753256 : attnum = variable->varattno;
417 : :
418 [ + + ]: 753256 : if (inputDesc == NULL)
3296 tgl@sss.pgh.pa.us 419 : 444377 : isSafeVar = true; /* can't check, just assume OK */
3395 andres@anarazel.de 420 [ + + ]: 308879 : else if (attnum <= inputDesc->natts)
421 : : {
3236 422 : 308452 : 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 : : */
3395 429 [ + + + + ]: 308452 : if (!attr->attisdropped && variable->vartype == attr->atttypid)
430 : : {
431 : 307841 : isSafeVar = true;
432 : : }
433 : : }
434 : : }
435 : :
436 [ + + ]: 1307169 : if (isSafeVar)
437 : : {
438 : : /* Fast-path: just generate an EEOP_ASSIGN_*_VAR step */
439 [ + + + ]: 752218 : switch (variable->varno)
440 : : {
441 : 137556 : case INNER_VAR:
442 : : /* get the tuple from the inner node */
443 : 137556 : scratch.opcode = EEOP_ASSIGN_INNER_VAR;
444 : 137556 : break;
445 : :
446 : 305879 : case OUTER_VAR:
447 : : /* get the tuple from the outer node */
448 : 305879 : scratch.opcode = EEOP_ASSIGN_OUTER_VAR;
449 : 305879 : break;
450 : :
451 : : /* INDEX_VAR is handled by default case */
452 : :
453 : 308783 : 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 : : */
530 dean.a.rasheed@gmail 459 [ + + + - ]: 308783 : switch (variable->varreturningtype)
460 : : {
461 : 307397 : case VAR_RETURNING_DEFAULT:
462 : 307397 : scratch.opcode = EEOP_ASSIGN_SCAN_VAR;
463 : 307397 : break;
464 : 692 : case VAR_RETURNING_OLD:
465 : 692 : scratch.opcode = EEOP_ASSIGN_OLD_VAR;
466 : 692 : state->flags |= EEO_FLAG_HAS_OLD;
467 : 692 : break;
468 : 694 : case VAR_RETURNING_NEW:
469 : 694 : scratch.opcode = EEOP_ASSIGN_NEW_VAR;
470 : 694 : state->flags |= EEO_FLAG_HAS_NEW;
471 : 694 : break;
472 : : }
3395 andres@anarazel.de 473 : 308783 : break;
474 : : }
475 : :
476 : 752218 : scratch.d.assign_var.attnum = attnum - 1;
477 : 752218 : scratch.d.assign_var.resultnum = tle->resno - 1;
478 : 752218 : 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 : : */
3113 tgl@sss.pgh.pa.us 490 : 554951 : ExecInitExprRec(tle->expr, state,
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 : : */
3395 andres@anarazel.de 497 [ + + ]: 554915 : if (get_typlen(exprType((Node *) tle->expr)) == -1)
498 : 204660 : scratch.opcode = EEOP_ASSIGN_TMP_MAKE_RO;
499 : : else
500 : 350255 : scratch.opcode = EEOP_ASSIGN_TMP;
501 : 554915 : scratch.d.assign_tmp.resultnum = tle->resno - 1;
502 : 554915 : ExprEvalPushStep(state, &scratch);
503 : : }
504 : : }
505 : :
476 dgustafsson@postgres 506 : 482947 : scratch.opcode = EEOP_DONE_NO_RETURN;
3395 andres@anarazel.de 507 : 482947 : ExprEvalPushStep(state, &scratch);
508 : :
509 : 482947 : ExecReadyExpr(state);
510 : :
511 : 482947 : return projInfo;
512 : : }
513 : :
514 : : /*
515 : : * ExecBuildUpdateProjection
516 : : *
517 : : * Build a ProjectionInfo node for constructing a new tuple during UPDATE.
518 : : * The projection will be executed in the given econtext and the result will
519 : : * be stored into the given tuple slot. (Caller must have ensured that tuple
520 : : * slot has a descriptor matching the target rel!)
521 : : *
522 : : * When evalTargetList is false, targetList contains the UPDATE ... SET
523 : : * expressions that have already been computed by a subplan node; the values
524 : : * from this tlist are assumed to be available in the "outer" tuple slot.
525 : : * When evalTargetList is true, targetList contains the UPDATE ... SET
526 : : * expressions that must be computed (which could contain references to
527 : : * the outer, inner, or scan tuple slots).
528 : : *
529 : : * In either case, targetColnos contains a list of the target column numbers
530 : : * corresponding to the non-resjunk entries of targetList. The tlist values
531 : : * are assigned into these columns of the result tuple slot. Target columns
532 : : * not listed in targetColnos are filled from the UPDATE's old tuple, which
533 : : * is assumed to be available in the "scan" tuple slot.
534 : : *
535 : : * targetList can also contain resjunk columns. These must be evaluated
536 : : * if evalTargetList is true, but their values are discarded.
537 : : *
538 : : * relDesc must describe the relation we intend to update.
539 : : *
540 : : * This is basically a specialized variant of ExecBuildProjectionInfo.
541 : : * However, it also performs sanity checks equivalent to ExecCheckPlanOutput.
542 : : * Since we never make a normal tlist equivalent to the whole
543 : : * tuple-to-be-assigned, there is no convenient way to apply
544 : : * ExecCheckPlanOutput, so we must do our safety checks here.
545 : : */
546 : : ProjectionInfo *
1877 tgl@sss.pgh.pa.us 547 : 10502 : ExecBuildUpdateProjection(List *targetList,
548 : : bool evalTargetList,
549 : : List *targetColnos,
550 : : TupleDesc relDesc,
551 : : ExprContext *econtext,
552 : : TupleTableSlot *slot,
553 : : PlanState *parent)
554 : : {
1917 555 : 10502 : ProjectionInfo *projInfo = makeNode(ProjectionInfo);
556 : : ExprState *state;
557 : : int nAssignableCols;
558 : : bool sawJunk;
559 : : Bitmapset *assignedCols;
530 dean.a.rasheed@gmail 560 : 10502 : ExprSetupInfo deform = {0, 0, 0, 0, 0, NIL};
1917 tgl@sss.pgh.pa.us 561 : 10502 : ExprEvalStep scratch = {0};
562 : : int outerattnum;
563 : : ListCell *lc,
564 : : *lc2;
565 : :
566 : 10502 : projInfo->pi_exprContext = econtext;
567 : : /* We embed ExprState into ProjectionInfo instead of doing extra palloc */
1805 peter@eisentraut.org 568 : 10502 : projInfo->pi_state.type = T_ExprState;
1917 tgl@sss.pgh.pa.us 569 : 10502 : state = &projInfo->pi_state;
1877 570 [ + + ]: 10502 : if (evalTargetList)
571 : 1768 : state->expr = (Expr *) targetList;
572 : : else
573 : 8734 : state->expr = NULL; /* not used */
1917 574 : 10502 : state->parent = parent;
575 : 10502 : state->ext_params = NULL;
576 : :
577 : 10502 : 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 : 10502 : nAssignableCols = 0;
584 : 10502 : sawJunk = false;
1877 585 [ + - + + : 35410 : foreach(lc, targetList)
+ + ]
586 : : {
1917 587 : 24908 : TargetEntry *tle = lfirst_node(TargetEntry, lc);
588 : :
589 [ + + ]: 24908 : if (tle->resjunk)
590 : 11074 : sawJunk = true;
591 : : else
592 : : {
593 [ - + ]: 13834 : if (sawJunk)
1917 tgl@sss.pgh.pa.us 594 [ # # ]:UBC 0 : elog(ERROR, "subplan target list is out of order");
1917 tgl@sss.pgh.pa.us 595 :CBC 13834 : nAssignableCols++;
596 : : }
597 : : }
598 : :
599 : : /* We should have one targetColnos entry per non-junk column */
600 [ - + ]: 10502 : if (nAssignableCols != list_length(targetColnos))
1917 tgl@sss.pgh.pa.us 601 [ # # ]:UBC 0 : 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 : : */
1917 tgl@sss.pgh.pa.us 608 :CBC 10502 : assignedCols = NULL;
609 [ + + + + : 24336 : foreach(lc, targetColnos)
+ + ]
610 : : {
611 : 13834 : AttrNumber targetattnum = lfirst_int(lc);
612 : :
613 : 13834 : 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 [ + + ]: 18229 : for (int attnum = relDesc->natts; attnum > 0; attnum--)
622 : : {
557 drowley@postgresql.o 623 : 16580 : CompactAttribute *attr = TupleDescCompactAttr(relDesc, attnum - 1);
624 : :
1917 tgl@sss.pgh.pa.us 625 [ + + ]: 16580 : if (attr->attisdropped)
626 : 148 : continue;
627 [ + + ]: 16432 : if (bms_is_member(attnum, assignedCols))
628 : 7579 : continue;
629 : 8853 : deform.last_scan = attnum;
630 : 8853 : 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 : : */
1877 638 [ + + ]: 10502 : if (evalTargetList)
1221 639 : 1768 : expr_setup_walker((Node *) targetList, &deform);
640 : : else
1877 641 : 8734 : deform.last_outer = nAssignableCols;
642 : :
1221 643 : 10502 : ExecPushExprSetupSteps(state, &deform);
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 : : */
1917 652 : 10502 : outerattnum = 0;
1877 653 [ + - + + : 24336 : forboth(lc, targetList, lc2, targetColnos)
+ + + + +
+ + + +
+ ]
654 : : {
1917 655 : 13834 : TargetEntry *tle = lfirst_node(TargetEntry, lc);
656 : 13834 : AttrNumber targetattnum = lfirst_int(lc2);
657 : : Form_pg_attribute attr;
658 : :
659 [ - + ]: 13834 : Assert(!tle->resjunk);
660 : :
661 : : /*
662 : : * Apply sanity checks comparable to ExecCheckPlanOutput().
663 : : */
664 [ + - - + ]: 13834 : if (targetattnum <= 0 || targetattnum > relDesc->natts)
1917 tgl@sss.pgh.pa.us 665 [ # # ]:UBC 0 : ereport(ERROR,
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.")));
1917 tgl@sss.pgh.pa.us 669 :CBC 13834 : attr = TupleDescAttr(relDesc, targetattnum - 1);
670 : :
671 [ - + ]: 13834 : if (attr->attisdropped)
1917 tgl@sss.pgh.pa.us 672 [ # # ]:UBC 0 : ereport(ERROR,
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)));
1917 tgl@sss.pgh.pa.us 677 [ - + ]:CBC 13834 : if (exprType((Node *) tle->expr) != attr->atttypid)
1917 tgl@sss.pgh.pa.us 678 [ # # ]:UBC 0 : ereport(ERROR,
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. */
1877 tgl@sss.pgh.pa.us 687 [ + + ]:CBC 13834 : 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 : : */
695 : 2387 : ExecInitExprRec(tle->expr, state,
696 : : &state->resvalue, &state->resnull);
697 : : /* Needn't worry about read-only-ness here, either. */
698 : 2387 : scratch.opcode = EEOP_ASSIGN_TMP;
699 : 2387 : scratch.d.assign_tmp.resultnum = targetattnum - 1;
700 : 2387 : ExprEvalPushStep(state, &scratch);
701 : : }
702 : : else
703 : : {
704 : : /* Just assign from the outer tuple. */
705 : 11447 : scratch.opcode = EEOP_ASSIGN_OUTER_VAR;
706 : 11447 : scratch.d.assign_var.attnum = outerattnum;
707 : 11447 : scratch.d.assign_var.resultnum = targetattnum - 1;
708 : 11447 : ExprEvalPushStep(state, &scratch);
709 : : }
710 : 13834 : 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 : : */
1917 717 [ + + ]: 92488 : for (int attnum = 1; attnum <= relDesc->natts; attnum++)
718 : : {
557 drowley@postgresql.o 719 : 81986 : CompactAttribute *attr = TupleDescCompactAttr(relDesc, attnum - 1);
720 : :
1917 tgl@sss.pgh.pa.us 721 [ + + ]: 81986 : if (attr->attisdropped)
722 : : {
723 : : /* Put a null into the ExprState's resvalue/resnull ... */
724 : 325 : scratch.opcode = EEOP_CONST;
725 : 325 : scratch.resvalue = &state->resvalue;
726 : 325 : scratch.resnull = &state->resnull;
727 : 325 : scratch.d.constval.value = (Datum) 0;
728 : 325 : scratch.d.constval.isnull = true;
729 : 325 : ExprEvalPushStep(state, &scratch);
730 : : /* ... then assign it to the result slot */
731 : 325 : scratch.opcode = EEOP_ASSIGN_TMP;
732 : 325 : scratch.d.assign_tmp.resultnum = attnum - 1;
733 : 325 : ExprEvalPushStep(state, &scratch);
734 : : }
735 [ + + ]: 81661 : else if (!bms_is_member(attnum, assignedCols))
736 : : {
737 : : /* Certainly the right type, so needn't check */
738 : 67827 : scratch.opcode = EEOP_ASSIGN_SCAN_VAR;
739 : 67827 : scratch.d.assign_var.attnum = attnum - 1;
740 : 67827 : scratch.d.assign_var.resultnum = attnum - 1;
741 : 67827 : ExprEvalPushStep(state, &scratch);
742 : : }
743 : : }
744 : :
476 dgustafsson@postgres 745 : 10502 : scratch.opcode = EEOP_DONE_NO_RETURN;
1917 tgl@sss.pgh.pa.us 746 : 10502 : ExprEvalPushStep(state, &scratch);
747 : :
748 : 10502 : ExecReadyExpr(state);
749 : :
750 : 10502 : return projInfo;
751 : : }
752 : :
753 : : /*
754 : : * ExecPrepareExpr --- initialize for expression execution outside a normal
755 : : * Plan tree context.
756 : : *
757 : : * This differs from ExecInitExpr in that we don't assume the caller is
758 : : * already running in the EState's per-query context. Also, we run the
759 : : * passed expression tree through expression_planner() to prepare it for
760 : : * execution. (In ordinary Plan trees the regular planning process will have
761 : : * made the appropriate transformations on expressions, but for standalone
762 : : * expressions this won't have happened.)
763 : : */
764 : : ExprState *
3395 andres@anarazel.de 765 : 16916 : ExecPrepareExpr(Expr *node, EState *estate)
766 : : {
767 : : ExprState *result;
768 : : MemoryContext oldcontext;
769 : :
770 : 16916 : oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
771 : :
772 : 16916 : node = expression_planner(node);
773 : :
22 andrew@dunslane.net 774 : 16912 : result = ExecInitExpr(node, NULL);
775 : :
3395 andres@anarazel.de 776 : 16908 : MemoryContextSwitchTo(oldcontext);
777 : :
778 : 16908 : return result;
779 : : }
780 : :
781 : : /*
782 : : * ExecPrepareQual --- initialize for qual execution outside a normal
783 : : * Plan tree context.
784 : : *
785 : : * This differs from ExecInitQual in that we don't assume the caller is
786 : : * already running in the EState's per-query context. Also, we run the
787 : : * passed expression tree through expression_planner() to prepare it for
788 : : * execution. (In ordinary Plan trees the regular planning process will have
789 : : * made the appropriate transformations on expressions, but for standalone
790 : : * expressions this won't have happened.)
791 : : */
792 : : ExprState *
793 : 37744 : ExecPrepareQual(List *qual, EState *estate)
794 : : {
795 : : ExprState *result;
796 : : MemoryContext oldcontext;
797 : :
798 : 37744 : oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
799 : :
800 : 37744 : qual = (List *) expression_planner((Expr *) qual);
801 : :
802 : 37744 : result = ExecInitQual(qual, NULL);
803 : :
804 : 37744 : MemoryContextSwitchTo(oldcontext);
805 : :
806 : 37744 : return result;
807 : : }
808 : :
809 : : /*
810 : : * ExecPrepareCheck -- initialize check constraint for execution outside a
811 : : * normal Plan tree context.
812 : : *
813 : : * See ExecPrepareExpr() and ExecInitCheck() for details.
814 : : */
815 : : ExprState *
816 : 2522 : ExecPrepareCheck(List *qual, EState *estate)
817 : : {
818 : : ExprState *result;
819 : : MemoryContext oldcontext;
820 : :
821 : 2522 : oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
822 : :
823 : 2522 : qual = (List *) expression_planner((Expr *) qual);
824 : :
825 : 2522 : result = ExecInitCheck(qual, NULL);
826 : :
827 : 2522 : MemoryContextSwitchTo(oldcontext);
828 : :
829 : 2522 : return result;
830 : : }
831 : :
832 : : /*
833 : : * Call ExecPrepareExpr() on each member of a list of Exprs, and return
834 : : * a list of ExprStates.
835 : : *
836 : : * See ExecPrepareExpr() for details.
837 : : */
838 : : List *
839 : 9385 : ExecPrepareExprList(List *nodes, EState *estate)
840 : : {
841 : 9385 : List *result = NIL;
842 : : MemoryContext oldcontext;
843 : : ListCell *lc;
844 : :
845 : : /* Ensure that the list cell nodes are in the right context too */
3371 tgl@sss.pgh.pa.us 846 : 9385 : oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
847 : :
3395 andres@anarazel.de 848 [ + + + + : 19125 : foreach(lc, nodes)
+ + ]
849 : : {
850 : 9740 : Expr *e = (Expr *) lfirst(lc);
851 : :
852 : 9740 : result = lappend(result, ExecPrepareExpr(e, estate));
853 : : }
854 : :
3371 tgl@sss.pgh.pa.us 855 : 9385 : MemoryContextSwitchTo(oldcontext);
856 : :
3395 andres@anarazel.de 857 : 9385 : return result;
858 : : }
859 : :
860 : : /*
861 : : * ExecCheck - evaluate a check constraint
862 : : *
863 : : * For check constraints, a null result is taken as TRUE, ie the constraint
864 : : * passes.
865 : : *
866 : : * The check constraint may have been prepared with ExecInitCheck
867 : : * (possibly via ExecPrepareCheck) if the caller had it in implicit-AND
868 : : * format, but a regular boolean expression prepared with ExecInitExpr or
869 : : * ExecPrepareExpr works too.
870 : : */
871 : : bool
872 : 71637 : ExecCheck(ExprState *state, ExprContext *econtext)
873 : : {
874 : : Datum ret;
875 : : bool isnull;
876 : :
877 : : /* short-circuit (here and in ExecInitCheck) for empty restriction list */
878 [ + + ]: 71637 : if (state == NULL)
879 : 112 : return true;
880 : :
881 : : /* verify that expression was not compiled using ExecInitQual */
882 [ - + ]: 71525 : Assert(!(state->flags & EEO_FLAG_IS_QUAL));
883 : :
884 : 71525 : ret = ExecEvalExprSwitchContext(state, econtext, &isnull);
885 : :
886 [ + + ]: 71521 : if (isnull)
887 : 1893 : return true;
888 : :
889 : 69628 : return DatumGetBool(ret);
890 : : }
891 : :
892 : : /*
893 : : * Prepare a compiled expression for execution. This has to be called for
894 : : * every ExprState before it can be executed.
895 : : *
896 : : * NB: While this currently only calls ExecReadyInterpretedExpr(),
897 : : * this will likely get extended to further expression evaluation methods.
898 : : * Therefore this should be used instead of directly calling
899 : : * ExecReadyInterpretedExpr().
900 : : */
901 : : static void
902 : 1590014 : ExecReadyExpr(ExprState *state)
903 : : {
3024 904 [ - + ]: 1590014 : if (jit_compile_expr(state))
3024 andres@anarazel.de 905 :UBC 0 : return;
906 : :
3395 andres@anarazel.de 907 :CBC 1590014 : ExecReadyInterpretedExpr(state);
908 : : }
909 : :
910 : : /*
911 : : * Append the steps necessary for the evaluation of node to ExprState->steps,
912 : : * possibly recursing into sub-expressions of node.
913 : : *
914 : : * node - expression to evaluate
915 : : * state - ExprState to whose ->steps to append the necessary operations
916 : : * resv / resnull - where to store the result of the node into
917 : : */
918 : : static void
3113 tgl@sss.pgh.pa.us 919 : 3435901 : ExecInitExprRec(Expr *node, ExprState *state,
920 : : Datum *resv, bool *resnull)
921 : : {
3080 andres@anarazel.de 922 : 3435901 : ExprEvalStep scratch = {0};
923 : :
924 : : /* Guard against stack overflow due to overly complex expressions */
3395 925 : 3435901 : check_stack_depth();
926 : :
927 : : /* Step's output location is always what the caller gave us */
928 [ + - - + ]: 3435901 : Assert(resv != NULL && resnull != NULL);
929 : 3435901 : scratch.resvalue = resv;
930 : 3435901 : scratch.resnull = resnull;
931 : :
932 : : /* cases should be ordered as they are in enum NodeTag */
933 [ + + + + : 3435901 : switch (nodeTag(node))
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + - ]
934 : : {
935 : 905115 : case T_Var:
936 : : {
937 : 905115 : Var *variable = (Var *) node;
938 : :
939 [ + + ]: 905115 : if (variable->varattno == InvalidAttrNumber)
940 : : {
941 : : /* whole-row Var */
3113 tgl@sss.pgh.pa.us 942 : 3431 : ExecInitWholeRowVar(&scratch, variable, state);
943 : : }
3395 andres@anarazel.de 944 [ + + ]: 901684 : else if (variable->varattno <= 0)
945 : : {
946 : : /* system column */
947 : 59734 : scratch.d.var.attnum = variable->varattno;
948 : 59734 : scratch.d.var.vartype = variable->vartype;
530 dean.a.rasheed@gmail 949 : 59734 : scratch.d.var.varreturningtype = variable->varreturningtype;
3395 andres@anarazel.de 950 [ + + + ]: 59734 : switch (variable->varno)
951 : : {
952 : 4 : case INNER_VAR:
953 : 4 : scratch.opcode = EEOP_INNER_SYSVAR;
954 : 4 : break;
955 : 8 : case OUTER_VAR:
956 : 8 : scratch.opcode = EEOP_OUTER_SYSVAR;
957 : 8 : break;
958 : :
959 : : /* INDEX_VAR is handled by default case */
960 : :
961 : 59722 : default:
530 dean.a.rasheed@gmail 962 [ + + + - ]: 59722 : switch (variable->varreturningtype)
963 : : {
964 : 59290 : case VAR_RETURNING_DEFAULT:
965 : 59290 : scratch.opcode = EEOP_SCAN_SYSVAR;
966 : 59290 : break;
967 : 216 : case VAR_RETURNING_OLD:
968 : 216 : scratch.opcode = EEOP_OLD_SYSVAR;
969 : 216 : state->flags |= EEO_FLAG_HAS_OLD;
970 : 216 : break;
971 : 216 : case VAR_RETURNING_NEW:
972 : 216 : scratch.opcode = EEOP_NEW_SYSVAR;
973 : 216 : state->flags |= EEO_FLAG_HAS_NEW;
974 : 216 : break;
975 : : }
3395 andres@anarazel.de 976 : 59722 : break;
977 : : }
978 : : }
979 : : else
980 : : {
981 : : /* regular user column */
982 : 841950 : scratch.d.var.attnum = variable->varattno - 1;
983 : 841950 : scratch.d.var.vartype = variable->vartype;
530 dean.a.rasheed@gmail 984 : 841950 : scratch.d.var.varreturningtype = variable->varreturningtype;
3395 andres@anarazel.de 985 [ + + + ]: 841950 : switch (variable->varno)
986 : : {
987 : 119904 : case INNER_VAR:
3105 988 : 119904 : scratch.opcode = EEOP_INNER_VAR;
3395 989 : 119904 : break;
990 : 254578 : case OUTER_VAR:
3105 991 : 254578 : scratch.opcode = EEOP_OUTER_VAR;
3395 992 : 254578 : break;
993 : :
994 : : /* INDEX_VAR is handled by default case */
995 : :
996 : 467468 : default:
530 dean.a.rasheed@gmail 997 [ + + + - ]: 467468 : switch (variable->varreturningtype)
998 : : {
999 : 467124 : case VAR_RETURNING_DEFAULT:
1000 : 467124 : scratch.opcode = EEOP_SCAN_VAR;
1001 : 467124 : break;
1002 : 172 : case VAR_RETURNING_OLD:
1003 : 172 : scratch.opcode = EEOP_OLD_VAR;
1004 : 172 : state->flags |= EEO_FLAG_HAS_OLD;
1005 : 172 : break;
1006 : 172 : case VAR_RETURNING_NEW:
1007 : 172 : scratch.opcode = EEOP_NEW_VAR;
1008 : 172 : state->flags |= EEO_FLAG_HAS_NEW;
1009 : 172 : break;
1010 : : }
3395 andres@anarazel.de 1011 : 467468 : break;
1012 : : }
1013 : : }
1014 : :
1015 : 905115 : ExprEvalPushStep(state, &scratch);
1016 : 905115 : break;
1017 : : }
1018 : :
1019 : 686522 : case T_Const:
1020 : : {
1021 : 686522 : Const *con = (Const *) node;
1022 : :
1023 : 686522 : scratch.opcode = EEOP_CONST;
1024 : 686522 : scratch.d.constval.value = con->constvalue;
1025 : 686522 : scratch.d.constval.isnull = con->constisnull;
1026 : :
1027 : 686522 : ExprEvalPushStep(state, &scratch);
1028 : 686522 : break;
1029 : : }
1030 : :
1031 : 461995 : case T_Param:
1032 : : {
1033 : 461995 : Param *param = (Param *) node;
1034 : : ParamListInfo params;
1035 : :
1036 [ + + - ]: 461995 : switch (param->paramkind)
1037 : : {
1038 : 165883 : case PARAM_EXEC:
1039 : 165883 : scratch.opcode = EEOP_PARAM_EXEC;
1040 : 165883 : scratch.d.param.paramid = param->paramid;
1041 : 165883 : scratch.d.param.paramtype = param->paramtype;
3113 tgl@sss.pgh.pa.us 1042 : 165883 : ExprEvalPushStep(state, &scratch);
3395 andres@anarazel.de 1043 : 165883 : break;
1044 : 296112 : 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 : : */
3113 tgl@sss.pgh.pa.us 1052 [ + + ]: 296112 : if (state->ext_params)
1053 : 52912 : params = state->ext_params;
1054 [ + + ]: 243200 : else if (state->parent &&
1055 [ + - ]: 243097 : state->parent->state)
1056 : 243097 : params = state->parent->state->es_param_list_info;
1057 : : else
1058 : 103 : params = NULL;
1059 [ + + + + ]: 296112 : if (params && params->paramCompile)
1060 : : {
1061 : 97128 : params->paramCompile(params, param, state,
1062 : : resv, resnull);
1063 : : }
1064 : : else
1065 : : {
1066 : 198984 : scratch.opcode = EEOP_PARAM_EXTERN;
1067 : 198984 : scratch.d.param.paramid = param->paramid;
1068 : 198984 : scratch.d.param.paramtype = param->paramtype;
1069 : 198984 : ExprEvalPushStep(state, &scratch);
1070 : : }
3395 andres@anarazel.de 1071 : 296112 : break;
3395 andres@anarazel.de 1072 :UBC 0 : default:
1073 [ # # ]: 0 : elog(ERROR, "unrecognized paramkind: %d",
1074 : : (int) param->paramkind);
1075 : : break;
1076 : : }
3395 andres@anarazel.de 1077 :CBC 461995 : break;
1078 : : }
1079 : :
1080 : 38614 : case T_Aggref:
1081 : : {
1082 : 38614 : Aggref *aggref = (Aggref *) node;
1083 : :
1084 : 38614 : scratch.opcode = EEOP_AGGREF;
2044 heikki.linnakangas@i 1085 : 38614 : scratch.d.aggref.aggno = aggref->aggno;
1086 : :
3113 tgl@sss.pgh.pa.us 1087 [ + - + - ]: 38614 : if (state->parent && IsA(state->parent, AggState))
3395 andres@anarazel.de 1088 : 38614 : {
3113 tgl@sss.pgh.pa.us 1089 : 38614 : AggState *aggstate = (AggState *) state->parent;
1090 : :
2044 heikki.linnakangas@i 1091 : 38614 : aggstate->aggs = lappend(aggstate->aggs, aggref);
1092 : : }
1093 : : else
1094 : : {
1095 : : /* planner messed up */
3395 andres@anarazel.de 1096 [ # # ]:UBC 0 : elog(ERROR, "Aggref found in non-Agg plan node");
1097 : : }
1098 : :
3395 andres@anarazel.de 1099 :CBC 38614 : ExprEvalPushStep(state, &scratch);
1100 : 38614 : break;
1101 : : }
1102 : :
1103 : 244 : case T_GroupingFunc:
1104 : : {
1105 : 244 : GroupingFunc *grp_node = (GroupingFunc *) node;
1106 : : Agg *agg;
1107 : :
3113 tgl@sss.pgh.pa.us 1108 [ + - + - ]: 244 : if (!state->parent || !IsA(state->parent, AggState) ||
1109 [ - + ]: 244 : !IsA(state->parent->plan, Agg))
3395 andres@anarazel.de 1110 [ # # ]:UBC 0 : elog(ERROR, "GroupingFunc found in non-Agg plan node");
1111 : :
3395 andres@anarazel.de 1112 :CBC 244 : scratch.opcode = EEOP_GROUPING_FUNC;
1113 : :
3113 tgl@sss.pgh.pa.us 1114 : 244 : agg = (Agg *) (state->parent->plan);
1115 : :
3395 andres@anarazel.de 1116 [ + + ]: 244 : if (agg->groupingSets)
1117 : 181 : scratch.d.grouping_func.clauses = grp_node->cols;
1118 : : else
1119 : 63 : scratch.d.grouping_func.clauses = NIL;
1120 : :
1121 : 244 : ExprEvalPushStep(state, &scratch);
1122 : 244 : break;
1123 : : }
1124 : :
1125 : 2486 : case T_WindowFunc:
1126 : : {
1127 : 2486 : WindowFunc *wfunc = (WindowFunc *) node;
1128 : 2486 : WindowFuncExprState *wfstate = makeNode(WindowFuncExprState);
1129 : :
1130 : 2486 : wfstate->wfunc = wfunc;
1131 : :
3113 tgl@sss.pgh.pa.us 1132 [ + - + - ]: 2486 : if (state->parent && IsA(state->parent, WindowAggState))
3395 andres@anarazel.de 1133 : 2486 : {
3113 tgl@sss.pgh.pa.us 1134 : 2486 : WindowAggState *winstate = (WindowAggState *) state->parent;
1135 : : int nfuncs;
1136 : :
2540 1137 : 2486 : winstate->funcs = lappend(winstate->funcs, wfstate);
3395 andres@anarazel.de 1138 : 2486 : nfuncs = ++winstate->numfuncs;
1139 [ + + ]: 2486 : if (wfunc->winagg)
1140 : 1111 : winstate->numaggs++;
1141 : :
1142 : : /* for now initialize agg using old style expressions */
3113 tgl@sss.pgh.pa.us 1143 : 2486 : wfstate->args = ExecInitExprList(wfunc->args,
3113 tgl@sss.pgh.pa.us 1144 :ECB (1642) : state->parent);
3395 andres@anarazel.de 1145 :CBC 2486 : wfstate->aggfilter = ExecInitExpr(wfunc->aggfilter,
3113 tgl@sss.pgh.pa.us 1146 :ECB (1642) : 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 : : */
3395 andres@anarazel.de 1154 [ - + ]:CBC 2486 : if (nfuncs != winstate->numfuncs)
3395 andres@anarazel.de 1155 [ # # ]:UBC 0 : ereport(ERROR,
1156 : : (errcode(ERRCODE_WINDOWING_ERROR),
1157 : : errmsg("window function calls cannot be nested")));
1158 : : }
1159 : : else
1160 : : {
1161 : : /* planner messed up */
1162 [ # # ]: 0 : elog(ERROR, "WindowFunc found in non-WindowAgg plan node");
1163 : : }
1164 : :
3395 andres@anarazel.de 1165 :CBC 2486 : scratch.opcode = EEOP_WINDOW_FUNC;
1166 : 2486 : scratch.d.window_func.wfstate = wfstate;
1167 : 2486 : ExprEvalPushStep(state, &scratch);
1168 : 2486 : break;
1169 : : }
1170 : :
835 dean.a.rasheed@gmail 1171 : 162 : case T_MergeSupportFunc:
1172 : : {
1173 : : /* must be in a MERGE, else something messed up */
1174 [ + - ]: 162 : if (!state->parent ||
1175 [ + - ]: 162 : !IsA(state->parent, ModifyTableState) ||
1176 [ - + ]: 162 : ((ModifyTableState *) state->parent)->operation != CMD_MERGE)
835 dean.a.rasheed@gmail 1177 [ # # ]:UBC 0 : elog(ERROR, "MergeSupportFunc found in non-merge plan node");
1178 : :
835 dean.a.rasheed@gmail 1179 :CBC 162 : scratch.opcode = EEOP_MERGE_SUPPORT_FUNC;
1180 : 162 : ExprEvalPushStep(state, &scratch);
1181 : 162 : break;
1182 : : }
1183 : :
2706 alvherre@alvh.no-ip. 1184 : 20422 : case T_SubscriptingRef:
1185 : : {
1186 : 20422 : SubscriptingRef *sbsref = (SubscriptingRef *) node;
1187 : :
1188 : 20422 : ExecInitSubscriptingRef(&scratch, sbsref, state, resv, resnull);
3395 andres@anarazel.de 1189 : 20422 : break;
1190 : : }
1191 : :
1192 : 372246 : case T_FuncExpr:
1193 : : {
1194 : 372246 : FuncExpr *func = (FuncExpr *) node;
1195 : :
1196 : 372246 : ExecInitFunc(&scratch, node,
1197 : : func->args, func->funcid, func->inputcollid,
1198 : : state);
1199 : 372194 : ExprEvalPushStep(state, &scratch);
1200 : 372194 : break;
1201 : : }
1202 : :
1203 : 572151 : case T_OpExpr:
1204 : : {
1205 : 572151 : OpExpr *op = (OpExpr *) node;
1206 : :
1207 : 572151 : ExecInitFunc(&scratch, node,
1208 : : op->args, op->opfuncid, op->inputcollid,
1209 : : state);
1210 : 572151 : ExprEvalPushStep(state, &scratch);
1211 : 572151 : break;
1212 : : }
1213 : :
1214 : 650 : case T_DistinctExpr:
1215 : : {
1216 : 650 : DistinctExpr *op = (DistinctExpr *) node;
1217 : :
1218 : 650 : 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 : 650 : scratch.opcode = EEOP_DISTINCT;
1232 : 650 : ExprEvalPushStep(state, &scratch);
1233 : 650 : break;
1234 : : }
1235 : :
1236 : 591 : case T_NullIfExpr:
1237 : : {
1238 : 591 : NullIfExpr *op = (NullIfExpr *) node;
1239 : :
1240 : 591 : 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 : : */
582 tgl@sss.pgh.pa.us 1249 : 591 : scratch.d.func.make_ro =
1250 : 591 : (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 : : */
3395 andres@anarazel.de 1261 : 591 : scratch.opcode = EEOP_NULLIF;
1262 : 591 : ExprEvalPushStep(state, &scratch);
1263 : 591 : break;
1264 : : }
1265 : :
1266 : 23946 : case T_ScalarArrayOpExpr:
1267 : : {
1268 : 23946 : 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 : : */
1819 drowley@postgresql.o 1282 [ + + ]: 23946 : if (OidIsValid(opexpr->negfuncid))
1283 : : {
1284 [ - + ]: 66 : Assert(OidIsValid(opexpr->hashfuncid));
1285 : 66 : cmpfuncid = opexpr->negfuncid;
1286 : : }
1287 : : else
1288 : 23880 : cmpfuncid = opexpr->opfuncid;
1289 : :
3395 andres@anarazel.de 1290 [ - + ]: 23946 : Assert(list_length(opexpr->args) == 2);
1291 : 23946 : scalararg = (Expr *) linitial(opexpr->args);
1292 : 23946 : arrayarg = (Expr *) lsecond(opexpr->args);
1293 : :
1294 : : /* Check permission to call function */
1325 peter@eisentraut.org 1295 : 23946 : aclresult = object_aclcheck(ProcedureRelationId, cmpfuncid,
1296 : : GetUserId(),
1297 : : ACL_EXECUTE);
3395 andres@anarazel.de 1298 [ - + ]: 23946 : if (aclresult != ACLCHECK_OK)
3132 peter_e@gmx.net 1299 :UBC 0 : aclcheck_error(aclresult, OBJECT_FUNCTION,
1819 drowley@postgresql.o 1300 : 0 : get_func_name(cmpfuncid));
1819 drowley@postgresql.o 1301 [ - + ]:CBC 23946 : InvokeFunctionExecuteHook(cmpfuncid);
1302 : :
1909 1303 [ + + ]: 23946 : if (OidIsValid(opexpr->hashfuncid))
1304 : : {
1325 peter@eisentraut.org 1305 : 213 : aclresult = object_aclcheck(ProcedureRelationId, opexpr->hashfuncid,
1306 : : GetUserId(),
1307 : : ACL_EXECUTE);
1909 drowley@postgresql.o 1308 [ - + ]: 213 : if (aclresult != ACLCHECK_OK)
1909 drowley@postgresql.o 1309 :UBC 0 : aclcheck_error(aclresult, OBJECT_FUNCTION,
1310 : 0 : get_func_name(opexpr->hashfuncid));
1909 drowley@postgresql.o 1311 [ - + ]:CBC 213 : InvokeFunctionExecuteHook(opexpr->hashfuncid);
1312 : : }
1313 : :
1314 : : /* Set up the primary fmgr lookup information */
202 michael@paquier.xyz 1315 :GNC 23946 : finfo = palloc0_object(FmgrInfo);
2712 andres@anarazel.de 1316 :CBC 23946 : fcinfo = palloc0(SizeForFunctionCallInfo(2));
1819 drowley@postgresql.o 1317 : 23946 : fmgr_info(cmpfuncid, finfo);
3395 andres@anarazel.de 1318 : 23946 : fmgr_info_set_expr((Node *) node, finfo);
1319 : 23946 : 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 : : */
1909 drowley@postgresql.o 1329 [ + + ]: 23946 : if (OidIsValid(opexpr->hashfuncid))
1330 : : {
1331 : : /* Evaluate scalar directly into left function argument */
1332 : 213 : 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 : 213 : ExecInitExprRec(arrayarg, state, resv, resnull);
1343 : :
1344 : : /* And perform the operation */
1345 : 213 : scratch.opcode = EEOP_HASHED_SCALARARRAYOP;
1819 1346 : 213 : scratch.d.hashedscalararrayop.inclause = opexpr->useOr;
1909 1347 : 213 : scratch.d.hashedscalararrayop.finfo = finfo;
1348 : 213 : scratch.d.hashedscalararrayop.fcinfo_data = fcinfo;
1455 1349 : 213 : scratch.d.hashedscalararrayop.saop = opexpr;
1350 : :
1351 : :
1909 1352 : 213 : ExprEvalPushStep(state, &scratch);
1353 : : }
1354 : : else
1355 : : {
1356 : : /* Evaluate scalar directly into left function argument */
1357 : 23733 : 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 : 23733 : ExecInitExprRec(arrayarg, state, resv, resnull);
1368 : :
1369 : : /* And perform the operation */
1370 : 23733 : scratch.opcode = EEOP_SCALARARRAYOP;
1371 : 23733 : scratch.d.scalararrayop.element_type = InvalidOid;
1372 : 23733 : scratch.d.scalararrayop.useOr = opexpr->useOr;
1373 : 23733 : scratch.d.scalararrayop.finfo = finfo;
1374 : 23733 : scratch.d.scalararrayop.fcinfo_data = fcinfo;
1375 : 23733 : scratch.d.scalararrayop.fn_addr = finfo->fn_addr;
1376 : 23733 : ExprEvalPushStep(state, &scratch);
1377 : : }
3395 andres@anarazel.de 1378 : 23946 : break;
1379 : : }
1380 : :
1381 : 45859 : case T_BoolExpr:
1382 : : {
1383 : 45859 : BoolExpr *boolexpr = (BoolExpr *) node;
1384 : 45859 : int nargs = list_length(boolexpr->args);
1385 : 45859 : List *adjust_jumps = NIL;
1386 : : int off;
1387 : : ListCell *lc;
1388 : :
1389 : : /* allocate scratch memory used by all steps of AND/OR */
1390 [ + + ]: 45859 : if (boolexpr->boolop != NOT_EXPR)
202 michael@paquier.xyz 1391 :GNC 33268 : scratch.d.boolexpr.anynull = palloc_object(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 : : */
3395 andres@anarazel.de 1406 :CBC 45859 : off = 0;
1407 [ + - + + : 134039 : foreach(lc, boolexpr->args)
+ + ]
1408 : : {
1409 : 88180 : Expr *arg = (Expr *) lfirst(lc);
1410 : :
1411 : : /* Evaluate argument into our output variable */
3113 tgl@sss.pgh.pa.us 1412 : 88180 : ExecInitExprRec(arg, state, resv, resnull);
1413 : :
1414 : : /* Perform the appropriate step type */
3395 andres@anarazel.de 1415 [ + + + - ]: 88180 : switch (boolexpr->boolop)
1416 : : {
1417 : 48379 : case AND_EXPR:
1418 [ - + ]: 48379 : Assert(nargs >= 2);
1419 : :
1420 [ + + ]: 48379 : if (off == 0)
1421 : 21304 : scratch.opcode = EEOP_BOOL_AND_STEP_FIRST;
1422 [ + + ]: 27075 : else if (off + 1 == nargs)
1423 : 21304 : scratch.opcode = EEOP_BOOL_AND_STEP_LAST;
1424 : : else
1425 : 5771 : scratch.opcode = EEOP_BOOL_AND_STEP;
1426 : 48379 : break;
1427 : 27210 : case OR_EXPR:
1428 [ - + ]: 27210 : Assert(nargs >= 2);
1429 : :
1430 [ + + ]: 27210 : if (off == 0)
1431 : 11964 : scratch.opcode = EEOP_BOOL_OR_STEP_FIRST;
1432 [ + + ]: 15246 : else if (off + 1 == nargs)
1433 : 11964 : scratch.opcode = EEOP_BOOL_OR_STEP_LAST;
1434 : : else
1435 : 3282 : scratch.opcode = EEOP_BOOL_OR_STEP;
1436 : 27210 : break;
1437 : 12591 : case NOT_EXPR:
1438 [ - + ]: 12591 : Assert(nargs == 1);
1439 : :
1440 : 12591 : scratch.opcode = EEOP_BOOL_NOT_STEP;
1441 : 12591 : break;
3395 andres@anarazel.de 1442 :UBC 0 : default:
1443 [ # # ]: 0 : elog(ERROR, "unrecognized boolop: %d",
1444 : : (int) boolexpr->boolop);
1445 : : break;
1446 : : }
1447 : :
3395 andres@anarazel.de 1448 :CBC 88180 : scratch.d.boolexpr.jumpdone = -1;
1449 : 88180 : ExprEvalPushStep(state, &scratch);
1450 : 88180 : adjust_jumps = lappend_int(adjust_jumps,
1451 : 88180 : state->steps_len - 1);
1452 : 88180 : off++;
1453 : : }
1454 : :
1455 : : /* adjust jump targets */
1456 [ + - + + : 134039 : foreach(lc, adjust_jumps)
+ + ]
1457 : : {
1458 : 88180 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
1459 : :
1460 [ - + ]: 88180 : Assert(as->d.boolexpr.jumpdone == -1);
1461 : 88180 : as->d.boolexpr.jumpdone = state->steps_len;
1462 : : }
1463 : :
1464 : 45859 : break;
1465 : : }
1466 : :
1467 : 18638 : case T_SubPlan:
1468 : : {
1469 : 18638 : 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 : : */
1221 tgl@sss.pgh.pa.us 1477 [ + + ]: 18638 : if (subplan->subLinkType == MULTIEXPR_SUBLINK)
1478 : : {
1479 : 39 : scratch.opcode = EEOP_CONST;
1480 : 39 : scratch.d.constval.value = (Datum) 0;
1481 : 39 : scratch.d.constval.isnull = true;
1482 : 39 : ExprEvalPushStep(state, &scratch);
1483 : 39 : break;
1484 : : }
1485 : :
699 andres@anarazel.de 1486 : 18599 : ExecInitSubPlanExpr(subplan, state, resv, resnull);
3395 1487 : 18599 : break;
1488 : : }
1489 : :
1490 : 26260 : case T_FieldSelect:
1491 : : {
1492 : 26260 : FieldSelect *fselect = (FieldSelect *) node;
1493 : :
1494 : : /* evaluate row/record argument into result area */
3113 tgl@sss.pgh.pa.us 1495 : 26260 : ExecInitExprRec(fselect->arg, state, resv, resnull);
1496 : :
1497 : : /* and extract field */
3395 andres@anarazel.de 1498 : 26260 : scratch.opcode = EEOP_FIELDSELECT;
1499 : 26260 : scratch.d.fieldselect.fieldnum = fselect->fieldnum;
1500 : 26260 : scratch.d.fieldselect.resulttype = fselect->resulttype;
1904 tgl@sss.pgh.pa.us 1501 : 26260 : scratch.d.fieldselect.rowcache.cacheptr = NULL;
1502 : :
3395 andres@anarazel.de 1503 : 26260 : ExprEvalPushStep(state, &scratch);
1504 : 26260 : break;
1505 : : }
1506 : :
1507 : 253 : case T_FieldStore:
1508 : : {
1509 : 253 : 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 : 253 : tupDesc = lookup_rowtype_tupdesc(fstore->resulttype, -1);
1520 : 253 : ncolumns = tupDesc->natts;
1658 tgl@sss.pgh.pa.us 1521 [ + - ]: 253 : ReleaseTupleDesc(tupDesc);
1522 : :
1523 : : /* create workspace for column values */
202 michael@paquier.xyz 1524 :GNC 253 : values = palloc_array(Datum, ncolumns);
1525 : 253 : nulls = palloc_array(bool, ncolumns);
1526 : :
1527 : : /* create shared composite-type-lookup cache struct */
1528 : 253 : rowcachep = palloc_object(ExprEvalRowtypeCache);
1904 tgl@sss.pgh.pa.us 1529 :CBC 253 : rowcachep->cacheptr = NULL;
1530 : :
1531 : : /* emit code to evaluate the composite input value */
3113 1532 : 253 : ExecInitExprRec(fstore->arg, state, resv, resnull);
1533 : :
1534 : : /* next, deform the input tuple into our workspace */
3395 andres@anarazel.de 1535 : 253 : scratch.opcode = EEOP_FIELDSTORE_DEFORM;
1536 : 253 : scratch.d.fieldstore.fstore = fstore;
1904 tgl@sss.pgh.pa.us 1537 : 253 : scratch.d.fieldstore.rowcache = rowcachep;
3395 andres@anarazel.de 1538 : 253 : scratch.d.fieldstore.values = values;
1539 : 253 : scratch.d.fieldstore.nulls = nulls;
1540 : 253 : scratch.d.fieldstore.ncolumns = ncolumns;
1541 : 253 : ExprEvalPushStep(state, &scratch);
1542 : :
1543 : : /* evaluate new field values, store in workspace columns */
1544 [ + - + + : 590 : forboth(l1, fstore->newvals, l2, fstore->fieldnums)
+ - + + +
+ + - +
+ ]
1545 : : {
1546 : 337 : Expr *e = (Expr *) lfirst(l1);
1547 : 337 : AttrNumber fieldnum = lfirst_int(l2);
1548 : : Datum *save_innermost_caseval;
1549 : : bool *save_innermost_casenull;
1550 : :
1551 [ + - - + ]: 337 : if (fieldnum <= 0 || fieldnum > ncolumns)
3395 andres@anarazel.de 1552 [ # # ]:UBC 0 : 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 : : */
3395 andres@anarazel.de 1579 :CBC 337 : save_innermost_caseval = state->innermost_caseval;
1580 : 337 : save_innermost_casenull = state->innermost_casenull;
1581 : 337 : state->innermost_caseval = &values[fieldnum - 1];
1582 : 337 : state->innermost_casenull = &nulls[fieldnum - 1];
1583 : :
3113 tgl@sss.pgh.pa.us 1584 : 337 : ExecInitExprRec(e, state,
3395 andres@anarazel.de 1585 : 337 : &values[fieldnum - 1],
1586 : 337 : &nulls[fieldnum - 1]);
1587 : :
1588 : 337 : state->innermost_caseval = save_innermost_caseval;
1589 : 337 : state->innermost_casenull = save_innermost_casenull;
1590 : : }
1591 : :
1592 : : /* finally, form result tuple */
1593 : 253 : scratch.opcode = EEOP_FIELDSTORE_FORM;
1594 : 253 : scratch.d.fieldstore.fstore = fstore;
1904 tgl@sss.pgh.pa.us 1595 : 253 : scratch.d.fieldstore.rowcache = rowcachep;
3395 andres@anarazel.de 1596 : 253 : scratch.d.fieldstore.values = values;
1597 : 253 : scratch.d.fieldstore.nulls = nulls;
1598 : 253 : scratch.d.fieldstore.ncolumns = ncolumns;
1599 : 253 : ExprEvalPushStep(state, &scratch);
1600 : 253 : break;
1601 : : }
1602 : :
1603 : 77860 : case T_RelabelType:
1604 : : {
1605 : : /* relabel doesn't need to do anything at runtime */
1606 : 77860 : RelabelType *relabel = (RelabelType *) node;
1607 : :
3113 tgl@sss.pgh.pa.us 1608 : 77860 : ExecInitExprRec(relabel->arg, state, resv, resnull);
3395 andres@anarazel.de 1609 : 77860 : break;
1610 : : }
1611 : :
1612 : 26285 : case T_CoerceViaIO:
1613 : : {
1614 : 26285 : 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 */
3113 tgl@sss.pgh.pa.us 1621 : 26285 : 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 : : */
888 amitlan@postgresql.o 1631 [ + - ]: 26285 : if (state->escontext == NULL)
1632 : 26285 : scratch.opcode = EEOP_IOCOERCE;
1633 : : else
888 amitlan@postgresql.o 1634 :UBC 0 : scratch.opcode = EEOP_IOCOERCE_SAFE;
1635 : :
1636 : : /* lookup the source type's output function */
202 michael@paquier.xyz 1637 :GNC 26285 : scratch.d.iocoerce.finfo_out = palloc0_object(FmgrInfo);
2712 andres@anarazel.de 1638 :CBC 26285 : scratch.d.iocoerce.fcinfo_data_out = palloc0(SizeForFunctionCallInfo(1));
1639 : :
3395 1640 : 26285 : getTypeOutputInfo(exprType((Node *) iocoerce->arg),
1641 : : &iofunc, &typisvarlena);
1642 : 26285 : fmgr_info(iofunc, scratch.d.iocoerce.finfo_out);
1643 : 26285 : fmgr_info_set_expr((Node *) node, scratch.d.iocoerce.finfo_out);
1644 : 26285 : InitFunctionCallInfoData(*scratch.d.iocoerce.fcinfo_data_out,
1645 : : scratch.d.iocoerce.finfo_out,
1646 : : 1, InvalidOid, NULL, NULL);
1647 : :
1648 : : /* lookup the result type's input function */
202 michael@paquier.xyz 1649 :GNC 26285 : scratch.d.iocoerce.finfo_in = palloc0_object(FmgrInfo);
1002 amitlan@postgresql.o 1650 :CBC 26285 : scratch.d.iocoerce.fcinfo_data_in = palloc0(SizeForFunctionCallInfo(3));
1651 : :
3395 andres@anarazel.de 1652 : 26285 : getTypeInputInfo(iocoerce->resulttype,
1653 : : &iofunc, &typioparam);
1654 : 26285 : fmgr_info(iofunc, scratch.d.iocoerce.finfo_in);
1655 : 26285 : fmgr_info_set_expr((Node *) node, scratch.d.iocoerce.finfo_in);
1002 amitlan@postgresql.o 1656 : 26285 : InitFunctionCallInfoData(*scratch.d.iocoerce.fcinfo_data_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 : 26285 : fcinfo_in = scratch.d.iocoerce.fcinfo_data_in;
1665 : 26285 : fcinfo_in->args[1].value = ObjectIdGetDatum(typioparam);
1666 : 26285 : fcinfo_in->args[1].isnull = false;
1667 : 26285 : fcinfo_in->args[2].value = Int32GetDatum(-1);
1668 : 26285 : fcinfo_in->args[2].isnull = false;
1669 : :
888 1670 : 26285 : fcinfo_in->context = (Node *) state->escontext;
1671 : :
3395 andres@anarazel.de 1672 : 26285 : ExprEvalPushStep(state, &scratch);
1673 : 26285 : break;
1674 : : }
1675 : :
1676 : 3902 : case T_ArrayCoerceExpr:
1677 : : {
1678 : 3902 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
1679 : : Oid resultelemtype;
1680 : : ExprState *elemstate;
1681 : :
1682 : : /* evaluate argument into step's result area */
3113 tgl@sss.pgh.pa.us 1683 : 3902 : ExecInitExprRec(acoerce->arg, state, resv, resnull);
1684 : :
3395 andres@anarazel.de 1685 : 3902 : resultelemtype = get_element_type(acoerce->resulttype);
1686 [ - + ]: 3902 : if (!OidIsValid(resultelemtype))
3395 andres@anarazel.de 1687 [ # # ]:UBC 0 : 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 : : */
3195 tgl@sss.pgh.pa.us 1697 :CBC 3902 : elemstate = makeNode(ExprState);
1698 : 3902 : elemstate->expr = acoerce->elemexpr;
3113 1699 : 3902 : elemstate->parent = state->parent;
1700 : 3902 : elemstate->ext_params = state->ext_params;
1701 : :
202 michael@paquier.xyz 1702 :GNC 3902 : elemstate->innermost_caseval = palloc_object(Datum);
1703 : 3902 : elemstate->innermost_casenull = palloc_object(bool);
1704 : :
3113 tgl@sss.pgh.pa.us 1705 :CBC 3902 : ExecInitExprRec(acoerce->elemexpr, elemstate,
1706 : : &elemstate->resvalue, &elemstate->resnull);
1707 : :
3195 1708 [ + + ]: 3898 : if (elemstate->steps_len == 1 &&
1709 [ + - ]: 3560 : elemstate->steps[0].opcode == EEOP_CASE_TESTVAL)
1710 : : {
1711 : : /* Trivial, so we need no per-element work at runtime */
1712 : 3560 : elemstate = NULL;
1713 : : }
1714 : : else
1715 : : {
1716 : : /* Not trivial, so append a DONE step */
476 dgustafsson@postgres 1717 : 338 : scratch.opcode = EEOP_DONE_RETURN;
3195 tgl@sss.pgh.pa.us 1718 : 338 : ExprEvalPushStep(elemstate, &scratch);
1719 : : /* and ready the subexpression */
1720 : 338 : ExecReadyExpr(elemstate);
1721 : : }
1722 : :
3395 andres@anarazel.de 1723 : 3898 : scratch.opcode = EEOP_ARRAYCOERCE;
3195 tgl@sss.pgh.pa.us 1724 : 3898 : scratch.d.arraycoerce.elemexprstate = elemstate;
3395 andres@anarazel.de 1725 : 3898 : scratch.d.arraycoerce.resultelemtype = resultelemtype;
1726 : :
3195 tgl@sss.pgh.pa.us 1727 [ + + ]: 3898 : if (elemstate)
1728 : : {
1729 : : /* Set up workspace for array_map */
202 michael@paquier.xyz 1730 :GNC 338 : scratch.d.arraycoerce.amstate = palloc0_object(ArrayMapState);
1731 : : }
1732 : : else
1733 : : {
1734 : : /* Don't need workspace if there's no subexpression */
3395 andres@anarazel.de 1735 :CBC 3560 : scratch.d.arraycoerce.amstate = NULL;
1736 : : }
1737 : :
1738 : 3898 : ExprEvalPushStep(state, &scratch);
1739 : 3898 : break;
1740 : : }
1741 : :
1742 : 538 : case T_ConvertRowtypeExpr:
1743 : : {
1744 : 538 : ConvertRowtypeExpr *convert = (ConvertRowtypeExpr *) node;
1745 : : ExprEvalRowtypeCache *rowcachep;
1746 : :
1747 : : /* cache structs must be out-of-line for space reasons */
1904 tgl@sss.pgh.pa.us 1748 : 538 : rowcachep = palloc(2 * sizeof(ExprEvalRowtypeCache));
1749 : 538 : rowcachep[0].cacheptr = NULL;
1750 : 538 : rowcachep[1].cacheptr = NULL;
1751 : :
1752 : : /* evaluate argument into step's result area */
3113 1753 : 538 : ExecInitExprRec(convert->arg, state, resv, resnull);
1754 : :
1755 : : /* and push conversion step */
3395 andres@anarazel.de 1756 : 538 : scratch.opcode = EEOP_CONVERT_ROWTYPE;
1904 tgl@sss.pgh.pa.us 1757 : 538 : scratch.d.convert_rowtype.inputtype =
1758 : 538 : exprType((Node *) convert->arg);
1759 : 538 : scratch.d.convert_rowtype.outputtype = convert->resulttype;
1760 : 538 : scratch.d.convert_rowtype.incache = &rowcachep[0];
1761 : 538 : scratch.d.convert_rowtype.outcache = &rowcachep[1];
3395 andres@anarazel.de 1762 : 538 : scratch.d.convert_rowtype.map = NULL;
1763 : :
1764 : 538 : ExprEvalPushStep(state, &scratch);
1765 : 538 : break;
1766 : : }
1767 : :
1768 : : /* note that CaseWhen expressions are handled within this block */
1769 : 61213 : case T_CaseExpr:
1770 : : {
1771 : 61213 : CaseExpr *caseExpr = (CaseExpr *) node;
1772 : 61213 : List *adjust_jumps = NIL;
1773 : 61213 : Datum *caseval = NULL;
1774 : 61213 : bool *casenull = NULL;
1775 : : ListCell *lc;
1776 : :
1777 : : /*
1778 : : * If there's a test expression, we have to evaluate it and
1779 : : * save the value where the CaseTestExpr placeholders can find
1780 : : * it.
1781 : : */
1782 [ + + ]: 61213 : if (caseExpr->arg != NULL)
1783 : : {
1784 : : /* Evaluate testexpr into caseval/casenull workspace */
202 michael@paquier.xyz 1785 :GNC 3290 : caseval = palloc_object(Datum);
1786 : 3290 : casenull = palloc_object(bool);
1787 : :
3113 tgl@sss.pgh.pa.us 1788 :CBC 3290 : ExecInitExprRec(caseExpr->arg, state,
1789 : : caseval, casenull);
1790 : :
1791 : : /*
1792 : : * Since value might be read multiple times, force to R/O
1793 : : * - but only if it could be an expanded datum.
1794 : : */
3395 andres@anarazel.de 1795 [ + + ]: 3290 : if (get_typlen(exprType((Node *) caseExpr->arg)) == -1)
1796 : : {
1797 : : /* change caseval in-place */
1798 : 79 : scratch.opcode = EEOP_MAKE_READONLY;
1799 : 79 : scratch.resvalue = caseval;
1800 : 79 : scratch.resnull = casenull;
1801 : 79 : scratch.d.make_readonly.value = caseval;
1802 : 79 : scratch.d.make_readonly.isnull = casenull;
1803 : 79 : ExprEvalPushStep(state, &scratch);
1804 : : /* restore normal settings of scratch fields */
1805 : 79 : scratch.resvalue = resv;
1806 : 79 : scratch.resnull = resnull;
1807 : : }
1808 : : }
1809 : :
1810 : : /*
1811 : : * Prepare to evaluate each of the WHEN clauses in turn; as
1812 : : * soon as one is true we return the value of the
1813 : : * corresponding THEN clause. If none are true then we return
1814 : : * the value of the ELSE clause, or NULL if there is none.
1815 : : */
1816 [ + - + + : 179007 : foreach(lc, caseExpr->args)
+ + ]
1817 : : {
1818 : 117794 : CaseWhen *when = (CaseWhen *) lfirst(lc);
1819 : : Datum *save_innermost_caseval;
1820 : : bool *save_innermost_casenull;
1821 : : int whenstep;
1822 : :
1823 : : /*
1824 : : * Make testexpr result available to CaseTestExpr nodes
1825 : : * within the condition. We must save and restore prior
1826 : : * setting of innermost_caseval fields, in case this node
1827 : : * is itself within a larger CASE.
1828 : : *
1829 : : * If there's no test expression, we don't actually need
1830 : : * to save and restore these fields; but it's less code to
1831 : : * just do so unconditionally.
1832 : : */
1833 : 117794 : save_innermost_caseval = state->innermost_caseval;
1834 : 117794 : save_innermost_casenull = state->innermost_casenull;
1835 : 117794 : state->innermost_caseval = caseval;
1836 : 117794 : state->innermost_casenull = casenull;
1837 : :
1838 : : /* evaluate condition into CASE's result variables */
3113 tgl@sss.pgh.pa.us 1839 : 117794 : ExecInitExprRec(when->expr, state, resv, resnull);
1840 : :
3395 andres@anarazel.de 1841 : 117794 : state->innermost_caseval = save_innermost_caseval;
1842 : 117794 : state->innermost_casenull = save_innermost_casenull;
1843 : :
1844 : : /* If WHEN result isn't true, jump to next CASE arm */
1845 : 117794 : scratch.opcode = EEOP_JUMP_IF_NOT_TRUE;
3296 tgl@sss.pgh.pa.us 1846 : 117794 : scratch.d.jump.jumpdone = -1; /* computed later */
3395 andres@anarazel.de 1847 : 117794 : ExprEvalPushStep(state, &scratch);
1848 : 117794 : whenstep = state->steps_len - 1;
1849 : :
1850 : : /*
1851 : : * If WHEN result is true, evaluate THEN result, storing
1852 : : * it into the CASE's result variables.
1853 : : */
3113 tgl@sss.pgh.pa.us 1854 : 117794 : ExecInitExprRec(when->result, state, resv, resnull);
1855 : :
1856 : : /* Emit JUMP step to jump to end of CASE's code */
3395 andres@anarazel.de 1857 : 117794 : scratch.opcode = EEOP_JUMP;
3296 tgl@sss.pgh.pa.us 1858 : 117794 : scratch.d.jump.jumpdone = -1; /* computed later */
3395 andres@anarazel.de 1859 : 117794 : ExprEvalPushStep(state, &scratch);
1860 : :
1861 : : /*
1862 : : * Don't know address for that jump yet, compute once the
1863 : : * whole CASE expression is built.
1864 : : */
1865 : 117794 : adjust_jumps = lappend_int(adjust_jumps,
1866 : 117794 : state->steps_len - 1);
1867 : :
1868 : : /*
1869 : : * But we can set WHEN test's jump target now, to make it
1870 : : * jump to the next WHEN subexpression or the ELSE.
1871 : : */
1872 : 117794 : state->steps[whenstep].d.jump.jumpdone = state->steps_len;
1873 : : }
1874 : :
1875 : : /* transformCaseExpr always adds a default */
3384 1876 [ - + ]: 61213 : Assert(caseExpr->defresult);
1877 : :
1878 : : /* evaluate ELSE expr into CASE's result variables */
3113 tgl@sss.pgh.pa.us 1879 : 61213 : ExecInitExprRec(caseExpr->defresult, state,
1880 : : resv, resnull);
1881 : :
1882 : : /* adjust jump targets */
3395 andres@anarazel.de 1883 [ + - + + : 179007 : foreach(lc, adjust_jumps)
+ + ]
1884 : : {
1885 : 117794 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
1886 : :
1887 [ - + ]: 117794 : Assert(as->opcode == EEOP_JUMP);
1888 [ - + ]: 117794 : Assert(as->d.jump.jumpdone == -1);
1889 : 117794 : as->d.jump.jumpdone = state->steps_len;
1890 : : }
1891 : :
1892 : 61213 : break;
1893 : : }
1894 : :
1895 : 18139 : case T_CaseTestExpr:
1896 : : {
1897 : : /*
1898 : : * Read from location identified by innermost_caseval. Note
1899 : : * that innermost_caseval could be NULL, if this node isn't
1900 : : * actually within a CaseExpr, ArrayCoerceExpr, etc structure.
1901 : : * That can happen because some parts of the system abuse
1902 : : * CaseTestExpr to cause a read of a value externally supplied
1903 : : * in econtext->caseValue_datum. We'll take care of that by
1904 : : * generating a specialized operation.
1905 : : */
516 tgl@sss.pgh.pa.us 1906 [ + + ]: 18139 : if (state->innermost_caseval == NULL)
1907 : 1021 : scratch.opcode = EEOP_CASE_TESTVAL_EXT;
1908 : : else
1909 : : {
1910 : 17118 : scratch.opcode = EEOP_CASE_TESTVAL;
1911 : 17118 : scratch.d.casetest.value = state->innermost_caseval;
1912 : 17118 : scratch.d.casetest.isnull = state->innermost_casenull;
1913 : : }
3395 andres@anarazel.de 1914 : 18139 : ExprEvalPushStep(state, &scratch);
1915 : 18139 : break;
1916 : : }
1917 : :
1918 : 21055 : case T_ArrayExpr:
1919 : : {
1920 : 21055 : ArrayExpr *arrayexpr = (ArrayExpr *) node;
1921 : 21055 : int nelems = list_length(arrayexpr->elements);
1922 : : ListCell *lc;
1923 : : int elemoff;
1924 : :
1925 : : /*
1926 : : * Evaluate by computing each element, and then forming the
1927 : : * array. Elements are computed into scratch arrays
1928 : : * associated with the ARRAYEXPR step.
1929 : : */
1930 : 21055 : scratch.opcode = EEOP_ARRAYEXPR;
1931 : 21055 : scratch.d.arrayexpr.elemvalues =
202 michael@paquier.xyz 1932 :GNC 21055 : palloc_array(Datum, nelems);
3395 andres@anarazel.de 1933 :CBC 21055 : scratch.d.arrayexpr.elemnulls =
202 michael@paquier.xyz 1934 :GNC 21055 : palloc_array(bool, nelems);
3395 andres@anarazel.de 1935 :CBC 21055 : scratch.d.arrayexpr.nelems = nelems;
1936 : :
1937 : : /* fill remaining fields of step */
1938 : 21055 : scratch.d.arrayexpr.multidims = arrayexpr->multidims;
1939 : 21055 : scratch.d.arrayexpr.elemtype = arrayexpr->element_typeid;
1940 : :
1941 : : /* do one-time catalog lookup for type info */
1942 : 21055 : get_typlenbyvalalign(arrayexpr->element_typeid,
1943 : : &scratch.d.arrayexpr.elemlength,
1944 : : &scratch.d.arrayexpr.elembyval,
1945 : : &scratch.d.arrayexpr.elemalign);
1946 : :
1947 : : /* prepare to evaluate all arguments */
1948 : 21055 : elemoff = 0;
1949 [ + + + + : 79405 : foreach(lc, arrayexpr->elements)
+ + ]
1950 : : {
1951 : 58350 : Expr *e = (Expr *) lfirst(lc);
1952 : :
3113 tgl@sss.pgh.pa.us 1953 : 58350 : ExecInitExprRec(e, state,
3395 andres@anarazel.de 1954 : 58350 : &scratch.d.arrayexpr.elemvalues[elemoff],
1955 : 58350 : &scratch.d.arrayexpr.elemnulls[elemoff]);
1956 : 58350 : elemoff++;
1957 : : }
1958 : :
1959 : : /* and then collect all into an array */
1960 : 21055 : ExprEvalPushStep(state, &scratch);
1961 : 21055 : break;
1962 : : }
1963 : :
1964 : 4158 : case T_RowExpr:
1965 : : {
1966 : 4158 : RowExpr *rowexpr = (RowExpr *) node;
1967 : 4158 : int nelems = list_length(rowexpr->args);
1968 : : TupleDesc tupdesc;
1969 : : int i;
1970 : : ListCell *l;
1971 : :
1972 : : /* Build tupdesc to describe result tuples */
1973 [ + + ]: 4158 : if (rowexpr->row_typeid == RECORDOID)
1974 : : {
1975 : : /* generic record, use types of given expressions */
1976 : 2111 : tupdesc = ExecTypeFromExprList(rowexpr->args);
1977 : : /* ... but adopt RowExpr's column aliases */
1566 tgl@sss.pgh.pa.us 1978 : 2111 : ExecTypeSetColNames(tupdesc, rowexpr->colnames);
1979 : : /* Bless the tupdesc so it can be looked up later */
1980 : 2111 : BlessTupleDesc(tupdesc);
1981 : : }
1982 : : else
1983 : : {
1984 : : /* it's been cast to a named type, use that */
3395 andres@anarazel.de 1985 : 2047 : tupdesc = lookup_rowtype_tupdesc_copy(rowexpr->row_typeid, -1);
1986 : : }
1987 : :
1988 : : /*
1989 : : * In the named-type case, the tupdesc could have more columns
1990 : : * than are in the args list, since the type might have had
1991 : : * columns added since the ROW() was parsed. We want those
1992 : : * extra columns to go to nulls, so we make sure that the
1993 : : * workspace arrays are large enough and then initialize any
1994 : : * extra columns to read as NULLs.
1995 : : */
1996 [ - + ]: 4158 : Assert(nelems <= tupdesc->natts);
1997 : 4158 : nelems = Max(nelems, tupdesc->natts);
1998 : :
1999 : : /*
2000 : : * Evaluate by first building datums for each field, and then
2001 : : * a final step forming the composite datum.
2002 : : */
2003 : 4158 : scratch.opcode = EEOP_ROW;
2004 : 4158 : scratch.d.row.tupdesc = tupdesc;
2005 : :
2006 : : /* space for the individual field datums */
2007 : 4158 : scratch.d.row.elemvalues =
202 michael@paquier.xyz 2008 :GNC 4158 : palloc_array(Datum, nelems);
3395 andres@anarazel.de 2009 :CBC 4158 : scratch.d.row.elemnulls =
202 michael@paquier.xyz 2010 :GNC 4158 : palloc_array(bool, nelems);
2011 : : /* as explained above, make sure any extra columns are null */
3395 andres@anarazel.de 2012 :CBC 4158 : memset(scratch.d.row.elemnulls, true, sizeof(bool) * nelems);
2013 : :
2014 : : /* Set up evaluation, skipping any deleted columns */
2015 : 4158 : i = 0;
2016 [ + + + + : 15345 : foreach(l, rowexpr->args)
+ + ]
2017 : : {
3236 2018 : 11191 : Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3395 2019 : 11191 : Expr *e = (Expr *) lfirst(l);
2020 : :
3236 2021 [ + + ]: 11191 : if (!att->attisdropped)
2022 : : {
2023 : : /*
2024 : : * Guard against ALTER COLUMN TYPE on rowtype since
2025 : : * the RowExpr was created. XXX should we check
2026 : : * typmod too? Not sure we can be sure it'll be the
2027 : : * same.
2028 : : */
2029 [ + + ]: 11182 : if (exprType((Node *) e) != att->atttypid)
3395 2030 [ + - ]: 4 : ereport(ERROR,
2031 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
2032 : : errmsg("ROW() column has type %s instead of type %s",
2033 : : format_type_be(exprType((Node *) e)),
2034 : : format_type_be(att->atttypid))));
2035 : : }
2036 : : else
2037 : : {
2038 : : /*
2039 : : * Ignore original expression and insert a NULL. We
2040 : : * don't really care what type of NULL it is, so
2041 : : * always make an int4 NULL.
2042 : : */
2043 : 9 : e = (Expr *) makeNullConst(INT4OID, -1, InvalidOid);
2044 : : }
2045 : :
2046 : : /* Evaluate column expr into appropriate workspace slot */
3113 tgl@sss.pgh.pa.us 2047 : 11187 : ExecInitExprRec(e, state,
3395 andres@anarazel.de 2048 : 11187 : &scratch.d.row.elemvalues[i],
2049 : 11187 : &scratch.d.row.elemnulls[i]);
2050 : 11187 : i++;
2051 : : }
2052 : :
2053 : : /* And finally build the row value */
2054 : 4154 : ExprEvalPushStep(state, &scratch);
2055 : 4154 : break;
2056 : : }
2057 : :
2058 : 188 : case T_RowCompareExpr:
2059 : : {
2060 : 188 : RowCompareExpr *rcexpr = (RowCompareExpr *) node;
2061 : 188 : int nopers = list_length(rcexpr->opnos);
2062 : 188 : List *adjust_jumps = NIL;
2063 : : ListCell *l_left_expr,
2064 : : *l_right_expr,
2065 : : *l_opno,
2066 : : *l_opfamily,
2067 : : *l_inputcollid;
2068 : : ListCell *lc;
2069 : :
2070 : : /*
2071 : : * Iterate over each field, prepare comparisons. To handle
2072 : : * NULL results, prepare jumps to after the expression. If a
2073 : : * comparison yields a != 0 result, jump to the final step.
2074 : : */
2075 [ - + ]: 188 : Assert(list_length(rcexpr->largs) == nopers);
2076 [ - + ]: 188 : Assert(list_length(rcexpr->rargs) == nopers);
2077 [ - + ]: 188 : Assert(list_length(rcexpr->opfamilies) == nopers);
2078 [ - + ]: 188 : Assert(list_length(rcexpr->inputcollids) == nopers);
2079 : :
2679 tgl@sss.pgh.pa.us 2080 [ + - + + : 600 : forfive(l_left_expr, rcexpr->largs,
+ - + + +
- + + + -
+ + + - +
+ + + + -
+ - + - +
- + + ]
2081 : : l_right_expr, rcexpr->rargs,
2082 : : l_opno, rcexpr->opnos,
2083 : : l_opfamily, rcexpr->opfamilies,
2084 : : l_inputcollid, rcexpr->inputcollids)
2085 : : {
3395 andres@anarazel.de 2086 : 412 : Expr *left_expr = (Expr *) lfirst(l_left_expr);
2087 : 412 : Expr *right_expr = (Expr *) lfirst(l_right_expr);
2088 : 412 : Oid opno = lfirst_oid(l_opno);
2089 : 412 : Oid opfamily = lfirst_oid(l_opfamily);
2090 : 412 : Oid inputcollid = lfirst_oid(l_inputcollid);
2091 : : int strategy;
2092 : : Oid lefttype;
2093 : : Oid righttype;
2094 : : Oid proc;
2095 : : FmgrInfo *finfo;
2096 : : FunctionCallInfo fcinfo;
2097 : :
2098 : 412 : get_op_opfamily_properties(opno, opfamily, false,
2099 : : &strategy,
2100 : : &lefttype,
2101 : : &righttype);
2102 : 412 : proc = get_opfamily_proc(opfamily,
2103 : : lefttype,
2104 : : righttype,
2105 : : BTORDER_PROC);
3263 tgl@sss.pgh.pa.us 2106 [ - + ]: 412 : if (!OidIsValid(proc))
3263 tgl@sss.pgh.pa.us 2107 [ # # ]:UBC 0 : elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
2108 : : BTORDER_PROC, lefttype, righttype, opfamily);
2109 : :
2110 : : /* Set up the primary fmgr lookup information */
202 michael@paquier.xyz 2111 :GNC 412 : finfo = palloc0_object(FmgrInfo);
2712 andres@anarazel.de 2112 :CBC 412 : fcinfo = palloc0(SizeForFunctionCallInfo(2));
3395 2113 : 412 : fmgr_info(proc, finfo);
2114 : 412 : fmgr_info_set_expr((Node *) node, finfo);
2115 : 412 : InitFunctionCallInfoData(*fcinfo, finfo, 2,
2116 : : inputcollid, NULL, NULL);
2117 : :
2118 : : /*
2119 : : * If we enforced permissions checks on index support
2120 : : * functions, we'd need to make a check here. But the
2121 : : * index support machinery doesn't do that, and thus
2122 : : * neither does this code.
2123 : : */
2124 : :
2125 : : /* evaluate left and right args directly into fcinfo */
3113 tgl@sss.pgh.pa.us 2126 : 412 : ExecInitExprRec(left_expr, state,
2127 : : &fcinfo->args[0].value, &fcinfo->args[0].isnull);
2128 : 412 : ExecInitExprRec(right_expr, state,
2129 : : &fcinfo->args[1].value, &fcinfo->args[1].isnull);
2130 : :
3395 andres@anarazel.de 2131 : 412 : scratch.opcode = EEOP_ROWCOMPARE_STEP;
2132 : 412 : scratch.d.rowcompare_step.finfo = finfo;
2133 : 412 : scratch.d.rowcompare_step.fcinfo_data = fcinfo;
2134 : 412 : scratch.d.rowcompare_step.fn_addr = finfo->fn_addr;
2135 : : /* jump targets filled below */
2136 : 412 : scratch.d.rowcompare_step.jumpnull = -1;
2137 : 412 : scratch.d.rowcompare_step.jumpdone = -1;
2138 : :
2139 : 412 : ExprEvalPushStep(state, &scratch);
2140 : 412 : adjust_jumps = lappend_int(adjust_jumps,
2141 : 412 : state->steps_len - 1);
2142 : : }
2143 : :
2144 : : /*
2145 : : * We could have a zero-column rowtype, in which case the rows
2146 : : * necessarily compare equal.
2147 : : */
2148 [ - + ]: 188 : if (nopers == 0)
2149 : : {
3395 andres@anarazel.de 2150 :UBC 0 : scratch.opcode = EEOP_CONST;
2151 : 0 : scratch.d.constval.value = Int32GetDatum(0);
2152 : 0 : scratch.d.constval.isnull = false;
2153 : 0 : ExprEvalPushStep(state, &scratch);
2154 : : }
2155 : :
2156 : : /* Finally, examine the last comparison result */
3395 andres@anarazel.de 2157 :CBC 188 : scratch.opcode = EEOP_ROWCOMPARE_FINAL;
531 peter@eisentraut.org 2158 : 188 : scratch.d.rowcompare_final.cmptype = rcexpr->cmptype;
3395 andres@anarazel.de 2159 : 188 : ExprEvalPushStep(state, &scratch);
2160 : :
2161 : : /* adjust jump targets */
2162 [ + - + + : 600 : foreach(lc, adjust_jumps)
+ + ]
2163 : : {
2164 : 412 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
2165 : :
2166 [ - + ]: 412 : Assert(as->opcode == EEOP_ROWCOMPARE_STEP);
2167 [ - + ]: 412 : Assert(as->d.rowcompare_step.jumpdone == -1);
2168 [ - + ]: 412 : Assert(as->d.rowcompare_step.jumpnull == -1);
2169 : :
2170 : : /* jump to comparison evaluation */
2171 : 412 : as->d.rowcompare_step.jumpdone = state->steps_len - 1;
2172 : : /* jump to the following expression */
2173 : 412 : as->d.rowcompare_step.jumpnull = state->steps_len;
2174 : : }
2175 : :
2176 : 188 : break;
2177 : : }
2178 : :
2179 : 2649 : case T_CoalesceExpr:
2180 : : {
2181 : 2649 : CoalesceExpr *coalesce = (CoalesceExpr *) node;
2182 : 2649 : List *adjust_jumps = NIL;
2183 : : ListCell *lc;
2184 : :
2185 : : /* We assume there's at least one arg */
2186 [ - + ]: 2649 : Assert(coalesce->args != NIL);
2187 : :
2188 : : /*
2189 : : * Prepare evaluation of all coalesced arguments, after each
2190 : : * one push a step that short-circuits if not null.
2191 : : */
2192 [ + - + + : 7955 : foreach(lc, coalesce->args)
+ + ]
2193 : : {
2194 : 5306 : Expr *e = (Expr *) lfirst(lc);
2195 : :
2196 : : /* evaluate argument, directly into result datum */
3113 tgl@sss.pgh.pa.us 2197 : 5306 : ExecInitExprRec(e, state, resv, resnull);
2198 : :
2199 : : /* if it's not null, skip to end of COALESCE expr */
3395 andres@anarazel.de 2200 : 5306 : scratch.opcode = EEOP_JUMP_IF_NOT_NULL;
3296 tgl@sss.pgh.pa.us 2201 : 5306 : scratch.d.jump.jumpdone = -1; /* adjust later */
3395 andres@anarazel.de 2202 : 5306 : ExprEvalPushStep(state, &scratch);
2203 : :
2204 : 5306 : adjust_jumps = lappend_int(adjust_jumps,
2205 : 5306 : state->steps_len - 1);
2206 : : }
2207 : :
2208 : : /*
2209 : : * No need to add a constant NULL return - we only can get to
2210 : : * the end of the expression if a NULL already is being
2211 : : * returned.
2212 : : */
2213 : :
2214 : : /* adjust jump targets */
2215 [ + - + + : 7955 : foreach(lc, adjust_jumps)
+ + ]
2216 : : {
2217 : 5306 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
2218 : :
2219 [ - + ]: 5306 : Assert(as->opcode == EEOP_JUMP_IF_NOT_NULL);
2220 [ - + ]: 5306 : Assert(as->d.jump.jumpdone == -1);
2221 : 5306 : as->d.jump.jumpdone = state->steps_len;
2222 : : }
2223 : :
2224 : 2649 : break;
2225 : : }
2226 : :
2227 : 1310 : case T_MinMaxExpr:
2228 : : {
2229 : 1310 : MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
2230 : 1310 : int nelems = list_length(minmaxexpr->args);
2231 : : TypeCacheEntry *typentry;
2232 : : FmgrInfo *finfo;
2233 : : FunctionCallInfo fcinfo;
2234 : : ListCell *lc;
2235 : : int off;
2236 : :
2237 : : /* Look up the btree comparison function for the datatype */
2238 : 1310 : typentry = lookup_type_cache(minmaxexpr->minmaxtype,
2239 : : TYPECACHE_CMP_PROC);
2240 [ - + ]: 1310 : if (!OidIsValid(typentry->cmp_proc))
3395 andres@anarazel.de 2241 [ # # ]:UBC 0 : ereport(ERROR,
2242 : : (errcode(ERRCODE_UNDEFINED_FUNCTION),
2243 : : errmsg("could not identify a comparison function for type %s",
2244 : : format_type_be(minmaxexpr->minmaxtype))));
2245 : :
2246 : : /*
2247 : : * If we enforced permissions checks on index support
2248 : : * functions, we'd need to make a check here. But the index
2249 : : * support machinery doesn't do that, and thus neither does
2250 : : * this code.
2251 : : */
2252 : :
2253 : : /* Perform function lookup */
202 michael@paquier.xyz 2254 :GNC 1310 : finfo = palloc0_object(FmgrInfo);
2712 andres@anarazel.de 2255 :CBC 1310 : fcinfo = palloc0(SizeForFunctionCallInfo(2));
3395 2256 : 1310 : fmgr_info(typentry->cmp_proc, finfo);
2257 : 1310 : fmgr_info_set_expr((Node *) node, finfo);
2258 : 1310 : InitFunctionCallInfoData(*fcinfo, finfo, 2,
2259 : : minmaxexpr->inputcollid, NULL, NULL);
2260 : :
2261 : 1310 : scratch.opcode = EEOP_MINMAX;
2262 : : /* allocate space to store arguments */
202 michael@paquier.xyz 2263 :GNC 1310 : scratch.d.minmax.values = palloc_array(Datum, nelems);
2264 : 1310 : scratch.d.minmax.nulls = palloc_array(bool, nelems);
3395 andres@anarazel.de 2265 :CBC 1310 : scratch.d.minmax.nelems = nelems;
2266 : :
2267 : 1310 : scratch.d.minmax.op = minmaxexpr->op;
2268 : 1310 : scratch.d.minmax.finfo = finfo;
2269 : 1310 : scratch.d.minmax.fcinfo_data = fcinfo;
2270 : :
2271 : : /* evaluate expressions into minmax->values/nulls */
2272 : 1310 : off = 0;
2273 [ + - + + : 4002 : foreach(lc, minmaxexpr->args)
+ + ]
2274 : : {
2275 : 2692 : Expr *e = (Expr *) lfirst(lc);
2276 : :
3113 tgl@sss.pgh.pa.us 2277 : 2692 : ExecInitExprRec(e, state,
3395 andres@anarazel.de 2278 : 2692 : &scratch.d.minmax.values[off],
2279 : 2692 : &scratch.d.minmax.nulls[off]);
2280 : 2692 : off++;
2281 : : }
2282 : :
2283 : : /* and push the final comparison */
2284 : 1310 : ExprEvalPushStep(state, &scratch);
2285 : 1310 : break;
2286 : : }
2287 : :
1140 michael@paquier.xyz 2288 : 3790 : case T_SQLValueFunction:
2289 : : {
2290 : 3790 : SQLValueFunction *svf = (SQLValueFunction *) node;
2291 : :
2292 : 3790 : scratch.opcode = EEOP_SQLVALUEFUNCTION;
2293 : 3790 : scratch.d.sqlvaluefunction.svf = svf;
2294 : :
2295 : 3790 : ExprEvalPushStep(state, &scratch);
2296 : 3790 : break;
2297 : : }
2298 : :
3395 andres@anarazel.de 2299 : 468 : case T_XmlExpr:
2300 : : {
2301 : 468 : XmlExpr *xexpr = (XmlExpr *) node;
2302 : 468 : int nnamed = list_length(xexpr->named_args);
2303 : 468 : int nargs = list_length(xexpr->args);
2304 : : int off;
2305 : : ListCell *arg;
2306 : :
2307 : 468 : scratch.opcode = EEOP_XMLEXPR;
2308 : 468 : scratch.d.xmlexpr.xexpr = xexpr;
2309 : :
2310 : : /* allocate space for storing all the arguments */
2311 [ + + ]: 468 : if (nnamed)
2312 : : {
202 michael@paquier.xyz 2313 :GNC 40 : scratch.d.xmlexpr.named_argvalue = palloc_array(Datum, nnamed);
2314 : 40 : scratch.d.xmlexpr.named_argnull = palloc_array(bool, nnamed);
2315 : : }
2316 : : else
2317 : : {
3395 andres@anarazel.de 2318 :CBC 428 : scratch.d.xmlexpr.named_argvalue = NULL;
2319 : 428 : scratch.d.xmlexpr.named_argnull = NULL;
2320 : : }
2321 : :
2322 [ + + ]: 468 : if (nargs)
2323 : : {
202 michael@paquier.xyz 2324 :GNC 412 : scratch.d.xmlexpr.argvalue = palloc_array(Datum, nargs);
2325 : 412 : scratch.d.xmlexpr.argnull = palloc_array(bool, nargs);
2326 : : }
2327 : : else
2328 : : {
3395 andres@anarazel.de 2329 :CBC 56 : scratch.d.xmlexpr.argvalue = NULL;
2330 : 56 : scratch.d.xmlexpr.argnull = NULL;
2331 : : }
2332 : :
2333 : : /* prepare argument execution */
2334 : 468 : off = 0;
2335 [ + + + + : 580 : foreach(arg, xexpr->named_args)
+ + ]
2336 : : {
2337 : 112 : Expr *e = (Expr *) lfirst(arg);
2338 : :
3113 tgl@sss.pgh.pa.us 2339 : 112 : ExecInitExprRec(e, state,
3395 andres@anarazel.de 2340 : 112 : &scratch.d.xmlexpr.named_argvalue[off],
2341 : 112 : &scratch.d.xmlexpr.named_argnull[off]);
2342 : 112 : off++;
2343 : : }
2344 : :
2345 : 468 : off = 0;
2346 [ + + + + : 1092 : foreach(arg, xexpr->args)
+ + ]
2347 : : {
2348 : 624 : Expr *e = (Expr *) lfirst(arg);
2349 : :
3113 tgl@sss.pgh.pa.us 2350 : 624 : ExecInitExprRec(e, state,
3395 andres@anarazel.de 2351 : 624 : &scratch.d.xmlexpr.argvalue[off],
2352 : 624 : &scratch.d.xmlexpr.argnull[off]);
2353 : 624 : off++;
2354 : : }
2355 : :
2356 : : /* and evaluate the actual XML expression */
2357 : 468 : ExprEvalPushStep(state, &scratch);
2358 : 468 : break;
2359 : : }
2360 : :
1189 alvherre@alvh.no-ip. 2361 : 160 : case T_JsonValueExpr:
2362 : : {
2363 : 160 : JsonValueExpr *jve = (JsonValueExpr *) node;
2364 : :
618 amitlan@postgresql.o 2365 [ - + ]: 160 : Assert(jve->raw_expr != NULL);
2366 : 160 : ExecInitExprRec(jve->raw_expr, state, resv, resnull);
1089 2367 [ - + ]: 160 : Assert(jve->formatted_expr != NULL);
2368 : 160 : ExecInitExprRec(jve->formatted_expr, state, resv, resnull);
1189 alvherre@alvh.no-ip. 2369 : 160 : break;
2370 : : }
2371 : :
2372 : 901 : case T_JsonConstructorExpr:
2373 : : {
2374 : 901 : JsonConstructorExpr *ctor = (JsonConstructorExpr *) node;
2375 : 901 : List *args = ctor->args;
2376 : : ListCell *lc;
2377 : 901 : int nargs = list_length(args);
2378 : 901 : int argno = 0;
2379 : :
2380 [ + + ]: 901 : if (ctor->func)
2381 : : {
2382 : 312 : ExecInitExprRec(ctor->func, state, resv, resnull);
2383 : : }
1076 amitlan@postgresql.o 2384 [ + + + + ]: 589 : else if ((ctor->type == JSCTOR_JSON_PARSE && !ctor->unique) ||
2385 [ + + ]: 516 : ctor->type == JSCTOR_JSON_SERIALIZE)
2386 : : {
2387 : : /* Use the value of the first argument as result */
2388 : 130 : ExecInitExprRec(linitial(args), state, resv, resnull);
2389 : : }
2390 : : else
2391 : : {
2392 : : JsonConstructorExprState *jcstate;
2393 : :
202 michael@paquier.xyz 2394 :GNC 459 : jcstate = palloc0_object(JsonConstructorExprState);
2395 : :
1189 alvherre@alvh.no-ip. 2396 :CBC 459 : scratch.opcode = EEOP_JSON_CONSTRUCTOR;
2397 : 459 : scratch.d.json_constructor.jcstate = jcstate;
2398 : :
2399 : 459 : jcstate->constructor = ctor;
202 michael@paquier.xyz 2400 :GNC 459 : jcstate->arg_values = palloc_array(Datum, nargs);
2401 : 459 : jcstate->arg_nulls = palloc_array(bool, nargs);
2402 : 459 : jcstate->arg_types = palloc_array(Oid, nargs);
1189 alvherre@alvh.no-ip. 2403 :CBC 459 : jcstate->nargs = nargs;
2404 : :
2405 [ + + + + : 1457 : foreach(lc, args)
+ + ]
2406 : : {
2407 : 998 : Expr *arg = (Expr *) lfirst(lc);
2408 : :
2409 : 998 : jcstate->arg_types[argno] = exprType((Node *) arg);
2410 : :
2411 [ + + ]: 998 : if (IsA(arg, Const))
2412 : : {
2413 : : /* Don't evaluate const arguments every round */
2414 : 906 : Const *con = (Const *) arg;
2415 : :
2416 : 906 : jcstate->arg_values[argno] = con->constvalue;
2417 : 906 : jcstate->arg_nulls[argno] = con->constisnull;
2418 : : }
2419 : : else
2420 : : {
2421 : 92 : ExecInitExprRec(arg, state,
2422 : 92 : &jcstate->arg_values[argno],
2423 : 92 : &jcstate->arg_nulls[argno]);
2424 : : }
2425 : 998 : argno++;
2426 : : }
2427 : :
2428 : : /* prepare type cache for datum_to_json[b]() */
1076 amitlan@postgresql.o 2429 [ + + ]: 459 : if (ctor->type == JSCTOR_JSON_SCALAR)
2430 : : {
2431 : 63 : bool is_jsonb =
2432 : 63 : ctor->returning->format->format_type == JS_FORMAT_JSONB;
2433 : :
2434 : 63 : jcstate->arg_type_cache =
2435 : 63 : palloc(sizeof(*jcstate->arg_type_cache) * nargs);
2436 : :
2437 [ + + ]: 126 : for (int i = 0; i < nargs; i++)
2438 : : {
2439 : : JsonTypeCategory category;
2440 : : Oid outfuncid;
2441 : 63 : Oid typid = jcstate->arg_types[i];
2442 : :
2443 : 63 : json_categorize_type(typid, is_jsonb,
2444 : : &category, &outfuncid);
2445 : :
2446 : 63 : jcstate->arg_type_cache[i].outfuncid = outfuncid;
2447 : 63 : jcstate->arg_type_cache[i].category = (int) category;
2448 : : }
2449 : : }
2450 : :
1189 alvherre@alvh.no-ip. 2451 : 459 : ExprEvalPushStep(state, &scratch);
2452 : : }
2453 : :
2454 [ + + ]: 901 : if (ctor->coercion)
2455 : : {
2456 : 248 : Datum *innermost_caseval = state->innermost_caseval;
2457 : 248 : bool *innermost_isnull = state->innermost_casenull;
2458 : :
2459 : 248 : state->innermost_caseval = resv;
2460 : 248 : state->innermost_casenull = resnull;
2461 : :
2462 : 248 : ExecInitExprRec(ctor->coercion, state, resv, resnull);
2463 : :
2464 : 248 : state->innermost_caseval = innermost_caseval;
2465 : 248 : state->innermost_casenull = innermost_isnull;
2466 : : }
2467 : : }
2468 : 901 : break;
2469 : :
1187 2470 : 260 : case T_JsonIsPredicate:
2471 : : {
2472 : 260 : JsonIsPredicate *pred = (JsonIsPredicate *) node;
2473 : :
2474 : 260 : ExecInitExprRec((Expr *) pred->expr, state, resv, resnull);
2475 : :
2476 : 260 : scratch.opcode = EEOP_IS_JSON;
2477 : 260 : scratch.d.is_json.pred = pred;
2478 : :
2479 : 260 : ExprEvalPushStep(state, &scratch);
2480 : 260 : break;
2481 : : }
2482 : :
831 amitlan@postgresql.o 2483 : 1804 : case T_JsonExpr:
2484 : : {
2485 : 1804 : JsonExpr *jsexpr = castNode(JsonExpr, node);
2486 : :
2487 : : /*
2488 : : * No need to initialize a full JsonExprState For
2489 : : * JSON_TABLE(), because the upstream caller tfuncFetchRows()
2490 : : * is only interested in the value of formatted_expr.
2491 : : */
817 2492 [ + + ]: 1804 : if (jsexpr->op == JSON_TABLE_OP)
2493 : 266 : ExecInitExprRec((Expr *) jsexpr->formatted_expr, state,
2494 : : resv, resnull);
2495 : : else
2496 : 1538 : ExecInitJsonExpr(jsexpr, state, resv, resnull, &scratch);
831 2497 : 1804 : break;
2498 : : }
2499 : :
3395 andres@anarazel.de 2500 : 19014 : case T_NullTest:
2501 : : {
2502 : 19014 : NullTest *ntest = (NullTest *) node;
2503 : :
2504 [ + + ]: 19014 : if (ntest->nulltesttype == IS_NULL)
2505 : : {
2506 [ + + ]: 5222 : if (ntest->argisrow)
2507 : 142 : scratch.opcode = EEOP_NULLTEST_ROWISNULL;
2508 : : else
2509 : 5080 : scratch.opcode = EEOP_NULLTEST_ISNULL;
2510 : : }
2511 [ + - ]: 13792 : else if (ntest->nulltesttype == IS_NOT_NULL)
2512 : : {
2513 [ + + ]: 13792 : if (ntest->argisrow)
2514 : 148 : scratch.opcode = EEOP_NULLTEST_ROWISNOTNULL;
2515 : : else
2516 : 13644 : scratch.opcode = EEOP_NULLTEST_ISNOTNULL;
2517 : : }
2518 : : else
2519 : : {
3395 andres@anarazel.de 2520 [ # # ]:UBC 0 : elog(ERROR, "unrecognized nulltesttype: %d",
2521 : : (int) ntest->nulltesttype);
2522 : : }
2523 : : /* initialize cache in case it's a row test */
1904 tgl@sss.pgh.pa.us 2524 :CBC 19014 : scratch.d.nulltest_row.rowcache.cacheptr = NULL;
2525 : :
2526 : : /* first evaluate argument into result variable */
3113 2527 : 19014 : ExecInitExprRec(ntest->arg, state,
2528 : : resv, resnull);
2529 : :
2530 : : /* then push the test of that argument */
3395 andres@anarazel.de 2531 : 19014 : ExprEvalPushStep(state, &scratch);
2532 : 19014 : break;
2533 : : }
2534 : :
2535 : 902 : case T_BooleanTest:
2536 : : {
2537 : 902 : BooleanTest *btest = (BooleanTest *) node;
2538 : :
2539 : : /*
2540 : : * Evaluate argument, directly into result datum. That's ok,
2541 : : * because resv/resnull is definitely not used anywhere else,
2542 : : * and will get overwritten by the below EEOP_BOOLTEST_IS_*
2543 : : * step.
2544 : : */
3113 tgl@sss.pgh.pa.us 2545 : 902 : ExecInitExprRec(btest->arg, state, resv, resnull);
2546 : :
3395 andres@anarazel.de 2547 [ + + + + : 902 : switch (btest->booltesttype)
+ + - ]
2548 : : {
2549 : 277 : case IS_TRUE:
2550 : 277 : scratch.opcode = EEOP_BOOLTEST_IS_TRUE;
2551 : 277 : break;
2552 : 263 : case IS_NOT_TRUE:
2553 : 263 : scratch.opcode = EEOP_BOOLTEST_IS_NOT_TRUE;
2554 : 263 : break;
2555 : 127 : case IS_FALSE:
2556 : 127 : scratch.opcode = EEOP_BOOLTEST_IS_FALSE;
2557 : 127 : break;
2558 : 113 : case IS_NOT_FALSE:
2559 : 113 : scratch.opcode = EEOP_BOOLTEST_IS_NOT_FALSE;
2560 : 113 : break;
2561 : 46 : case IS_UNKNOWN:
2562 : : /* Same as scalar IS NULL test */
2563 : 46 : scratch.opcode = EEOP_NULLTEST_ISNULL;
2564 : 46 : break;
2565 : 76 : case IS_NOT_UNKNOWN:
2566 : : /* Same as scalar IS NOT NULL test */
2567 : 76 : scratch.opcode = EEOP_NULLTEST_ISNOTNULL;
2568 : 76 : break;
3395 andres@anarazel.de 2569 :UBC 0 : default:
2570 [ # # ]: 0 : elog(ERROR, "unrecognized booltesttype: %d",
2571 : : (int) btest->booltesttype);
2572 : : }
2573 : :
3395 andres@anarazel.de 2574 :CBC 902 : ExprEvalPushStep(state, &scratch);
2575 : 902 : break;
2576 : : }
2577 : :
2578 : 5896 : case T_CoerceToDomain:
2579 : : {
2580 : 5896 : CoerceToDomain *ctest = (CoerceToDomain *) node;
2581 : :
3113 tgl@sss.pgh.pa.us 2582 : 5896 : ExecInitCoerceToDomain(&scratch, ctest, state,
2583 : : resv, resnull);
3395 andres@anarazel.de 2584 : 5896 : break;
2585 : : }
2586 : :
2587 : 8488 : case T_CoerceToDomainValue:
2588 : : {
2589 : : /*
2590 : : * Read from location identified by innermost_domainval. Note
2591 : : * that innermost_domainval could be NULL, if we're compiling
2592 : : * a standalone domain check rather than one embedded in a
2593 : : * larger expression. In that case we must read from
2594 : : * econtext->domainValue_datum. We'll take care of that by
2595 : : * generating a specialized operation.
2596 : : */
516 tgl@sss.pgh.pa.us 2597 [ + + ]: 8488 : if (state->innermost_domainval == NULL)
2598 : 1940 : scratch.opcode = EEOP_DOMAIN_TESTVAL_EXT;
2599 : : else
2600 : : {
2601 : 6548 : scratch.opcode = EEOP_DOMAIN_TESTVAL;
2602 : : /* we share instruction union variant with case testval */
2603 : 6548 : scratch.d.casetest.value = state->innermost_domainval;
2604 : 6548 : scratch.d.casetest.isnull = state->innermost_domainnull;
2605 : : }
3395 andres@anarazel.de 2606 : 8488 : ExprEvalPushStep(state, &scratch);
2607 : 8488 : break;
2608 : : }
2609 : :
2610 : 1 : case T_CurrentOfExpr:
2611 : : {
2612 : 1 : scratch.opcode = EEOP_CURRENTOFEXPR;
2613 : 1 : ExprEvalPushStep(state, &scratch);
2614 : 1 : break;
2615 : : }
2616 : :
3372 peter_e@gmx.net 2617 : 462 : case T_NextValueExpr:
2618 : : {
2619 : 462 : NextValueExpr *nve = (NextValueExpr *) node;
2620 : :
2621 : 462 : scratch.opcode = EEOP_NEXTVALUEEXPR;
2622 : 462 : scratch.d.nextvalueexpr.seqid = nve->seqid;
2623 : 462 : scratch.d.nextvalueexpr.seqtypid = nve->typeId;
2624 : :
2625 : 462 : ExprEvalPushStep(state, &scratch);
2626 : 462 : break;
2627 : : }
2628 : :
530 dean.a.rasheed@gmail 2629 : 304 : case T_ReturningExpr:
2630 : : {
2631 : 304 : ReturningExpr *rexpr = (ReturningExpr *) node;
2632 : : int retstep;
2633 : :
2634 : : /* Skip expression evaluation if OLD/NEW row doesn't exist */
2635 : 304 : scratch.opcode = EEOP_RETURNINGEXPR;
2636 [ + + ]: 304 : scratch.d.returningexpr.nullflag = rexpr->retold ?
2637 : : EEO_FLAG_OLD_IS_NULL : EEO_FLAG_NEW_IS_NULL;
2638 : 304 : scratch.d.returningexpr.jumpdone = -1; /* set below */
2639 : 304 : ExprEvalPushStep(state, &scratch);
2640 : 304 : retstep = state->steps_len - 1;
2641 : :
2642 : : /* Steps to evaluate expression to return */
2643 : 304 : ExecInitExprRec(rexpr->retexpr, state, resv, resnull);
2644 : :
2645 : : /* Jump target used if OLD/NEW row doesn't exist */
2646 : 304 : state->steps[retstep].d.returningexpr.jumpdone = state->steps_len;
2647 : :
2648 : : /* Update ExprState flags */
2649 [ + + ]: 304 : if (rexpr->retold)
2650 : 152 : state->flags |= EEO_FLAG_HAS_OLD;
2651 : : else
2652 : 152 : state->flags |= EEO_FLAG_HAS_NEW;
2653 : :
2654 : 304 : break;
2655 : : }
2656 : :
3395 andres@anarazel.de 2657 :UBC 0 : default:
2658 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d",
2659 : : (int) nodeTag(node));
2660 : : break;
2661 : : }
3395 andres@anarazel.de 2662 :CBC 3435841 : }
2663 : :
2664 : : /*
2665 : : * Add another expression evaluation step to ExprState->steps.
2666 : : *
2667 : : * Note that this potentially re-allocates es->steps, therefore no pointer
2668 : : * into that array may be used while the expression is still being built.
2669 : : */
2670 : : void
2671 : 7910330 : ExprEvalPushStep(ExprState *es, const ExprEvalStep *s)
2672 : : {
2673 [ + + ]: 7910330 : if (es->steps_alloc == 0)
2674 : : {
2675 : 1593586 : es->steps_alloc = 16;
202 michael@paquier.xyz 2676 :GNC 1593586 : es->steps = palloc_array(ExprEvalStep, es->steps_alloc);
2677 : : }
3395 andres@anarazel.de 2678 [ + + ]:CBC 6316744 : else if (es->steps_alloc == es->steps_len)
2679 : : {
2680 : 59155 : es->steps_alloc *= 2;
2681 : 59155 : es->steps = repalloc(es->steps,
2682 : 59155 : sizeof(ExprEvalStep) * es->steps_alloc);
2683 : : }
2684 : :
2685 : 7910330 : memcpy(&es->steps[es->steps_len++], s, sizeof(ExprEvalStep));
2686 : 7910330 : }
2687 : :
2688 : : /*
2689 : : * Perform setup necessary for the evaluation of a function-like expression,
2690 : : * appending argument evaluation steps to the steps list in *state, and
2691 : : * setting up *scratch so it is ready to be pushed.
2692 : : *
2693 : : * *scratch is not pushed here, so that callers may override the opcode,
2694 : : * which is useful for function-like cases like DISTINCT.
2695 : : */
2696 : : static void
2697 : 945638 : ExecInitFunc(ExprEvalStep *scratch, Expr *node, List *args, Oid funcid,
2698 : : Oid inputcollid, ExprState *state)
2699 : : {
2700 : 945638 : int nargs = list_length(args);
2701 : : AclResult aclresult;
2702 : : FmgrInfo *flinfo;
2703 : : FunctionCallInfo fcinfo;
2704 : : int argno;
2705 : : ListCell *lc;
2706 : :
2707 : : /* Check permission to call function */
1325 peter@eisentraut.org 2708 : 945638 : aclresult = object_aclcheck(ProcedureRelationId, funcid, GetUserId(), ACL_EXECUTE);
3395 andres@anarazel.de 2709 [ + + ]: 945638 : if (aclresult != ACLCHECK_OK)
3132 peter_e@gmx.net 2710 : 52 : aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(funcid));
3395 andres@anarazel.de 2711 [ - + ]: 945586 : InvokeFunctionExecuteHook(funcid);
2712 : :
2713 : : /*
2714 : : * Safety check on nargs. Under normal circumstances this should never
2715 : : * fail, as parser should check sooner. But possibly it might fail if
2716 : : * server has been compiled with FUNC_MAX_ARGS smaller than some functions
2717 : : * declared in pg_proc?
2718 : : */
2719 [ - + ]: 945586 : if (nargs > FUNC_MAX_ARGS)
3395 andres@anarazel.de 2720 [ # # ]:UBC 0 : ereport(ERROR,
2721 : : (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
2722 : : errmsg_plural("cannot pass more than %d argument to a function",
2723 : : "cannot pass more than %d arguments to a function",
2724 : : FUNC_MAX_ARGS,
2725 : : FUNC_MAX_ARGS)));
2726 : :
2727 : : /* Allocate function lookup data and parameter workspace for this call */
202 michael@paquier.xyz 2728 :GNC 945586 : scratch->d.func.finfo = palloc0_object(FmgrInfo);
2712 andres@anarazel.de 2729 :CBC 945586 : scratch->d.func.fcinfo_data = palloc0(SizeForFunctionCallInfo(nargs));
3395 2730 : 945586 : flinfo = scratch->d.func.finfo;
2731 : 945586 : fcinfo = scratch->d.func.fcinfo_data;
2732 : :
2733 : : /* Set up the primary fmgr lookup information */
2734 : 945586 : fmgr_info(funcid, flinfo);
2735 : 945586 : fmgr_info_set_expr((Node *) node, flinfo);
2736 : :
2737 : : /* Initialize function call parameter structure too */
2738 : 945586 : InitFunctionCallInfoData(*fcinfo, flinfo,
2739 : : nargs, inputcollid, NULL, NULL);
2740 : :
2741 : : /* Keep extra copies of this info to save an indirection at runtime */
2742 : 945586 : scratch->d.func.fn_addr = flinfo->fn_addr;
2743 : 945586 : scratch->d.func.nargs = nargs;
2744 : :
2745 : : /* We only support non-set functions here */
2746 [ - + ]: 945586 : if (flinfo->fn_retset)
3395 andres@anarazel.de 2747 [ # # # # ]:UBC 0 : ereport(ERROR,
2748 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2749 : : errmsg("set-valued function called in context that cannot accept a set"),
2750 : : state->parent ?
2751 : : executor_errposition(state->parent->state,
2752 : : exprLocation((Node *) node)) : 0));
2753 : :
2754 : : /* Build code to evaluate arguments directly into the fcinfo struct */
3395 andres@anarazel.de 2755 :CBC 945586 : argno = 0;
2756 [ + + + + : 2605592 : foreach(lc, args)
+ + ]
2757 : : {
2758 : 1660006 : Expr *arg = (Expr *) lfirst(lc);
2759 : :
2760 [ + + ]: 1660006 : if (IsA(arg, Const))
2761 : : {
2762 : : /*
2763 : : * Don't evaluate const arguments every round; especially
2764 : : * interesting for constants in comparisons.
2765 : : */
2766 : 695097 : Const *con = (Const *) arg;
2767 : :
2712 2768 : 695097 : fcinfo->args[argno].value = con->constvalue;
2769 : 695097 : fcinfo->args[argno].isnull = con->constisnull;
2770 : : }
2771 : : else
2772 : : {
3113 tgl@sss.pgh.pa.us 2773 : 964909 : ExecInitExprRec(arg, state,
2774 : : &fcinfo->args[argno].value,
2775 : : &fcinfo->args[argno].isnull);
2776 : : }
3395 andres@anarazel.de 2777 : 1660006 : argno++;
2778 : : }
2779 : :
2780 : : /* Insert appropriate opcode depending on strictness and stats level */
2781 [ + + ]: 945586 : if (pgstat_track_functions <= flinfo->fn_stats)
2782 : : {
2783 [ + + + + ]: 945470 : if (flinfo->fn_strict && nargs > 0)
2784 : : {
2785 : : /* Choose nargs optimized implementation if available. */
476 dgustafsson@postgres 2786 [ + + ]: 854841 : if (nargs == 1)
2787 : 164559 : scratch->opcode = EEOP_FUNCEXPR_STRICT_1;
2788 [ + + ]: 690282 : else if (nargs == 2)
2789 : 664101 : scratch->opcode = EEOP_FUNCEXPR_STRICT_2;
2790 : : else
2791 : 26181 : scratch->opcode = EEOP_FUNCEXPR_STRICT;
2792 : : }
2793 : : else
3395 andres@anarazel.de 2794 : 90629 : scratch->opcode = EEOP_FUNCEXPR;
2795 : : }
2796 : : else
2797 : : {
2798 [ + + + - ]: 116 : if (flinfo->fn_strict && nargs > 0)
2799 : 4 : scratch->opcode = EEOP_FUNCEXPR_STRICT_FUSAGE;
2800 : : else
2801 : 112 : scratch->opcode = EEOP_FUNCEXPR_FUSAGE;
2802 : : }
2803 : 945586 : }
2804 : :
2805 : : /*
2806 : : * Append the steps necessary for the evaluation of a SubPlan node to
2807 : : * ExprState->steps.
2808 : : *
2809 : : * subplan - SubPlan expression to evaluate
2810 : : * state - ExprState to whose ->steps to append the necessary operations
2811 : : * resv / resnull - where to store the result of the node into
2812 : : */
2813 : : static void
699 2814 : 18682 : ExecInitSubPlanExpr(SubPlan *subplan,
2815 : : ExprState *state,
2816 : : Datum *resv, bool *resnull)
2817 : : {
2818 : 18682 : ExprEvalStep scratch = {0};
2819 : : SubPlanState *sstate;
2820 : : ListCell *pvar;
2821 : : ListCell *l;
2822 : :
2823 [ - + ]: 18682 : if (!state->parent)
699 andres@anarazel.de 2824 [ # # ]:UBC 0 : elog(ERROR, "SubPlan found with no parent plan");
2825 : :
2826 : : /*
2827 : : * Generate steps to evaluate input arguments for the subplan.
2828 : : *
2829 : : * We evaluate the argument expressions into resv/resnull, and then use
2830 : : * PARAM_SET to update the parameter. We do that, instead of evaluating
2831 : : * directly into the param, to avoid depending on the pointer value
2832 : : * remaining stable / being included in the generated expression. It's ok
2833 : : * to use resv/resnull for multiple params, as each parameter evaluation
2834 : : * is immediately followed by an EEOP_PARAM_SET (and thus are saved before
2835 : : * they could be overwritten again).
2836 : : *
2837 : : * Any calculation we have to do can be done in the parent econtext, since
2838 : : * the Param values don't need to have per-query lifetime.
2839 : : */
699 andres@anarazel.de 2840 [ - + ]:CBC 18682 : Assert(list_length(subplan->parParam) == list_length(subplan->args));
2841 [ + + + + : 47598 : forboth(l, subplan->parParam, pvar, subplan->args)
+ + + + +
+ + - +
+ ]
2842 : : {
2843 : 28916 : int paramid = lfirst_int(l);
2844 : 28916 : Expr *arg = (Expr *) lfirst(pvar);
2845 : :
175 2846 : 28916 : ExecInitExprRec(arg, state, resv, resnull);
2847 : :
699 2848 : 28916 : scratch.opcode = EEOP_PARAM_SET;
175 2849 : 28916 : scratch.resvalue = resv;
2850 : 28916 : scratch.resnull = resnull;
699 2851 : 28916 : scratch.d.param.paramid = paramid;
2852 : : /* paramtype's not actually used, but we might as well fill it */
2853 : 28916 : scratch.d.param.paramtype = exprType((Node *) arg);
2854 : 28916 : ExprEvalPushStep(state, &scratch);
2855 : : }
2856 : :
2857 : 18682 : sstate = ExecInitSubPlan(subplan, state->parent);
2858 : :
2859 : : /* add SubPlanState nodes to state->parent->subPlan */
2860 : 18682 : state->parent->subPlan = lappend(state->parent->subPlan,
2861 : : sstate);
2862 : :
2863 : 18682 : scratch.opcode = EEOP_SUBPLAN;
2864 : 18682 : scratch.resvalue = resv;
2865 : 18682 : scratch.resnull = resnull;
2866 : 18682 : scratch.d.subplan.sstate = sstate;
2867 : :
2868 : 18682 : ExprEvalPushStep(state, &scratch);
2869 : 18682 : }
2870 : :
2871 : : /*
2872 : : * Add expression steps performing setup that's needed before any of the
2873 : : * main execution of the expression.
2874 : : */
2875 : : static void
1221 tgl@sss.pgh.pa.us 2876 : 1523880 : ExecCreateExprSetupSteps(ExprState *state, Node *node)
2877 : : {
530 dean.a.rasheed@gmail 2878 : 1523880 : ExprSetupInfo info = {0, 0, 0, 0, 0, NIL};
2879 : :
2880 : : /* Prescan to find out what we need. */
1221 tgl@sss.pgh.pa.us 2881 : 1523880 : expr_setup_walker(node, &info);
2882 : :
2883 : : /* And generate those steps. */
2884 : 1523876 : ExecPushExprSetupSteps(state, &info);
3094 andres@anarazel.de 2885 : 1523876 : }
2886 : :
2887 : : /*
2888 : : * Add steps performing expression setup as indicated by "info".
2889 : : * This is useful when building an ExprState covering more than one expression.
2890 : : */
2891 : : static void
1221 tgl@sss.pgh.pa.us 2892 : 1569121 : ExecPushExprSetupSteps(ExprState *state, ExprSetupInfo *info)
2893 : : {
3080 andres@anarazel.de 2894 : 1569121 : ExprEvalStep scratch = {0};
2895 : : ListCell *lc;
2896 : :
2897 : 1569121 : scratch.resvalue = NULL;
2898 : 1569121 : scratch.resnull = NULL;
2899 : :
2900 : : /*
2901 : : * Add steps deforming the ExprState's inner/outer/scan/old/new slots as
2902 : : * much as required by any Vars appearing in the expression.
2903 : : */
3094 2904 [ + + ]: 1569121 : if (info->last_inner > 0)
2905 : : {
3395 2906 : 130569 : scratch.opcode = EEOP_INNER_FETCHSOME;
3094 2907 : 130569 : scratch.d.fetch.last_var = info->last_inner;
2784 2908 : 130569 : scratch.d.fetch.fixed = false;
2909 : 130569 : scratch.d.fetch.kind = NULL;
3018 2910 : 130569 : scratch.d.fetch.known_desc = NULL;
2465 2911 [ + + ]: 130569 : if (ExecComputeSlotInfo(state, &scratch))
2912 : 120904 : ExprEvalPushStep(state, &scratch);
2913 : : }
3094 2914 [ + + ]: 1569121 : if (info->last_outer > 0)
2915 : : {
3395 2916 : 245489 : scratch.opcode = EEOP_OUTER_FETCHSOME;
3094 2917 : 245489 : scratch.d.fetch.last_var = info->last_outer;
2784 2918 : 245489 : scratch.d.fetch.fixed = false;
2919 : 245489 : scratch.d.fetch.kind = NULL;
3018 2920 : 245489 : scratch.d.fetch.known_desc = NULL;
2465 2921 [ + + ]: 245489 : if (ExecComputeSlotInfo(state, &scratch))
2922 : 136299 : ExprEvalPushStep(state, &scratch);
2923 : : }
3094 2924 [ + + ]: 1569121 : if (info->last_scan > 0)
2925 : : {
3395 2926 : 401488 : scratch.opcode = EEOP_SCAN_FETCHSOME;
3094 2927 : 401488 : scratch.d.fetch.last_var = info->last_scan;
2784 2928 : 401488 : scratch.d.fetch.fixed = false;
2929 : 401488 : scratch.d.fetch.kind = NULL;
3018 2930 : 401488 : scratch.d.fetch.known_desc = NULL;
2465 2931 [ + + ]: 401488 : if (ExecComputeSlotInfo(state, &scratch))
2932 : 375829 : ExprEvalPushStep(state, &scratch);
2933 : : }
530 dean.a.rasheed@gmail 2934 [ + + ]: 1569121 : if (info->last_old > 0)
2935 : : {
2936 : 267 : scratch.opcode = EEOP_OLD_FETCHSOME;
2937 : 267 : scratch.d.fetch.last_var = info->last_old;
2938 : 267 : scratch.d.fetch.fixed = false;
2939 : 267 : scratch.d.fetch.kind = NULL;
2940 : 267 : scratch.d.fetch.known_desc = NULL;
2941 [ + - ]: 267 : if (ExecComputeSlotInfo(state, &scratch))
2942 : 267 : ExprEvalPushStep(state, &scratch);
2943 : : }
2944 [ + + ]: 1569121 : if (info->last_new > 0)
2945 : : {
2946 : 269 : scratch.opcode = EEOP_NEW_FETCHSOME;
2947 : 269 : scratch.d.fetch.last_var = info->last_new;
2948 : 269 : scratch.d.fetch.fixed = false;
2949 : 269 : scratch.d.fetch.kind = NULL;
2950 : 269 : scratch.d.fetch.known_desc = NULL;
2951 [ + - ]: 269 : if (ExecComputeSlotInfo(state, &scratch))
2952 : 269 : ExprEvalPushStep(state, &scratch);
2953 : : }
2954 : :
2955 : : /*
2956 : : * Add steps to execute any MULTIEXPR SubPlans appearing in the
2957 : : * expression. We need to evaluate these before any of the Params
2958 : : * referencing their outputs are used, but after we've prepared for any
2959 : : * Var references they may contain. (There cannot be cross-references
2960 : : * between MULTIEXPR SubPlans, so we needn't worry about their order.)
2961 : : */
1221 tgl@sss.pgh.pa.us 2962 [ + + + + : 1569204 : foreach(lc, info->multiexpr_subplans)
+ + ]
2963 : : {
2964 : 83 : SubPlan *subplan = (SubPlan *) lfirst(lc);
2965 : :
2966 [ - + ]: 83 : Assert(subplan->subLinkType == MULTIEXPR_SUBLINK);
2967 : :
2968 : : /* The result can be ignored, but we better put it somewhere */
699 andres@anarazel.de 2969 : 83 : ExecInitSubPlanExpr(subplan, state,
2970 : : &state->resvalue, &state->resnull);
2971 : : }
3395 2972 : 1569121 : }
2973 : :
2974 : : /*
2975 : : * expr_setup_walker: expression walker for ExecCreateExprSetupSteps
2976 : : */
2977 : : static bool
1221 tgl@sss.pgh.pa.us 2978 : 7384247 : expr_setup_walker(Node *node, ExprSetupInfo *info)
2979 : : {
3395 andres@anarazel.de 2980 [ + + ]: 7384247 : if (node == NULL)
2981 : 285889 : return false;
2982 [ + + ]: 7098358 : if (IsA(node, Var))
2983 : : {
2984 : 1659117 : Var *variable = (Var *) node;
2985 : 1659117 : AttrNumber attnum = variable->varattno;
2986 : :
2987 [ + + + ]: 1659117 : switch (variable->varno)
2988 : : {
2989 : 257588 : case INNER_VAR:
2990 : 257588 : info->last_inner = Max(info->last_inner, attnum);
2991 : 257588 : break;
2992 : :
2993 : 561876 : case OUTER_VAR:
2994 : 561876 : info->last_outer = Max(info->last_outer, attnum);
2995 : 561876 : break;
2996 : :
2997 : : /* INDEX_VAR is handled by default case */
2998 : :
2999 : 839653 : default:
530 dean.a.rasheed@gmail 3000 [ + + + - ]: 839653 : switch (variable->varreturningtype)
3001 : : {
3002 : 837305 : case VAR_RETURNING_DEFAULT:
3003 : 837305 : info->last_scan = Max(info->last_scan, attnum);
3004 : 837305 : break;
3005 : 1173 : case VAR_RETURNING_OLD:
3006 : 1173 : info->last_old = Max(info->last_old, attnum);
3007 : 1173 : break;
3008 : 1175 : case VAR_RETURNING_NEW:
3009 : 1175 : info->last_new = Max(info->last_new, attnum);
3010 : 1175 : break;
3011 : : }
3395 andres@anarazel.de 3012 : 839653 : break;
3013 : : }
3014 : 1659117 : return false;
3015 : : }
3016 : :
3017 : : /* Collect all MULTIEXPR SubPlans, too */
1221 tgl@sss.pgh.pa.us 3018 [ + + ]: 5439241 : if (IsA(node, SubPlan))
3019 : : {
3020 : 18682 : SubPlan *subplan = (SubPlan *) node;
3021 : :
3022 [ + + ]: 18682 : if (subplan->subLinkType == MULTIEXPR_SUBLINK)
3023 : 83 : info->multiexpr_subplans = lappend(info->multiexpr_subplans,
3024 : : subplan);
3025 : : }
3026 : :
3027 : : /*
3028 : : * Don't examine the arguments or filters of Aggrefs or WindowFuncs,
3029 : : * because those do not represent expressions to be evaluated within the
3030 : : * calling expression's econtext. GroupingFunc arguments are never
3031 : : * evaluated at all.
3032 : : */
3395 andres@anarazel.de 3033 [ + + ]: 5439241 : if (IsA(node, Aggref))
3034 : 38622 : return false;
3035 [ + + ]: 5400619 : if (IsA(node, WindowFunc))
3036 : 2486 : return false;
3037 [ + + ]: 5398133 : if (IsA(node, GroupingFunc))
3038 : 244 : return false;
579 peter@eisentraut.org 3039 : 5397889 : return expression_tree_walker(node, expr_setup_walker, info);
3040 : : }
3041 : :
3042 : : /*
3043 : : * Compute additional information for EEOP_*_FETCHSOME ops.
3044 : : *
3045 : : * The goal is to determine whether a slot is 'fixed', that is, every
3046 : : * evaluation of the expression will have the same type of slot, with an
3047 : : * equivalent descriptor.
3048 : : *
3049 : : * EEOP_OLD_FETCHSOME and EEOP_NEW_FETCHSOME are used to process RETURNING, if
3050 : : * OLD/NEW columns are referred to explicitly. In both cases, the tuple
3051 : : * descriptor comes from the parent scan node, so we treat them the same as
3052 : : * EEOP_SCAN_FETCHSOME.
3053 : : *
3054 : : * Returns true if the deforming step is required, false otherwise.
3055 : : */
3056 : : static bool
2784 andres@anarazel.de 3057 : 813550 : ExecComputeSlotInfo(ExprState *state, ExprEvalStep *op)
3058 : : {
2596 tgl@sss.pgh.pa.us 3059 : 813550 : PlanState *parent = state->parent;
2784 andres@anarazel.de 3060 : 813550 : TupleDesc desc = NULL;
3061 : 813550 : const TupleTableSlotOps *tts_ops = NULL;
2596 tgl@sss.pgh.pa.us 3062 : 813550 : bool isfixed = false;
2465 andres@anarazel.de 3063 : 813550 : ExprEvalOp opcode = op->opcode;
3064 : :
3065 [ + + + + : 813550 : Assert(opcode == EEOP_INNER_FETCHSOME ||
+ + + + -
+ ]
3066 : : opcode == EEOP_OUTER_FETCHSOME ||
3067 : : opcode == EEOP_SCAN_FETCHSOME ||
3068 : : opcode == EEOP_OLD_FETCHSOME ||
3069 : : opcode == EEOP_NEW_FETCHSOME);
3070 : :
2784 3071 [ + + ]: 813550 : if (op->d.fetch.known_desc != NULL)
3072 : : {
3073 : 35468 : desc = op->d.fetch.known_desc;
3074 : 35468 : tts_ops = op->d.fetch.kind;
3075 : 35468 : isfixed = op->d.fetch.kind != NULL;
3076 : : }
3077 [ + + ]: 778082 : else if (!parent)
3078 : : {
3079 : 10577 : isfixed = false;
3080 : : }
2465 3081 [ + + ]: 767505 : else if (opcode == EEOP_INNER_FETCHSOME)
3082 : : {
2784 3083 : 130495 : PlanState *is = innerPlanState(parent);
3084 : :
3085 [ - + - - ]: 130495 : if (parent->inneropsset && !parent->inneropsfixed)
3086 : : {
2784 andres@anarazel.de 3087 :UBC 0 : isfixed = false;
3088 : : }
2784 andres@anarazel.de 3089 [ - + - - ]:CBC 130495 : else if (parent->inneropsset && parent->innerops)
3090 : : {
2784 andres@anarazel.de 3091 :UBC 0 : isfixed = true;
3092 : 0 : tts_ops = parent->innerops;
2466 3093 : 0 : desc = ExecGetResultType(is);
3094 : : }
2784 andres@anarazel.de 3095 [ + + ]:CBC 130495 : else if (is)
3096 : : {
3097 : 128693 : tts_ops = ExecGetResultSlotOps(is, &isfixed);
3098 : 128693 : desc = ExecGetResultType(is);
3099 : : }
3100 : : }
2465 3101 [ + + ]: 637010 : else if (opcode == EEOP_OUTER_FETCHSOME)
3102 : : {
2784 3103 : 245349 : PlanState *os = outerPlanState(parent);
3104 : :
3105 [ + + + + ]: 245349 : if (parent->outeropsset && !parent->outeropsfixed)
3106 : : {
3107 : 826 : isfixed = false;
3108 : : }
3109 [ + + + - ]: 244523 : else if (parent->outeropsset && parent->outerops)
3110 : : {
3111 : 30239 : isfixed = true;
3112 : 30239 : tts_ops = parent->outerops;
2466 3113 : 30239 : desc = ExecGetResultType(os);
3114 : : }
2784 3115 [ + + ]: 214284 : else if (os)
3116 : : {
3117 : 214276 : tts_ops = ExecGetResultSlotOps(os, &isfixed);
3118 : 214276 : desc = ExecGetResultType(os);
3119 : : }
3120 : : }
530 dean.a.rasheed@gmail 3121 [ + + + + ]: 391661 : else if (opcode == EEOP_SCAN_FETCHSOME ||
3122 [ + - ]: 269 : opcode == EEOP_OLD_FETCHSOME ||
3123 : : opcode == EEOP_NEW_FETCHSOME)
3124 : : {
2784 andres@anarazel.de 3125 : 391661 : desc = parent->scandesc;
3126 : :
2387 bruce@momjian.us 3127 [ + + ]: 391661 : if (parent->scanops)
2784 andres@anarazel.de 3128 : 375946 : tts_ops = parent->scanops;
3129 : :
3130 [ + + ]: 391661 : if (parent->scanopsset)
3131 : 375946 : isfixed = parent->scanopsfixed;
3132 : : }
3133 : :
3134 [ + + + - : 813550 : if (isfixed && desc != NULL && tts_ops != NULL)
+ - ]
3135 : : {
3136 : 766969 : op->d.fetch.fixed = true;
3137 : 766969 : op->d.fetch.kind = tts_ops;
3138 : 766969 : op->d.fetch.known_desc = desc;
3139 : : }
3140 : : else
3141 : : {
3142 : 46581 : op->d.fetch.fixed = false;
3143 : 46581 : op->d.fetch.kind = NULL;
3144 : 46581 : op->d.fetch.known_desc = NULL;
3145 : : }
3146 : :
3147 : : /* if the slot is known to always virtual we never need to deform */
2465 3148 [ + + + + ]: 813550 : if (op->d.fetch.fixed && op->d.fetch.kind == &TTSOpsVirtual)
3149 : 149232 : return false;
3150 : :
3151 : 664318 : return true;
3152 : : }
3153 : :
3154 : : /*
3155 : : * Prepare step for the evaluation of a whole-row variable.
3156 : : * The caller still has to push the step.
3157 : : */
3158 : : static void
3113 tgl@sss.pgh.pa.us 3159 : 3431 : ExecInitWholeRowVar(ExprEvalStep *scratch, Var *variable, ExprState *state)
3160 : : {
3161 : 3431 : PlanState *parent = state->parent;
3162 : :
3163 : : /* fill in all but the target */
3395 andres@anarazel.de 3164 : 3431 : scratch->opcode = EEOP_WHOLEROW;
3165 : 3431 : scratch->d.wholerow.var = variable;
3166 : 3431 : scratch->d.wholerow.first = true;
3167 : 3431 : scratch->d.wholerow.slow = false;
3168 : 3431 : scratch->d.wholerow.tupdesc = NULL; /* filled at runtime */
3169 : 3431 : scratch->d.wholerow.junkFilter = NULL;
3170 : :
3171 : : /* update ExprState flags if Var refers to OLD/NEW */
530 dean.a.rasheed@gmail 3172 [ + + ]: 3431 : if (variable->varreturningtype == VAR_RETURNING_OLD)
3173 : 93 : state->flags |= EEO_FLAG_HAS_OLD;
3174 [ + + ]: 3338 : else if (variable->varreturningtype == VAR_RETURNING_NEW)
3175 : 93 : state->flags |= EEO_FLAG_HAS_NEW;
3176 : :
3177 : : /*
3178 : : * If the input tuple came from a subquery, it might contain "resjunk"
3179 : : * columns (such as GROUP BY or ORDER BY columns), which we don't want to
3180 : : * keep in the whole-row result. We can get rid of such columns by
3181 : : * passing the tuple through a JunkFilter --- but to make one, we have to
3182 : : * lay our hands on the subquery's targetlist. Fortunately, there are not
3183 : : * very many cases where this can happen, and we can identify all of them
3184 : : * by examining our parent PlanState. We assume this is not an issue in
3185 : : * standalone expressions that don't have parent plans. (Whole-row Vars
3186 : : * can occur in such expressions, but they will always be referencing
3187 : : * table rows.)
3188 : : */
3395 andres@anarazel.de 3189 [ + + ]: 3431 : if (parent)
3190 : : {
3191 : 3390 : PlanState *subplan = NULL;
3192 : :
3193 [ + + + ]: 3390 : switch (nodeTag(parent))
3194 : : {
3195 : 217 : case T_SubqueryScanState:
3196 : 217 : subplan = ((SubqueryScanState *) parent)->subplan;
3197 : 217 : break;
3198 : 109 : case T_CteScanState:
3199 : 109 : subplan = ((CteScanState *) parent)->cteplanstate;
3200 : 109 : break;
3201 : 3064 : default:
3202 : 3064 : break;
3203 : : }
3204 : :
3205 [ + + ]: 3390 : if (subplan)
3206 : : {
3207 : 326 : bool junk_filter_needed = false;
3208 : : ListCell *tlist;
3209 : :
3210 : : /* Detect whether subplan tlist actually has any junk columns */
3211 [ + - + + : 1016 : foreach(tlist, subplan->plan->targetlist)
+ + ]
3212 : : {
3213 : 698 : TargetEntry *tle = (TargetEntry *) lfirst(tlist);
3214 : :
3215 [ + + ]: 698 : if (tle->resjunk)
3216 : : {
3217 : 8 : junk_filter_needed = true;
3218 : 8 : break;
3219 : : }
3220 : : }
3221 : :
3222 : : /* If so, build the junkfilter now */
3223 [ + + ]: 326 : if (junk_filter_needed)
3224 : : {
3225 : 8 : scratch->d.wholerow.junkFilter =
3226 : 8 : ExecInitJunkFilter(subplan->plan->targetlist,
3227 : : ExecInitExtraTupleSlot(parent->state, NULL,
3228 : : &TTSOpsVirtual));
3229 : : }
3230 : : }
3231 : : }
3232 : 3431 : }
3233 : :
3234 : : /*
3235 : : * Prepare evaluation of a SubscriptingRef expression.
3236 : : */
3237 : : static void
2706 alvherre@alvh.no-ip. 3238 : 20422 : ExecInitSubscriptingRef(ExprEvalStep *scratch, SubscriptingRef *sbsref,
3239 : : ExprState *state, Datum *resv, bool *resnull)
3240 : : {
3241 : 20422 : bool isAssignment = (sbsref->refassgnexpr != NULL);
2029 tgl@sss.pgh.pa.us 3242 : 20422 : int nupper = list_length(sbsref->refupperindexpr);
3243 : 20422 : int nlower = list_length(sbsref->reflowerindexpr);
3244 : : const SubscriptRoutines *sbsroutines;
3245 : : SubscriptingRefState *sbsrefstate;
3246 : : SubscriptExecSteps methods;
3247 : : char *ptr;
3395 andres@anarazel.de 3248 : 20422 : List *adjust_jumps = NIL;
3249 : : ListCell *lc;
3250 : : int i;
3251 : :
3252 : : /* Look up the subscripting support methods */
2029 tgl@sss.pgh.pa.us 3253 : 20422 : sbsroutines = getSubscriptingRoutines(sbsref->refcontainertype, NULL);
2027 3254 [ - + ]: 20422 : if (!sbsroutines)
2027 tgl@sss.pgh.pa.us 3255 [ # # # # ]:UBC 0 : ereport(ERROR,
3256 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
3257 : : errmsg("cannot subscript type %s because it does not support subscripting",
3258 : : format_type_be(sbsref->refcontainertype)),
3259 : : state->parent ?
3260 : : executor_errposition(state->parent->state,
3261 : : exprLocation((Node *) sbsref)) : 0));
3262 : :
3263 : : /* Allocate sbsrefstate, with enough space for per-subscript arrays too */
2029 tgl@sss.pgh.pa.us 3264 :CBC 20422 : sbsrefstate = palloc0(MAXALIGN(sizeof(SubscriptingRefState)) +
3265 : 20422 : (nupper + nlower) * (sizeof(Datum) +
3266 : : 2 * sizeof(bool)));
3267 : :
3268 : : /* Fill constant fields of SubscriptingRefState */
2706 alvherre@alvh.no-ip. 3269 : 20422 : sbsrefstate->isassignment = isAssignment;
2029 tgl@sss.pgh.pa.us 3270 : 20422 : sbsrefstate->numupper = nupper;
3271 : 20422 : sbsrefstate->numlower = nlower;
3272 : : /* Set up per-subscript arrays */
3273 : 20422 : ptr = ((char *) sbsrefstate) + MAXALIGN(sizeof(SubscriptingRefState));
3274 : 20422 : sbsrefstate->upperindex = (Datum *) ptr;
3275 : 20422 : ptr += nupper * sizeof(Datum);
3276 : 20422 : sbsrefstate->lowerindex = (Datum *) ptr;
3277 : 20422 : ptr += nlower * sizeof(Datum);
3278 : 20422 : sbsrefstate->upperprovided = (bool *) ptr;
3279 : 20422 : ptr += nupper * sizeof(bool);
3280 : 20422 : sbsrefstate->lowerprovided = (bool *) ptr;
3281 : 20422 : ptr += nlower * sizeof(bool);
3282 : 20422 : sbsrefstate->upperindexnull = (bool *) ptr;
3283 : 20422 : ptr += nupper * sizeof(bool);
3284 : 20422 : sbsrefstate->lowerindexnull = (bool *) ptr;
3285 : : /* ptr += nlower * sizeof(bool); */
3286 : :
3287 : : /*
3288 : : * Let the container-type-specific code have a chance. It must fill the
3289 : : * "methods" struct with function pointers for us to possibly use in
3290 : : * execution steps below; and it can optionally set up some data pointed
3291 : : * to by the workspace field.
3292 : : */
3293 : 20422 : memset(&methods, 0, sizeof(methods));
3294 : 20422 : sbsroutines->exec_setup(sbsref, sbsrefstate, &methods);
3295 : :
3296 : : /*
3297 : : * Evaluate array input. It's safe to do so into resv/resnull, because we
3298 : : * won't use that as target for any of the other subexpressions, and it'll
3299 : : * be overwritten by the final EEOP_SBSREF_FETCH/ASSIGN step, which is
3300 : : * pushed last.
3301 : : */
2706 alvherre@alvh.no-ip. 3302 : 20422 : ExecInitExprRec(sbsref->refexpr, state, resv, resnull);
3303 : :
3304 : : /*
3305 : : * If refexpr yields NULL, and the operation should be strict, then result
3306 : : * is NULL. We can implement this with just JUMP_IF_NULL, since we
3307 : : * evaluated the array into the desired target location.
3308 : : */
2029 tgl@sss.pgh.pa.us 3309 [ + + + - ]: 20422 : if (!isAssignment && sbsroutines->fetch_strict)
3310 : : {
3395 andres@anarazel.de 3311 : 19524 : scratch->opcode = EEOP_JUMP_IF_NULL;
3312 : 19524 : scratch->d.jump.jumpdone = -1; /* adjust later */
3313 : 19524 : ExprEvalPushStep(state, scratch);
3314 : 19524 : adjust_jumps = lappend_int(adjust_jumps,
3315 : 19524 : state->steps_len - 1);
3316 : : }
3317 : :
3318 : : /* Evaluate upper subscripts */
3319 : 20422 : i = 0;
2706 alvherre@alvh.no-ip. 3320 [ + - + + : 41268 : foreach(lc, sbsref->refupperindexpr)
+ + ]
3321 : : {
3395 andres@anarazel.de 3322 : 20846 : Expr *e = (Expr *) lfirst(lc);
3323 : :
3324 : : /* When slicing, individual subscript bounds can be omitted */
3325 [ + + ]: 20846 : if (!e)
3326 : : {
2706 alvherre@alvh.no-ip. 3327 : 52 : sbsrefstate->upperprovided[i] = false;
2029 tgl@sss.pgh.pa.us 3328 : 52 : sbsrefstate->upperindexnull[i] = true;
3329 : : }
3330 : : else
3331 : : {
3332 : 20794 : sbsrefstate->upperprovided[i] = true;
3333 : : /* Each subscript is evaluated into appropriate array entry */
3334 : 20794 : ExecInitExprRec(e, state,
3335 : 20794 : &sbsrefstate->upperindex[i],
3336 : 20794 : &sbsrefstate->upperindexnull[i]);
3337 : : }
3395 andres@anarazel.de 3338 : 20846 : i++;
3339 : : }
3340 : :
3341 : : /* Evaluate lower subscripts similarly */
3342 : 20422 : i = 0;
2706 alvherre@alvh.no-ip. 3343 [ + + + + : 20847 : foreach(lc, sbsref->reflowerindexpr)
+ + ]
3344 : : {
3395 andres@anarazel.de 3345 : 425 : Expr *e = (Expr *) lfirst(lc);
3346 : :
3347 : : /* When slicing, individual subscript bounds can be omitted */
3348 [ + + ]: 425 : if (!e)
3349 : : {
2706 alvherre@alvh.no-ip. 3350 : 52 : sbsrefstate->lowerprovided[i] = false;
2029 tgl@sss.pgh.pa.us 3351 : 52 : sbsrefstate->lowerindexnull[i] = true;
3352 : : }
3353 : : else
3354 : : {
3355 : 373 : sbsrefstate->lowerprovided[i] = true;
3356 : : /* Each subscript is evaluated into appropriate array entry */
3357 : 373 : ExecInitExprRec(e, state,
3358 : 373 : &sbsrefstate->lowerindex[i],
3359 : 373 : &sbsrefstate->lowerindexnull[i]);
3360 : : }
3361 : 425 : i++;
3362 : : }
3363 : :
3364 : : /* SBSREF_SUBSCRIPTS checks and converts all the subscripts at once */
3365 [ + + ]: 20422 : if (methods.sbs_check_subscripts)
3366 : : {
3367 : 20415 : scratch->opcode = EEOP_SBSREF_SUBSCRIPTS;
3368 : 20415 : scratch->d.sbsref_subscript.subscriptfunc = methods.sbs_check_subscripts;
2706 alvherre@alvh.no-ip. 3369 : 20415 : scratch->d.sbsref_subscript.state = sbsrefstate;
3370 : 20415 : scratch->d.sbsref_subscript.jumpdone = -1; /* adjust later */
3395 andres@anarazel.de 3371 : 20415 : ExprEvalPushStep(state, scratch);
3372 : 20415 : adjust_jumps = lappend_int(adjust_jumps,
3373 : 20415 : state->steps_len - 1);
3374 : : }
3375 : :
3376 [ + + ]: 20422 : if (isAssignment)
3377 : : {
3378 : : Datum *save_innermost_caseval;
3379 : : bool *save_innermost_casenull;
3380 : :
3381 : : /* Check for unimplemented methods */
2029 tgl@sss.pgh.pa.us 3382 [ - + ]: 898 : if (!methods.sbs_assign)
2029 tgl@sss.pgh.pa.us 3383 [ # # ]:UBC 0 : ereport(ERROR,
3384 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3385 : : errmsg("type %s does not support subscripted assignment",
3386 : : format_type_be(sbsref->refcontainertype))));
3387 : :
3388 : : /*
3389 : : * We might have a nested-assignment situation, in which the
3390 : : * refassgnexpr is itself a FieldStore or SubscriptingRef that needs
3391 : : * to obtain and modify the previous value of the array element or
3392 : : * slice being replaced. If so, we have to extract that value from
3393 : : * the array and pass it down via the CaseTestExpr mechanism. It's
3394 : : * safe to reuse the CASE mechanism because there cannot be a CASE
3395 : : * between here and where the value would be needed, and an array
3396 : : * assignment can't be within a CASE either. (So saving and restoring
3397 : : * innermost_caseval is just paranoia, but let's do it anyway.)
3398 : : *
3399 : : * Since fetching the old element might be a nontrivial expense, do it
3400 : : * only if the argument actually needs it.
3401 : : */
2706 alvherre@alvh.no-ip. 3402 [ + + ]:CBC 898 : if (isAssignmentIndirectionExpr(sbsref->refassgnexpr))
3403 : : {
2029 tgl@sss.pgh.pa.us 3404 [ - + ]: 123 : if (!methods.sbs_fetch_old)
2029 tgl@sss.pgh.pa.us 3405 [ # # ]:UBC 0 : ereport(ERROR,
3406 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3407 : : errmsg("type %s does not support subscripted assignment",
3408 : : format_type_be(sbsref->refcontainertype))));
2706 alvherre@alvh.no-ip. 3409 :CBC 123 : scratch->opcode = EEOP_SBSREF_OLD;
2029 tgl@sss.pgh.pa.us 3410 : 123 : scratch->d.sbsref.subscriptfunc = methods.sbs_fetch_old;
2706 alvherre@alvh.no-ip. 3411 : 123 : scratch->d.sbsref.state = sbsrefstate;
3395 andres@anarazel.de 3412 : 123 : ExprEvalPushStep(state, scratch);
3413 : : }
3414 : :
3415 : : /* SBSREF_OLD puts extracted value into prevvalue/prevnull */
3416 : 898 : save_innermost_caseval = state->innermost_caseval;
3417 : 898 : save_innermost_casenull = state->innermost_casenull;
2706 alvherre@alvh.no-ip. 3418 : 898 : state->innermost_caseval = &sbsrefstate->prevvalue;
3419 : 898 : state->innermost_casenull = &sbsrefstate->prevnull;
3420 : :
3421 : : /* evaluate replacement value into replacevalue/replacenull */
3422 : 898 : ExecInitExprRec(sbsref->refassgnexpr, state,
3423 : : &sbsrefstate->replacevalue, &sbsrefstate->replacenull);
3424 : :
3395 andres@anarazel.de 3425 : 898 : state->innermost_caseval = save_innermost_caseval;
3426 : 898 : state->innermost_casenull = save_innermost_casenull;
3427 : :
3428 : : /* and perform the assignment */
2706 alvherre@alvh.no-ip. 3429 : 898 : scratch->opcode = EEOP_SBSREF_ASSIGN;
2029 tgl@sss.pgh.pa.us 3430 : 898 : scratch->d.sbsref.subscriptfunc = methods.sbs_assign;
2706 alvherre@alvh.no-ip. 3431 : 898 : scratch->d.sbsref.state = sbsrefstate;
3395 andres@anarazel.de 3432 : 898 : ExprEvalPushStep(state, scratch);
3433 : : }
3434 : : else
3435 : : {
3436 : : /* array fetch is much simpler */
2706 alvherre@alvh.no-ip. 3437 : 19524 : scratch->opcode = EEOP_SBSREF_FETCH;
2029 tgl@sss.pgh.pa.us 3438 : 19524 : scratch->d.sbsref.subscriptfunc = methods.sbs_fetch;
2706 alvherre@alvh.no-ip. 3439 : 19524 : scratch->d.sbsref.state = sbsrefstate;
3395 andres@anarazel.de 3440 : 19524 : ExprEvalPushStep(state, scratch);
3441 : : }
3442 : :
3443 : : /* adjust jump targets */
3444 [ + + + + : 60361 : foreach(lc, adjust_jumps)
+ + ]
3445 : : {
3446 : 39939 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
3447 : :
2029 tgl@sss.pgh.pa.us 3448 [ + + ]: 39939 : if (as->opcode == EEOP_SBSREF_SUBSCRIPTS)
3449 : : {
2706 alvherre@alvh.no-ip. 3450 [ - + ]: 20415 : Assert(as->d.sbsref_subscript.jumpdone == -1);
3451 : 20415 : as->d.sbsref_subscript.jumpdone = state->steps_len;
3452 : : }
3453 : : else
3454 : : {
3395 andres@anarazel.de 3455 [ - + ]: 19524 : Assert(as->opcode == EEOP_JUMP_IF_NULL);
3456 [ - + ]: 19524 : Assert(as->d.jump.jumpdone == -1);
3457 : 19524 : as->d.jump.jumpdone = state->steps_len;
3458 : : }
3459 : : }
3460 : 20422 : }
3461 : :
3462 : : /*
3463 : : * Helper for preparing SubscriptingRef expressions for evaluation: is expr
3464 : : * a nested FieldStore or SubscriptingRef that needs the old element value
3465 : : * passed down?
3466 : : *
3467 : : * (We could use this in FieldStore too, but in that case passing the old
3468 : : * value is so cheap there's no need.)
3469 : : *
3470 : : * Note: it might seem that this needs to recurse, but in most cases it does
3471 : : * not; the CaseTestExpr, if any, will be directly the arg or refexpr of the
3472 : : * top-level node. Nested-assignment situations give rise to expression
3473 : : * trees in which each level of assignment has its own CaseTestExpr, and the
3474 : : * recursive structure appears within the newvals or refassgnexpr field.
3475 : : * There is an exception, though: if the array is an array-of-domain, we will
3476 : : * have a CoerceToDomain or RelabelType as the refassgnexpr, and we need to
3477 : : * be able to look through that.
3478 : : */
3479 : : static bool
3480 : 954 : isAssignmentIndirectionExpr(Expr *expr)
3481 : : {
3482 [ - + ]: 954 : if (expr == NULL)
3395 andres@anarazel.de 3483 :UBC 0 : return false; /* just paranoia */
3395 andres@anarazel.de 3484 [ + + ]:CBC 954 : if (IsA(expr, FieldStore))
3485 : : {
3486 : 123 : FieldStore *fstore = (FieldStore *) expr;
3487 : :
3488 [ + - + - ]: 123 : if (fstore->arg && IsA(fstore->arg, CaseTestExpr))
3489 : 123 : return true;
3490 : : }
2706 alvherre@alvh.no-ip. 3491 [ + + ]: 831 : else if (IsA(expr, SubscriptingRef))
3492 : : {
3493 : 21 : SubscriptingRef *sbsRef = (SubscriptingRef *) expr;
3494 : :
3495 [ + - - + ]: 21 : if (sbsRef->refexpr && IsA(sbsRef->refexpr, CaseTestExpr))
3395 andres@anarazel.de 3496 :UBC 0 : return true;
3497 : : }
1715 tgl@sss.pgh.pa.us 3498 [ + + ]:CBC 810 : else if (IsA(expr, CoerceToDomain))
3499 : : {
3500 : 44 : CoerceToDomain *cd = (CoerceToDomain *) expr;
3501 : :
3502 : 44 : return isAssignmentIndirectionExpr(cd->arg);
3503 : : }
1172 3504 [ + + ]: 766 : else if (IsA(expr, RelabelType))
3505 : : {
3506 : 12 : RelabelType *r = (RelabelType *) expr;
3507 : :
3508 : 12 : return isAssignmentIndirectionExpr(r->arg);
3509 : : }
3395 andres@anarazel.de 3510 : 775 : return false;
3511 : : }
3512 : :
3513 : : /*
3514 : : * Prepare evaluation of a CoerceToDomain expression.
3515 : : */
3516 : : static void
3517 : 5896 : ExecInitCoerceToDomain(ExprEvalStep *scratch, CoerceToDomain *ctest,
3518 : : ExprState *state, Datum *resv, bool *resnull)
3519 : : {
3520 : : DomainConstraintRef *constraint_ref;
1701 tgl@sss.pgh.pa.us 3521 : 5896 : Datum *domainval = NULL;
3522 : 5896 : bool *domainnull = NULL;
3523 : : ListCell *l;
3524 : :
3395 andres@anarazel.de 3525 : 5896 : scratch->d.domaincheck.resulttype = ctest->resulttype;
3526 : : /* we'll allocate workspace only if needed */
3527 : 5896 : scratch->d.domaincheck.checkvalue = NULL;
3528 : 5896 : scratch->d.domaincheck.checknull = NULL;
888 amitlan@postgresql.o 3529 : 5896 : scratch->d.domaincheck.escontext = state->escontext;
3530 : :
3531 : : /*
3532 : : * Evaluate argument - it's fine to directly store it into resv/resnull,
3533 : : * if there's constraint failures there'll be errors, otherwise it's what
3534 : : * needs to be returned.
3535 : : */
3113 tgl@sss.pgh.pa.us 3536 : 5896 : ExecInitExprRec(ctest->arg, state, resv, resnull);
3537 : :
3538 : : /*
3539 : : * Note: if the argument is of varlena type, it could be a R/W expanded
3540 : : * object. We want to return the R/W pointer as the final result, but we
3541 : : * have to pass a R/O pointer as the value to be tested by any functions
3542 : : * in check expressions. We don't bother to emit a MAKE_READONLY step
3543 : : * unless there's actually at least one check expression, though. Until
3544 : : * we've tested that, domainval/domainnull are NULL.
3545 : : */
3546 : :
3547 : : /*
3548 : : * Collect the constraints associated with the domain.
3549 : : *
3550 : : * Note: before PG v10 we'd recheck the set of constraints during each
3551 : : * evaluation of the expression. Now we bake them into the ExprState
3552 : : * during executor initialization. That means we don't need typcache.c to
3553 : : * provide compiled exprs.
3554 : : */
202 michael@paquier.xyz 3555 :GNC 5896 : constraint_ref = palloc_object(DomainConstraintRef);
3395 andres@anarazel.de 3556 :CBC 5896 : InitDomainConstraintRef(ctest->resulttype,
3557 : : constraint_ref,
3558 : : CurrentMemoryContext,
3559 : : false);
3560 : :
3561 : : /*
3562 : : * Compile code to check each domain constraint. NOTNULL constraints can
3563 : : * just be applied on the resv/resnull value, but for CHECK constraints we
3564 : : * need more pushups.
3565 : : */
3566 [ + + + + : 12330 : foreach(l, constraint_ref->constraints)
+ + ]
3567 : : {
3568 : 6434 : DomainConstraintState *con = (DomainConstraintState *) lfirst(l);
3569 : : Datum *save_innermost_domainval;
3570 : : bool *save_innermost_domainnull;
3571 : :
3572 : 6434 : scratch->d.domaincheck.constraintname = con->name;
3573 : :
3574 [ + + - ]: 6434 : switch (con->constrainttype)
3575 : : {
3576 : 256 : case DOM_CONSTRAINT_NOTNULL:
3577 : 256 : scratch->opcode = EEOP_DOMAIN_NOTNULL;
3578 : 256 : ExprEvalPushStep(state, scratch);
3579 : 256 : break;
3580 : 6178 : case DOM_CONSTRAINT_CHECK:
3581 : : /* Allocate workspace for CHECK output if we didn't yet */
3582 [ + + ]: 6178 : if (scratch->d.domaincheck.checkvalue == NULL)
3583 : : {
3584 : 5732 : scratch->d.domaincheck.checkvalue =
202 michael@paquier.xyz 3585 :GNC 5732 : palloc_object(Datum);
3395 andres@anarazel.de 3586 :CBC 5732 : scratch->d.domaincheck.checknull =
202 michael@paquier.xyz 3587 :GNC 5732 : palloc_object(bool);
3588 : : }
3589 : :
3590 : : /*
3591 : : * If first time through, determine where CoerceToDomainValue
3592 : : * nodes should read from.
3593 : : */
3395 andres@anarazel.de 3594 [ + + ]:CBC 6178 : if (domainval == NULL)
3595 : : {
3596 : : /*
3597 : : * Since value might be read multiple times, force to R/O
3598 : : * - but only if it could be an expanded datum.
3599 : : */
3600 [ + + ]: 5732 : if (get_typlen(ctest->resulttype) == -1)
3601 : : {
2336 3602 : 2059 : ExprEvalStep scratch2 = {0};
3603 : :
3604 : : /* Yes, so make output workspace for MAKE_READONLY */
202 michael@paquier.xyz 3605 :GNC 2059 : domainval = palloc_object(Datum);
3606 : 2059 : domainnull = palloc_object(bool);
3607 : :
3608 : : /* Emit MAKE_READONLY */
3395 andres@anarazel.de 3609 :CBC 2059 : scratch2.opcode = EEOP_MAKE_READONLY;
3610 : 2059 : scratch2.resvalue = domainval;
3611 : 2059 : scratch2.resnull = domainnull;
3612 : 2059 : scratch2.d.make_readonly.value = resv;
3613 : 2059 : scratch2.d.make_readonly.isnull = resnull;
3614 : 2059 : ExprEvalPushStep(state, &scratch2);
3615 : : }
3616 : : else
3617 : : {
3618 : : /* No, so it's fine to read from resv/resnull */
3619 : 3673 : domainval = resv;
3620 : 3673 : domainnull = resnull;
3621 : : }
3622 : : }
3623 : :
3624 : : /*
3625 : : * Set up value to be returned by CoerceToDomainValue nodes.
3626 : : * We must save and restore innermost_domainval/null fields,
3627 : : * in case this node is itself within a check expression for
3628 : : * another domain.
3629 : : */
3630 : 6178 : save_innermost_domainval = state->innermost_domainval;
3631 : 6178 : save_innermost_domainnull = state->innermost_domainnull;
3632 : 6178 : state->innermost_domainval = domainval;
3633 : 6178 : state->innermost_domainnull = domainnull;
3634 : :
3635 : : /* evaluate check expression value */
3113 tgl@sss.pgh.pa.us 3636 : 6178 : ExecInitExprRec(con->check_expr, state,
3637 : : scratch->d.domaincheck.checkvalue,
3638 : : scratch->d.domaincheck.checknull);
3639 : :
3395 andres@anarazel.de 3640 : 6178 : state->innermost_domainval = save_innermost_domainval;
3641 : 6178 : state->innermost_domainnull = save_innermost_domainnull;
3642 : :
3643 : : /* now test result */
3644 : 6178 : scratch->opcode = EEOP_DOMAIN_CHECK;
3645 : 6178 : ExprEvalPushStep(state, scratch);
3646 : :
3647 : 6178 : break;
3395 andres@anarazel.de 3648 :UBC 0 : default:
3649 [ # # ]: 0 : elog(ERROR, "unrecognized constraint type: %d",
3650 : : (int) con->constrainttype);
3651 : : break;
3652 : : }
3653 : : }
3395 andres@anarazel.de 3654 :CBC 5896 : }
3655 : :
3656 : : /*
3657 : : * Build transition/combine function invocations for all aggregate transition
3658 : : * / combination function invocations in a grouping sets phase. This has to
3659 : : * invoke all sort based transitions in a phase (if doSort is true), all hash
3660 : : * based transitions (if doHash is true), or both (both true).
3661 : : *
3662 : : * The resulting expression will, for each set of transition values, first
3663 : : * check for filters, evaluate aggregate input, check that that input is not
3664 : : * NULL for a strict transition function, and then finally invoke the
3665 : : * transition for each of the concurrently computed grouping sets.
3666 : : *
3667 : : * If nullcheck is true, the generated code will check for a NULL pointer to
3668 : : * the array of AggStatePerGroup, and skip evaluation if so.
3669 : : */
3670 : : ExprState *
3094 3671 : 34743 : ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
3672 : : bool doSort, bool doHash, bool nullcheck)
3673 : : {
3674 : 34743 : ExprState *state = makeNode(ExprState);
3675 : 34743 : PlanState *parent = &aggstate->ss.ps;
3080 3676 : 34743 : ExprEvalStep scratch = {0};
3094 3677 : 34743 : bool isCombine = DO_AGGSPLIT_COMBINE(aggstate->aggsplit);
530 dean.a.rasheed@gmail 3678 : 34743 : ExprSetupInfo deform = {0, 0, 0, 0, 0, NIL};
3679 : :
3094 andres@anarazel.de 3680 : 34743 : state->expr = (Expr *) aggstate;
3681 : 34743 : state->parent = parent;
3682 : :
3683 : 34743 : scratch.resvalue = &state->resvalue;
3684 : 34743 : scratch.resnull = &state->resnull;
3685 : :
3686 : : /*
3687 : : * First figure out which slots, and how many columns from each, we're
3688 : : * going to need.
3689 : : */
2336 3690 [ + + ]: 73207 : for (int transno = 0; transno < aggstate->numtrans; transno++)
3691 : : {
3094 3692 : 38464 : AggStatePerTrans pertrans = &aggstate->pertrans[transno];
3693 : :
1221 tgl@sss.pgh.pa.us 3694 : 38464 : expr_setup_walker((Node *) pertrans->aggref->aggdirectargs,
3695 : : &deform);
3696 : 38464 : expr_setup_walker((Node *) pertrans->aggref->args,
3697 : : &deform);
3698 : 38464 : expr_setup_walker((Node *) pertrans->aggref->aggorder,
3699 : : &deform);
3700 : 38464 : expr_setup_walker((Node *) pertrans->aggref->aggdistinct,
3701 : : &deform);
3702 : 38464 : expr_setup_walker((Node *) pertrans->aggref->aggfilter,
3703 : : &deform);
3704 : : }
3705 : 34743 : ExecPushExprSetupSteps(state, &deform);
3706 : :
3707 : : /*
3708 : : * Emit instructions for each transition value / grouping set combination.
3709 : : */
2336 andres@anarazel.de 3710 [ + + ]: 73207 : for (int transno = 0; transno < aggstate->numtrans; transno++)
3711 : : {
3094 3712 : 38464 : AggStatePerTrans pertrans = &aggstate->pertrans[transno];
2712 3713 : 38464 : FunctionCallInfo trans_fcinfo = pertrans->transfn_fcinfo;
3094 3714 : 38464 : List *adjust_bailout = NIL;
2712 3715 : 38464 : NullableDatum *strictargs = NULL;
3094 3716 : 38464 : bool *strictnulls = NULL;
3717 : : int argno;
3718 : : ListCell *bail;
3719 : :
3720 : : /*
3721 : : * If filter present, emit. Do so before evaluating the input, to
3722 : : * avoid potentially unneeded computations, or even worse, unintended
3723 : : * side-effects. When combining, all the necessary filtering has
3724 : : * already been done.
3725 : : */
3726 [ + + + - ]: 38464 : if (pertrans->aggref->aggfilter && !isCombine)
3727 : : {
3728 : : /* evaluate filter expression */
3729 : 428 : ExecInitExprRec(pertrans->aggref->aggfilter, state,
3730 : : &state->resvalue, &state->resnull);
3731 : : /* and jump out if false */
3732 : 428 : scratch.opcode = EEOP_JUMP_IF_NOT_TRUE;
3733 : 428 : scratch.d.jump.jumpdone = -1; /* adjust later */
3734 : 428 : ExprEvalPushStep(state, &scratch);
3735 : 428 : adjust_bailout = lappend_int(adjust_bailout,
3736 : 428 : state->steps_len - 1);
3737 : : }
3738 : :
3739 : : /*
3740 : : * Evaluate arguments to aggregate/combine function.
3741 : : */
3742 : 38464 : argno = 0;
3743 [ + + ]: 38464 : if (isCombine)
3744 : : {
3745 : : /*
3746 : : * Combining two aggregate transition values. Instead of directly
3747 : : * coming from a tuple the input is a, potentially deserialized,
3748 : : * transition value.
3749 : : */
3750 : : TargetEntry *source_tle;
3751 : :
3752 [ - + ]: 1494 : Assert(pertrans->numSortCols == 0);
3753 [ - + ]: 1494 : Assert(list_length(pertrans->aggref->args) == 1);
3754 : :
2712 3755 : 1494 : strictargs = trans_fcinfo->args + 1;
3094 3756 : 1494 : source_tle = (TargetEntry *) linitial(pertrans->aggref->args);
3757 : :
3758 : : /*
3759 : : * deserialfn_oid will be set if we must deserialize the input
3760 : : * state before calling the combine function.
3761 : : */
3762 [ + + ]: 1494 : if (!OidIsValid(pertrans->deserialfn_oid))
3763 : : {
3764 : : /*
3765 : : * Start from 1, since the 0th arg will be the transition
3766 : : * value
3767 : : */
3768 : 1414 : ExecInitExprRec(source_tle->expr, state,
2712 3769 : 1414 : &trans_fcinfo->args[argno + 1].value,
3770 : 1414 : &trans_fcinfo->args[argno + 1].isnull);
3771 : : }
3772 : : else
3773 : : {
3774 : 80 : FunctionCallInfo ds_fcinfo = pertrans->deserialfn_fcinfo;
3775 : :
3776 : : /* evaluate argument */
3094 3777 : 80 : ExecInitExprRec(source_tle->expr, state,
3778 : : &ds_fcinfo->args[0].value,
3779 : : &ds_fcinfo->args[0].isnull);
3780 : :
3781 : : /* Dummy second argument for type-safety reasons */
2712 3782 : 80 : ds_fcinfo->args[1].value = PointerGetDatum(NULL);
3783 : 80 : ds_fcinfo->args[1].isnull = false;
3784 : :
3785 : : /*
3786 : : * Don't call a strict deserialization function with NULL
3787 : : * input
3788 : : */
3094 3789 [ + - ]: 80 : if (pertrans->deserialfn.fn_strict)
3790 : 80 : scratch.opcode = EEOP_AGG_STRICT_DESERIALIZE;
3791 : : else
3094 andres@anarazel.de 3792 :UBC 0 : scratch.opcode = EEOP_AGG_DESERIALIZE;
3793 : :
3094 andres@anarazel.de 3794 :CBC 80 : scratch.d.agg_deserialize.fcinfo_data = ds_fcinfo;
3795 : 80 : scratch.d.agg_deserialize.jumpnull = -1; /* adjust later */
2712 3796 : 80 : scratch.resvalue = &trans_fcinfo->args[argno + 1].value;
3797 : 80 : scratch.resnull = &trans_fcinfo->args[argno + 1].isnull;
3798 : :
3094 3799 : 80 : ExprEvalPushStep(state, &scratch);
3800 : : /* don't add an adjustment unless the function is strict */
1979 rhodiumtoad@postgres 3801 [ + - ]: 80 : if (pertrans->deserialfn.fn_strict)
3802 : 80 : adjust_bailout = lappend_int(adjust_bailout,
3803 : 80 : state->steps_len - 1);
3804 : :
3805 : : /* restore normal settings of scratch fields */
3094 andres@anarazel.de 3806 : 80 : scratch.resvalue = &state->resvalue;
3807 : 80 : scratch.resnull = &state->resnull;
3808 : : }
3809 : 1494 : argno++;
3810 : :
1428 drowley@postgresql.o 3811 [ - + ]: 1494 : Assert(pertrans->numInputs == argno);
3812 : : }
3813 [ + + ]: 36970 : else if (!pertrans->aggsortrequired)
3814 : : {
3815 : : ListCell *arg;
3816 : :
3817 : : /*
3818 : : * Normal transition function without ORDER BY / DISTINCT or with
3819 : : * ORDER BY / DISTINCT but the planner has given us pre-sorted
3820 : : * input.
3821 : : */
2712 andres@anarazel.de 3822 : 36774 : strictargs = trans_fcinfo->args + 1;
3823 : :
3094 3824 [ + + + + : 63642 : foreach(arg, pertrans->aggref->args)
+ + ]
3825 : : {
3826 : 27779 : TargetEntry *source_tle = (TargetEntry *) lfirst(arg);
3827 : :
3828 : : /*
3829 : : * Don't initialize args for any ORDER BY clause that might
3830 : : * exist in a presorted aggregate.
3831 : : */
1428 drowley@postgresql.o 3832 [ + + ]: 27779 : if (argno == pertrans->numTransInputs)
3833 : 911 : break;
3834 : :
3835 : : /*
3836 : : * Start from 1, since the 0th arg will be the transition
3837 : : * value
3838 : : */
3094 andres@anarazel.de 3839 : 26868 : ExecInitExprRec(source_tle->expr, state,
2712 3840 : 26868 : &trans_fcinfo->args[argno + 1].value,
3841 : 26868 : &trans_fcinfo->args[argno + 1].isnull);
3094 3842 : 26868 : argno++;
3843 : : }
1428 drowley@postgresql.o 3844 [ - + ]: 36774 : Assert(pertrans->numTransInputs == argno);
3845 : : }
3094 andres@anarazel.de 3846 [ + + ]: 196 : else if (pertrans->numInputs == 1)
3847 : : {
3848 : : /*
3849 : : * Non-presorted DISTINCT and/or ORDER BY case, with a single
3850 : : * column sorted on.
3851 : : */
3852 : 164 : TargetEntry *source_tle =
1138 tgl@sss.pgh.pa.us 3853 : 164 : (TargetEntry *) linitial(pertrans->aggref->args);
3854 : :
3094 andres@anarazel.de 3855 [ - + ]: 164 : Assert(list_length(pertrans->aggref->args) == 1);
3856 : :
3857 : 164 : ExecInitExprRec(source_tle->expr, state,
3858 : : &state->resvalue,
3859 : : &state->resnull);
3860 : 164 : strictnulls = &state->resnull;
3861 : 164 : argno++;
3862 : :
1428 drowley@postgresql.o 3863 [ - + ]: 164 : Assert(pertrans->numInputs == argno);
3864 : : }
3865 : : else
3866 : : {
3867 : : /*
3868 : : * Non-presorted DISTINCT and/or ORDER BY case, with multiple
3869 : : * columns sorted on.
3870 : : */
3094 andres@anarazel.de 3871 : 32 : Datum *values = pertrans->sortslot->tts_values;
3872 : 32 : bool *nulls = pertrans->sortslot->tts_isnull;
3873 : : ListCell *arg;
3874 : :
3875 : 32 : strictnulls = nulls;
3876 : :
3877 [ + - + + : 112 : foreach(arg, pertrans->aggref->args)
+ + ]
3878 : : {
3879 : 80 : TargetEntry *source_tle = (TargetEntry *) lfirst(arg);
3880 : :
3881 : 80 : ExecInitExprRec(source_tle->expr, state,
3882 : 80 : &values[argno], &nulls[argno]);
3883 : 80 : argno++;
3884 : : }
1428 drowley@postgresql.o 3885 [ - + ]: 32 : Assert(pertrans->numInputs == argno);
3886 : : }
3887 : :
3888 : : /*
3889 : : * For a strict transfn, nothing happens when there's a NULL input; we
3890 : : * just keep the prior transValue. This is true for both plain and
3891 : : * sorted/distinct aggregates.
3892 : : */
2796 andres@anarazel.de 3893 [ + + + + ]: 38464 : if (trans_fcinfo->flinfo->fn_strict && pertrans->numTransInputs > 0)
3894 : : {
2712 3895 [ + + ]: 7110 : if (strictnulls)
3896 : 108 : scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK_NULLS;
476 dgustafsson@postgres 3897 [ + - + + ]: 7002 : else if (strictargs && pertrans->numTransInputs == 1)
3898 : 6834 : scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1;
3899 : : else
2712 andres@anarazel.de 3900 : 168 : scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK_ARGS;
3094 3901 : 7110 : scratch.d.agg_strict_input_check.nulls = strictnulls;
2712 3902 : 7110 : scratch.d.agg_strict_input_check.args = strictargs;
3094 3903 : 7110 : scratch.d.agg_strict_input_check.jumpnull = -1; /* adjust later */
2796 3904 : 7110 : scratch.d.agg_strict_input_check.nargs = pertrans->numTransInputs;
3094 3905 : 7110 : ExprEvalPushStep(state, &scratch);
3906 : 7110 : adjust_bailout = lappend_int(adjust_bailout,
3907 : 7110 : state->steps_len - 1);
3908 : : }
3909 : :
3910 : : /* Handle DISTINCT aggregates which have pre-sorted input */
1428 drowley@postgresql.o 3911 [ + + + + ]: 38464 : if (pertrans->numDistinctCols > 0 && !pertrans->aggsortrequired)
3912 : : {
3913 [ + + ]: 294 : if (pertrans->numDistinctCols > 1)
3914 : 68 : scratch.opcode = EEOP_AGG_PRESORTED_DISTINCT_MULTI;
3915 : : else
3916 : 226 : scratch.opcode = EEOP_AGG_PRESORTED_DISTINCT_SINGLE;
3917 : :
3918 : 294 : scratch.d.agg_presorted_distinctcheck.pertrans = pertrans;
3919 : 294 : scratch.d.agg_presorted_distinctcheck.jumpdistinct = -1; /* adjust later */
3920 : 294 : ExprEvalPushStep(state, &scratch);
3921 : 294 : adjust_bailout = lappend_int(adjust_bailout,
3922 : 294 : state->steps_len - 1);
3923 : : }
3924 : :
3925 : : /*
3926 : : * Call transition function (once for each concurrently evaluated
3927 : : * grouping set). Do so for both sort and hash based computations, as
3928 : : * applicable.
3929 : : */
3094 andres@anarazel.de 3930 [ + + ]: 38464 : if (doSort)
3931 : : {
3932 : 32969 : int processGroupingSets = Max(phase->numsets, 1);
2336 3933 : 32969 : int setoff = 0;
3934 : :
3935 [ + + ]: 66718 : for (int setno = 0; setno < processGroupingSets; setno++)
3936 : : {
3094 3937 : 33749 : ExecBuildAggTransCall(state, aggstate, &scratch, trans_fcinfo,
3938 : : pertrans, transno, setno, setoff, false,
3939 : : nullcheck);
3940 : 33749 : setoff++;
3941 : : }
3942 : : }
3943 : :
3944 [ + + ]: 38464 : if (doHash)
3945 : : {
3946 : 5745 : int numHashes = aggstate->num_hashes;
3947 : : int setoff;
3948 : :
3949 : : /* in MIXED mode, there'll be preceding transition values */
3950 [ + + ]: 5745 : if (aggstate->aggstrategy != AGG_HASHED)
3951 : 266 : setoff = aggstate->maxsets;
3952 : : else
3953 : 5479 : setoff = 0;
3954 : :
2336 3955 [ + + ]: 12296 : for (int setno = 0; setno < numHashes; setno++)
3956 : : {
3094 3957 : 6551 : ExecBuildAggTransCall(state, aggstate, &scratch, trans_fcinfo,
3958 : : pertrans, transno, setno, setoff, true,
3959 : : nullcheck);
3960 : 6551 : setoff++;
3961 : : }
3962 : : }
3963 : :
3964 : : /* adjust early bail out jump target(s) */
3965 [ + + + + : 46376 : foreach(bail, adjust_bailout)
+ + ]
3966 : : {
3967 : 7912 : ExprEvalStep *as = &state->steps[lfirst_int(bail)];
3968 : :
3969 [ + + ]: 7912 : if (as->opcode == EEOP_JUMP_IF_NOT_TRUE)
3970 : : {
3971 [ - + ]: 428 : Assert(as->d.jump.jumpdone == -1);
3972 : 428 : as->d.jump.jumpdone = state->steps_len;
3973 : : }
2712 3974 [ + + ]: 7484 : else if (as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_ARGS ||
476 dgustafsson@postgres 3975 [ + + ]: 7316 : as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1 ||
2712 andres@anarazel.de 3976 [ + + ]: 482 : as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_NULLS)
3977 : : {
3094 3978 [ - + ]: 7110 : Assert(as->d.agg_strict_input_check.jumpnull == -1);
3979 : 7110 : as->d.agg_strict_input_check.jumpnull = state->steps_len;
3980 : : }
3981 [ + + ]: 374 : else if (as->opcode == EEOP_AGG_STRICT_DESERIALIZE)
3982 : : {
3983 [ - + ]: 80 : Assert(as->d.agg_deserialize.jumpnull == -1);
3984 : 80 : as->d.agg_deserialize.jumpnull = state->steps_len;
3985 : : }
1428 drowley@postgresql.o 3986 [ + + ]: 294 : else if (as->opcode == EEOP_AGG_PRESORTED_DISTINCT_SINGLE ||
3987 [ + - ]: 68 : as->opcode == EEOP_AGG_PRESORTED_DISTINCT_MULTI)
3988 : : {
3989 [ - + ]: 294 : Assert(as->d.agg_presorted_distinctcheck.jumpdistinct == -1);
3990 : 294 : as->d.agg_presorted_distinctcheck.jumpdistinct = state->steps_len;
3991 : : }
3992 : : else
2336 andres@anarazel.de 3993 :UBC 0 : Assert(false);
3994 : : }
3995 : : }
3996 : :
3094 andres@anarazel.de 3997 :CBC 34743 : scratch.resvalue = NULL;
3998 : 34743 : scratch.resnull = NULL;
476 dgustafsson@postgres 3999 : 34743 : scratch.opcode = EEOP_DONE_NO_RETURN;
3094 andres@anarazel.de 4000 : 34743 : ExprEvalPushStep(state, &scratch);
4001 : :
4002 : 34743 : ExecReadyExpr(state);
4003 : :
4004 : 34743 : return state;
4005 : : }
4006 : :
4007 : : /*
4008 : : * Build transition/combine function invocation for a single transition
4009 : : * value. This is separated from ExecBuildAggTrans() because there are
4010 : : * multiple callsites (hash and sort in some grouping set cases).
4011 : : */
4012 : : static void
4013 : 40300 : ExecBuildAggTransCall(ExprState *state, AggState *aggstate,
4014 : : ExprEvalStep *scratch,
4015 : : FunctionCallInfo fcinfo, AggStatePerTrans pertrans,
4016 : : int transno, int setno, int setoff, bool ishash,
4017 : : bool nullcheck)
4018 : : {
4019 : : ExprContext *aggcontext;
2238 tgl@sss.pgh.pa.us 4020 : 40300 : int adjust_jumpnull = -1;
4021 : :
3094 andres@anarazel.de 4022 [ + + ]: 40300 : if (ishash)
4023 : 6551 : aggcontext = aggstate->hashcontext;
4024 : : else
4025 : 33749 : aggcontext = aggstate->aggcontexts[setno];
4026 : :
4027 : : /* add check for NULL pointer? */
2309 jdavis@postgresql.or 4028 [ + + ]: 40300 : if (nullcheck)
4029 : : {
4030 : 278 : scratch->opcode = EEOP_AGG_PLAIN_PERGROUP_NULLCHECK;
4031 : 278 : scratch->d.agg_plain_pergroup_nullcheck.setoff = setoff;
4032 : : /* adjust later */
4033 : 278 : scratch->d.agg_plain_pergroup_nullcheck.jumpnull = -1;
4034 : 278 : ExprEvalPushStep(state, scratch);
4035 : 278 : adjust_jumpnull = state->steps_len - 1;
4036 : : }
4037 : :
4038 : : /*
4039 : : * Determine appropriate transition implementation.
4040 : : *
4041 : : * For non-ordered aggregates and ORDER BY / DISTINCT aggregates with
4042 : : * presorted input:
4043 : : *
4044 : : * If the initial value for the transition state doesn't exist in the
4045 : : * pg_aggregate table then we will let the first non-NULL value returned
4046 : : * from the outer procNode become the initial value. (This is useful for
4047 : : * aggregates like max() and min().) The noTransValue flag signals that we
4048 : : * need to do so. If true, generate a
4049 : : * EEOP_AGG_INIT_STRICT_PLAIN_TRANS{,_BYVAL} step. This step also needs to
4050 : : * do the work described next:
4051 : : *
4052 : : * If the function is strict, but does have an initial value, choose
4053 : : * EEOP_AGG_STRICT_PLAIN_TRANS{,_BYVAL}, which skips the transition
4054 : : * function if the transition value has become NULL (because a previous
4055 : : * transition function returned NULL). This step also needs to do the work
4056 : : * described next:
4057 : : *
4058 : : * Otherwise we call EEOP_AGG_PLAIN_TRANS{,_BYVAL}, which does not have to
4059 : : * perform either of the above checks.
4060 : : *
4061 : : * Having steps with overlapping responsibilities is not nice, but
4062 : : * aggregations are very performance sensitive, making this worthwhile.
4063 : : *
4064 : : * For ordered aggregates:
4065 : : *
4066 : : * Only need to choose between the faster path for a single ordered
4067 : : * column, and the one between multiple columns. Checking strictness etc
4068 : : * is done when finalizing the aggregate. See
4069 : : * process_ordered_aggregate_{single, multi} and
4070 : : * advance_transition_function.
4071 : : */
1428 drowley@postgresql.o 4072 [ + + ]: 40300 : if (!pertrans->aggsortrequired)
4073 : : {
2318 andres@anarazel.de 4074 [ + + ]: 40072 : if (pertrans->transtypeByVal)
4075 : : {
4076 [ + + ]: 37290 : if (fcinfo->flinfo->fn_strict &&
4077 [ + + ]: 19088 : pertrans->initValueIsNull)
4078 : 3134 : scratch->opcode = EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL;
4079 [ + + ]: 34156 : else if (fcinfo->flinfo->fn_strict)
4080 : 15954 : scratch->opcode = EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL;
4081 : : else
4082 : 18202 : scratch->opcode = EEOP_AGG_PLAIN_TRANS_BYVAL;
4083 : : }
4084 : : else
4085 : : {
4086 [ + + ]: 2782 : if (fcinfo->flinfo->fn_strict &&
4087 [ + + ]: 2535 : pertrans->initValueIsNull)
4088 : 725 : scratch->opcode = EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF;
4089 [ + + ]: 2057 : else if (fcinfo->flinfo->fn_strict)
4090 : 1810 : scratch->opcode = EEOP_AGG_PLAIN_TRANS_STRICT_BYREF;
4091 : : else
4092 : 247 : scratch->opcode = EEOP_AGG_PLAIN_TRANS_BYREF;
4093 : : }
4094 : : }
3094 4095 [ + + ]: 228 : else if (pertrans->numInputs == 1)
4096 : 188 : scratch->opcode = EEOP_AGG_ORDERED_TRANS_DATUM;
4097 : : else
4098 : 40 : scratch->opcode = EEOP_AGG_ORDERED_TRANS_TUPLE;
4099 : :
4100 : 40300 : scratch->d.agg_trans.pertrans = pertrans;
4101 : 40300 : scratch->d.agg_trans.setno = setno;
4102 : 40300 : scratch->d.agg_trans.setoff = setoff;
4103 : 40300 : scratch->d.agg_trans.transno = transno;
4104 : 40300 : scratch->d.agg_trans.aggcontext = aggcontext;
4105 : 40300 : ExprEvalPushStep(state, scratch);
4106 : :
4107 : : /* fix up jumpnull */
2309 jdavis@postgresql.or 4108 [ + + ]: 40300 : if (adjust_jumpnull != -1)
4109 : : {
4110 : 278 : ExprEvalStep *as = &state->steps[adjust_jumpnull];
4111 : :
4112 [ - + ]: 278 : Assert(as->opcode == EEOP_AGG_PLAIN_PERGROUP_NULLCHECK);
4113 [ - + ]: 278 : Assert(as->d.agg_plain_pergroup_nullcheck.jumpnull == -1);
4114 : 278 : as->d.agg_plain_pergroup_nullcheck.jumpnull = state->steps_len;
4115 : : }
3094 andres@anarazel.de 4116 : 40300 : }
4117 : :
4118 : : /*
4119 : : * Build an ExprState that calls the given hash function(s) on the attnums
4120 : : * given by 'keyColIdx' . When numCols > 1, the hash values returned by each
4121 : : * hash function are combined to produce a single hash value.
4122 : : *
4123 : : * desc: tuple descriptor for the to-be-hashed columns
4124 : : * ops: TupleTableSlotOps to use for the give TupleDesc
4125 : : * hashfunctions: FmgrInfos for each hash function to call, one per numCols.
4126 : : * These are used directly in the returned ExprState so must remain allocated.
4127 : : * collations: collation to use when calling the hash function.
4128 : : * numCols: array length of hashfunctions, collations and keyColIdx.
4129 : : * parent: PlanState node that the resulting ExprState will be evaluated at
4130 : : * init_value: Normally 0, but can be set to other values to seed the hash
4131 : : * with. Non-zero is marginally slower, so best to only use if it's provably
4132 : : * worthwhile.
4133 : : */
4134 : : ExprState *
566 drowley@postgresql.o 4135 : 5754 : ExecBuildHash32FromAttrs(TupleDesc desc, const TupleTableSlotOps *ops,
4136 : : FmgrInfo *hashfunctions, Oid *collations,
4137 : : int numCols, AttrNumber *keyColIdx,
4138 : : PlanState *parent, uint32 init_value)
4139 : : {
4140 : 5754 : ExprState *state = makeNode(ExprState);
4141 : 5754 : ExprEvalStep scratch = {0};
4142 : 5754 : NullableDatum *iresult = NULL;
4143 : : intptr_t opcode;
4144 : 5754 : AttrNumber last_attnum = 0;
4145 : :
4146 [ - + ]: 5754 : Assert(numCols >= 0);
4147 : :
4148 : 5754 : state->parent = parent;
4149 : :
4150 : : /*
4151 : : * Make a place to store intermediate hash values between subsequent
4152 : : * hashing of individual columns. We only need this if there is more than
4153 : : * one column to hash or an initial value plus one column.
4154 : : */
4155 [ + + ]: 5754 : if ((int64) numCols + (init_value != 0) > 1)
202 michael@paquier.xyz 4156 :GNC 2263 : iresult = palloc_object(NullableDatum);
4157 : :
4158 : : /* find the highest attnum so we deform the tuple to that point */
566 drowley@postgresql.o 4159 [ + + ]:CBC 15295 : for (int i = 0; i < numCols; i++)
4160 : 9541 : last_attnum = Max(last_attnum, keyColIdx[i]);
4161 : :
4162 : 5754 : scratch.opcode = EEOP_INNER_FETCHSOME;
4163 : 5754 : scratch.d.fetch.last_var = last_attnum;
4164 : 5754 : scratch.d.fetch.fixed = false;
4165 : 5754 : scratch.d.fetch.kind = ops;
4166 : 5754 : scratch.d.fetch.known_desc = desc;
4167 [ + + ]: 5754 : if (ExecComputeSlotInfo(state, &scratch))
4168 : 4059 : ExprEvalPushStep(state, &scratch);
4169 : :
4170 [ + + ]: 5754 : if (init_value == 0)
4171 : : {
4172 : : /*
4173 : : * No initial value, so we can assign the result of the hash function
4174 : : * for the first attribute without having to concern ourselves with
4175 : : * combining the result with any initial value.
4176 : : */
4177 : 5193 : opcode = EEOP_HASHDATUM_FIRST;
4178 : : }
4179 : : else
4180 : : {
4181 : : /*
4182 : : * Set up operation to set the initial value. Normally we store this
4183 : : * in the intermediate hash value location, but if there are no
4184 : : * columns to hash, store it in the ExprState's result field.
4185 : : */
4186 : 561 : scratch.opcode = EEOP_HASHDATUM_SET_INITVAL;
4187 : 561 : scratch.d.hashdatum_initvalue.init_value = UInt32GetDatum(init_value);
4188 [ + - ]: 561 : scratch.resvalue = numCols > 0 ? &iresult->value : &state->resvalue;
4189 [ + - ]: 561 : scratch.resnull = numCols > 0 ? &iresult->isnull : &state->resnull;
4190 : :
4191 : 561 : ExprEvalPushStep(state, &scratch);
4192 : :
4193 : : /*
4194 : : * When using an initial value use the NEXT32 ops as the FIRST ops
4195 : : * would overwrite the stored initial value.
4196 : : */
4197 : 561 : opcode = EEOP_HASHDATUM_NEXT32;
4198 : : }
4199 : :
4200 [ + + ]: 15295 : for (int i = 0; i < numCols; i++)
4201 : : {
4202 : : FmgrInfo *finfo;
4203 : : FunctionCallInfo fcinfo;
4204 : 9541 : Oid inputcollid = collations[i];
4205 : 9541 : AttrNumber attnum = keyColIdx[i] - 1;
4206 : :
4207 : 9541 : finfo = &hashfunctions[i];
4208 : 9541 : fcinfo = palloc0(SizeForFunctionCallInfo(1));
4209 : :
4210 : : /* Initialize function call parameter structure too */
4211 : 9541 : InitFunctionCallInfoData(*fcinfo, finfo, 1, inputcollid, NULL, NULL);
4212 : :
4213 : : /*
4214 : : * Fetch inner Var for this attnum and store it in the 1st arg of the
4215 : : * hash func.
4216 : : */
4217 : 9541 : scratch.opcode = EEOP_INNER_VAR;
4218 : 9541 : scratch.resvalue = &fcinfo->args[0].value;
4219 : 9541 : scratch.resnull = &fcinfo->args[0].isnull;
4220 : 9541 : scratch.d.var.attnum = attnum;
4221 : 9541 : scratch.d.var.vartype = TupleDescAttr(desc, attnum)->atttypid;
530 dean.a.rasheed@gmail 4222 : 9541 : scratch.d.var.varreturningtype = VAR_RETURNING_DEFAULT;
4223 : :
566 drowley@postgresql.o 4224 : 9541 : ExprEvalPushStep(state, &scratch);
4225 : :
4226 : : /* Call the hash function */
4227 : 9541 : scratch.opcode = opcode;
4228 : :
4229 [ + + ]: 9541 : if (i == numCols - 1)
4230 : : {
4231 : : /*
4232 : : * The result for hashing the final column is stored in the
4233 : : * ExprState.
4234 : : */
4235 : 5754 : scratch.resvalue = &state->resvalue;
4236 : 5754 : scratch.resnull = &state->resnull;
4237 : : }
4238 : : else
4239 : : {
4240 [ - + ]: 3787 : Assert(iresult != NULL);
4241 : :
4242 : : /* intermediate values are stored in an intermediate result */
4243 : 3787 : scratch.resvalue = &iresult->value;
4244 : 3787 : scratch.resnull = &iresult->isnull;
4245 : : }
4246 : :
4247 : : /*
4248 : : * NEXT32 opcodes need to look at the intermediate result. We might
4249 : : * as well just set this for all ops. FIRSTs won't look at it.
4250 : : */
4251 : 9541 : scratch.d.hashdatum.iresult = iresult;
4252 : :
4253 : 9541 : scratch.d.hashdatum.finfo = finfo;
4254 : 9541 : scratch.d.hashdatum.fcinfo_data = fcinfo;
4255 : 9541 : scratch.d.hashdatum.fn_addr = finfo->fn_addr;
4256 : 9541 : scratch.d.hashdatum.jumpdone = -1;
4257 : :
4258 : 9541 : ExprEvalPushStep(state, &scratch);
4259 : :
4260 : : /* subsequent attnums must be combined with the previous */
4261 : 9541 : opcode = EEOP_HASHDATUM_NEXT32;
4262 : : }
4263 : :
4264 : 5754 : scratch.resvalue = NULL;
4265 : 5754 : scratch.resnull = NULL;
476 dgustafsson@postgres 4266 : 5754 : scratch.opcode = EEOP_DONE_RETURN;
566 drowley@postgresql.o 4267 : 5754 : ExprEvalPushStep(state, &scratch);
4268 : :
4269 : 5754 : ExecReadyExpr(state);
4270 : :
4271 : 5754 : return state;
4272 : : }
4273 : :
4274 : : /*
4275 : : * Build an ExprState that calls the given hash function(s) on the given
4276 : : * 'hash_exprs'. When multiple expressions are present, the hash values
4277 : : * returned by each hash function are combined to produce a single hash value.
4278 : : *
4279 : : * If any hash_expr yields NULL and the corresponding hash operator is strict,
4280 : : * the created ExprState will return NULL. (If the operator is not strict,
4281 : : * we treat NULL values as having a hash value of zero. The hash functions
4282 : : * themselves are always treated as strict.)
4283 : : *
4284 : : * desc: tuple descriptor for the to-be-hashed expressions
4285 : : * ops: TupleTableSlotOps for the TupleDesc
4286 : : * hashfunc_oids: Oid for each hash function to call, one for each 'hash_expr'
4287 : : * collations: collation to use when calling the hash function
4288 : : * hash_exprs: list of expressions to hash the value of
4289 : : * opstrict: strictness flag for each hash function's comparison operator
4290 : : * parent: PlanState node that the 'hash_exprs' will be evaluated at
4291 : : * init_value: Normally 0, but can be set to other values to seed the hash
4292 : : * with some other value. Using non-zero is slightly less efficient but can
4293 : : * be useful.
4294 : : */
4295 : : ExprState *
679 4296 : 54472 : ExecBuildHash32Expr(TupleDesc desc, const TupleTableSlotOps *ops,
4297 : : const Oid *hashfunc_oids, const List *collations,
4298 : : const List *hash_exprs, const bool *opstrict,
4299 : : PlanState *parent, uint32 init_value)
4300 : : {
4301 : 54472 : ExprState *state = makeNode(ExprState);
4302 : 54472 : ExprEvalStep scratch = {0};
621 4303 : 54472 : NullableDatum *iresult = NULL;
679 4304 : 54472 : List *adjust_jumps = NIL;
4305 : : ListCell *lc;
4306 : : ListCell *lc2;
4307 : : intptr_t strict_opcode;
4308 : : intptr_t opcode;
601 4309 : 54472 : int num_exprs = list_length(hash_exprs);
4310 : :
4311 [ - + ]: 54472 : Assert(num_exprs == list_length(collations));
4312 : :
679 4313 : 54472 : state->parent = parent;
4314 : :
4315 : : /* Insert setup steps as needed. */
4316 : 54472 : ExecCreateExprSetupSteps(state, (Node *) hash_exprs);
4317 : :
4318 : : /*
4319 : : * Make a place to store intermediate hash values between subsequent
4320 : : * hashing of individual expressions. We only need this if there is more
4321 : : * than one expression to hash or an initial value plus one expression.
4322 : : */
601 4323 [ + + ]: 54472 : if ((int64) num_exprs + (init_value != 0) > 1)
202 michael@paquier.xyz 4324 :GNC 5132 : iresult = palloc_object(NullableDatum);
4325 : :
679 drowley@postgresql.o 4326 [ + - ]:CBC 54472 : if (init_value == 0)
4327 : : {
4328 : : /*
4329 : : * No initial value, so we can assign the result of the hash function
4330 : : * for the first hash_expr without having to concern ourselves with
4331 : : * combining the result with any initial value.
4332 : : */
4333 : 54472 : strict_opcode = EEOP_HASHDATUM_FIRST_STRICT;
4334 : 54472 : opcode = EEOP_HASHDATUM_FIRST;
4335 : : }
4336 : : else
4337 : : {
4338 : : /*
4339 : : * Set up operation to set the initial value. Normally we store this
4340 : : * in the intermediate hash value location, but if there are no exprs
4341 : : * to hash, store it in the ExprState's result field.
4342 : : */
679 drowley@postgresql.o 4343 :UBC 0 : scratch.opcode = EEOP_HASHDATUM_SET_INITVAL;
4344 : 0 : scratch.d.hashdatum_initvalue.init_value = UInt32GetDatum(init_value);
601 4345 [ # # ]: 0 : scratch.resvalue = num_exprs > 0 ? &iresult->value : &state->resvalue;
4346 [ # # ]: 0 : scratch.resnull = num_exprs > 0 ? &iresult->isnull : &state->resnull;
4347 : :
679 4348 : 0 : ExprEvalPushStep(state, &scratch);
4349 : :
4350 : : /*
4351 : : * When using an initial value use the NEXT32/NEXT32_STRICT ops as the
4352 : : * FIRST/FIRST_STRICT ops would overwrite the stored initial value.
4353 : : */
4354 : 0 : strict_opcode = EEOP_HASHDATUM_NEXT32_STRICT;
4355 : 0 : opcode = EEOP_HASHDATUM_NEXT32;
4356 : : }
4357 : :
679 drowley@postgresql.o 4358 [ + - + + :CBC 114460 : forboth(lc, hash_exprs, lc2, collations)
+ - + + +
+ + - +
+ ]
4359 : : {
4360 : 59988 : Expr *expr = (Expr *) lfirst(lc);
4361 : : FmgrInfo *finfo;
4362 : : FunctionCallInfo fcinfo;
4363 : 59988 : int i = foreach_current_index(lc);
4364 : : Oid funcid;
4365 : 59988 : Oid inputcollid = lfirst_oid(lc2);
4366 : :
4367 : 59988 : funcid = hashfunc_oids[i];
4368 : :
4369 : : /* Allocate hash function lookup data. */
202 michael@paquier.xyz 4370 :GNC 59988 : finfo = palloc0_object(FmgrInfo);
679 drowley@postgresql.o 4371 :CBC 59988 : fcinfo = palloc0(SizeForFunctionCallInfo(1));
4372 : :
4373 : 59988 : fmgr_info(funcid, finfo);
4374 : :
4375 : : /*
4376 : : * Build the steps to evaluate the hash function's argument, placing
4377 : : * the value in the 0th argument of the hash func.
4378 : : */
4379 : 59988 : ExecInitExprRec(expr,
4380 : : state,
4381 : : &fcinfo->args[0].value,
4382 : : &fcinfo->args[0].isnull);
4383 : :
601 4384 [ + + ]: 59988 : if (i == num_exprs - 1)
4385 : : {
4386 : : /* the result for hashing the final expr is stored in the state */
621 4387 : 54472 : scratch.resvalue = &state->resvalue;
4388 : 54472 : scratch.resnull = &state->resnull;
4389 : : }
4390 : : else
4391 : : {
4392 [ - + ]: 5516 : Assert(iresult != NULL);
4393 : :
4394 : : /* intermediate values are stored in an intermediate result */
4395 : 5516 : scratch.resvalue = &iresult->value;
4396 : 5516 : scratch.resnull = &iresult->isnull;
4397 : : }
4398 : :
4399 : : /*
4400 : : * NEXT32 opcodes need to look at the intermediate result. We might
4401 : : * as well just set this for all ops. FIRSTs won't look at it.
4402 : : */
4403 : 59988 : scratch.d.hashdatum.iresult = iresult;
4404 : :
4405 : : /* Initialize function call parameter structure too */
679 4406 : 59988 : InitFunctionCallInfoData(*fcinfo, finfo, 1, inputcollid, NULL, NULL);
4407 : :
4408 : 59988 : scratch.d.hashdatum.finfo = finfo;
4409 : 59988 : scratch.d.hashdatum.fcinfo_data = fcinfo;
4410 : 59988 : scratch.d.hashdatum.fn_addr = finfo->fn_addr;
4411 : :
103 tgl@sss.pgh.pa.us 4412 [ + - ]:GNC 59988 : scratch.opcode = opstrict[i] ? strict_opcode : opcode;
679 drowley@postgresql.o 4413 :CBC 59988 : scratch.d.hashdatum.jumpdone = -1;
4414 : :
4415 : 59988 : ExprEvalPushStep(state, &scratch);
4416 : 59988 : adjust_jumps = lappend_int(adjust_jumps, state->steps_len - 1);
4417 : :
4418 : : /*
4419 : : * For subsequent keys we must combine the hash value with the
4420 : : * previous hashes.
4421 : : */
4422 : 59988 : strict_opcode = EEOP_HASHDATUM_NEXT32_STRICT;
4423 : 59988 : opcode = EEOP_HASHDATUM_NEXT32;
4424 : : }
4425 : :
4426 : : /* adjust jump targets */
4427 [ + - + + : 114460 : foreach(lc, adjust_jumps)
+ + ]
4428 : : {
4429 : 59988 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
4430 : :
4431 [ + - + + : 59988 : Assert(as->opcode == EEOP_HASHDATUM_FIRST ||
+ - - + ]
4432 : : as->opcode == EEOP_HASHDATUM_FIRST_STRICT ||
4433 : : as->opcode == EEOP_HASHDATUM_NEXT32 ||
4434 : : as->opcode == EEOP_HASHDATUM_NEXT32_STRICT);
4435 [ - + ]: 59988 : Assert(as->d.hashdatum.jumpdone == -1);
4436 : 59988 : as->d.hashdatum.jumpdone = state->steps_len;
4437 : : }
4438 : :
4439 : 54472 : scratch.resvalue = NULL;
4440 : 54472 : scratch.resnull = NULL;
476 dgustafsson@postgres 4441 : 54472 : scratch.opcode = EEOP_DONE_RETURN;
679 drowley@postgresql.o 4442 : 54472 : ExprEvalPushStep(state, &scratch);
4443 : :
4444 : 54472 : ExecReadyExpr(state);
4445 : :
4446 : 54472 : return state;
4447 : : }
4448 : :
4449 : : /*
4450 : : * Build equality expression that can be evaluated using ExecQual(), returning
4451 : : * true if the expression context's inner/outer tuple are NOT DISTINCT. I.e
4452 : : * two nulls match, a null and a not-null don't match.
4453 : : *
4454 : : * desc: tuple descriptor of the to-be-compared tuples
4455 : : * numCols: the number of attributes to be examined
4456 : : * keyColIdx: array of attribute column numbers
4457 : : * eqFunctions: array of function oids of the equality functions to use
4458 : : * parent: parent executor node
4459 : : */
4460 : : ExprState *
3057 andres@anarazel.de 4461 : 13529 : ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc,
4462 : : const TupleTableSlotOps *lops, const TupleTableSlotOps *rops,
4463 : : int numCols,
4464 : : const AttrNumber *keyColIdx,
4465 : : const Oid *eqfunctions,
4466 : : const Oid *collations,
4467 : : PlanState *parent)
4468 : : {
4469 : 13529 : ExprState *state = makeNode(ExprState);
4470 : 13529 : ExprEvalStep scratch = {0};
4471 : 13529 : int maxatt = -1;
4472 : 13529 : List *adjust_jumps = NIL;
4473 : : ListCell *lc;
4474 : :
4475 : : /*
4476 : : * When no columns are actually compared, the result's always true. See
4477 : : * special case in ExecQual().
4478 : : */
4479 [ - + ]: 13529 : if (numCols == 0)
3057 andres@anarazel.de 4480 :UBC 0 : return NULL;
4481 : :
3057 andres@anarazel.de 4482 :CBC 13529 : state->expr = NULL;
4483 : 13529 : state->flags = EEO_FLAG_IS_QUAL;
4484 : 13529 : state->parent = parent;
4485 : :
4486 : 13529 : scratch.resvalue = &state->resvalue;
4487 : 13529 : scratch.resnull = &state->resnull;
4488 : :
4489 : : /* compute max needed attribute */
2336 4490 [ + + ]: 36698 : for (int natt = 0; natt < numCols; natt++)
4491 : : {
3057 4492 : 23169 : int attno = keyColIdx[natt];
4493 : :
4494 [ + + ]: 23169 : if (attno > maxatt)
4495 : 22913 : maxatt = attno;
4496 : : }
4497 [ - + ]: 13529 : Assert(maxatt >= 0);
4498 : :
4499 : : /* push deform steps */
4500 : 13529 : scratch.opcode = EEOP_INNER_FETCHSOME;
4501 : 13529 : scratch.d.fetch.last_var = maxatt;
2784 4502 : 13529 : scratch.d.fetch.fixed = false;
3018 4503 : 13529 : scratch.d.fetch.known_desc = ldesc;
2784 4504 : 13529 : scratch.d.fetch.kind = lops;
2465 4505 [ + + ]: 13529 : if (ExecComputeSlotInfo(state, &scratch))
4506 : 11834 : ExprEvalPushStep(state, &scratch);
4507 : :
3057 4508 : 13529 : scratch.opcode = EEOP_OUTER_FETCHSOME;
4509 : 13529 : scratch.d.fetch.last_var = maxatt;
2784 4510 : 13529 : scratch.d.fetch.fixed = false;
3018 4511 : 13529 : scratch.d.fetch.known_desc = rdesc;
2784 4512 : 13529 : scratch.d.fetch.kind = rops;
2465 4513 [ + - ]: 13529 : if (ExecComputeSlotInfo(state, &scratch))
4514 : 13529 : ExprEvalPushStep(state, &scratch);
4515 : :
4516 : : /*
4517 : : * Start comparing at the last field (least significant sort key). That's
4518 : : * the most likely to be different if we are dealing with sorted input.
4519 : : */
2336 4520 [ + + ]: 36698 : for (int natt = numCols; --natt >= 0;)
4521 : : {
3057 4522 : 23169 : int attno = keyColIdx[natt];
4523 : 23169 : Form_pg_attribute latt = TupleDescAttr(ldesc, attno - 1);
4524 : 23169 : Form_pg_attribute ratt = TupleDescAttr(rdesc, attno - 1);
4525 : 23169 : Oid foid = eqfunctions[natt];
2657 peter@eisentraut.org 4526 : 23169 : Oid collid = collations[natt];
4527 : : FmgrInfo *finfo;
4528 : : FunctionCallInfo fcinfo;
4529 : : AclResult aclresult;
4530 : :
4531 : : /* Check permission to call function */
1325 4532 : 23169 : aclresult = object_aclcheck(ProcedureRelationId, foid, GetUserId(), ACL_EXECUTE);
3057 andres@anarazel.de 4533 [ - + ]: 23169 : if (aclresult != ACLCHECK_OK)
3057 andres@anarazel.de 4534 :UBC 0 : aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(foid));
4535 : :
3057 andres@anarazel.de 4536 [ - + ]:CBC 23169 : InvokeFunctionExecuteHook(foid);
4537 : :
4538 : : /* Set up the primary fmgr lookup information */
202 michael@paquier.xyz 4539 :GNC 23169 : finfo = palloc0_object(FmgrInfo);
2712 andres@anarazel.de 4540 :CBC 23169 : fcinfo = palloc0(SizeForFunctionCallInfo(2));
3057 4541 : 23169 : fmgr_info(foid, finfo);
4542 : 23169 : fmgr_info_set_expr(NULL, finfo);
4543 : 23169 : InitFunctionCallInfoData(*fcinfo, finfo, 2,
4544 : : collid, NULL, NULL);
4545 : :
4546 : : /* left arg */
4547 : 23169 : scratch.opcode = EEOP_INNER_VAR;
4548 : 23169 : scratch.d.var.attnum = attno - 1;
4549 : 23169 : scratch.d.var.vartype = latt->atttypid;
530 dean.a.rasheed@gmail 4550 : 23169 : scratch.d.var.varreturningtype = VAR_RETURNING_DEFAULT;
2712 andres@anarazel.de 4551 : 23169 : scratch.resvalue = &fcinfo->args[0].value;
4552 : 23169 : scratch.resnull = &fcinfo->args[0].isnull;
3057 4553 : 23169 : ExprEvalPushStep(state, &scratch);
4554 : :
4555 : : /* right arg */
4556 : 23169 : scratch.opcode = EEOP_OUTER_VAR;
4557 : 23169 : scratch.d.var.attnum = attno - 1;
4558 : 23169 : scratch.d.var.vartype = ratt->atttypid;
530 dean.a.rasheed@gmail 4559 : 23169 : scratch.d.var.varreturningtype = VAR_RETURNING_DEFAULT;
2712 andres@anarazel.de 4560 : 23169 : scratch.resvalue = &fcinfo->args[1].value;
4561 : 23169 : scratch.resnull = &fcinfo->args[1].isnull;
3057 4562 : 23169 : ExprEvalPushStep(state, &scratch);
4563 : :
4564 : : /* evaluate distinctness */
1915 drowley@postgresql.o 4565 : 23169 : scratch.opcode = EEOP_NOT_DISTINCT;
4566 : 23169 : scratch.d.func.finfo = finfo;
4567 : 23169 : scratch.d.func.fcinfo_data = fcinfo;
4568 : 23169 : scratch.d.func.fn_addr = finfo->fn_addr;
4569 : 23169 : scratch.d.func.nargs = 2;
4570 : 23169 : scratch.resvalue = &state->resvalue;
4571 : 23169 : scratch.resnull = &state->resnull;
4572 : 23169 : ExprEvalPushStep(state, &scratch);
4573 : :
4574 : : /* then emit EEOP_QUAL to detect if result is false (or null) */
4575 : 23169 : scratch.opcode = EEOP_QUAL;
4576 : 23169 : scratch.d.qualexpr.jumpdone = -1;
4577 : 23169 : scratch.resvalue = &state->resvalue;
4578 : 23169 : scratch.resnull = &state->resnull;
4579 : 23169 : ExprEvalPushStep(state, &scratch);
4580 : 23169 : adjust_jumps = lappend_int(adjust_jumps,
4581 : 23169 : state->steps_len - 1);
4582 : : }
4583 : :
4584 : : /* adjust jump targets */
4585 [ + - + + : 36698 : foreach(lc, adjust_jumps)
+ + ]
4586 : : {
4587 : 23169 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
4588 : :
4589 [ - + ]: 23169 : Assert(as->opcode == EEOP_QUAL);
4590 [ - + ]: 23169 : Assert(as->d.qualexpr.jumpdone == -1);
4591 : 23169 : as->d.qualexpr.jumpdone = state->steps_len;
4592 : : }
4593 : :
4594 : 13529 : scratch.resvalue = NULL;
4595 : 13529 : scratch.resnull = NULL;
476 dgustafsson@postgres 4596 : 13529 : scratch.opcode = EEOP_DONE_RETURN;
1915 drowley@postgresql.o 4597 : 13529 : ExprEvalPushStep(state, &scratch);
4598 : :
4599 : 13529 : ExecReadyExpr(state);
4600 : :
4601 : 13529 : return state;
4602 : : }
4603 : :
4604 : : /*
4605 : : * Build equality expression that can be evaluated using ExecQual(), returning
4606 : : * true if the expression context's inner/outer tuples are equal. Datums in
4607 : : * the inner/outer slots are assumed to be in the same order and quantity as
4608 : : * the 'eqfunctions' parameter. NULLs are treated as equal.
4609 : : *
4610 : : * desc: tuple descriptor of the to-be-compared tuples
4611 : : * lops: the slot ops for the inner tuple slots
4612 : : * rops: the slot ops for the outer tuple slots
4613 : : * eqFunctions: array of function oids of the equality functions to use
4614 : : * this must be the same length as the 'param_exprs' list.
4615 : : * collations: collation Oids to use for equality comparison. Must be the
4616 : : * same length as the 'param_exprs' list.
4617 : : * parent: parent executor node
4618 : : */
4619 : : ExprState *
4620 : 1328 : ExecBuildParamSetEqual(TupleDesc desc,
4621 : : const TupleTableSlotOps *lops,
4622 : : const TupleTableSlotOps *rops,
4623 : : const Oid *eqfunctions,
4624 : : const Oid *collations,
4625 : : const List *param_exprs,
4626 : : PlanState *parent)
4627 : : {
4628 : 1328 : ExprState *state = makeNode(ExprState);
4629 : 1328 : ExprEvalStep scratch = {0};
4630 : 1328 : int maxatt = list_length(param_exprs);
4631 : 1328 : List *adjust_jumps = NIL;
4632 : : ListCell *lc;
4633 : :
4634 : 1328 : state->expr = NULL;
4635 : 1328 : state->flags = EEO_FLAG_IS_QUAL;
4636 : 1328 : state->parent = parent;
4637 : :
4638 : 1328 : scratch.resvalue = &state->resvalue;
4639 : 1328 : scratch.resnull = &state->resnull;
4640 : :
4641 : : /* push deform steps */
4642 : 1328 : scratch.opcode = EEOP_INNER_FETCHSOME;
4643 : 1328 : scratch.d.fetch.last_var = maxatt;
4644 : 1328 : scratch.d.fetch.fixed = false;
4645 : 1328 : scratch.d.fetch.known_desc = desc;
4646 : 1328 : scratch.d.fetch.kind = lops;
4647 [ + - ]: 1328 : if (ExecComputeSlotInfo(state, &scratch))
4648 : 1328 : ExprEvalPushStep(state, &scratch);
4649 : :
4650 : 1328 : scratch.opcode = EEOP_OUTER_FETCHSOME;
4651 : 1328 : scratch.d.fetch.last_var = maxatt;
4652 : 1328 : scratch.d.fetch.fixed = false;
4653 : 1328 : scratch.d.fetch.known_desc = desc;
4654 : 1328 : scratch.d.fetch.kind = rops;
4655 [ - + ]: 1328 : if (ExecComputeSlotInfo(state, &scratch))
1915 drowley@postgresql.o 4656 :UBC 0 : ExprEvalPushStep(state, &scratch);
4657 : :
1915 drowley@postgresql.o 4658 [ + + ]:CBC 2700 : for (int attno = 0; attno < maxatt; attno++)
4659 : : {
4660 : 1372 : Form_pg_attribute att = TupleDescAttr(desc, attno);
4661 : 1372 : Oid foid = eqfunctions[attno];
4662 : 1372 : Oid collid = collations[attno];
4663 : : FmgrInfo *finfo;
4664 : : FunctionCallInfo fcinfo;
4665 : : AclResult aclresult;
4666 : :
4667 : : /* Check permission to call function */
1325 peter@eisentraut.org 4668 : 1372 : aclresult = object_aclcheck(ProcedureRelationId, foid, GetUserId(), ACL_EXECUTE);
1915 drowley@postgresql.o 4669 [ - + ]: 1372 : if (aclresult != ACLCHECK_OK)
1915 drowley@postgresql.o 4670 :UBC 0 : aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(foid));
4671 : :
1915 drowley@postgresql.o 4672 [ - + ]:CBC 1372 : InvokeFunctionExecuteHook(foid);
4673 : :
4674 : : /* Set up the primary fmgr lookup information */
202 michael@paquier.xyz 4675 :GNC 1372 : finfo = palloc0_object(FmgrInfo);
1915 drowley@postgresql.o 4676 :CBC 1372 : fcinfo = palloc0(SizeForFunctionCallInfo(2));
4677 : 1372 : fmgr_info(foid, finfo);
4678 : 1372 : fmgr_info_set_expr(NULL, finfo);
4679 : 1372 : InitFunctionCallInfoData(*fcinfo, finfo, 2,
4680 : : collid, NULL, NULL);
4681 : :
4682 : : /* left arg */
4683 : 1372 : scratch.opcode = EEOP_INNER_VAR;
4684 : 1372 : scratch.d.var.attnum = attno;
4685 : 1372 : scratch.d.var.vartype = att->atttypid;
530 dean.a.rasheed@gmail 4686 : 1372 : scratch.d.var.varreturningtype = VAR_RETURNING_DEFAULT;
1915 drowley@postgresql.o 4687 : 1372 : scratch.resvalue = &fcinfo->args[0].value;
4688 : 1372 : scratch.resnull = &fcinfo->args[0].isnull;
4689 : 1372 : ExprEvalPushStep(state, &scratch);
4690 : :
4691 : : /* right arg */
4692 : 1372 : scratch.opcode = EEOP_OUTER_VAR;
4693 : 1372 : scratch.d.var.attnum = attno;
4694 : 1372 : scratch.d.var.vartype = att->atttypid;
530 dean.a.rasheed@gmail 4695 : 1372 : scratch.d.var.varreturningtype = VAR_RETURNING_DEFAULT;
1915 drowley@postgresql.o 4696 : 1372 : scratch.resvalue = &fcinfo->args[1].value;
4697 : 1372 : scratch.resnull = &fcinfo->args[1].isnull;
4698 : 1372 : ExprEvalPushStep(state, &scratch);
4699 : :
4700 : : /* evaluate distinctness */
3057 andres@anarazel.de 4701 : 1372 : scratch.opcode = EEOP_NOT_DISTINCT;
4702 : 1372 : scratch.d.func.finfo = finfo;
4703 : 1372 : scratch.d.func.fcinfo_data = fcinfo;
4704 : 1372 : scratch.d.func.fn_addr = finfo->fn_addr;
4705 : 1372 : scratch.d.func.nargs = 2;
4706 : 1372 : scratch.resvalue = &state->resvalue;
4707 : 1372 : scratch.resnull = &state->resnull;
4708 : 1372 : ExprEvalPushStep(state, &scratch);
4709 : :
4710 : : /* then emit EEOP_QUAL to detect if result is false (or null) */
4711 : 1372 : scratch.opcode = EEOP_QUAL;
4712 : 1372 : scratch.d.qualexpr.jumpdone = -1;
4713 : 1372 : scratch.resvalue = &state->resvalue;
4714 : 1372 : scratch.resnull = &state->resnull;
4715 : 1372 : ExprEvalPushStep(state, &scratch);
4716 : 1372 : adjust_jumps = lappend_int(adjust_jumps,
4717 : 1372 : state->steps_len - 1);
4718 : : }
4719 : :
4720 : : /* adjust jump targets */
4721 [ + - + + : 2700 : foreach(lc, adjust_jumps)
+ + ]
4722 : : {
4723 : 1372 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
4724 : :
4725 [ - + ]: 1372 : Assert(as->opcode == EEOP_QUAL);
4726 [ - + ]: 1372 : Assert(as->d.qualexpr.jumpdone == -1);
4727 : 1372 : as->d.qualexpr.jumpdone = state->steps_len;
4728 : : }
4729 : :
4730 : 1328 : scratch.resvalue = NULL;
4731 : 1328 : scratch.resnull = NULL;
476 dgustafsson@postgres 4732 : 1328 : scratch.opcode = EEOP_DONE_RETURN;
3057 andres@anarazel.de 4733 : 1328 : ExprEvalPushStep(state, &scratch);
4734 : :
4735 : 1328 : ExecReadyExpr(state);
4736 : :
4737 : 1328 : return state;
4738 : : }
4739 : :
4740 : : /*
4741 : : * Push steps to evaluate a JsonExpr and its various subsidiary expressions.
4742 : : */
4743 : : static void
831 amitlan@postgresql.o 4744 : 1538 : ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
4745 : : Datum *resv, bool *resnull,
4746 : : ExprEvalStep *scratch)
4747 : : {
202 michael@paquier.xyz 4748 :GNC 1538 : JsonExprState *jsestate = palloc0_object(JsonExprState);
4749 : : ListCell *argexprlc;
4750 : : ListCell *argnamelc;
831 amitlan@postgresql.o 4751 :CBC 1538 : List *jumps_return_null = NIL;
4752 : 1538 : List *jumps_to_end = NIL;
4753 : : ListCell *lc;
4754 : : ErrorSaveContext *escontext;
659 4755 : 1538 : bool returning_domain =
4756 : 1538 : get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN;
4757 : :
4758 [ - + ]: 1538 : Assert(jsexpr->on_error != NULL);
4759 : :
831 4760 : 1538 : jsestate->jsexpr = jsexpr;
4761 : :
4762 : : /*
4763 : : * Evaluate formatted_expr storing the result into
4764 : : * jsestate->formatted_expr.
4765 : : */
4766 : 1538 : ExecInitExprRec((Expr *) jsexpr->formatted_expr, state,
4767 : : &jsestate->formatted_expr.value,
4768 : : &jsestate->formatted_expr.isnull);
4769 : :
4770 : : /* JUMP to return NULL if formatted_expr evaluates to NULL */
4771 : 1538 : jumps_return_null = lappend_int(jumps_return_null, state->steps_len);
4772 : 1538 : scratch->opcode = EEOP_JUMP_IF_NULL;
4773 : 1538 : scratch->resnull = &jsestate->formatted_expr.isnull;
4774 : 1538 : scratch->d.jump.jumpdone = -1; /* set below */
4775 : 1538 : ExprEvalPushStep(state, scratch);
4776 : :
4777 : : /*
4778 : : * Evaluate pathspec expression storing the result into
4779 : : * jsestate->pathspec.
4780 : : */
4781 : 1538 : ExecInitExprRec((Expr *) jsexpr->path_spec, state,
4782 : : &jsestate->pathspec.value,
4783 : : &jsestate->pathspec.isnull);
4784 : :
4785 : : /* JUMP to return NULL if path_spec evaluates to NULL */
4786 : 1538 : jumps_return_null = lappend_int(jumps_return_null, state->steps_len);
4787 : 1538 : scratch->opcode = EEOP_JUMP_IF_NULL;
4788 : 1538 : scratch->resnull = &jsestate->pathspec.isnull;
4789 : 1538 : scratch->d.jump.jumpdone = -1; /* set below */
4790 : 1538 : ExprEvalPushStep(state, scratch);
4791 : :
4792 : : /* Steps to compute PASSING args. */
4793 : 1538 : jsestate->args = NIL;
4794 [ + + + + : 2142 : forboth(argexprlc, jsexpr->passing_values,
+ + + + +
+ + - +
+ ]
4795 : : argnamelc, jsexpr->passing_names)
4796 : : {
4797 : 604 : Expr *argexpr = (Expr *) lfirst(argexprlc);
4798 : 604 : String *argname = lfirst_node(String, argnamelc);
202 michael@paquier.xyz 4799 :GNC 604 : JsonPathVariable *var = palloc_object(JsonPathVariable);
4800 : :
831 amitlan@postgresql.o 4801 :CBC 604 : var->name = argname->sval;
741 4802 : 604 : var->namelen = strlen(var->name);
831 4803 : 604 : var->typid = exprType((Node *) argexpr);
4804 : 604 : var->typmod = exprTypmod((Node *) argexpr);
4805 : :
210 peter@eisentraut.org 4806 :GNC 604 : ExecInitExprRec(argexpr, state, &var->value, &var->isnull);
4807 : :
831 amitlan@postgresql.o 4808 :CBC 604 : jsestate->args = lappend(jsestate->args, var);
4809 : : }
4810 : :
4811 : : /* Step for jsonpath evaluation; see ExecEvalJsonExprPath(). */
4812 : 1538 : scratch->opcode = EEOP_JSONEXPR_PATH;
4813 : 1538 : scratch->resvalue = resv;
4814 : 1538 : scratch->resnull = resnull;
4815 : 1538 : scratch->d.jsonexpr.jsestate = jsestate;
4816 : 1538 : ExprEvalPushStep(state, scratch);
4817 : :
4818 : : /*
4819 : : * Step to return NULL after jumping to skip the EEOP_JSONEXPR_PATH step
4820 : : * when either formatted_expr or pathspec is NULL. Adjust jump target
4821 : : * addresses of JUMPs that we added above.
4822 : : */
4823 [ + - + + : 4614 : foreach(lc, jumps_return_null)
+ + ]
4824 : : {
4825 : 3076 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
4826 : :
4827 : 3076 : as->d.jump.jumpdone = state->steps_len;
4828 : : }
4829 : 1538 : scratch->opcode = EEOP_CONST;
4830 : 1538 : scratch->resvalue = resv;
4831 : 1538 : scratch->resnull = resnull;
4832 : 1538 : scratch->d.constval.value = (Datum) 0;
4833 : 1538 : scratch->d.constval.isnull = true;
4834 : 1538 : ExprEvalPushStep(state, scratch);
4835 : :
659 4836 : 3076 : escontext = jsexpr->on_error->btype != JSON_BEHAVIOR_ERROR ?
4837 [ + + ]: 1538 : &jsestate->escontext : NULL;
4838 : :
4839 : : /*
4840 : : * To handle coercion errors softly, use the following ErrorSaveContext to
4841 : : * pass to ExecInitExprRec() when initializing the coercion expressions
4842 : : * and in the EEOP_JSONEXPR_COERCION step.
4843 : : */
831 4844 : 1538 : jsestate->escontext.type = T_ErrorSaveContext;
4845 : :
4846 : : /*
4847 : : * Steps to coerce the result value computed by EEOP_JSONEXPR_PATH or the
4848 : : * NULL returned on NULL input as described above.
4849 : : */
4850 : 1538 : jsestate->jump_eval_coercion = -1;
732 4851 [ + + ]: 1538 : if (jsexpr->use_json_coercion)
4852 : : {
831 4853 : 592 : jsestate->jump_eval_coercion = state->steps_len;
4854 : :
732 4855 : 592 : ExecInitJsonCoercion(state, jsexpr->returning, escontext,
700 4856 : 592 : jsexpr->omit_quotes,
4857 : 592 : jsexpr->op == JSON_EXISTS_OP,
4858 : : resv, resnull);
4859 : : }
831 4860 [ + + ]: 946 : else if (jsexpr->use_io_coercion)
4861 : : {
4862 : : /*
4863 : : * Here we only need to initialize the FunctionCallInfo for the target
4864 : : * type's input function, which is called by ExecEvalJsonExprPath()
4865 : : * itself, so no additional step is necessary.
4866 : : */
4867 : : Oid typinput;
4868 : : Oid typioparam;
4869 : : FmgrInfo *finfo;
4870 : : FunctionCallInfo fcinfo;
4871 : :
4872 : 430 : getTypeInputInfo(jsexpr->returning->typid, &typinput, &typioparam);
202 michael@paquier.xyz 4873 :GNC 430 : finfo = palloc0_object(FmgrInfo);
831 amitlan@postgresql.o 4874 :CBC 430 : fcinfo = palloc0(SizeForFunctionCallInfo(3));
4875 : 430 : fmgr_info(typinput, finfo);
4876 : 430 : fmgr_info_set_expr((Node *) jsexpr->returning, finfo);
4877 : 430 : InitFunctionCallInfoData(*fcinfo, finfo, 3, InvalidOid, NULL, NULL);
4878 : :
4879 : : /*
4880 : : * We can preload the second and third arguments for the input
4881 : : * function, since they're constants.
4882 : : */
4883 : 430 : fcinfo->args[1].value = ObjectIdGetDatum(typioparam);
4884 : 430 : fcinfo->args[1].isnull = false;
4885 : 430 : fcinfo->args[2].value = Int32GetDatum(jsexpr->returning->typmod);
4886 : 430 : fcinfo->args[2].isnull = false;
4887 : 430 : fcinfo->context = (Node *) escontext;
4888 : :
4889 : 430 : jsestate->input_fcinfo = fcinfo;
4890 : : }
4891 : :
4892 : : /*
4893 : : * Add a special step, if needed, to check if the coercion evaluation ran
4894 : : * into an error but was not thrown because the ON ERROR behavior is not
4895 : : * ERROR. It will set jsestate->error if an error did occur.
4896 : : */
4897 [ + + + + ]: 1538 : if (jsestate->jump_eval_coercion >= 0 && escontext != NULL)
4898 : : {
4899 : 444 : scratch->opcode = EEOP_JSONEXPR_COERCION_FINISH;
4900 : 444 : scratch->d.jsonexpr.jsestate = jsestate;
4901 : 444 : ExprEvalPushStep(state, scratch);
4902 : : }
4903 : :
662 4904 : 1538 : jsestate->jump_empty = jsestate->jump_error = -1;
4905 : :
4906 : : /*
4907 : : * Step to check jsestate->error and return the ON ERROR expression if
4908 : : * there is one. This handles both the errors that occur during jsonpath
4909 : : * evaluation in EEOP_JSONEXPR_PATH and subsequent coercion evaluation.
4910 : : *
4911 : : * Speed up common cases by avoiding extra steps for a NULL-valued ON
4912 : : * ERROR expression unless RETURNING a domain type, where constraints must
4913 : : * be checked. ExecEvalJsonExprPath() already returns NULL on error,
4914 : : * making additional steps unnecessary in typical scenarios. Note that the
4915 : : * default ON ERROR behavior for JSON_VALUE() and JSON_QUERY() is to
4916 : : * return NULL.
4917 : : */
659 4918 [ + + ]: 1538 : if (jsexpr->on_error->btype != JSON_BEHAVIOR_ERROR &&
4919 [ + + ]: 1254 : (!(IsA(jsexpr->on_error->expr, Const) &&
4920 [ + + + + ]: 1222 : ((Const *) jsexpr->on_error->expr)->constisnull) ||
4921 : : returning_domain))
4922 : : {
4923 : : ErrorSaveContext *saved_escontext;
4924 : :
662 4925 : 396 : jsestate->jump_error = state->steps_len;
4926 : :
4927 : : /* JUMP to end if false, that is, skip the ON ERROR expression. */
831 4928 : 396 : jumps_to_end = lappend_int(jumps_to_end, state->steps_len);
4929 : 396 : scratch->opcode = EEOP_JUMP_IF_NOT_TRUE;
4930 : 396 : scratch->resvalue = &jsestate->error.value;
4931 : 396 : scratch->resnull = &jsestate->error.isnull;
4932 : 396 : scratch->d.jump.jumpdone = -1; /* set below */
4933 : 396 : ExprEvalPushStep(state, scratch);
4934 : :
4935 : : /*
4936 : : * Steps to evaluate the ON ERROR expression; handle errors softly to
4937 : : * rethrow them in COERCION_FINISH step that will be added later.
4938 : : */
704 4939 : 396 : saved_escontext = state->escontext;
4940 : 396 : state->escontext = escontext;
831 4941 : 396 : ExecInitExprRec((Expr *) jsexpr->on_error->expr,
4942 : : state, resv, resnull);
704 4943 : 396 : state->escontext = saved_escontext;
4944 : :
4945 : : /* Step to coerce the ON ERROR expression if needed */
831 4946 [ + + ]: 396 : if (jsexpr->on_error->coerce)
732 4947 : 116 : ExecInitJsonCoercion(state, jsexpr->returning, escontext,
700 4948 : 116 : jsexpr->omit_quotes, false,
4949 : : resv, resnull);
4950 : :
4951 : : /*
4952 : : * Add a COERCION_FINISH step to check for errors that may occur when
4953 : : * coercing and rethrow them.
4954 : : */
704 4955 [ + + ]: 396 : if (jsexpr->on_error->coerce ||
4956 [ + - ]: 280 : IsA(jsexpr->on_error->expr, CoerceViaIO) ||
4957 [ + + ]: 280 : IsA(jsexpr->on_error->expr, CoerceToDomain))
4958 : : {
4959 : 148 : scratch->opcode = EEOP_JSONEXPR_COERCION_FINISH;
4960 : 148 : scratch->resvalue = resv;
4961 : 148 : scratch->resnull = resnull;
4962 : 148 : scratch->d.jsonexpr.jsestate = jsestate;
4963 : 148 : ExprEvalPushStep(state, scratch);
4964 : : }
4965 : :
4966 : : /* JUMP to end to skip the ON EMPTY steps added below. */
831 4967 : 396 : jumps_to_end = lappend_int(jumps_to_end, state->steps_len);
4968 : 396 : scratch->opcode = EEOP_JUMP;
4969 : 396 : scratch->d.jump.jumpdone = -1;
4970 : 396 : ExprEvalPushStep(state, scratch);
4971 : : }
4972 : :
4973 : : /*
4974 : : * Step to check jsestate->empty and return the ON EMPTY expression if
4975 : : * there is one.
4976 : : *
4977 : : * See the comment above for details on the optimization for NULL-valued
4978 : : * expressions.
4979 : : */
4980 [ + + ]: 1538 : if (jsexpr->on_empty != NULL &&
659 4981 [ + + ]: 1322 : jsexpr->on_empty->btype != JSON_BEHAVIOR_ERROR &&
4982 [ + + ]: 1278 : (!(IsA(jsexpr->on_empty->expr, Const) &&
4983 [ + + + + ]: 1242 : ((Const *) jsexpr->on_empty->expr)->constisnull) ||
4984 : : returning_domain))
4985 : : {
4986 : : ErrorSaveContext *saved_escontext;
4987 : :
662 4988 : 256 : jsestate->jump_empty = state->steps_len;
4989 : :
4990 : : /* JUMP to end if false, that is, skip the ON EMPTY expression. */
831 4991 : 256 : jumps_to_end = lappend_int(jumps_to_end, state->steps_len);
4992 : 256 : scratch->opcode = EEOP_JUMP_IF_NOT_TRUE;
4993 : 256 : scratch->resvalue = &jsestate->empty.value;
4994 : 256 : scratch->resnull = &jsestate->empty.isnull;
4995 : 256 : scratch->d.jump.jumpdone = -1; /* set below */
4996 : 256 : ExprEvalPushStep(state, scratch);
4997 : :
4998 : : /*
4999 : : * Steps to evaluate the ON EMPTY expression; handle errors softly to
5000 : : * rethrow them in COERCION_FINISH step that will be added later.
5001 : : */
704 5002 : 256 : saved_escontext = state->escontext;
5003 : 256 : state->escontext = escontext;
831 5004 : 256 : ExecInitExprRec((Expr *) jsexpr->on_empty->expr,
5005 : : state, resv, resnull);
704 5006 : 256 : state->escontext = saved_escontext;
5007 : :
5008 : : /* Step to coerce the ON EMPTY expression if needed */
831 5009 [ + + ]: 256 : if (jsexpr->on_empty->coerce)
732 5010 : 116 : ExecInitJsonCoercion(state, jsexpr->returning, escontext,
700 5011 : 116 : jsexpr->omit_quotes, false,
5012 : : resv, resnull);
5013 : :
5014 : : /*
5015 : : * Add a COERCION_FINISH step to check for errors that may occur when
5016 : : * coercing and rethrow them.
5017 : : */
704 5018 [ + + ]: 256 : if (jsexpr->on_empty->coerce ||
5019 [ + - ]: 140 : IsA(jsexpr->on_empty->expr, CoerceViaIO) ||
5020 [ + + ]: 140 : IsA(jsexpr->on_empty->expr, CoerceToDomain))
5021 : : {
5022 : :
5023 : 152 : scratch->opcode = EEOP_JSONEXPR_COERCION_FINISH;
5024 : 152 : scratch->resvalue = resv;
5025 : 152 : scratch->resnull = resnull;
5026 : 152 : scratch->d.jsonexpr.jsestate = jsestate;
5027 : 152 : ExprEvalPushStep(state, scratch);
5028 : : }
5029 : : }
5030 : :
831 5031 [ + + + + : 2586 : foreach(lc, jumps_to_end)
+ + ]
5032 : : {
5033 : 1048 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
5034 : :
5035 : 1048 : as->d.jump.jumpdone = state->steps_len;
5036 : : }
5037 : :
5038 : 1538 : jsestate->jump_end = state->steps_len;
5039 : 1538 : }
5040 : :
5041 : : /*
5042 : : * Initialize a EEOP_JSONEXPR_COERCION step to coerce the value given in resv
5043 : : * to the given RETURNING type.
5044 : : */
5045 : : static void
5046 : 824 : ExecInitJsonCoercion(ExprState *state, JsonReturning *returning,
5047 : : ErrorSaveContext *escontext, bool omit_quotes,
5048 : : bool exists_coerce,
5049 : : Datum *resv, bool *resnull)
5050 : : {
5051 : 824 : ExprEvalStep scratch = {0};
5052 : :
5053 : : /* For json_populate_type() */
5054 : 824 : scratch.opcode = EEOP_JSONEXPR_COERCION;
5055 : 824 : scratch.resvalue = resv;
5056 : 824 : scratch.resnull = resnull;
5057 : 824 : scratch.d.jsonexpr_coercion.targettype = returning->typid;
5058 : 824 : scratch.d.jsonexpr_coercion.targettypmod = returning->typmod;
700 5059 : 824 : scratch.d.jsonexpr_coercion.json_coercion_cache = NULL;
831 5060 : 824 : scratch.d.jsonexpr_coercion.escontext = escontext;
732 5061 : 824 : scratch.d.jsonexpr_coercion.omit_quotes = omit_quotes;
700 5062 : 824 : scratch.d.jsonexpr_coercion.exists_coerce = exists_coerce;
5063 [ + + + + ]: 904 : scratch.d.jsonexpr_coercion.exists_cast_to_int = exists_coerce &&
5064 : 80 : getBaseType(returning->typid) == INT4OID;
5065 [ + + + + ]: 904 : scratch.d.jsonexpr_coercion.exists_check_domain = exists_coerce &&
110 andrew@dunslane.net 5066 :GNC 80 : DomainHasConstraints(returning->typid, NULL);
831 amitlan@postgresql.o 5067 :CBC 824 : ExprEvalPushStep(state, &scratch);
5068 : 824 : }
|