Fix the handling of sub-SELECTs appearing in the arguments of an outer-level
authorTom Lane
Sat, 25 Apr 2009 16:44:56 +0000 (16:44 +0000)
committerTom Lane
Sat, 25 Apr 2009 16:44:56 +0000 (16:44 +0000)
aggregate function.  By definition, such a sub-SELECT cannot reference any
variables of query levels between itself and the aggregate's semantic level
(else the aggregate would've been assigned to that lower level instead).
So the correct, most efficient implementation is to treat the sub-SELECT as
being a sub-select of that outer query level, not the level the aggregate
syntactically appears in.  Not doing so also confuses the heck out of our
parameter-passing logic, as illustrated in bug report from Daniel Grace.

Fortunately, we were already copying the whole Aggref expression up to the
outer query level, so all that's needed is to delay SS_process_sublinks
processing of the sub-SELECT until control returns to the outer level.

This has been broken since we introduced spec-compliant treatment of
outer aggregates in 7.4; so patch all the way back.

src/backend/optimizer/plan/subselect.c
src/test/regress/expected/aggregates.out
src/test/regress/sql/aggregates.sql

index 595ff1d46bb6338e05867b776802c8c8767f3e9f..6839e5d99badf6f5ae00f9de1738d8d339e25119 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.148 2009/04/05 19:59:40 tgl Exp $
+ *   $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.149 2009/04/25 16:44:56 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -437,13 +437,24 @@ build_subplan(PlannerInfo *root, Plan *plan, List *rtable,
 
        if (pitem->abslevel == root->query_level)
        {
-           splan->parParam = lappend_int(splan->parParam, paramid);
+           Node   *arg;
+
            /*
             * The Var or Aggref has already been adjusted to have the correct
             * varlevelsup or agglevelsup.  We probably don't even need to
             * copy it again, but be safe.
             */
-           splan->args = lappend(splan->args, copyObject(pitem->item));
+           arg = copyObject(pitem->item);
+
+           /*
+            * If it's an Aggref, its arguments might contain SubLinks,
+            * which have not yet been processed.  Do that now.
+            */
+           if (IsA(arg, Aggref))
+               arg = SS_process_sublinks(root, arg, false);
+
+           splan->parParam = lappend_int(splan->parParam, paramid);
+           splan->args = lappend(splan->args, arg);
        }
    }
    bms_free(tmpset);
@@ -1531,6 +1542,12 @@ convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
  * so after expanding its sublinks to subplans.  And we don't want any steps
  * in between, else those steps would never get applied to the aggregate
  * argument expressions, either in the parent or the child level.
+ *
+ * Another fairly tricky thing going on here is the handling of SubLinks in
+ * the arguments of uplevel aggregates.  Those are not touched inside the
+ * intermediate query level, either.  Instead, SS_process_sublinks recurses
+ * on them after copying the Aggref expression into the parent plan level
+ * (this is actually taken care of in build_subplan).
  */
 Node *
 SS_replace_correlation_vars(PlannerInfo *root, Node *expr)
@@ -1607,6 +1624,18 @@ process_sublinks_mutator(Node *node, process_sublinks_context *context)
                            context->isTopQual);
    }
 
+   /*
+    * Don't recurse into the arguments of an outer aggregate here.
+    * Any SubLinks in the arguments have to be dealt with at the outer
+    * query level; they'll be handled when build_subplan collects the
+    * Aggref into the arguments to be passed down to the current subplan.
+    */
+   if (IsA(node, Aggref))
+   {
+       if (((Aggref *) node)->agglevelsup > 0)
+           return node;
+   }
+
    /*
     * We should never see a SubPlan expression in the input (since this is
     * the very routine that creates 'em to begin with).  We shouldn't find
index 46f6f18ed0562c0d79024091ff06ff7bda067b99..a48b45007f34d9604b943b0667975afd087873b1 100644 (file)
@@ -295,6 +295,16 @@ having exists (select 1 from onek b
 ERROR:  aggregates not allowed in WHERE clause
 LINE 4:                where sum(distinct a.four + b.four) = b.four)...
                              ^
+-- Test handling of sublinks within outer-level aggregates.
+-- Per bug report from Daniel Grace.
+select
+  (select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1)))
+from tenk1 o;
+ ?column? 
+----------
+     9999
+(1 row)
+
 --
 -- test for bitwise integer aggregates
 --
index afa997e7ea7e8d0a608e9665386fcc47cc9d623d..39b0187a1e86af56dda7d487f7537d96ca31a510 100644 (file)
@@ -80,6 +80,12 @@ group by ten
 having exists (select 1 from onek b
                where sum(distinct a.four + b.four) = b.four);
 
+-- Test handling of sublinks within outer-level aggregates.
+-- Per bug report from Daniel Grace.
+select
+  (select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1)))
+from tenk1 o;
+
 --
 -- test for bitwise integer aggregates
 --