| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* nodeSubplan.c |
| 4 |
* routines to support sub-selects appearing in expressions |
| 5 |
* |
| 6 |
* This module is concerned with executing SubPlan expression nodes, which |
| 7 |
* should not be confused with sub-SELECTs appearing in FROM. SubPlans are |
| 8 |
* divided into "initplans", which are those that need only one evaluation per |
| 9 |
* query (among other restrictions, this requires that they don't use any |
| 10 |
* direct correlation variables from the parent plan level), and "regular" |
| 11 |
* subplans, which are re-evaluated every time their result is required. |
| 12 |
* |
| 13 |
* |
| 14 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 15 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 16 |
* |
| 17 |
* IDENTIFICATION |
| 18 |
* src/backend/executor/nodeSubplan.c |
| 19 |
* |
| 20 |
*------------------------------------------------------------------------- |
| 21 |
*/ |
| 22 |
/* |
| 23 |
* INTERFACE ROUTINES |
| 24 |
* ExecSubPlan - process a subselect |
| 25 |
* ExecInitSubPlan - initialize a subselect |
| 26 |
*/ |
| 27 |
#include "postgres.h" |
| 28 |
|
| 29 |
#include <math.h> |
| 30 |
|
| 31 |
#include "access/htup_details.h" |
| 32 |
#include "executor/executor.h" |
| 33 |
#include "executor/nodeSubplan.h" |
| 34 |
#include "miscadmin.h" |
| 35 |
#include "nodes/makefuncs.h" |
| 36 |
#include "nodes/nodeFuncs.h" |
| 37 |
#include "optimizer/optimizer.h" |
| 38 |
#include "utils/array.h" |
| 39 |
#include "utils/lsyscache.h" |
| 40 |
#include "utils/memutils.h" |
| 41 |
|
| 42 |
static Datum ExecHashSubPlan(SubPlanState *node, |
| 43 |
ExprContext *econtext, |
| 44 |
bool *isNull); |
| 45 |
static Datum ExecScanSubPlan(SubPlanState *node, |
| 46 |
ExprContext *econtext, |
| 47 |
bool *isNull); |
| 48 |
static void buildSubPlanHash(SubPlanState *node, ExprContext *econtext); |
| 49 |
static bool findPartialMatch(TupleHashTable hashtable, TupleTableSlot *slot, |
| 50 |
FmgrInfo *eqfunctions); |
| 51 |
static bool slotAllNulls(TupleTableSlot *slot); |
| 52 |
static bool slotNoNulls(TupleTableSlot *slot); |
| 53 |
|
| 54 |
|
| 55 |
/* ---------------------------------------------------------------- |
| 56 |
* ExecSubPlan |
| 57 |
* |
| 58 |
* This is the main entry point for execution of a regular SubPlan. |
| 59 |
* ---------------------------------------------------------------- |
| 60 |
*/ |
| 61 |
Datum |
| 62 |
ExecSubPlan(SubPlanState *node, |
| 63 |
ExprContext *econtext, |
| 64 |
bool *isNull) |
| 65 |
{ |
| 66 |
SubPlan *subplan = node->subplan; |
| 67 |
EState *estate = node->planstate->state; |
| 68 |
ScanDirection dir = estate->es_direction; |
| 69 |
Datum retval; |
| 70 |
|
| 71 |
CHECK_FOR_INTERRUPTS(); |
| 72 |
|
| 73 |
/* Set non-null as default */ |
| 74 |
*isNull = false; |
| 75 |
|
| 76 |
/* Sanity checks */ |
| 77 |
if (subplan->subLinkType == CTE_SUBLINK) |
| 78 |
elog(ERROR, "CTE subplans should not be executed via ExecSubPlan"); |
| 79 |
if (subplan->setParam != NIL && subplan->subLinkType != MULTIEXPR_SUBLINK) |
| 80 |
elog(ERROR, "cannot set parent params from subquery"); |
| 81 |
|
| 82 |
/* Force forward-scan mode for evaluation */ |
| 83 |
estate->es_direction = ForwardScanDirection; |
| 84 |
|
| 85 |
/* Select appropriate evaluation strategy */ |
| 86 |
if (subplan->useHashTable) |
| 87 |
retval = ExecHashSubPlan(node, econtext, isNull); |
| 88 |
else |
| 89 |
retval = ExecScanSubPlan(node, econtext, isNull); |
| 90 |
|
| 91 |
/* restore scan direction */ |
| 92 |
estate->es_direction = dir; |
| 93 |
|
| 94 |
return retval; |
| 95 |
} |
| 96 |
|
| 97 |
/* |
| 98 |
* ExecHashSubPlan: store subselect result in an in-memory hash table |
| 99 |
*/ |
| 100 |
static Datum |
| 101 |
ExecHashSubPlan(SubPlanState *node, |
| 102 |
ExprContext *econtext, |
| 103 |
bool *isNull) |
| 104 |
{ |
| 105 |
bool result = false; |
| 106 |
SubPlan *subplan = node->subplan; |
| 107 |
PlanState *planstate = node->planstate; |
| 108 |
TupleTableSlot *slot; |
| 109 |
|
| 110 |
/* Shouldn't have any direct correlation Vars */ |
| 111 |
if (subplan->parParam != NIL || subplan->args != NIL) |
| 112 |
elog(ERROR, "hashed subplan with direct correlation not supported"); |
| 113 |
|
| 114 |
/* |
| 115 |
* If first time through or we need to rescan the subplan, build the hash |
| 116 |
* table. |
| 117 |
*/ |
| 118 |
if (node->hashtable == NULL || planstate->chgParam != NULL) |
| 119 |
buildSubPlanHash(node, econtext); |
| 120 |
|
| 121 |
/* |
| 122 |
* The result for an empty subplan is always FALSE; no need to evaluate |
| 123 |
* lefthand side. |
| 124 |
*/ |
| 125 |
*isNull = false; |
| 126 |
if (!node->havehashrows && !node->havenullrows) |
| 127 |
return BoolGetDatum(false); |
| 128 |
|
| 129 |
/* |
| 130 |
* Evaluate lefthand expressions and form a projection tuple. First we |
| 131 |
* have to set the econtext to use (hack alert!). |
| 132 |
*/ |
| 133 |
node->projLeft->pi_exprContext = econtext; |
| 134 |
slot = ExecProject(node->projLeft); |
| 135 |
|
| 136 |
/* |
| 137 |
* If the LHS is all non-null, probe for an exact match in the main hash |
| 138 |
* table. If we find one, the result is TRUE. Otherwise, scan the |
| 139 |
* partly-null table to see if there are any rows that aren't provably |
| 140 |
* unequal to the LHS; if so, the result is UNKNOWN. (We skip that part |
| 141 |
* if we don't care about UNKNOWN.) Otherwise, the result is FALSE. |
| 142 |
* |
| 143 |
* Note: the reason we can avoid a full scan of the main hash table is |
| 144 |
* that the combining operators are assumed never to yield NULL when both |
| 145 |
* inputs are non-null. If they were to do so, we might need to produce |
| 146 |
* UNKNOWN instead of FALSE because of an UNKNOWN result in comparing the |
| 147 |
* LHS to some main-table entry --- which is a comparison we will not even |
| 148 |
* make, unless there's a chance match of hash keys. |
| 149 |
*/ |
| 150 |
if (slotNoNulls(slot)) |
| 151 |
{ |
| 152 |
if (node->havehashrows && |
| 153 |
FindTupleHashEntry(node->hashtable, |
| 154 |
slot, |
| 155 |
node->cur_eq_comp, |
| 156 |
node->lhs_hash_expr) != NULL) |
| 157 |
result = true; |
| 158 |
else if (node->havenullrows && |
| 159 |
findPartialMatch(node->hashnulls, slot, node->cur_eq_funcs)) |
| 160 |
*isNull = true; |
| 161 |
} |
| 162 |
|
| 163 |
/* |
| 164 |
* When the LHS is partly or wholly NULL, we can never return TRUE. If we |
| 165 |
* don't care about UNKNOWN, just return FALSE. Otherwise, if the LHS is |
| 166 |
* wholly NULL, immediately return UNKNOWN. (Since the combining |
| 167 |
* operators are strict, the result could only be FALSE if the sub-select |
| 168 |
* were empty, but we already handled that case.) Otherwise, we must scan |
| 169 |
* both the main and partly-null tables to see if there are any rows that |
| 170 |
* aren't provably unequal to the LHS; if so, the result is UNKNOWN. |
| 171 |
* Otherwise, the result is FALSE. |
| 172 |
*/ |
| 173 |
else if (node->hashnulls == NULL) |
| 174 |
/* just return FALSE */ ; |
| 175 |
else if (slotAllNulls(slot)) |
| 176 |
*isNull = true; |
| 177 |
/* Scan partly-null table first, since more likely to get a match */ |
| 178 |
else if (node->havenullrows && |
| 179 |
findPartialMatch(node->hashnulls, slot, node->cur_eq_funcs)) |
| 180 |
*isNull = true; |
| 181 |
else if (node->havehashrows && |
| 182 |
findPartialMatch(node->hashtable, slot, node->cur_eq_funcs)) |
| 183 |
*isNull = true; |
| 184 |
|
| 185 |
/* |
| 186 |
* Note: because we are typically called in a per-tuple context, we have |
| 187 |
* to explicitly clear the projected tuple before returning. Otherwise, |
| 188 |
* we'll have a double-free situation: the per-tuple context will probably |
| 189 |
* be reset before we're called again, and then the tuple slot will think |
| 190 |
* it still needs to free the tuple. |
| 191 |
*/ |
| 192 |
ExecClearTuple(slot); |
| 193 |
|
| 194 |
/* Also must reset the hashtempcxt after each hashtable lookup. */ |
| 195 |
MemoryContextReset(node->hashtempcxt); |
| 196 |
|
| 197 |
return BoolGetDatum(result); |
| 198 |
} |
| 199 |
|
| 200 |
/* |
| 201 |
* ExecScanSubPlan: default case where we have to rescan subplan each time |
| 202 |
*/ |
| 203 |
static Datum |
| 204 |
ExecScanSubPlan(SubPlanState *node, |
| 205 |
ExprContext *econtext, |
| 206 |
bool *isNull) |
| 207 |
{ |
| 208 |
SubPlan *subplan = node->subplan; |
| 209 |
PlanState *planstate = node->planstate; |
| 210 |
SubLinkType subLinkType = subplan->subLinkType; |
| 211 |
MemoryContext oldcontext; |
| 212 |
TupleTableSlot *slot; |
| 213 |
Datum result; |
| 214 |
bool found = false; /* true if got at least one subplan tuple */ |
| 215 |
ListCell *l; |
| 216 |
ArrayBuildStateAny *astate = NULL; |
| 217 |
|
| 218 |
/* Initialize ArrayBuildStateAny in caller's context, if needed */ |
| 219 |
if (subLinkType == ARRAY_SUBLINK) |
| 220 |
astate = initArrayResultAny(subplan->firstColType, |
| 221 |
CurrentMemoryContext, true); |
| 222 |
|
| 223 |
/* |
| 224 |
* We are probably in a short-lived expression-evaluation context. Switch |
| 225 |
* to the per-query context for manipulating the child plan's chgParam, |
| 226 |
* calling ExecProcNode on it, etc. |
| 227 |
*/ |
| 228 |
oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory); |
| 229 |
|
| 230 |
/* |
| 231 |
* We rely on the caller to evaluate plan correlation values, if |
| 232 |
* necessary. However we still need to record the fact that the values |
| 233 |
* (might have) changed, otherwise the ExecReScan() below won't know that |
| 234 |
* nodes need to be rescanned. |
| 235 |
*/ |
| 236 |
foreach(l, subplan->parParam) |
| 237 |
{ |
| 238 |
int paramid = lfirst_int(l); |
| 239 |
|
| 240 |
planstate->chgParam = bms_add_member(planstate->chgParam, paramid); |
| 241 |
} |
| 242 |
|
| 243 |
/* with that done, we can reset the subplan */ |
| 244 |
ExecReScan(planstate); |
| 245 |
|
| 246 |
/* |
| 247 |
* For all sublink types except EXPR_SUBLINK and ARRAY_SUBLINK, the result |
| 248 |
* is boolean as are the results of the combining operators. We combine |
| 249 |
* results across tuples (if the subplan produces more than one) using OR |
| 250 |
* semantics for ANY_SUBLINK or AND semantics for ALL_SUBLINK. |
| 251 |
* (ROWCOMPARE_SUBLINK doesn't allow multiple tuples from the subplan.) |
| 252 |
* NULL results from the combining operators are handled according to the |
| 253 |
* usual SQL semantics for OR and AND. The result for no input tuples is |
| 254 |
* FALSE for ANY_SUBLINK, TRUE for ALL_SUBLINK, NULL for |
| 255 |
* ROWCOMPARE_SUBLINK. |
| 256 |
* |
| 257 |
* For EXPR_SUBLINK we require the subplan to produce no more than one |
| 258 |
* tuple, else an error is raised. If zero tuples are produced, we return |
| 259 |
* NULL. Assuming we get a tuple, we just use its first column (there can |
| 260 |
* be only one non-junk column in this case). |
| 261 |
* |
| 262 |
* For MULTIEXPR_SUBLINK, we push the per-column subplan outputs out to |
| 263 |
* the setParams and then return a dummy false value. There must not be |
| 264 |
* multiple tuples returned from the subplan; if zero tuples are produced, |
| 265 |
* set the setParams to NULL. |
| 266 |
* |
| 267 |
* For ARRAY_SUBLINK we allow the subplan to produce any number of tuples, |
| 268 |
* and form an array of the first column's values. Note in particular |
| 269 |
* that we produce a zero-element array if no tuples are produced (this is |
| 270 |
* a change from pre-8.3 behavior of returning NULL). |
| 271 |
*/ |
| 272 |
result = BoolGetDatum(subLinkType == ALL_SUBLINK); |
| 273 |
*isNull = false; |
| 274 |
|
| 275 |
for (slot = ExecProcNode(planstate); |
| 276 |
!TupIsNull(slot); |
| 277 |
slot = ExecProcNode(planstate)) |
| 278 |
{ |
| 279 |
TupleDesc tdesc = slot->tts_tupleDescriptor; |
| 280 |
Datum rowresult; |
| 281 |
bool rownull; |
| 282 |
int col; |
| 283 |
ListCell *plst; |
| 284 |
|
| 285 |
if (subLinkType == EXISTS_SUBLINK) |
| 286 |
{ |
| 287 |
found = true; |
| 288 |
result = BoolGetDatum(true); |
| 289 |
break; |
| 290 |
} |
| 291 |
|
| 292 |
if (subLinkType == EXPR_SUBLINK) |
| 293 |
{ |
| 294 |
/* cannot allow multiple input tuples for EXPR sublink */ |
| 295 |
if (found) |
| 296 |
ereport(ERROR, |
| 297 |
(errcode(ERRCODE_CARDINALITY_VIOLATION), |
| 298 |
errmsg("more than one row returned by a subquery used as an expression"))); |
| 299 |
found = true; |
| 300 |
|
| 301 |
/* |
| 302 |
* We need to copy the subplan's tuple in case the result is of |
| 303 |
* pass-by-ref type --- our return value will point into this |
| 304 |
* copied tuple! Can't use the subplan's instance of the tuple |
| 305 |
* since it won't still be valid after next ExecProcNode() call. |
| 306 |
* node->curTuple keeps track of the copied tuple for eventual |
| 307 |
* freeing. |
| 308 |
*/ |
| 309 |
if (node->curTuple) |
| 310 |
heap_freetuple(node->curTuple); |
| 311 |
node->curTuple = ExecCopySlotHeapTuple(slot); |
| 312 |
|
| 313 |
result = heap_getattr(node->curTuple, 1, tdesc, isNull); |
| 314 |
/* keep scanning subplan to make sure there's only one tuple */ |
| 315 |
continue; |
| 316 |
} |
| 317 |
|
| 318 |
if (subLinkType == MULTIEXPR_SUBLINK) |
| 319 |
{ |
| 320 |
/* cannot allow multiple input tuples for MULTIEXPR sublink */ |
| 321 |
if (found) |
| 322 |
ereport(ERROR, |
| 323 |
(errcode(ERRCODE_CARDINALITY_VIOLATION), |
| 324 |
errmsg("more than one row returned by a subquery used as an expression"))); |
| 325 |
found = true; |
| 326 |
|
| 327 |
/* |
| 328 |
* We need to copy the subplan's tuple in case any result is of |
| 329 |
* pass-by-ref type --- our output values will point into this |
| 330 |
* copied tuple! Can't use the subplan's instance of the tuple |
| 331 |
* since it won't still be valid after next ExecProcNode() call. |
| 332 |
* node->curTuple keeps track of the copied tuple for eventual |
| 333 |
* freeing. |
| 334 |
*/ |
| 335 |
if (node->curTuple) |
| 336 |
heap_freetuple(node->curTuple); |
| 337 |
node->curTuple = ExecCopySlotHeapTuple(slot); |
| 338 |
|
| 339 |
/* |
| 340 |
* Now set all the setParam params from the columns of the tuple |
| 341 |
*/ |
| 342 |
col = 1; |
| 343 |
foreach(plst, subplan->setParam) |
| 344 |
{ |
| 345 |
int paramid = lfirst_int(plst); |
| 346 |
ParamExecData *prmdata; |
| 347 |
|
| 348 |
prmdata = &(econtext->ecxt_param_exec_vals[paramid]); |
| 349 |
Assert(prmdata->execPlan == NULL); |
| 350 |
prmdata->value = heap_getattr(node->curTuple, col, tdesc, |
| 351 |
&(prmdata->isnull)); |
| 352 |
col++; |
| 353 |
} |
| 354 |
|
| 355 |
/* keep scanning subplan to make sure there's only one tuple */ |
| 356 |
continue; |
| 357 |
} |
| 358 |
|
| 359 |
if (subLinkType == ARRAY_SUBLINK) |
| 360 |
{ |
| 361 |
Datum dvalue; |
| 362 |
bool disnull; |
| 363 |
|
| 364 |
found = true; |
| 365 |
/* stash away current value */ |
| 366 |
Assert(subplan->firstColType == TupleDescAttr(tdesc, 0)->atttypid); |
| 367 |
dvalue = slot_getattr(slot, 1, &disnull); |
| 368 |
astate = accumArrayResultAny(astate, dvalue, disnull, |
| 369 |
subplan->firstColType, oldcontext); |
| 370 |
/* keep scanning subplan to collect all values */ |
| 371 |
continue; |
| 372 |
} |
| 373 |
|
| 374 |
/* cannot allow multiple input tuples for ROWCOMPARE sublink either */ |
| 375 |
if (subLinkType == ROWCOMPARE_SUBLINK && found) |
| 376 |
ereport(ERROR, |
| 377 |
(errcode(ERRCODE_CARDINALITY_VIOLATION), |
| 378 |
errmsg("more than one row returned by a subquery used as an expression"))); |
| 379 |
|
| 380 |
found = true; |
| 381 |
|
| 382 |
/* |
| 383 |
* For ALL, ANY, and ROWCOMPARE sublinks, load up the Params |
| 384 |
* representing the columns of the sub-select, and then evaluate the |
| 385 |
* combining expression. |
| 386 |
*/ |
| 387 |
col = 1; |
| 388 |
foreach(plst, subplan->paramIds) |
| 389 |
{ |
| 390 |
int paramid = lfirst_int(plst); |
| 391 |
ParamExecData *prmdata; |
| 392 |
|
| 393 |
prmdata = &(econtext->ecxt_param_exec_vals[paramid]); |
| 394 |
Assert(prmdata->execPlan == NULL); |
| 395 |
prmdata->value = slot_getattr(slot, col, &(prmdata->isnull)); |
| 396 |
col++; |
| 397 |
} |
| 398 |
|
| 399 |
rowresult = ExecEvalExprSwitchContext(node->testexpr, econtext, |
| 400 |
&rownull); |
| 401 |
|
| 402 |
if (subLinkType == ANY_SUBLINK) |
| 403 |
{ |
| 404 |
/* combine across rows per OR semantics */ |
| 405 |
if (rownull) |
| 406 |
*isNull = true; |
| 407 |
else if (DatumGetBool(rowresult)) |
| 408 |
{ |
| 409 |
result = BoolGetDatum(true); |
| 410 |
*isNull = false; |
| 411 |
break; /* needn't look at any more rows */ |
| 412 |
} |
| 413 |
} |
| 414 |
else if (subLinkType == ALL_SUBLINK) |
| 415 |
{ |
| 416 |
/* combine across rows per AND semantics */ |
| 417 |
if (rownull) |
| 418 |
*isNull = true; |
| 419 |
else if (!DatumGetBool(rowresult)) |
| 420 |
{ |
| 421 |
result = BoolGetDatum(false); |
| 422 |
*isNull = false; |
| 423 |
break; /* needn't look at any more rows */ |
| 424 |
} |
| 425 |
} |
| 426 |
else |
| 427 |
{ |
| 428 |
/* must be ROWCOMPARE_SUBLINK */ |
| 429 |
result = rowresult; |
| 430 |
*isNull = rownull; |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
MemoryContextSwitchTo(oldcontext); |
| 435 |
|
| 436 |
if (subLinkType == ARRAY_SUBLINK) |
| 437 |
{ |
| 438 |
/* We return the result in the caller's context */ |
| 439 |
result = makeArrayResultAny(astate, oldcontext, true); |
| 440 |
} |
| 441 |
else if (!found) |
| 442 |
{ |
| 443 |
/* |
| 444 |
* deal with empty subplan result. result/isNull were previously |
| 445 |
* initialized correctly for all sublink types except EXPR and |
| 446 |
* ROWCOMPARE; for those, return NULL. |
| 447 |
*/ |
| 448 |
if (subLinkType == EXPR_SUBLINK || |
| 449 |
subLinkType == ROWCOMPARE_SUBLINK) |
| 450 |
{ |
| 451 |
result = (Datum) 0; |
| 452 |
*isNull = true; |
| 453 |
} |
| 454 |
else if (subLinkType == MULTIEXPR_SUBLINK) |
| 455 |
{ |
| 456 |
/* We don't care about function result, but set the setParams */ |
| 457 |
foreach(l, subplan->setParam) |
| 458 |
{ |
| 459 |
int paramid = lfirst_int(l); |
| 460 |
ParamExecData *prmdata; |
| 461 |
|
| 462 |
prmdata = &(econtext->ecxt_param_exec_vals[paramid]); |
| 463 |
Assert(prmdata->execPlan == NULL); |
| 464 |
prmdata->value = (Datum) 0; |
| 465 |
prmdata->isnull = true; |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
return result; |
| 471 |
} |
| 472 |
|
| 473 |
/* |
| 474 |
* buildSubPlanHash: load hash table by scanning subplan output. |
| 475 |
*/ |
| 476 |
static void |
| 477 |
buildSubPlanHash(SubPlanState *node, ExprContext *econtext) |
| 478 |
{ |
| 479 |
SubPlan *subplan = node->subplan; |
| 480 |
PlanState *planstate = node->planstate; |
| 481 |
int ncols = node->numCols; |
| 482 |
ExprContext *innerecontext = node->innerecontext; |
| 483 |
MemoryContext oldcontext; |
| 484 |
long nbuckets; |
| 485 |
TupleTableSlot *slot; |
| 486 |
|
| 487 |
Assert(subplan->subLinkType == ANY_SUBLINK); |
| 488 |
|
| 489 |
/* |
| 490 |
* If we already had any hash tables, reset 'em; otherwise create empty |
| 491 |
* hash table(s). |
| 492 |
* |
| 493 |
* If we need to distinguish accurately between FALSE and UNKNOWN (i.e., |
| 494 |
* NULL) results of the IN operation, then we have to store subplan output |
| 495 |
* rows that are partly or wholly NULL. We store such rows in a separate |
| 496 |
* hash table that we expect will be much smaller than the main table. (We |
| 497 |
* can use hashing to eliminate partly-null rows that are not distinct. We |
| 498 |
* keep them separate to minimize the cost of the inevitable full-table |
| 499 |
* searches; see findPartialMatch.) |
| 500 |
* |
| 501 |
* If it's not necessary to distinguish FALSE and UNKNOWN, then we don't |
| 502 |
* need to store subplan output rows that contain NULL. |
| 503 |
* |
| 504 |
* Because the input slot for each hash table is always the slot resulting |
| 505 |
* from an ExecProject(), we can use TTSOpsVirtual for the input ops. This |
| 506 |
* saves a needless fetch inner op step for the hashing ExprState created |
| 507 |
* in BuildTupleHashTable(). |
| 508 |
*/ |
| 509 |
MemoryContextReset(node->hashtablecxt); |
| 510 |
node->havehashrows = false; |
| 511 |
node->havenullrows = false; |
| 512 |
|
| 513 |
nbuckets = clamp_cardinality_to_long(planstate->plan->plan_rows); |
| 514 |
if (nbuckets < 1) |
| 515 |
nbuckets = 1; |
| 516 |
|
| 517 |
if (node->hashtable) |
| 518 |
ResetTupleHashTable(node->hashtable); |
| 519 |
else |
| 520 |
node->hashtable = BuildTupleHashTable(node->parent, |
| 521 |
node->descRight, |
| 522 |
&TTSOpsVirtual, |
| 523 |
ncols, |
| 524 |
node->keyColIdx, |
| 525 |
node->tab_eq_funcoids, |
| 526 |
node->tab_hash_funcs, |
| 527 |
node->tab_collations, |
| 528 |
nbuckets, |
| 529 |
0, |
| 530 |
node->planstate->state->es_query_cxt, |
| 531 |
node->hashtablecxt, |
| 532 |
node->hashtempcxt, |
| 533 |
false); |
| 534 |
|
| 535 |
if (!subplan->unknownEqFalse) |
| 536 |
{ |
| 537 |
if (ncols == 1) |
| 538 |
nbuckets = 1; /* there can only be one entry */ |
| 539 |
else |
| 540 |
{ |
| 541 |
nbuckets /= 16; |
| 542 |
if (nbuckets < 1) |
| 543 |
nbuckets = 1; |
| 544 |
} |
| 545 |
|
| 546 |
if (node->hashnulls) |
| 547 |
ResetTupleHashTable(node->hashnulls); |
| 548 |
else |
| 549 |
node->hashnulls = BuildTupleHashTable(node->parent, |
| 550 |
node->descRight, |
| 551 |
&TTSOpsVirtual, |
| 552 |
ncols, |
| 553 |
node->keyColIdx, |
| 554 |
node->tab_eq_funcoids, |
| 555 |
node->tab_hash_funcs, |
| 556 |
node->tab_collations, |
| 557 |
nbuckets, |
| 558 |
0, |
| 559 |
node->planstate->state->es_query_cxt, |
| 560 |
node->hashtablecxt, |
| 561 |
node->hashtempcxt, |
| 562 |
false); |
| 563 |
} |
| 564 |
else |
| 565 |
node->hashnulls = NULL; |
| 566 |
|
| 567 |
/* |
| 568 |
* We are probably in a short-lived expression-evaluation context. Switch |
| 569 |
* to the per-query context for manipulating the child plan. |
| 570 |
*/ |
| 571 |
oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory); |
| 572 |
|
| 573 |
/* |
| 574 |
* Reset subplan to start. |
| 575 |
*/ |
| 576 |
ExecReScan(planstate); |
| 577 |
|
| 578 |
/* |
| 579 |
* Scan the subplan and load the hash table(s). Note that when there are |
| 580 |
* duplicate rows coming out of the sub-select, only one copy is stored. |
| 581 |
*/ |
| 582 |
for (slot = ExecProcNode(planstate); |
| 583 |
!TupIsNull(slot); |
| 584 |
slot = ExecProcNode(planstate)) |
| 585 |
{ |
| 586 |
int col = 1; |
| 587 |
ListCell *plst; |
| 588 |
bool isnew; |
| 589 |
|
| 590 |
/* |
| 591 |
* Load up the Params representing the raw sub-select outputs, then |
| 592 |
* form the projection tuple to store in the hashtable. |
| 593 |
*/ |
| 594 |
foreach(plst, subplan->paramIds) |
| 595 |
{ |
| 596 |
int paramid = lfirst_int(plst); |
| 597 |
ParamExecData *prmdata; |
| 598 |
|
| 599 |
prmdata = &(innerecontext->ecxt_param_exec_vals[paramid]); |
| 600 |
Assert(prmdata->execPlan == NULL); |
| 601 |
prmdata->value = slot_getattr(slot, col, |
| 602 |
&(prmdata->isnull)); |
| 603 |
col++; |
| 604 |
} |
| 605 |
slot = ExecProject(node->projRight); |
| 606 |
|
| 607 |
/* |
| 608 |
* If result contains any nulls, store separately or not at all. |
| 609 |
*/ |
| 610 |
if (slotNoNulls(slot)) |
| 611 |
{ |
| 612 |
(void) LookupTupleHashEntry(node->hashtable, slot, &isnew, NULL); |
| 613 |
node->havehashrows = true; |
| 614 |
} |
| 615 |
else if (node->hashnulls) |
| 616 |
{ |
| 617 |
(void) LookupTupleHashEntry(node->hashnulls, slot, &isnew, NULL); |
| 618 |
node->havenullrows = true; |
| 619 |
} |
| 620 |
|
| 621 |
/* |
| 622 |
* Reset innerecontext after each inner tuple to free any memory used |
| 623 |
* during ExecProject. |
| 624 |
*/ |
| 625 |
ResetExprContext(innerecontext); |
| 626 |
|
| 627 |
/* Also must reset the hashtempcxt after each hashtable lookup. */ |
| 628 |
MemoryContextReset(node->hashtempcxt); |
| 629 |
} |
| 630 |
|
| 631 |
/* |
| 632 |
* Since the projected tuples are in the sub-query's context and not the |
| 633 |
* main context, we'd better clear the tuple slot before there's any |
| 634 |
* chance of a reset of the sub-query's context. Else we will have the |
| 635 |
* potential for a double free attempt. (XXX possibly no longer needed, |
| 636 |
* but can't hurt.) |
| 637 |
*/ |
| 638 |
ExecClearTuple(node->projRight->pi_state.resultslot); |
| 639 |
|
| 640 |
MemoryContextSwitchTo(oldcontext); |
| 641 |
} |
| 642 |
|
| 643 |
/* |
| 644 |
* execTuplesUnequal |
| 645 |
* Return true if two tuples are definitely unequal in the indicated |
| 646 |
* fields. |
| 647 |
* |
| 648 |
* Nulls are neither equal nor unequal to anything else. A true result |
| 649 |
* is obtained only if there are non-null fields that compare not-equal. |
| 650 |
* |
| 651 |
* slot1, slot2: the tuples to compare (must have same columns!) |
| 652 |
* numCols: the number of attributes to be examined |
| 653 |
* matchColIdx: array of attribute column numbers |
| 654 |
* eqFunctions: array of fmgr lookup info for the equality functions to use |
| 655 |
* evalContext: short-term memory context for executing the functions |
| 656 |
*/ |
| 657 |
static bool |
| 658 |
execTuplesUnequal(TupleTableSlot *slot1, |
| 659 |
TupleTableSlot *slot2, |
| 660 |
int numCols, |
| 661 |
AttrNumber *matchColIdx, |
| 662 |
FmgrInfo *eqfunctions, |
| 663 |
const Oid *collations, |
| 664 |
MemoryContext evalContext) |
| 665 |
{ |
| 666 |
MemoryContext oldContext; |
| 667 |
bool result; |
| 668 |
int i; |
| 669 |
|
| 670 |
/* Reset and switch into the temp context. */ |
| 671 |
MemoryContextReset(evalContext); |
| 672 |
oldContext = MemoryContextSwitchTo(evalContext); |
| 673 |
|
| 674 |
/* |
| 675 |
* We cannot report a match without checking all the fields, but we can |
| 676 |
* report a non-match as soon as we find unequal fields. So, start |
| 677 |
* comparing at the last field (least significant sort key). That's the |
| 678 |
* most likely to be different if we are dealing with sorted input. |
| 679 |
*/ |
| 680 |
result = false; |
| 681 |
|
| 682 |
for (i = numCols; --i >= 0;) |
| 683 |
{ |
| 684 |
AttrNumber att = matchColIdx[i]; |
| 685 |
Datum attr1, |
| 686 |
attr2; |
| 687 |
bool isNull1, |
| 688 |
isNull2; |
| 689 |
|
| 690 |
attr1 = slot_getattr(slot1, att, &isNull1); |
| 691 |
|
| 692 |
if (isNull1) |
| 693 |
continue; /* can't prove anything here */ |
| 694 |
|
| 695 |
attr2 = slot_getattr(slot2, att, &isNull2); |
| 696 |
|
| 697 |
if (isNull2) |
| 698 |
continue; /* can't prove anything here */ |
| 699 |
|
| 700 |
/* Apply the type-specific equality function */ |
| 701 |
if (!DatumGetBool(FunctionCall2Coll(&eqfunctions[i], |
| 702 |
collations[i], |
| 703 |
attr1, attr2))) |
| 704 |
{ |
| 705 |
result = true; /* they are unequal */ |
| 706 |
break; |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
MemoryContextSwitchTo(oldContext); |
| 711 |
|
| 712 |
return result; |
| 713 |
} |
| 714 |
|
| 715 |
/* |
| 716 |
* findPartialMatch: does the hashtable contain an entry that is not |
| 717 |
* provably distinct from the tuple? |
| 718 |
* |
| 719 |
* We have to scan the whole hashtable; we can't usefully use hashkeys |
| 720 |
* to guide probing, since we might get partial matches on tuples with |
| 721 |
* hashkeys quite unrelated to what we'd get from the given tuple. |
| 722 |
* |
| 723 |
* Caller must provide the equality functions to use, since in cross-type |
| 724 |
* cases these are different from the hashtable's internal functions. |
| 725 |
*/ |
| 726 |
static bool |
| 727 |
findPartialMatch(TupleHashTable hashtable, TupleTableSlot *slot, |
| 728 |
FmgrInfo *eqfunctions) |
| 729 |
{ |
| 730 |
int numCols = hashtable->numCols; |
| 731 |
AttrNumber *keyColIdx = hashtable->keyColIdx; |
| 732 |
TupleHashIterator hashiter; |
| 733 |
TupleHashEntry entry; |
| 734 |
|
| 735 |
InitTupleHashIterator(hashtable, &hashiter); |
| 736 |
while ((entry = ScanTupleHashTable(hashtable, &hashiter)) != NULL) |
| 737 |
{ |
| 738 |
CHECK_FOR_INTERRUPTS(); |
| 739 |
|
| 740 |
ExecStoreMinimalTuple(TupleHashEntryGetTuple(entry), hashtable->tableslot, false); |
| 741 |
if (!execTuplesUnequal(slot, hashtable->tableslot, |
| 742 |
numCols, keyColIdx, |
| 743 |
eqfunctions, |
| 744 |
hashtable->tab_collations, |
| 745 |
hashtable->tempcxt)) |
| 746 |
{ |
| 747 |
TermTupleHashIterator(&hashiter); |
| 748 |
return true; |
| 749 |
} |
| 750 |
} |
| 751 |
/* No TermTupleHashIterator call needed here */ |
| 752 |
return false; |
| 753 |
} |
| 754 |
|
| 755 |
/* |
| 756 |
* slotAllNulls: is the slot completely NULL? |
| 757 |
* |
| 758 |
* This does not test for dropped columns, which is OK because we only |
| 759 |
* use it on projected tuples. |
| 760 |
*/ |
| 761 |
static bool |
| 762 |
slotAllNulls(TupleTableSlot *slot) |
| 763 |
{ |
| 764 |
int ncols = slot->tts_tupleDescriptor->natts; |
| 765 |
int i; |
| 766 |
|
| 767 |
for (i = 1; i <= ncols; i++) |
| 768 |
{ |
| 769 |
if (!slot_attisnull(slot, i)) |
| 770 |
return false; |
| 771 |
} |
| 772 |
return true; |
| 773 |
} |
| 774 |
|
| 775 |
/* |
| 776 |
* slotNoNulls: is the slot entirely not NULL? |
| 777 |
* |
| 778 |
* This does not test for dropped columns, which is OK because we only |
| 779 |
* use it on projected tuples. |
| 780 |
*/ |
| 781 |
static bool |
| 782 |
slotNoNulls(TupleTableSlot *slot) |
| 783 |
{ |
| 784 |
int ncols = slot->tts_tupleDescriptor->natts; |
| 785 |
int i; |
| 786 |
|
| 787 |
for (i = 1; i <= ncols; i++) |
| 788 |
{ |
| 789 |
if (slot_attisnull(slot, i)) |
| 790 |
return false; |
| 791 |
} |
| 792 |
return true; |
| 793 |
} |
| 794 |
|
| 795 |
/* ---------------------------------------------------------------- |
| 796 |
* ExecInitSubPlan |
| 797 |
* |
| 798 |
* Create a SubPlanState for a SubPlan; this is the SubPlan-specific part |
| 799 |
* of ExecInitExpr(). We split it out so that it can be used for InitPlans |
| 800 |
* as well as regular SubPlans. Note that we don't link the SubPlan into |
| 801 |
* the parent's subPlan list, because that shouldn't happen for InitPlans. |
| 802 |
* Instead, ExecInitExpr() does that one part. |
| 803 |
* |
| 804 |
* We also rely on ExecInitExpr(), more precisely ExecInitSubPlanExpr(), to |
| 805 |
* evaluate input parameters, as that allows them to be evaluated as part of |
| 806 |
* the expression referencing the SubPlan. |
| 807 |
* ---------------------------------------------------------------- |
| 808 |
*/ |
| 809 |
SubPlanState * |
| 810 |
ExecInitSubPlan(SubPlan *subplan, PlanState *parent) |
| 811 |
{ |
| 812 |
SubPlanState *sstate = makeNode(SubPlanState); |
| 813 |
EState *estate = parent->state; |
| 814 |
|
| 815 |
sstate->subplan = subplan; |
| 816 |
|
| 817 |
/* Link the SubPlanState to already-initialized subplan */ |
| 818 |
sstate->planstate = (PlanState *) list_nth(estate->es_subplanstates, |
| 819 |
subplan->plan_id - 1); |
| 820 |
|
| 821 |
/* |
| 822 |
* This check can fail if the planner mistakenly puts a parallel-unsafe |
| 823 |
* subplan into a parallelized subquery; see ExecSerializePlan. |
| 824 |
*/ |
| 825 |
if (sstate->planstate == NULL) |
| 826 |
elog(ERROR, "subplan \"%s\" was not initialized", |
| 827 |
subplan->plan_name); |
| 828 |
|
| 829 |
/* Link to parent's state, too */ |
| 830 |
sstate->parent = parent; |
| 831 |
|
| 832 |
/* Initialize subexpressions */ |
| 833 |
sstate->testexpr = ExecInitExpr((Expr *) subplan->testexpr, parent); |
| 834 |
|
| 835 |
/* |
| 836 |
* initialize my state |
| 837 |
*/ |
| 838 |
sstate->curTuple = NULL; |
| 839 |
sstate->curArray = PointerGetDatum(NULL); |
| 840 |
sstate->projLeft = NULL; |
| 841 |
sstate->projRight = NULL; |
| 842 |
sstate->hashtable = NULL; |
| 843 |
sstate->hashnulls = NULL; |
| 844 |
sstate->hashtablecxt = NULL; |
| 845 |
sstate->hashtempcxt = NULL; |
| 846 |
sstate->innerecontext = NULL; |
| 847 |
sstate->keyColIdx = NULL; |
| 848 |
sstate->tab_eq_funcoids = NULL; |
| 849 |
sstate->tab_hash_funcs = NULL; |
| 850 |
sstate->tab_collations = NULL; |
| 851 |
sstate->cur_eq_funcs = NULL; |
| 852 |
|
| 853 |
/* |
| 854 |
* If this is an initplan, it has output parameters that the parent plan |
| 855 |
* will use, so mark those parameters as needing evaluation. We don't |
| 856 |
* actually run the subplan until we first need one of its outputs. |
| 857 |
* |
| 858 |
* A CTE subplan's output parameter is never to be evaluated in the normal |
| 859 |
* way, so skip this in that case. |
| 860 |
* |
| 861 |
* Note that we don't set parent->chgParam here: the parent plan hasn't |
| 862 |
* been run yet, so no need to force it to re-run. |
| 863 |
*/ |
| 864 |
if (subplan->setParam != NIL && subplan->parParam == NIL && |
| 865 |
subplan->subLinkType != CTE_SUBLINK) |
| 866 |
{ |
| 867 |
ListCell *lst; |
| 868 |
|
| 869 |
foreach(lst, subplan->setParam) |
| 870 |
{ |
| 871 |
int paramid = lfirst_int(lst); |
| 872 |
ParamExecData *prm = &(estate->es_param_exec_vals[paramid]); |
| 873 |
|
| 874 |
prm->execPlan = sstate; |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
/* |
| 879 |
* If we are going to hash the subquery output, initialize relevant stuff. |
| 880 |
* (We don't create the hashtable until needed, though.) |
| 881 |
*/ |
| 882 |
if (subplan->useHashTable) |
| 883 |
{ |
| 884 |
int ncols, |
| 885 |
i; |
| 886 |
TupleDesc tupDescLeft; |
| 887 |
TupleDesc tupDescRight; |
| 888 |
Oid *cross_eq_funcoids; |
| 889 |
TupleTableSlot *slot; |
| 890 |
FmgrInfo *lhs_hash_funcs; |
| 891 |
List *oplist, |
| 892 |
*lefttlist, |
| 893 |
*righttlist; |
| 894 |
ListCell *l; |
| 895 |
|
| 896 |
/* We need a memory context to hold the hash table(s) */ |
| 897 |
sstate->hashtablecxt = |
| 898 |
AllocSetContextCreate(CurrentMemoryContext, |
| 899 |
"Subplan HashTable Context", |
| 900 |
ALLOCSET_DEFAULT_SIZES); |
| 901 |
/* and a small one for the hash tables to use as temp storage */ |
| 902 |
sstate->hashtempcxt = |
| 903 |
AllocSetContextCreate(CurrentMemoryContext, |
| 904 |
"Subplan HashTable Temp Context", |
| 905 |
ALLOCSET_SMALL_SIZES); |
| 906 |
/* and a short-lived exprcontext for function evaluation */ |
| 907 |
sstate->innerecontext = CreateExprContext(estate); |
| 908 |
|
| 909 |
/* |
| 910 |
* We use ExecProject to evaluate the lefthand and righthand |
| 911 |
* expression lists and form tuples. (You might think that we could |
| 912 |
* use the sub-select's output tuples directly, but that is not the |
| 913 |
* case if we had to insert any run-time coercions of the sub-select's |
| 914 |
* output datatypes; anyway this avoids storing any resjunk columns |
| 915 |
* that might be in the sub-select's output.) Run through the |
| 916 |
* combining expressions to build tlists for the lefthand and |
| 917 |
* righthand sides. |
| 918 |
* |
| 919 |
* We also extract the combining operators themselves to initialize |
| 920 |
* the equality and hashing functions for the hash tables. |
| 921 |
*/ |
| 922 |
if (IsA(subplan->testexpr, OpExpr)) |
| 923 |
{ |
| 924 |
/* single combining operator */ |
| 925 |
oplist = list_make1(subplan->testexpr); |
| 926 |
} |
| 927 |
else if (is_andclause(subplan->testexpr)) |
| 928 |
{ |
| 929 |
/* multiple combining operators */ |
| 930 |
oplist = castNode(BoolExpr, subplan->testexpr)->args; |
| 931 |
} |
| 932 |
else |
| 933 |
{ |
| 934 |
/* shouldn't see anything else in a hashable subplan */ |
| 935 |
elog(ERROR, "unrecognized testexpr type: %d", |
| 936 |
(int) nodeTag(subplan->testexpr)); |
| 937 |
oplist = NIL; /* keep compiler quiet */ |
| 938 |
} |
| 939 |
ncols = list_length(oplist); |
| 940 |
|
| 941 |
lefttlist = righttlist = NIL; |
| 942 |
sstate->numCols = ncols; |
| 943 |
sstate->keyColIdx = (AttrNumber *) palloc(ncols * sizeof(AttrNumber)); |
| 944 |
sstate->tab_eq_funcoids = (Oid *) palloc(ncols * sizeof(Oid)); |
| 945 |
sstate->tab_collations = (Oid *) palloc(ncols * sizeof(Oid)); |
| 946 |
sstate->tab_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo)); |
| 947 |
lhs_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo)); |
| 948 |
sstate->cur_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo)); |
| 949 |
/* we'll need the cross-type equality fns below, but not in sstate */ |
| 950 |
cross_eq_funcoids = (Oid *) palloc(ncols * sizeof(Oid)); |
| 951 |
|
| 952 |
i = 1; |
| 953 |
foreach(l, oplist) |
| 954 |
{ |
| 955 |
OpExpr *opexpr = lfirst_node(OpExpr, l); |
| 956 |
Expr *expr; |
| 957 |
TargetEntry *tle; |
| 958 |
Oid rhs_eq_oper; |
| 959 |
Oid left_hashfn; |
| 960 |
Oid right_hashfn; |
| 961 |
|
| 962 |
Assert(list_length(opexpr->args) == 2); |
| 963 |
|
| 964 |
/* Process lefthand argument */ |
| 965 |
expr = (Expr *) linitial(opexpr->args); |
| 966 |
tle = makeTargetEntry(expr, |
| 967 |
i, |
| 968 |
NULL, |
| 969 |
false); |
| 970 |
lefttlist = lappend(lefttlist, tle); |
| 971 |
|
| 972 |
/* Process righthand argument */ |
| 973 |
expr = (Expr *) lsecond(opexpr->args); |
| 974 |
tle = makeTargetEntry(expr, |
| 975 |
i, |
| 976 |
NULL, |
| 977 |
false); |
| 978 |
righttlist = lappend(righttlist, tle); |
| 979 |
|
| 980 |
/* Lookup the equality function (potentially cross-type) */ |
| 981 |
cross_eq_funcoids[i - 1] = opexpr->opfuncid; |
| 982 |
fmgr_info(opexpr->opfuncid, &sstate->cur_eq_funcs[i - 1]); |
| 983 |
fmgr_info_set_expr((Node *) opexpr, &sstate->cur_eq_funcs[i - 1]); |
| 984 |
|
| 985 |
/* Look up the equality function for the RHS type */ |
| 986 |
if (!get_compatible_hash_operators(opexpr->opno, |
| 987 |
NULL, &rhs_eq_oper)) |
| 988 |
elog(ERROR, "could not find compatible hash operator for operator %u", |
| 989 |
opexpr->opno); |
| 990 |
sstate->tab_eq_funcoids[i - 1] = get_opcode(rhs_eq_oper); |
| 991 |
|
| 992 |
/* Lookup the associated hash functions */ |
| 993 |
if (!get_op_hash_functions(opexpr->opno, |
| 994 |
&left_hashfn, &right_hashfn)) |
| 995 |
elog(ERROR, "could not find hash function for hash operator %u", |
| 996 |
opexpr->opno); |
| 997 |
fmgr_info(left_hashfn, &lhs_hash_funcs[i - 1]); |
| 998 |
fmgr_info(right_hashfn, &sstate->tab_hash_funcs[i - 1]); |
| 999 |
|
| 1000 |
/* Set collation */ |
| 1001 |
sstate->tab_collations[i - 1] = opexpr->inputcollid; |
| 1002 |
|
| 1003 |
/* keyColIdx is just column numbers 1..n */ |
| 1004 |
sstate->keyColIdx[i - 1] = i; |
| 1005 |
|
| 1006 |
i++; |
| 1007 |
} |
| 1008 |
|
| 1009 |
/* |
| 1010 |
* Construct tupdescs, slots and projection nodes for left and right |
| 1011 |
* sides. The lefthand expressions will be evaluated in the parent |
| 1012 |
* plan node's exprcontext, which we don't have access to here. |
| 1013 |
* Fortunately we can just pass NULL for now and fill it in later |
| 1014 |
* (hack alert!). The righthand expressions will be evaluated in our |
| 1015 |
* own innerecontext. |
| 1016 |
*/ |
| 1017 |
tupDescLeft = ExecTypeFromTL(lefttlist); |
| 1018 |
slot = ExecInitExtraTupleSlot(estate, tupDescLeft, &TTSOpsVirtual); |
| 1019 |
sstate->projLeft = ExecBuildProjectionInfo(lefttlist, |
| 1020 |
NULL, |
| 1021 |
slot, |
| 1022 |
parent, |
| 1023 |
NULL); |
| 1024 |
|
| 1025 |
sstate->descRight = tupDescRight = ExecTypeFromTL(righttlist); |
| 1026 |
slot = ExecInitExtraTupleSlot(estate, tupDescRight, &TTSOpsVirtual); |
| 1027 |
sstate->projRight = ExecBuildProjectionInfo(righttlist, |
| 1028 |
sstate->innerecontext, |
| 1029 |
slot, |
| 1030 |
sstate->planstate, |
| 1031 |
NULL); |
| 1032 |
|
| 1033 |
/* Build the ExprState for generating hash values */ |
| 1034 |
sstate->lhs_hash_expr = ExecBuildHash32FromAttrs(tupDescLeft, |
| 1035 |
&TTSOpsVirtual, |
| 1036 |
lhs_hash_funcs, |
| 1037 |
sstate->tab_collations, |
| 1038 |
sstate->numCols, |
| 1039 |
sstate->keyColIdx, |
| 1040 |
parent, |
| 1041 |
0); |
| 1042 |
|
| 1043 |
/* |
| 1044 |
* Create comparator for lookups of rows in the table (potentially |
| 1045 |
* cross-type comparisons). |
| 1046 |
*/ |
| 1047 |
sstate->cur_eq_comp = ExecBuildGroupingEqual(tupDescLeft, tupDescRight, |
| 1048 |
&TTSOpsVirtual, &TTSOpsMinimalTuple, |
| 1049 |
ncols, |
| 1050 |
sstate->keyColIdx, |
| 1051 |
cross_eq_funcoids, |
| 1052 |
sstate->tab_collations, |
| 1053 |
parent); |
| 1054 |
} |
| 1055 |
|
| 1056 |
return sstate; |
| 1057 |
} |
| 1058 |
|
| 1059 |
/* ---------------------------------------------------------------- |
| 1060 |
* ExecSetParamPlan |
| 1061 |
* |
| 1062 |
* Executes a subplan and sets its output parameters. |
| 1063 |
* |
| 1064 |
* This is called from ExecEvalParamExec() when the value of a PARAM_EXEC |
| 1065 |
* parameter is requested and the param's execPlan field is set (indicating |
| 1066 |
* that the param has not yet been evaluated). This allows lazy evaluation |
| 1067 |
* of initplans: we don't run the subplan until/unless we need its output. |
| 1068 |
* Note that this routine MUST clear the execPlan fields of the plan's |
| 1069 |
* output parameters after evaluating them! |
| 1070 |
* |
| 1071 |
* The results of this function are stored in the EState associated with the |
| 1072 |
* ExprContext (particularly, its ecxt_param_exec_vals); any pass-by-ref |
| 1073 |
* result Datums are allocated in the EState's per-query memory. The passed |
| 1074 |
* econtext can be any ExprContext belonging to that EState; which one is |
| 1075 |
* important only to the extent that the ExprContext's per-tuple memory |
| 1076 |
* context is used to evaluate any parameters passed down to the subplan. |
| 1077 |
* (Thus in principle, the shorter-lived the ExprContext the better, since |
| 1078 |
* that data isn't needed after we return. In practice, because initplan |
| 1079 |
* parameters are never more complex than Vars, Aggrefs, etc, evaluating them |
| 1080 |
* currently never leaks any memory anyway.) |
| 1081 |
* ---------------------------------------------------------------- |
| 1082 |
*/ |
| 1083 |
void |
| 1084 |
ExecSetParamPlan(SubPlanState *node, ExprContext *econtext) |
| 1085 |
{ |
| 1086 |
SubPlan *subplan = node->subplan; |
| 1087 |
PlanState *planstate = node->planstate; |
| 1088 |
SubLinkType subLinkType = subplan->subLinkType; |
| 1089 |
EState *estate = planstate->state; |
| 1090 |
ScanDirection dir = estate->es_direction; |
| 1091 |
MemoryContext oldcontext; |
| 1092 |
TupleTableSlot *slot; |
| 1093 |
ListCell *l; |
| 1094 |
bool found = false; |
| 1095 |
ArrayBuildStateAny *astate = NULL; |
| 1096 |
|
| 1097 |
if (subLinkType == ANY_SUBLINK || |
| 1098 |
subLinkType == ALL_SUBLINK) |
| 1099 |
elog(ERROR, "ANY/ALL subselect unsupported as initplan"); |
| 1100 |
if (subLinkType == CTE_SUBLINK) |
| 1101 |
elog(ERROR, "CTE subplans should not be executed via ExecSetParamPlan"); |
| 1102 |
if (subplan->parParam || subplan->args) |
| 1103 |
elog(ERROR, "correlated subplans should not be executed via ExecSetParamPlan"); |
| 1104 |
|
| 1105 |
/* |
| 1106 |
* Enforce forward scan direction regardless of caller. It's hard but not |
| 1107 |
* impossible to get here in backward scan, so make it work anyway. |
| 1108 |
*/ |
| 1109 |
estate->es_direction = ForwardScanDirection; |
| 1110 |
|
| 1111 |
/* Initialize ArrayBuildStateAny in caller's context, if needed */ |
| 1112 |
if (subLinkType == ARRAY_SUBLINK) |
| 1113 |
astate = initArrayResultAny(subplan->firstColType, |
| 1114 |
CurrentMemoryContext, true); |
| 1115 |
|
| 1116 |
/* |
| 1117 |
* Must switch to per-query memory context. |
| 1118 |
*/ |
| 1119 |
oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory); |
| 1120 |
|
| 1121 |
/* |
| 1122 |
* Run the plan. (If it needs to be rescanned, the first ExecProcNode |
| 1123 |
* call will take care of that.) |
| 1124 |
*/ |
| 1125 |
for (slot = ExecProcNode(planstate); |
| 1126 |
!TupIsNull(slot); |
| 1127 |
slot = ExecProcNode(planstate)) |
| 1128 |
{ |
| 1129 |
TupleDesc tdesc = slot->tts_tupleDescriptor; |
| 1130 |
int i = 1; |
| 1131 |
|
| 1132 |
if (subLinkType == EXISTS_SUBLINK) |
| 1133 |
{ |
| 1134 |
/* There can be only one setParam... */ |
| 1135 |
int paramid = linitial_int(subplan->setParam); |
| 1136 |
ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]); |
| 1137 |
|
| 1138 |
prm->execPlan = NULL; |
| 1139 |
prm->value = BoolGetDatum(true); |
| 1140 |
prm->isnull = false; |
| 1141 |
found = true; |
| 1142 |
break; |
| 1143 |
} |
| 1144 |
|
| 1145 |
if (subLinkType == ARRAY_SUBLINK) |
| 1146 |
{ |
| 1147 |
Datum dvalue; |
| 1148 |
bool disnull; |
| 1149 |
|
| 1150 |
found = true; |
| 1151 |
/* stash away current value */ |
| 1152 |
Assert(subplan->firstColType == TupleDescAttr(tdesc, 0)->atttypid); |
| 1153 |
dvalue = slot_getattr(slot, 1, &disnull); |
| 1154 |
astate = accumArrayResultAny(astate, dvalue, disnull, |
| 1155 |
subplan->firstColType, oldcontext); |
| 1156 |
/* keep scanning subplan to collect all values */ |
| 1157 |
continue; |
| 1158 |
} |
| 1159 |
|
| 1160 |
if (found && |
| 1161 |
(subLinkType == EXPR_SUBLINK || |
| 1162 |
subLinkType == MULTIEXPR_SUBLINK || |
| 1163 |
subLinkType == ROWCOMPARE_SUBLINK)) |
| 1164 |
ereport(ERROR, |
| 1165 |
(errcode(ERRCODE_CARDINALITY_VIOLATION), |
| 1166 |
errmsg("more than one row returned by a subquery used as an expression"))); |
| 1167 |
|
| 1168 |
found = true; |
| 1169 |
|
| 1170 |
/* |
| 1171 |
* We need to copy the subplan's tuple into our own context, in case |
| 1172 |
* any of the params are pass-by-ref type --- the pointers stored in |
| 1173 |
* the param structs will point at this copied tuple! node->curTuple |
| 1174 |
* keeps track of the copied tuple for eventual freeing. |
| 1175 |
*/ |
| 1176 |
if (node->curTuple) |
| 1177 |
heap_freetuple(node->curTuple); |
| 1178 |
node->curTuple = ExecCopySlotHeapTuple(slot); |
| 1179 |
|
| 1180 |
/* |
| 1181 |
* Now set all the setParam params from the columns of the tuple |
| 1182 |
*/ |
| 1183 |
foreach(l, subplan->setParam) |
| 1184 |
{ |
| 1185 |
int paramid = lfirst_int(l); |
| 1186 |
ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]); |
| 1187 |
|
| 1188 |
prm->execPlan = NULL; |
| 1189 |
prm->value = heap_getattr(node->curTuple, i, tdesc, |
| 1190 |
&(prm->isnull)); |
| 1191 |
i++; |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
if (subLinkType == ARRAY_SUBLINK) |
| 1196 |
{ |
| 1197 |
/* There can be only one setParam... */ |
| 1198 |
int paramid = linitial_int(subplan->setParam); |
| 1199 |
ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]); |
| 1200 |
|
| 1201 |
/* |
| 1202 |
* We build the result array in query context so it won't disappear; |
| 1203 |
* to avoid leaking memory across repeated calls, we have to remember |
| 1204 |
* the latest value, much as for curTuple above. |
| 1205 |
*/ |
| 1206 |
if (node->curArray != PointerGetDatum(NULL)) |
| 1207 |
pfree(DatumGetPointer(node->curArray)); |
| 1208 |
node->curArray = makeArrayResultAny(astate, |
| 1209 |
econtext->ecxt_per_query_memory, |
| 1210 |
true); |
| 1211 |
prm->execPlan = NULL; |
| 1212 |
prm->value = node->curArray; |
| 1213 |
prm->isnull = false; |
| 1214 |
} |
| 1215 |
else if (!found) |
| 1216 |
{ |
| 1217 |
if (subLinkType == EXISTS_SUBLINK) |
| 1218 |
{ |
| 1219 |
/* There can be only one setParam... */ |
| 1220 |
int paramid = linitial_int(subplan->setParam); |
| 1221 |
ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]); |
| 1222 |
|
| 1223 |
prm->execPlan = NULL; |
| 1224 |
prm->value = BoolGetDatum(false); |
| 1225 |
prm->isnull = false; |
| 1226 |
} |
| 1227 |
else |
| 1228 |
{ |
| 1229 |
/* For other sublink types, set all the output params to NULL */ |
| 1230 |
foreach(l, subplan->setParam) |
| 1231 |
{ |
| 1232 |
int paramid = lfirst_int(l); |
| 1233 |
ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]); |
| 1234 |
|
| 1235 |
prm->execPlan = NULL; |
| 1236 |
prm->value = (Datum) 0; |
| 1237 |
prm->isnull = true; |
| 1238 |
} |
| 1239 |
} |
| 1240 |
} |
| 1241 |
|
| 1242 |
MemoryContextSwitchTo(oldcontext); |
| 1243 |
|
| 1244 |
/* restore scan direction */ |
| 1245 |
estate->es_direction = dir; |
| 1246 |
} |
| 1247 |
|
| 1248 |
/* |
| 1249 |
* ExecSetParamPlanMulti |
| 1250 |
* |
| 1251 |
* Apply ExecSetParamPlan to evaluate any not-yet-evaluated initplan output |
| 1252 |
* parameters whose ParamIDs are listed in "params". Any listed params that |
| 1253 |
* are not initplan outputs are ignored. |
| 1254 |
* |
| 1255 |
* As with ExecSetParamPlan, any ExprContext belonging to the current EState |
| 1256 |
* can be used, but in principle a shorter-lived ExprContext is better than a |
| 1257 |
* longer-lived one. |
| 1258 |
*/ |
| 1259 |
void |
| 1260 |
ExecSetParamPlanMulti(const Bitmapset *params, ExprContext *econtext) |
| 1261 |
{ |
| 1262 |
int paramid; |
| 1263 |
|
| 1264 |
paramid = -1; |
| 1265 |
while ((paramid = bms_next_member(params, paramid)) >= 0) |
| 1266 |
{ |
| 1267 |
ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]); |
| 1268 |
|
| 1269 |
if (prm->execPlan != NULL) |
| 1270 |
{ |
| 1271 |
/* Parameter not evaluated yet, so go do it */ |
| 1272 |
ExecSetParamPlan(prm->execPlan, econtext); |
| 1273 |
/* ExecSetParamPlan should have processed this param... */ |
| 1274 |
Assert(prm->execPlan == NULL); |
| 1275 |
} |
| 1276 |
} |
| 1277 |
} |
| 1278 |
|
| 1279 |
/* |
| 1280 |
* Mark an initplan as needing recalculation |
| 1281 |
*/ |
| 1282 |
void |
| 1283 |
ExecReScanSetParamPlan(SubPlanState *node, PlanState *parent) |
| 1284 |
{ |
| 1285 |
PlanState *planstate = node->planstate; |
| 1286 |
SubPlan *subplan = node->subplan; |
| 1287 |
EState *estate = parent->state; |
| 1288 |
ListCell *l; |
| 1289 |
|
| 1290 |
/* sanity checks */ |
| 1291 |
if (subplan->parParam != NIL) |
| 1292 |
elog(ERROR, "direct correlated subquery unsupported as initplan"); |
| 1293 |
if (subplan->setParam == NIL) |
| 1294 |
elog(ERROR, "setParam list of initplan is empty"); |
| 1295 |
if (bms_is_empty(planstate->plan->extParam)) |
| 1296 |
elog(ERROR, "extParam set of initplan is empty"); |
| 1297 |
|
| 1298 |
/* |
| 1299 |
* Don't actually re-scan: it'll happen inside ExecSetParamPlan if needed. |
| 1300 |
*/ |
| 1301 |
|
| 1302 |
/* |
| 1303 |
* Mark this subplan's output parameters as needing recalculation. |
| 1304 |
* |
| 1305 |
* CTE subplans are never executed via parameter recalculation; instead |
| 1306 |
* they get run when called by nodeCtescan.c. So don't mark the output |
| 1307 |
* parameter of a CTE subplan as dirty, but do set the chgParam bit for it |
| 1308 |
* so that dependent plan nodes will get told to rescan. |
| 1309 |
*/ |
| 1310 |
foreach(l, subplan->setParam) |
| 1311 |
{ |
| 1312 |
int paramid = lfirst_int(l); |
| 1313 |
ParamExecData *prm = &(estate->es_param_exec_vals[paramid]); |
| 1314 |
|
| 1315 |
if (subplan->subLinkType != CTE_SUBLINK) |
| 1316 |
prm->execPlan = node; |
| 1317 |
|
| 1318 |
parent->chgParam = bms_add_member(parent->chgParam, paramid); |
| 1319 |
} |
| 1320 |
} |