mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-12-09 08:10:09 +08:00
Be smart about outer-join qualifications that mention only one side of
the join, per recent discussion on pgsql-sql. Not clear that this will come up often in real queries, but it's not any more expensive to do it right, so we may as well do it right.
This commit is contained in:
parent
a1dbe521bc
commit
30a695d356
@ -8,7 +8,7 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/initsplan.c,v 1.84 2003/02/08 20:20:54 tgl Exp $
|
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/initsplan.c,v 1.85 2003/03/02 23:46:34 tgl Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -36,10 +36,10 @@
|
|||||||
static void mark_baserels_for_outer_join(Query *root, Relids rels,
|
static void mark_baserels_for_outer_join(Query *root, Relids rels,
|
||||||
Relids outerrels);
|
Relids outerrels);
|
||||||
static void distribute_qual_to_rels(Query *root, Node *clause,
|
static void distribute_qual_to_rels(Query *root, Node *clause,
|
||||||
bool ispusheddown,
|
bool ispusheddown,
|
||||||
bool isouterjoin,
|
bool isdeduced,
|
||||||
bool isdeduced,
|
Relids outerjoin_nonnullable,
|
||||||
Relids qualscope);
|
Relids qualscope);
|
||||||
static void add_vars_to_targetlist(Query *root, List *vars);
|
static void add_vars_to_targetlist(Query *root, List *vars);
|
||||||
static bool qual_is_redundant(Query *root, RestrictInfo *restrictinfo,
|
static bool qual_is_redundant(Query *root, RestrictInfo *restrictinfo,
|
||||||
List *restrictlist);
|
List *restrictlist);
|
||||||
@ -209,48 +209,53 @@ distribute_quals_to_rels(Query *root, Node *jtnode)
|
|||||||
*/
|
*/
|
||||||
foreach(qual, (List *) f->quals)
|
foreach(qual, (List *) f->quals)
|
||||||
distribute_qual_to_rels(root, (Node *) lfirst(qual),
|
distribute_qual_to_rels(root, (Node *) lfirst(qual),
|
||||||
true, false, false, result);
|
true, false, NULL, result);
|
||||||
}
|
}
|
||||||
else if (IsA(jtnode, JoinExpr))
|
else if (IsA(jtnode, JoinExpr))
|
||||||
{
|
{
|
||||||
JoinExpr *j = (JoinExpr *) jtnode;
|
JoinExpr *j = (JoinExpr *) jtnode;
|
||||||
Relids leftids,
|
Relids leftids,
|
||||||
rightids;
|
rightids,
|
||||||
bool isouterjoin;
|
nonnullable_rels,
|
||||||
|
nullable_rels;
|
||||||
List *qual;
|
List *qual;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Order of operations here is subtle and critical. First we
|
* Order of operations here is subtle and critical. First we
|
||||||
* recurse to handle sub-JOINs. Their join quals will be placed
|
* recurse to handle sub-JOINs. Their join quals will be placed
|
||||||
* without regard for whether this level is an outer join, which
|
* without regard for whether this level is an outer join, which
|
||||||
* is correct. Then, if we are an outer join, we mark baserels
|
* is correct. Then we place our own join quals, which are restricted
|
||||||
* contained within the nullable side(s) with our own rel set;
|
* by lower outer joins in any case, and are forced to this level if
|
||||||
* this will restrict placement of subsequent quals using those
|
* this is an outer join and they mention the outer side. Finally, if
|
||||||
* rels, including our own quals and quals above us in the join
|
* this is an outer join, we mark baserels contained within the inner
|
||||||
* tree. Finally we place our own join quals.
|
* side(s) with our own rel set; this will prevent quals above us in
|
||||||
|
* the join tree that use those rels from being pushed down below this
|
||||||
|
* level. (It's okay for upper quals to be pushed down to the outer
|
||||||
|
* side, however.)
|
||||||
*/
|
*/
|
||||||
leftids = distribute_quals_to_rels(root, j->larg);
|
leftids = distribute_quals_to_rels(root, j->larg);
|
||||||
rightids = distribute_quals_to_rels(root, j->rarg);
|
rightids = distribute_quals_to_rels(root, j->rarg);
|
||||||
|
|
||||||
result = bms_union(leftids, rightids);
|
result = bms_union(leftids, rightids);
|
||||||
|
|
||||||
isouterjoin = false;
|
nonnullable_rels = nullable_rels = NULL;
|
||||||
switch (j->jointype)
|
switch (j->jointype)
|
||||||
{
|
{
|
||||||
case JOIN_INNER:
|
case JOIN_INNER:
|
||||||
/* Inner join adds no restrictions for quals */
|
/* Inner join adds no restrictions for quals */
|
||||||
break;
|
break;
|
||||||
case JOIN_LEFT:
|
case JOIN_LEFT:
|
||||||
mark_baserels_for_outer_join(root, rightids, result);
|
nonnullable_rels = leftids;
|
||||||
isouterjoin = true;
|
nullable_rels = rightids;
|
||||||
break;
|
break;
|
||||||
case JOIN_FULL:
|
case JOIN_FULL:
|
||||||
mark_baserels_for_outer_join(root, result, result);
|
/* each side is both outer and inner */
|
||||||
isouterjoin = true;
|
nonnullable_rels = result;
|
||||||
|
nullable_rels = result;
|
||||||
break;
|
break;
|
||||||
case JOIN_RIGHT:
|
case JOIN_RIGHT:
|
||||||
mark_baserels_for_outer_join(root, leftids, result);
|
nonnullable_rels = rightids;
|
||||||
isouterjoin = true;
|
nullable_rels = leftids;
|
||||||
break;
|
break;
|
||||||
case JOIN_UNION:
|
case JOIN_UNION:
|
||||||
|
|
||||||
@ -269,7 +274,11 @@ distribute_quals_to_rels(Query *root, Node *jtnode)
|
|||||||
|
|
||||||
foreach(qual, (List *) j->quals)
|
foreach(qual, (List *) j->quals)
|
||||||
distribute_qual_to_rels(root, (Node *) lfirst(qual),
|
distribute_qual_to_rels(root, (Node *) lfirst(qual),
|
||||||
false, isouterjoin, false, result);
|
false, false,
|
||||||
|
nonnullable_rels, result);
|
||||||
|
|
||||||
|
if (nullable_rels != NULL)
|
||||||
|
mark_baserels_for_outer_join(root, nullable_rels, result);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
elog(ERROR, "distribute_quals_to_rels: unexpected node type %d",
|
elog(ERROR, "distribute_quals_to_rels: unexpected node type %d",
|
||||||
@ -324,14 +333,16 @@ mark_baserels_for_outer_join(Query *root, Relids rels, Relids outerrels)
|
|||||||
* (depending on whether the clause is a join) of each base relation
|
* (depending on whether the clause is a join) of each base relation
|
||||||
* mentioned in the clause. A RestrictInfo node is created and added to
|
* mentioned in the clause. A RestrictInfo node is created and added to
|
||||||
* the appropriate list for each rel. Also, if the clause uses a
|
* the appropriate list for each rel. Also, if the clause uses a
|
||||||
* mergejoinable operator and is not an outer-join qual, enter the left-
|
* mergejoinable operator and is not delayed by outer-join rules, enter
|
||||||
* and right-side expressions into the query's lists of equijoined vars.
|
* the left- and right-side expressions into the query's lists of
|
||||||
|
* equijoined vars.
|
||||||
*
|
*
|
||||||
* 'clause': the qual clause to be distributed
|
* 'clause': the qual clause to be distributed
|
||||||
* 'ispusheddown': if TRUE, force the clause to be marked 'ispusheddown'
|
* 'ispusheddown': if TRUE, force the clause to be marked 'ispusheddown'
|
||||||
* (this indicates the clause came from a FromExpr, not a JoinExpr)
|
* (this indicates the clause came from a FromExpr, not a JoinExpr)
|
||||||
* 'isouterjoin': TRUE if the qual came from an OUTER JOIN's ON-clause
|
|
||||||
* 'isdeduced': TRUE if the qual came from implied-equality deduction
|
* 'isdeduced': TRUE if the qual came from implied-equality deduction
|
||||||
|
* 'outerjoin_nonnullable': NULL if not an outer-join qual, else the set of
|
||||||
|
* baserels appearing on the outer (nonnullable) side of the join
|
||||||
* 'qualscope': set of baserels the qual's syntactic scope covers
|
* 'qualscope': set of baserels the qual's syntactic scope covers
|
||||||
*
|
*
|
||||||
* 'qualscope' identifies what level of JOIN the qual came from. For a top
|
* 'qualscope' identifies what level of JOIN the qual came from. For a top
|
||||||
@ -341,8 +352,8 @@ mark_baserels_for_outer_join(Query *root, Relids rels, Relids outerrels)
|
|||||||
static void
|
static void
|
||||||
distribute_qual_to_rels(Query *root, Node *clause,
|
distribute_qual_to_rels(Query *root, Node *clause,
|
||||||
bool ispusheddown,
|
bool ispusheddown,
|
||||||
bool isouterjoin,
|
|
||||||
bool isdeduced,
|
bool isdeduced,
|
||||||
|
Relids outerjoin_nonnullable,
|
||||||
Relids qualscope)
|
Relids qualscope)
|
||||||
{
|
{
|
||||||
RestrictInfo *restrictinfo = makeNode(RestrictInfo);
|
RestrictInfo *restrictinfo = makeNode(RestrictInfo);
|
||||||
@ -392,63 +403,80 @@ distribute_qual_to_rels(Query *root, Node *clause,
|
|||||||
relids = qualscope;
|
relids = qualscope;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* For an outer-join qual, pretend that the clause references all rels
|
* Check to see if clause application must be delayed by outer-join
|
||||||
* appearing within its syntactic scope, even if it really doesn't.
|
* considerations.
|
||||||
* This ensures that the clause will be evaluated exactly at the level
|
|
||||||
* of joining corresponding to the outer join.
|
|
||||||
*
|
|
||||||
* For a non-outer-join qual, we can evaluate the qual as soon as (1) we
|
|
||||||
* have all the rels it mentions, and (2) we are at or above any outer
|
|
||||||
* joins that can null any of these rels and are below the syntactic
|
|
||||||
* location of the given qual. To enforce the latter, scan the base
|
|
||||||
* rels listed in relids, and merge their outer-join sets into the
|
|
||||||
* clause's own reference list. At the time we are called, the
|
|
||||||
* outerjoinset of each baserel will show exactly those outer
|
|
||||||
* joins that are below the qual in the join tree.
|
|
||||||
*
|
|
||||||
* If the qual came from implied-equality deduction, we can evaluate the
|
|
||||||
* qual at its natural semantic level.
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
if (isdeduced)
|
if (isdeduced)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
* If the qual came from implied-equality deduction, we can evaluate
|
||||||
|
* the qual at its natural semantic level. It is not affected by
|
||||||
|
* any outer-join rules (else we'd not have decided the vars were
|
||||||
|
* equal).
|
||||||
|
*/
|
||||||
Assert(bms_equal(relids, qualscope));
|
Assert(bms_equal(relids, qualscope));
|
||||||
can_be_equijoin = true;
|
can_be_equijoin = true;
|
||||||
}
|
}
|
||||||
else if (isouterjoin)
|
else if (bms_overlap(relids, outerjoin_nonnullable))
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
* The qual is attached to an outer join and mentions (some of the)
|
||||||
|
* rels on the nonnullable side. Force the qual to be evaluated
|
||||||
|
* exactly at the level of joining corresponding to the outer join.
|
||||||
|
* We cannot let it get pushed down into the nonnullable side, since
|
||||||
|
* then we'd produce no output rows, rather than the intended single
|
||||||
|
* null-extended row, for any nonnullable-side rows failing the qual.
|
||||||
|
*
|
||||||
|
* Note: an outer-join qual that mentions only nullable-side rels can
|
||||||
|
* be pushed down into the nullable side without changing the join
|
||||||
|
* result, so we treat it the same as an ordinary inner-join qual.
|
||||||
|
*/
|
||||||
relids = qualscope;
|
relids = qualscope;
|
||||||
can_be_equijoin = false;
|
can_be_equijoin = false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* copy to ensure we don't change caller's qualscope set */
|
/*
|
||||||
Relids newrelids = bms_copy(relids);
|
* For a non-outer-join qual, we can evaluate the qual as soon as
|
||||||
|
* (1) we have all the rels it mentions, and (2) we are at or above
|
||||||
|
* any outer joins that can null any of these rels and are below the
|
||||||
|
* syntactic location of the given qual. To enforce the latter, scan
|
||||||
|
* the base rels listed in relids, and merge their outer-join sets
|
||||||
|
* into the clause's own reference list. At the time we are called,
|
||||||
|
* the outerjoinset of each baserel will show exactly those outer
|
||||||
|
* joins that are below the qual in the join tree.
|
||||||
|
*/
|
||||||
|
Relids addrelids = NULL;
|
||||||
Relids tmprelids;
|
Relids tmprelids;
|
||||||
int relno;
|
int relno;
|
||||||
|
|
||||||
can_be_equijoin = true;
|
|
||||||
tmprelids = bms_copy(relids);
|
tmprelids = bms_copy(relids);
|
||||||
while ((relno = bms_first_member(tmprelids)) >= 0)
|
while ((relno = bms_first_member(tmprelids)) >= 0)
|
||||||
{
|
{
|
||||||
RelOptInfo *rel = find_base_rel(root, relno);
|
RelOptInfo *rel = find_base_rel(root, relno);
|
||||||
|
|
||||||
if (!bms_is_subset(rel->outerjoinset, relids))
|
if (rel->outerjoinset != NULL)
|
||||||
{
|
addrelids = bms_add_members(addrelids, rel->outerjoinset);
|
||||||
newrelids = bms_add_members(newrelids, rel->outerjoinset);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Because application of the qual will be delayed by
|
|
||||||
* outer join, we mustn't assume its vars are equal
|
|
||||||
* everywhere.
|
|
||||||
*/
|
|
||||||
can_be_equijoin = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
bms_free(tmprelids);
|
bms_free(tmprelids);
|
||||||
relids = newrelids;
|
|
||||||
/* Should still be a subset of current scope ... */
|
if (bms_is_subset(addrelids, relids))
|
||||||
Assert(bms_is_subset(relids, qualscope));
|
{
|
||||||
|
/* Qual is not affected by any outer-join restriction */
|
||||||
|
can_be_equijoin = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
relids = bms_union(relids, addrelids);
|
||||||
|
/* Should still be a subset of current scope ... */
|
||||||
|
Assert(bms_is_subset(relids, qualscope));
|
||||||
|
/*
|
||||||
|
* Because application of the qual will be delayed by outer join,
|
||||||
|
* we mustn't assume its vars are equal everywhere.
|
||||||
|
*/
|
||||||
|
can_be_equijoin = false;
|
||||||
|
}
|
||||||
|
bms_free(addrelids);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -725,8 +753,7 @@ process_implied_equality(Query *root,
|
|||||||
* taken for an original JOIN/ON clause.
|
* taken for an original JOIN/ON clause.
|
||||||
*/
|
*/
|
||||||
distribute_qual_to_rels(root, (Node *) clause,
|
distribute_qual_to_rels(root, (Node *) clause,
|
||||||
true, false, true,
|
true, true, NULL, relids);
|
||||||
relids);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user