1 /*-------------------------------------------------------------------------
4 * Utilities for matching and building join and path keys
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/backend/optimizer/path/Attic/pathkey.c,v 1.1 1999/02/18 19:58:52 momjian Exp $
12 *-------------------------------------------------------------------------
16 #include "nodes/pg_list.h"
17 #include "nodes/relation.h"
18 #include "nodes/plannodes.h"
20 #include "optimizer/internal.h"
21 #include "optimizer/paths.h"
22 #include "optimizer/var.h"
23 #include "optimizer/keys.h"
24 #include "optimizer/tlist.h"
25 #include "optimizer/joininfo.h"
26 #include "optimizer/ordering.h"
29 static int match_pathkey_joinkeys(List *pathkey, List *joinkeys,
31 static bool every_func(List *joinkeys, List *pathkey,
33 static List *new_join_pathkey(List *subkeys, List *considered_subkeys,
34 List *join_rel_tlist, List *joinclauses);
35 static List *new_matching_subkeys(Var *subkey, List *considered_subkeys,
36 List *join_rel_tlist, List *joinclauses);
38 /****************************************************************************
40 ****************************************************************************/
43 * match_pathkeys_joinkeys
44 * Attempts to match the keys of a path against the keys of join clauses.
45 * This is done by looking for a matching join key in 'joinkeys' for
46 * every path key in the list 'path.keys'. If there is a matching join key
47 * (not necessarily unique) for every path key, then the list of
48 * corresponding join keys and join clauses are returned in the order in
49 * which the keys matched the path keys.
51 * 'pathkeys' is a list of path keys:
52 * ( ( (var) (var) ... ) ( (var) ... ) )
53 * 'joinkeys' is a list of join keys:
54 * ( (outer inner) (outer inner) ... )
55 * 'joinclauses' is a list of clauses corresponding to the join keys in
57 * 'which_subkey' is a flag that selects the desired subkey of a join key
60 * Returns the join keys and corresponding join clauses in a list if all
61 * of the path keys were matched:
63 * ( (outerkey0 innerkey0) ... (outerkeyN innerkeyN) )
64 * ( clause0 ... clauseN )
68 * Returns a list of matched join keys and a list of matched join clauses
69 * in matchedJoinClausesPtr. - ay 11/94
72 match_pathkeys_joinkeys(List *pathkeys,
76 List **matchedJoinClausesPtr)
78 List *matched_joinkeys = NIL;
79 List *matched_joinclauses = NIL;
82 int matched_joinkey_index = -1;
87 matched_joinkey_index = match_pathkey_joinkeys(pathkey, joinkeys, which_subkey);
89 if (matched_joinkey_index != -1)
91 List *xjoinkey = nth(matched_joinkey_index, joinkeys);
92 List *joinclause = nth(matched_joinkey_index, joinclauses);
94 /* XXX was "push" function */
95 matched_joinkeys = lappend(matched_joinkeys, xjoinkey);
96 matched_joinkeys = nreverse(matched_joinkeys);
98 matched_joinclauses = lappend(matched_joinclauses, joinclause);
99 matched_joinclauses = nreverse(matched_joinclauses);
100 joinkeys = LispRemove(xjoinkey, joinkeys);
106 if (matched_joinkeys == NULL ||
107 length(matched_joinkeys) != length(pathkeys))
110 *matchedJoinClausesPtr = nreverse(matched_joinclauses);
111 return nreverse(matched_joinkeys);
115 * match_pathkey_joinkeys
116 * Returns the 0-based index into 'joinkeys' of the first joinkey whose
117 * outer or inner subkey matches any subkey of 'pathkey'.
120 match_pathkey_joinkeys(List *pathkey,
132 path_subkey = (Var *) lfirst(i);
136 jk = (JoinKey *) lfirst(x);
137 if (var_equal(path_subkey,
138 extract_join_subkey(jk, which_subkey)))
143 return -1; /* no index found */
147 * match_paths_joinkeys
148 * Attempts to find a path in 'paths' whose keys match a set of join
149 * keys 'joinkeys'. To match,
150 * 1. the path node ordering must equal 'ordering'.
151 * 2. each subkey of a given path must match(i.e., be(var_equal) to) the
152 * appropriate subkey of the corresponding join key in 'joinkeys',
153 * i.e., the Nth path key must match its subkeys against the subkey of
154 * the Nth join key in 'joinkeys'.
156 * 'joinkeys' is the list of key pairs to which the path keys must be
158 * 'ordering' is the ordering of the(outer) path to which 'joinkeys'
160 * 'paths' is a list of(inner) paths which are to be matched against
161 * each join key in 'joinkeys'
162 * 'which_subkey' is a flag that selects the desired subkey of a join key
165 * Returns the matching path node if one exists, nil otherwise.
168 every_func(List *joinkeys, List *pathkey, int which_subkey)
179 xjoinkey = (JoinKey *) lfirst(i);
183 temp = (Var *) lfirst((List *) lfirst(j));
186 tempkey = extract_join_subkey(xjoinkey, which_subkey);
187 if (var_equal(tempkey, temp))
201 * match_paths_joinkeys -
202 * find the cheapest path that matches the join keys
205 match_paths_joinkeys(List *joinkeys,
210 Path *matched_path = NULL;
211 bool key_match = false;
216 Path *path = (Path *) lfirst(i);
219 key_match = every_func(joinkeys, path->pathkeys, which_subkey);
221 if (pathorder_match(ordering, path->pathorder, &better_sort) &&
223 length(joinkeys) == length(path->pathkeys) && key_match)
228 if (path->path_cost < matched_path->path_cost)
242 * Builds a subkey list for a path by pulling one of the subkeys from
243 * a list of join keys 'joinkeys' and then finding the var node in the
244 * target list 'tlist' that corresponds to that subkey.
246 * 'joinkeys' is a list of join key pairs
247 * 'tlist' is a relation target list
248 * 'which_subkey' is a flag that selects the desired subkey of a join key
251 * Returns a list of pathkeys: ((tlvar1)(tlvar2)...(tlvarN)).
252 * It is a list of lists because of multi-key indexes.
255 extract_path_keys(List *joinkeys,
259 List *pathkeys = NIL;
262 foreach(jk, joinkeys)
264 JoinKey *jkey = (JoinKey *) lfirst(jk);
270 * find the right Var in the target list for this key
272 var = (Var *) extract_join_subkey(jkey, which_subkey);
273 key = (Var *) matching_tlist_var(var, tlist);
276 * Include it in the pathkeys list if we haven't already done so
280 Var *pkey = lfirst((List *) lfirst(p)); /* XXX fix me */
286 continue; /* key already in pathkeys */
288 pathkeys = lappend(pathkeys, lcons(key, NIL));
294 /****************************************************************************
295 * NEW PATHKEY FORMATION
296 ****************************************************************************/
300 * Find the path keys for a join relation by finding all vars in the list
301 * of join clauses 'joinclauses' such that:
302 * (1) the var corresponding to the outer join relation is a
303 * key on the outer path
304 * (2) the var appears in the target list of the join relation
305 * In other words, add to each outer path key the inner path keys that
306 * are required for qualification.
308 * 'outer_pathkeys' is the list of the outer path's path keys
309 * 'join_rel_tlist' is the target list of the join relation
310 * 'joinclauses' is the list of restricting join clauses
312 * Returns the list of new path keys.
316 new_join_pathkeys(List *outer_pathkeys,
317 List *join_rel_tlist,
320 List *outer_pathkey = NIL;
325 foreach(i, outer_pathkeys)
327 outer_pathkey = lfirst(i);
328 x = new_join_pathkey(outer_pathkey, NIL, join_rel_tlist, joinclauses);
330 t_list = lappend(t_list, x);
337 * Finds new vars that become subkeys due to qualification clauses that
338 * contain any previously considered subkeys. These new subkeys plus the
339 * subkeys from 'subkeys' form a new pathkey for the join relation.
341 * Note that each returned subkey is the var node found in
342 * 'join_rel_tlist' rather than the joinclause var node.
344 * 'subkeys' is a list of subkeys for which matching subkeys are to be
346 * 'considered_subkeys' is the current list of all subkeys corresponding
349 * Returns a new pathkey(list of subkeys).
353 new_join_pathkey(List *subkeys,
354 List *considered_subkeys,
355 List *join_rel_tlist,
361 List *matched_subkeys = NIL;
362 Expr *tlist_key = (Expr *) NULL;
363 List *newly_considered_subkeys = NIL;
367 subkey = (Var *) lfirst(i);
369 break; /* XXX something is wrong */
370 matched_subkeys = new_matching_subkeys(subkey, considered_subkeys,
371 join_rel_tlist, joinclauses);
372 tlist_key = matching_tlist_var(subkey, join_rel_tlist);
373 newly_considered_subkeys = NIL;
377 if (!member(tlist_key, matched_subkeys))
378 newly_considered_subkeys = lcons(tlist_key, matched_subkeys);
381 newly_considered_subkeys = matched_subkeys;
383 considered_subkeys = append(considered_subkeys, newly_considered_subkeys);
385 t_list = nconc(t_list, newly_considered_subkeys);
391 * new_matching_subkeys
392 * Returns a list of new subkeys:
393 * (1) which are not listed in 'considered_subkeys'
394 * (2) for which the "other" variable in some clause in 'joinclauses' is
396 * (3) which are mentioned in 'join_rel_tlist'
398 * Note that each returned subkey is the var node found in
399 * 'join_rel_tlist' rather than the joinclause var node.
401 * 'subkey' is the var node for which we are trying to find matching
404 * Returns a list of new subkeys.
408 new_matching_subkeys(Var *subkey,
409 List *considered_subkeys,
410 List *join_rel_tlist,
416 Expr *tlist_other_var;
418 foreach(i, joinclauses)
420 joinclause = lfirst(i);
421 tlist_other_var = matching_tlist_var(
422 other_join_clause_var(subkey, joinclause),
425 if (tlist_other_var &&
426 !(member(tlist_other_var, considered_subkeys)))
429 /* XXX was "push" function */
430 considered_subkeys = lappend(considered_subkeys,
434 * considered_subkeys = nreverse(considered_subkeys); XXX -- I
435 * am not sure of this.
438 t_list = lappend(t_list, tlist_other_var);