55 *
66 * Copyright (c) 1994, Regents of the University of California
77 *
8- * $Id: geqo_eval.c,v 1.35 1999/02/18 05:26:18 momjian Exp $
8+ * $Id: geqo_eval.c,v 1.36 1999/05/16 19:45:00 tgl Exp $
99 *
1010 *-------------------------------------------------------------------------
1111 */
4848#include "optimizer/geqo_gene.h"
4949#include "optimizer/geqo.h"
5050
51- static RelOptInfo * geqo_nth (int stop ,List * rels );
52-
5351/*
5452 * geqo_eval
5553 *
@@ -62,31 +60,28 @@ geqo_eval(Query *root, Gene *tour, int num_gene)
6260Cost fitness ;
6361List * temp ;
6462
65-
66- /* remember root->join_rel_list ... */
67- /* because root->join_rel_list will be changed during the following */
63+ /* remember root->join_rel_list ... */
64+ /* because root->join_rel_list will be changed during the following */
6865temp = listCopy (root -> join_rel_list );
6966
70- /* joinrel is readily processed query tree -- left-sided ! */
67+ /* joinrel is readily processed query tree -- left-sided ! */
7168joinrel = gimme_tree (root ,tour ,0 ,num_gene ,NULL );
7269
73- /* compute fitness */
70+ /* compute fitness */
7471fitness = (Cost )joinrel -> cheapestpath -> path_cost ;
7572
76- root -> join_rel_list = listCopy ( temp ) ;
73+ root -> join_rel_list = temp ;
7774
7875pfree (joinrel );
79- freeList (temp );
8076
8177return fitness ;
82-
8378}
8479
8580/*
8681 * gimme_tree
8782 * this program presumes that only LEFT-SIDED TREES are considered!
8883 *
89- * 'old_rel' is thepreceeding join
84+ * 'old_rel' is thepreceding join
9085 *
9186 * Returns a new join relation incorporating all joins in a left-sided tree.
9287 */
@@ -95,17 +90,16 @@ gimme_tree(Query *root, Gene *tour, int rel_count, int num_gene, RelOptInfo *old
9590{
9691RelOptInfo * inner_rel ;/* current relation */
9792int base_rel_index ;
98-
99- List * new_rels = NIL ;
100- RelOptInfo * new_rel = NULL ;
93+ List * new_rels ;
94+ RelOptInfo * new_rel ;
10195
10296if (rel_count < num_gene )
10397{/* tree not yet finished */
10498
10599/* tour[0] = 3; tour[1] = 1; tour[2] = 2 */
106100base_rel_index = (int )tour [rel_count ];
107101
108- inner_rel = (RelOptInfo * )geqo_nth (base_rel_index ,root -> base_rel_list );
102+ inner_rel = (RelOptInfo * )nth (base_rel_index - 1 ,root -> base_rel_list );
109103
110104if (rel_count == 0 )
111105{/* processing first join with
@@ -116,11 +110,11 @@ gimme_tree(Query *root, Gene *tour, int rel_count, int num_gene, RelOptInfo *old
116110else
117111{/* tree main part */
118112if (!(new_rels = make_rels_by_clause_joins (root ,old_rel ,
119- inner_rel -> joininfo ,
113+ old_rel -> joininfo ,
120114inner_rel -> relids )))
121115{
122116new_rels = make_rels_by_clauseless_joins (old_rel ,
123- lcons (inner_rel ,NIL ));
117+ lcons (inner_rel ,NIL ));
124118/* we don't do bushy plans in geqo, do we? bjm 02/18/1999
125119new_rels = append(new_rels,
126120 make_rels_by_clauseless_joins(old_rel,
@@ -151,12 +145,12 @@ gimme_tree(Query *root, Gene *tour, int rel_count, int num_gene, RelOptInfo *old
151145elog (DEBUG ,"gimme_tree: still %d relations left" ,length (new_rels ));
152146}
153147
148+ rels_set_cheapest (new_rels );
149+
154150/* get essential new relation */
155151new_rel = (RelOptInfo * )lfirst (new_rels );
156152rel_count ++ ;
157153
158- set_cheapest (new_rel ,new_rel -> pathlist );
159-
160154/* processing of other new_rel attributes */
161155if (new_rel -> size <=0 )
162156new_rel -> size = compute_rel_size (new_rel );
@@ -166,24 +160,7 @@ gimme_tree(Query *root, Gene *tour, int rel_count, int num_gene, RelOptInfo *old
166160
167161return gimme_tree (root ,tour ,rel_count ,num_gene ,new_rel );
168162}
169-
170163}
171164
172165return old_rel ;/* tree finished ... */
173166}
174-
175- static RelOptInfo *
176- geqo_nth (int stop ,List * rels )
177- {
178- List * r ;
179- int i = 1 ;
180-
181- foreach (r ,rels )
182- {
183- if (i == stop )
184- return lfirst (r );
185- i ++ ;
186- }
187- elog (ERROR ,"geqo_nth: Internal error - ran off end of list" );
188- return NULL ;/* to keep compiler happy */
189- }