reduce_elision.slt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 unsafe_enable_table_keys = true
  11. ----
  12. COMPLETE 0
  13. query I
  14. select (select sum(1) from (select c) group by c) from (select 1 as c)
  15. ----
  16. 1
  17. query T
  18. select (select jsonb_agg(1) from (select c) group by c) from (select 1 as c)
  19. ----
  20. [1]
  21. statement ok
  22. CREATE TABLE x (f0 int4, f1 string);
  23. statement ok
  24. CREATE TABLE y (f0 int2, f1 string primary key);
  25. # `ReductionPushdown` pushes the Distinct from after the Join into both join inputs.
  26. # Then, `ReduceElision` eliminates the Distinct from the 2nd join input.
  27. query T multiline
  28. EXPLAIN OPTIMIZED PLAN WITH(arity, join implementations, keys, humanized expressions) AS VERBOSE TEXT FOR
  29. SELECT DISTINCT *
  30. FROM x, y
  31. WHERE x.f1 = y.f1
  32. ----
  33. Explained Query:
  34. Project (#0{f0}..=#2{f0}, #1{f1}) // { arity: 4, keys: "([0, 1])" }
  35. Join on=(#1{f1} = #3{f1}) type=differential // { arity: 4, keys: "([0, 1])" }
  36. implementation
  37. %1:y[#1]UK » %0[#1]K
  38. ArrangeBy keys=[[#1{f1}]] // { arity: 2, keys: "([0, 1])" }
  39. Distinct project=[#0{f0}, #1{f1}] // { arity: 2, keys: "([0, 1])" }
  40. Filter (#1{f1}) IS NOT NULL // { arity: 2, keys: "()" }
  41. ReadStorage materialize.public.x // { arity: 2, keys: "()" }
  42. ArrangeBy keys=[[#1{f1}]] // { arity: 2, keys: "([1])" }
  43. ReadStorage materialize.public.y // { arity: 2, keys: "([1])" }
  44. Source materialize.public.x
  45. filter=((#1{f1}) IS NOT NULL)
  46. Source materialize.public.y
  47. Target cluster: quickstart
  48. EOF
  49. ## -------------------- Tests for WITH MUTUALLY RECURSIVE --------------------
  50. # `ReductionPushdown` pushes the Distinct from after the Join into both join inputs.
  51. # Then, `ReduceElision` eliminates the Distinct from the 2nd join input.
  52. query T multiline
  53. EXPLAIN OPTIMIZED PLAN WITH(arity, join implementations, keys, humanized expressions) AS VERBOSE TEXT FOR
  54. WITH MUTUALLY RECURSIVE
  55. c0(f0 int4, f1 string, f2 int2, f3 string) AS (
  56. (SELECT DISTINCT *
  57. FROM x, y
  58. WHERE x.f1 = y.f1)
  59. UNION ALL
  60. (SELECT *
  61. FROM c0)
  62. )
  63. SELECT * FROM c0;
  64. ----
  65. Explained Query:
  66. With Mutually Recursive
  67. cte l0 =
  68. Union // { arity: 4, keys: "()" }
  69. Project (#0{f0}..=#2{f0}, #1{f1}) // { arity: 4, keys: "([0, 1])" }
  70. Join on=(#1{f1} = #3{f1}) type=differential // { arity: 4, keys: "([0, 1])" }
  71. implementation
  72. %1:y[#1]UK » %0[#1]K
  73. ArrangeBy keys=[[#1{f1}]] // { arity: 2, keys: "([0, 1])" }
  74. Distinct project=[#0{f0}, #1{f1}] // { arity: 2, keys: "([0, 1])" }
  75. Filter (#1{f1}) IS NOT NULL // { arity: 2, keys: "()" }
  76. ReadStorage materialize.public.x // { arity: 2, keys: "()" }
  77. ArrangeBy keys=[[#1{f1}]] // { arity: 2, keys: "([1])" }
  78. ReadStorage materialize.public.y // { arity: 2, keys: "([1])" }
  79. Get l0 // { arity: 4, keys: "()" }
  80. Return // { arity: 4, keys: "()" }
  81. Get l0 // { arity: 4, keys: "()" }
  82. Source materialize.public.x
  83. filter=((#1{f1}) IS NOT NULL)
  84. Source materialize.public.y
  85. Target cluster: quickstart
  86. EOF
  87. # Similar to the previous test, but
  88. # - Has UNION instead of UNION ALL. This means that there is a Distinct at the root of l0.
  89. # - The second input of the UNION has a DISTINCT. This should be eliminated later by `ReduceElision` after we make the
  90. # unique key inference smarter for `LetRec`.
  91. query T multiline
  92. EXPLAIN OPTIMIZED PLAN WITH(arity, join implementations, keys, humanized expressions) AS VERBOSE TEXT FOR
  93. WITH MUTUALLY RECURSIVE
  94. c0(f0 int4, f1 string, f2 int2, f3 string) AS (
  95. (SELECT DISTINCT *
  96. FROM x, y
  97. WHERE x.f1 = y.f1)
  98. UNION
  99. (SELECT DISTINCT *
  100. FROM c0)
  101. )
  102. SELECT * FROM c0;
  103. ----
  104. Explained Query:
  105. With Mutually Recursive
  106. cte l0 =
  107. Distinct project=[#0{f0}..=#3{f1}] // { arity: 4, keys: "([0, 1, 2, 3])" }
  108. Union // { arity: 4, keys: "()" }
  109. Project (#0{f0}..=#2{f0}, #1{f1}) // { arity: 4, keys: "()" }
  110. Join on=(#1{f1} = #3{f1}) type=differential // { arity: 4, keys: "()" }
  111. implementation
  112. %1:y[#1{f1}]UK » %0:x[#1{f1}]K
  113. ArrangeBy keys=[[#1{f1}]] // { arity: 2, keys: "()" }
  114. Filter (#1{f1}) IS NOT NULL // { arity: 2, keys: "()" }
  115. ReadStorage materialize.public.x // { arity: 2, keys: "()" }
  116. ArrangeBy keys=[[#1{f1}]] // { arity: 2, keys: "([1])" }
  117. ReadStorage materialize.public.y // { arity: 2, keys: "([1])" }
  118. Get l0 // { arity: 4, keys: "([0, 1, 2, 3])" }
  119. Return // { arity: 4, keys: "([0, 1, 2, 3])" }
  120. Get l0 // { arity: 4, keys: "([0, 1, 2, 3])" }
  121. Source materialize.public.x
  122. filter=((#1{f1}) IS NOT NULL)
  123. Source materialize.public.y
  124. Target cluster: quickstart
  125. EOF