1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-25 20:23:07 +03:00

Handle SubPlan cases in find_nonnullable_rels/vars.

We can use some variants of SubPlan to deduce that Vars appearing
in the testexpr must be non-null.

Richard Guo

Discussion: https://postgr.es/m/CAMbWs4-jV=199A2Y_6==99dYnpnmaO_Wz_RGkRTTaCB=Pihw2w@mail.gmail.com
This commit is contained in:
Tom Lane
2022-11-05 15:24:36 -04:00
parent c3652cd84a
commit ff8fa0bf7e
3 changed files with 75 additions and 0 deletions

View File

@@ -1511,6 +1511,31 @@ find_nonnullable_rels_walker(Node *node, bool top_level)
expr->booltesttype == IS_NOT_UNKNOWN))
result = find_nonnullable_rels_walker((Node *) expr->arg, false);
}
else if (IsA(node, SubPlan))
{
SubPlan *splan = (SubPlan *) node;
/*
* For some types of SubPlan, we can infer strictness from Vars in the
* testexpr (the LHS of the original SubLink).
*
* For ANY_SUBLINK, if the subquery produces zero rows, the result is
* always FALSE. If the subquery produces more than one row, the
* per-row results of the testexpr are combined using OR semantics.
* Hence ANY_SUBLINK can be strict only at top level, but there it's
* as strict as the testexpr is.
*
* For ROWCOMPARE_SUBLINK, if the subquery produces zero rows, the
* result is always NULL. Otherwise, the result is as strict as the
* testexpr is. So we can check regardless of top_level.
*
* We can't prove anything for other sublink types (in particular,
* note that ALL_SUBLINK will return TRUE if the subquery is empty).
*/
if ((top_level && splan->subLinkType == ANY_SUBLINK) ||
splan->subLinkType == ROWCOMPARE_SUBLINK)
result = find_nonnullable_rels_walker(splan->testexpr, top_level);
}
else if (IsA(node, PlaceHolderVar))
{
PlaceHolderVar *phv = (PlaceHolderVar *) node;
@@ -1736,6 +1761,15 @@ find_nonnullable_vars_walker(Node *node, bool top_level)
expr->booltesttype == IS_NOT_UNKNOWN))
result = find_nonnullable_vars_walker((Node *) expr->arg, false);
}
else if (IsA(node, SubPlan))
{
SubPlan *splan = (SubPlan *) node;
/* See analysis in find_nonnullable_rels_walker */
if ((top_level && splan->subLinkType == ANY_SUBLINK) ||
splan->subLinkType == ROWCOMPARE_SUBLINK)
result = find_nonnullable_vars_walker(splan->testexpr, top_level);
}
else if (IsA(node, PlaceHolderVar))
{
PlaceHolderVar *phv = (PlaceHolderVar *) node;