cte.slt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. # Additional tests in test/sqllogictest/cockroach/with.slt
  10. mode cockroach
  11. statement ok
  12. CREATE TABLE x (a int)
  13. statement ok
  14. INSERT INTO x VALUES (1), (2), (3)
  15. statement ok
  16. CREATE TABLE y (a int)
  17. statement ok
  18. INSERT INTO y VALUES (2), (3), (4)
  19. # Check that a CTE on the lhs of a join works
  20. query I
  21. WITH t AS (SELECT * FROM y WHERE a < 3)
  22. SELECT * FROM t NATURAL JOIN x
  23. ----
  24. 2
  25. # Using a CTE inside a correlated subquery
  26. query I
  27. WITH t(x) AS (SELECT * FROM y WHERE a < 3)
  28. SELECT * FROM x WHERE a IN (
  29. SELECT a FROM t WHERE x.a = t.x
  30. )
  31. ----
  32. 2
  33. # Using a correlated subquery inside a CTE
  34. query I
  35. SELECT * FROM x WHERE a IN
  36. (WITH t AS (SELECT * FROM y WHERE y.a = x.a) SELECT * FROM t);
  37. ----
  38. 2
  39. 3
  40. statement error more than one record produced in subquery
  41. WITH c AS (SELECT a + 1 FROM x) SELECT (SELECT * FROM c);
  42. # Allow re-using names laterally.
  43. query I rowsort
  44. SELECT * FROM (
  45. (WITH c AS (SELECT 1) SELECT * FROM c)
  46. UNION ALL
  47. (WITH c AS (SELECT 2) SELECT * FROM c)
  48. )
  49. ----
  50. 1
  51. 2
  52. statement error specified more than once
  53. SELECT * FROM (
  54. (WITH c AS (SELECT 1), c AS (SELECT 2)
  55. SELECT * FROM c UNION ALL SELECT * FROM c)
  56. )
  57. # Allow re-using names nested.
  58. query I rowsort
  59. SELECT * FROM (
  60. (WITH c AS (SELECT 1) SELECT * FROM
  61. (WITH c AS (SELECT 2) SELECT * FROM c)
  62. UNION ALL
  63. SELECT * FROM c
  64. )
  65. )
  66. ----
  67. 1
  68. 2
  69. # CTE names should only be accessible in their scope.
  70. statement error unknown catalog item
  71. SELECT * FROM (
  72. (WITH c AS (SELECT 1) SELECT * FROM c)
  73. UNION ALL
  74. SELECT * FROM c
  75. )
  76. query I
  77. WITH foo AS (SELECT 1)
  78. (SELECT * FROM foo
  79. UNION ALL
  80. (WITH foo AS (SELECT 2) SELECT * FROM foo)
  81. UNION ALL
  82. (SELECT * FROM foo))
  83. ----
  84. 1
  85. 1
  86. 2
  87. # See 5766.
  88. query error column "a2.f1" does not exist
  89. SELECT * FROM (VALUES (true)) a2 (f1) WHERE (
  90. SELECT TRUE FROM (VALUES (true)) AS a2 (f2)
  91. WHERE (SELECT a2.f1)
  92. )
  93. statement ok
  94. CREATE TABLE squares (x int, y int);
  95. statement ok
  96. CREATE TABLE roots (x int, y int);
  97. statement ok
  98. CREATE TABLE cubes (x int, y int);
  99. statement ok
  100. INSERT INTO squares VALUES
  101. (1, 1), (2, 4), (3, 9), (4, 16);
  102. statement ok
  103. INSERT INTO roots VALUES
  104. (1, 1), (4, 2), (9, 3), (16, 4);
  105. statement ok
  106. INSERT INTO cubes VALUES
  107. (1, 1), (2, 8), (3, 27), (4, 16);
  108. # Correlated expression––this should only return values where squares.y is
  109. # in roots.y and sqaures.x
  110. query II
  111. SELECT * FROM squares
  112. WHERE x IN (
  113. SELECT y FROM roots
  114. WHERE y IN (
  115. SELECT squares.y
  116. )
  117. );
  118. ----
  119. 1 1
  120. # Correlated CTE
  121. query II
  122. SELECT * FROM squares
  123. WHERE x IN (
  124. WITH squares_y AS (
  125. SELECT squares.y
  126. )
  127. SELECT y FROM roots
  128. WHERE y IN (
  129. SELECT y FROM squares_y
  130. )
  131. );
  132. ----
  133. 1 1
  134. # Same query, but inside a view. Regression test for database-issues#1582.
  135. statement ok
  136. CREATE MATERIALIZED VIEW v AS
  137. SELECT * FROM squares
  138. WHERE x IN (
  139. WITH squares_y AS (
  140. SELECT squares.y
  141. )
  142. SELECT y FROM roots
  143. WHERE y IN (
  144. SELECT y FROM squares_y
  145. )
  146. );
  147. query II
  148. SELECT * FROM v
  149. ----
  150. 1 1
  151. # Correlated CTE in different level than it was introduced. This is needlessly
  152. # convoluted but caused crashes in early iterations of CTE's development.
  153. query II
  154. SELECT * FROM squares
  155. WHERE x IN (
  156. WITH squares_x AS (
  157. SELECT squares.x
  158. )
  159. SELECT t0.x
  160. FROM (
  161. SELECT roots.x
  162. FROM roots
  163. JOIN (
  164. SELECT t2.x FROM (
  165. SELECT cubes.x FROM cubes
  166. JOIN squares_x
  167. ON squares_x.x = cubes.x
  168. WHERE cubes.x IN (SELECT x FROM squares_x)
  169. ) t2
  170. ) AS t1
  171. ON t1.x = roots.x
  172. ) AS t0
  173. );
  174. ----
  175. 1 1
  176. 4 16
  177. # Use same query at two levels. Obtusely check for quadratic powers.
  178. query I rowsort
  179. WITH squares_y AS (
  180. SELECT y FROM squares
  181. )
  182. SELECT * FROM squares_y WHERE y IN (
  183. SELECT y * y FROM squares_y
  184. )
  185. ----
  186. 1
  187. 16
  188. # PostgreSQL tests
  189. query TT
  190. WITH q AS (SELECT 'foo' AS x)
  191. SELECT x, pg_typeof(x) FROM q;
  192. ----
  193. foo text
  194. query I rowsort
  195. WITH outermost(x) AS (
  196. SELECT 1
  197. UNION (WITH innermost as (SELECT 2)
  198. SELECT * FROM innermost
  199. UNION SELECT 3)
  200. )
  201. SELECT * FROM outermost ORDER BY 1;
  202. ----
  203. 1
  204. 2
  205. 3
  206. query error unknown catalog item 'outermost'
  207. WITH outermost(x) AS (
  208. SELECT 1
  209. UNION (WITH innermost as (SELECT 2)
  210. SELECT * FROM outermost -- fail
  211. UNION SELECT * FROM innermost)
  212. )
  213. SELECT * FROM outermost ORDER BY 1;
  214. # test database-issues#7102
  215. query I colnames
  216. WITH count AS (VALUES (9)) SELECT count(*) FROM count;
  217. ----
  218. count
  219. 1