monotonic.slt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #
  10. # Test Common subexpression elimination for Relations.
  11. # PR https://github.com/MaterializeInc/materialize/pull/7715
  12. #
  13. statement ok
  14. CREATE SOURCE counter FROM LOAD GENERATOR COUNTER;
  15. # Properly TopK
  16. query T multiline
  17. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * FROM (SELECT counter FROM counter limit 1);
  18. ----
  19. Explained Query:
  20. TopK limit=1 monotonic
  21. ReadStorage materialize.public.counter
  22. Source materialize.public.counter
  23. Target cluster: quickstart
  24. EOF
  25. # Infer monotonic Reduce operator
  26. query T multiline
  27. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT counter % 3, MAX(counter) as sum FROM counter GROUP BY counter % 3;
  28. ----
  29. Explained Query:
  30. Reduce group_by=[(#0{counter} % 3)] aggregates=[max(#0{counter})] monotonic
  31. ReadStorage materialize.public.counter
  32. Source materialize.public.counter
  33. Target cluster: quickstart
  34. EOF
  35. # _No_ propagation of monotonicity through materialized views
  36. statement ok
  37. CREATE MATERIALIZED VIEW v1 AS SELECT DISTINCT counter % 3 as f1 FROM counter GROUP BY counter % 3;
  38. statement ok
  39. CREATE MATERIALIZED VIEW v2 AS SELECT f1 as counter FROM v1 WHERE f1 % 7 = 0;
  40. query T multiline
  41. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT * FROM v1 CROSS JOIN LATERAL (SELECT * FROM v2 WHERE counter < f1 ORDER BY counter DESC LIMIT 3);
  42. ----
  43. Explained Query:
  44. TopK group_by=[#0{f1}] order_by=[#1{counter} desc nulls_first] limit=3
  45. Filter (#1{counter} < #0{f1})
  46. CrossJoin type=differential
  47. ArrangeBy keys=[[]]
  48. ReadStorage materialize.public.v1
  49. ArrangeBy keys=[[]]
  50. ReadStorage materialize.public.v2
  51. Source materialize.public.v1
  52. Source materialize.public.v2
  53. Target cluster: quickstart
  54. EOF
  55. # Propagating monotonicity analysis thorugh recursive queries
  56. # Positive example: both c1 and c2 and consequently the body of the
  57. # WMR are monotonic.
  58. query T multiline
  59. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  60. WITH MUTUALLY RECURSIVE
  61. c0(x INT, y INT) AS (
  62. SELECT * FROM (VALUES (1, 2), (3, 4), (5, 6))
  63. ),
  64. c1(x INT, y INT) AS (
  65. SELECT * FROM c0
  66. UNION ALL
  67. SELECT DISTINCT y, y-1 FROM c2 WHERE x < 1
  68. ),
  69. c2(x INT, y INT) AS (
  70. SELECT * FROM c0
  71. UNION ALL
  72. SELECT DISTINCT x, x+1 FROM c1 WHERE x >= 1
  73. )
  74. SELECT x, MAX(y) FROM (SELECT * FROM c1 UNION SELECT * FROM c2) GROUP BY x
  75. ----
  76. Explained Query:
  77. With Mutually Recursive
  78. cte l0 =
  79. Union
  80. Distinct project=[#0, (#0{y} - 1)] monotonic
  81. Project (#1)
  82. Filter (#0{x} < 1)
  83. Get l1
  84. Constant
  85. - (1, 2)
  86. - (3, 4)
  87. - (5, 6)
  88. cte l1 =
  89. Union
  90. Distinct project=[#0, (#0{x} + 1)] monotonic
  91. Project (#0)
  92. Filter (#0{x} >= 1)
  93. Get l0
  94. Constant
  95. - (1, 2)
  96. - (3, 4)
  97. - (5, 6)
  98. Return
  99. Reduce group_by=[#0] aggregates=[max(#1{y})] monotonic
  100. Distinct project=[#0, #1] monotonic
  101. Union
  102. Get l0
  103. Get l1
  104. Target cluster: quickstart
  105. EOF
  106. # Negative example: c2 (and consequently c2 and the WMR block body) are not
  107. # monotonic because c2 has a LIMIT clause.
  108. query T multiline
  109. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  110. WITH MUTUALLY RECURSIVE
  111. c0(x INT, y INT) AS (
  112. SELECT * FROM (VALUES (1, 2), (3, 4), (5, 6))
  113. ),
  114. c1(x INT, y INT) AS (
  115. SELECT * FROM c0
  116. UNION ALL
  117. SELECT DISTINCT y, y-1 FROM c2 WHERE x < 1
  118. ),
  119. c2(x INT, y INT) AS (
  120. SELECT * FROM c0
  121. UNION ALL
  122. SELECT DISTINCT x, x+1 FROM c1 WHERE x >= 1 LIMIT 2
  123. )
  124. SELECT x, MAX(y) FROM (SELECT * FROM c1 UNION SELECT * FROM c2) GROUP BY x
  125. ----
  126. Explained Query:
  127. With Mutually Recursive
  128. cte l0 =
  129. Union
  130. Distinct project=[#0, (#0{y} - 1)]
  131. Project (#1)
  132. Filter (#0{x} < 1)
  133. Get l1
  134. Constant
  135. - (1, 2)
  136. - (3, 4)
  137. - (5, 6)
  138. cte l1 =
  139. TopK limit=2
  140. Union
  141. Distinct project=[#0, (#0{x} + 1)]
  142. Project (#0)
  143. Filter (#0{x} >= 1)
  144. Get l0
  145. Constant
  146. - (1, 2)
  147. - (3, 4)
  148. - (5, 6)
  149. Return
  150. Reduce group_by=[#0] aggregates=[max(#1{y})]
  151. Distinct project=[#0, #1]
  152. Union
  153. Get l0
  154. Get l1
  155. Target cluster: quickstart
  156. EOF