1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* joinutils.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Utilities for matching and building join and path keys
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1999-02-20 17:28:20 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/pathkeys.c,v 1.2 1999/02/20 16:28:20 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "nodes/pg_list.h"
|
|
|
|
#include "nodes/relation.h"
|
|
|
|
#include "nodes/plannodes.h"
|
|
|
|
|
|
|
|
#include "optimizer/internal.h"
|
|
|
|
#include "optimizer/paths.h"
|
|
|
|
#include "optimizer/var.h"
|
|
|
|
#include "optimizer/keys.h"
|
|
|
|
#include "optimizer/tlist.h"
|
|
|
|
#include "optimizer/joininfo.h"
|
|
|
|
#include "optimizer/ordering.h"
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
static int match_pathkey_joinkeys(List *pathkey, List *joinkeys,
|
1999-02-19 03:05:20 +01:00
|
|
|
int outer_or_inner);
|
1999-02-19 06:18:06 +01:00
|
|
|
static bool joinkeys_pathkeys_match(List *joinkeys, List *pathkey,
|
1999-02-19 03:05:20 +01:00
|
|
|
int outer_or_inner);
|
1999-02-10 18:14:32 +01:00
|
|
|
static List *new_join_pathkey(List *subkeys, List *considered_subkeys,
|
|
|
|
List *join_rel_tlist, List *joinclauses);
|
1998-09-01 06:40:42 +02:00
|
|
|
static List *new_matching_subkeys(Var *subkey, List *considered_subkeys,
|
1999-02-10 18:14:32 +01:00
|
|
|
List *join_rel_tlist, List *joinclauses);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-02-20 17:28:20 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Explanation of Path.pathkeys
|
|
|
|
*
|
|
|
|
* This structure is a List of List of Var nodes that represent the sort
|
|
|
|
* order of the result generated by the Path.
|
|
|
|
*
|
|
|
|
* In single/base relation RelOptInfo's, the Path's represent various ways
|
|
|
|
* of generate the relation. Sequential scan Paths have a NIL pathkeys.
|
|
|
|
* Index scans have Path.pathkeys that represent the chosen index. A
|
|
|
|
* single-key index pathkeys would be { {tab1_indexkey1} }. The pathkeys
|
|
|
|
* entry for a multi-key index would be { {tab1_indexkey1}, {tab1_indexkey2} }.
|
|
|
|
*
|
|
|
|
* Multi-relation RelOptInfo Path's are more complicated. Mergejoins are
|
|
|
|
* only performed with equajoins("="). Because of this, the multi-relation
|
|
|
|
* path actually has more than one ordering. For example, a mergejoin Path
|
|
|
|
* of "tab1.col1 = tab2.col1" would generate a pathkeys of
|
|
|
|
* { {tab1.col1, tab2.col1} }. This allows future joins to use either Var
|
|
|
|
* as a pre-sorted key to prevent Mergejoins from having to re-sort the Path.
|
|
|
|
* They are equal, so they are both primary sort keys. This is why pathkeys
|
|
|
|
* is a List of Lists.
|
|
|
|
*/
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/****************************************************************************
|
1997-09-07 07:04:48 +02:00
|
|
|
* KEY COMPARISONS
|
1996-07-09 08:22:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-19 06:18:06 +01:00
|
|
|
* order_joinkeys_by_pathkeys
|
1997-09-07 07:04:48 +02:00
|
|
|
* Attempts to match the keys of a path against the keys of join clauses.
|
|
|
|
* This is done by looking for a matching join key in 'joinkeys' for
|
1999-02-10 04:52:54 +01:00
|
|
|
* every path key in the list 'path.keys'. If there is a matching join key
|
1997-09-07 07:04:48 +02:00
|
|
|
* (not necessarily unique) for every path key, then the list of
|
|
|
|
* corresponding join keys and join clauses are returned in the order in
|
|
|
|
* which the keys matched the path keys.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'pathkeys' is a list of path keys:
|
1997-09-07 07:04:48 +02:00
|
|
|
* ( ( (var) (var) ... ) ( (var) ... ) )
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'joinkeys' is a list of join keys:
|
1997-09-07 07:04:48 +02:00
|
|
|
* ( (outer inner) (outer inner) ... )
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'joinclauses' is a list of clauses corresponding to the join keys in
|
1997-09-07 07:04:48 +02:00
|
|
|
* 'joinkeys'
|
1999-02-19 03:05:20 +01:00
|
|
|
* 'outer_or_inner' is a flag that selects the desired subkey of a join key
|
1997-09-07 07:04:48 +02:00
|
|
|
* in 'joinkeys'
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns the join keys and corresponding join clauses in a list if all
|
|
|
|
* of the path keys were matched:
|
1997-09-07 07:04:48 +02:00
|
|
|
* (
|
|
|
|
* ( (outerkey0 innerkey0) ... (outerkeyN innerkeyN) )
|
|
|
|
* ( clause0 ... clauseN )
|
|
|
|
* )
|
1996-07-09 08:22:35 +02:00
|
|
|
* and nil otherwise.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns a list of matched join keys and a list of matched join clauses
|
|
|
|
* in matchedJoinClausesPtr. - ay 11/94
|
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
List *
|
1999-02-19 06:18:06 +01:00
|
|
|
order_joinkeys_by_pathkeys(List *pathkeys,
|
1997-09-08 23:56:23 +02:00
|
|
|
List *joinkeys,
|
|
|
|
List *joinclauses,
|
1999-02-19 03:05:20 +01:00
|
|
|
int outer_or_inner,
|
1997-09-08 23:56:23 +02:00
|
|
|
List **matchedJoinClausesPtr)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *matched_joinkeys = NIL;
|
|
|
|
List *matched_joinclauses = NIL;
|
|
|
|
List *pathkey = NIL;
|
|
|
|
List *i = NIL;
|
|
|
|
int matched_joinkey_index = -1;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(i, pathkeys)
|
|
|
|
{
|
|
|
|
pathkey = lfirst(i);
|
1999-02-19 03:05:20 +01:00
|
|
|
matched_joinkey_index = match_pathkey_joinkeys(pathkey, joinkeys,
|
|
|
|
outer_or_inner);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (matched_joinkey_index != -1)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *xjoinkey = nth(matched_joinkey_index, joinkeys);
|
|
|
|
List *joinclause = nth(matched_joinkey_index, joinclauses);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-02-19 06:18:06 +01:00
|
|
|
matched_joinkeys = lappend(matched_joinkeys, xjoinkey);
|
|
|
|
matched_joinclauses = lappend(matched_joinclauses, joinclause);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
1999-02-19 06:18:06 +01:00
|
|
|
{
|
|
|
|
*matchedJoinClausesPtr = NIL;
|
1998-09-01 05:29:17 +02:00
|
|
|
return NIL;
|
1999-02-19 06:18:06 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1999-02-19 06:18:06 +01:00
|
|
|
*matchedJoinClausesPtr = matched_joinclauses;
|
|
|
|
return matched_joinkeys;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-02-19 03:05:20 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* match_pathkey_joinkeys
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns the 0-based index into 'joinkeys' of the first joinkey whose
|
|
|
|
* outer or inner subkey matches any subkey of 'pathkey'.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
static int
|
1997-09-08 23:56:23 +02:00
|
|
|
match_pathkey_joinkeys(List *pathkey,
|
|
|
|
List *joinkeys,
|
1999-02-19 03:05:20 +01:00
|
|
|
int outer_or_inner)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Var *path_subkey;
|
|
|
|
int pos;
|
|
|
|
List *i = NIL;
|
|
|
|
List *x = NIL;
|
|
|
|
JoinKey *jk;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(i, pathkey)
|
|
|
|
{
|
|
|
|
path_subkey = (Var *) lfirst(i);
|
|
|
|
pos = 0;
|
|
|
|
foreach(x, joinkeys)
|
|
|
|
{
|
|
|
|
jk = (JoinKey *) lfirst(x);
|
|
|
|
if (var_equal(path_subkey,
|
1999-02-19 03:05:20 +01:00
|
|
|
extract_join_key(jk, outer_or_inner)))
|
1998-09-01 05:29:17 +02:00
|
|
|
return pos;
|
1997-09-07 07:04:48 +02:00
|
|
|
pos++;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1998-09-01 06:40:42 +02:00
|
|
|
return -1; /* no index found */
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-02-19 03:05:20 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-19 06:18:06 +01:00
|
|
|
* get_cheapest_path_for_joinkeys
|
1997-09-07 07:04:48 +02:00
|
|
|
* Attempts to find a path in 'paths' whose keys match a set of join
|
|
|
|
* keys 'joinkeys'. To match,
|
|
|
|
* 1. the path node ordering must equal 'ordering'.
|
|
|
|
* 2. each subkey of a given path must match(i.e., be(var_equal) to) the
|
|
|
|
* appropriate subkey of the corresponding join key in 'joinkeys',
|
|
|
|
* i.e., the Nth path key must match its subkeys against the subkey of
|
|
|
|
* the Nth join key in 'joinkeys'.
|
|
|
|
*
|
|
|
|
* 'joinkeys' is the list of key pairs to which the path keys must be
|
|
|
|
* matched
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'ordering' is the ordering of the(outer) path to which 'joinkeys'
|
1997-09-07 07:04:48 +02:00
|
|
|
* must correspond
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'paths' is a list of(inner) paths which are to be matched against
|
1997-09-07 07:04:48 +02:00
|
|
|
* each join key in 'joinkeys'
|
1999-02-19 03:05:20 +01:00
|
|
|
* 'outer_or_inner' is a flag that selects the desired subkey of a join key
|
1997-09-07 07:04:48 +02:00
|
|
|
* in 'joinkeys'
|
|
|
|
*
|
1999-02-19 03:05:20 +01:00
|
|
|
* Find the cheapest path that matches the join keys
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
Path *
|
1999-02-19 06:18:06 +01:00
|
|
|
get_cheapest_path_for_joinkeys(List *joinkeys,
|
|
|
|
PathOrder *ordering,
|
|
|
|
List *paths,
|
|
|
|
int outer_or_inner)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Path *matched_path = NULL;
|
|
|
|
List *i = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(i, paths)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Path *path = (Path *) lfirst(i);
|
1999-02-19 06:18:06 +01:00
|
|
|
int better_sort, better_key;
|
1999-02-11 15:59:09 +01:00
|
|
|
|
1999-02-19 06:18:06 +01:00
|
|
|
if (joinkeys_pathkeys_match(joinkeys, path->pathkeys, outer_or_inner) &&
|
|
|
|
length(joinkeys) == length(path->pathkeys) &&
|
|
|
|
pathorder_match(ordering, path->pathorder, &better_sort) &&
|
|
|
|
better_sort == 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
if (matched_path)
|
|
|
|
if (path->path_cost < matched_path->path_cost)
|
|
|
|
matched_path = path;
|
|
|
|
else
|
|
|
|
matched_path = path;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
return matched_path;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* extract_path_keys
|
1997-09-07 07:04:48 +02:00
|
|
|
* Builds a subkey list for a path by pulling one of the subkeys from
|
|
|
|
* a list of join keys 'joinkeys' and then finding the var node in the
|
|
|
|
* target list 'tlist' that corresponds to that subkey.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'joinkeys' is a list of join key pairs
|
|
|
|
* 'tlist' is a relation target list
|
1999-02-19 03:05:20 +01:00
|
|
|
* 'outer_or_inner' is a flag that selects the desired subkey of a join key
|
1997-09-07 07:04:48 +02:00
|
|
|
* in 'joinkeys'
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns a list of pathkeys: ((tlvar1)(tlvar2)...(tlvarN)).
|
1999-02-10 04:52:54 +01:00
|
|
|
* It is a list of lists because of multi-key indexes.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
List *
|
1997-09-08 23:56:23 +02:00
|
|
|
extract_path_keys(List *joinkeys,
|
|
|
|
List *tlist,
|
1999-02-19 03:05:20 +01:00
|
|
|
int outer_or_inner)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *pathkeys = NIL;
|
|
|
|
List *jk;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(jk, joinkeys)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
JoinKey *jkey = (JoinKey *) lfirst(jk);
|
|
|
|
Var *var,
|
|
|
|
*key;
|
|
|
|
List *p;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
|
|
|
* find the right Var in the target list for this key
|
|
|
|
*/
|
1999-02-19 03:05:20 +01:00
|
|
|
var = (Var *) extract_join_key(jkey, outer_or_inner);
|
1999-02-15 06:56:07 +01:00
|
|
|
key = (Var *) matching_tlist_var(var, tlist);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
1999-02-10 18:14:32 +01:00
|
|
|
* Include it in the pathkeys list if we haven't already done so
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
foreach(p, pathkeys)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Var *pkey = lfirst((List *) lfirst(p)); /* XXX fix me */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (key == pkey)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p != NIL)
|
|
|
|
continue; /* key already in pathkeys */
|
|
|
|
|
1999-02-03 22:18:02 +01:00
|
|
|
pathkeys = lappend(pathkeys, lcons(key, NIL));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-09-01 05:29:17 +02:00
|
|
|
return pathkeys;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-19 03:05:20 +01:00
|
|
|
/*
|
1999-02-19 06:18:06 +01:00
|
|
|
* joinkeys_pathkeys_match
|
1999-02-19 03:05:20 +01:00
|
|
|
*/
|
|
|
|
static bool
|
1999-02-19 06:18:06 +01:00
|
|
|
joinkeys_pathkeys_match(List *joinkeys, List *pathkey, int outer_or_inner)
|
1999-02-19 03:05:20 +01:00
|
|
|
{
|
|
|
|
JoinKey *xjoinkey;
|
|
|
|
Var *temp;
|
|
|
|
Var *tempkey = NULL;
|
|
|
|
bool found = false;
|
|
|
|
List *i = NIL;
|
|
|
|
List *j = NIL;
|
|
|
|
|
|
|
|
foreach(i, joinkeys)
|
|
|
|
{
|
|
|
|
xjoinkey = (JoinKey *) lfirst(i);
|
|
|
|
found = false;
|
|
|
|
foreach(j, pathkey)
|
|
|
|
{
|
|
|
|
temp = (Var *) lfirst((List *) lfirst(j));
|
|
|
|
if (temp == NULL)
|
|
|
|
continue;
|
|
|
|
tempkey = extract_join_key(xjoinkey, outer_or_inner);
|
|
|
|
if (var_equal(tempkey, temp))
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found == false)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/****************************************************************************
|
1997-09-07 07:04:48 +02:00
|
|
|
* NEW PATHKEY FORMATION
|
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_pathkeys
|
1997-09-07 07:04:48 +02:00
|
|
|
* Find the path keys for a join relation by finding all vars in the list
|
|
|
|
* of join clauses 'joinclauses' such that:
|
|
|
|
* (1) the var corresponding to the outer join relation is a
|
|
|
|
* key on the outer path
|
|
|
|
* (2) the var appears in the target list of the join relation
|
|
|
|
* In other words, add to each outer path key the inner path keys that
|
|
|
|
* are required for qualification.
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'outer_pathkeys' is the list of the outer path's path keys
|
|
|
|
* 'join_rel_tlist' is the target list of the join relation
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'joinclauses' is the list of restricting join clauses
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
|
|
|
* Returns the list of new path keys.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
List *
|
1997-09-08 23:56:23 +02:00
|
|
|
new_join_pathkeys(List *outer_pathkeys,
|
|
|
|
List *join_rel_tlist,
|
|
|
|
List *joinclauses)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *outer_pathkey = NIL;
|
|
|
|
List *t_list = NIL;
|
|
|
|
List *x;
|
|
|
|
List *i = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(i, outer_pathkeys)
|
|
|
|
{
|
|
|
|
outer_pathkey = lfirst(i);
|
1999-02-09 18:03:14 +01:00
|
|
|
x = new_join_pathkey(outer_pathkey, NIL, join_rel_tlist, joinclauses);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (x != NIL)
|
|
|
|
t_list = lappend(t_list, x);
|
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
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* new_join_pathkey
|
1997-09-07 07:04:48 +02:00
|
|
|
* Finds new vars that become subkeys due to qualification clauses that
|
|
|
|
* contain any previously considered subkeys. These new subkeys plus the
|
|
|
|
* subkeys from 'subkeys' form a new pathkey for the join relation.
|
|
|
|
*
|
|
|
|
* Note that each returned subkey is the var node found in
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'join_rel_tlist' rather than the joinclause var node.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'subkeys' is a list of subkeys for which matching subkeys are to be
|
1997-09-07 07:04:48 +02:00
|
|
|
* found
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'considered_subkeys' is the current list of all subkeys corresponding
|
1997-09-07 07:04:48 +02:00
|
|
|
* to a given pathkey
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns a new pathkey(list of subkeys).
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static List *
|
1997-09-08 23:56:23 +02:00
|
|
|
new_join_pathkey(List *subkeys,
|
|
|
|
List *considered_subkeys,
|
|
|
|
List *join_rel_tlist,
|
|
|
|
List *joinclauses)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *t_list = NIL;
|
|
|
|
Var *subkey;
|
|
|
|
List *i = NIL;
|
|
|
|
List *matched_subkeys = NIL;
|
|
|
|
Expr *tlist_key = (Expr *) NULL;
|
|
|
|
List *newly_considered_subkeys = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(i, subkeys)
|
|
|
|
{
|
|
|
|
subkey = (Var *) lfirst(i);
|
|
|
|
if (subkey == NULL)
|
|
|
|
break; /* XXX something is wrong */
|
1999-02-03 22:18:02 +01:00
|
|
|
matched_subkeys = new_matching_subkeys(subkey, considered_subkeys,
|
1997-09-07 07:04:48 +02:00
|
|
|
join_rel_tlist, joinclauses);
|
1999-02-15 06:56:07 +01:00
|
|
|
tlist_key = matching_tlist_var(subkey, join_rel_tlist);
|
1997-09-07 07:04:48 +02:00
|
|
|
newly_considered_subkeys = NIL;
|
|
|
|
|
|
|
|
if (tlist_key)
|
|
|
|
{
|
|
|
|
if (!member(tlist_key, matched_subkeys))
|
1999-02-11 06:29:08 +01:00
|
|
|
newly_considered_subkeys = lcons(tlist_key, matched_subkeys);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
newly_considered_subkeys = matched_subkeys;
|
|
|
|
|
1999-02-10 18:14:32 +01:00
|
|
|
considered_subkeys = append(considered_subkeys, newly_considered_subkeys);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
t_list = nconc(t_list, newly_considered_subkeys);
|
|
|
|
}
|
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_matching_subkeys
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns a list of new subkeys:
|
1999-02-14 00:22:53 +01:00
|
|
|
* (1) which are not listed in 'considered_subkeys'
|
1997-09-07 07:04:48 +02:00
|
|
|
* (2) for which the "other" variable in some clause in 'joinclauses' is
|
|
|
|
* 'subkey'
|
1999-02-14 00:22:53 +01:00
|
|
|
* (3) which are mentioned in 'join_rel_tlist'
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
|
|
|
* Note that each returned subkey is the var node found in
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'join_rel_tlist' rather than the joinclause var node.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'subkey' is the var node for which we are trying to find matching
|
1997-09-07 07:04:48 +02:00
|
|
|
* clauses
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns a list of new subkeys.
|
|
|
|
*
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static List *
|
1997-09-08 23:56:23 +02:00
|
|
|
new_matching_subkeys(Var *subkey,
|
|
|
|
List *considered_subkeys,
|
|
|
|
List *join_rel_tlist,
|
|
|
|
List *joinclauses)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *t_list = NIL;
|
1999-02-15 03:04:58 +01:00
|
|
|
Expr *joinclause;
|
|
|
|
List *i;
|
|
|
|
Expr *tlist_other_var;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(i, joinclauses)
|
|
|
|
{
|
|
|
|
joinclause = lfirst(i);
|
1999-02-15 06:56:07 +01:00
|
|
|
tlist_other_var = matching_tlist_var(
|
1999-02-09 18:03:14 +01:00
|
|
|
other_join_clause_var(subkey, joinclause),
|
|
|
|
join_rel_tlist);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (tlist_other_var &&
|
|
|
|
!(member(tlist_other_var, considered_subkeys)))
|
|
|
|
{
|
|
|
|
|
|
|
|
/* XXX was "push" function */
|
|
|
|
considered_subkeys = lappend(considered_subkeys,
|
|
|
|
tlist_other_var);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* considered_subkeys = nreverse(considered_subkeys); XXX -- I
|
|
|
|
* am not sure of this.
|
|
|
|
*/
|
|
|
|
|
1999-02-15 03:04:58 +01:00
|
|
|
t_list = lappend(t_list, tlist_other_var);
|
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
|
|
|
}
|