aggregates.slt 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. statement ok
  11. CREATE TABLE t (
  12. a INT NOT NULL,
  13. b TEXT NOT NULL,
  14. c TEXT
  15. );
  16. statement ok
  17. INSERT INTO t VALUES (1, '10', 'x'), (2, '20', NULL), (3, '30', NULL), (4, '40', 'x'), (5, '50a', 'x'), (5, '50b', 'y'), (5, '50c', 'z');
  18. query T multiline
  19. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT a, array_agg(b) FROM t GROUP BY a;
  20. ----
  21. Explained Query:
  22. Reduce group_by=[#0{a}] aggregates=[array_agg[order_by=[]](row(array[#1{b}]))]
  23. Project (#0{a}, #1{b})
  24. ReadStorage materialize.public.t
  25. Source materialize.public.t
  26. Target cluster: quickstart
  27. EOF
  28. query T multiline
  29. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT a, array_agg(b), array_agg(c) FROM t GROUP BY a;
  30. ----
  31. Explained Query:
  32. Reduce group_by=[#0{a}] aggregates=[array_agg[order_by=[]](row(array[#1{b}])), array_agg[order_by=[]](row(array[#2{c}]))]
  33. ReadStorage materialize.public.t
  34. Source materialize.public.t
  35. Target cluster: quickstart
  36. EOF
  37. query T multiline
  38. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT a, array_agg(b), string_agg(c, ',') FROM t GROUP BY a;
  39. ----
  40. Explained Query:
  41. Reduce group_by=[#0{a}] aggregates=[array_agg[order_by=[]](row(array[#1{b}])), string_agg[order_by=[]](row(row(#2{c}, ",")))]
  42. ReadStorage materialize.public.t
  43. Source materialize.public.t
  44. Target cluster: quickstart
  45. EOF
  46. query T multiline
  47. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT a, array_agg(b), string_agg(c, ',' ORDER BY b DESC) FROM t GROUP BY a;
  48. ----
  49. Explained Query:
  50. Reduce group_by=[#0{a}] aggregates=[array_agg[order_by=[]](row(array[#1{b}])), string_agg[order_by=[#0{a} desc nulls_first]](row(row(#2{c}, ","), #1{b}))]
  51. ReadStorage materialize.public.t
  52. Source materialize.public.t
  53. Target cluster: quickstart
  54. EOF
  55. query T multiline
  56. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT a, array_agg(b), max(c) FROM t WHERE c <> 'x' GROUP BY a;
  57. ----
  58. Explained Query:
  59. Reduce group_by=[#0{a}] aggregates=[array_agg[order_by=[]](row(array[#1{b}])), max(#2{c})]
  60. Filter (#2{c} != "x")
  61. ReadStorage materialize.public.t
  62. Source materialize.public.t
  63. filter=((#2{c} != "x"))
  64. Target cluster: quickstart
  65. EOF
  66. query T multiline
  67. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT a, array_agg(b), max(b) FROM t GROUP BY a HAVING count(a) > 1;
  68. ----
  69. Explained Query:
  70. Project (#0{a}..=#2{max_b})
  71. Filter (#3{count} > 1)
  72. Reduce group_by=[#0{a}] aggregates=[array_agg[order_by=[]](row(array[#1{b}])), max(#1{b}), count(*)]
  73. Project (#0{a}, #1{b})
  74. ReadStorage materialize.public.t
  75. Source materialize.public.t
  76. Target cluster: quickstart
  77. EOF
  78. query T multiline
  79. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT a, min(b), max(b) FROM t GROUP BY a;
  80. ----
  81. Explained Query:
  82. Reduce group_by=[#0{a}] aggregates=[min(#1{b}), max(#1{b})]
  83. Project (#0{a}, #1{b})
  84. ReadStorage materialize.public.t
  85. Source materialize.public.t
  86. Target cluster: quickstart
  87. EOF
  88. query T multiline
  89. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT a, array_agg(b ORDER BY b ASC), array_agg(b ORDER BY b DESC) FROM t GROUP BY a;
  90. ----
  91. Explained Query:
  92. Reduce group_by=[#0{a}] aggregates=[array_agg[order_by=[#0{a} asc nulls_last]](row(array[#1{b}], #1{b})), array_agg[order_by=[#0{a} desc nulls_first]](row(array[#1{b}], #1{b}))]
  93. Project (#0{a}, #1{b})
  94. ReadStorage materialize.public.t
  95. Source materialize.public.t
  96. Target cluster: quickstart
  97. EOF
  98. query T multiline
  99. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT array_agg(b ORDER BY b ASC), array_agg(b ORDER BY b DESC), bool_or(b IS NOT NULL) FROM t;
  100. ----
  101. Explained Query:
  102. With
  103. cte l0 =
  104. Reduce aggregates=[array_agg[order_by=[#0{b} asc nulls_last]](row(array[#0{b}], #0{b})), array_agg[order_by=[#0{b} desc nulls_first]](row(array[#0{b}], #0{b})), sum(1)]
  105. Project (#1{b})
  106. ReadStorage materialize.public.t
  107. Return
  108. Project (#0{array_agg}, #1{array_agg}, #3)
  109. Map ((#2{sum} > 0))
  110. Union
  111. Get l0
  112. Map (null, null, null)
  113. Union
  114. Negate
  115. Project ()
  116. Get l0
  117. Constant
  118. - ()
  119. Source materialize.public.t
  120. Target cluster: quickstart
  121. EOF
  122. query T multiline
  123. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT t1.a, array_agg(t1.c), array_agg(t2.c) FROM t t1 INNER JOIN t t2 ON t1.c = t2.c WHERE t1.c IS NOT NULL GROUP BY t1.a;
  124. ----
  125. Explained Query:
  126. With
  127. cte l0 =
  128. Project (#0{a}, #2{c})
  129. Filter (#2{c}) IS NOT NULL
  130. ReadStorage materialize.public.t
  131. Return
  132. Project (#0{a}, #1{array_agg}, #1{array_agg})
  133. Reduce group_by=[#0{a}] aggregates=[array_agg[order_by=[]](row(array[#1{c}]))]
  134. Project (#0{a}, #1{c})
  135. Join on=(#1{c} = #2{c}) type=differential
  136. ArrangeBy keys=[[#1{c}]]
  137. Get l0
  138. ArrangeBy keys=[[#0{c}]]
  139. Project (#1{c})
  140. Get l0
  141. Source materialize.public.t
  142. filter=((#2{c}) IS NOT NULL)
  143. Target cluster: quickstart
  144. EOF
  145. query T multiline
  146. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT sum(a), jsonb_agg(b), array_agg(b), array_agg(b) FROM t;
  147. ----
  148. Explained Query:
  149. With
  150. cte l0 =
  151. Reduce aggregates=[sum(#0{a}), jsonb_agg[order_by=[]](row(jsonbable_to_jsonb(#1{b}))), array_agg[order_by=[]](row(array[#1{b}]))]
  152. Project (#0{a}, #1{b})
  153. ReadStorage materialize.public.t
  154. Return
  155. Project (#0{sum_a}..=#2{array_agg}, #2{array_agg})
  156. Union
  157. Get l0
  158. Map (null, null, null)
  159. Union
  160. Negate
  161. Project ()
  162. Get l0
  163. Constant
  164. - ()
  165. Source materialize.public.t
  166. Target cluster: quickstart
  167. EOF
  168. query T multiline
  169. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT a, array_agg(b ORDER BY b) FROM t GROUP BY a HAVING array_agg(b ORDER BY b) = array_agg(b ORDER BY b DESC);
  170. ----
  171. Explained Query:
  172. Project (#0{a}, #1{array_agg})
  173. Filter (#1{array_agg} = #2{array_agg})
  174. Reduce group_by=[#0{a}] aggregates=[array_agg[order_by=[#0{a} asc nulls_last]](row(array[#1{b}], #1{b})), array_agg[order_by=[#0{a} desc nulls_first]](row(array[#1{b}], #1{b}))]
  175. Project (#0{a}, #1{b})
  176. ReadStorage materialize.public.t
  177. Source materialize.public.t
  178. Target cluster: quickstart
  179. EOF
  180. query T multiline
  181. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT a, array_agg(b), array_agg(sha256(b::BYTEA)) FROM t GROUP BY a;
  182. ----
  183. Explained Query:
  184. Reduce group_by=[#0{a}] aggregates=[array_agg[order_by=[]](row(array[#1{b}])), array_agg[order_by=[]](row(array[digest(text_to_bytea(#1{b}), "sha256")]))]
  185. Project (#0{a}, #1{b})
  186. ReadStorage materialize.public.t
  187. Source materialize.public.t
  188. Target cluster: quickstart
  189. EOF
  190. query T multiline
  191. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT a, array_agg(b), array_agg(CASE WHEN a = 1 THEN 'ooo' ELSE b END) FROM t GROUP BY a;
  192. ----
  193. Explained Query:
  194. Reduce group_by=[#0{a}] aggregates=[array_agg[order_by=[]](row(array[#1{b}])), array_agg[order_by=[]](row(array[case when (#0{a} = 1) then "ooo" else #1{b} end]))]
  195. Project (#0{a}, #1{b})
  196. ReadStorage materialize.public.t
  197. Source materialize.public.t
  198. Target cluster: quickstart
  199. EOF
  200. query T multiline
  201. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT a, dense_rank() OVER (ORDER BY a), array_agg(b) FROM t GROUP BY a;
  202. ----
  203. Explained Query:
  204. Project (#3, #5, #4)
  205. Map (record_get[1](#1), record_get[0](#2), record_get[1](#2), record_get[0](#1))
  206. FlatMap unnest_list(#0{dense_rank})
  207. Reduce aggregates=[dense_rank[order_by=[#0{a} asc nulls_last]](row(list[row(#0{a}, #1{array_agg})], #0{a}))]
  208. Reduce group_by=[#0{a}] aggregates=[array_agg[order_by=[]](row(array[#1{b}]))]
  209. Project (#0{a}, #1{b})
  210. ReadStorage materialize.public.t
  211. Source materialize.public.t
  212. Target cluster: quickstart
  213. EOF