Commit 7fbf0af2 authored by Tom Lane's avatar Tom Lane

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.
parent 2ae6e863
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.69 2000/03/16 03:23:18 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.70 2000/04/04 02:30:52 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "nodes/makefuncs.h" #include "nodes/makefuncs.h"
#include "optimizer/clauses.h" #include "optimizer/clauses.h"
#include "optimizer/prep.h" #include "optimizer/prep.h"
#include "optimizer/var.h"
#include "parser/analyze.h" #include "parser/analyze.h"
#include "parser/parse_expr.h" #include "parser/parse_expr.h"
#include "parser/parse_relation.h" #include "parser/parse_relation.h"
...@@ -437,8 +438,8 @@ modifyAggrefDropQual(Node *node, Node *targetNode) ...@@ -437,8 +438,8 @@ modifyAggrefDropQual(Node *node, Node *targetNode)
static SubLink * static SubLink *
modifyAggrefMakeSublink(Aggref *aggref, Query *parsetree) modifyAggrefMakeSublink(Aggref *aggref, Query *parsetree)
{ {
/* target and rte point to old structures: */ List *aggVarNos;
Var *target; /* rte points to old structure: */
RangeTblEntry *rte; RangeTblEntry *rte;
/* these point to newly-created structures: */ /* these point to newly-created structures: */
Query *subquery; Query *subquery;
...@@ -446,10 +447,10 @@ modifyAggrefMakeSublink(Aggref *aggref, Query *parsetree) ...@@ -446,10 +447,10 @@ modifyAggrefMakeSublink(Aggref *aggref, Query *parsetree)
TargetEntry *tle; TargetEntry *tle;
Resdom *resdom; Resdom *resdom;
target = (Var *) (aggref->target); aggVarNos = pull_varnos(aggref->target);
if (! IsA(target, Var)) if (length(aggVarNos) != 1)
elog(ERROR, "rewrite: aggregates of views only allowed on simple variables for now"); elog(ERROR, "rewrite: aggregates of views only allowed on single tables for now");
rte = rt_fetch(target->varno, parsetree->rtable); rte = rt_fetch(lfirsti(aggVarNos), parsetree->rtable);
resdom = makeNode(Resdom); resdom = makeNode(Resdom);
resdom->resno = 1; resdom->resno = 1;
...@@ -503,11 +504,13 @@ modifyAggrefMakeSublink(Aggref *aggref, Query *parsetree) ...@@ -503,11 +504,13 @@ modifyAggrefMakeSublink(Aggref *aggref, Query *parsetree)
/* Increment all varlevelsup fields in the new subquery */ /* Increment all varlevelsup fields in the new subquery */
IncrementVarSublevelsUp((Node *) subquery, 1, 0); IncrementVarSublevelsUp((Node *) subquery, 1, 0);
/* Replace references to the target table with correct local varno. /* Replace references to the target table with correct local varno, 1.
* Note +1 here to account for effects of previous line! * Note that because of previous line, these references have
* varlevelsup = 1, which must be changed to 0.
*/ */
modifyAggrefChangeVarnodes((Node *) subquery, target->varno, modifyAggrefChangeVarnodes((Node *) subquery,
1, target->varlevelsup+1, 0); lfirsti(aggVarNos), 1,
1, 0);
return sublink; return sublink;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment