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
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1999-02-15 03:04:58 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/joinrels.c,v 1.22 1999/02/15 02:04:57 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "nodes/pg_list.h"
|
|
|
|
#include "nodes/relation.h"
|
|
|
|
|
|
|
|
#include "optimizer/internal.h"
|
|
|
|
#include "optimizer/cost.h"
|
|
|
|
#include "optimizer/paths.h"
|
|
|
|
#include "optimizer/tlist.h"
|
|
|
|
#include "optimizer/joininfo.h"
|
|
|
|
#include "optimizer/pathnode.h"
|
|
|
|
|
1997-05-20 12:37:26 +02:00
|
|
|
#ifdef USE_RIGHT_SIDED_PLANS
|
1997-09-08 04:41:22 +02:00
|
|
|
bool _use_right_sided_plans_ = true;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-05-20 12:37:26 +02:00
|
|
|
#else
|
1997-09-08 04:41:22 +02:00
|
|
|
bool _use_right_sided_plans_ = false;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-05-20 12:37:26 +02:00
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-02-10 22:02:50 +01:00
|
|
|
static List *find_clause_joins(Query *root, RelOptInfo *outer_rel, List *joininfo_list);
|
|
|
|
static List *find_clauseless_joins(RelOptInfo *outer_rel, List *inner_rels);
|
|
|
|
static RelOptInfo *init_join_rel(RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinInfo * joininfo);
|
1998-09-01 06:40:42 +02:00
|
|
|
static List *new_join_tlist(List *tlist, List *other_relids,
|
1997-09-07 07:04:48 +02:00
|
|
|
int first_resdomno);
|
1997-09-08 23:56:23 +02:00
|
|
|
static List *new_joininfo_list(List *joininfo_list, List *join_relids);
|
1999-02-10 22:02:50 +01:00
|
|
|
static void add_superrels(RelOptInfo *rel, RelOptInfo *super_rel);
|
|
|
|
static bool nonoverlap_rels(RelOptInfo *rel1, RelOptInfo *rel2);
|
1997-09-08 23:56:23 +02:00
|
|
|
static bool nonoverlap_sets(List *s1, List *s2);
|
1999-02-10 22:02:50 +01:00
|
|
|
static void set_joinrel_size(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel,
|
1998-09-01 06:40:42 +02:00
|
|
|
JoinInfo * jinfo);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
1999-02-14 05:57:02 +01:00
|
|
|
* make_new_rels_by_joins
|
1997-09-07 07:04:48 +02:00
|
|
|
* Find all possible joins for each of the outer join relations in
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'outer_rels'. A rel node is created for each possible join relation,
|
1997-09-07 07:04:48 +02:00
|
|
|
* and the resulting list of nodes is returned. If at all possible, only
|
|
|
|
* those relations for which join clauses exist are considered. If none
|
|
|
|
* of these exist for a given relation, all remaining possibilities are
|
|
|
|
* considered.
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'outer_rels' is the list of rel nodes
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns a list of rel nodes corresponding to the new join relations.
|
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
List *
|
1999-02-14 05:57:02 +01:00
|
|
|
make_new_rels_by_joins(Query *root, List *outer_rels)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *joins = NIL;
|
|
|
|
List *join_list = NIL;
|
|
|
|
List *r = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(r, outer_rels)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
RelOptInfo *outer_rel = (RelOptInfo *) lfirst(r);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (!(joins = find_clause_joins(root, outer_rel, outer_rel->joininfo)))
|
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
|
|
|
{
|
1999-02-14 05:57:02 +01:00
|
|
|
/*
|
|
|
|
* Oops, we have a relation that is not joined to any other
|
|
|
|
* relation. Cartesian product time.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
if (BushyPlanFlag)
|
|
|
|
joins = find_clauseless_joins(outer_rel, outer_rels);
|
|
|
|
else
|
1998-08-10 04:26:40 +02:00
|
|
|
joins = find_clauseless_joins(outer_rel, root->base_rel_list);
|
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
|
|
|
|
|
|
|
join_list = nconc(join_list, joins);
|
|
|
|
}
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return join_list;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* find_clause_joins
|
1997-09-07 07:04:48 +02:00
|
|
|
* Determines whether joins can be performed between an outer relation
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'outer_rel' and those relations within 'outer_rel's joininfo nodes
|
|
|
|
* (i.e., relations that participate in join clauses that 'outer_rel'
|
1997-09-07 07:04:48 +02:00
|
|
|
* participates in). This is possible if all but one of the relations
|
|
|
|
* contained within the join clauses of the joininfo node are already
|
1999-02-14 00:22:53 +01:00
|
|
|
* contained within 'outer_rel'.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'outer_rel' is the relation entry for the outer relation
|
|
|
|
* 'joininfo_list' is a list of join clauses which 'outer_rel'
|
1997-09-07 07:04:48 +02:00
|
|
|
* participates in
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns a list of new join relations.
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static List *
|
1999-02-10 22:02:50 +01:00
|
|
|
find_clause_joins(Query *root, RelOptInfo *outer_rel, List *joininfo_list)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *join_list = NIL;
|
|
|
|
List *i = NIL;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
foreach(i, joininfo_list)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
JoinInfo *joininfo = (JoinInfo *) lfirst(i);
|
|
|
|
RelOptInfo *rel;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (!joininfo->inactive)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *other_rels = joininfo->otherrels;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (other_rels != NIL)
|
|
|
|
{
|
|
|
|
if (length(other_rels) == 1)
|
|
|
|
{
|
|
|
|
rel = init_join_rel(outer_rel,
|
|
|
|
get_base_rel(root, lfirsti(other_rels)),
|
|
|
|
joininfo);
|
|
|
|
/* how about right-sided plan ? */
|
|
|
|
if (_use_right_sided_plans_ &&
|
|
|
|
length(outer_rel->relids) > 1)
|
|
|
|
{
|
|
|
|
if (rel != NULL)
|
|
|
|
join_list = lappend(join_list, rel);
|
1999-02-14 05:57:02 +01:00
|
|
|
rel = init_join_rel(get_base_rel(root,
|
|
|
|
lfirsti(other_rels)),
|
1997-09-07 07:04:48 +02:00
|
|
|
outer_rel,
|
|
|
|
joininfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (BushyPlanFlag)
|
|
|
|
{
|
|
|
|
rel = init_join_rel(outer_rel,
|
|
|
|
get_join_rel(root, other_rels),
|
|
|
|
joininfo);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rel = NULL;
|
|
|
|
|
|
|
|
if (rel != NULL)
|
|
|
|
join_list = lappend(join_list, rel);
|
|
|
|
}
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return join_list;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* find_clauseless_joins
|
|
|
|
* Given an outer relation 'outer_rel' and a list of inner relations
|
|
|
|
* 'inner_rels', create a join relation between 'outer_rel' and each
|
|
|
|
* member of 'inner_rels' that isn't already included in 'outer_rel'.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns a list of new join relations.
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static List *
|
1999-02-10 22:02:50 +01:00
|
|
|
find_clauseless_joins(RelOptInfo *outer_rel, List *inner_rels)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
RelOptInfo *inner_rel;
|
1997-09-08 04:41:22 +02:00
|
|
|
List *t_list = NIL;
|
|
|
|
List *i = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(i, inner_rels)
|
|
|
|
{
|
1998-07-18 06:22:52 +02:00
|
|
|
inner_rel = (RelOptInfo *) lfirst(i);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (nonoverlap_rels(inner_rel, outer_rel))
|
|
|
|
{
|
1999-02-15 03:04:58 +01:00
|
|
|
t_list = lappend(t_list,
|
|
|
|
init_join_rel(outer_rel,
|
|
|
|
inner_rel,
|
|
|
|
(JoinInfo *) NULL));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return t_list;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* init_join_rel
|
1997-09-07 07:04:48 +02:00
|
|
|
* Creates and initializes a new join relation.
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'outer_rel' and 'inner_rel' are relation nodes for the relations to be
|
1997-09-07 07:04:48 +02:00
|
|
|
* joined
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'joininfo' is the joininfo node(join clause) containing both
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'outer_rel' and 'inner_rel', if any exists
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns the new join relation node.
|
|
|
|
*/
|
1998-07-18 06:22:52 +02:00
|
|
|
static RelOptInfo *
|
1999-02-10 22:02:50 +01:00
|
|
|
init_join_rel(RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinInfo * joininfo)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
RelOptInfo *joinrel = makeNode(RelOptInfo);
|
1997-09-08 04:41:22 +02:00
|
|
|
List *joinrel_joininfo_list = NIL;
|
|
|
|
List *new_outer_tlist;
|
|
|
|
List *new_inner_tlist;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new tlist by removing irrelevant elements from both tlists
|
|
|
|
* of the outer and inner join relations and then merging the results
|
|
|
|
* together.
|
|
|
|
*/
|
1999-02-03 22:18:02 +01:00
|
|
|
new_outer_tlist = new_join_tlist(outer_rel->targetlist, /* XXX 1-based attnos */
|
1997-09-07 07:04:48 +02:00
|
|
|
inner_rel->relids, 1);
|
1999-02-03 22:18:02 +01:00
|
|
|
new_inner_tlist = new_join_tlist(inner_rel->targetlist, /* XXX 1-based attnos */
|
1997-09-07 07:04:48 +02:00
|
|
|
outer_rel->relids,
|
|
|
|
length(new_outer_tlist) + 1);
|
|
|
|
|
|
|
|
joinrel->relids = NIL;
|
|
|
|
joinrel->indexed = false;
|
|
|
|
joinrel->pages = 0;
|
|
|
|
joinrel->tuples = 0;
|
|
|
|
joinrel->width = 0;
|
|
|
|
/* joinrel->targetlist = NIL;*/
|
|
|
|
joinrel->pathlist = NIL;
|
|
|
|
joinrel->cheapestpath = (Path *) NULL;
|
|
|
|
joinrel->pruneable = true;
|
|
|
|
joinrel->classlist = NULL;
|
|
|
|
joinrel->relam = InvalidOid;
|
|
|
|
joinrel->ordering = NULL;
|
1999-02-03 21:15:53 +01:00
|
|
|
joinrel->restrictinfo = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
joinrel->joininfo = NULL;
|
|
|
|
joinrel->innerjoin = NIL;
|
|
|
|
joinrel->superrels = NIL;
|
|
|
|
|
|
|
|
joinrel->relids = lcons(outer_rel->relids, /* ??? aren't they lists?
|
|
|
|
* -ay */
|
|
|
|
lcons(inner_rel->relids, NIL));
|
|
|
|
|
|
|
|
new_outer_tlist = nconc(new_outer_tlist, new_inner_tlist);
|
|
|
|
joinrel->targetlist = new_outer_tlist;
|
|
|
|
|
|
|
|
if (joininfo)
|
|
|
|
{
|
1999-02-03 21:15:53 +01:00
|
|
|
joinrel->restrictinfo = joininfo->jinfo_restrictinfo;
|
1997-09-07 07:04:48 +02:00
|
|
|
if (BushyPlanFlag)
|
|
|
|
joininfo->inactive = true;
|
|
|
|
}
|
|
|
|
|
1999-02-03 22:18:02 +01:00
|
|
|
joinrel_joininfo_list = new_joininfo_list(append(outer_rel->joininfo, inner_rel->joininfo),
|
1997-09-07 07:04:48 +02:00
|
|
|
intAppend(outer_rel->relids, inner_rel->relids));
|
|
|
|
|
|
|
|
joinrel->joininfo = joinrel_joininfo_list;
|
|
|
|
|
|
|
|
set_joinrel_size(joinrel, outer_rel, inner_rel, joininfo);
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return joinrel;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* new_join_tlist
|
1997-09-07 07:04:48 +02:00
|
|
|
* Builds a join relations's target list by keeping those elements that
|
|
|
|
* will be in the final target list and any other elements that are still
|
|
|
|
* needed for future joins. For a target list entry to still be needed
|
|
|
|
* for future joins, its 'joinlist' field must not be empty after removal
|
1999-02-14 00:22:53 +01:00
|
|
|
* of all relids in 'other_relids'.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'tlist' is the target list of one of the join relations
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'other_relids' is a list of relids contained within the other
|
1997-09-07 07:04:48 +02:00
|
|
|
* join relation
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'first_resdomno' is the resdom number to use for the first created
|
1997-09-07 07:04:48 +02:00
|
|
|
* target list entry
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns the new target list.
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static List *
|
1997-09-08 23:56:23 +02:00
|
|
|
new_join_tlist(List *tlist,
|
|
|
|
List *other_relids,
|
1997-09-07 07:04:48 +02:00
|
|
|
int first_resdomno)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int resdomno = first_resdomno - 1;
|
|
|
|
TargetEntry *xtl = NULL;
|
|
|
|
List *t_list = NIL;
|
|
|
|
List *i = NIL;
|
|
|
|
List *join_list = NIL;
|
|
|
|
bool in_final_tlist = false;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(i, tlist)
|
|
|
|
{
|
|
|
|
xtl = lfirst(i);
|
1999-02-15 03:04:58 +01:00
|
|
|
/* XXX surely this is wrong? join_list is never changed? tgl 2/99 */
|
1997-09-07 07:04:48 +02:00
|
|
|
in_final_tlist = (join_list == NIL);
|
|
|
|
if (in_final_tlist)
|
|
|
|
{
|
|
|
|
resdomno += 1;
|
1999-02-15 03:04:58 +01:00
|
|
|
t_list = lappend(t_list,
|
|
|
|
create_tl_element(get_expr(xtl), resdomno));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return t_list;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* new_joininfo_list
|
1997-09-07 07:04:48 +02:00
|
|
|
* Builds a join relation's joininfo list by checking for join clauses
|
|
|
|
* which still need to used in future joins involving this relation. A
|
|
|
|
* join clause is still needed if there are still relations in the clause
|
|
|
|
* not contained in the list of relations comprising this join relation.
|
|
|
|
* New joininfo nodes are only created and added to
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'current_joininfo_list' if a node for a particular join hasn't already
|
1997-09-07 07:04:48 +02:00
|
|
|
* been created.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'current_joininfo_list' contains a list of those joininfo nodes that
|
1997-09-07 07:04:48 +02:00
|
|
|
* have already been built
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'joininfo_list' is the list of join clauses involving this relation
|
|
|
|
* 'join_relids' is a list of relids corresponding to the relations
|
1997-09-07 07:04:48 +02:00
|
|
|
* currently being joined
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns a list of joininfo nodes, new and old.
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static List *
|
1997-09-08 23:56:23 +02:00
|
|
|
new_joininfo_list(List *joininfo_list, List *join_relids)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *current_joininfo_list = NIL;
|
|
|
|
List *new_otherrels = NIL;
|
1998-09-01 06:40:42 +02:00
|
|
|
JoinInfo *other_joininfo = (JoinInfo *) NULL;
|
1997-09-08 04:41:22 +02:00
|
|
|
List *xjoininfo = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(xjoininfo, joininfo_list)
|
1997-02-20 03:54:09 +01:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *or;
|
1998-09-01 06:40:42 +02:00
|
|
|
JoinInfo *joininfo = (JoinInfo *) lfirst(xjoininfo);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
new_otherrels = joininfo->otherrels;
|
|
|
|
foreach(or, new_otherrels)
|
|
|
|
{
|
|
|
|
if (intMember(lfirsti(or), join_relids))
|
|
|
|
new_otherrels = lremove((void *) lfirst(or), new_otherrels);
|
|
|
|
}
|
|
|
|
joininfo->otherrels = new_otherrels;
|
|
|
|
if (new_otherrels != NIL)
|
|
|
|
{
|
|
|
|
other_joininfo = joininfo_member(new_otherrels,
|
|
|
|
current_joininfo_list);
|
|
|
|
if (other_joininfo)
|
|
|
|
{
|
1999-02-03 22:18:02 +01:00
|
|
|
other_joininfo->jinfo_restrictinfo = (List *) LispUnion(joininfo->jinfo_restrictinfo,
|
1999-02-03 21:15:53 +01:00
|
|
|
other_joininfo->jinfo_restrictinfo);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
other_joininfo = makeNode(JoinInfo);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-02-03 21:15:53 +01:00
|
|
|
other_joininfo->otherrels = joininfo->otherrels;
|
|
|
|
other_joininfo->jinfo_restrictinfo = joininfo->jinfo_restrictinfo;
|
|
|
|
other_joininfo->mergejoinable = joininfo->mergejoinable;
|
|
|
|
other_joininfo->hashjoinable = joininfo->hashjoinable;
|
1997-09-07 07:04:48 +02:00
|
|
|
other_joininfo->inactive = false;
|
|
|
|
|
|
|
|
current_joininfo_list = lcons(other_joininfo,
|
|
|
|
current_joininfo_list);
|
|
|
|
}
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return current_joininfo_list;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* add_new_joininfos
|
1997-09-07 07:04:48 +02:00
|
|
|
* For each new join relation, create new joininfos that
|
|
|
|
* use the join relation as inner relation, and add
|
|
|
|
* the new joininfos to those rel nodes that still
|
|
|
|
* have joins with the join relation.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* 'joinrels' is a list of join relations.
|
|
|
|
*
|
|
|
|
* Modifies the joininfo field of appropriate rel nodes.
|
|
|
|
*/
|
|
|
|
void
|
1997-09-08 23:56:23 +02:00
|
|
|
add_new_joininfos(Query *root, List *joinrels, List *outerrels)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *xjoinrel = NIL;
|
|
|
|
List *xrelid = NIL;
|
|
|
|
List *xrel = NIL;
|
|
|
|
List *xjoininfo = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(xjoinrel, joinrels)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
RelOptInfo *joinrel = (RelOptInfo *) lfirst(xjoinrel);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(xrelid, joinrel->relids)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relid relid = (Relid) lfirst(xrelid);
|
1998-09-01 06:40:42 +02:00
|
|
|
RelOptInfo *rel = get_join_rel(root, relid);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
add_superrels(rel, joinrel);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
foreach(xjoinrel, joinrels)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
RelOptInfo *joinrel = (RelOptInfo *) lfirst(xjoinrel);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(xjoininfo, joinrel->joininfo)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
JoinInfo *joininfo = (JoinInfo *) lfirst(xjoininfo);
|
1997-09-08 04:41:22 +02:00
|
|
|
List *other_rels = joininfo->otherrels;
|
1999-02-03 21:15:53 +01:00
|
|
|
List *restrict_info = joininfo->jinfo_restrictinfo;
|
1998-08-04 18:44:31 +02:00
|
|
|
bool mergejoinable = joininfo->mergejoinable;
|
1997-09-08 04:41:22 +02:00
|
|
|
bool hashjoinable = joininfo->hashjoinable;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(xrelid, other_rels)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relid relid = (Relid) lfirst(xrelid);
|
1998-09-01 06:40:42 +02:00
|
|
|
RelOptInfo *rel = get_join_rel(root, relid);
|
1997-09-08 04:41:22 +02:00
|
|
|
List *super_rels = rel->superrels;
|
|
|
|
List *xsuper_rel = NIL;
|
1998-09-01 06:40:42 +02:00
|
|
|
JoinInfo *new_joininfo = makeNode(JoinInfo);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
new_joininfo->otherrels = joinrel->relids;
|
1999-02-03 21:15:53 +01:00
|
|
|
new_joininfo->jinfo_restrictinfo = restrict_info;
|
1998-08-04 18:44:31 +02:00
|
|
|
new_joininfo->mergejoinable = mergejoinable;
|
1997-09-07 07:04:48 +02:00
|
|
|
new_joininfo->hashjoinable = hashjoinable;
|
|
|
|
new_joininfo->inactive = false;
|
1999-02-03 22:18:02 +01:00
|
|
|
rel->joininfo = lappend(rel->joininfo, new_joininfo);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(xsuper_rel, super_rels)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
RelOptInfo *super_rel = (RelOptInfo *) lfirst(xsuper_rel);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (nonoverlap_rels(super_rel, joinrel))
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *new_relids = super_rel->relids;
|
1999-02-03 22:18:02 +01:00
|
|
|
JoinInfo *other_joininfo = joininfo_member(new_relids,
|
1997-09-07 07:04:48 +02:00
|
|
|
joinrel->joininfo);
|
|
|
|
|
|
|
|
if (other_joininfo)
|
|
|
|
{
|
1999-02-03 22:18:02 +01:00
|
|
|
other_joininfo->jinfo_restrictinfo = (List *) LispUnion(restrict_info,
|
1999-02-03 21:15:53 +01:00
|
|
|
other_joininfo->jinfo_restrictinfo);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
JoinInfo *new_joininfo = makeNode(JoinInfo);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
new_joininfo->otherrels = new_relids;
|
1999-02-03 21:15:53 +01:00
|
|
|
new_joininfo->jinfo_restrictinfo = restrict_info;
|
1998-08-04 18:44:31 +02:00
|
|
|
new_joininfo->mergejoinable = mergejoinable;
|
1997-09-07 07:04:48 +02:00
|
|
|
new_joininfo->hashjoinable = hashjoinable;
|
|
|
|
new_joininfo->inactive = false;
|
1999-02-03 22:18:02 +01:00
|
|
|
joinrel->joininfo = lappend(joinrel->joininfo,
|
1997-09-07 07:04:48 +02:00
|
|
|
new_joininfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
foreach(xrel, outerrels)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
RelOptInfo *rel = (RelOptInfo *) lfirst(xrel);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
rel->superrels = NIL;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* final_join_rels
|
1997-09-07 07:04:48 +02:00
|
|
|
* Find the join relation that includes all the original
|
|
|
|
* relations, i.e. the final join result.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'join_rel_list' is a list of join relations.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Returns the list of final join relations.
|
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
List *
|
1997-09-08 23:56:23 +02:00
|
|
|
final_join_rels(List *join_rel_list)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *xrel = NIL;
|
|
|
|
List *t_list = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* find the relations that has no further joins, i.e., its joininfos
|
|
|
|
* all have otherrels nil.
|
|
|
|
*/
|
|
|
|
foreach(xrel, join_rel_list)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
RelOptInfo *rel = (RelOptInfo *) lfirst(xrel);
|
1997-09-08 04:41:22 +02:00
|
|
|
List *xjoininfo = NIL;
|
|
|
|
bool final = true;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(xjoininfo, rel->joininfo)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
JoinInfo *joininfo = (JoinInfo *) lfirst(xjoininfo);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (joininfo->otherrels != NIL)
|
|
|
|
{
|
|
|
|
final = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (final)
|
|
|
|
{
|
1999-02-15 03:04:58 +01:00
|
|
|
t_list = lappend(t_list, rel);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return t_list;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* add_superrels
|
1997-09-07 07:04:48 +02:00
|
|
|
* add rel to the temporary property list superrels.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* 'rel' a rel node
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'super_rel' rel node of a join relation that includes rel
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Modifies the superrels field of rel
|
|
|
|
*/
|
|
|
|
static void
|
1999-02-10 22:02:50 +01:00
|
|
|
add_superrels(RelOptInfo *rel, RelOptInfo *super_rel)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
rel->superrels = lappend(rel->superrels, super_rel);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* nonoverlap_rels
|
1997-09-07 07:04:48 +02:00
|
|
|
* test if two join relations overlap, i.e., includes the same
|
|
|
|
* relation.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* 'rel1' and 'rel2' are two join relations
|
|
|
|
*
|
|
|
|
* Returns non-nil if rel1 and rel2 do not overlap.
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static bool
|
1999-02-10 22:02:50 +01:00
|
|
|
nonoverlap_rels(RelOptInfo *rel1, RelOptInfo *rel2)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
return nonoverlap_sets(rel1->relids, rel2->relids);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
static bool
|
1997-09-08 23:56:23 +02:00
|
|
|
nonoverlap_sets(List *s1, List *s2)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *x = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(x, s1)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int e = lfirsti(x);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (intMember(e, s2))
|
1998-09-01 05:29:17 +02:00
|
|
|
return false;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-09-01 05:29:17 +02:00
|
|
|
return true;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-02-10 22:02:50 +01:00
|
|
|
set_joinrel_size(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinInfo * jinfo)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int ntuples;
|
|
|
|
float selec;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* voodoo magic. but better than a size of 0. I have no idea why we
|
|
|
|
* didn't set the size before. -ay 2/95
|
|
|
|
*/
|
|
|
|
if (jinfo == NULL)
|
|
|
|
{
|
|
|
|
/* worst case: the cartesian product */
|
|
|
|
ntuples = outer_rel->tuples * inner_rel->tuples;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-02-03 21:15:53 +01:00
|
|
|
selec = product_selec(jinfo->jinfo_restrictinfo);
|
1997-09-07 07:04:48 +02:00
|
|
|
/* ntuples = Min(outer_rel->tuples,inner_rel->tuples) * selec; */
|
|
|
|
ntuples = outer_rel->tuples * inner_rel->tuples * selec;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* I bet sizes less than 1 will screw up optimization so make the best
|
|
|
|
* case 1 instead of 0 - jolly
|
|
|
|
*/
|
|
|
|
if (ntuples < 1)
|
|
|
|
ntuples = 1;
|
|
|
|
|
|
|
|
joinrel->tuples = ntuples;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|