createdrop.td 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #
  11. # Test basic create and drop functionality
  12. $ set schema={
  13. "name": "row",
  14. "type": "record",
  15. "fields": [
  16. {"name": "X", "type": "long"},
  17. {"name": "Y", "type": "long"}
  18. ]
  19. }
  20. $ kafka-create-topic topic=data
  21. > CREATE CONNECTION kafka_conn
  22. TO KAFKA (BROKER '${testdrive.kafka-addr}', SECURITY PROTOCOL PLAINTEXT);
  23. > CREATE SOURCE s
  24. IN CLUSTER ${arg.single-replica-cluster}
  25. FROM KAFKA CONNECTION kafka_conn (TOPIC 'testdrive-data-${testdrive.seed}')
  26. > CREATE TABLE s_tbl FROM SOURCE s (REFERENCE "testdrive-data-${testdrive.seed}")
  27. FORMAT AVRO USING SCHEMA '${schema}'
  28. $ kafka-ingest format=avro topic=data schema=${schema}
  29. {"X": 1, "Y": 1}
  30. {"X": 2, "Y": 1}
  31. {"X": 3, "Y": 1}
  32. {"X": 1, "Y": 2}
  33. > CREATE MATERIALIZED VIEW v AS SELECT 42 AS a
  34. > CREATE MATERIALIZED VIEW v2 AS SELECT 0 AS x
  35. > CREATE INDEX i ON v(a)
  36. > CREATE INDEX j on s_tbl("X");
  37. # Test that creating objects of the same name does not work
  38. ! CREATE MATERIALIZED VIEW i AS SELECT 1.5 AS c
  39. contains:index "materialize.public.i" already exists
  40. ! CREATE INDEX i ON s_tbl("Y")
  41. contains:index "materialize.public.i" already exists
  42. ! CREATE INDEX j on v2(x)
  43. contains:index "materialize.public.j" already exists
  44. ! CREATE INDEX v ON v2(x)
  45. contains:materialized view "materialize.public.v" already exists
  46. $ set dummy={
  47. "type": "record",
  48. "name": "envelope",
  49. "fields": [
  50. {
  51. "name": "before",
  52. "type": [
  53. {
  54. "name": "row",
  55. "type": "record",
  56. "fields": [
  57. {"name": "X",
  58. "type": {
  59. "type": "bytes",
  60. "scale": 3,
  61. "precision": 10,
  62. "logicalType": "decimal"
  63. }}
  64. ]
  65. },
  66. "null"
  67. ]
  68. },
  69. { "name": "after", "type": ["row", "null"] }
  70. ]
  71. }
  72. ! CREATE SOURCE v2
  73. IN CLUSTER ${arg.single-replica-cluster}
  74. FROM KAFKA CONNECTION kafka_conn (TOPIC 'testdrive-data-${testdrive.seed}')
  75. contains:materialized view "materialize.public.v2" already exists
  76. ! CREATE SOURCE i
  77. IN CLUSTER ${arg.single-replica-cluster}
  78. FROM KAFKA CONNECTION kafka_conn (TOPIC 'testdrive-data-${testdrive.seed}')
  79. contains:index "materialize.public.i" already exists
  80. ! CREATE INDEX s ON v2(x)
  81. contains:source "materialize.public.s" already exists
  82. ! CREATE MATERIALIZED VIEW s AS SELECT 'bloop' AS d
  83. contains:source "materialize.public.s" already exists
  84. # Test that objects do not get dropped if the drop command does not specify the correct type
  85. ! DROP SOURCE v
  86. exact:v is a materialized view not a source
  87. ! DROP SOURCE IF EXISTS v
  88. exact:v is a materialized view not a source
  89. ! DROP INDEX v
  90. exact:v is a materialized view not an index
  91. ! DROP INDEX IF EXISTS v
  92. exact:v is a materialized view not an index
  93. ! DROP SOURCE i
  94. exact:i is an index not a source
  95. ! DROP SOURCE IF EXISTS i
  96. exact:i is an index not a source
  97. ! DROP VIEW i
  98. exact:i is an index not a view
  99. ! DROP VIEW IF EXISTS i
  100. exact:i is an index not a view
  101. ! DROP INDEX s
  102. exact:s is a source not an index
  103. ! DROP INDEX IF EXISTS s
  104. exact:s is a source not an index
  105. ! DROP VIEW s
  106. exact:s is a source not a view
  107. ! DROP VIEW IF EXISTS s
  108. exact:s is a source not a view
  109. # Delete objects
  110. > DROP INDEX j
  111. > DROP INDEX i
  112. > DROP MATERIALIZED VIEW v
  113. > DROP SOURCE s CASCADE
  114. # test that the names of the deleted objects can be used to create objects of other types
  115. > CREATE MATERIALIZED VIEW s AS SELECT 84 AS b
  116. > CREATE SOURCE i
  117. IN CLUSTER ${arg.single-replica-cluster}
  118. FROM KAFKA CONNECTION kafka_conn (TOPIC 'testdrive-data-${testdrive.seed}')
  119. > CREATE TABLE i_tbl FROM SOURCE i (REFERENCE "testdrive-data-${testdrive.seed}")
  120. FORMAT AVRO USING SCHEMA '${schema}'
  121. > CREATE INDEX v ON s(b)
  122. # Test that if exists works when the referred objects exist
  123. > DROP INDEX IF EXISTS v
  124. > DROP MATERIALIZED VIEW IF EXISTS s
  125. > DROP SOURCE IF EXISTS i CASCADE
  126. # Test that if exists works when the referred objects do not exist
  127. > DROP INDEX IF EXISTS nonexistent
  128. > DROP VIEW IF EXISTS nonexistent
  129. > DROP SOURCE IF EXISTS nonexistent CASCADE
  130. # Test that drop without if exists does not work if the object does not exist
  131. ! DROP INDEX nonexistent
  132. contains:unknown catalog item 'nonexistent'
  133. ! DROP VIEW nonexistent
  134. contains:unknown catalog item 'nonexistent'
  135. ! DROP SOURCE nonexistent
  136. contains:unknown catalog item 'nonexistent'
  137. # Test CREATE VIEW IF NOT EXISTS
  138. > CREATE VIEW IF NOT EXISTS test1 AS SELECT 42 AS a
  139. > SHOW CREATE VIEW test1
  140. name create_sql
  141. --------------------------------------------------------------------------------------------------
  142. materialize.public.test1 "CREATE VIEW materialize.public.test1 AS SELECT 42 AS a;"
  143. ! CREATE VIEW test1 AS SELECT 43 AS b
  144. contains:view "materialize.public.test1" already exists
  145. > SELECT * FROM test1
  146. a
  147. -----------
  148. 42
  149. > CREATE VIEW IF NOT EXISTS test1 AS SELECT 42 AS a
  150. > DROP VIEW test1