1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* joinrels.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Routines to determine which relations should be joined
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2001-01-24 20:43:33 +01:00
|
|
|
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2001-03-22 05:01:46 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/joinrels.c,v 1.52 2001/03/22 03:59:35 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "optimizer/pathnode.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "optimizer/paths.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
|
|
|
|
static RelOptInfo *make_join_rel(Query *root, RelOptInfo *rel1,
|
2001-03-22 05:01:46 +01:00
|
|
|
RelOptInfo *rel2, JoinType jointype);
|
2000-02-07 05:41:04 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
1999-02-15 04:22:37 +01:00
|
|
|
* make_rels_by_joins
|
2000-02-07 05:41:04 +01:00
|
|
|
* Consider ways to produce join relations containing exactly 'level'
|
2000-09-12 23:07:18 +02:00
|
|
|
* jointree items. (This is one step of the dynamic-programming method
|
2000-02-07 05:41:04 +01:00
|
|
|
* embodied in make_one_rel_by_joins.) Join rel nodes for each feasible
|
2000-09-12 23:07:18 +02:00
|
|
|
* combination of lower-level rels are created and returned in a list.
|
|
|
|
* Implementation paths are created for each such joinrel, too.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2000-09-12 23:07:18 +02:00
|
|
|
* level: level of rels we want to make this time.
|
|
|
|
* joinrels[j], 1 <= j < level, is a list of rels containing j items.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-09-12 23:07:18 +02:00
|
|
|
List *
|
|
|
|
make_rels_by_joins(Query *root, int level, List **joinrels)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-09-12 23:07:18 +02:00
|
|
|
List *result_rels = NIL;
|
|
|
|
List *new_rels;
|
|
|
|
List *nr;
|
1999-08-16 04:17:58 +02:00
|
|
|
List *r;
|
2000-09-12 23:07:18 +02:00
|
|
|
int k;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
/*
|
|
|
|
* First, consider left-sided and right-sided plans, in which rels of
|
2001-03-22 05:01:46 +01:00
|
|
|
* exactly level-1 member relations are joined against initial
|
|
|
|
* relations. We prefer to join using join clauses, but if we find a
|
|
|
|
* rel of level-1 members that has no join clauses, we will generate
|
|
|
|
* Cartesian-product joins against all initial rels not already
|
|
|
|
* contained in it.
|
2000-02-07 05:41:04 +01:00
|
|
|
*
|
2001-03-22 05:01:46 +01:00
|
|
|
* In the first pass (level == 2), we try to join each initial rel to
|
|
|
|
* each initial rel that appears later in joinrels[1]. (The
|
|
|
|
* mirror-image joins are handled automatically by make_join_rel.) In
|
|
|
|
* later passes, we try to join rels of size level-1 from
|
|
|
|
* joinrels[level-1] to each initial rel in joinrels[1].
|
2000-02-07 05:41:04 +01:00
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
foreach(r, joinrels[level - 1])
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-02-18 05:45:36 +01:00
|
|
|
RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
|
2000-02-07 05:41:04 +01:00
|
|
|
List *other_rels;
|
|
|
|
|
|
|
|
if (level == 2)
|
2000-09-12 23:07:18 +02:00
|
|
|
other_rels = lnext(r); /* only consider remaining initial
|
2000-04-12 19:17:23 +02:00
|
|
|
* rels */
|
2000-02-07 05:41:04 +01:00
|
|
|
else
|
2000-09-12 23:07:18 +02:00
|
|
|
other_rels = joinrels[1]; /* consider all initial rels */
|
2000-02-07 05:41:04 +01:00
|
|
|
|
|
|
|
if (old_rel->joininfo != NIL)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* Note that if all available join clauses for this rel
|
|
|
|
* require more than one other rel, we will fail to make any
|
|
|
|
* joins against it here. That's OK; it'll be considered by
|
2001-03-22 05:01:46 +01:00
|
|
|
* "bushy plan" join code in a higher-level pass where we have
|
|
|
|
* those other rels collected into a join rel. See also the
|
|
|
|
* last-ditch case below.
|
2000-02-07 05:41:04 +01:00
|
|
|
*/
|
2000-09-12 23:07:18 +02:00
|
|
|
new_rels = make_rels_by_clause_joins(root,
|
|
|
|
old_rel,
|
|
|
|
other_rels);
|
2000-02-07 05:41:04 +01:00
|
|
|
}
|
|
|
|
else
|
There's a patch attached to fix gcc 2.8.x warnings, except for the
yyerror ones from bison. It also includes a few 'enhancements' to
the C programming style (which are, of course, personal).
The other patch removes the compilation of backend/lib/qsort.c, as
qsort() is a standard function in stdlib.h and can be used any
where else (and it is). It was only used in
backend/optimizer/geqo/geqo_pool.c, backend/optimizer/path/predmig.c,
and backend/storage/page/bufpage.c
> > Some or all of these changes might not be appropriate for v6.3,
since we > > are in beta testing and since they do not affect the
current functionality. > > For those cases, how about submitting
patches based on the final v6.3 > > release?
There's more to come. Please review these patches. I ran the
regression tests and they only failed where this was expected
(random, geo, etc).
Cheers,
Jeroen
1998-03-30 18:47:35 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-02-14 05:57:02 +01:00
|
|
|
/*
|
|
|
|
* Oops, we have a relation that is not joined to any other
|
|
|
|
* relation. Cartesian product time.
|
|
|
|
*/
|
2000-09-12 23:07:18 +02:00
|
|
|
new_rels = make_rels_by_clauseless_joins(root,
|
|
|
|
old_rel,
|
|
|
|
other_rels);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* At levels above 2 we will generate the same joined relation in
|
|
|
|
* multiple ways --- for example (a join b) join c is the same
|
2000-09-12 23:07:18 +02:00
|
|
|
* RelOptInfo as (b join c) join a, though the second case will
|
2001-03-22 05:01:46 +01:00
|
|
|
* add a different set of Paths to it. To avoid making extra work
|
|
|
|
* for subsequent passes, do not enter the same RelOptInfo into
|
|
|
|
* our output list multiple times.
|
2000-09-12 23:07:18 +02:00
|
|
|
*/
|
|
|
|
foreach(nr, new_rels)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
RelOptInfo *jrel = (RelOptInfo *) lfirst(nr);
|
2000-09-12 23:07:18 +02:00
|
|
|
|
|
|
|
if (!ptrMember(jrel, result_rels))
|
|
|
|
result_rels = lcons(jrel, result_rels);
|
There's a patch attached to fix gcc 2.8.x warnings, except for the
yyerror ones from bison. It also includes a few 'enhancements' to
the C programming style (which are, of course, personal).
The other patch removes the compilation of backend/lib/qsort.c, as
qsort() is a standard function in stdlib.h and can be used any
where else (and it is). It was only used in
backend/optimizer/geqo/geqo_pool.c, backend/optimizer/path/predmig.c,
and backend/storage/page/bufpage.c
> > Some or all of these changes might not be appropriate for v6.3,
since we > > are in beta testing and since they do not affect the
current functionality. > > For those cases, how about submitting
patches based on the final v6.3 > > release?
There's more to come. Please review these patches. I ran the
regression tests and they only failed where this was expected
(random, geo, etc).
Cheers,
Jeroen
1998-03-30 18:47:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Now, consider "bushy plans" in which relations of k initial rels
|
|
|
|
* are joined to relations of level-k initial rels, for 2 <= k <=
|
|
|
|
* level-2.
|
2000-02-07 05:41:04 +01:00
|
|
|
*
|
2000-04-12 19:17:23 +02:00
|
|
|
* We only consider bushy-plan joins for pairs of rels where there is a
|
|
|
|
* suitable join clause, in order to avoid unreasonable growth of
|
2000-02-07 05:41:04 +01:00
|
|
|
* planning time.
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
for (k = 2;; k++)
|
1999-05-25 18:15:34 +02:00
|
|
|
{
|
2000-09-12 23:07:18 +02:00
|
|
|
int other_level = level - k;
|
1999-08-16 04:17:58 +02:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Since make_join_rel(x, y) handles both x,y and y,x cases, we
|
|
|
|
* only need to go as far as the halfway point.
|
2000-02-07 05:41:04 +01:00
|
|
|
*/
|
2000-09-12 23:07:18 +02:00
|
|
|
if (k > other_level)
|
2000-02-07 05:41:04 +01:00
|
|
|
break;
|
1999-08-16 04:17:58 +02:00
|
|
|
|
2000-09-12 23:07:18 +02:00
|
|
|
foreach(r, joinrels[k])
|
1999-08-16 04:17:58 +02:00
|
|
|
{
|
2000-09-12 23:07:18 +02:00
|
|
|
RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
|
|
|
|
List *other_rels;
|
|
|
|
List *r2;
|
|
|
|
|
|
|
|
if (old_rel->joininfo == NIL)
|
|
|
|
continue; /* we ignore clauseless joins here */
|
|
|
|
|
|
|
|
if (k == other_level)
|
2001-03-22 05:01:46 +01:00
|
|
|
other_rels = lnext(r); /* only consider remaining rels */
|
2000-09-12 23:07:18 +02:00
|
|
|
else
|
|
|
|
other_rels = joinrels[other_level];
|
|
|
|
|
|
|
|
foreach(r2, other_rels)
|
1999-02-18 01:49:48 +01:00
|
|
|
{
|
2000-09-12 23:07:18 +02:00
|
|
|
RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
|
2000-02-07 05:41:04 +01:00
|
|
|
|
2000-09-12 23:07:18 +02:00
|
|
|
if (nonoverlap_setsi(old_rel->relids, new_rel->relids))
|
|
|
|
{
|
|
|
|
List *i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OK, we can build a rel of the right level from this
|
2001-03-22 05:01:46 +01:00
|
|
|
* pair of rels. Do so if there is at least one
|
|
|
|
* usable join clause.
|
2000-09-12 23:07:18 +02:00
|
|
|
*/
|
|
|
|
foreach(i, old_rel->joininfo)
|
2000-02-07 05:41:04 +01:00
|
|
|
{
|
2000-09-12 23:07:18 +02:00
|
|
|
JoinInfo *joininfo = (JoinInfo *) lfirst(i);
|
|
|
|
|
|
|
|
if (is_subseti(joininfo->unjoined_relids,
|
|
|
|
new_rel->relids))
|
|
|
|
{
|
|
|
|
RelOptInfo *jrel;
|
|
|
|
|
|
|
|
jrel = make_join_rel(root, old_rel, new_rel,
|
|
|
|
JOIN_INNER);
|
|
|
|
/* Avoid making duplicate entries ... */
|
|
|
|
if (!ptrMember(jrel, result_rels))
|
|
|
|
result_rels = lcons(jrel, result_rels);
|
2001-03-22 05:01:46 +01:00
|
|
|
break; /* need not consider more
|
|
|
|
* joininfos */
|
2000-09-12 23:07:18 +02:00
|
|
|
}
|
2000-02-07 05:41:04 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2000-04-27 20:35:04 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Last-ditch effort: if we failed to find any usable joins so far,
|
2001-03-22 05:01:46 +01:00
|
|
|
* force a set of cartesian-product joins to be generated. This
|
2000-04-27 20:35:04 +02:00
|
|
|
* handles the special case where all the available rels have join
|
2001-03-22 05:01:46 +01:00
|
|
|
* clauses but we cannot use any of the joins yet. An example is
|
2000-04-27 20:35:04 +02:00
|
|
|
*
|
|
|
|
* SELECT * FROM a,b,c WHERE (a.f1 + b.f2 + c.f3) = 0;
|
|
|
|
*
|
2001-03-22 05:01:46 +01:00
|
|
|
* The join clause will be usable at level 3, but at level 2 we have no
|
|
|
|
* choice but to make cartesian joins. We consider only left-sided
|
2000-04-27 20:35:04 +02:00
|
|
|
* and right-sided cartesian joins in this case (no bushy).
|
|
|
|
*/
|
2000-09-12 23:07:18 +02:00
|
|
|
if (result_rels == NIL)
|
2000-04-27 20:35:04 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This loop is just like the first one, except we always call
|
2000-04-27 20:35:04 +02:00
|
|
|
* make_rels_by_clauseless_joins().
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
foreach(r, joinrels[level - 1])
|
2000-04-27 20:35:04 +02:00
|
|
|
{
|
|
|
|
RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
|
|
|
|
List *other_rels;
|
|
|
|
|
|
|
|
if (level == 2)
|
2001-03-22 05:01:46 +01:00
|
|
|
other_rels = lnext(r); /* only consider remaining initial
|
|
|
|
* rels */
|
2000-04-27 20:35:04 +02:00
|
|
|
else
|
2001-03-22 05:01:46 +01:00
|
|
|
other_rels = joinrels[1]; /* consider all initial
|
|
|
|
* rels */
|
2000-09-12 23:07:18 +02:00
|
|
|
|
|
|
|
new_rels = make_rels_by_clauseless_joins(root,
|
|
|
|
old_rel,
|
|
|
|
other_rels);
|
|
|
|
|
|
|
|
foreach(nr, new_rels)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
RelOptInfo *jrel = (RelOptInfo *) lfirst(nr);
|
2000-04-27 20:35:04 +02:00
|
|
|
|
2000-09-12 23:07:18 +02:00
|
|
|
if (!ptrMember(jrel, result_rels))
|
|
|
|
result_rels = lcons(jrel, result_rels);
|
|
|
|
}
|
2000-04-27 20:35:04 +02:00
|
|
|
}
|
|
|
|
|
2000-09-12 23:07:18 +02:00
|
|
|
if (result_rels == NIL)
|
2000-04-27 20:35:04 +02:00
|
|
|
elog(ERROR, "make_rels_by_joins: failed to build any %d-way joins",
|
|
|
|
level);
|
|
|
|
}
|
2000-09-12 23:07:18 +02:00
|
|
|
|
|
|
|
return result_rels;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2000-02-07 05:41:04 +01:00
|
|
|
* make_rels_by_clause_joins
|
|
|
|
* Build joins between the given relation 'old_rel' and other relations
|
|
|
|
* that are mentioned within old_rel's joininfo nodes (i.e., relations
|
|
|
|
* that participate in join clauses that 'old_rel' also participates in).
|
2000-09-12 23:07:18 +02:00
|
|
|
* The join rel nodes are returned in a list.
|
2000-02-07 05:41:04 +01:00
|
|
|
*
|
|
|
|
* 'old_rel' is the relation entry for the relation to be joined
|
|
|
|
* 'other_rels': other rels to be considered for joining
|
|
|
|
*
|
2000-09-12 23:07:18 +02:00
|
|
|
* Currently, this is only used with initial rels in other_rels, but it
|
|
|
|
* will work for joining to joinrels too, if the caller ensures there is no
|
2000-02-07 05:41:04 +01:00
|
|
|
* membership overlap between old_rel and the rels in other_rels. (We need
|
2000-09-12 23:07:18 +02:00
|
|
|
* no extra test for overlap for initial rels, since the is_subset test can
|
2000-02-07 05:41:04 +01:00
|
|
|
* only succeed when other_rel is not already part of old_rel.)
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-09-12 23:07:18 +02:00
|
|
|
List *
|
2000-02-07 05:41:04 +01:00
|
|
|
make_rels_by_clause_joins(Query *root,
|
|
|
|
RelOptInfo *old_rel,
|
|
|
|
List *other_rels)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-09-12 23:07:18 +02:00
|
|
|
List *result = NIL;
|
2000-02-07 05:41:04 +01:00
|
|
|
List *i,
|
|
|
|
*j;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
foreach(i, old_rel->joininfo)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-02-07 05:41:04 +01:00
|
|
|
JoinInfo *joininfo = (JoinInfo *) lfirst(i);
|
|
|
|
Relids unjoined_relids = joininfo->unjoined_relids;
|
1999-08-16 04:17:58 +02:00
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
foreach(j, other_rels)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
RelOptInfo *other_rel = (RelOptInfo *) lfirst(j);
|
2000-02-07 05:41:04 +01:00
|
|
|
|
|
|
|
if (is_subseti(unjoined_relids, other_rel->relids))
|
2000-12-18 07:50:51 +01:00
|
|
|
{
|
|
|
|
RelOptInfo *jrel;
|
|
|
|
|
|
|
|
jrel = make_join_rel(root, old_rel, other_rel, JOIN_INNER);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-12-18 07:50:51 +01:00
|
|
|
/*
|
|
|
|
* Avoid entering same joinrel into our output list more
|
|
|
|
* than once. (make_rels_by_joins doesn't really care,
|
|
|
|
* but GEQO does.)
|
|
|
|
*/
|
|
|
|
if (!ptrMember(jrel, result))
|
|
|
|
result = lcons(jrel, result);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
return result;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2000-02-07 05:41:04 +01:00
|
|
|
* make_rels_by_clauseless_joins
|
|
|
|
* Given a relation 'old_rel' and a list of other relations
|
|
|
|
* 'other_rels', create a join relation between 'old_rel' and each
|
|
|
|
* member of 'other_rels' that isn't already included in 'old_rel'.
|
2000-09-12 23:07:18 +02:00
|
|
|
* The join rel nodes are returned in a list.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2000-02-07 05:41:04 +01:00
|
|
|
* 'old_rel' is the relation entry for the relation to be joined
|
|
|
|
* 'other_rels': other rels to be considered for joining
|
1999-08-16 04:17:58 +02:00
|
|
|
*
|
2000-09-12 23:07:18 +02:00
|
|
|
* Currently, this is only used with initial rels in other_rels, but it would
|
2000-02-07 05:41:04 +01:00
|
|
|
* work for joining to joinrels too.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-09-12 23:07:18 +02:00
|
|
|
List *
|
2000-02-07 05:41:04 +01:00
|
|
|
make_rels_by_clauseless_joins(Query *root,
|
|
|
|
RelOptInfo *old_rel,
|
|
|
|
List *other_rels)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-09-12 23:07:18 +02:00
|
|
|
List *result = NIL;
|
1999-08-16 04:17:58 +02:00
|
|
|
List *i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
foreach(i, other_rels)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-02-07 05:41:04 +01:00
|
|
|
RelOptInfo *other_rel = (RelOptInfo *) lfirst(i);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
if (nonoverlap_setsi(other_rel->relids, old_rel->relids))
|
2000-12-18 07:50:51 +01:00
|
|
|
{
|
|
|
|
RelOptInfo *jrel;
|
|
|
|
|
|
|
|
jrel = make_join_rel(root, old_rel, other_rel, JOIN_INNER);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-12-18 07:50:51 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* As long as given other_rels are distinct, don't need to
|
|
|
|
* test to see if jrel is already part of output list.
|
2000-12-18 07:50:51 +01:00
|
|
|
*/
|
|
|
|
result = lcons(jrel, result);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
return result;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-12 23:07:18 +02:00
|
|
|
/*
|
2000-09-29 20:21:41 +02:00
|
|
|
* make_jointree_rel
|
2000-09-12 23:07:18 +02:00
|
|
|
* Find or build a RelOptInfojoin rel representing a specific
|
2001-03-22 05:01:46 +01:00
|
|
|
* jointree item. For JoinExprs, we only consider the construction
|
2000-09-12 23:07:18 +02:00
|
|
|
* path that corresponds exactly to what the user wrote.
|
|
|
|
*/
|
|
|
|
RelOptInfo *
|
2000-09-29 20:21:41 +02:00
|
|
|
make_jointree_rel(Query *root, Node *jtnode)
|
2000-09-12 23:07:18 +02:00
|
|
|
{
|
|
|
|
if (IsA(jtnode, RangeTblRef))
|
|
|
|
{
|
|
|
|
int varno = ((RangeTblRef *) jtnode)->rtindex;
|
|
|
|
|
|
|
|
return get_base_rel(root, varno);
|
|
|
|
}
|
2000-09-29 20:21:41 +02:00
|
|
|
else if (IsA(jtnode, FromExpr))
|
|
|
|
{
|
|
|
|
FromExpr *f = (FromExpr *) jtnode;
|
|
|
|
|
|
|
|
/* Recurse back to multi-way-join planner */
|
|
|
|
return make_fromexpr_rel(root, f);
|
|
|
|
}
|
2000-09-12 23:07:18 +02:00
|
|
|
else if (IsA(jtnode, JoinExpr))
|
|
|
|
{
|
|
|
|
JoinExpr *j = (JoinExpr *) jtnode;
|
|
|
|
RelOptInfo *rel,
|
|
|
|
*lrel,
|
|
|
|
*rrel;
|
|
|
|
|
|
|
|
/* Recurse */
|
2000-09-29 20:21:41 +02:00
|
|
|
lrel = make_jointree_rel(root, j->larg);
|
|
|
|
rrel = make_jointree_rel(root, j->rarg);
|
2000-09-12 23:07:18 +02:00
|
|
|
|
|
|
|
/* Make this join rel */
|
|
|
|
rel = make_join_rel(root, lrel, rrel, j->jointype);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since we are only going to consider this one way to do it,
|
|
|
|
* we're done generating Paths for this joinrel and can now select
|
|
|
|
* the cheapest. In fact we *must* do so now, since next level up
|
|
|
|
* will need it!
|
|
|
|
*/
|
|
|
|
set_cheapest(rel);
|
|
|
|
|
|
|
|
return rel;
|
|
|
|
}
|
|
|
|
else
|
2000-09-29 20:21:41 +02:00
|
|
|
elog(ERROR, "make_jointree_rel: unexpected node type %d",
|
2000-09-12 23:07:18 +02:00
|
|
|
nodeTag(jtnode));
|
|
|
|
return NULL; /* keep compiler quiet */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2000-02-07 05:41:04 +01:00
|
|
|
* make_join_rel
|
|
|
|
* Find or create a join RelOptInfo that represents the join of
|
|
|
|
* the two given rels, and add to it path information for paths
|
|
|
|
* created with the two rels as outer and inner rel.
|
|
|
|
* (The join rel may already contain paths generated from other
|
|
|
|
* pairs of rels that add up to the same set of base rels.)
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-02-07 05:41:04 +01:00
|
|
|
static RelOptInfo *
|
2000-09-12 23:07:18 +02:00
|
|
|
make_join_rel(Query *root, RelOptInfo *rel1, RelOptInfo *rel2,
|
|
|
|
JoinType jointype)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
RelOptInfo *joinrel;
|
|
|
|
List *restrictlist;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
2000-02-07 05:41:04 +01:00
|
|
|
* Find or build the join RelOptInfo, and compute the restrictlist
|
|
|
|
* that goes with this particular joining.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-02-16 01:03:08 +01:00
|
|
|
joinrel = get_join_rel(root, rel1, rel2, jointype, &restrictlist);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
/*
|
2000-09-12 23:07:18 +02:00
|
|
|
* Consider paths using each rel as both outer and inner.
|
2000-02-07 05:41:04 +01:00
|
|
|
*/
|
2000-09-12 23:07:18 +02:00
|
|
|
switch (jointype)
|
|
|
|
{
|
|
|
|
case JOIN_INNER:
|
|
|
|
add_paths_to_joinrel(root, joinrel, rel1, rel2, JOIN_INNER,
|
|
|
|
restrictlist);
|
|
|
|
add_paths_to_joinrel(root, joinrel, rel2, rel1, JOIN_INNER,
|
|
|
|
restrictlist);
|
|
|
|
break;
|
|
|
|
case JOIN_LEFT:
|
|
|
|
add_paths_to_joinrel(root, joinrel, rel1, rel2, JOIN_LEFT,
|
|
|
|
restrictlist);
|
|
|
|
add_paths_to_joinrel(root, joinrel, rel2, rel1, JOIN_RIGHT,
|
|
|
|
restrictlist);
|
|
|
|
break;
|
|
|
|
case JOIN_FULL:
|
|
|
|
add_paths_to_joinrel(root, joinrel, rel1, rel2, JOIN_FULL,
|
|
|
|
restrictlist);
|
|
|
|
add_paths_to_joinrel(root, joinrel, rel2, rel1, JOIN_FULL,
|
|
|
|
restrictlist);
|
|
|
|
break;
|
|
|
|
case JOIN_RIGHT:
|
|
|
|
add_paths_to_joinrel(root, joinrel, rel1, rel2, JOIN_RIGHT,
|
|
|
|
restrictlist);
|
|
|
|
add_paths_to_joinrel(root, joinrel, rel2, rel1, JOIN_LEFT,
|
|
|
|
restrictlist);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "make_join_rel: unsupported join type %d",
|
|
|
|
(int) jointype);
|
|
|
|
break;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
return joinrel;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|