* subquery_push_qual - push down a qual that we have determined is safe */
| 4451 | * subquery_push_qual - push down a qual that we have determined is safe |
| 4452 | */ |
| 4453 | static void |
| 4454 | subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual) |
| 4455 | { |
| 4456 | if (subquery->setOperations != NULL) |
| 4457 | { |
| 4458 | /* Recurse to push it separately to each component query */ |
| 4459 | recurse_push_qual(subquery->setOperations, subquery, |
| 4460 | rte, rti, qual); |
| 4461 | } |
| 4462 | else |
| 4463 | { |
| 4464 | /* |
| 4465 | * We need to replace Vars in the qual (which must refer to outputs of |
| 4466 | * the subquery) with copies of the subquery's targetlist expressions. |
| 4467 | * Note that at this point, any uplevel Vars in the qual should have |
| 4468 | * been replaced with Params, so they need no work. |
| 4469 | * |
| 4470 | * This step also ensures that when we are pushing into a setop tree, |
| 4471 | * each component query gets its own copy of the qual. |
| 4472 | */ |
| 4473 | qual = ReplaceVarsFromTargetList(qual, rti, 0, rte, |
| 4474 | subquery->targetList, |
| 4475 | REPLACEVARS_REPORT_ERROR, 0, |
| 4476 | &subquery->hasSubLinks); |
| 4477 | |
| 4478 | /* |
| 4479 | * Now attach the qual to the proper place: normally WHERE, but if the |
| 4480 | * subquery uses grouping or aggregation, put it in HAVING (since the |
| 4481 | * qual really refers to the group-result rows). |
| 4482 | */ |
| 4483 | if (subquery->hasAggs || subquery->groupClause || subquery->groupingSets || subquery->havingQual) |
| 4484 | subquery->havingQual = make_and_qual(subquery->havingQual, qual); |
| 4485 | else |
| 4486 | subquery->jointree->quals = |
| 4487 | make_and_qual(subquery->jointree->quals, qual); |
| 4488 | |
| 4489 | /* |
| 4490 | * We need not change the subquery's hasAggs or hasSubLinks flags, |
| 4491 | * since we can't be pushing down any aggregates that weren't there |
| 4492 | * before, and we don't push down subselects at all. |
| 4493 | */ |
| 4494 | } |
| 4495 | } |
| 4496 | |
| 4497 | /* |
| 4498 | * Helper routine to recurse through setOperations tree |
no test coverage detected