aggregates.slt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. mode cockroach
  10. simple conn=mz_system,user=mz_system
  11. ALTER SYSTEM SET unsafe_enable_table_keys = true
  12. ----
  13. COMPLETE 0
  14. statement ok
  15. CREATE TABLE t (a int, b int)
  16. statement ok
  17. INSERT INTO t (a, b) VALUES (1, 1), (1, 2), (2, 3), (3, 1)
  18. query error aggregate functions are not allowed in WHERE clause \(function pg_catalog.sum\)
  19. SELECT a FROM t WHERE sum(b) = 3 GROUP BY a
  20. query error column "t.b" must appear in the GROUP BY clause or be used in an aggregate function
  21. SELECT b FROM t GROUP BY a
  22. query error column "t.b" must appear in the GROUP BY clause or be used in an aggregate function
  23. SELECT 1 FROM t GROUP BY a ORDER BY b
  24. query error column "t.b" must appear in the GROUP BY clause or be used in an aggregate function
  25. SELECT 1 FROM t GROUP BY a HAVING b > 0
  26. query error column "t.b" must appear in the GROUP BY clause or be used in an aggregate function
  27. SELECT DISTINCT ON (b) a FROM t GROUP BY a ORDER BY b
  28. query error column "t.b" must appear in the GROUP BY clause or be used in an aggregate function
  29. SELECT t.b FROM t GROUP BY a
  30. query error column "t.a" must appear in the GROUP BY clause or be used in an aggregate function
  31. SELECT (SELECT a FROM t GROUP BY b) FROM t
  32. query error column "t1.b" must appear in the GROUP BY clause or be used in an aggregate function
  33. SELECT (SELECT t2.a FROM t t2 WHERE t1.b = t2.b) FROM t t1 GROUP BY t1.a;
  34. query error column "t1.b" must appear in the GROUP BY clause or be used in an aggregate function
  35. SELECT sum(t1.a), (SELECT t2.a FROM t t2 WHERE t1.b = t2.b) FROM t t1;
  36. query error column "c" does not exist
  37. SELECT c FROM t GROUP BY a
  38. query error column "t.c" does not exist
  39. SELECT t.c FROM t GROUP BY a
  40. query IIIR colnames
  41. SELECT 1 AS literal, sum(a) as sum_a, sum(b), avg(a) FROM t
  42. ----
  43. literal sum_a sum avg
  44. 1 7 7 1.75
  45. query I rowsort
  46. SELECT a FROM t GROUP BY a HAVING sum(b) = 3
  47. ----
  48. 1
  49. 2
  50. query I rowsort
  51. SELECT a + 1 FROM t GROUP BY a + 1 HAVING sum(b) = 3
  52. ----
  53. 2
  54. 3
  55. query II rowsort
  56. SELECT t1.a, (SELECT t2.a FROM t t2 WHERE t2.b = 2 AND t1.b = t2.b GROUP BY t2.a) FROM t t1;
  57. ----
  58. 1 NULL
  59. 2 NULL
  60. 3 NULL
  61. 1 1
  62. query I
  63. SELECT (SELECT sum(b) FROM t WHERE b = 2 GROUP BY a) FROM t t1 GROUP BY t1.b;
  64. ----
  65. 2
  66. 2
  67. 2
  68. # Simple column names in GROUP BY can refer to columns from the output list...
  69. query TII rowsort
  70. SELECT 'dummy', a AS c, sum(b) FROM t GROUP BY c
  71. ----
  72. dummy 1 3
  73. dummy 2 3
  74. dummy 3 1
  75. # ...unless they are ambiguous...
  76. query error column reference "c" is ambiguous
  77. SELECT a AS c, sum(b) AS c FROM t GROUP BY c
  78. # ...although ambiguity between the input list and the output list is not an
  79. # error; the column in the input list is preferred.
  80. query II rowsort
  81. SELECT a, sum(b) AS a FROM t GROUP BY a
  82. ----
  83. 1 3
  84. 2 3
  85. 3 1
  86. query I rowsort
  87. SELECT a FROM t GROUP BY t.a, t.a
  88. ----
  89. 1
  90. 2
  91. 3
  92. query I rowsort
  93. SELECT a FROM t GROUP BY t.a, public.t.a
  94. ----
  95. 1
  96. 2
  97. 3
  98. # Smoke test to make sure multiple accumulable and hierarchical reductions work
  99. query IIIII rowsort
  100. SELECT a, count(b), min(b), sum(b), max(b) FROM t GROUP BY a
  101. ----
  102. 1 2 1 3 2
  103. 2 1 3 3 3
  104. 3 1 1 1 1
  105. # Test that hinting the group size works
  106. query II rowsort
  107. SELECT a, sum(b) AS a FROM t GROUP BY a OPTIONS (AGGREGATE INPUT GROUP SIZE 100)
  108. ----
  109. 1 3
  110. 2 3
  111. 3 1
  112. # unless hint is bad
  113. query error invalid AGGREGATE INPUT GROUP SIZE: cannot use value as number
  114. SELECT a, sum(b) AS a FROM t GROUP BY a OPTIONS (AGGREGATE INPUT GROUP SIZE = 'foo')
  115. query error
  116. SELECT a, sum(b) AS a FROM t GROUP BY a OPTIONS (AGGREGATE INPUT GROUP SIZE = 0.1)
  117. # Test that an ordinal in a GROUP BY that refers to a column that is an
  118. # expression, rather than a simple column reference, works.
  119. query IT rowsort
  120. SELECT 2 * a, sum(b) FROM t GROUP BY 1
  121. ----
  122. 2 3
  123. 4 3
  124. 6 1
  125. # Ensure that the sum of NULLs is NULL.
  126. query T
  127. SELECT sum(column1) FROM (VALUES (NULL::int2), (NULL))
  128. ----
  129. NULL
  130. query T
  131. SELECT sum(column1) FROM (VALUES (NULL::int4), (NULL))
  132. ----
  133. NULL
  134. query T
  135. SELECT sum(column1) FROM (VALUES (NULL::int8), (NULL))
  136. ----
  137. NULL
  138. query T
  139. SELECT sum(column1) FROM (VALUES (NULL::numeric), (NULL))
  140. ----
  141. NULL
  142. query TTTT colnames
  143. SHOW COLUMNS FROM t
  144. ----
  145. name nullable type comment
  146. a true integer (empty)
  147. b true integer (empty)
  148. # Tests on int8 sums to make sure we handle overflow and underflow correctly
  149. statement ok
  150. CREATE TABLE t_bigint (a bigint, b bigint)
  151. statement ok
  152. INSERT INTO t_bigint (a, b) VALUES (1, 1), (1, 2), (2, 9223372036854775807), (2, 9223372036854775807), (3, -9223372036854775808), (3, -9223372036854775808)
  153. query II rowsort
  154. SELECT a, sum(b) FROM t_bigint GROUP BY a
  155. ----
  156. 1 3
  157. 2 18446744073709551614
  158. 3 -18446744073709551616
  159. query T colnames
  160. SELECT pg_typeof(sum(b)) FROM t_bigint
  161. ----
  162. pg_typeof
  163. numeric
  164. query TT colnames
  165. SELECT pg_typeof(a) as a_type, pg_typeof(b) as b_type FROM t_bigint GROUP BY a_type, b_type
  166. ----
  167. a_type b_type
  168. bigint bigint
  169. # Tests to make sure reduce elision works correctly
  170. statement ok
  171. CREATE TABLE agg_pk (a INT PRIMARY KEY, b INT, c BIGINT)
  172. statement ok
  173. INSERT INTO agg_pk VALUES (1, 2, 3), (2, 3, 4), (3, 4, 5)
  174. query II
  175. SELECT a, sum(b) from agg_pk group by a order by a
  176. ----
  177. 1 2
  178. 2 3
  179. 3 4
  180. query T multiline
  181. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT a, sum(b) from agg_pk group by a
  182. ----
  183. Explained Query:
  184. Project (#0{a}, #3) // { arity: 2 }
  185. Map (integer_to_bigint(#1{b})) // { arity: 4 }
  186. ReadStorage materialize.public.agg_pk // { arity: 3 }
  187. Source materialize.public.agg_pk
  188. Target cluster: quickstart
  189. EOF
  190. query II
  191. SELECT a, sum(c) from agg_pk group by a order by a
  192. ----
  193. 1 3
  194. 2 4
  195. 3 5
  196. query T multiline
  197. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT a, sum(c) from agg_pk group by a
  198. ----
  199. Explained Query:
  200. Project (#0{a}, #3) // { arity: 2 }
  201. Map (bigint_to_numeric(#2{c})) // { arity: 4 }
  202. ReadStorage materialize.public.agg_pk // { arity: 3 }
  203. Source materialize.public.agg_pk
  204. Target cluster: quickstart
  205. EOF
  206. # avg on an integer column should return a decimal with the default decimal
  207. # division scale increase.
  208. query R
  209. SELECT avg(a) FROM t
  210. ----
  211. 1.75
  212. # But avg on a float column should return a float.
  213. statement ok
  214. CREATE TABLE t2 (a float)
  215. statement ok
  216. INSERT INTO t2 VALUES (1.0), (1.0), (2.0), (3.0)
  217. query R
  218. SELECT avg(a) FROM t2
  219. ----
  220. 1.75
  221. # avg of an explicit NULL should return an error.
  222. query error db error: ERROR: function sum\(unknown\) is not unique
  223. SELECT avg(NULL)
  224. statement error
  225. SELECT * ORDER BY SUM(fake_column)
  226. query RRRRRR colnames
  227. SELECT variance(a), var_samp(a), var_pop(a), stddev(a), stddev_samp(a), stddev_pop(a) FROM t
  228. ----
  229. variance var_samp var_pop stddev stddev_samp stddev_pop
  230. 0.916666666666666666666666666666666666667 0.916666666666666666666666666666666666667 0.6875 0.957427107756338109975101911369821553037 0.957427107756338109975101911369821553037 0.829156197588849962278733184167671670982
  231. query RRRRRR
  232. SELECT variance(a), var_samp(a), var_pop(a), stddev(a), stddev_samp(a), stddev_pop(a) FROM t2
  233. ----
  234. 0.9166666666666666 0.9166666666666666 0.6875 0.9574271077563381 0.9574271077563381 0.82915619758885
  235. # TODO(benesch): these filter tests are copied from cockroach/aggregate.slt;
  236. # remove them from here when we can run that file in its entirely.
  237. statement ok
  238. CREATE TABLE filter_test (
  239. k INT,
  240. v INT,
  241. mark BOOL
  242. )
  243. statement OK
  244. INSERT INTO filter_test VALUES
  245. (1, 2, false),
  246. (3, 4, true),
  247. (5, NULL, true),
  248. (6, 2, true),
  249. (7, 2, true),
  250. (8, 4, true),
  251. (NULL, 4, true)
  252. # FILTER should eliminate some results.
  253. query II rowsort
  254. SELECT v, count(*) FILTER (WHERE k > 5) FROM filter_test GROUP BY v
  255. ----
  256. 2 2
  257. 4 1
  258. NULL 0
  259. # Test multiple filters
  260. query IBIII rowsort
  261. SELECT v, mark, count(*) FILTER (WHERE k > 5), count(*), max(k) FILTER (WHERE k < 8) FROM filter_test GROUP BY v, mark
  262. ----
  263. 2 false 0 1 1
  264. 2 true 2 2 7
  265. 4 true 1 3 3
  266. NULL true 0 1 5
  267. query error FILTER specified, but abs is not an aggregate function
  268. SELECT abs(1) FILTER (WHERE false)
  269. query error Expected end of statement, found left parenthesis
  270. SELECT column1 FILTER (WHERE column1 = 1) FROM (VALUES (1))
  271. query error db error: ERROR: aggregate functions are not allowed in FILTER \(function pg_catalog\.count\)
  272. SELECT v, count(*) FILTER (WHERE count(1) > 5) FROM filter_test GROUP BY v
  273. # These filter tests are Materialize-specific.
  274. # Test avg, which needs to propgate the filter through its implementation.
  275. query IR rowsort
  276. SELECT v, avg(k) FILTER (WHERE k > 5) FROM filter_test GROUP BY v
  277. ----
  278. 2 6.5
  279. 4 8
  280. NULL NULL
  281. # Similarly for variance and stddev.
  282. query IRR rowsort
  283. SELECT v, variance(k) FILTER (WHERE k > 5), stddev(k) FILTER (WHERE k > 5) FROM filter_test GROUP BY v
  284. ----
  285. 2 0.5 0.707106781186547524400844362104849039285
  286. 4 NULL NULL
  287. NULL NULL NULL
  288. # Multiple tests related to distinctness of aggregates on constants (issue database-issues#887)
  289. query I rowsort
  290. select count(distinct column1) from (values (1)) _;
  291. ----
  292. 1
  293. query I rowsort
  294. select count(distinct column1) from (values (1), (2), (1), (4)) _;
  295. ----
  296. 3
  297. query I rowsort
  298. select sum(distinct column1) from (values (1), (2), (1), (4)) _;
  299. ----
  300. 7
  301. query error count\(\*\) must be used to call a parameterless aggregate function
  302. SELECT count()
  303. query error db error: ERROR: function sum\(\) does not exist
  304. SELECT sum(*)
  305. # Ensure int2 has its own max implementation
  306. query I
  307. SELECT max(column1) FROM (VALUES (1::int2), (-1::int2));
  308. ----
  309. 1
  310. query T
  311. SELECT pg_typeof(max(column1)) FROM (VALUES (1::int2), (-1::int2));
  312. ----
  313. smallint
  314. # ORDER BY
  315. query TTTT
  316. WITH
  317. v (a, b)
  318. AS (
  319. VALUES
  320. ('a', 1),
  321. ('b', 2),
  322. ('c', 0),
  323. ('d', 2)
  324. )
  325. SELECT
  326. list_agg(a ORDER BY a DESC)::text AS a_desc,
  327. list_agg(a ORDER BY b)::text AS b,
  328. list_agg(a ORDER BY b, a ASC)::text AS b_a_asc,
  329. list_agg(a ORDER BY b, a DESC)::text AS b_a_desc
  330. FROM
  331. v
  332. ----
  333. {d,c,b,a} {c,a,b,d} {c,a,b,d} {c,a,d,b}
  334. query TTTTT
  335. WITH
  336. v (a, b)
  337. AS (
  338. VALUES
  339. ('a', 1),
  340. ('b', 2),
  341. ('c', 0),
  342. ('d', 2),
  343. ('e', NULL)
  344. )
  345. SELECT
  346. array_agg(a ORDER BY a DESC)::text AS a_desc,
  347. array_agg(a ORDER BY b)::text AS b,
  348. array_agg(a ORDER BY b, a ASC)::text AS b_a_asc,
  349. array_agg(a ORDER BY b, a DESC)::text AS b_a_desc,
  350. array_agg(a ORDER BY b NULLS FIRST, a DESC)::text AS b_a_desc_nulls_first
  351. FROM
  352. v
  353. ----
  354. {e,d,c,b,a} {c,a,b,d,e} {c,a,b,d,e} {c,a,d,b,e} {e,c,a,d,b}
  355. query TTTT
  356. WITH
  357. v (a, b)
  358. AS (
  359. VALUES
  360. ('a', 1),
  361. ('b', 2),
  362. ('c', 0),
  363. ('d', 2)
  364. )
  365. SELECT
  366. string_agg(a, '-' ORDER BY a DESC)::text AS a_desc,
  367. string_agg(a, '-' ORDER BY b)::text AS b,
  368. string_agg(a, '-' ORDER BY b, a ASC)::text AS b_a_asc,
  369. string_agg(a, '-' ORDER BY b, a DESC)::text AS b_a_desc
  370. FROM
  371. v
  372. ----
  373. d-c-b-a c-a-b-d c-a-b-d c-a-d-b
  374. query TTTT
  375. WITH
  376. v (a, b)
  377. AS (
  378. VALUES
  379. ('a', 1),
  380. ('b', 2),
  381. ('c', 0),
  382. ('d', 2)
  383. )
  384. SELECT
  385. jsonb_agg(a ORDER BY a DESC)::text AS a_desc,
  386. jsonb_agg(a ORDER BY b)::text AS b,
  387. jsonb_agg(a ORDER BY b, a ASC)::text AS b_a_asc,
  388. jsonb_agg(a ORDER BY b, a DESC)::text AS b_a_desc
  389. FROM
  390. v
  391. ----
  392. ["d","c","b","a"] ["c","a","b","d"] ["c","a","b","d"] ["c","a","d","b"]
  393. query TTTT
  394. WITH
  395. v (a, b)
  396. AS (
  397. VALUES
  398. ('a', 1),
  399. ('b', 2),
  400. ('c', 0),
  401. ('d', 2)
  402. )
  403. SELECT
  404. jsonb_object_agg(b, a ORDER BY a DESC)::text AS a_desc,
  405. jsonb_object_agg(b, a ORDER BY b)::text AS b,
  406. jsonb_object_agg(b, a ORDER BY b, a ASC)::text AS b_a_asc,
  407. jsonb_object_agg(b, a ORDER BY b, a DESC)::text AS b_a_desc
  408. FROM
  409. v
  410. ----
  411. {"0":"c","1":"a","2":"b"} {"0":"c","1":"a","2":"d"} {"0":"c","1":"a","2":"d"} {"0":"c","1":"a","2":"b"}
  412. # Test Reduction elision
  413. statement ok
  414. CREATE TABLE a (x text)
  415. statement ok
  416. INSERT INTO a VALUES ('a'),('b')
  417. statement ok
  418. CREATE TABLE qs (q int not null)
  419. query T
  420. SELECT STRING_AGG(x, ',') FROM (SELECT * FROM a ORDER BY x);
  421. ----
  422. a,b
  423. query T
  424. SELECT STRING_AGG(x, ',') FROM (SELECT * FROM a ORDER BY x limit 1);
  425. ----
  426. a
  427. query T
  428. SELECT STRING_AGG(x, ',') FROM (SELECT * FROM (SELECT 'a' as x UNION ALL SELECT 'b' as x) ORDER BY x)
  429. ----
  430. a,b
  431. query T
  432. SELECT STRING_AGG(x, ',') FROM (SELECT * FROM (SELECT 'a' as x UNION ALL SELECT 'b' as x) ORDER BY x limit 1)
  433. ----
  434. a
  435. query T
  436. SELECT STRING_AGG(x, ',') from (SELECT TRUE::text as x FROM(SELECT AVG(0) FROM qs))
  437. ----
  438. true
  439. query T
  440. SELECT LIST_AGG(x)::text FROM (SELECT * FROM a ORDER BY x)
  441. ----
  442. {a,b}
  443. query T
  444. SELECT LIST_AGG(x)::text FROM (SELECT * FROM a ORDER BY x limit 1)
  445. ----
  446. {a}
  447. query T
  448. SELECT LIST_AGG(x)::text FROM (SELECT * FROM (SELECT 'a' as x UNION ALL SELECT 'b' as x) ORDER BY x)
  449. ----
  450. {a,b}
  451. query T
  452. SELECT LIST_AGG(x)::text FROM (SELECT * FROM (SELECT 'a' as x UNION ALL SELECT 'b' as x) ORDER BY x limit 1)
  453. ----
  454. {a}
  455. query T
  456. SELECT LIST_AGG(x)::text from (SELECT TRUE::text as x FROM(SELECT AVG(0) FROM qs))
  457. ----
  458. {true}
  459. query T
  460. SELECT ARRAY_AGG(x) FROM (SELECT * FROM a ORDER BY x)
  461. ----
  462. {a,b}
  463. query T
  464. SELECT ARRAY_AGG(x) FROM (SELECT * FROM a ORDER BY x limit 1)
  465. ----
  466. {a}
  467. query T
  468. SELECT ARRAY_AGG(x) FROM (SELECT * FROM (SELECT 'a' as x UNION ALL SELECT 'b' as x) ORDER BY x)
  469. ----
  470. {a,b}
  471. query T
  472. SELECT ARRAY_AGG(x) FROM (SELECT * FROM (SELECT 'a' as x UNION ALL SELECT 'b' as x) ORDER BY x limit 1)
  473. ----
  474. {a}
  475. query T
  476. SELECT ARRAY_AGG(x) from (SELECT TRUE::text as x FROM(SELECT AVG(0) FROM qs))
  477. ----
  478. {true}
  479. query T
  480. SELECT JSONB_AGG(x) FROM (SELECT * FROM a ORDER BY x)
  481. ----
  482. ["a","b"]
  483. query T
  484. SELECT JSONB_AGG(x) FROM (SELECT * FROM a ORDER BY x limit 1)
  485. ----
  486. ["a"]
  487. query T
  488. SELECT JSONB_AGG(x) FROM (SELECT * FROM (SELECT 'a' as x UNION ALL SELECT 'b' as x) ORDER BY x)
  489. ----
  490. ["a","b"]
  491. query T
  492. SELECT JSONB_AGG(x) FROM (SELECT * FROM (SELECT 'a' as x UNION ALL SELECT 'b' as x) ORDER BY x limit 1)
  493. ----
  494. ["a"]
  495. query T
  496. SELECT JSONB_AGG(x) from (SELECT TRUE::text as x FROM(SELECT AVG(0) FROM qs))
  497. ----
  498. ["true"]
  499. query T
  500. SELECT JSONB_OBJECT_AGG(a,b) FROM (SELECT * FROM t ORDER BY a)
  501. ----
  502. {"1":2,"2":3,"3":1}
  503. query T
  504. SELECT JSONB_OBJECT_AGG(a,b) FROM (SELECT * FROM t ORDER BY a limit 1)
  505. ----
  506. {"1":1}
  507. query T
  508. SELECT JSONB_OBJECT_AGG(a,b) FROM (SELECT * FROM (SELECT 'a' as a,'b' as b UNION ALL SELECT 'c' as a,'d' as b) ORDER by a)
  509. ----
  510. {"a":"b","c":"d"}
  511. query T
  512. SELECT JSONB_OBJECT_AGG(a,b) FROM (SELECT * FROM (SELECT 'a' as a,'b' as b UNION ALL SELECT 'c' as a,'d' as b) ORDER by a limit 1)
  513. ----
  514. {"a":"b"}
  515. query T
  516. SELECT JSONB_OBJECT_AGG(a,b) from (SELECT TRUE::text as a, FALSE::text as b FROM(SELECT AVG(0) FROM qs))
  517. ----
  518. {"true":"false"}
  519. query TI
  520. SELECT a.*, ROW_NUMBER() over () FROM (SELECT * FROM a ORDER BY x) a
  521. ----
  522. a 1
  523. b 2
  524. query TI
  525. SELECT a.*, ROW_NUMBER() over () FROM (SELECT * FROM a ORDER BY x limit 1) a
  526. ----
  527. a 1
  528. query TI
  529. SELECT a.*, ROW_NUMBER() OVER() FROM (SELECT * FROM (SELECT 'a' as x UNION ALL SELECT 'b' as x) ORDER BY x) a
  530. ----
  531. a 1
  532. b 2
  533. query TI
  534. SELECT a.*, ROW_NUMBER() OVER() FROM (SELECT * FROM (SELECT 'a' as x UNION ALL SELECT 'b' as x) ORDER BY x limit 1) a
  535. ----
  536. a 1
  537. query TI
  538. SELECT a.*, ROW_NUMBER() OVER() from (SELECT TRUE::text as x FROM(SELECT AVG(0) FROM qs)) a
  539. ----
  540. true 1
  541. statement ok
  542. CREATE TABLE t_16 (i16 smallint)
  543. statement ok
  544. INSERT INTO t_16 VALUES (0), (-1), (1)
  545. query TT
  546. SELECT MIN(i16), MAX(i16) from t_16
  547. ----
  548. -1 1
  549. # Verify that the behavior of `stddev` is sane when variance is small
  550. statement ok
  551. CREATE TABLE t_variance (x float)
  552. statement ok
  553. INSERT INTO t_variance VALUES (0.2)
  554. query I
  555. SELECT stddev(x) FROM t_variance
  556. ----
  557. NULL
  558. statement ok
  559. INSERT INTO t_variance VALUES (0.2)
  560. query I
  561. SELECT stddev(x) FROM t_variance
  562. ----
  563. 0
  564. query I
  565. SELECT stddev(x) FROM generate_series(0, -1) empty(x)
  566. ----
  567. NULL
  568. # Should include two sum(*) aggregates.
  569. query T multiline
  570. EXPLAIN RAW PLAN FOR SELECT stddev(x), sum(x) FROM t_variance;
  571. ----
  572. Project (#4, #3)
  573. Map (sqrtf64(case when (((#0{?column?} - ((#1{?column?} * #1{?column?}) / bigint_to_double(case when (#2{?column?} = integer_to_bigint(0)) then null else #2{?column?} end))) / bigint_to_double(case when ((#2{?column?} - integer_to_bigint(1)) = integer_to_bigint(0)) then null else (#2{?column?} - integer_to_bigint(1)) end))) IS NULL then null else greatest(((#0{?column?} - ((#1{?column?} * #1{?column?}) / bigint_to_double(case when (#2{?column?} = integer_to_bigint(0)) then null else #2{?column?} end))) / bigint_to_double(case when ((#2{?column?} - integer_to_bigint(1)) = integer_to_bigint(0)) then null else (#2{?column?} - integer_to_bigint(1)) end)), integer_to_double(0)) end))
  574. Reduce aggregates=[sum((#0{x} * #0{x})), sum(#0{x}), count(#0{x}), sum(#0{x})]
  575. Get materialize.public.t_variance
  576. Target cluster: quickstart
  577. EOF
  578. # Should include only one sum(*) aggregate.
  579. query T multiline
  580. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT stddev(x), sum(x) FROM t_variance;
  581. ----
  582. Explained Query:
  583. With
  584. cte l0 =
  585. Reduce aggregates=[sum((#0{x} * #0{x})), sum(#0{x}), count(#0{x})]
  586. ReadStorage materialize.public.t_variance
  587. Return
  588. Project (#4, #3{sum_x})
  589. Map (sqrtf64(case when ((#0{sum}) IS NULL OR (#1{sum_x}) IS NULL OR (case when (#2{count_x} = 0) then null else #2{count_x} end) IS NULL OR (case when (0 = (#2{count_x} - 1)) then null else (#2{count_x} - 1) end) IS NULL) then null else greatest(((#0{sum} - ((#1{sum_x} * #1{sum_x}) / bigint_to_double(case when (#2{count_x} = 0) then null else #2{count_x} end))) / bigint_to_double(case when (0 = (#2{count_x} - 1)) then null else (#2{count_x} - 1) end)), 0) end))
  590. Union
  591. Project (#0{sum}..=#2{count_x}, #1{sum_x})
  592. Get l0
  593. Map (null, null, 0, null)
  594. Union
  595. Negate
  596. Project ()
  597. Get l0
  598. Constant
  599. - ()
  600. Source materialize.public.t_variance
  601. Target cluster: quickstart
  602. EOF