pgsql: Avoid listing ungrouped Vars in the targetlist of Agg-underneath

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Avoid listing ungrouped Vars in the targetlist of Agg-underneath
Date: 2011-07-12 22:25:14
Message-ID: E1QglOE-0001FU-Sv@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Avoid listing ungrouped Vars in the targetlist of Agg-underneath-Window.

Regular aggregate functions in combination with, or within the arguments
of, window functions are OK per spec; they have the semantics that the
aggregate output rows are computed and then we run the window functions
over that row set. (Thus, this combination is not really useful unless
there's a GROUP BY so that more than one aggregate output row is possible.)
The case without GROUP BY could fail, as recently reported by Jeff Davis,
because sloppy construction of the Agg node's targetlist resulted in extra
references to possibly-ungrouped Vars appearing outside the aggregate
function calls themselves. See the added regression test case for an
example.

Fixing this requires modifying the API of flatten_tlist and its underlying
function pull_var_clause. I chose to make pull_var_clause's API for
aggregates identical to what it was already doing for placeholders, since
the useful behaviors turn out to be the same (error, report node as-is, or
recurse into it). I also tightened the error checking in this area a bit:
if it was ever valid to see an uplevel Var, Aggref, or PlaceHolderVar here,
that was a long time ago, so complain instead of ignoring them.

Backpatch into 9.1. The failure exists in 8.4 and 9.0 as well, but seeing
that it only occurs in a basically-useless corner case, it doesn't seem
worth the risks of changing a function API in a minor release. There might
be third-party code using pull_var_clause.

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/c1d9579dd8bf3c921ca6bc2b62c40da6d25372e5

Modified Files
--------------
src/backend/catalog/heap.c | 8 +++-
src/backend/commands/trigger.c | 4 ++-
src/backend/optimizer/path/allpaths.c | 7 +++-
src/backend/optimizer/path/equivclass.c | 1 +
src/backend/optimizer/plan/createplan.c | 1 +
src/backend/optimizer/plan/initsplan.c | 5 ++-
src/backend/optimizer/plan/planner.c | 31 +++++++++++------
src/backend/optimizer/prep/preptlist.c | 1 +
src/backend/optimizer/util/clauses.c | 36 -------------------
src/backend/optimizer/util/placeholder.c | 5 ++-
src/backend/optimizer/util/tlist.c | 11 +++---
src/backend/optimizer/util/var.c | 55 +++++++++++++++++++++++-------
src/backend/utils/adt/selfuncs.c | 4 ++-
src/include/optimizer/clauses.h | 1 -
src/include/optimizer/tlist.h | 5 ++-
src/include/optimizer/var.h | 12 +++++-
src/test/regress/expected/window.out | 7 ++++
src/test/regress/sql/window.sql | 3 ++
18 files changed, 119 insertions(+), 78 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Eisentraut 2011-07-12 22:32:11 pgsql: Mark collation-related information schema columns as not impleme
Previous Message Alvaro Herrera 2011-07-12 21:28:58 pgsql: Add support for blocked commands in isolationtester