Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9c7dc89

Browse files
committed
Re-allow SRFs and window functions within sub-selects within aggregates.
check_agg_arguments_walker threw an error upon seeing a SRF or windowfunction, but that is too aggressive: if the function is within asub-select then it's perfectly fine. I broke the SRF case in commit0436f6b by copying the logic for window functions ... but that wasbroken too, and had been since commiteaccfde.Repair both cases in HEAD, and the window function case back to 9.3.9.2 gets this right.
1 parent2710ccd commit9c7dc89

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

‎src/backend/parser/parse_agg.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -705,21 +705,28 @@ check_agg_arguments_walker(Node *node,
705705
}
706706
/* Continue and descend into subtree */
707707
}
708-
/* We can throw error on sight for a set-returning function */
709-
if ((IsA(node,FuncExpr)&&((FuncExpr*)node)->funcretset)||
710-
(IsA(node,OpExpr)&&((OpExpr*)node)->opretset))
711-
ereport(ERROR,
712-
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
713-
errmsg("aggregate function calls cannot contain set-returning function calls"),
714-
errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
715-
parser_errposition(context->pstate,exprLocation(node))));
716-
/* We can throw error on sight for a window function */
717-
if (IsA(node,WindowFunc))
718-
ereport(ERROR,
719-
(errcode(ERRCODE_GROUPING_ERROR),
720-
errmsg("aggregate function calls cannot contain window function calls"),
721-
parser_errposition(context->pstate,
722-
((WindowFunc*)node)->location)));
708+
709+
/*
710+
* SRFs and window functions can be rejected immediately, unless we are
711+
* within a sub-select within the aggregate's arguments; in that case
712+
* they're OK.
713+
*/
714+
if (context->sublevels_up==0)
715+
{
716+
if ((IsA(node,FuncExpr)&&((FuncExpr*)node)->funcretset)||
717+
(IsA(node,OpExpr)&&((OpExpr*)node)->opretset))
718+
ereport(ERROR,
719+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
720+
errmsg("aggregate function calls cannot contain set-returning function calls"),
721+
errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
722+
parser_errposition(context->pstate,exprLocation(node))));
723+
if (IsA(node,WindowFunc))
724+
ereport(ERROR,
725+
(errcode(ERRCODE_GROUPING_ERROR),
726+
errmsg("aggregate function calls cannot contain window function calls"),
727+
parser_errposition(context->pstate,
728+
((WindowFunc*)node)->location)));
729+
}
723730
if (IsA(node,Query))
724731
{
725732
/* Recurse into subselects */

‎src/test/regress/expected/tsrf.out

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,20 @@ ERROR: aggregate function calls cannot contain set-returning function calls
212212
LINE 1: SELECT min(generate_series(1, 3)) FROM few;
213213
^
214214
HINT: You might be able to move the set-returning function into a LATERAL FROM item.
215+
-- ... unless they're within a sub-select
216+
SELECT sum((3 = ANY(SELECT generate_series(1,4)))::int);
217+
sum
218+
-----
219+
1
220+
(1 row)
221+
222+
SELECT sum((3 = ANY(SELECT lag(x) over(order by x)
223+
FROM generate_series(1,4) x))::int);
224+
sum
225+
-----
226+
1
227+
(1 row)
228+
215229
-- SRFs are not allowed in window function arguments, either
216230
SELECT min(generate_series(1, 3)) OVER() FROM few;
217231
ERROR: window function calls cannot contain set-returning function calls

‎src/test/regress/sql/tsrf.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ SELECT q1, coalesce(generate_series(1,3), 0) FROM int8_tbl;
6161
-- SRFs are not allowed in aggregate arguments
6262
SELECTmin(generate_series(1,3))FROM few;
6363

64+
-- ... unless they're within a sub-select
65+
SELECTsum((3= ANY(SELECT generate_series(1,4)))::int);
66+
67+
SELECTsum((3= ANY(SELECT lag(x) over(order by x)
68+
FROM generate_series(1,4) x))::int);
69+
6470
-- SRFs are not allowed in window function arguments, either
6571
SELECTmin(generate_series(1,3)) OVER()FROM few;
6672

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp