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

Поиск
Список
Период
Сортировка
От Tom Lane
Тема pgsql: Avoid listing ungrouped Vars in the targetlist of Agg-underneath
Дата
Msg-id E1QglOE-0001FU-Sv@gemulon.postgresql.org
обсуждение исходный текст
Список 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(-)


В списке pgsql-committers по дате отправления:

Предыдущее
От: Alvaro Herrera
Дата:
Сообщение: pgsql: Add support for blocked commands in isolationtester
Следующее
От: Tom Lane
Дата:
Сообщение: pgsql: Avoid listing ungrouped Vars in the targetlist of Agg-underneath