mir_arity.slt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. statement ok
  10. CREATE TABLE t (
  11. a int,
  12. b int
  13. )
  14. statement ok
  15. CREATE TABLE u (
  16. c int,
  17. d int
  18. )
  19. statement ok
  20. CREATE TABLE v (
  21. e int,
  22. f int
  23. )
  24. statement ok
  25. CREATE INDEX t_a_idx ON T(a);
  26. statement ok
  27. CREATE INDEX u_d_idx ON U(d);
  28. # Constant EXCEPT (<outer join> order by ..) will return at least one instance
  29. # of each flavor of MirRelationExpr.
  30. statement ok
  31. CREATE VIEW test1 AS
  32. (SELECT 1 as a, 2 as b, 11 as h, 12 as g) EXCEPT (SELECT t.*, u.c + 1 as g FROM (SELECT a, b, generate_series(a, b) as h FROM t) t LEFT OUTER JOIN u on t.a = u.d
  33. ORDER BY t.b LIMIT 10 OFFSET 1);
  34. query T multiline
  35. EXPLAIN OPTIMIZED PLAN WITH(arity, humanized expressions) AS VERBOSE TEXT FOR SELECT * FROM test1
  36. ----
  37. Explained Query:
  38. With
  39. cte l0 =
  40. FlatMap generate_series(#0{a}, #1{b}, 1) // { arity: 3 }
  41. ReadIndex on=t t_a_idx=[*** full scan ***] // { arity: 2 }
  42. cte l1 =
  43. ArrangeBy keys=[[#0{a}]] // { arity: 3 }
  44. Filter (#0{a}) IS NOT NULL // { arity: 3 }
  45. Get l0 // { arity: 3 }
  46. cte l2 =
  47. Project (#0{a}..=#3{c}) // { arity: 4 }
  48. Join on=(#0{a} = #4{d}) type=differential // { arity: 5 }
  49. Get l1 // { arity: 3 }
  50. ArrangeBy keys=[[#1{d}]] // { arity: 2 }
  51. ReadIndex on=u u_d_idx=[differential join] // { arity: 2 }
  52. Return // { arity: 4 }
  53. Threshold // { arity: 4 }
  54. Union // { arity: 4 }
  55. Negate // { arity: 4 }
  56. Distinct project=[#0{a}..=#3] // { arity: 4 }
  57. TopK order_by=[#1{b} asc nulls_last] limit=10 offset=1 // { arity: 4 }
  58. Project (#0{a}..=#2, #4) // { arity: 4 }
  59. Map ((#3{c} + 1)) // { arity: 5 }
  60. Union // { arity: 4 }
  61. Map (null) // { arity: 4 }
  62. Union // { arity: 3 }
  63. Negate // { arity: 3 }
  64. Project (#0{a}..=#2) // { arity: 3 }
  65. Join on=(#0{a} = #3{a}) type=differential // { arity: 4 }
  66. Get l1 // { arity: 3 }
  67. ArrangeBy keys=[[#0{a}]] // { arity: 1 }
  68. Distinct project=[#0{a}] // { arity: 1 }
  69. Project (#0{a}) // { arity: 1 }
  70. Get l2 // { arity: 4 }
  71. Get l0 // { arity: 3 }
  72. Get l2 // { arity: 4 }
  73. Constant // { arity: 4 }
  74. - (1, 2, 11, 12)
  75. Used Indexes:
  76. - materialize.public.t_a_idx (*** full scan ***)
  77. - materialize.public.u_d_idx (differential join)
  78. Target cluster: quickstart
  79. EOF
  80. # a reduce with an aggregation.
  81. query T multiline
  82. EXPLAIN OPTIMIZED PLAN WITH(arity, humanized expressions) AS VERBOSE TEXT FOR
  83. SELECT sum(e * f), max(f) FROM v GROUP BY mod(e, 5)
  84. ----
  85. Explained Query:
  86. Project (#1{sum}, #2{max_f}) // { arity: 2 }
  87. Reduce group_by=[(#0{e} % 5)] aggregates=[sum((#0{e} * #1{f})), max(#1{f})] // { arity: 3 }
  88. ReadStorage materialize.public.v // { arity: 2 }
  89. Source materialize.public.v
  90. Target cluster: quickstart
  91. EOF
  92. # A let where the value has a different arity from the body
  93. query T multiline
  94. EXPLAIN OPTIMIZED PLAN WITH(arity, humanized expressions) AS VERBOSE TEXT FOR
  95. WITH u AS (select u.c + 1 as g from u)
  96. SELECT u.g as g, w.g as h FROM u, u as w WHERE u.g = w.g
  97. ----
  98. Explained Query:
  99. With
  100. cte l0 =
  101. ArrangeBy keys=[[#0{g}]] // { arity: 1 }
  102. Project (#2) // { arity: 1 }
  103. Filter (#0{c}) IS NOT NULL // { arity: 3 }
  104. Map ((#0{c} + 1)) // { arity: 3 }
  105. ReadIndex on=u u_d_idx=[*** full scan ***] // { arity: 2 }
  106. Return // { arity: 2 }
  107. Project (#0, #0) // { arity: 2 }
  108. Join on=(#0{g} = #1{g}) type=differential // { arity: 2 }
  109. Get l0 // { arity: 1 }
  110. Get l0 // { arity: 1 }
  111. Used Indexes:
  112. - materialize.public.u_d_idx (*** full scan ***)
  113. Target cluster: quickstart
  114. EOF
  115. # a constant error
  116. query T multiline
  117. EXPLAIN OPTIMIZED PLAN WITH(arity, humanized expressions) AS VERBOSE TEXT FOR
  118. SELECT * FROM u WHERE (SELECT f FROM v WHERE v.e = u.d) = 1
  119. ----
  120. Explained Query:
  121. With
  122. cte l0 =
  123. Project (#0{d}, #2{f}) // { arity: 2 }
  124. Join on=(#0{d} = #1{e}) type=differential // { arity: 3 }
  125. ArrangeBy keys=[[#0{d}]] // { arity: 1 }
  126. Distinct project=[#0{d}] // { arity: 1 }
  127. Project (#1{d}) // { arity: 1 }
  128. Filter (#1{d}) IS NOT NULL // { arity: 2 }
  129. ReadIndex on=u u_d_idx=[*** full scan ***] // { arity: 2 }
  130. ArrangeBy keys=[[#0{e}]] // { arity: 2 }
  131. Filter (#0{e}) IS NOT NULL // { arity: 2 }
  132. ReadStorage materialize.public.v // { arity: 2 }
  133. Return // { arity: 2 }
  134. Project (#0{c}, #1{d}) // { arity: 2 }
  135. Join on=(#1{d} = #2{d}) type=differential // { arity: 3 }
  136. ArrangeBy keys=[[#1{d}]] // { arity: 2 }
  137. ReadIndex on=u u_d_idx=[differential join] // { arity: 2 }
  138. ArrangeBy keys=[[#0{d}]] // { arity: 1 }
  139. Union // { arity: 1 }
  140. Project (#0{d}) // { arity: 1 }
  141. Filter (#1{f} = 1) // { arity: 2 }
  142. Get l0 // { arity: 2 }
  143. Project (#0{d}) // { arity: 1 }
  144. Filter (#2 = 1) // { arity: 3 }
  145. FlatMap guard_subquery_size(#1{count}) // { arity: 3 }
  146. Reduce group_by=[#0{d}] aggregates=[count(*)] // { arity: 2 }
  147. Project (#0{d}) // { arity: 1 }
  148. Get l0 // { arity: 2 }
  149. Source materialize.public.v
  150. filter=((#0{e}) IS NOT NULL)
  151. Used Indexes:
  152. - materialize.public.u_d_idx (*** full scan ***, differential join)
  153. Target cluster: quickstart
  154. EOF