aggregates.sql 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. --
  2. -- AGGREGATES
  3. --
  4. -- avoid bit-exact output here because operations may not be bit-exact.
  5. SET extra_float_digits = 0;
  6. SELECT avg(four) AS avg_1 FROM onek;
  7. SELECT avg(a) AS avg_32 FROM aggtest WHERE a < 100;
  8. -- In 7.1, avg(float4) is computed using float8 arithmetic.
  9. -- Round the result to 3 digits to avoid platform-specific results.
  10. SELECT avg(b)::numeric(10,3) AS avg_107_943 FROM aggtest;
  11. SELECT avg(gpa) AS avg_3_4 FROM ONLY student;
  12. SELECT sum(four) AS sum_1500 FROM onek;
  13. SELECT sum(a) AS sum_198 FROM aggtest;
  14. SELECT sum(b) AS avg_431_773 FROM aggtest;
  15. SELECT sum(gpa) AS avg_6_8 FROM ONLY student;
  16. SELECT max(four) AS max_3 FROM onek;
  17. SELECT max(a) AS max_100 FROM aggtest;
  18. SELECT max(aggtest.b) AS max_324_78 FROM aggtest;
  19. SELECT max(student.gpa) AS max_3_7 FROM student;
  20. SELECT stddev_pop(b) FROM aggtest;
  21. SELECT stddev_samp(b) FROM aggtest;
  22. SELECT var_pop(b) FROM aggtest;
  23. SELECT var_samp(b) FROM aggtest;
  24. SELECT stddev_pop(b::numeric) FROM aggtest;
  25. SELECT stddev_samp(b::numeric) FROM aggtest;
  26. SELECT var_pop(b::numeric) FROM aggtest;
  27. SELECT var_samp(b::numeric) FROM aggtest;
  28. -- population variance is defined for a single tuple, sample variance
  29. -- is not
  30. SELECT var_pop(1.0::float8), var_samp(2.0::float8);
  31. SELECT stddev_pop(3.0::float8), stddev_samp(4.0::float8);
  32. SELECT var_pop('inf'::float8), var_samp('inf'::float8);
  33. SELECT stddev_pop('inf'::float8), stddev_samp('inf'::float8);
  34. SELECT var_pop('nan'::float8), var_samp('nan'::float8);
  35. SELECT stddev_pop('nan'::float8), stddev_samp('nan'::float8);
  36. SELECT var_pop(1.0::float4), var_samp(2.0::float4);
  37. SELECT stddev_pop(3.0::float4), stddev_samp(4.0::float4);
  38. SELECT var_pop('inf'::float4), var_samp('inf'::float4);
  39. SELECT stddev_pop('inf'::float4), stddev_samp('inf'::float4);
  40. SELECT var_pop('nan'::float4), var_samp('nan'::float4);
  41. SELECT stddev_pop('nan'::float4), stddev_samp('nan'::float4);
  42. SELECT var_pop(1.0::numeric), var_samp(2.0::numeric);
  43. SELECT stddev_pop(3.0::numeric), stddev_samp(4.0::numeric);
  44. SELECT var_pop('inf'::numeric), var_samp('inf'::numeric);
  45. SELECT stddev_pop('inf'::numeric), stddev_samp('inf'::numeric);
  46. SELECT var_pop('nan'::numeric), var_samp('nan'::numeric);
  47. SELECT stddev_pop('nan'::numeric), stddev_samp('nan'::numeric);
  48. -- verify correct results for null and NaN inputs
  49. select sum(null::int4) from generate_series(1,3);
  50. select sum(null::int8) from generate_series(1,3);
  51. select sum(null::numeric) from generate_series(1,3);
  52. select sum(null::float8) from generate_series(1,3);
  53. select avg(null::int4) from generate_series(1,3);
  54. select avg(null::int8) from generate_series(1,3);
  55. select avg(null::numeric) from generate_series(1,3);
  56. select avg(null::float8) from generate_series(1,3);
  57. select sum('NaN'::numeric) from generate_series(1,3);
  58. select avg('NaN'::numeric) from generate_series(1,3);
  59. -- verify correct results for infinite inputs
  60. SELECT sum(x::float8), avg(x::float8), var_pop(x::float8)
  61. FROM (VALUES ('1'), ('infinity')) v(x);
  62. SELECT sum(x::float8), avg(x::float8), var_pop(x::float8)
  63. FROM (VALUES ('infinity'), ('1')) v(x);
  64. SELECT sum(x::float8), avg(x::float8), var_pop(x::float8)
  65. FROM (VALUES ('infinity'), ('infinity')) v(x);
  66. SELECT sum(x::float8), avg(x::float8), var_pop(x::float8)
  67. FROM (VALUES ('-infinity'), ('infinity')) v(x);
  68. SELECT sum(x::float8), avg(x::float8), var_pop(x::float8)
  69. FROM (VALUES ('-infinity'), ('-infinity')) v(x);
  70. SELECT sum(x::numeric), avg(x::numeric), var_pop(x::numeric)
  71. FROM (VALUES ('1'), ('infinity')) v(x);
  72. SELECT sum(x::numeric), avg(x::numeric), var_pop(x::numeric)
  73. FROM (VALUES ('infinity'), ('1')) v(x);
  74. SELECT sum(x::numeric), avg(x::numeric), var_pop(x::numeric)
  75. FROM (VALUES ('infinity'), ('infinity')) v(x);
  76. SELECT sum(x::numeric), avg(x::numeric), var_pop(x::numeric)
  77. FROM (VALUES ('-infinity'), ('infinity')) v(x);
  78. SELECT sum(x::numeric), avg(x::numeric), var_pop(x::numeric)
  79. FROM (VALUES ('-infinity'), ('-infinity')) v(x);
  80. -- test accuracy with a large input offset
  81. SELECT avg(x::float8), var_pop(x::float8)
  82. FROM (VALUES (100000003), (100000004), (100000006), (100000007)) v(x);
  83. SELECT avg(x::float8), var_pop(x::float8)
  84. FROM (VALUES (7000000000005), (7000000000007)) v(x);
  85. -- SQL2003 binary aggregates
  86. SELECT regr_count(b, a) FROM aggtest;
  87. SELECT regr_sxx(b, a) FROM aggtest;
  88. SELECT regr_syy(b, a) FROM aggtest;
  89. SELECT regr_sxy(b, a) FROM aggtest;
  90. SELECT regr_avgx(b, a), regr_avgy(b, a) FROM aggtest;
  91. SELECT regr_r2(b, a) FROM aggtest;
  92. SELECT regr_slope(b, a), regr_intercept(b, a) FROM aggtest;
  93. SELECT covar_pop(b, a), covar_samp(b, a) FROM aggtest;
  94. SELECT corr(b, a) FROM aggtest;
  95. -- check single-tuple behavior
  96. SELECT covar_pop(1::float8,2::float8), covar_samp(3::float8,4::float8);
  97. SELECT covar_pop(1::float8,'inf'::float8), covar_samp(3::float8,'inf'::float8);
  98. SELECT covar_pop(1::float8,'nan'::float8), covar_samp(3::float8,'nan'::float8);
  99. -- test accum and combine functions directly
  100. CREATE TABLE regr_test (x float8, y float8);
  101. INSERT INTO regr_test VALUES (10,150),(20,250),(30,350),(80,540),(100,200);
  102. SELECT count(*), sum(x), regr_sxx(y,x), sum(y),regr_syy(y,x), regr_sxy(y,x)
  103. FROM regr_test WHERE x IN (10,20,30,80);
  104. SELECT count(*), sum(x), regr_sxx(y,x), sum(y),regr_syy(y,x), regr_sxy(y,x)
  105. FROM regr_test;
  106. SELECT float8_accum('{4,140,2900}'::float8[], 100);
  107. SELECT float8_regr_accum('{4,140,2900,1290,83075,15050}'::float8[], 200, 100);
  108. SELECT count(*), sum(x), regr_sxx(y,x), sum(y),regr_syy(y,x), regr_sxy(y,x)
  109. FROM regr_test WHERE x IN (10,20,30);
  110. SELECT count(*), sum(x), regr_sxx(y,x), sum(y),regr_syy(y,x), regr_sxy(y,x)
  111. FROM regr_test WHERE x IN (80,100);
  112. SELECT float8_combine('{3,60,200}'::float8[], '{0,0,0}'::float8[]);
  113. SELECT float8_combine('{0,0,0}'::float8[], '{2,180,200}'::float8[]);
  114. SELECT float8_combine('{3,60,200}'::float8[], '{2,180,200}'::float8[]);
  115. SELECT float8_regr_combine('{3,60,200,750,20000,2000}'::float8[],
  116. '{0,0,0,0,0,0}'::float8[]);
  117. SELECT float8_regr_combine('{0,0,0,0,0,0}'::float8[],
  118. '{2,180,200,740,57800,-3400}'::float8[]);
  119. SELECT float8_regr_combine('{3,60,200,750,20000,2000}'::float8[],
  120. '{2,180,200,740,57800,-3400}'::float8[]);
  121. DROP TABLE regr_test;
  122. -- test count, distinct
  123. SELECT count(four) AS cnt_1000 FROM onek;
  124. SELECT count(DISTINCT four) AS cnt_4 FROM onek;
  125. select ten, count(*), sum(four) from onek
  126. group by ten order by ten;
  127. select ten, count(four), sum(DISTINCT four) from onek
  128. group by ten order by ten;
  129. -- user-defined aggregates
  130. SELECT newavg(four) AS avg_1 FROM onek;
  131. SELECT newsum(four) AS sum_1500 FROM onek;
  132. SELECT newcnt(four) AS cnt_1000 FROM onek;
  133. SELECT newcnt(*) AS cnt_1000 FROM onek;
  134. SELECT oldcnt(*) AS cnt_1000 FROM onek;
  135. SELECT sum2(q1,q2) FROM int8_tbl;
  136. -- test for outer-level aggregates
  137. -- this should work
  138. select ten, sum(distinct four) from onek a
  139. group by ten
  140. having exists (select 1 from onek b where sum(distinct a.four) = b.four);
  141. -- this should fail because subquery has an agg of its own in WHERE
  142. select ten, sum(distinct four) from onek a
  143. group by ten
  144. having exists (select 1 from onek b
  145. where sum(distinct a.four + b.four) = b.four);
  146. -- Test handling of sublinks within outer-level aggregates.
  147. -- Per bug report from Daniel Grace.
  148. select
  149. (select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1)))
  150. from tenk1 o;
  151. -- Test handling of Params within aggregate arguments in hashed aggregation.
  152. -- Per bug report from Jeevan Chalke.
  153. explain (verbose, costs off)
  154. select s1, s2, sm
  155. from generate_series(1, 3) s1,
  156. lateral (select s2, sum(s1 + s2) sm
  157. from generate_series(1, 3) s2 group by s2) ss
  158. order by 1, 2;
  159. select s1, s2, sm
  160. from generate_series(1, 3) s1,
  161. lateral (select s2, sum(s1 + s2) sm
  162. from generate_series(1, 3) s2 group by s2) ss
  163. order by 1, 2;
  164. explain (verbose, costs off)
  165. select array(select sum(x+y) s
  166. from generate_series(1,3) y group by y order by s)
  167. from generate_series(1,3) x;
  168. select array(select sum(x+y) s
  169. from generate_series(1,3) y group by y order by s)
  170. from generate_series(1,3) x;
  171. --
  172. -- test for bitwise integer aggregates
  173. --
  174. CREATE TEMPORARY TABLE bitwise_test(
  175. i2 INT2,
  176. i4 INT4,
  177. i8 INT8,
  178. i INTEGER,
  179. x INT2,
  180. y BIT(4)
  181. );
  182. -- empty case
  183. SELECT
  184. BIT_AND(i2) AS "?",
  185. BIT_OR(i4) AS "?",
  186. BIT_XOR(i8) AS "?"
  187. FROM bitwise_test;
  188. COPY bitwise_test FROM STDIN NULL 'null';
  189. 1 1 1 1 1 B0101
  190. 3 3 3 null 2 B0100
  191. 7 7 7 3 4 B1100
  192. \.
  193. SELECT
  194. BIT_AND(i2) AS "1",
  195. BIT_AND(i4) AS "1",
  196. BIT_AND(i8) AS "1",
  197. BIT_AND(i) AS "?",
  198. BIT_AND(x) AS "0",
  199. BIT_AND(y) AS "0100",
  200. BIT_OR(i2) AS "7",
  201. BIT_OR(i4) AS "7",
  202. BIT_OR(i8) AS "7",
  203. BIT_OR(i) AS "?",
  204. BIT_OR(x) AS "7",
  205. BIT_OR(y) AS "1101",
  206. BIT_XOR(i2) AS "5",
  207. BIT_XOR(i4) AS "5",
  208. BIT_XOR(i8) AS "5",
  209. BIT_XOR(i) AS "?",
  210. BIT_XOR(x) AS "7",
  211. BIT_XOR(y) AS "1101"
  212. FROM bitwise_test;
  213. --
  214. -- test boolean aggregates
  215. --
  216. -- first test all possible transition and final states
  217. SELECT
  218. -- boolean and transitions
  219. -- null because strict
  220. booland_statefunc(NULL, NULL) IS NULL AS "t",
  221. booland_statefunc(TRUE, NULL) IS NULL AS "t",
  222. booland_statefunc(FALSE, NULL) IS NULL AS "t",
  223. booland_statefunc(NULL, TRUE) IS NULL AS "t",
  224. booland_statefunc(NULL, FALSE) IS NULL AS "t",
  225. -- and actual computations
  226. booland_statefunc(TRUE, TRUE) AS "t",
  227. NOT booland_statefunc(TRUE, FALSE) AS "t",
  228. NOT booland_statefunc(FALSE, TRUE) AS "t",
  229. NOT booland_statefunc(FALSE, FALSE) AS "t";
  230. SELECT
  231. -- boolean or transitions
  232. -- null because strict
  233. boolor_statefunc(NULL, NULL) IS NULL AS "t",
  234. boolor_statefunc(TRUE, NULL) IS NULL AS "t",
  235. boolor_statefunc(FALSE, NULL) IS NULL AS "t",
  236. boolor_statefunc(NULL, TRUE) IS NULL AS "t",
  237. boolor_statefunc(NULL, FALSE) IS NULL AS "t",
  238. -- actual computations
  239. boolor_statefunc(TRUE, TRUE) AS "t",
  240. boolor_statefunc(TRUE, FALSE) AS "t",
  241. boolor_statefunc(FALSE, TRUE) AS "t",
  242. NOT boolor_statefunc(FALSE, FALSE) AS "t";
  243. CREATE TEMPORARY TABLE bool_test(
  244. b1 BOOL,
  245. b2 BOOL,
  246. b3 BOOL,
  247. b4 BOOL);
  248. -- empty case
  249. SELECT
  250. BOOL_AND(b1) AS "n",
  251. BOOL_OR(b3) AS "n"
  252. FROM bool_test;
  253. COPY bool_test FROM STDIN NULL 'null';
  254. TRUE null FALSE null
  255. FALSE TRUE null null
  256. null TRUE FALSE null
  257. \.
  258. SELECT
  259. BOOL_AND(b1) AS "f",
  260. BOOL_AND(b2) AS "t",
  261. BOOL_AND(b3) AS "f",
  262. BOOL_AND(b4) AS "n",
  263. BOOL_AND(NOT b2) AS "f",
  264. BOOL_AND(NOT b3) AS "t"
  265. FROM bool_test;
  266. SELECT
  267. EVERY(b1) AS "f",
  268. EVERY(b2) AS "t",
  269. EVERY(b3) AS "f",
  270. EVERY(b4) AS "n",
  271. EVERY(NOT b2) AS "f",
  272. EVERY(NOT b3) AS "t"
  273. FROM bool_test;
  274. SELECT
  275. BOOL_OR(b1) AS "t",
  276. BOOL_OR(b2) AS "t",
  277. BOOL_OR(b3) AS "f",
  278. BOOL_OR(b4) AS "n",
  279. BOOL_OR(NOT b2) AS "f",
  280. BOOL_OR(NOT b3) AS "t"
  281. FROM bool_test;
  282. --
  283. -- Test cases that should be optimized into indexscans instead of
  284. -- the generic aggregate implementation.
  285. --
  286. -- Basic cases
  287. explain (costs off)
  288. select min(unique1) from tenk1;
  289. select min(unique1) from tenk1;
  290. explain (costs off)
  291. select max(unique1) from tenk1;
  292. select max(unique1) from tenk1;
  293. explain (costs off)
  294. select max(unique1) from tenk1 where unique1 < 42;
  295. select max(unique1) from tenk1 where unique1 < 42;
  296. explain (costs off)
  297. select max(unique1) from tenk1 where unique1 > 42;
  298. select max(unique1) from tenk1 where unique1 > 42;
  299. -- the planner may choose a generic aggregate here if parallel query is
  300. -- enabled, since that plan will be parallel safe and the "optimized"
  301. -- plan, which has almost identical cost, will not be. we want to test
  302. -- the optimized plan, so temporarily disable parallel query.
  303. begin;
  304. set local max_parallel_workers_per_gather = 0;
  305. explain (costs off)
  306. select max(unique1) from tenk1 where unique1 > 42000;
  307. select max(unique1) from tenk1 where unique1 > 42000;
  308. rollback;
  309. -- multi-column index (uses tenk1_thous_tenthous)
  310. explain (costs off)
  311. select max(tenthous) from tenk1 where thousand = 33;
  312. select max(tenthous) from tenk1 where thousand = 33;
  313. explain (costs off)
  314. select min(tenthous) from tenk1 where thousand = 33;
  315. select min(tenthous) from tenk1 where thousand = 33;
  316. -- check parameter propagation into an indexscan subquery
  317. explain (costs off)
  318. select f1, (select min(unique1) from tenk1 where unique1 > f1) AS gt
  319. from int4_tbl;
  320. select f1, (select min(unique1) from tenk1 where unique1 > f1) AS gt
  321. from int4_tbl;
  322. -- check some cases that were handled incorrectly in 8.3.0
  323. explain (costs off)
  324. select distinct max(unique2) from tenk1;
  325. select distinct max(unique2) from tenk1;
  326. explain (costs off)
  327. select max(unique2) from tenk1 order by 1;
  328. select max(unique2) from tenk1 order by 1;
  329. explain (costs off)
  330. select max(unique2) from tenk1 order by max(unique2);
  331. select max(unique2) from tenk1 order by max(unique2);
  332. explain (costs off)
  333. select max(unique2) from tenk1 order by max(unique2)+1;
  334. select max(unique2) from tenk1 order by max(unique2)+1;
  335. explain (costs off)
  336. select max(unique2), generate_series(1,3) as g from tenk1 order by g desc;
  337. select max(unique2), generate_series(1,3) as g from tenk1 order by g desc;
  338. -- interesting corner case: constant gets optimized into a seqscan
  339. explain (costs off)
  340. select max(100) from tenk1;
  341. select max(100) from tenk1;
  342. -- try it on an inheritance tree
  343. create table minmaxtest(f1 int);
  344. create table minmaxtest1() inherits (minmaxtest);
  345. create table minmaxtest2() inherits (minmaxtest);
  346. create table minmaxtest3() inherits (minmaxtest);
  347. create index minmaxtesti on minmaxtest(f1);
  348. create index minmaxtest1i on minmaxtest1(f1);
  349. create index minmaxtest2i on minmaxtest2(f1 desc);
  350. create index minmaxtest3i on minmaxtest3(f1) where f1 is not null;
  351. insert into minmaxtest values(11), (12);
  352. insert into minmaxtest1 values(13), (14);
  353. insert into minmaxtest2 values(15), (16);
  354. insert into minmaxtest3 values(17), (18);
  355. explain (costs off)
  356. select min(f1), max(f1) from minmaxtest;
  357. select min(f1), max(f1) from minmaxtest;
  358. -- DISTINCT doesn't do anything useful here, but it shouldn't fail
  359. explain (costs off)
  360. select distinct min(f1), max(f1) from minmaxtest;
  361. select distinct min(f1), max(f1) from minmaxtest;
  362. drop table minmaxtest cascade;
  363. -- check for correct detection of nested-aggregate errors
  364. select max(min(unique1)) from tenk1;
  365. select (select max(min(unique1)) from int8_tbl) from tenk1;
  366. --
  367. -- Test removal of redundant GROUP BY columns
  368. --
  369. create temp table t1 (a int, b int, c int, d int, primary key (a, b));
  370. create temp table t2 (x int, y int, z int, primary key (x, y));
  371. create temp table t3 (a int, b int, c int, primary key(a, b) deferrable);
  372. -- Non-primary-key columns can be removed from GROUP BY
  373. explain (costs off) select * from t1 group by a,b,c,d;
  374. -- No removal can happen if the complete PK is not present in GROUP BY
  375. explain (costs off) select a,c from t1 group by a,c,d;
  376. -- Test removal across multiple relations
  377. explain (costs off) select *
  378. from t1 inner join t2 on t1.a = t2.x and t1.b = t2.y
  379. group by t1.a,t1.b,t1.c,t1.d,t2.x,t2.y,t2.z;
  380. -- Test case where t1 can be optimized but not t2
  381. explain (costs off) select t1.*,t2.x,t2.z
  382. from t1 inner join t2 on t1.a = t2.x and t1.b = t2.y
  383. group by t1.a,t1.b,t1.c,t1.d,t2.x,t2.z;
  384. -- Cannot optimize when PK is deferrable
  385. explain (costs off) select * from t3 group by a,b,c;
  386. create temp table t1c () inherits (t1);
  387. -- Ensure we don't remove any columns when t1 has a child table
  388. explain (costs off) select * from t1 group by a,b,c,d;
  389. -- Okay to remove columns if we're only querying the parent.
  390. explain (costs off) select * from only t1 group by a,b,c,d;
  391. create temp table p_t1 (
  392. a int,
  393. b int,
  394. c int,
  395. d int,
  396. primary key(a,b)
  397. ) partition by list(a);
  398. create temp table p_t1_1 partition of p_t1 for values in(1);
  399. create temp table p_t1_2 partition of p_t1 for values in(2);
  400. -- Ensure we can remove non-PK columns for partitioned tables.
  401. explain (costs off) select * from p_t1 group by a,b,c,d;
  402. drop table t1 cascade;
  403. drop table t2;
  404. drop table t3;
  405. drop table p_t1;
  406. --
  407. -- Test GROUP BY matching of join columns that are type-coerced due to USING
  408. --
  409. create temp table t1(f1 int, f2 bigint);
  410. create temp table t2(f1 bigint, f22 bigint);
  411. select f1 from t1 left join t2 using (f1) group by f1;
  412. select f1 from t1 left join t2 using (f1) group by t1.f1;
  413. select t1.f1 from t1 left join t2 using (f1) group by t1.f1;
  414. -- only this one should fail:
  415. select t1.f1 from t1 left join t2 using (f1) group by f1;
  416. drop table t1, t2;
  417. --
  418. -- Test combinations of DISTINCT and/or ORDER BY
  419. --
  420. select array_agg(a order by b)
  421. from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
  422. select array_agg(a order by a)
  423. from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
  424. select array_agg(a order by a desc)
  425. from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
  426. select array_agg(b order by a desc)
  427. from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
  428. select array_agg(distinct a)
  429. from (values (1),(2),(1),(3),(null),(2)) v(a);
  430. select array_agg(distinct a order by a)
  431. from (values (1),(2),(1),(3),(null),(2)) v(a);
  432. select array_agg(distinct a order by a desc)
  433. from (values (1),(2),(1),(3),(null),(2)) v(a);
  434. select array_agg(distinct a order by a desc nulls last)
  435. from (values (1),(2),(1),(3),(null),(2)) v(a);
  436. -- multi-arg aggs, strict/nonstrict, distinct/order by
  437. select aggfstr(a,b,c)
  438. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
  439. select aggfns(a,b,c)
  440. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
  441. select aggfstr(distinct a,b,c)
  442. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
  443. generate_series(1,3) i;
  444. select aggfns(distinct a,b,c)
  445. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
  446. generate_series(1,3) i;
  447. select aggfstr(distinct a,b,c order by b)
  448. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
  449. generate_series(1,3) i;
  450. select aggfns(distinct a,b,c order by b)
  451. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
  452. generate_series(1,3) i;
  453. -- test specific code paths
  454. select aggfns(distinct a,a,c order by c using ~<~,a)
  455. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
  456. generate_series(1,2) i;
  457. select aggfns(distinct a,a,c order by c using ~<~)
  458. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
  459. generate_series(1,2) i;
  460. select aggfns(distinct a,a,c order by a)
  461. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
  462. generate_series(1,2) i;
  463. select aggfns(distinct a,b,c order by a,c using ~<~,b)
  464. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
  465. generate_series(1,2) i;
  466. -- check node I/O via view creation and usage, also deparsing logic
  467. create view agg_view1 as
  468. select aggfns(a,b,c)
  469. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
  470. select * from agg_view1;
  471. select pg_get_viewdef('agg_view1'::regclass);
  472. create or replace view agg_view1 as
  473. select aggfns(distinct a,b,c)
  474. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
  475. generate_series(1,3) i;
  476. select * from agg_view1;
  477. select pg_get_viewdef('agg_view1'::regclass);
  478. create or replace view agg_view1 as
  479. select aggfns(distinct a,b,c order by b)
  480. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
  481. generate_series(1,3) i;
  482. select * from agg_view1;
  483. select pg_get_viewdef('agg_view1'::regclass);
  484. create or replace view agg_view1 as
  485. select aggfns(a,b,c order by b+1)
  486. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
  487. select * from agg_view1;
  488. select pg_get_viewdef('agg_view1'::regclass);
  489. create or replace view agg_view1 as
  490. select aggfns(a,a,c order by b)
  491. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
  492. select * from agg_view1;
  493. select pg_get_viewdef('agg_view1'::regclass);
  494. create or replace view agg_view1 as
  495. select aggfns(a,b,c order by c using ~<~)
  496. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
  497. select * from agg_view1;
  498. select pg_get_viewdef('agg_view1'::regclass);
  499. create or replace view agg_view1 as
  500. select aggfns(distinct a,b,c order by a,c using ~<~,b)
  501. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
  502. generate_series(1,2) i;
  503. select * from agg_view1;
  504. select pg_get_viewdef('agg_view1'::regclass);
  505. drop view agg_view1;
  506. -- incorrect DISTINCT usage errors
  507. select aggfns(distinct a,b,c order by i)
  508. from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
  509. select aggfns(distinct a,b,c order by a,b+1)
  510. from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
  511. select aggfns(distinct a,b,c order by a,b,i,c)
  512. from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
  513. select aggfns(distinct a,a,c order by a,b)
  514. from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
  515. -- string_agg tests
  516. select string_agg(a,',') from (values('aaaa'),('bbbb'),('cccc')) g(a);
  517. select string_agg(a,',') from (values('aaaa'),(null),('bbbb'),('cccc')) g(a);
  518. select string_agg(a,'AB') from (values(null),(null),('bbbb'),('cccc')) g(a);
  519. select string_agg(a,',') from (values(null),(null)) g(a);
  520. -- check some implicit casting cases, as per bug #5564
  521. select string_agg(distinct f1, ',' order by f1) from varchar_tbl; -- ok
  522. select string_agg(distinct f1::text, ',' order by f1) from varchar_tbl; -- not ok
  523. select string_agg(distinct f1, ',' order by f1::text) from varchar_tbl; -- not ok
  524. select string_agg(distinct f1::text, ',' order by f1::text) from varchar_tbl; -- ok
  525. -- string_agg bytea tests
  526. create table bytea_test_table(v bytea);
  527. select string_agg(v, '') from bytea_test_table;
  528. insert into bytea_test_table values(decode('ff','hex'));
  529. select string_agg(v, '') from bytea_test_table;
  530. insert into bytea_test_table values(decode('aa','hex'));
  531. select string_agg(v, '') from bytea_test_table;
  532. select string_agg(v, NULL) from bytea_test_table;
  533. select string_agg(v, decode('ee', 'hex')) from bytea_test_table;
  534. drop table bytea_test_table;
  535. -- FILTER tests
  536. select min(unique1) filter (where unique1 > 100) from tenk1;
  537. select sum(1/ten) filter (where ten > 0) from tenk1;
  538. select ten, sum(distinct four) filter (where four::text ~ '123') from onek a
  539. group by ten;
  540. select ten, sum(distinct four) filter (where four > 10) from onek a
  541. group by ten
  542. having exists (select 1 from onek b where sum(distinct a.four) = b.four);
  543. select max(foo COLLATE "C") filter (where (bar collate "POSIX") > '0')
  544. from (values ('a', 'b')) AS v(foo,bar);
  545. -- outer reference in FILTER (PostgreSQL extension)
  546. select (select count(*)
  547. from (values (1)) t0(inner_c))
  548. from (values (2),(3)) t1(outer_c); -- inner query is aggregation query
  549. select (select count(*) filter (where outer_c <> 0)
  550. from (values (1)) t0(inner_c))
  551. from (values (2),(3)) t1(outer_c); -- outer query is aggregation query
  552. select (select count(inner_c) filter (where outer_c <> 0)
  553. from (values (1)) t0(inner_c))
  554. from (values (2),(3)) t1(outer_c); -- inner query is aggregation query
  555. select
  556. (select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1))
  557. filter (where o.unique1 < 10))
  558. from tenk1 o; -- outer query is aggregation query
  559. -- subquery in FILTER clause (PostgreSQL extension)
  560. select sum(unique1) FILTER (WHERE
  561. unique1 IN (SELECT unique1 FROM onek where unique1 < 100)) FROM tenk1;
  562. -- exercise lots of aggregate parts with FILTER
  563. select aggfns(distinct a,b,c order by a,c using ~<~,b) filter (where a > 1)
  564. from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
  565. generate_series(1,2) i;
  566. -- check handling of bare boolean Var in FILTER
  567. select max(0) filter (where b1) from bool_test;
  568. select (select max(0) filter (where b1)) from bool_test;
  569. -- check for correct detection of nested-aggregate errors in FILTER
  570. select max(unique1) filter (where sum(ten) > 0) from tenk1;
  571. select (select max(unique1) filter (where sum(ten) > 0) from int8_tbl) from tenk1;
  572. select max(unique1) filter (where bool_or(ten > 0)) from tenk1;
  573. select (select max(unique1) filter (where bool_or(ten > 0)) from int8_tbl) from tenk1;
  574. -- ordered-set aggregates
  575. select p, percentile_cont(p) within group (order by x::float8)
  576. from generate_series(1,5) x,
  577. (values (0::float8),(0.1),(0.25),(0.4),(0.5),(0.6),(0.75),(0.9),(1)) v(p)
  578. group by p order by p;
  579. select p, percentile_cont(p order by p) within group (order by x) -- error
  580. from generate_series(1,5) x,
  581. (values (0::float8),(0.1),(0.25),(0.4),(0.5),(0.6),(0.75),(0.9),(1)) v(p)
  582. group by p order by p;
  583. select p, sum() within group (order by x::float8) -- error
  584. from generate_series(1,5) x,
  585. (values (0::float8),(0.1),(0.25),(0.4),(0.5),(0.6),(0.75),(0.9),(1)) v(p)
  586. group by p order by p;
  587. select p, percentile_cont(p,p) -- error
  588. from generate_series(1,5) x,
  589. (values (0::float8),(0.1),(0.25),(0.4),(0.5),(0.6),(0.75),(0.9),(1)) v(p)
  590. group by p order by p;
  591. select percentile_cont(0.5) within group (order by b) from aggtest;
  592. select percentile_cont(0.5) within group (order by b), sum(b) from aggtest;
  593. select percentile_cont(0.5) within group (order by thousand) from tenk1;
  594. select percentile_disc(0.5) within group (order by thousand) from tenk1;
  595. select rank(3) within group (order by x)
  596. from (values (1),(1),(2),(2),(3),(3),(4)) v(x);
  597. select cume_dist(3) within group (order by x)
  598. from (values (1),(1),(2),(2),(3),(3),(4)) v(x);
  599. select percent_rank(3) within group (order by x)
  600. from (values (1),(1),(2),(2),(3),(3),(4),(5)) v(x);
  601. select dense_rank(3) within group (order by x)
  602. from (values (1),(1),(2),(2),(3),(3),(4)) v(x);
  603. select percentile_disc(array[0,0.1,0.25,0.5,0.75,0.9,1]) within group (order by thousand)
  604. from tenk1;
  605. select percentile_cont(array[0,0.25,0.5,0.75,1]) within group (order by thousand)
  606. from tenk1;
  607. select percentile_disc(array[[null,1,0.5],[0.75,0.25,null]]) within group (order by thousand)
  608. from tenk1;
  609. select percentile_cont(array[0,1,0.25,0.75,0.5,1,0.3,0.32,0.35,0.38,0.4]) within group (order by x)
  610. from generate_series(1,6) x;
  611. select ten, mode() within group (order by string4) from tenk1 group by ten;
  612. select percentile_disc(array[0.25,0.5,0.75]) within group (order by x)
  613. from unnest('{fred,jim,fred,jack,jill,fred,jill,jim,jim,sheila,jim,sheila}'::text[]) u(x);
  614. -- check collation propagates up in suitable cases:
  615. select pg_collation_for(percentile_disc(1) within group (order by x collate "POSIX"))
  616. from (values ('fred'),('jim')) v(x);
  617. -- ordered-set aggs created with CREATE AGGREGATE
  618. select test_rank(3) within group (order by x)
  619. from (values (1),(1),(2),(2),(3),(3),(4)) v(x);
  620. select test_percentile_disc(0.5) within group (order by thousand) from tenk1;
  621. -- ordered-set aggs can't use ungrouped vars in direct args:
  622. select rank(x) within group (order by x) from generate_series(1,5) x;
  623. -- outer-level agg can't use a grouped arg of a lower level, either:
  624. select array(select percentile_disc(a) within group (order by x)
  625. from (values (0.3),(0.7)) v(a) group by a)
  626. from generate_series(1,5) g(x);
  627. -- agg in the direct args is a grouping violation, too:
  628. select rank(sum(x)) within group (order by x) from generate_series(1,5) x;
  629. -- hypothetical-set type unification and argument-count failures:
  630. select rank(3) within group (order by x) from (values ('fred'),('jim')) v(x);
  631. select rank(3) within group (order by stringu1,stringu2) from tenk1;
  632. select rank('fred') within group (order by x) from generate_series(1,5) x;
  633. select rank('adam'::text collate "C") within group (order by x collate "POSIX")
  634. from (values ('fred'),('jim')) v(x);
  635. -- hypothetical-set type unification successes:
  636. select rank('adam'::varchar) within group (order by x) from (values ('fred'),('jim')) v(x);
  637. select rank('3') within group (order by x) from generate_series(1,5) x;
  638. -- divide by zero check
  639. select percent_rank(0) within group (order by x) from generate_series(1,0) x;
  640. -- deparse and multiple features:
  641. create view aggordview1 as
  642. select ten,
  643. percentile_disc(0.5) within group (order by thousand) as p50,
  644. percentile_disc(0.5) within group (order by thousand) filter (where hundred=1) as px,
  645. rank(5,'AZZZZ',50) within group (order by hundred, string4 desc, hundred)
  646. from tenk1
  647. group by ten order by ten;
  648. select pg_get_viewdef('aggordview1');
  649. select * from aggordview1 order by ten;
  650. drop view aggordview1;
  651. -- variadic aggregates
  652. select least_agg(q1,q2) from int8_tbl;
  653. select least_agg(variadic array[q1,q2]) from int8_tbl;
  654. select cleast_agg(q1,q2) from int8_tbl;
  655. select cleast_agg(4.5,f1) from int4_tbl;
  656. select cleast_agg(variadic array[4.5,f1]) from int4_tbl;
  657. select pg_typeof(cleast_agg(variadic array[4.5,f1])) from int4_tbl;
  658. -- test aggregates with common transition functions share the same states
  659. begin work;
  660. create type avg_state as (total bigint, count bigint);
  661. create or replace function avg_transfn(state avg_state, n int) returns avg_state as
  662. $$
  663. declare new_state avg_state;
  664. begin
  665. raise notice 'avg_transfn called with %', n;
  666. if state is null then
  667. if n is not null then
  668. new_state.total := n;
  669. new_state.count := 1;
  670. return new_state;
  671. end if;
  672. return null;
  673. elsif n is not null then
  674. state.total := state.total + n;
  675. state.count := state.count + 1;
  676. return state;
  677. end if;
  678. return null;
  679. end
  680. $$ language plpgsql;
  681. create function avg_finalfn(state avg_state) returns int4 as
  682. $$
  683. begin
  684. if state is null then
  685. return NULL;
  686. else
  687. return state.total / state.count;
  688. end if;
  689. end
  690. $$ language plpgsql;
  691. create function sum_finalfn(state avg_state) returns int4 as
  692. $$
  693. begin
  694. if state is null then
  695. return NULL;
  696. else
  697. return state.total;
  698. end if;
  699. end
  700. $$ language plpgsql;
  701. create aggregate my_avg(int4)
  702. (
  703. stype = avg_state,
  704. sfunc = avg_transfn,
  705. finalfunc = avg_finalfn
  706. );
  707. create aggregate my_sum(int4)
  708. (
  709. stype = avg_state,
  710. sfunc = avg_transfn,
  711. finalfunc = sum_finalfn
  712. );
  713. -- aggregate state should be shared as aggs are the same.
  714. select my_avg(one),my_avg(one) from (values(1),(3)) t(one);
  715. -- aggregate state should be shared as transfn is the same for both aggs.
  716. select my_avg(one),my_sum(one) from (values(1),(3)) t(one);
  717. -- same as previous one, but with DISTINCT, which requires sorting the input.
  718. select my_avg(distinct one),my_sum(distinct one) from (values(1),(3),(1)) t(one);
  719. -- shouldn't share states due to the distinctness not matching.
  720. select my_avg(distinct one),my_sum(one) from (values(1),(3)) t(one);
  721. -- shouldn't share states due to the filter clause not matching.
  722. select my_avg(one) filter (where one > 1),my_sum(one) from (values(1),(3)) t(one);
  723. -- this should not share the state due to different input columns.
  724. select my_avg(one),my_sum(two) from (values(1,2),(3,4)) t(one,two);
  725. -- exercise cases where OSAs share state
  726. select
  727. percentile_cont(0.5) within group (order by a),
  728. percentile_disc(0.5) within group (order by a)
  729. from (values(1::float8),(3),(5),(7)) t(a);
  730. select
  731. percentile_cont(0.25) within group (order by a),
  732. percentile_disc(0.5) within group (order by a)
  733. from (values(1::float8),(3),(5),(7)) t(a);
  734. -- these can't share state currently
  735. select
  736. rank(4) within group (order by a),
  737. dense_rank(4) within group (order by a)
  738. from (values(1),(3),(5),(7)) t(a);
  739. -- test that aggs with the same sfunc and initcond share the same agg state
  740. create aggregate my_sum_init(int4)
  741. (
  742. stype = avg_state,
  743. sfunc = avg_transfn,
  744. finalfunc = sum_finalfn,
  745. initcond = '(10,0)'
  746. );
  747. create aggregate my_avg_init(int4)
  748. (
  749. stype = avg_state,
  750. sfunc = avg_transfn,
  751. finalfunc = avg_finalfn,
  752. initcond = '(10,0)'
  753. );
  754. create aggregate my_avg_init2(int4)
  755. (
  756. stype = avg_state,
  757. sfunc = avg_transfn,
  758. finalfunc = avg_finalfn,
  759. initcond = '(4,0)'
  760. );
  761. -- state should be shared if INITCONDs are matching
  762. select my_sum_init(one),my_avg_init(one) from (values(1),(3)) t(one);
  763. -- Varying INITCONDs should cause the states not to be shared.
  764. select my_sum_init(one),my_avg_init2(one) from (values(1),(3)) t(one);
  765. rollback;
  766. -- test aggregate state sharing to ensure it works if one aggregate has a
  767. -- finalfn and the other one has none.
  768. begin work;
  769. create or replace function sum_transfn(state int4, n int4) returns int4 as
  770. $$
  771. declare new_state int4;
  772. begin
  773. raise notice 'sum_transfn called with %', n;
  774. if state is null then
  775. if n is not null then
  776. new_state := n;
  777. return new_state;
  778. end if;
  779. return null;
  780. elsif n is not null then
  781. state := state + n;
  782. return state;
  783. end if;
  784. return null;
  785. end
  786. $$ language plpgsql;
  787. create function halfsum_finalfn(state int4) returns int4 as
  788. $$
  789. begin
  790. if state is null then
  791. return NULL;
  792. else
  793. return state / 2;
  794. end if;
  795. end
  796. $$ language plpgsql;
  797. create aggregate my_sum(int4)
  798. (
  799. stype = int4,
  800. sfunc = sum_transfn
  801. );
  802. create aggregate my_half_sum(int4)
  803. (
  804. stype = int4,
  805. sfunc = sum_transfn,
  806. finalfunc = halfsum_finalfn
  807. );
  808. -- Agg state should be shared even though my_sum has no finalfn
  809. select my_sum(one),my_half_sum(one) from (values(1),(2),(3),(4)) t(one);
  810. rollback;
  811. -- test that the aggregate transition logic correctly handles
  812. -- transition / combine functions returning NULL
  813. -- First test the case of a normal transition function returning NULL
  814. BEGIN;
  815. CREATE FUNCTION balkifnull(int8, int4)
  816. RETURNS int8
  817. STRICT
  818. LANGUAGE plpgsql AS $$
  819. BEGIN
  820. IF $1 IS NULL THEN
  821. RAISE 'erroneously called with NULL argument';
  822. END IF;
  823. RETURN NULL;
  824. END$$;
  825. CREATE AGGREGATE balk(int4)
  826. (
  827. SFUNC = balkifnull(int8, int4),
  828. STYPE = int8,
  829. PARALLEL = SAFE,
  830. INITCOND = '0'
  831. );
  832. SELECT balk(hundred) FROM tenk1;
  833. ROLLBACK;
  834. -- Secondly test the case of a parallel aggregate combiner function
  835. -- returning NULL. For that use normal transition function, but a
  836. -- combiner function returning NULL.
  837. BEGIN;
  838. CREATE FUNCTION balkifnull(int8, int8)
  839. RETURNS int8
  840. PARALLEL SAFE
  841. STRICT
  842. LANGUAGE plpgsql AS $$
  843. BEGIN
  844. IF $1 IS NULL THEN
  845. RAISE 'erroneously called with NULL argument';
  846. END IF;
  847. RETURN NULL;
  848. END$$;
  849. CREATE AGGREGATE balk(int4)
  850. (
  851. SFUNC = int4_sum(int8, int4),
  852. STYPE = int8,
  853. COMBINEFUNC = balkifnull(int8, int8),
  854. PARALLEL = SAFE,
  855. INITCOND = '0'
  856. );
  857. -- force use of parallelism
  858. ALTER TABLE tenk1 set (parallel_workers = 4);
  859. SET LOCAL parallel_setup_cost=0;
  860. SET LOCAL max_parallel_workers_per_gather=4;
  861. EXPLAIN (COSTS OFF) SELECT balk(hundred) FROM tenk1;
  862. SELECT balk(hundred) FROM tenk1;
  863. ROLLBACK;
  864. -- test coverage for aggregate combine/serial/deserial functions
  865. BEGIN;
  866. SET parallel_setup_cost = 0;
  867. SET parallel_tuple_cost = 0;
  868. SET min_parallel_table_scan_size = 0;
  869. SET max_parallel_workers_per_gather = 4;
  870. SET parallel_leader_participation = off;
  871. SET enable_indexonlyscan = off;
  872. -- variance(int4) covers numeric_poly_combine
  873. -- sum(int8) covers int8_avg_combine
  874. -- regr_count(float8, float8) covers int8inc_float8_float8 and aggregates with > 1 arg
  875. EXPLAIN (COSTS OFF, VERBOSE)
  876. SELECT variance(unique1::int4), sum(unique1::int8), regr_count(unique1::float8, unique1::float8)
  877. FROM (SELECT * FROM tenk1
  878. UNION ALL SELECT * FROM tenk1
  879. UNION ALL SELECT * FROM tenk1
  880. UNION ALL SELECT * FROM tenk1) u;
  881. SELECT variance(unique1::int4), sum(unique1::int8), regr_count(unique1::float8, unique1::float8)
  882. FROM (SELECT * FROM tenk1
  883. UNION ALL SELECT * FROM tenk1
  884. UNION ALL SELECT * FROM tenk1
  885. UNION ALL SELECT * FROM tenk1) u;
  886. -- variance(int8) covers numeric_combine
  887. -- avg(numeric) covers numeric_avg_combine
  888. EXPLAIN (COSTS OFF, VERBOSE)
  889. SELECT variance(unique1::int8), avg(unique1::numeric)
  890. FROM (SELECT * FROM tenk1
  891. UNION ALL SELECT * FROM tenk1
  892. UNION ALL SELECT * FROM tenk1
  893. UNION ALL SELECT * FROM tenk1) u;
  894. SELECT variance(unique1::int8), avg(unique1::numeric)
  895. FROM (SELECT * FROM tenk1
  896. UNION ALL SELECT * FROM tenk1
  897. UNION ALL SELECT * FROM tenk1
  898. UNION ALL SELECT * FROM tenk1) u;
  899. ROLLBACK;
  900. -- test coverage for dense_rank
  901. SELECT dense_rank(x) WITHIN GROUP (ORDER BY x) FROM (VALUES (1),(1),(2),(2),(3),(3)) v(x) GROUP BY (x) ORDER BY 1;
  902. -- Ensure that the STRICT checks for aggregates does not take NULLness
  903. -- of ORDER BY columns into account. See bug report around
  904. -- 2a505161-2727-2473-7c46-591ed108ac52@email.cz
  905. SELECT min(x ORDER BY y) FROM (VALUES(1, NULL)) AS d(x,y);
  906. SELECT min(x ORDER BY y) FROM (VALUES(1, 2)) AS d(x,y);
  907. -- check collation-sensitive matching between grouping expressions
  908. select v||'a', case v||'a' when 'aa' then 1 else 0 end, count(*)
  909. from unnest(array['a','b']) u(v)
  910. group by v||'a' order by 1;
  911. select v||'a', case when v||'a' = 'aa' then 1 else 0 end, count(*)
  912. from unnest(array['a','b']) u(v)
  913. group by v||'a' order by 1;
  914. -- Make sure that generation of HashAggregate for uniqification purposes
  915. -- does not lead to array overflow due to unexpected duplicate hash keys
  916. -- see CAFeeJoKKu0u+A_A9R9316djW-YW3-+Gtgvy3ju655qRHR3jtdA@mail.gmail.com
  917. set enable_memoize to off;
  918. explain (costs off)
  919. select 1 from tenk1
  920. where (hundred, thousand) in (select twothousand, twothousand from onek);
  921. reset enable_memoize;
  922. --
  923. -- Hash Aggregation Spill tests
  924. --
  925. set enable_sort=false;
  926. set work_mem='64kB';
  927. select unique1, count(*), sum(twothousand) from tenk1
  928. group by unique1
  929. having sum(fivethous) > 4975
  930. order by sum(twothousand);
  931. set work_mem to default;
  932. set enable_sort to default;
  933. --
  934. -- Compare results between plans using sorting and plans using hash
  935. -- aggregation. Force spilling in both cases by setting work_mem low.
  936. --
  937. set work_mem='64kB';
  938. create table agg_data_2k as
  939. select g from generate_series(0, 1999) g;
  940. analyze agg_data_2k;
  941. create table agg_data_20k as
  942. select g from generate_series(0, 19999) g;
  943. analyze agg_data_20k;
  944. -- Produce results with sorting.
  945. set enable_hashagg = false;
  946. set jit_above_cost = 0;
  947. explain (costs off)
  948. select g%10000 as c1, sum(g::numeric) as c2, count(*) as c3
  949. from agg_data_20k group by g%10000;
  950. create table agg_group_1 as
  951. select g%10000 as c1, sum(g::numeric) as c2, count(*) as c3
  952. from agg_data_20k group by g%10000;
  953. create table agg_group_2 as
  954. select * from
  955. (values (100), (300), (500)) as r(a),
  956. lateral (
  957. select (g/2)::numeric as c1,
  958. array_agg(g::numeric) as c2,
  959. count(*) as c3
  960. from agg_data_2k
  961. where g < r.a
  962. group by g/2) as s;
  963. set jit_above_cost to default;
  964. create table agg_group_3 as
  965. select (g/2)::numeric as c1, sum(7::int4) as c2, count(*) as c3
  966. from agg_data_2k group by g/2;
  967. create table agg_group_4 as
  968. select (g/2)::numeric as c1, array_agg(g::numeric) as c2, count(*) as c3
  969. from agg_data_2k group by g/2;
  970. -- Produce results with hash aggregation
  971. set enable_hashagg = true;
  972. set enable_sort = false;
  973. set jit_above_cost = 0;
  974. explain (costs off)
  975. select g%10000 as c1, sum(g::numeric) as c2, count(*) as c3
  976. from agg_data_20k group by g%10000;
  977. create table agg_hash_1 as
  978. select g%10000 as c1, sum(g::numeric) as c2, count(*) as c3
  979. from agg_data_20k group by g%10000;
  980. create table agg_hash_2 as
  981. select * from
  982. (values (100), (300), (500)) as r(a),
  983. lateral (
  984. select (g/2)::numeric as c1,
  985. array_agg(g::numeric) as c2,
  986. count(*) as c3
  987. from agg_data_2k
  988. where g < r.a
  989. group by g/2) as s;
  990. set jit_above_cost to default;
  991. create table agg_hash_3 as
  992. select (g/2)::numeric as c1, sum(7::int4) as c2, count(*) as c3
  993. from agg_data_2k group by g/2;
  994. create table agg_hash_4 as
  995. select (g/2)::numeric as c1, array_agg(g::numeric) as c2, count(*) as c3
  996. from agg_data_2k group by g/2;
  997. set enable_sort = true;
  998. set work_mem to default;
  999. -- Compare group aggregation results to hash aggregation results
  1000. (select * from agg_hash_1 except select * from agg_group_1)
  1001. union all
  1002. (select * from agg_group_1 except select * from agg_hash_1);
  1003. (select * from agg_hash_2 except select * from agg_group_2)
  1004. union all
  1005. (select * from agg_group_2 except select * from agg_hash_2);
  1006. (select * from agg_hash_3 except select * from agg_group_3)
  1007. union all
  1008. (select * from agg_group_3 except select * from agg_hash_3);
  1009. (select * from agg_hash_4 except select * from agg_group_4)
  1010. union all
  1011. (select * from agg_group_4 except select * from agg_hash_4);
  1012. drop table agg_group_1;
  1013. drop table agg_group_2;
  1014. drop table agg_group_3;
  1015. drop table agg_group_4;
  1016. drop table agg_hash_1;
  1017. drop table agg_hash_2;
  1018. drop table agg_hash_3;
  1019. drop table agg_hash_4;