7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.31 1999/05/25 16:09:47 momjian Exp $
10
+ * $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.32 1999/06/06 17:38:11 tgl Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
35
35
#include "optimizer/planmain.h"
36
36
37
37
static List * plan_inherit_query (Relids relids , Index rt_index ,
38
- RangeTblEntry * rt_entry , Query * parse ,
38
+ RangeTblEntry * rt_entry , Query * parse , List * tlist ,
39
39
List * * union_rtentriesPtr );
40
40
static RangeTblEntry * new_rangetable_entry (Oid new_relid ,
41
41
RangeTblEntry * old_entry );
@@ -208,32 +208,46 @@ plan_union_queries(Query *parse)
208
208
*
209
209
* Plans the queries for a given parent relation.
210
210
*
211
- * Returns a list containing a list of plans and a list of rangetable
212
- * entries to be inserted into an APPEND node.
213
- * XXX - what exactly does this mean, look for make_append
211
+ * Inputs:
212
+ * parse = parent parse tree
213
+ * tlist = target list for inheritance subqueries (not same as parent's!)
214
+ * rt_index = rangetable index for current inheritance item
215
+ *
216
+ * Returns an APPEND node that forms the result of performing the given
217
+ * query for each member relation of the inheritance group.
218
+ *
219
+ * If grouping, aggregation, or sorting is specified in the parent plan,
220
+ * the subplans should not do any of those steps --- we must do those
221
+ * operations just once above the APPEND node. The given tlist has been
222
+ * modified appropriately to remove group/aggregate expressions, but the
223
+ * Query node still has the relevant fields set. We remove them in the
224
+ * copies used for subplans (see plan_inherit_query).
225
+ *
226
+ * NOTE: this can be invoked recursively if more than one inheritance wildcard
227
+ * is present. At each level of recursion, the first wildcard remaining in
228
+ * the rangetable is expanded.
214
229
*/
215
230
Append *
216
- plan_inherit_queries (Query * parse , Index rt_index )
231
+ plan_inherit_queries (Query * parse , List * tlist , Index rt_index )
217
232
{
218
- List * union_plans = NIL ;
219
-
220
233
List * rangetable = parse -> rtable ;
221
234
RangeTblEntry * rt_entry = rt_fetch (rt_index , rangetable );
222
235
List * inheritrtable = NIL ;
223
- List * union_relids = NIL ;
236
+ List * union_relids ;
237
+ List * union_plans ;
224
238
225
239
union_relids = find_all_inheritors (lconsi (rt_entry -> relid ,
226
240
NIL ),
227
241
NIL );
228
242
229
243
/*
230
244
* Remove the flag for this relation, since we're about to handle it
231
- * (do it before recursing!). XXX destructive parse tree change
245
+ * (do it before recursing!). XXX destructive change to parent parse tree
232
246
*/
233
- rt_fetch ( rt_index , rangetable ) -> inh = false;
247
+ rt_entry -> inh = false;
234
248
235
249
union_plans = plan_inherit_query (union_relids , rt_index , rt_entry ,
236
- parse , & inheritrtable );
250
+ parse , tlist , & inheritrtable );
237
251
238
252
return (make_append (union_plans ,
239
253
NULL ,
@@ -252,11 +266,12 @@ plan_inherit_query(Relids relids,
252
266
Index rt_index ,
253
267
RangeTblEntry * rt_entry ,
254
268
Query * root ,
269
+ List * tlist ,
255
270
List * * union_rtentriesPtr )
256
271
{
257
- List * i ;
258
272
List * union_plans = NIL ;
259
273
List * union_rtentries = NIL ;
274
+ List * i ;
260
275
261
276
foreach (i , relids )
262
277
{
@@ -268,19 +283,21 @@ plan_inherit_query(Relids relids,
268
283
new_rt_entry );
269
284
270
285
/*
271
- * reset the uniqueflag and sortclause in parse tree root, so that
272
- * sorting will only be done once after append
286
+ * Insert the desired simplified tlist into the subquery
287
+ */
288
+ new_root -> targetList = copyObject (tlist );
289
+
290
+ /*
291
+ * Clear the sorting and grouping qualifications in the subquery,
292
+ * so that sorting will only be done once after append
273
293
*/
274
294
new_root -> uniqueFlag = NULL ;
275
295
new_root -> sortClause = NULL ;
276
296
new_root -> groupClause = NULL ;
277
297
new_root -> havingQual = NULL ;
298
+ new_root -> hasAggs = false; /* shouldn't be any left ... */
278
299
279
- if (new_root -> hasAggs )
280
- {
281
- new_root -> hasAggs = false;
282
- del_agg_tlist_references (new_root -> targetList );
283
- }
300
+ /* Fix attribute numbers as necessary */
284
301
fix_parsetree_attnums (rt_index ,
285
302
rt_entry -> relid ,
286
303
relid ,
346
363
first_inherit_rt_entry (List * rangetable )
347
364
{
348
365
int count = 0 ;
349
- List * temp = NIL ;
366
+ List * temp ;
350
367
351
368
foreach (temp , rangetable )
352
369
{
@@ -393,8 +410,8 @@ static Query *
393
410
subst_rangetable (Query * root , Index index , RangeTblEntry * new_entry )
394
411
{
395
412
Query * new_root = copyObject (root );
396
- List * temp = NIL ;
397
- int i = 0 ;
413
+ List * temp ;
414
+ int i ;
398
415
399
416
for (temp = new_root -> rtable , i = 1 ; i < index ; temp = lnext (temp ), i ++ )
400
417
;
@@ -403,6 +420,15 @@ subst_rangetable(Query *root, Index index, RangeTblEntry *new_entry)
403
420
return new_root ;
404
421
}
405
422
423
+ /*
424
+ * Adjust varnos for child tables. This routine makes it possible for
425
+ * child tables to have different column positions for the "same" attribute
426
+ * as a parent, which helps ALTER TABLE ADD COLUMN. Unfortunately this isn't
427
+ * nearly enough to make it work transparently; there are other places where
428
+ * things fall down if children and parents don't have the same column numbers
429
+ * for inherited attributes. It'd be better to rip this code out and fix
430
+ * ALTER TABLE...
431
+ */
406
432
static void
407
433
fix_parsetree_attnums_nodes (Index rt_index ,
408
434
Oid old_relid ,
@@ -433,16 +459,11 @@ fix_parsetree_attnums_nodes(Index rt_index,
433
459
case T_Var :
434
460
{
435
461
Var * var = (Var * ) node ;
436
- Oid old_typeid ,
437
- new_typeid ;
438
-
439
- old_typeid = old_relid ;
440
- new_typeid = new_relid ;
441
462
442
463
if (var -> varno == rt_index && var -> varattno != 0 )
443
464
{
444
- var -> varattno = get_attnum (new_typeid ,
445
- get_attname (old_typeid , var -> varattno ));
465
+ var -> varattno = get_attnum (new_relid ,
466
+ get_attname (old_relid , var -> varattno ));
446
467
}
447
468
}
448
469
break ;
0 commit comments