Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7fbf0af

Browse files
committed
When rewriting an aggregate introduced into WHERE, allow agg argument to
be an expression not just a simple Var, so long as only one table is referenced (so that code isn't really any more difficult than before). This whole thing is still fundamentally bogus, but at least we can accept a few more cases than before.
1 parent 2ae6e86 commit 7fbf0af

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/backend/rewrite/rewriteHandler.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.69 2000/03/16 03:23:18 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.70 2000/04/04 02:30:52 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -20,6 +20,7 @@
2020
#include "nodes/makefuncs.h"
2121
#include "optimizer/clauses.h"
2222
#include "optimizer/prep.h"
23+
#include "optimizer/var.h"
2324
#include "parser/analyze.h"
2425
#include "parser/parse_expr.h"
2526
#include "parser/parse_relation.h"
@@ -437,19 +438,19 @@ modifyAggrefDropQual(Node *node, Node *targetNode)
437438
static SubLink *
438439
modifyAggrefMakeSublink(Aggref *aggref, Query *parsetree)
439440
{
440-
/* target and rte point to old structures: */
441-
Var *target;
441+
List *aggVarNos;
442+
/* rte points to old structure: */
442443
RangeTblEntry *rte;
443444
/* these point to newly-created structures: */
444445
Query *subquery;
445446
SubLink *sublink;
446447
TargetEntry *tle;
447448
Resdom *resdom;
448449

449-
target = (Var *) (aggref->target);
450-
if (! IsA(target, Var))
451-
elog(ERROR, "rewrite: aggregates of views only allowed on simple variables for now");
452-
rte = rt_fetch(target->varno, parsetree->rtable);
450+
aggVarNos = pull_varnos(aggref->target);
451+
if (length(aggVarNos) != 1)
452+
elog(ERROR, "rewrite: aggregates of views only allowed on single tables for now");
453+
rte = rt_fetch(lfirsti(aggVarNos), parsetree->rtable);
453454

454455
resdom = makeNode(Resdom);
455456
resdom->resno = 1;
@@ -503,11 +504,13 @@ modifyAggrefMakeSublink(Aggref *aggref, Query *parsetree)
503504
/* Increment all varlevelsup fields in the new subquery */
504505
IncrementVarSublevelsUp((Node *) subquery, 1, 0);
505506

506-
/* Replace references to the target table with correct local varno.
507-
* Note +1 here to account for effects of previous line!
507+
/* Replace references to the target table with correct local varno, 1.
508+
* Note that because of previous line, these references have
509+
* varlevelsup = 1, which must be changed to 0.
508510
*/
509-
modifyAggrefChangeVarnodes((Node *) subquery, target->varno,
510-
1, target->varlevelsup+1, 0);
511+
modifyAggrefChangeVarnodes((Node *) subquery,
512+
lfirsti(aggVarNos), 1,
513+
1, 0);
511514

512515
return sublink;
513516
}

0 commit comments

Comments
 (0)