redundant_join.slt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 t1(f1 INT, f2 INT);
  11. statement ok
  12. CREATE TABLE t2(f1 INT NOT NULL, f2 INT NOT NULL);
  13. statement ok
  14. CREATE VIEW v1 AS SELECT t1 from t1;
  15. statement ok
  16. CREATE DEFAULT INDEX ON v1
  17. query T multiline
  18. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  19. SELECT * FROM t1, (SELECT DISTINCT f1 % 2 AS F FROM t1) T WHERE t1.f1 % 2 = t.f;
  20. ----
  21. Explained Query:
  22. Filter (#0{f1}) IS NOT NULL // { arity: 3 }
  23. Map ((#0{f1} % 2)) // { arity: 3 }
  24. ReadStorage materialize.public.t1 // { arity: 2 }
  25. Source materialize.public.t1
  26. filter=((#0{f1}) IS NOT NULL)
  27. Target cluster: quickstart
  28. EOF
  29. query T multiline
  30. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  31. SELECT * FROM v1, (SELECT DISTINCT (v1.t1).f1 as f1 FROM v1) Y WHERE (v1.t1).f1 = y.f1;
  32. ----
  33. Explained Query (fast path):
  34. Filter (#1) IS NOT NULL
  35. Map (record_get[0](#0{t1}))
  36. ReadIndex on=materialize.public.v1 v1_primary_idx=[*** full scan ***]
  37. Used Indexes:
  38. - materialize.public.v1_primary_idx (*** full scan ***)
  39. Target cluster: quickstart
  40. EOF
  41. ## -------------------- Tests for WITH MUTUALLY RECURSIVE --------------------
  42. # Wrapping the first example from this file in a WMR works, but only if we can
  43. # eliminate the `<expr> IS NOT NULL` predicates that are added when pushing the
  44. # join condition through the CrossJoin.
  45. query T multiline
  46. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  47. WITH MUTUALLY RECURSIVE
  48. c0(f1 INT, f2 INT, f INT) AS (
  49. SELECT * FROM c0
  50. UNION
  51. SELECT * FROM t2, (SELECT DISTINCT f1 % 2 AS f FROM t2) t0 WHERE t2.f1 % 2 = t0.f
  52. )
  53. SELECT * FROM c0;
  54. ----
  55. Explained Query:
  56. With Mutually Recursive
  57. cte l0 =
  58. Distinct project=[#0{f1}..=#2] // { arity: 3 }
  59. Union // { arity: 3 }
  60. Get l0 // { arity: 3 }
  61. Map ((#0{f1} % 2)) // { arity: 3 }
  62. ReadStorage materialize.public.t2 // { arity: 2 }
  63. Return // { arity: 3 }
  64. Get l0 // { arity: 3 }
  65. Source materialize.public.t2
  66. Target cluster: quickstart
  67. EOF
  68. # Same query, but selecting from t1 instead of t2. The added `- IS NOT NULL`
  69. # filters prevent redundant join elimination at first. To be able to eliminate
  70. # the join we need to factor out the filter on top of t1 behind a common
  71. # binding (fixed by database-issues#5342).
  72. query T multiline
  73. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  74. WITH MUTUALLY RECURSIVE
  75. c0(f1 INT, f2 INT, f INT) AS (
  76. SELECT * FROM c0
  77. UNION
  78. SELECT * FROM t1, (SELECT DISTINCT f1 % 2 AS f FROM t1) t0 WHERE t1.f1 % 2 = t0.f
  79. )
  80. SELECT * FROM c0;
  81. ----
  82. Explained Query:
  83. With Mutually Recursive
  84. cte l0 =
  85. Distinct project=[#0{f1}..=#2] // { arity: 3 }
  86. Union // { arity: 3 }
  87. Get l0 // { arity: 3 }
  88. Filter (#0{f1}) IS NOT NULL // { arity: 3 }
  89. Map ((#0{f1} % 2)) // { arity: 3 }
  90. ReadStorage materialize.public.t1 // { arity: 2 }
  91. Return // { arity: 3 }
  92. Get l0 // { arity: 3 }
  93. Source materialize.public.t1
  94. filter=((#0{f1}) IS NOT NULL)
  95. Target cluster: quickstart
  96. EOF
  97. # Another case that does not work at the moment because of the naive ProvInfo
  98. # initialization for WMR bindings.
  99. query T multiline
  100. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  101. WITH
  102. t0 AS (
  103. SELECT DISTINCT f1 % 2 AS f, 42 as c FROM t2
  104. )
  105. SELECT * FROM (
  106. WITH MUTUALLY RECURSIVE
  107. c0(f INT) AS (
  108. SELECT f FROM t0
  109. ),
  110. c1(f1 INT, f2 INT, f INT) AS (
  111. SELECT * FROM c1
  112. UNION
  113. SELECT f, f, f from c0
  114. UNION
  115. SELECT * FROM t2, c0 WHERE t2.f1 % 2 = c0.f
  116. )
  117. SELECT f FROM c1 UNION ALL SELECT c FROM t0
  118. );
  119. ----
  120. Explained Query:
  121. With
  122. cte l0 =
  123. Distinct project=[(#0{f1} % 2)] // { arity: 1 }
  124. Project (#0{f1}) // { arity: 1 }
  125. ReadStorage materialize.public.t2 // { arity: 2 }
  126. Return // { arity: 1 }
  127. With Mutually Recursive
  128. cte l1 =
  129. Distinct project=[#0{f1}..=#2] // { arity: 3 }
  130. Union // { arity: 3 }
  131. Get l1 // { arity: 3 }
  132. Project (#0, #0, #0) // { arity: 3 }
  133. Get l0 // { arity: 1 }
  134. Map ((#0{f1} % 2)) // { arity: 3 }
  135. ReadStorage materialize.public.t2 // { arity: 2 }
  136. Return // { arity: 1 }
  137. Union // { arity: 1 }
  138. Project (#2) // { arity: 1 }
  139. Get l1 // { arity: 3 }
  140. Project (#1) // { arity: 1 }
  141. Map (42) // { arity: 2 }
  142. Get l0 // { arity: 1 }
  143. Source materialize.public.t2
  144. Target cluster: quickstart
  145. EOF