mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	Fix failure to detect some cases of improperly-nested aggregates.
check_agg_arguments_walker() supposed that it needn't descend into the arguments of a lower-level aggregate function, but this is just wrong in the presence of multiple levels of sub-select. The oversight would lead to executor failures on queries that should be rejected. (Prior to v11, they actually were rejected, thanks to a "redundant" execution-time check.) Per bug #17835 from Anban Company. Back-patch to all supported branches. Discussion: https://postgr.es/m/17835-4f29f3098b2d0ba4@postgresql.org
This commit is contained in:
		@@ -717,8 +717,7 @@ check_agg_arguments_walker(Node *node,
 | 
				
			|||||||
				context->min_agglevel > agglevelsup)
 | 
									context->min_agglevel > agglevelsup)
 | 
				
			||||||
				context->min_agglevel = agglevelsup;
 | 
									context->min_agglevel = agglevelsup;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		/* no need to examine args of the inner aggregate */
 | 
							/* Continue and descend into subtree */
 | 
				
			||||||
		return false;
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if (IsA(node, GroupingFunc))
 | 
						if (IsA(node, GroupingFunc))
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1187,6 +1187,12 @@ select (select max(min(unique1)) from int8_tbl) from tenk1;
 | 
				
			|||||||
ERROR:  aggregate function calls cannot be nested
 | 
					ERROR:  aggregate function calls cannot be nested
 | 
				
			||||||
LINE 1: select (select max(min(unique1)) from int8_tbl) from tenk1;
 | 
					LINE 1: select (select max(min(unique1)) from int8_tbl) from tenk1;
 | 
				
			||||||
                           ^
 | 
					                           ^
 | 
				
			||||||
 | 
					select avg((select avg(a1.col1 order by (select avg(a2.col2) from tenk1 a3))
 | 
				
			||||||
 | 
					            from tenk1 a1(col1)))
 | 
				
			||||||
 | 
					from tenk1 a2(col2);
 | 
				
			||||||
 | 
					ERROR:  aggregate function calls cannot be nested
 | 
				
			||||||
 | 
					LINE 1: select avg((select avg(a1.col1 order by (select avg(a2.col2)...
 | 
				
			||||||
 | 
					                                                        ^
 | 
				
			||||||
--
 | 
					--
 | 
				
			||||||
-- Test removal of redundant GROUP BY columns
 | 
					-- Test removal of redundant GROUP BY columns
 | 
				
			||||||
--
 | 
					--
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -397,6 +397,9 @@ drop table minmaxtest cascade;
 | 
				
			|||||||
-- check for correct detection of nested-aggregate errors
 | 
					-- check for correct detection of nested-aggregate errors
 | 
				
			||||||
select max(min(unique1)) from tenk1;
 | 
					select max(min(unique1)) from tenk1;
 | 
				
			||||||
select (select max(min(unique1)) from int8_tbl) from tenk1;
 | 
					select (select max(min(unique1)) from int8_tbl) from tenk1;
 | 
				
			||||||
 | 
					select avg((select avg(a1.col1 order by (select avg(a2.col2) from tenk1 a3))
 | 
				
			||||||
 | 
					            from tenk1 a1(col1)))
 | 
				
			||||||
 | 
					from tenk1 a2(col2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
--
 | 
					--
 | 
				
			||||||
-- Test removal of redundant GROUP BY columns
 | 
					-- Test removal of redundant GROUP BY columns
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user