with.slt 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # Copyright 2015 - 2019 The Cockroach Authors. All rights reserved.
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # This file is derived from the logic test suite in CockroachDB. The
  12. # original file was retrieved on June 10, 2019 from:
  13. #
  14. # https://github.com/cockroachdb/cockroach/blob/d2f7fbf5dd1fc1a099bbad790a2e1f7c60a66cc3/pkg/sql/logictest/testdata/logic_test/with
  15. #
  16. # The original source code is subject to the terms of the Apache
  17. # 2.0 license, a copy of which can be found in the LICENSE file at the
  18. # root of this repository.
  19. mode cockroach
  20. query error table name "a" specified more than once
  21. WITH a AS (SELECT 1) SELECT * FROM a CROSS JOIN a
  22. statement ok
  23. CREATE TABLE x (a int)
  24. statement ok
  25. INSERT INTO x VALUES (1), (2), (3)
  26. statement ok
  27. CREATE TABLE y (a int)
  28. statement ok
  29. INSERT INTO y VALUES (2), (3), (4)
  30. query I rowsort
  31. WITH t AS (SELECT a FROM y WHERE a < 3)
  32. SELECT * FROM x NATURAL JOIN t
  33. ----
  34. 2
  35. query I
  36. WITH t AS (SELECT * FROM y WHERE a < 3)
  37. SELECT * FROM x NATURAL JOIN t
  38. ----
  39. 2
  40. # Using a CTE inside a subquery
  41. query I rowsort
  42. WITH t(x) AS (SELECT a FROM x)
  43. SELECT * FROM y WHERE a IN (SELECT x FROM t)
  44. ----
  45. 2
  46. 3
  47. # Using a subquery inside a CTE
  48. query I
  49. SELECT * FROM x WHERE a IN
  50. (WITH t AS (SELECT * FROM y WHERE a < 3) SELECT * FROM t)
  51. ----
  52. 2
  53. # Rename columns
  54. query II rowsort
  55. WITH t(b) AS (SELECT a FROM x) SELECT b, t.b FROM t
  56. ----
  57. 1 1
  58. 2 2
  59. 3 3
  60. query BB
  61. WITH t(a, b) AS (SELECT true a, false b)
  62. SELECT a, b FROM t
  63. ----
  64. true false
  65. query BB
  66. WITH t(b, a) AS (SELECT true a, false b)
  67. SELECT a, b FROM t
  68. ----
  69. false true
  70. statement error WITH query name "t" specified more than once
  71. WITH
  72. t AS (SELECT true),
  73. t AS (SELECT false)
  74. SELECT * FROM t
  75. query error CTE t definition names 2 columns, but CTE t has 1 column
  76. WITH t(b, c) AS (SELECT a FROM x) SELECT b, t.b FROM t
  77. # Ensure you can't reference the original table name
  78. query error column "x.t" does not exist
  79. WITH t AS (SELECT a FROM x) SELECT a, x.t FROM t
  80. # Nested WITH, name shadowing
  81. query I
  82. WITH t(x) AS (WITH t(x) AS (SELECT 1) SELECT x * 10 FROM t) SELECT x + 2 FROM t
  83. ----
  84. 12
  85. # not supported yet
  86. halt
  87. # CTEs with DMLs
  88. query error pgcode 42P01 relation "t" does not exist
  89. WITH t AS (SELECT * FROM x) INSERT INTO t VALUES (1)
  90. query I rowsort
  91. WITH t AS (SELECT a FROM x) INSERT INTO x SELECT a + 20 FROM t RETURNING *
  92. ----
  93. 21
  94. 22
  95. 23
  96. query I rowsort
  97. SELECT * from x
  98. ----
  99. 1
  100. 2
  101. 3
  102. 21
  103. 22
  104. 23
  105. query I rowsort
  106. WITH t AS (
  107. UPDATE x SET a = a * 100 RETURNING a
  108. )
  109. SELECT * FROM t
  110. ----
  111. 100
  112. 200
  113. 300
  114. 2100
  115. 2200
  116. 2300
  117. query I rowsort
  118. SELECT * from x
  119. ----
  120. 100
  121. 200
  122. 300
  123. 2100
  124. 2200
  125. 2300
  126. query I rowsort
  127. WITH t AS (
  128. DELETE FROM x RETURNING a
  129. )
  130. SELECT * FROM t
  131. ----
  132. 100
  133. 200
  134. 300
  135. 2100
  136. 2200
  137. 2300
  138. query I rowsort
  139. SELECT * from x
  140. ----
  141. # materialize#22420: ensure okay error message for CTE clause without output columns
  142. query error WITH clause "t" does not have a RETURNING clause
  143. WITH t AS (
  144. INSERT INTO x(a) VALUES(0)
  145. )
  146. SELECT * FROM t
  147. # Regression test for materialize#24307 until CockroachDB learns how to execute
  148. # side effects no matter what.
  149. query error unimplemented: common table expression "t" with side effects was not used in query
  150. WITH t AS (
  151. INSERT INTO x(a) VALUES(0) RETURNING a
  152. )
  153. SELECT 1
  154. query error unimplemented: common table expression "t" with side effects was not used in query
  155. WITH t AS (
  156. SELECT * FROM (
  157. WITH b AS (INSERT INTO x(a) VALUES(0) RETURNING a)
  158. TABLE b
  159. )
  160. )
  161. SELECT 1
  162. query error unimplemented: common table expression "t" with side effects was not used in query
  163. WITH t AS (
  164. DELETE FROM x RETURNING a
  165. )
  166. SELECT 1
  167. query error unimplemented: common table expression "t" with side effects was not used in query
  168. WITH t AS (
  169. UPSERT INTO x(a) VALUES(0) RETURNING a
  170. )
  171. SELECT 1
  172. query error unimplemented: common table expression "t" with side effects was not used in query
  173. WITH t AS (
  174. UPDATE x SET a = 0 RETURNING a
  175. )
  176. SELECT 1
  177. # however if there are no side effects, no errors are required.
  178. query I
  179. WITH t AS (SELECT 1) SELECT 2
  180. ----
  181. 2
  182. # Regression tests for materialize#24303.
  183. statement ok
  184. CREATE TABLE a(x INT);
  185. statement count 3
  186. INSERT INTO a(x)
  187. (WITH b(z) AS (VALUES (1),(2),(3)) SELECT z+1 AS w FROM b)
  188. statement count 1
  189. INSERT INTO a(x)
  190. (WITH a(z) AS (VALUES (1)) SELECT z+1 AS w FROM a);
  191. # When materialize#24303 is fixed, the following query should succeed.
  192. query error unimplemented: multiple WITH clauses in parentheses
  193. (WITH woo AS (VALUES (1))
  194. (WITH waa AS (VALUES (2))
  195. TABLE waa))
  196. # When materialize#24303 is fixed, the following query should fail with
  197. # error "no such relation woo".
  198. query error unimplemented: multiple WITH clauses in parentheses
  199. (WITH woo AS (VALUES (1))
  200. (WITH waa AS (VALUES (2))
  201. TABLE woo))
  202. statement ok
  203. CREATE TABLE lim(x) AS SELECT 0
  204. # This is an oddity in PostgreSQL: even though the WITH clause
  205. # occurs in the inside parentheses, the scope of the alias `lim`
  206. # extends to the outer parentheses.
  207. query I
  208. ((WITH lim(x) AS (SELECT 1) SELECT 123)
  209. LIMIT (
  210. SELECT x FROM lim -- intuitively this should refer to the real table lim defined above
  211. -- and use LIMIT 0;
  212. -- however, postgres flattens the inner WITH and outer LIMIT
  213. -- at the same scope so the limit becomes 1.
  214. ))
  215. ----
  216. 123
  217. # Ditto if table `lim` did not even exist.
  218. statement ok
  219. DROP TABLE lim
  220. query I
  221. ((WITH lim(x) AS (SELECT 1) SELECT 123) LIMIT (SELECT x FROM lim))
  222. ----
  223. 123