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