subscribe-output.td 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. $ postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  10. ALTER SYSTEM SET enable_within_timestamp_order_by_in_subscribe = true;
  11. ALTER SYSTEM SET enable_envelope_debezium_in_subscribe = true;
  12. $ set-regex match=\d{13,20} replacement=<TIMESTAMP>
  13. $ postgres-connect name=mz_system url=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  14. > BEGIN
  15. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT 1 as a, 2 as b union select 2, 1) WITHIN TIMESTAMP ORDER BY a, mz_diff
  16. > FETCH 1 c
  17. <TIMESTAMP> 1 1 2
  18. > FETCH 1 c
  19. <TIMESTAMP> 1 2 1
  20. > COMMIT
  21. > BEGIN
  22. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT 1 as a, 2 as b union select 2, 1) WITHIN TIMESTAMP ORDER BY a, b
  23. > FETCH 1 c
  24. <TIMESTAMP> 1 1 2
  25. > FETCH 1 c
  26. <TIMESTAMP> 1 2 1
  27. > COMMIT
  28. > BEGIN
  29. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT 1 as a, 2 as b union select 2, 1) WITHIN TIMESTAMP ORDER BY b, a
  30. > FETCH 1 c
  31. <TIMESTAMP> 1 2 1
  32. > FETCH 1 c
  33. <TIMESTAMP> 1 1 2
  34. > COMMIT
  35. > BEGIN
  36. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT 1 as a, 2 as b union select 2, 1) WITHIN TIMESTAMP ORDER BY b DESC NULLS LAST, a ASC
  37. > FETCH 1 c
  38. <TIMESTAMP> 1 1 2
  39. > FETCH 1 c
  40. <TIMESTAMP> 1 2 1
  41. > COMMIT
  42. > CREATE TABLE t(a int, b int, c int)
  43. > INSERT INTO t VALUES (1,2,3), (4,0,-4), (2,5,-6)
  44. > BEGIN
  45. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT * FROM t)
  46. > FETCH 3 c
  47. <TIMESTAMP> 1 1 2 3
  48. <TIMESTAMP> 1 4 0 -4
  49. <TIMESTAMP> 1 2 5 -6
  50. > COMMIT
  51. > BEGIN
  52. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT * FROM t) WITHIN TIMESTAMP ORDER BY a, b, c
  53. > FETCH 1 c
  54. <TIMESTAMP> 1 1 2 3
  55. > FETCH 1 c
  56. <TIMESTAMP> 1 2 5 -6
  57. > FETCH 1 c
  58. <TIMESTAMP> 1 4 0 -4
  59. > COMMIT
  60. > BEGIN
  61. > DECLARE c CURSOR FOR SUBSCRIBE t WITHIN TIMESTAMP ORDER BY a, b, c
  62. > FETCH 1 c
  63. <TIMESTAMP> 1 1 2 3
  64. > FETCH 1 c
  65. <TIMESTAMP> 1 2 5 -6
  66. > FETCH 1 c
  67. <TIMESTAMP> 1 4 0 -4
  68. > COMMIT
  69. > BEGIN
  70. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT * FROM t) WITHIN TIMESTAMP ORDER BY c, b, mz_diff, a
  71. > FETCH 1 c
  72. <TIMESTAMP> 1 2 5 -6
  73. > FETCH 1 c
  74. <TIMESTAMP> 1 4 0 -4
  75. > FETCH 1 c
  76. <TIMESTAMP> 1 1 2 3
  77. > COMMIT
  78. > BEGIN
  79. > DECLARE c CURSOR FOR SUBSCRIBE t WITHIN TIMESTAMP ORDER BY c, b, mz_diff, a
  80. > FETCH 1 c
  81. <TIMESTAMP> 1 2 5 -6
  82. > FETCH 1 c
  83. <TIMESTAMP> 1 4 0 -4
  84. > FETCH 1 c
  85. <TIMESTAMP> 1 1 2 3
  86. > COMMIT
  87. > BEGIN
  88. > DECLARE c CURSOR FOR SUBSCRIBE t WITHIN TIMESTAMP ORDER BY 4, 3, 1, 2 -- same as above
  89. > FETCH 1 c
  90. <TIMESTAMP> 1 2 5 -6
  91. > FETCH 1 c
  92. <TIMESTAMP> 1 4 0 -4
  93. > FETCH 1 c
  94. <TIMESTAMP> 1 1 2 3
  95. > COMMIT
  96. > CREATE TABLE t0(key int, value int)
  97. > INSERT INTO t0 VALUES (1, 2), (2, 3)
  98. > BEGIN
  99. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT * FROM t0) WITHIN TIMESTAMP ORDER BY key, mz_diff
  100. > FETCH 1 c
  101. <TIMESTAMP> 1 1 2
  102. > FETCH 1 c
  103. <TIMESTAMP> 1 2 3
  104. $ postgres-execute connection=mz_system
  105. UPDATE t0 SET value = 4 WHERE key = 1
  106. > FETCH 1 c
  107. <TIMESTAMP> -1 1 2
  108. > FETCH 1 c
  109. <TIMESTAMP> 1 1 4
  110. > COMMIT
  111. # make sure progress messages are ok
  112. > BEGIN
  113. > DECLARE c CURSOR FOR SUBSCRIBE t WITH (PROGRESS, SNAPSHOT) WITHIN TIMESTAMP ORDER BY c, b, a
  114. > FETCH 1 c
  115. <TIMESTAMP> true <null> <null> <null> <null>
  116. > FETCH 3 c
  117. <TIMESTAMP> false 1 2 5 -6
  118. <TIMESTAMP> false 1 4 0 -4
  119. <TIMESTAMP> false 1 1 2 3
  120. > COMMIT
  121. ! SUBSCRIBE (SELECT 1 as a, 2 as b union select 2, 1) WITHIN TIMESTAMP ORDER BY c
  122. contains: column "c" does not exist
  123. ! SUBSCRIBE (SELECT 1 as c, 2 as c union select 2, 1) WITHIN TIMESTAMP ORDER BY c
  124. contains: column reference "c" is ambiguous
  125. ! SUBSCRIBE (SELECT 1 as a) WITHIN TIMESTAMP ORDER BY mz_diff + 1 DESC
  126. contains: invalid ORDER BY in SUBSCRIBE WITHIN TIMESTAMP ORDER BY
  127. ! SUBSCRIBE (SELECT 1 as a) WITHIN TIMESTAMP ORDER BY a + 1 DESC
  128. contains: invalid ORDER BY in SUBSCRIBE WITHIN TIMESTAMP ORDER BY
  129. > BEGIN
  130. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT 1 as a, 2 as b union select 2, 1) ENVELOPE UPSERT (KEY (a, b))
  131. > FETCH 1 c
  132. <TIMESTAMP> upsert 1 2
  133. > FETCH 1 c
  134. <TIMESTAMP> upsert 2 1
  135. > COMMIT
  136. > BEGIN
  137. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT 1 as a, 2 as b union select 2, 1) ENVELOPE UPSERT (KEY (b, a))
  138. > FETCH 1 c
  139. <TIMESTAMP> upsert 1 2
  140. > FETCH 1 c
  141. <TIMESTAMP> upsert 2 1
  142. > COMMIT
  143. > CREATE TABLE t1(key int, value int, UNIQUE (key))
  144. > INSERT INTO t1 VALUES (1, 2), (2, 3)
  145. > BEGIN
  146. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT * FROM t1) ENVELOPE UPSERT (KEY (key))
  147. > FETCH 2 c
  148. <TIMESTAMP> upsert 1 2
  149. <TIMESTAMP> upsert 2 3
  150. $ postgres-execute connection=mz_system
  151. UPDATE t1 SET value = 4 WHERE key = 1
  152. > FETCH 1 c
  153. <TIMESTAMP> upsert 1 4
  154. $ postgres-execute connection=mz_system
  155. DELETE FROM t1 WHERE key = 1
  156. > FETCH 1 c
  157. <TIMESTAMP> delete 1 <null>
  158. $ postgres-execute connection=mz_system
  159. INSERT INTO t1 VALUES (1, 1), (1, 1)
  160. > FETCH 1 c
  161. <TIMESTAMP> "key_violation" 1 <null>
  162. $ postgres-execute connection=mz_system
  163. DELETE FROM t1 WHERE key = 1
  164. > FETCH 1 c
  165. <TIMESTAMP> "key_violation" 1 <null>
  166. $ postgres-execute connection=mz_system
  167. INSERT INTO t1 VALUES (1, 1), (1, 2)
  168. > FETCH 1 c
  169. <TIMESTAMP> "key_violation" 1 <null>
  170. $ postgres-execute connection=mz_system
  171. INSERT INTO t1 VALUES (1, 3)
  172. > FETCH 1 c
  173. <TIMESTAMP> upsert 1 3
  174. $ postgres-execute connection=mz_system
  175. DELETE FROM t1 WHERE key = 1 AND value = 1
  176. > FETCH 1 c
  177. <TIMESTAMP> delete 1 <null>
  178. $ postgres-execute connection=mz_system
  179. DELETE FROM t1 WHERE key = 1
  180. > FETCH 1 c
  181. <TIMESTAMP> "key_violation" 1 <null>
  182. > COMMIT
  183. > CREATE TABLE t2(value int, key int, UNIQUE (key))
  184. > INSERT INTO t2 VALUES (2, 1), (3, 2)
  185. > BEGIN
  186. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT * FROM t2) ENVELOPE UPSERT (KEY (key))
  187. > FETCH 2 c
  188. <TIMESTAMP> upsert 1 2
  189. <TIMESTAMP> upsert 2 3
  190. $ postgres-execute connection=mz_system
  191. UPDATE t2 SET value = 4 WHERE key = 1
  192. > FETCH 1 c
  193. <TIMESTAMP> upsert 1 4
  194. $ postgres-execute connection=mz_system
  195. DELETE FROM t2 WHERE key = 1
  196. > FETCH 1 c
  197. <TIMESTAMP> delete 1 <null>
  198. > COMMIT
  199. # make sure progress messages are ok
  200. > BEGIN
  201. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT * FROM t2) WITH (PROGRESS, SNAPSHOT) ENVELOPE UPSERT (KEY (key))
  202. > FETCH 1 c
  203. <TIMESTAMP> true <null> <null> <null>
  204. > FETCH 1 c
  205. <TIMESTAMP> false upsert 2 3
  206. > FETCH 1 c
  207. <TIMESTAMP> true <null> <null> <null>
  208. > COMMIT
  209. ! SUBSCRIBE (SELECT * FROM t2) WITH (PROGRESS, SNAPSHOT) ENVELOPE UPSERT (KEY (invalid_key))
  210. contains: column "invalid_key" does not exist
  211. > BEGIN
  212. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT 1 as a, 2 as b union select 2, 1) ENVELOPE DEBEZIUM (KEY (a, b))
  213. > FETCH 1 c
  214. <TIMESTAMP> insert 1 2
  215. > FETCH 1 c
  216. <TIMESTAMP> insert 2 1
  217. > COMMIT
  218. > BEGIN
  219. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT 1 as a, 2 as b union select 2, 1) ENVELOPE DEBEZIUM (KEY (b, a))
  220. > FETCH 1 c
  221. <TIMESTAMP> insert 1 2
  222. > FETCH 1 c
  223. <TIMESTAMP> insert 2 1
  224. > COMMIT
  225. > DROP TABLE t1 CASCADE
  226. > CREATE TABLE t1(key int, value int, UNIQUE (key))
  227. > INSERT INTO t1 VALUES (1, 2), (2, 3)
  228. > BEGIN
  229. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT * FROM t1) ENVELOPE DEBEZIUM (KEY (key))
  230. > FETCH 2 c
  231. <TIMESTAMP> insert 1 <null> 2
  232. <TIMESTAMP> insert 2 <null> 3
  233. $ postgres-execute connection=mz_system
  234. UPDATE t1 SET value = 4 WHERE key = 1
  235. > FETCH 1 c
  236. <TIMESTAMP> upsert 1 2 4
  237. $ postgres-execute connection=mz_system
  238. DELETE FROM t1 WHERE key = 1
  239. > FETCH 1 c
  240. <TIMESTAMP> delete 1 4 <null>
  241. $ postgres-execute connection=mz_system
  242. INSERT INTO t1 VALUES (1, 1), (1, 1)
  243. > FETCH 1 c
  244. <TIMESTAMP> "key_violation" 1 <null> <null>
  245. $ postgres-execute connection=mz_system
  246. DELETE FROM t1 WHERE key = 1
  247. > FETCH 1 c
  248. <TIMESTAMP> "key_violation" 1 <null> <null>
  249. $ postgres-execute connection=mz_system
  250. INSERT INTO t1 VALUES (1, 1), (1, 2)
  251. > FETCH 1 c
  252. <TIMESTAMP> "key_violation" 1 <null> <null>
  253. $ postgres-execute connection=mz_system
  254. INSERT INTO t1 VALUES (1, 3)
  255. > FETCH 1 c
  256. <TIMESTAMP> insert 1 <null> 3
  257. $ postgres-execute connection=mz_system
  258. DELETE FROM t1 WHERE key = 1 AND value = 1
  259. > FETCH 1 c
  260. <TIMESTAMP> delete 1 1 <null>
  261. $ postgres-execute connection=mz_system
  262. DELETE FROM t1 WHERE key = 1
  263. > FETCH 1 c
  264. <TIMESTAMP> "key_violation" 1 <null> <null>
  265. > COMMIT
  266. > DROP TABLE t2 CASCADE
  267. > CREATE TABLE t2(value int, key int, UNIQUE (key))
  268. > INSERT INTO t2 VALUES (2, 1), (3, 2)
  269. > BEGIN
  270. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT * FROM t2) ENVELOPE DEBEZIUM (KEY (key))
  271. > FETCH 2 c
  272. <TIMESTAMP> insert 1 <null> 2
  273. <TIMESTAMP> insert 2 <null> 3
  274. $ postgres-execute connection=mz_system
  275. UPDATE t2 SET value = 4 WHERE key = 1
  276. > FETCH 1 c
  277. <TIMESTAMP> upsert 1 2 4
  278. $ postgres-execute connection=mz_system
  279. DELETE FROM t2 WHERE key = 1
  280. > FETCH 1 c
  281. <TIMESTAMP> delete 1 4 <null>
  282. > COMMIT
  283. # make sure progress messages are ok
  284. > BEGIN
  285. > DECLARE c CURSOR FOR SUBSCRIBE (SELECT * FROM t2) WITH (PROGRESS, SNAPSHOT) ENVELOPE DEBEZIUM (KEY (key))
  286. > FETCH 1 c
  287. <TIMESTAMP> true <null> <null> <null> <null>
  288. > FETCH 1 c
  289. <TIMESTAMP> false insert 2 <null> 3
  290. > FETCH 1 c
  291. <TIMESTAMP> true <null> <null> <null> <null>
  292. > COMMIT
  293. ! SUBSCRIBE (SELECT * FROM t2) WITH (PROGRESS, SNAPSHOT) ENVELOPE DEBEZIUM (KEY (invalid_key))
  294. contains: column "invalid_key" does not exist
  295. # SUBSCRIBE with a query that has a `RowSetFinishing`, which needs to be turned into a TopK
  296. > CREATE TABLE t3 (a int, b int);
  297. > INSERT INTO t3 VALUES (1,2), (3,4), (5,6), (7,8);
  298. > CREATE VIEW limited AS
  299. SELECT * FROM t3
  300. ORDER BY a DESC, b
  301. LIMIT 3;
  302. > BEGIN;
  303. > DECLARE c CURSOR FOR SUBSCRIBE limited;
  304. > FETCH 2 c;
  305. <TIMESTAMP> 1 5 6
  306. <TIMESTAMP> 1 3 4
  307. > COMMIT;
  308. # Subscribe with a query that uses two columns for upsert key.
  309. > CREATE TABLE t4 (a int, b text, c uuid);
  310. > INSERT INTO t4 VALUES (1, 'foo', '30a7947e-4b38-46a4-b7c2-602ba62ce20b');
  311. > BEGIN;
  312. > DECLARE c4u CURSOR FOR SUBSCRIBE t4 ENVELOPE UPSERT (KEY (c, a));
  313. > FETCH 1 c4u;
  314. <TIMESTAMP> upsert 30a7947e-4b38-46a4-b7c2-602ba62ce20b 1 foo
  315. $ postgres-execute connection=mz_system
  316. UPDATE t4 SET b = 'bar' WHERE a = 1
  317. > FETCH 1 c4u;
  318. <TIMESTAMP> upsert 30a7947e-4b38-46a4-b7c2-602ba62ce20b 1 bar
  319. $ postgres-execute connection=mz_system
  320. DELETE FROM t4 WHERE b = 'bar'
  321. > FETCH 1 c4u;
  322. <TIMESTAMP> delete 30a7947e-4b38-46a4-b7c2-602ba62ce20b 1 <null>
  323. > COMMIT;
  324. > INSERT INTO t4 VALUES (1, 'foo', '30a7947e-4b38-46a4-b7c2-602ba62ce20b');
  325. > BEGIN;
  326. > DECLARE c4d CURSOR FOR SUBSCRIBE t4 ENVELOPE DEBEZIUM (KEY (c, a));
  327. > FETCH 1 c4d;
  328. <TIMESTAMP> insert 30a7947e-4b38-46a4-b7c2-602ba62ce20b 1 <null> foo
  329. $ postgres-execute connection=mz_system
  330. UPDATE t4 SET b = 'bar' WHERE a = 1
  331. > FETCH 1 c4d;
  332. <TIMESTAMP> upsert 30a7947e-4b38-46a4-b7c2-602ba62ce20b 1 foo bar
  333. $ postgres-execute connection=mz_system
  334. DELETE FROM t4 WHERE b = 'bar'
  335. > FETCH 1 c4d;
  336. <TIMESTAMP> delete 30a7947e-4b38-46a4-b7c2-602ba62ce20b 1 bar <null>
  337. > COMMIT;