rename_column.slt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # Copyright 2015 - 2019 The Cockroach Authors. All rights reserved.
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # This file is derived from the logic test suite in CockroachDB. The
  12. # original file was retrieved on June 10, 2019 from:
  13. #
  14. # https://github.com/cockroachdb/cockroach/blob/d2f7fbf5dd1fc1a099bbad790a2e1f7c60a66cc3/pkg/sql/logictest/testdata/logic_test/rename_column
  15. #
  16. # The original source code is subject to the terms of the Apache
  17. # 2.0 license, a copy of which can be found in the LICENSE file at the
  18. # root of this repository.
  19. # not supported yet
  20. halt
  21. mode cockroach
  22. statement ok
  23. CREATE TABLE users (
  24. uid INT PRIMARY KEY,
  25. name VARCHAR NOT NULL,
  26. title VARCHAR,
  27. INDEX foo (name) STORING (title),
  28. UNIQUE INDEX bar (uid, name)
  29. )
  30. statement ok
  31. INSERT INTO users VALUES (1, 'tom', 'cat'),(2, 'jerry', 'rat')
  32. query ITT colnames,rowsort
  33. SELECT * FROM users
  34. ----
  35. uid name title
  36. 1 tom cat
  37. 2 jerry rat
  38. statement error column name "name" already exists
  39. ALTER TABLE users RENAME COLUMN title TO name
  40. statement error empty column name
  41. ALTER TABLE users RENAME COLUMN title TO ""
  42. statement error pgcode 42703 column "ttle" does not exist
  43. ALTER TABLE users RENAME COLUMN ttle TO species
  44. statement error pgcode 42P01 relation "uses" does not exist
  45. ALTER TABLE uses RENAME COLUMN title TO species
  46. statement ok
  47. ALTER TABLE IF EXISTS uses RENAME COLUMN title TO species
  48. statement ok
  49. ALTER TABLE users RENAME COLUMN uid TO id
  50. statement ok
  51. ALTER TABLE users RENAME COLUMN title TO species
  52. query ITT colnames,rowsort
  53. SELECT * FROM users
  54. ----
  55. id name species
  56. 1 tom cat
  57. 2 jerry rat
  58. user testuser
  59. statement error user testuser does not have CREATE privilege on relation users
  60. ALTER TABLE users RENAME COLUMN name TO username
  61. user root
  62. statement ok
  63. GRANT CREATE ON TABLE users TO testuser
  64. user testuser
  65. statement ok
  66. ALTER TABLE users RENAME COLUMN name TO username
  67. user root
  68. query ITT colnames,rowsort
  69. SELECT * FROM users
  70. ----
  71. id username species
  72. 1 tom cat
  73. 2 jerry rat
  74. # Renaming a column updates the column names in an index.
  75. query TTBITTBB colnames
  76. SHOW INDEXES ON users
  77. ----
  78. table_name index_name non_unique seq_in_index column_name direction storing implicit
  79. users primary false 1 id ASC false false
  80. users foo true 1 username ASC false false
  81. users foo true 2 species N/A true false
  82. users foo true 3 id ASC false true
  83. users bar false 1 id ASC false false
  84. users bar false 2 username ASC false false
  85. statement ok
  86. CREATE VIEW v1 AS SELECT id FROM users WHERE username = 'tom'
  87. statement error cannot rename column "id" because view "v1" depends on it
  88. ALTER TABLE users RENAME COLUMN id TO uid
  89. statement error cannot rename column "username" because view "v1" depends on it
  90. ALTER TABLE users RENAME COLUMN username TO name
  91. # TODO(knz): restore test after cockroach#17269 / cockroach#10083 is fixed.
  92. #statement ok
  93. #ALTER TABLE users RENAME COLUMN species TO title
  94. statement ok
  95. CREATE VIEW v2 AS SELECT id from users
  96. statement ok
  97. DROP VIEW v1
  98. statement error cannot rename column "id" because view "v2" depends on it
  99. ALTER TABLE users RENAME COLUMN id TO uid
  100. # TODO(knz): restore test after cockroach#17269 / cockroach#10083 is fixed.
  101. # statement ok
  102. # ALTER TABLE users RENAME COLUMN username TO name
  103. statement ok
  104. DROP VIEW v2
  105. query T
  106. SELECT column_name FROM [SHOW COLUMNS FROM users]
  107. ----
  108. id
  109. username
  110. species
  111. query TTT
  112. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR ALTER TABLE users RENAME COLUMN species TO woo
  113. ----
  114. alter table · ·
  115. # Verify that EXPLAIN did not actually rename the column
  116. query T
  117. SELECT column_name FROM [SHOW COLUMNS FROM users]
  118. ----
  119. id
  120. username
  121. species
  122. # Check that a column can be added and renamed in the same statement
  123. statement ok
  124. ALTER TABLE users RENAME COLUMN species TO species_old,
  125. ADD COLUMN species STRING AS (species_old || ' woo') STORED
  126. query T rowsort
  127. SELECT species FROM users
  128. ----
  129. cat woo
  130. rat woo