insert-select.td 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. FORMAT AVRO USING SCHEMA '${schema}'
  75. > CREATE MATERIALIZED VIEW source_data_mat_view AS
  76. SELECT * FROM source_data;
  77. ! INSERT INTO t SELECT * FROM source_data_mat_view;
  78. contains:invalid selection
  79. > SELECT * FROM t ORDER BY i
  80. 1 2 a
  81. 3 4 b
  82. 5 6 c
  83. 7 8 d
  84. 9 10 e
  85. 11 12 f
  86. 13 14 g
  87. 17 18 i
  88. # Multiple connections
  89. > CREATE TABLE c (a int);
  90. > INSERT INTO c VALUES (1);
  91. > CREATE TABLE s (a int);
  92. $ postgres-connect name=writer url=postgres://materialize:materialize@${testdrive.materialize-sql-addr}
  93. # In-flight txns don't affect updates/deletes, and vice versa
  94. $ postgres-execute connection=writer
  95. BEGIN;
  96. INSERT INTO s VALUES (2);
  97. > INSERT INTO c SELECT * FROM s;
  98. > SELECT a FROM c
  99. 1
  100. $ postgres-execute connection=writer
  101. INSERT INTO s VALUES (3);
  102. COMMIT;
  103. > INSERT INTO c SELECT * FROM s;
  104. > SELECT a FROM c
  105. 1
  106. 2
  107. 3
  108. > BEGIN;
  109. > SELECT a FROM c
  110. 1
  111. 2
  112. 3
  113. $ postgres-execute connection=writer
  114. INSERT INTO c SELECT * FROM s;
  115. > SELECT a FROM c
  116. 1
  117. 2
  118. 3
  119. > COMMIT;
  120. # Every value from s should be duplicated in c
  121. > SELECT a FROM s
  122. 2
  123. 3
  124. > SELECT a FROM c;
  125. 1
  126. 2
  127. 2
  128. 3
  129. 3
  130. # Test DELETE and INSERT INTO SELECT in Serializable mode.
  131. > SET TRANSACTION_ISOLATION TO 'SERIALIZABLE';
  132. > DELETE FROM t
  133. > SELECT * FROM t
  134. > INSERT INTO t (i, f, t) SELECT a, b, c FROM v;
  135. > SELECT * FROM t
  136. 11 12 f
  137. > SET TRANSACTION_ISOLATION TO 'STRICT SERIALIZABLE';