locally_optimized_plan.slt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. simple conn=mz_system,user=mz_system
  10. ALTER SYSTEM SET enable_new_outer_join_lowering TO false;
  11. ----
  12. COMPLETE 0
  13. statement ok
  14. CREATE TABLE accounts(id int, balance int);
  15. # Use `id bigint` instead of `id int` to force differences in planning based on
  16. # the `enable_new_outer_join_lowering` feature flag value.
  17. statement ok
  18. CREATE TABLE account_details(id bigint, address string);
  19. statement ok
  20. CREATE OR REPLACE VIEW v AS
  21. SELECT
  22. *
  23. FROM
  24. accounts a
  25. LEFT JOIN account_details ad USING(id)
  26. WHERE
  27. balance = 100;
  28. mode cockroach
  29. # Must explain the "Locally Optimized Plan".
  30. query T multiline
  31. EXPLAIN LOCALLY OPTIMIZED PLAN FOR
  32. VIEW v;
  33. ----
  34. With
  35. cte l0 =
  36. Join on=(#2{id} = integer_to_bigint(#0{id}))
  37. Filter (#1{balance} = 100) AND (#0{id}) IS NOT NULL
  38. Get materialize.public.accounts
  39. Filter (#0{id}) IS NOT NULL
  40. Get materialize.public.account_details
  41. cte l1 =
  42. Filter (#1 = 100)
  43. Get materialize.public.accounts
  44. Return
  45. Project (#0, #1, #3)
  46. Union
  47. Get l0
  48. Project (#0, #3..=#5)
  49. Map (100, null, null)
  50. Join on=(#0 = #1)
  51. Union
  52. Negate
  53. Distinct project=[#0]
  54. Get l0
  55. Distinct project=[#0]
  56. Get l1
  57. Get l1
  58. EOF
  59. # Must explain the "Locally Optimized Plan" (same as above).
  60. query T multiline
  61. EXPLAIN LOCALLY OPTIMIZED PLAN FOR
  62. REPLAN VIEW v;
  63. ----
  64. With
  65. cte l0 =
  66. Join on=(#2{id} = integer_to_bigint(#0{id}))
  67. Filter (#1{balance} = 100) AND (#0{id}) IS NOT NULL
  68. Get materialize.public.accounts
  69. Filter (#0{id}) IS NOT NULL
  70. Get materialize.public.account_details
  71. cte l1 =
  72. Filter (#1 = 100)
  73. Get materialize.public.accounts
  74. Return
  75. Project (#0, #1, #3)
  76. Union
  77. Get l0
  78. Project (#0, #3..=#5)
  79. Map (100, null, null)
  80. Join on=(#0 = #1)
  81. Union
  82. Negate
  83. Distinct project=[#0]
  84. Get l0
  85. Distinct project=[#0]
  86. Get l1
  87. Get l1
  88. EOF
  89. # Must explain the "Locally Optimized Plan" after changing the feature flag
  90. # (same as below).
  91. query T multiline
  92. EXPLAIN LOCALLY OPTIMIZED PLAN WITH(ENABLE NEW OUTER JOIN LOWERING = TRUE) FOR
  93. REPLAN VIEW v;
  94. ----
  95. With
  96. cte l0 =
  97. Join on=(#2{id} = integer_to_bigint(#0{id}))
  98. Filter (#0{id}) IS NOT NULL
  99. Get materialize.public.accounts
  100. Filter (#0{id}) IS NOT NULL
  101. Get materialize.public.account_details
  102. Return
  103. Project (#0, #1, #3)
  104. Union
  105. Map (null, null)
  106. Union
  107. Project (#0, #1)
  108. Negate
  109. Join on=(#2 = integer_to_bigint(#0{id}))
  110. Filter (#1{balance} = 100) AND (#0{id}) IS NOT NULL
  111. Get materialize.public.accounts
  112. Distinct project=[#2]
  113. Get l0
  114. Filter (#1{balance} = 100)
  115. Get materialize.public.accounts
  116. Filter (#1{balance} = 100)
  117. Get l0
  118. EOF
  119. # Change the feature flag value
  120. simple conn=mz_system,user=mz_system
  121. ALTER SYSTEM SET enable_new_outer_join_lowering TO true;
  122. ----
  123. COMPLETE 0
  124. # Must be planning with the feature flag turned on.
  125. statement ok
  126. CREATE OR REPLACE VIEW v AS
  127. SELECT
  128. *
  129. FROM
  130. accounts a
  131. LEFT JOIN account_details ad USING(id)
  132. WHERE
  133. balance = 100;
  134. # Ensure that flag was used during planning.
  135. query T multiline
  136. EXPLAIN LOCALLY OPTIMIZED PLAN FOR
  137. VIEW v;
  138. ----
  139. With
  140. cte l0 =
  141. Join on=(#2{id} = integer_to_bigint(#0{id}))
  142. Filter (#0{id}) IS NOT NULL
  143. Get materialize.public.accounts
  144. Filter (#0{id}) IS NOT NULL
  145. Get materialize.public.account_details
  146. Return
  147. Project (#0, #1, #3)
  148. Union
  149. Map (null, null)
  150. Union
  151. Project (#0, #1)
  152. Negate
  153. Join on=(#2 = integer_to_bigint(#0{id}))
  154. Filter (#1{balance} = 100) AND (#0{id}) IS NOT NULL
  155. Get materialize.public.accounts
  156. Distinct project=[#2]
  157. Get l0
  158. Filter (#1{balance} = 100)
  159. Get materialize.public.accounts
  160. Filter (#1{balance} = 100)
  161. Get l0
  162. EOF
  163. # Must be re-planning with the feature flag turned off.
  164. query T multiline
  165. EXPLAIN LOCALLY OPTIMIZED PLAN WITH(ENABLE NEW OUTER JOIN LOWERING = FALSE) FOR
  166. REPLAN VIEW v;
  167. ----
  168. With
  169. cte l0 =
  170. Join on=(#2{id} = integer_to_bigint(#0{id}))
  171. Filter (#1{balance} = 100) AND (#0{id}) IS NOT NULL
  172. Get materialize.public.accounts
  173. Filter (#0{id}) IS NOT NULL
  174. Get materialize.public.account_details
  175. cte l1 =
  176. Filter (#1 = 100)
  177. Get materialize.public.accounts
  178. Return
  179. Project (#0, #1, #3)
  180. Union
  181. Get l0
  182. Project (#0, #3..=#5)
  183. Map (100, null, null)
  184. Join on=(#0 = #1)
  185. Union
  186. Negate
  187. Distinct project=[#0]
  188. Get l0
  189. Distinct project=[#0]
  190. Get l1
  191. Get l1
  192. EOF
  193. ## Constant views
  194. ## (Regression tests for https://github.com/MaterializeInc/database-issues/issues/8985 )
  195. statement ok
  196. CREATE VIEW v2 AS SELECT 1;
  197. query T multiline
  198. EXPLAIN LOCALLY OPTIMIZED PLAN FOR
  199. REPLAN VIEW v2
  200. ----
  201. Constant
  202. - (1)
  203. EOF
  204. query T multiline
  205. EXPLAIN LOCALLY OPTIMIZED PLAN FOR
  206. CREATE VIEW v3 AS SELECT 5;
  207. ----
  208. Constant
  209. - (5)
  210. EOF
  211. # LOCALLY OPTIMIZED PLAN FOR constant MV
  212. query T multiline
  213. EXPLAIN LOCALLY OPTIMIZED PLAN FOR
  214. CREATE MATERIALIZED VIEW v3 AS SELECT 5;
  215. ----
  216. Constant
  217. - (5)
  218. Target cluster: quickstart
  219. EOF
  220. # LOCALLY OPTIMIZED PLAN FOR constant peek
  221. query T multiline
  222. EXPLAIN LOCALLY OPTIMIZED PLAN FOR
  223. SELECT 5;
  224. ----
  225. Constant
  226. - (5)
  227. Target cluster: mz_catalog_server
  228. EOF