mirror of
https://github.com/postgres/postgres.git
synced 2025-11-22 12:22:45 +03:00
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.
This commit is contained in:
@@ -76,9 +76,8 @@ tlist_member_ignore_relabel(Node *node, List *targetlist)
|
||||
* flatten_tlist
|
||||
* Create a target list that only contains unique variables.
|
||||
*
|
||||
* Note that Vars with varlevelsup > 0 are not included in the output
|
||||
* tlist. We expect that those will eventually be replaced with Params,
|
||||
* but that probably has not happened at the time this routine is called.
|
||||
* Aggrefs and PlaceHolderVars in the input are treated according to
|
||||
* aggbehavior and phbehavior, for which see pull_var_clause().
|
||||
*
|
||||
* 'tlist' is the current target list
|
||||
*
|
||||
@@ -88,10 +87,12 @@ tlist_member_ignore_relabel(Node *node, List *targetlist)
|
||||
* Copying the Var nodes is probably overkill, but be safe for now.
|
||||
*/
|
||||
List *
|
||||
flatten_tlist(List *tlist)
|
||||
flatten_tlist(List *tlist, PVCAggregateBehavior aggbehavior,
|
||||
PVCPlaceHolderBehavior phbehavior)
|
||||
{
|
||||
List *vlist = pull_var_clause((Node *) tlist,
|
||||
PVC_INCLUDE_PLACEHOLDERS);
|
||||
aggbehavior,
|
||||
phbehavior);
|
||||
List *new_tlist;
|
||||
|
||||
new_tlist = add_to_flat_tlist(NIL, vlist);
|
||||
|
||||
Reference in New Issue
Block a user