7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.3 1997/04/05 06:37:37 vadim Exp $
10
+ * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.4 1997/04/29 04:32:50 vadim Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -359,8 +359,9 @@ make_groupPlan(List **tlist,
359
359
List * sort_tlist ;
360
360
List * sl , * gl ;
361
361
List * glc = listCopy (groupClause );
362
- List * aggvals = NIL ; /* list of vars of aggregates */
363
- int aggvcnt ;
362
+ List * otles = NIL ; /* list of removed non-GroupBy entries */
363
+ List * otlvars = NIL ; /* list of var in them */
364
+ int otlvcnt ;
364
365
Sort * sortplan ;
365
366
Group * grpplan ;
366
367
int numCols ;
@@ -375,7 +376,8 @@ make_groupPlan(List **tlist,
375
376
376
377
/*
377
378
* Make template TL for subplan, Sort & Group:
378
- * 1. Take away Aggregates and re-set resno-s accordantly.
379
+ * 1. If there are aggregates (tuplePerGroup is true) then take
380
+ * away non-GroupBy entries and re-set resno-s accordantly.
379
381
* 2. Make grpColIdx
380
382
*
381
383
* Note: we assume that TLEs in *tlist are ordered in accordance
@@ -390,7 +392,7 @@ make_groupPlan(List **tlist,
390
392
{
391
393
GroupClause * grpcl = (GroupClause * )lfirst (gl );
392
394
393
- if ( grpcl -> resdom -> resno == te -> resdom -> resno )
395
+ if ( grpcl -> entry -> resdom -> resno == te -> resdom -> resno )
394
396
{
395
397
396
398
resdom = te -> resdom ;
@@ -403,15 +405,20 @@ make_groupPlan(List **tlist,
403
405
break ;
404
406
}
405
407
}
406
- if ( resdom == NULL ) /* Not GroupBy-ed entry: remove */
407
- { /* aggregate(s) from Group/Sort TL */
408
- if ( IsA (te -> expr , Aggreg ) )
409
- { /* save Aggregate' Vars */
410
- aggvals = nconc (aggvals , pull_var_clause (te -> expr ));
411
- sort_tlist = lremove (lfirst (sl ), sort_tlist );
408
+ /*
409
+ * Non-GroupBy entry: remove it from Group/Sort TL if there are
410
+ * aggregates in query - it will be evaluated by Aggregate plan
411
+ */
412
+ if ( resdom == NULL )
413
+ {
414
+ if ( tuplePerGroup )
415
+ {
416
+ otlvars = nconc (otlvars , pull_var_clause (te -> expr ));
417
+ otles = lcons (te , otles );
418
+ sort_tlist = lremove (te , sort_tlist );
412
419
}
413
420
else
414
- resdom -> resno = last_resno ++ ; /* re-set */
421
+ te -> resdom -> resno = last_resno ++ ;
415
422
}
416
423
}
417
424
@@ -421,12 +428,12 @@ make_groupPlan(List **tlist,
421
428
}
422
429
423
430
/*
424
- * Aggregates were removed from TL - we are to add Vars for them
425
- * to the end of TL if there are no such Vars in TL already.
431
+ * If non-GroupBy entries were removed from TL - we are to add Vars for
432
+ * them to the end of TL if there are no such Vars in TL already.
426
433
*/
427
434
428
- aggvcnt = length (aggvals );
429
- foreach (gl , aggvals )
435
+ otlvcnt = length (otlvars );
436
+ foreach (gl , otlvars )
430
437
{
431
438
Var * v = (Var * )lfirst (gl );
432
439
@@ -437,9 +444,9 @@ make_groupPlan(List **tlist,
437
444
last_resno ++ ;
438
445
}
439
446
else /* already in TL */
440
- aggvcnt -- ;
447
+ otlvcnt -- ;
441
448
}
442
- /* Now aggvcnt is number of Vars added in TL for Aggregates */
449
+ /* Now otlvcnt is number of Vars added in TL for non-GroupBy entries */
443
450
444
451
/* Make TL for subplan: substitute Vars from subplan TL into new TL */
445
452
sl = flatten_tlist_vars (sort_tlist , subplan -> targetlist );
@@ -489,17 +496,28 @@ make_groupPlan(List **tlist,
489
496
grpColIdx , sortplan );
490
497
491
498
/*
492
- * Make TL for parent: "restore" Aggregates and
493
- * resno-s of others accordantly.
499
+ * Make TL for parent: "restore" non-GroupBy entries (if they
500
+ * were removed) and set resno-s of others accordantly.
494
501
*/
495
502
sl = sort_tlist ;
496
503
sort_tlist = NIL ; /* to be new parent TL */
497
504
foreach (gl , * tlist )
498
505
{
506
+ List * temp = NIL ;
499
507
TargetEntry * te = (TargetEntry * ) lfirst (gl );
500
-
501
- if ( !IsA (te -> expr , Aggreg ) ) /* It's "our" TLE - we're to return */
502
- { /* it from Sort/Group plans */
508
+
509
+ foreach (temp , otles ) /* Is it removed non-GroupBy entry ? */
510
+ {
511
+ TargetEntry * ote = (TargetEntry * ) lfirst (temp );
512
+
513
+ if ( ote -> resdom -> resno == te -> resdom -> resno )
514
+ {
515
+ otles = lremove (ote , otles );
516
+ break ;
517
+ }
518
+ }
519
+ if ( temp == NIL ) /* It's "our" TLE - we're to return */
520
+ { /* it from Sort/Group plans */
503
521
TargetEntry * my = (TargetEntry * ) lfirst (sl ); /* get it */
504
522
505
523
sl = sl -> next ; /* prepare for the next "our" */
@@ -508,15 +526,16 @@ make_groupPlan(List **tlist,
508
526
sort_tlist = lappend (sort_tlist , my );
509
527
continue ;
510
528
}
511
- /* TLE of an aggregate */
529
+ /* else - it's TLE of an non-GroupBy entry */
512
530
sort_tlist = lappend (sort_tlist , copyObject (te ));
513
531
}
514
532
/*
515
- * Pure aggregates Vars were at the end of Group' TL.
533
+ * Pure non-GroupBy entries Vars were at the end of Group' TL.
516
534
* They shouldn't appear in parent TL, all others shouldn't
517
535
* disappear.
518
536
*/
519
- Assert ( aggvcnt == length (sl ) );
537
+ Assert ( otlvcnt == length (sl ) );
538
+ Assert ( length (otles ) == 0 );
520
539
521
540
* tlist = sort_tlist ;
522
541
0 commit comments