insert-select.td 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 INSERT INTO...SELECT. This must be a testdrive test to avoid symbiosis
  11. # in sqllogictest.
  12. > CREATE TABLE t (i INT, f REAL, 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. > CREATE TABLE u (i INT, f REAL, t TEXT);
  18. > INSERT INTO u VALUES (5, 6, 'c');
  19. > INSERT INTO t SELECT * FROM u;
  20. # Assignment casts are valid
  21. > CREATE TABLE bigger (i INT8, f FLOAT, t TEXT);
  22. > INSERT INTO bigger VALUES (7, 8, 'd');
  23. > INSERT INTO t SELECT * FROM bigger;
  24. # Obliquely go through SELECT * FROM ( VALUES ... )
  25. > INSERT INTO t SELECT * FROM (
  26. VALUES (9.1::numeric, 10, 'e')
  27. );
  28. ! INSERT INTO t SELECT * FROM (
  29. VALUES ('11', '12', 'f')
  30. );
  31. contains:column "i" is of type integer but expression is of type text
  32. > BEGIN
  33. > INSERT INTO t VALUES (11, 12, 'f')
  34. ! INSERT INTO t SELECT * FROM t;
  35. contains:cannot be run inside a transaction block
  36. > COMMIT
  37. > SELECT * FROM t ORDER BY i
  38. 1 2 a
  39. 3 4 b
  40. 5 6 c
  41. 7 8 d
  42. 9 10 e
  43. > BEGIN
  44. > INSERT INTO t SELECT * FROM (
  45. VALUES (17, 18, 'i')
  46. );
  47. > COMMIT
  48. > CREATE MATERIALIZED VIEW v (a, b, c) AS SELECT 11, 12::real, 'f';
  49. > INSERT INTO t (i, f, t) SELECT a, b, c FROM v;
  50. # Table check descends into select targets
  51. > INSERT INTO t (i, f, t) SELECT column1, column2, column3
  52. FROM ( VALUES (13, 14, 'g') )
  53. LEFT JOIN (
  54. SELECT a, b, c FROM v
  55. ) AS y
  56. ON y.a = column1
  57. $ set schema={
  58. "type": "record",
  59. "name": "row",
  60. "fields": [
  61. {"name": "a", "type": "long"},
  62. {"name": "b", "type": "float"},
  63. {"name": "c", "type": "string"}
  64. ]
  65. }
  66. $ kafka-create-topic topic=data
  67. $ kafka-ingest format=avro topic=data schema=${schema} timestamp=1
  68. {"a": 15, "b": 16, "c": "h"}
  69. > CREATE CONNECTION kafka_conn
  70. TO KAFKA (BROKER '${testdrive.kafka-addr}', SECURITY PROTOCOL PLAINTEXT);
  71. > CREATE SOURCE source_data
  72. IN CLUSTER ${arg.single-replica-cluster}
  73. FROM KAFKA CONNECTION kafka_conn (TOPIC 'testdrive-data-${testdrive.seed}')
  74. > CREATE TABLE source_data_tbl
  75. FROM SOURCE source_data (REFERENCE "testdrive-data-${testdrive.seed}")
  76. FORMAT AVRO USING SCHEMA '${schema}'
  77. ! INSERT INTO source_data_tbl VALUES (100, 200, 'x');
  78. contains:cannot insert into non-writeable table 'materialize.public.source_data_tbl'
  79. > CREATE MATERIALIZED VIEW source_data_mat_view AS
  80. SELECT * FROM source_data_tbl;
  81. ! INSERT INTO t SELECT * FROM source_data_mat_view;
  82. contains:invalid selection
  83. > SELECT * FROM t ORDER BY i
  84. 1 2 a
  85. 3 4 b
  86. 5 6 c
  87. 7 8 d
  88. 9 10 e
  89. 11 12 f
  90. 13 14 g
  91. 17 18 i
  92. # Multiple connections
  93. > CREATE TABLE c (a int);
  94. > INSERT INTO c VALUES (1);
  95. > CREATE TABLE s (a int);
  96. $ postgres-connect name=writer url=postgres://materialize:materialize@${testdrive.materialize-sql-addr}
  97. # In-flight txns don't affect updates/deletes, and vice versa
  98. $ postgres-execute connection=writer
  99. BEGIN;
  100. INSERT INTO s VALUES (2);
  101. > INSERT INTO c SELECT * FROM s;
  102. > SELECT a FROM c
  103. 1
  104. $ postgres-execute connection=writer
  105. INSERT INTO s VALUES (3);
  106. COMMIT;
  107. > INSERT INTO c SELECT * FROM s;
  108. > SELECT a FROM c
  109. 1
  110. 2
  111. 3
  112. > BEGIN;
  113. > SELECT a FROM c
  114. 1
  115. 2
  116. 3
  117. $ postgres-execute connection=writer
  118. INSERT INTO c SELECT * FROM s;
  119. > SELECT a FROM c
  120. 1
  121. 2
  122. 3
  123. > COMMIT;
  124. # Every value from s should be duplicated in c
  125. > SELECT a FROM s
  126. 2
  127. 3
  128. > SELECT a FROM c;
  129. 1
  130. 2
  131. 2
  132. 3
  133. 3
  134. # Test DELETE and INSERT INTO SELECT in Serializable mode.
  135. > SET TRANSACTION_ISOLATION TO 'SERIALIZABLE';
  136. > DELETE FROM t
  137. > SELECT * FROM t
  138. > INSERT INTO t (i, f, t) SELECT a, b, c FROM v;
  139. > SELECT * FROM t
  140. 11 12 f
  141. > SET TRANSACTION_ISOLATION TO 'STRICT SERIALIZABLE';