update.td 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. $ set-arg-default single-replica-cluster=quickstart
  10. # Test UPDATE and DELETE. This must be a testdrive test to avoid symbiosis
  11. # in sqllogictest.
  12. > CREATE TABLE t (i INT, f FLOAT, t TEXT);
  13. > INSERT INTO t VALUES (1, 2, 'a'), (3, 4, 'b');
  14. > SELECT * FROM t ORDER BY i
  15. 1 2 a
  16. 3 4 b
  17. > UPDATE t SET i = i + 1
  18. > SELECT * FROM t ORDER BY i
  19. 2 2 a
  20. 4 4 b
  21. > UPDATE t SET i = i + 1 WHERE i = 2
  22. > SELECT * FROM t ORDER BY i
  23. 3 2 a
  24. 4 4 b
  25. # Check for assignment, implicit casts
  26. > UPDATE t SET i = i::bigint, f = 5, t = t || 'e'
  27. > SELECT * FROM t ORDER BY i
  28. 3 5 ae
  29. 4 5 be
  30. ! UPDATE t SET i = '4'::text
  31. contains:SET clause does not support casting from text to integer
  32. > DELETE FROM t WHERE i < 4
  33. > SELECT * FROM t ORDER BY i
  34. 4 5 be
  35. ! UPDATE t SET f = 'a'
  36. contains:invalid input syntax for type double precision
  37. # Ensure that we can update after an error.
  38. > UPDATE t SET f = 6::FLOAT
  39. > SELECT * FROM t ORDER BY i
  40. 4 6 be
  41. > CREATE TABLE o (i int);
  42. > INSERT INTO o VALUES (1), (2), (3), (4);
  43. # Support subqueries
  44. > UPDATE t SET t = 'xy' WHERE i IN (SELECT i FROM o)
  45. > SELECT * FROM t
  46. 4 6 xy
  47. > CREATE MATERIALIZED VIEW v (a) AS SELECT 4;
  48. > UPDATE t SET i = i + 1 WHERE i IN (SELECT a FROM v);
  49. > SELECT * FROM t
  50. 5 6 xy
  51. ! UPDATE v SET a = 1
  52. contains:cannot mutate materialized view
  53. ! UPDATE mz_tables SET a = 1
  54. contains:cannot mutate system table
  55. ! UPDATE t SET a = 1
  56. contains:unknown column a
  57. ! UPDATE t SET i = 1 WHERE a = 1
  58. contains:column "a" does not exist
  59. ! UPDATE t SET i = 1 WHERE i = 'a'
  60. contains:invalid input syntax for type integer
  61. ! UPDATE t SET i = 1, i = 1
  62. contains:column i set twice
  63. > BEGIN
  64. ! UPDATE t SET i = 1
  65. contains:cannot be run inside a transaction block
  66. > ROLLBACK
  67. # Verify that UPDATE and INSERT cannot co-exist.
  68. > BEGIN
  69. > INSERT INTO t DEFAULT VALUES;
  70. ! UPDATE t SET i = 1
  71. contains:cannot be run inside a transaction block
  72. > ROLLBACK
  73. > BEGIN
  74. ! DELETE FROM t
  75. contains:cannot be run inside a transaction block
  76. > ROLLBACK
  77. # Update subqueries cannot reference sources
  78. $ set schema={
  79. "type": "record",
  80. "name": "row",
  81. "fields": [
  82. {"name": "a", "type": "long"},
  83. {"name": "b", "type": "float"},
  84. {"name": "c", "type": "string"}
  85. ]
  86. }
  87. $ kafka-create-topic topic=data
  88. $ kafka-ingest format=avro topic=data schema=${schema} timestamp=1
  89. {"a": 1, "b": 2, "c": "a"}
  90. > CREATE CONNECTION kafka_conn
  91. TO KAFKA (BROKER '${testdrive.kafka-addr}', SECURITY PROTOCOL PLAINTEXT);
  92. > CREATE SOURCE source_data
  93. IN CLUSTER ${arg.single-replica-cluster}
  94. FROM KAFKA CONNECTION kafka_conn (TOPIC 'testdrive-data-${testdrive.seed}')
  95. > CREATE TABLE source_data_tbl FROM SOURCE source_data (REFERENCE "testdrive-data-${testdrive.seed}")
  96. FORMAT AVRO USING SCHEMA '${schema}'
  97. > CREATE MATERIALIZED VIEW source_data_mat_view AS
  98. SELECT * FROM source_data_tbl;
  99. ! UPDATE source_data_tbl SET i = 3;
  100. contains:cannot mutate non-writeable table 'materialize.public.source_data_tbl'
  101. ! UPDATE t SET i = i + 1 WHERE i IN (SELECT a FROM source_data_mat_view)
  102. contains:invalid selection
  103. # Verify that multiple inserts can be run in a transaction.
  104. > BEGIN
  105. > INSERT INTO t DEFAULT VALUES;
  106. > INSERT INTO t DEFAULT VALUES;
  107. > COMMIT
  108. > SELECT * FROM t ORDER BY i
  109. 5 6 xy
  110. <null> <null> <null>
  111. <null> <null> <null>
  112. # Multiple connections
  113. > CREATE TABLE c (a int);
  114. > INSERT INTO c VALUES (1);
  115. $ postgres-connect name=txn url=postgres://materialize:materialize@${testdrive.materialize-sql-addr}
  116. # In-flight txns don't affect updates/deletes, and vice versa
  117. $ postgres-execute connection=txn
  118. BEGIN;
  119. INSERT INTO c VALUES (1);
  120. > UPDATE c SET a = 3 WHERE a < 3;
  121. > SELECT a FROM c
  122. 3
  123. $ postgres-execute connection=txn
  124. INSERT INTO c VALUES (2);
  125. COMMIT;
  126. > SELECT a FROM c
  127. 1
  128. 2
  129. 3
  130. > BEGIN;
  131. > SELECT a FROM c
  132. 1
  133. 2
  134. 3
  135. $ postgres-execute connection=txn
  136. DELETE FROM c WHERE a < 3;
  137. > SELECT a FROM c
  138. 1
  139. 2
  140. 3
  141. > COMMIT;
  142. > SELECT a FROM c;
  143. 3
  144. # DELETE USING
  145. > CREATE TABLE t1 (a int, b int);
  146. > CREATE TABLE t2 (a int, b int);
  147. > CREATE TABLE t3 (x int, y int);
  148. # This test should exercise at least one case where a cross-join of t1, t3
  149. # produce more rows than t3 wants to DELETE.
  150. > INSERT INTO t1 VALUES
  151. (5, 10), (5, 15), (15, 20), (100, 100), (200, 1000);
  152. > INSERT INTO t2 VALUES
  153. (200, 2000), (200, 2000);
  154. > INSERT INTO t3 VALUES
  155. (5, 20), (6, 7), (7, 8), (500, 100);
  156. > DELETE FROM t3 USING t1 table1 WHERE t3.x = table1.a;
  157. > SELECT * FROM t3 ORDER BY x;
  158. 6 7
  159. 7 8
  160. 500 100
  161. > DELETE FROM t3 USING t1 JOIN t2 USING (a) WHERE t3.x > t1.a;
  162. > SELECT * FROM t3 ORDER BY x;
  163. 6 7
  164. 7 8
  165. > DELETE FROM t3 USING t3 t3_other WHERE t3.x = t3_other.y;
  166. > SELECT * FROM t3 ORDER BY x;
  167. 6 7
  168. # Ensure that `USING` empty tables does not delete any rows
  169. > DELETE FROM t1;
  170. > SELECT * FROM t1;
  171. > DELETE FROM t3 USING t1;
  172. > SELECT * FROM t3 ORDER BY x;
  173. 6 7
  174. > DELETE FROM t3 USING t1, t2;
  175. > SELECT * FROM t3 ORDER BY x;
  176. 6 7
  177. > DELETE FROM t3 USING t2;
  178. > SELECT * FROM t3 ORDER BY x;
  179. # Lateral subqueries cannot access `FROM` table
  180. ! DELETE FROM t3 USING lateral (SELECT t3.x FROM t1) t4 WHERE true;
  181. contains:column "t3.x" does not exist
  182. # ...unless it's explicitly brought into scope
  183. > DELETE FROM t3 USING lateral (SELECT t3.x FROM t3) t4 WHERE true;
  184. # Test different arities
  185. > CREATE TABLE t4 (a int, b int, c int);
  186. > CREATE TABLE t5 (a int);
  187. > CREATE TABLE t6 (a int, b int);
  188. > INSERT INTO t4 VALUES
  189. (6, 0, 0), (7, 0, 0), (10, 0, 0), (11, 0, 0);
  190. > INSERT INTO t5 VALUES
  191. (8), (9), (10), (12);
  192. > INSERT INTO t6 VALUES
  193. (0, 6), (0, 7), (0, 8), (0, 9);
  194. > DELETE FROM t6 USING t4 WHERE t4.a + t4.b + t4.c = t6.a + t6.b;
  195. > DELETE FROM t4 USING t6 WHERE t4.a + t4.b + t4.c = t6.a + t6.b;
  196. > DELETE FROM t6 USING t5 WHERE t5.a = t6.a + t6.b;
  197. > DELETE FROM t5 USING t6 WHERE t5.a = t6.a + t6.b;
  198. > DELETE FROM t4 USING t5 WHERE t4.a + t4.b + t4.c = t5.a;
  199. > DELETE FROM t5 USING t4 WHERE t4.a + t4.b + t4.c + 1 = t5.a;
  200. > SELECT * FROM t4 ORDER BY a;
  201. 6 0 0
  202. 7 0 0
  203. 11 0 0
  204. > SELECT * FROM t5 ORDER BY a;
  205. 9
  206. 10
  207. > SELECT * FROM t6 ORDER BY a;