github-24506.slt 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. mode cockroach
  10. simple conn=mz_system,user=mz_system
  11. ALTER SYSTEM SET enable_eager_delta_joins TO true;
  12. ----
  13. COMPLETE 0
  14. # Tests for nested WITH MUTUALLY RECURSIVE
  15. statement ok
  16. CREATE TABLE edges (src int, dst int);
  17. statement ok
  18. INSERT INTO edges SELECT x, x + 1 FROM generate_series(0, 9) as x;
  19. statement ok
  20. INSERT INTO edges VALUES (4, 2), (8, 6);
  21. statement ok
  22. CREATE VIEW strongly_connected_components AS
  23. WITH MUTUALLY RECURSIVE
  24. intra_edges (src int, dst int) as (
  25. SELECT * FROM edges
  26. EXCEPT ALL
  27. SELECT * FROM edges_delayed
  28. UNION ALL
  29. SELECT src, dst
  30. FROM
  31. edges,
  32. forward_labels f_src,
  33. forward_labels f_dst,
  34. reverse_labels r_src,
  35. reverse_labels r_dst
  36. WHERE src = f_src.node
  37. AND src = r_src.node
  38. AND dst = f_dst.node
  39. AND dst = r_dst.node
  40. AND f_src.label = f_dst.label
  41. AND r_src.label = r_dst.label
  42. ),
  43. forward_labels (node int, label int) AS (
  44. WITH MUTUALLY RECURSIVE
  45. label (node int, comp int) AS (
  46. SELECT dst, MIN(comp)
  47. FROM (
  48. SELECT dst, dst AS comp FROM edges
  49. UNION ALL
  50. SELECT intra_edges.dst, label.comp
  51. FROM intra_edges, label
  52. WHERE intra_edges.src = label.node
  53. )
  54. GROUP BY dst
  55. )
  56. SELECT * FROM label
  57. ),
  58. reverse_labels (node int, label int) AS (
  59. WITH MUTUALLY RECURSIVE
  60. label (node int, comp int) AS (
  61. SELECT src, MIN(comp)
  62. FROM (
  63. SELECT src, src AS comp FROM edges
  64. UNION ALL
  65. SELECT intra_edges.src, label.comp
  66. FROM intra_edges, label
  67. WHERE intra_edges.dst = label.node
  68. )
  69. GROUP BY src
  70. )
  71. SELECT * FROM label
  72. ),
  73. edges_delayed (src int, dst int) AS (SELECT * FROM edges)
  74. SELECT * FROM forward_labels UNION SELECT * FROM reverse_labels;
  75. statement ok
  76. CREATE DEFAULT INDEX ON strongly_connected_components;
  77. query II
  78. SELECT size, COUNT(*) FROM (
  79. SELECT label, COUNT(*) as size
  80. FROM strongly_connected_components
  81. GROUP BY label
  82. )
  83. GROUP BY size;
  84. ----
  85. 1 5
  86. 3 2