aggregate.slt 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. # Copyright 2015 - 2019 The Cockroach Authors. All rights reserved.
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # This file is derived from the logic test suite in CockroachDB. The
  12. # original file was retrieved on June 10, 2019 from:
  13. #
  14. # https://github.com/cockroachdb/cockroach/blob/d2f7fbf5dd1fc1a099bbad790a2e1f7c60a66cc3/pkg/sql/logictest/testdata/logic_test/aggregate
  15. #
  16. # The original source code is subject to the terms of the Apache
  17. # 2.0 license, a copy of which can be found in the LICENSE file at the
  18. # root of this repository.
  19. mode cockroach
  20. subtest other
  21. simple conn=mz_system,user=mz_system
  22. ALTER SYSTEM SET unsafe_enable_table_keys = true
  23. ----
  24. COMPLETE 0
  25. statement ok
  26. CREATE TABLE kv (
  27. k INT PRIMARY KEY,
  28. v INT,
  29. w INT,
  30. s TEXT
  31. )
  32. # Aggregate functions return NULL if there are no rows.
  33. query IIIRRRR
  34. SELECT min(1), max(1), count(1), avg(1), sum(1), stddev(1), variance(1) FROM kv
  35. ----
  36. NULL NULL 0 NULL NULL NULL NULL
  37. # Regression test for materialize#29695
  38. query T
  39. SELECT min(NULL)
  40. ----
  41. NULL
  42. # Aggregate functions return NULL if there are no rows.
  43. query T
  44. SELECT array_agg(1) FROM kv
  45. ----
  46. NULL
  47. statement error db error: ERROR: function "json_agg" does not exist
  48. SELECT json_agg(1) FROM kv
  49. ----
  50. NULL
  51. query T
  52. SELECT jsonb_agg(1) FROM kv
  53. ----
  54. NULL
  55. query IIIRRRR
  56. SELECT min(v), max(v), count(v), avg(v), sum(v), stddev(v), variance(v) FROM kv
  57. ----
  58. NULL NULL 0 NULL NULL NULL NULL
  59. query B
  60. SELECT bool_and(v = 1) FROM kv
  61. ----
  62. NULL
  63. query B
  64. SELECT bool_or(v = 1) FROM kv
  65. ----
  66. NULL
  67. query T
  68. SELECT array_agg(v) FROM kv
  69. ----
  70. NULL
  71. statement error db error: ERROR: function "json_agg" does not exist
  72. SELECT json_agg(v) FROM kv
  73. ----
  74. NULL
  75. query T
  76. SELECT jsonb_agg(v) FROM kv
  77. ----
  78. NULL
  79. # Aggregate functions triggers aggregation and computation when there is no source.
  80. query IIIRRRR
  81. SELECT min(1), count(1), max(1), avg(1)::float, sum(1), stddev(1), variance(1)
  82. ----
  83. 1 1 1 1 1 NULL NULL
  84. # Aggregate functions triggers aggregation and computation when there is no source.
  85. query T
  86. SELECT array_agg(1)
  87. ----
  88. {1}
  89. statement error db error: ERROR: function "json_agg" does not exist
  90. SELECT json_agg(1)
  91. query T
  92. SELECT jsonb_agg(1)
  93. ----
  94. [1]
  95. # Some aggregate functions are not normalized to NULL when given a NULL
  96. # argument.
  97. query I
  98. SELECT count(NULL)
  99. ----
  100. 0
  101. statement error db error: ERROR: function "json_agg" does not exist
  102. SELECT json_agg(NULL)
  103. query T
  104. SELECT jsonb_agg(NULL)
  105. ----
  106. [null]
  107. query error db error: ERROR: function array_agg\(unknown\) is not unique
  108. SELECT array_agg(NULL)
  109. # With an explicit cast, this works as expected.
  110. query T
  111. SELECT array_agg(NULL::TEXT)
  112. ----
  113. {NULL}
  114. # Regression test for materialize#25724 (problem with typed NULLs and distsql planning).
  115. # The previous query doesn't run under distsql.
  116. query T
  117. SELECT array_agg(NULL::TEXT) FROM (VALUES (1)) AS t(x)
  118. ----
  119. {NULL}
  120. # Check that COALESCE using aggregate results over an empty table
  121. # work properly.
  122. query I
  123. SELECT COALESCE(max(1), 0) FROM generate_series(1,0)
  124. ----
  125. 0
  126. # Same, using arithmetic on COUNT.
  127. query I
  128. SELECT 1 + count(*) FROM generate_series(1,0)
  129. ----
  130. 1
  131. # Same, using an empty table.
  132. # The following test *must* occur before the first INSERT to the tables,
  133. # so that it can observe an empty table.
  134. query II
  135. SELECT count(*), COALESCE(max(k), 1) FROM kv
  136. ----
  137. 0 1
  138. # Same, using a subquery. (materialize#12705)
  139. query I
  140. SELECT (SELECT COALESCE(max(1), 0) FROM generate_series(1,0))
  141. ----
  142. 0
  143. statement OK
  144. INSERT INTO kv VALUES
  145. (1, 2, 3, 'a'),
  146. (3, 4, 5, 'a'),
  147. (5, NULL, 5, NULL),
  148. (6, 2, 3, 'b'),
  149. (7, 2, 2, 'b'),
  150. (8, 4, 2, 'A')
  151. # Aggregate functions triggers aggregation and computation for every row even when applied to a constant.
  152. query IIIRRRR
  153. SELECT min(1), count(1), max(1), avg(1)::float, sum(1), stddev(1)::float, variance(1)::float FROM kv
  154. ----
  155. 1 6 1 1 6 0 0
  156. # Aggregate functions triggers aggregation and computation for every row even when applied to a constant.
  157. query T
  158. SELECT array_agg(1) FROM kv
  159. ----
  160. {1,1,1,1,1,1}
  161. statement error db error: ERROR: function "json_agg" does not exist
  162. SELECT json_agg(1) FROM kv
  163. query T
  164. SELECT jsonb_agg(1) FROM kv
  165. ----
  166. [1,1,1,1,1,1]
  167. # Even with no aggregate functions, grouping occurs in the presence of GROUP BY.
  168. query I rowsort
  169. SELECT 1 FROM kv GROUP BY v
  170. ----
  171. 1
  172. 1
  173. 1
  174. # Presence of HAVING triggers aggregation, reducing results to one row (even without GROUP BY).
  175. query I rowsort
  176. SELECT 3 FROM kv HAVING TRUE
  177. ----
  178. 3
  179. query error column "kv.k" must appear in the GROUP BY clause or be used in an aggregate function
  180. SELECT count(*), k FROM kv
  181. # database-issues#1036
  182. # query error unsupported comparison operator: <string> < <int>
  183. # SELECT count(*) FROM kv GROUP BY s < 5
  184. query II rowsort
  185. SELECT count(*), k FROM kv GROUP BY k
  186. ----
  187. 1 1
  188. 1 3
  189. 1 5
  190. 1 6
  191. 1 7
  192. 1 8
  193. # GROUP BY specified using column index works.
  194. query II rowsort
  195. SELECT count(*), k FROM kv GROUP BY 2
  196. ----
  197. 1 1
  198. 1 3
  199. 1 5
  200. 1 6
  201. 1 7
  202. 1 8
  203. query error aggregate functions are not allowed in GROUP BY
  204. SELECT * FROM kv GROUP BY v, count(DISTINCT w)
  205. query error aggregate functions are not allowed in GROUP BY
  206. SELECT count(DISTINCT w) FROM kv GROUP BY 1
  207. query error aggregate functions are not allowed in RETURNING clause \(function pg_catalog.sum\)
  208. INSERT INTO kv (k, v) VALUES (99, 100) RETURNING sum(v)
  209. query error aggregate functions are not allowed in LIMIT \(function pg_catalog.sum\)
  210. SELECT sum(v) FROM kv GROUP BY k LIMIT sum(v)
  211. query error db error: ERROR: aggregate functions are not allowed in OFFSET \(function pg_catalog\.sum\)
  212. SELECT sum(v) FROM kv GROUP BY k LIMIT 1 OFFSET sum(v)
  213. query error aggregate functions are not allowed in VALUES
  214. INSERT INTO kv (k, v) VALUES (99, count(1))
  215. query error pgcode 42P10 column reference 5 in GROUP BY clause is out of range \(1 - 2\)
  216. SELECT count(*), k FROM kv GROUP BY 5
  217. query error pgcode 42P10 column reference 0 in GROUP BY clause is out of range \(1 - 2\)
  218. SELECT count(*), k FROM kv GROUP BY 0
  219. # unsure about spec, but this is consistent with our stance of always treating GROUP BY as an expr
  220. # query error pgcode 42601 non-integer constant in GROUP BY
  221. # SELECT 1 GROUP BY 'a'
  222. # Qualifying a name in the SELECT, the GROUP BY, both or neither should not affect validation.
  223. query IT rowsort
  224. SELECT count(*), kv.s FROM kv GROUP BY s
  225. ----
  226. 1 A
  227. 1 NULL
  228. 2 a
  229. 2 b
  230. query IT rowsort
  231. SELECT count(*), s FROM kv GROUP BY kv.s
  232. ----
  233. 1 A
  234. 1 NULL
  235. 2 a
  236. 2 b
  237. query IT rowsort
  238. SELECT count(*), kv.s FROM kv GROUP BY kv.s
  239. ----
  240. 1 A
  241. 1 NULL
  242. 2 a
  243. 2 b
  244. query IT rowsort
  245. SELECT count(*), s FROM kv GROUP BY s
  246. ----
  247. 1 A
  248. 1 NULL
  249. 2 a
  250. 2 b
  251. # Grouping by more than one column works.
  252. query III rowsort
  253. SELECT v, count(*), w FROM kv GROUP BY v, w
  254. ----
  255. 2 1 2
  256. 2 2 3
  257. 4 1 2
  258. 4 1 5
  259. NULL 1 5
  260. # Grouping by more than one column using column numbers works.
  261. query III rowsort
  262. SELECT v, count(*), w FROM kv GROUP BY 1, 3
  263. ----
  264. 2 1 2
  265. 2 2 3
  266. 4 1 2
  267. 4 1 5
  268. NULL 1 5
  269. # Selecting and grouping on a function expression works.
  270. query IT rowsort
  271. SELECT count(*), length(s) FROM kv GROUP BY length(s)
  272. ----
  273. 1 NULL
  274. 5 1
  275. # Selecting and grouping on a constant works.
  276. query I
  277. SELECT count(*) FROM kv GROUP BY 1+2
  278. ----
  279. 6
  280. query I
  281. SELECT count(*) FROM kv GROUP BY length('abc')
  282. ----
  283. 6
  284. # Selecting a function of something which is grouped works.
  285. query IT rowsort
  286. SELECT count(*), length(s) FROM kv GROUP BY s
  287. ----
  288. 1 1
  289. 1 NULL
  290. 2 1
  291. 2 1
  292. # Selecting a value that is not grouped, even if a function of it it, does not work.
  293. query error column "kv.s" must appear in the GROUP BY clause or be used in an aggregate function
  294. SELECT count(*), s FROM kv GROUP BY length(s)
  295. # Selecting and grouping on a more complex expression works.
  296. query II rowsort
  297. SELECT count(*), k+v FROM kv GROUP BY k+v
  298. ----
  299. 1 12
  300. 1 3
  301. 1 7
  302. 1 8
  303. 1 9
  304. 1 NULL
  305. # Selecting a more complex expression, made up of things which are each grouped, works.
  306. query II rowsort
  307. SELECT count(*), k+v FROM kv GROUP BY k, v
  308. ----
  309. 1 12
  310. 1 3
  311. 1 7
  312. 1 8
  313. 1 9
  314. 1 NULL
  315. query error column "kv.v" must appear in the GROUP BY clause or be used in an aggregate function
  316. SELECT count(*), k+v FROM kv GROUP BY k
  317. query error column "kv.k" must appear in the GROUP BY clause or be used in an aggregate function
  318. SELECT count(*), k+v FROM kv GROUP BY v
  319. query error column "kv.v" must appear in the GROUP BY clause or be used in an aggregate function
  320. SELECT count(*), v/(k+v) FROM kv GROUP BY k+v
  321. query error aggregate functions are not allowed in WHERE
  322. SELECT k FROM kv WHERE avg(k) > 1
  323. query error nested aggregate functions are not allowed
  324. SELECT max(avg(k)) FROM kv
  325. # Test case from materialize#2761.
  326. query II rowsort
  327. SELECT count(kv.k) AS count_1, kv.v + kv.w AS lx FROM kv GROUP BY kv.v + kv.w
  328. ----
  329. 1 4
  330. 1 6
  331. 1 9
  332. 1 NULL
  333. 2 5
  334. query TI rowsort
  335. SELECT s, count(*) FROM kv GROUP BY s HAVING count(*) > 1
  336. ----
  337. a 2
  338. b 2
  339. query TII rowsort
  340. SELECT
  341. length(s),
  342. count(DISTINCT s),
  343. count(DISTINCT length(s))
  344. FROM kv GROUP BY length(s) HAVING count(DISTINCT s) > 1
  345. ----
  346. 1 3 1
  347. query II rowsort
  348. SELECT max(k), min(v) FROM kv HAVING min(v) > 2
  349. ----
  350. query II rowsort
  351. SELECT max(k), min(v) FROM kv HAVING max(v) > 2
  352. ----
  353. 8 2
  354. query error nested aggregate functions are not allowed
  355. SELECT max(k), min(v) FROM kv HAVING max(min(v)) > 2
  356. query error column "kv.k" must appear in the GROUP BY clause or be used in an aggregate function
  357. SELECT max(k), min(v) FROM kv HAVING k
  358. # Expressions listed in the HAVING clause must conform to same validation as the SELECT clause (grouped or aggregated).
  359. query error column "kv.k" must appear in the GROUP BY clause or be used in an aggregate function
  360. SELECT 3 FROM kv GROUP BY v HAVING k > 5
  361. # pg has a special case for grouping on primary key, which would allow this, but we do not.
  362. # See http://www.postgresql.org/docs/current/static/sql-select.html#SQL-GROUPBY
  363. query error column "kv.v" must appear in the GROUP BY clause or be used in an aggregate function
  364. SELECT 3 FROM kv GROUP BY k HAVING v > 2
  365. query error column "kv.k" must appear in the GROUP BY clause or be used in an aggregate function
  366. SELECT k FROM kv HAVING k > 7
  367. query error Expected right parenthesis, found comma
  368. SELECT count(*, 1) FROM kv
  369. query I
  370. SELECT count(*)
  371. ----
  372. 1
  373. query I
  374. SELECT count(k) from kv
  375. ----
  376. 6
  377. query I
  378. SELECT count(1)
  379. ----
  380. 1
  381. query I
  382. SELECT count(1) from kv
  383. ----
  384. 6
  385. query error db error: ERROR: function count\(integer, integer\) does not exist
  386. SELECT count(k, v) FROM kv
  387. # Note: Result differs from Cockroach but matches Postgres.
  388. query II
  389. SELECT v, count(k) FROM kv GROUP BY v ORDER BY v
  390. ----
  391. 2 3
  392. 4 2
  393. NULL 1
  394. # Note: Result differs from Cockroach but matches Postgres.
  395. query II
  396. SELECT v, count(k) FROM kv GROUP BY v ORDER BY v DESC
  397. ----
  398. NULL 1
  399. 4 2
  400. 2 3
  401. # Note: Result differs from Cockroach but matches Postgres.
  402. query II
  403. SELECT v, count(k) FROM kv GROUP BY v ORDER BY count(k) DESC
  404. ----
  405. 2 3
  406. 4 2
  407. NULL 1
  408. # Note: Result differs from Cockroach but matches Postgres.
  409. query II
  410. SELECT v, count(k) FROM kv GROUP BY v ORDER BY v-count(k)
  411. ----
  412. 2 3
  413. 4 2
  414. NULL 1
  415. # Note: Result differs from Cockroach but matches Postgres.
  416. query II
  417. SELECT v, count(k) FROM kv GROUP BY v ORDER BY 1 DESC
  418. ----
  419. NULL 1
  420. 4 2
  421. 2 3
  422. query III colnames
  423. SELECT count(*), count(k), count(kv.v) FROM kv
  424. ----
  425. count count count
  426. 6 6 5
  427. query I
  428. SELECT count(kv.*) FROM kv
  429. ----
  430. 6
  431. query III
  432. SELECT count(DISTINCT k), count(DISTINCT v), count(DISTINCT (v)) FROM kv
  433. ----
  434. 6 2 2
  435. query TIII rowsort
  436. SELECT length(s), count(DISTINCT k), count(DISTINCT v), count(DISTINCT (v)) FROM kv GROUP BY length(s)
  437. ----
  438. 1 5 2 2
  439. NULL 1 0 0
  440. # database-issues#414
  441. # query I
  442. # SELECT count((k, v)) FROM kv
  443. # ----
  444. # 6
  445. #
  446. # query I
  447. # SELECT count(DISTINCT (k, v)) FROM kv
  448. # ----
  449. # 6
  450. #
  451. # query I
  452. # SELECT count(DISTINCT (k, (v))) FROM kv
  453. # ----
  454. # 6
  455. #
  456. # query I
  457. # SELECT count((k, v)) FROM kv LIMIT 1
  458. # ----
  459. # 6
  460. #
  461. # query I
  462. # SELECT count((k, v)) FROM kv OFFSET 1
  463. # ----
  464. #
  465. # query II
  466. # SELECT count(NULL::int), count((NULL, NULL))
  467. # ----
  468. # 0 1
  469. query I
  470. SELECT count(*) FROM kv a, kv b
  471. ----
  472. 36
  473. query I
  474. SELECT count(DISTINCT a.*) FROM kv a, kv b
  475. ----
  476. 6
  477. query I
  478. SELECT count(k)+count(kv.v) FROM kv
  479. ----
  480. 11
  481. query IIII
  482. SELECT min(k), max(k), min(v), max(v) FROM kv
  483. ----
  484. 1 8 2 4
  485. # Even if no input rows match, we expect a row (of nulls).
  486. query IIII
  487. SELECT min(k), max(k), min(v), max(v) FROM kv WHERE k > 8
  488. ----
  489. NULL NULL NULL NULL
  490. query TT
  491. SELECT array_agg(k), array_agg(s) FROM (SELECT k, s FROM kv ORDER BY k)
  492. ----
  493. {1,3,5,6,7,8} {A,a,a,b,b,NULL}
  494. query error db error: ERROR: operator does not exist: integer\[\] \|\| integer
  495. SELECT array_agg(k) || 1 FROM (SELECT k FROM kv ORDER BY k)
  496. query T
  497. SELECT array_agg(s) FROM kv WHERE s IS NULL
  498. ----
  499. {NULL}
  500. query error db error: ERROR: function "json_agg" does not exist
  501. SELECT json_agg(s) FROM kv WHERE s IS NULL
  502. query T
  503. SELECT jsonb_agg(s) FROM kv WHERE s IS NULL
  504. ----
  505. [null]
  506. query RRRR
  507. SELECT avg(k)::FLOAT, avg(v)::FLOAT, sum(k)::FLOAT, sum(v)::FLOAT FROM kv
  508. ----
  509. 5 2.8 30 14
  510. query RRRR
  511. SELECT
  512. avg(k::decimal)::float,
  513. avg(v::decimal)::float,
  514. sum(k::decimal)::float,
  515. sum(v::decimal)::float
  516. FROM kv
  517. ----
  518. 5 2.8 30 14
  519. query RRRR
  520. SELECT
  521. avg(DISTINCT k)::FLOAT,
  522. avg(DISTINCT v)::FLOAT,
  523. sum(DISTINCT k)::FLOAT,
  524. sum(DISTINCT v)::FLOAT
  525. FROM kv
  526. ----
  527. 5 3 30 6
  528. query R
  529. SELECT (avg(k) * 2.0 + max(v)::DECIMAL)::FLOAT FROM kv
  530. ----
  531. 14
  532. # Verify things work with distsql when some of the nodes emit no results in the
  533. # local stage.
  534. query R
  535. SELECT (avg(k) * 2.0 + max(v)::DECIMAL)::FLOAT FROM kv WHERE w*2 = k
  536. ----
  537. 14
  538. # Grouping columns can be eliminated, but should still return zero rows (i.e.
  539. # shouldn't use scalar GroupBy).
  540. query I
  541. SELECT max(v) FROM kv GROUP BY k HAVING k=100
  542. ----
  543. # Same query as above, but using scalar GroupBy (should return default row).
  544. query I
  545. SELECT max(v) FROM kv WHERE k=100
  546. ----
  547. NULL
  548. statement ok
  549. CREATE TABLE abc (
  550. a VARCHAR PRIMARY KEY,
  551. b FLOAT,
  552. c BOOLEAN
  553. )
  554. statement ok
  555. INSERT INTO abc VALUES ('one', 1.5, true), ('two', 2.0, false)
  556. query TRB
  557. SELECT min(a), min(b), min(c) FROM abc
  558. ----
  559. one 1.5 false
  560. query TRB
  561. SELECT max(a), max(b), max(c) FROM abc
  562. ----
  563. two 2 true
  564. query RR
  565. SELECT avg(b), sum(b) FROM abc
  566. ----
  567. 1.75 3.5
  568. # not supported yet
  569. # Verify summing of intervals
  570. # statement ok
  571. # CREATE TABLE intervals (
  572. # a INTERVAL PRIMARY KEY
  573. # )
  574. #
  575. # statement ok
  576. # INSERT INTO intervals VALUES (INTERVAL '1 year 2 months 3 days 4 seconds'), (INTERVAL '2 year 3 months 4 days 5 seconds'), (INTERVAL '10000ms')
  577. #
  578. # query T
  579. # SELECT sum(a) FROM intervals
  580. # ----
  581. # 3 years 5 mons 7 days 00:00:19
  582. query error db error: ERROR: function sum\(varchar\) does not exist
  583. SELECT avg(a) FROM abc
  584. query error db error: ERROR: function sum\(boolean\) does not exist
  585. SELECT avg(c) FROM abc
  586. query error db error: ERROR: function sum\(record\(f1: varchar,f2: boolean\?\)\) does not exist
  587. SELECT avg((a,c)) FROM abc
  588. query error db error: ERROR: function sum\(varchar\) does not exist
  589. SELECT sum(a) FROM abc
  590. query error db error: ERROR: function sum\(boolean\) does not exist
  591. SELECT sum(c) FROM abc
  592. query error db error: ERROR: function sum\(record\(f1: varchar,f2: boolean\?\)\) does not exist
  593. SELECT sum((a,c)) FROM abc
  594. statement ok
  595. CREATE TABLE xyz (
  596. x INT PRIMARY KEY,
  597. y INT,
  598. z FLOAT,
  599. w INT
  600. )
  601. statement ok
  602. INSERT INTO xyz VALUES (1, 2, 3.0, NULL), (4, 5, 6.0, 2), (7, NULL, 8.0, 3)
  603. query I
  604. SELECT min(x) FROM xyz
  605. ----
  606. 1
  607. query I
  608. SELECT min(y) FROM xyz
  609. ----
  610. 2
  611. query I
  612. SELECT min(w) FROM xyz
  613. ----
  614. 2
  615. query I
  616. SELECT min(x) FROM xyz WHERE x in (0, 4, 7)
  617. ----
  618. 4
  619. query I
  620. SELECT max(x) FROM xyz
  621. ----
  622. 7
  623. query I
  624. SELECT min(y) FROM xyz WHERE x = 1
  625. ----
  626. 2
  627. query I
  628. SELECT max(y) FROM xyz WHERE x = 1
  629. ----
  630. 2
  631. query I
  632. SELECT min(y) FROM xyz WHERE x = 7
  633. ----
  634. NULL
  635. query I
  636. SELECT max(y) FROM xyz WHERE x = 7
  637. ----
  638. NULL
  639. # database-issues#414
  640. # query I
  641. # SELECT min(x) FROM xyz WHERE (y, z) = (2, 3.0)
  642. # ----
  643. # 1
  644. #
  645. # query I
  646. # SELECT max(x) FROM xyz WHERE (z, y) = (3.0, 2)
  647. # ----
  648. # 1
  649. # VARIANCE/STDDEV
  650. query RRR
  651. SELECT variance(x)::FLOAT, variance(y::decimal)::FLOAT, variance(z)::DECIMAL(38, 14) FROM xyz
  652. ----
  653. 9 4.5 6.33333333333334
  654. query R
  655. SELECT variance(x) FROM xyz WHERE x = 10
  656. ----
  657. NULL
  658. query R
  659. SELECT variance(x) FROM xyz WHERE x = 1
  660. ----
  661. NULL
  662. query RRR
  663. SELECT stddev(x)::FLOAT, stddev(y::decimal)::FLOAT, stddev(z)::DECIMAL(38, 14) FROM xyz
  664. ----
  665. 3 2.1213203435596424 2.51661147842358
  666. query R
  667. SELECT stddev(x) FROM xyz WHERE x = 1
  668. ----
  669. NULL
  670. # Numerical stability test for VARIANCE/STDDEV.
  671. # See https://www.johndcook.com/blog/2008/09/28/theoretical-explanation-for-numerical-results.
  672. # Avoid using random() since we do not have the deterministic option to specify a pseudo-random seed yet.
  673. # Note under distsql, this is non-deterministic since the running variance/stddev algorithms depend on
  674. # the local sum of squared difference values which depend on how the data is distributed across the distsql nodes.
  675. statement ok
  676. CREATE TABLE mnop (
  677. m INT PRIMARY KEY,
  678. n FLOAT,
  679. o DECIMAL,
  680. p BIGINT
  681. )
  682. # not supported yet
  683. # statement ok
  684. # INSERT INTO mnop (m, n) SELECT i, (1e9 + i/2e4)::float FROM
  685. # generate_series(1, 2e4) AS i(i)
  686. #
  687. # statement ok
  688. # UPDATE mnop SET o = n::decimal, p = (n * 10)::bigint
  689. #
  690. # query RRR
  691. # SELECT round(variance(n), 2), round(variance(n), 2), round(variance(p)) FROM mnop
  692. # ----
  693. # 0.08 0.08 8
  694. #
  695. #
  696. # query RRR
  697. # SELECT round(stddev(n), 2), round(stddev(n), 2), round(stddev(p)) FROM mnop
  698. # ----
  699. # 0.29 0.29 3
  700. query RRR
  701. SELECT avg(1::int)::float, avg(2::float)::float, avg(3::decimal)::float
  702. ----
  703. 1 2 3
  704. query III
  705. SELECT count(2::int), count(3::float), count(4::decimal)
  706. ----
  707. 1 1 1
  708. query RRR
  709. SELECT sum(1::int), sum(2::float), sum(3::decimal)
  710. ----
  711. 1 2 3
  712. query RRR
  713. SELECT variance(1::int), variance(1::float), variance(1::decimal)
  714. ----
  715. NULL NULL NULL
  716. query RRR
  717. SELECT stddev(1::int), stddev(1::float), stddev(1::decimal)
  718. ----
  719. NULL NULL NULL
  720. # Ensure subqueries don't trigger aggregation.
  721. query B
  722. SELECT x > (SELECT avg(0)) FROM xyz LIMIT 1
  723. ----
  724. true
  725. statement ok
  726. CREATE TABLE bools (b BOOL)
  727. query BB
  728. SELECT bool_and(b), bool_or(b) FROM bools
  729. ----
  730. NULL NULL
  731. statement OK
  732. INSERT INTO bools VALUES (true), (true), (true)
  733. query BB
  734. SELECT bool_and(b), bool_or(b) FROM bools
  735. ----
  736. true true
  737. statement OK
  738. INSERT INTO bools VALUES (false), (false)
  739. query BB
  740. SELECT bool_and(b), bool_or(b) FROM bools
  741. ----
  742. false true
  743. statement OK
  744. DELETE FROM bools WHERE b
  745. query BB
  746. SELECT bool_and(b), bool_or(b) FROM bools
  747. ----
  748. false false
  749. query error concat_agg not yet supported
  750. SELECT concat_agg(s) FROM (SELECT s FROM kv ORDER BY k)
  751. query error db error: ERROR: function "json_agg" does not exist
  752. SELECT json_agg(s) FROM (SELECT s FROM kv ORDER BY k)
  753. # This ORDER BY doesn't have to be respected, because the relation returned by a subquery is an inherently unordered
  754. # thing. Postgres docs: https://www.postgresql.org/docs/13/functions-aggregate.html
  755. # which says that "supplying the input values from a sorted subquery will usually work", which sounds like this is
  756. # indeed not mandated.
  757. query T
  758. SELECT jsonb_agg(s) FROM (SELECT s FROM kv ORDER BY k)
  759. ----
  760. ["A","a","a","b","b",null]
  761. # Verify that FILTER works.
  762. statement ok
  763. CREATE TABLE filter_test (
  764. k INT,
  765. v INT,
  766. mark BOOL
  767. )
  768. statement OK
  769. INSERT INTO filter_test VALUES
  770. (1, 2, false),
  771. (3, 4, true),
  772. (5, NULL, true),
  773. (6, 2, true),
  774. (7, 2, true),
  775. (8, 4, true),
  776. (NULL, 4, true)
  777. # FILTER should eliminate some results.
  778. query II rowsort
  779. SELECT v, count(*) FILTER (WHERE k > 5) FROM filter_test GROUP BY v
  780. ----
  781. 2 2
  782. 4 1
  783. NULL 0
  784. # Test multiple filters
  785. query IBIII rowsort
  786. SELECT v, mark, count(*) FILTER (WHERE k > 5), count(*), max(k) FILTER (WHERE k < 8) FROM filter_test GROUP BY v, mark
  787. ----
  788. 2 false 0 1 1
  789. 2 true 2 2 7
  790. 4 true 1 3 3
  791. NULL true 0 1 5
  792. query error FILTER specified, but abs is not an aggregate function
  793. SELECT k, abs(k) FILTER (WHERE k=1) FROM kv
  794. query error Expected end of statement, found left parenthesis
  795. SELECT k FILTER (WHERE k=1) FROM kv GROUP BY k
  796. query error aggregate functions are not allowed in FILTER
  797. SELECT v, count(*) FILTER (WHERE count(*) > 5) FROM filter_test GROUP BY v
  798. # Tests with * inside GROUP BY.
  799. query I
  800. SELECT 1 FROM kv GROUP BY kv.*
  801. ----
  802. 1
  803. 1
  804. 1
  805. 1
  806. 1
  807. 1
  808. query R rowsort
  809. SELECT sum(abc.b) FROM kv JOIN abc ON kv.v > abc.b GROUP BY kv.*
  810. ----
  811. 1.5
  812. 1.5
  813. 1.5
  814. 3.5
  815. 3.5
  816. query BB
  817. SELECT max(true), min(true)
  818. ----
  819. true
  820. true
  821. # Grouping and rendering tuples.
  822. statement OK
  823. CREATE TABLE ab (
  824. a INT PRIMARY KEY,
  825. b INT
  826. )
  827. statement ok
  828. INSERT INTO ab(a,b) VALUES (1,2), (3,4);
  829. statement ok
  830. CREATE TABLE xy(x TEXT, y TEXT);
  831. statement ok
  832. INSERT INTO xy(x, y) VALUES ('a', 'b'), ('c', 'd')
  833. # database-issues#414
  834. # query T rowsort
  835. # SELECT (b, a) FROM ab GROUP BY (b, a)
  836. # ----
  837. # (2,1)
  838. # (4,3)
  839. #
  840. # query TT rowsort
  841. # SELECT min(y), (b, a)
  842. # FROM ab, xy GROUP BY (x, (a, b))
  843. # ----
  844. # b (2,1)
  845. # d (2,1)
  846. # b (4,3)
  847. # d (4,3)
  848. # Test that ordering on GROUP BY columns is maintained.
  849. statement ok
  850. CREATE TABLE group_ord (
  851. x INT PRIMARY KEY,
  852. y INT,
  853. z INT
  854. )
  855. statement ok
  856. INSERT INTO group_ord VALUES
  857. (1, 2, 3),
  858. (3, 4, 5),
  859. (5, NULL, 5),
  860. (6, 2, 3),
  861. (7, 2, 2),
  862. (8, 4, 2)
  863. # The ordering is on all the GROUP BY columns, and isn't preserved after the
  864. # aggregation.
  865. query II rowsort
  866. SELECT x, max(y) FROM group_ord GROUP BY x
  867. ----
  868. 1 2
  869. 3 4
  870. 5 NULL
  871. 6 2
  872. 7 2
  873. 8 4
  874. # The ordering is on all the GROUP BY columns, and is preserved after the
  875. # aggregation.
  876. query II
  877. SELECT x, max(y) FROM group_ord GROUP BY x ORDER BY x
  878. ----
  879. 1 2
  880. 3 4
  881. 5 NULL
  882. 6 2
  883. 7 2
  884. 8 4
  885. # The ordering is on some of the GROUP BY columns, and isn't preserved after
  886. # the aggregation.
  887. query III rowsort
  888. SELECT z, x, max(y) FROM group_ord GROUP BY x, z
  889. ----
  890. 5 3 4
  891. 3 6 2
  892. 3 1 2
  893. 5 5 NULL
  894. 2 7 2
  895. 2 8 4
  896. # The ordering is on some of the GROUP BY columns, and is preserved after
  897. # the aggregation.
  898. query III
  899. SELECT z, x, max(y) FROM group_ord GROUP BY x, z ORDER BY x
  900. ----
  901. 3 1 2
  902. 5 3 4
  903. 5 5 NULL
  904. 3 6 2
  905. 2 7 2
  906. 2 8 4
  907. # Regression test for materialize#25533 (crash when propagating filter through GROUP BY).
  908. query I
  909. SELECT 1 FROM kv GROUP BY v, w::DECIMAL HAVING w::DECIMAL > 1
  910. ----
  911. 1
  912. 1
  913. 1
  914. 1
  915. 1
  916. # Regression test for distsql aggregator crash when using hash aggregation.
  917. query error db error: ERROR: function array_agg\(unknown\) is not unique
  918. SELECT v, array_agg('a') FROM kv GROUP BY v
  919. query I
  920. SELECT 123 FROM kv ORDER BY max(v)
  921. ----
  922. 123
  923. subtest string_agg
  924. statement OK
  925. CREATE TABLE string_agg_test (
  926. id INT PRIMARY KEY,
  927. company_id INT,
  928. employee TEXT
  929. )
  930. query TT
  931. SELECT company_id, string_agg(employee, ',')
  932. FROM string_agg_test
  933. GROUP BY company_id
  934. ORDER BY company_id;
  935. ----
  936. query TT
  937. SELECT company_id, string_agg(employee, NULL)
  938. FROM string_agg_test
  939. GROUP BY company_id
  940. ORDER BY company_id;
  941. ----
  942. statement OK
  943. INSERT INTO string_agg_test VALUES
  944. (1, 1, 'A'),
  945. (2, 2, 'B'),
  946. (3, 3, 'C'),
  947. (4, 4, 'D'),
  948. (5, 3, 'C'),
  949. (6, 4, 'D'),
  950. (7, 4, 'D'),
  951. (8, 4, 'D'),
  952. (9, 3, 'C'),
  953. (10, 2, 'B')
  954. query TT
  955. SELECT company_id, string_agg(employee, employee)
  956. FROM string_agg_test
  957. GROUP BY company_id;
  958. ----
  959. 1 A
  960. 2 BBB
  961. 3 CCCCC
  962. 4 DDDDDDD
  963. query TT
  964. SELECT company_id, string_agg(employee, ',')
  965. FROM string_agg_test
  966. GROUP BY company_id
  967. ORDER BY company_id;
  968. ----
  969. 1 A
  970. 2 B,B
  971. 3 C,C,C
  972. 4 D,D,D,D
  973. query TT
  974. SELECT company_id, string_agg(DISTINCT employee, ',')
  975. FROM string_agg_test
  976. GROUP BY company_id
  977. ORDER BY company_id;
  978. ----
  979. 1 A
  980. 2 B
  981. 3 C
  982. 4 D
  983. query error type "b" does not exist
  984. SELECT company_id, string_agg(employee::BYTEA, b',')
  985. FROM string_agg_test
  986. GROUP BY company_id
  987. ORDER BY company_id;
  988. query TT
  989. SELECT company_id, string_agg(employee, '')
  990. FROM string_agg_test
  991. GROUP BY company_id
  992. ORDER BY company_id;
  993. ----
  994. 1 A
  995. 2 BB
  996. 3 CCC
  997. 4 DDDD
  998. query error type "b" does not exist
  999. SELECT company_id, string_agg(employee::BYTEA, b'')
  1000. FROM string_agg_test
  1001. GROUP BY company_id
  1002. ORDER BY company_id;
  1003. query TT
  1004. SELECT company_id, string_agg(employee, NULL)
  1005. FROM string_agg_test
  1006. GROUP BY company_id
  1007. ORDER BY company_id;
  1008. ----
  1009. 1 A
  1010. 2 BB
  1011. 3 CCC
  1012. 4 DDDD
  1013. query error supported
  1014. SELECT company_id, string_agg(employee::BYTEA, NULL)
  1015. FROM string_agg_test
  1016. GROUP BY company_id
  1017. ORDER BY company_id;
  1018. query TT
  1019. SELECT company_id, string_agg(NULL::TEXT, ',')
  1020. FROM string_agg_test
  1021. GROUP BY company_id
  1022. ORDER BY company_id;
  1023. ----
  1024. 1 NULL
  1025. 2 NULL
  1026. 3 NULL
  1027. 4 NULL
  1028. query error supported
  1029. SELECT company_id, string_agg(NULL::BYTEA, ',')
  1030. FROM string_agg_test
  1031. GROUP BY company_id
  1032. ORDER BY company_id;
  1033. query TT
  1034. SELECT company_id, string_agg(NULL::TEXT, NULL)
  1035. FROM string_agg_test
  1036. GROUP BY company_id
  1037. ORDER BY company_id;
  1038. ----
  1039. 1 NULL
  1040. 2 NULL
  1041. 3 NULL
  1042. 4 NULL
  1043. query error supported
  1044. SELECT company_id, string_agg(NULL::BYTEA, NULL)
  1045. FROM string_agg_test
  1046. GROUP BY company_id
  1047. ORDER BY company_id;
  1048. query TT
  1049. SELECT company_id, string_agg(NULL, NULL)
  1050. FROM string_agg_test
  1051. GROUP BY company_id
  1052. ORDER BY company_id;
  1053. ----
  1054. 1 NULL
  1055. 2 NULL
  1056. 3 NULL
  1057. 4 NULL
  1058. # Now test the window function version of string_agg.
  1059. query IT
  1060. SELECT company_id, string_agg(employee, ',')
  1061. OVER (PARTITION BY company_id ORDER BY id)
  1062. FROM string_agg_test
  1063. ORDER BY company_id, id;
  1064. ----
  1065. 1 A
  1066. 2 B
  1067. 2 B,B
  1068. 3 C
  1069. 3 C,C
  1070. 3 C,C,C
  1071. 4 D
  1072. 4 D,D
  1073. 4 D,D,D
  1074. 4 D,D,D,D
  1075. query error type "b" does not exist
  1076. SELECT company_id, string_agg(employee::BYTEA, b',')
  1077. OVER (PARTITION BY company_id ORDER BY id)
  1078. FROM string_agg_test
  1079. ORDER BY company_id, id;
  1080. query IT
  1081. SELECT company_id, string_agg(employee, '')
  1082. OVER (PARTITION BY company_id ORDER BY id)
  1083. FROM string_agg_test
  1084. ORDER BY company_id, id;
  1085. ----
  1086. 1 A
  1087. 2 B
  1088. 2 BB
  1089. 3 C
  1090. 3 CC
  1091. 3 CCC
  1092. 4 D
  1093. 4 DD
  1094. 4 DDD
  1095. 4 DDDD
  1096. query error type "b" does not exist
  1097. SELECT company_id, string_agg(employee::BYTEA, b'')
  1098. OVER (PARTITION BY company_id ORDER BY id)
  1099. FROM string_agg_test
  1100. ORDER BY company_id, id;
  1101. query IT
  1102. SELECT company_id, string_agg(employee, NULL)
  1103. OVER (PARTITION BY company_id ORDER BY id)
  1104. FROM string_agg_test
  1105. ORDER BY company_id, id;
  1106. ----
  1107. 1 A
  1108. 2 B
  1109. 2 BB
  1110. 3 C
  1111. 3 CC
  1112. 3 CCC
  1113. 4 D
  1114. 4 DD
  1115. 4 DDD
  1116. 4 DDDD
  1117. query error string_agg on BYTEA not yet supported
  1118. SELECT company_id, string_agg(employee::BYTEA, NULL)
  1119. OVER (PARTITION BY company_id ORDER BY id)
  1120. FROM string_agg_test
  1121. ORDER BY company_id, id;
  1122. query IT
  1123. SELECT company_id, string_agg(NULL::TEXT, employee)
  1124. OVER (PARTITION BY company_id ORDER BY id)
  1125. FROM string_agg_test
  1126. ORDER BY company_id, id;
  1127. ----
  1128. 1 NULL
  1129. 2 NULL
  1130. 2 NULL
  1131. 3 NULL
  1132. 3 NULL
  1133. 3 NULL
  1134. 4 NULL
  1135. 4 NULL
  1136. 4 NULL
  1137. 4 NULL
  1138. query error string_agg on BYTEA not yet supported
  1139. SELECT company_id, string_agg(NULL::BYTEA, employee::BYTEA)
  1140. OVER (PARTITION BY company_id ORDER BY id)
  1141. FROM string_agg_test
  1142. ORDER BY company_id, id;
  1143. query IT
  1144. SELECT company_id, string_agg(NULL::TEXT, NULL)
  1145. OVER (PARTITION BY company_id ORDER BY id)
  1146. FROM string_agg_test
  1147. ORDER BY company_id, id;
  1148. ----
  1149. 1 NULL
  1150. 2 NULL
  1151. 2 NULL
  1152. 3 NULL
  1153. 3 NULL
  1154. 3 NULL
  1155. 4 NULL
  1156. 4 NULL
  1157. 4 NULL
  1158. 4 NULL
  1159. query error string_agg on BYTEA not yet supported
  1160. SELECT company_id, string_agg(NULL::BYTEA, NULL)
  1161. OVER (PARTITION BY company_id ORDER BY id)
  1162. FROM string_agg_test
  1163. ORDER BY company_id, id;
  1164. query IT
  1165. SELECT company_id, string_agg(NULL, NULL::TEXT)
  1166. OVER (PARTITION BY company_id ORDER BY id)
  1167. FROM string_agg_test
  1168. ORDER BY company_id, id;
  1169. ----
  1170. 1 NULL
  1171. 2 NULL
  1172. 2 NULL
  1173. 3 NULL
  1174. 3 NULL
  1175. 3 NULL
  1176. 4 NULL
  1177. 4 NULL
  1178. 4 NULL
  1179. 4 NULL
  1180. query error string_agg on BYTEA not yet supported
  1181. SELECT company_id, string_agg(NULL, NULL::BYTEA)
  1182. OVER (PARTITION BY company_id ORDER BY id)
  1183. FROM string_agg_test
  1184. ORDER BY company_id, id;
  1185. query IT
  1186. SELECT company_id, string_agg(NULL, NULL)
  1187. OVER (PARTITION BY company_id ORDER BY id)
  1188. FROM string_agg_test
  1189. ORDER BY company_id, id;
  1190. ----
  1191. 1 NULL
  1192. 2 NULL
  1193. 2 NULL
  1194. 3 NULL
  1195. 3 NULL
  1196. 3 NULL
  1197. 4 NULL
  1198. 4 NULL
  1199. 4 NULL
  1200. 4 NULL
  1201. query IT
  1202. SELECT company_id, string_agg(employee, lower(employee))
  1203. OVER (PARTITION BY company_id)
  1204. FROM string_agg_test
  1205. ORDER BY company_id, id;
  1206. ----
  1207. 1 A
  1208. 2 BbB
  1209. 2 BbB
  1210. 3 CcCcC
  1211. 3 CcCcC
  1212. 3 CcCcC
  1213. 4 DdDdDdD
  1214. 4 DdDdDdD
  1215. 4 DdDdDdD
  1216. 4 DdDdDdD
  1217. query IT
  1218. SELECT company_id, string_agg(lower(employee), employee)
  1219. OVER (PARTITION BY company_id)
  1220. FROM string_agg_test
  1221. ORDER BY company_id, id;
  1222. ----
  1223. 1 a
  1224. 2 bBb
  1225. 2 bBb
  1226. 3 cCcCc
  1227. 3 cCcCc
  1228. 3 cCcCc
  1229. 4 dDdDdDd
  1230. 4 dDdDdDd
  1231. 4 dDdDdDd
  1232. 4 dDdDdDd
  1233. query error db error: ERROR: function string_agg\(text, text, text\) does not exist
  1234. SELECT company_id, string_agg(employee, employee, employee)
  1235. OVER (PARTITION BY company_id)
  1236. FROM string_agg_test
  1237. ORDER BY company_id, id;
  1238. query error db error: ERROR: function string_agg\(text\) does not exist
  1239. SELECT company_id, string_agg(employee)
  1240. OVER (PARTITION BY company_id)
  1241. FROM string_agg_test
  1242. ORDER BY company_id, id;
  1243. statement OK
  1244. CREATE TABLE string_agg_test2 (
  1245. id INT PRIMARY KEY,
  1246. company_id INT,
  1247. employee TEXT
  1248. )
  1249. statement OK
  1250. INSERT INTO string_agg_test2 VALUES
  1251. (1, 1, 'A'),
  1252. (2, 1, 'B'),
  1253. (3, 1, 'C'),
  1254. (4, 1, 'D')
  1255. query TT
  1256. SELECT e.company_id, string_agg(e.employee, ', ')
  1257. FROM (
  1258. SELECT employee, company_id
  1259. FROM string_agg_test2
  1260. ORDER BY employee
  1261. ) AS e
  1262. GROUP BY e.company_id
  1263. ORDER BY e.company_id;
  1264. ----
  1265. 1 A,␠B,␠C,␠D
  1266. query error type "b" does not exist
  1267. SELECT e.company_id, string_agg(e.employee, b', ')
  1268. FROM (
  1269. SELECT employee::BYTEA, company_id
  1270. FROM string_agg_test2
  1271. ORDER BY employee
  1272. ) AS e
  1273. GROUP BY e.company_id
  1274. ORDER BY e.company_id;
  1275. # This will differ from PG until we close https://github.com/MaterializeInc/database-issues/issues/843
  1276. query TT
  1277. SELECT e.company_id, string_agg(e.employee, ', ')
  1278. FROM (
  1279. SELECT employee, company_id
  1280. FROM string_agg_test2
  1281. ORDER BY employee DESC
  1282. ) AS e
  1283. GROUP BY e.company_id
  1284. ORDER BY e.company_id;
  1285. ----
  1286. 1 A,␠B,␠C,␠D
  1287. query error type "b" does not exist
  1288. SELECT e.company_id, string_agg(e.employee, b', ')
  1289. FROM (
  1290. SELECT employee::BYTEA, company_id
  1291. FROM string_agg_test2
  1292. ORDER BY employee DESC
  1293. ) AS e
  1294. GROUP BY e.company_id
  1295. ORDER BY e.company_id;
  1296. # This will differ from PG until we close https://github.com/MaterializeInc/database-issues/issues/843
  1297. query TT
  1298. SELECT e.company_id, string_agg(e.employee, NULL)
  1299. FROM (
  1300. SELECT employee, company_id
  1301. FROM string_agg_test2
  1302. ORDER BY employee DESC
  1303. ) AS e
  1304. GROUP BY e.company_id
  1305. ORDER BY e.company_id;
  1306. ----
  1307. 1 ABCD
  1308. query error supported
  1309. SELECT e.company_id, string_agg(e.employee, NULL)
  1310. FROM (
  1311. SELECT employee::BYTEA, company_id
  1312. FROM string_agg_test2
  1313. ORDER BY employee DESC
  1314. ) AS e
  1315. GROUP BY e.company_id
  1316. ORDER BY e.company_id;
  1317. statement OK
  1318. DROP TABLE string_agg_test2
  1319. # Regression test for materialize#28836.
  1320. query error supported
  1321. SELECT string_agg('foo', CAST ((SELECT NULL) AS BYTEA)) OVER ();
  1322. query error table functions are not allowed in aggregate function calls
  1323. SELECT array_agg(generate_series(1, 2))
  1324. # Regression test for cockroach#31882.
  1325. statement ok
  1326. CREATE TABLE uvw (u INT, v INT, w INT)
  1327. statement ok
  1328. INSERT INTO uvw VALUES (1, 2, 3), (1, 2, 3), (3, 2, 1), (3, 2, 3)
  1329. query IIT
  1330. SELECT u, v, array_agg(w) AS s FROM (SELECT * FROM uvw ORDER BY w) GROUP BY u, v ORDER BY u
  1331. ----
  1332. 1 2 {3,3}
  1333. 3 2 {1,3}
  1334. # Regression test for cockroach#36433: don't panic with count_agg if a post-render produces an error.
  1335. query error lpad
  1336. SELECT count(*)::TEXT||lpad('foo', 23984729388383834723984) FROM (VALUES(1));