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