projection_lifting.slt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. # This file contains tests for the ProjectionLifting transform.
  10. mode cockroach
  11. statement ok
  12. CREATE TABLE edges (src int, dst int)
  13. # Lifting the projections from the inner SELECT enables join fusion, resulting
  14. # in a single 3-way join in the optimized plan.
  15. query T multiline
  16. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  17. SELECT
  18. a, b, c
  19. FROM
  20. edges as edge,
  21. (
  22. SELECT
  23. e2.src as a,
  24. e2.dst as b,
  25. e3.dst as c
  26. FROM
  27. edges as e2,
  28. edges as e3
  29. WHERE
  30. e2.dst = e3.src
  31. ) as apex(a, b, c)
  32. WHERE
  33. edge.dst = apex.a AND
  34. edge.src = apex.c;
  35. ----
  36. Explained Query:
  37. With
  38. cte l0 =
  39. Filter (#0{src}) IS NOT NULL AND (#1{dst}) IS NOT NULL
  40. ReadStorage materialize.public.edges
  41. Return
  42. Project (#1{dst}, #3{dst}, #0{src})
  43. Join on=(#0{src} = #5{dst} AND #1{dst} = #2{src} AND #3{dst} = #4{src}) type=differential
  44. ArrangeBy keys=[[#1{dst}]]
  45. Get l0
  46. ArrangeBy keys=[[#0{src}]]
  47. Get l0
  48. ArrangeBy keys=[[#0{src}, #1{dst}]]
  49. Get l0
  50. Source materialize.public.edges
  51. filter=((#0{src}) IS NOT NULL AND (#1{dst}) IS NOT NULL)
  52. Target cluster: quickstart
  53. EOF
  54. # The above works also in WMR blocks.
  55. query T multiline
  56. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  57. WITH MUTUALLY RECURSIVE
  58. triangles(a int, b int, c int) AS (
  59. SELECT
  60. a, b, c
  61. FROM
  62. edges as edge,
  63. (
  64. SELECT
  65. e2.src as a,
  66. e2.dst as b,
  67. e3.dst as c
  68. FROM
  69. edges as e2,
  70. edges as e3
  71. WHERE
  72. e2.dst = e3.src
  73. ) as apex(a, b, c)
  74. WHERE
  75. edge.dst = apex.a AND
  76. edge.src = apex.c
  77. ),
  78. triangle_cycles(a int, b int, c int) AS (
  79. SELECT a, b, c FROM triangles
  80. UNION
  81. SELECT c, a, b FROM triangle_cycles
  82. )
  83. SELECT * FROM triangle_cycles;
  84. ----
  85. Explained Query:
  86. With
  87. cte l0 =
  88. Filter (#0{src}) IS NOT NULL AND (#1{dst}) IS NOT NULL
  89. ReadStorage materialize.public.edges
  90. Return
  91. With Mutually Recursive
  92. cte l1 =
  93. Distinct project=[#0{dst}..=#2{src}]
  94. Union
  95. Project (#1{dst}, #3{dst}, #0{src})
  96. Join on=(#0{src} = #5{dst} AND #1{dst} = #2{src} AND #3{dst} = #4{src}) type=differential
  97. ArrangeBy keys=[[#1{dst}]]
  98. Get l0
  99. ArrangeBy keys=[[#0{src}]]
  100. Get l0
  101. ArrangeBy keys=[[#0{src}, #1{dst}]]
  102. Get l0
  103. Project (#2{src}, #0{dst}, #1{dst})
  104. Get l1
  105. Return
  106. Get l1
  107. Source materialize.public.edges
  108. filter=((#0{src}) IS NOT NULL AND (#1{dst}) IS NOT NULL)
  109. Target cluster: quickstart
  110. EOF