@@ -100,8 +100,12 @@ abstract class DefaultCollationTestSuite extends QueryTest with SharedSparkSessi
100
100
sql(s " ALTER TABLE $testTable ALTER COLUMN c2 TYPE STRING COLLATE UNICODE " )
101
101
assertTableColumnCollation(testTable, " c2" , " UNICODE" )
102
102
103
+ // When using ALTER TABLE ALTER COLUMN TYPE, the column should inherit the table's collation
104
+ // only if it wasn't a string column before. If the column was already a string, and we're
105
+ // just changing its type to string (without explicit collation) again, keep the original
106
+ // collation.
103
107
sql(s " ALTER TABLE $testTable ALTER COLUMN c2 TYPE STRING " )
104
- assertTableColumnCollation(testTable, " c2" , " UTF8_BINARY " )
108
+ assertTableColumnCollation(testTable, " c2" , " UNICODE " )
105
109
}
106
110
}
107
111
@@ -148,9 +152,26 @@ abstract class DefaultCollationTestSuite extends QueryTest with SharedSparkSessi
148
152
sql(s " ALTER TABLE $testTable ALTER COLUMN c1 TYPE STRING COLLATE UNICODE_CI " )
149
153
assertTableColumnCollation(testTable, " c1" , " UNICODE_CI" )
150
154
155
+ // When using ALTER TABLE ALTER COLUMN TYPE, the column should inherit the table's collation
156
+ // only if it wasn't a string column before. If the column was already a string, and we're
157
+ // just changing its type to string (without explicit collation) again, keep the original
158
+ // collation.
159
+ sql(s " ALTER TABLE $testTable ALTER COLUMN c1 TYPE STRING " )
160
+ assertTableColumnCollation(testTable, " c1" , " UNICODE_CI" )
161
+
151
162
// alter table add columns with explicit collation, check collation for each column
152
163
sql(s " ALTER TABLE $testTable ADD COLUMN c7 STRING COLLATE SR_CI_AI " )
153
164
sql(s " ALTER TABLE $testTable ADD COLUMN c8 STRING COLLATE UTF8_BINARY " )
165
+ assertTableColumnCollation(testTable, " c7" , " SR_CI_AI" )
166
+ assertTableColumnCollation(testTable, " c8" , " UTF8_BINARY" )
167
+
168
+ // When using ALTER TABLE ALTER COLUMN TYPE, the column should inherit the table's collation
169
+ // only if it wasn't a string column before. If the column was already a string, and we're
170
+ // just changing its type to string (without explicit collation) again, keep the original
171
+ // collation.
172
+ sql(s " ALTER TABLE $testTable ALTER COLUMN c8 TYPE STRING " )
173
+ assertTableColumnCollation(testTable, " c8" , " UTF8_BINARY" )
174
+
154
175
assertTableColumnCollation(testTable, " c1" , " UNICODE_CI" )
155
176
assertTableColumnCollation(testTable, " c2" , " SR" )
156
177
assertTableColumnCollation(testTable, " c3" , " UTF8_BINARY" )
@@ -162,6 +183,24 @@ abstract class DefaultCollationTestSuite extends QueryTest with SharedSparkSessi
162
183
}
163
184
}
164
185
186
+ test(" Alter table alter column type with default collation" ) {
187
+ // When using ALTER TABLE ALTER COLUMN TYPE, the column should inherit the table's collation
188
+ // only if it wasn't a string column before. If the column was already a string, and we're
189
+ // just changing its type to string (without explicit collation) again, keep the original
190
+ // collation.
191
+ withTable(testTable) {
192
+ sql(s " CREATE TABLE $testTable (c1 STRING, c2 STRING COLLATE UTF8_LCASE, c3 STRING) " +
193
+ s " DEFAULT COLLATION UTF8_LCASE " )
194
+ sql(s " ALTER TABLE $testTable ALTER COLUMN c1 TYPE STRING " )
195
+ sql(s " ALTER TABLE $testTable ALTER COLUMN c2 TYPE STRING " )
196
+ sql(s " ALTER TABLE $testTable ALTER COLUMN c3 TYPE STRING COLLATE UNICODE " )
197
+
198
+ assertTableColumnCollation(testTable, " c1" , " UTF8_LCASE" )
199
+ assertTableColumnCollation(testTable, " c2" , " UTF8_LCASE" )
200
+ assertTableColumnCollation(testTable, " c3" , " UNICODE" )
201
+ }
202
+ }
203
+
165
204
schemaAndObjectCollationPairs.foreach {
166
205
case (schemaDefaultCollation, tableDefaultCollation) =>
167
206
test(
@@ -221,7 +260,7 @@ abstract class DefaultCollationTestSuite extends QueryTest with SharedSparkSessi
221
260
sql(s " ALTER SCHEMA $testSchema DEFAULT COLLATION $schemaNewCollation" )
222
261
223
262
// Altering schema default collation should not affect existing objects.
224
- addAndAlterColumns(tableDefaultCollation = tableDefaultCollation)
263
+ addAndAlterColumns(c2Collation = " SR_AI " , tableDefaultCollation = tableDefaultCollation)
225
264
}
226
265
227
266
withTable(testTable) {
@@ -413,12 +452,12 @@ abstract class DefaultCollationTestSuite extends QueryTest with SharedSparkSessi
413
452
sql(s " CREATE TABLE $testTable (c1 STRING, c2 STRING COLLATE SR_AI) " +
414
453
s " $tableDefaultCollationClause" )
415
454
416
- addAndAlterColumns(tableDefaultCollation = resolvedDefaultCollation)
455
+ addAndAlterColumns(c2Collation = " SR_AI " , tableDefaultCollation = resolvedDefaultCollation)
417
456
}
418
457
}
419
458
}
420
459
421
- private def addAndAlterColumns (tableDefaultCollation : String ): Unit = {
460
+ private def addAndAlterColumns (c2Collation : String , tableDefaultCollation : String ): Unit = {
422
461
// ADD COLUMN
423
462
sql(s " ALTER TABLE $testTable ADD COLUMN c3 STRING " )
424
463
sql(s " ALTER TABLE $testTable ADD COLUMN c4 STRING COLLATE SR_AI " )
@@ -432,7 +471,7 @@ abstract class DefaultCollationTestSuite extends QueryTest with SharedSparkSessi
432
471
sql(s " ALTER TABLE $testTable ALTER COLUMN c2 TYPE STRING " )
433
472
sql(s " ALTER TABLE $testTable ALTER COLUMN c3 TYPE STRING COLLATE UTF8_BINARY " )
434
473
assertTableColumnCollation(testTable, " c1" , " UNICODE" )
435
- assertTableColumnCollation(testTable, " c2" , tableDefaultCollation )
474
+ assertTableColumnCollation(testTable, " c2" , c2Collation )
436
475
assertTableColumnCollation(testTable, " c3" , " UTF8_BINARY" )
437
476
}
438
477
}
@@ -806,6 +845,28 @@ class DefaultCollationTestSuiteV2 extends DefaultCollationTestSuite with Datasou
806
845
}
807
846
}
808
847
848
+ test(" alter char/varchar column to string type" ) {
849
+ withTable(testTable) {
850
+ sql(s " CREATE TABLE $testTable (c1 VARCHAR(10), c2 CHAR(10)) " +
851
+ s " DEFAULT COLLATION UTF8_LCASE " )
852
+
853
+ sql(s " ALTER TABLE $testTable ALTER COLUMN c1 TYPE STRING " )
854
+ sql(s " ALTER TABLE $testTable ALTER COLUMN c2 TYPE STRING " )
855
+ assertTableColumnCollation(testTable, " c1" , " UTF8_LCASE" )
856
+ assertTableColumnCollation(testTable, " c2" , " UTF8_LCASE" )
857
+ }
858
+
859
+ withTable(testTable) {
860
+ sql(s " CREATE TABLE $testTable (c1 VARCHAR(10), c2 CHAR(10)) " +
861
+ s " DEFAULT COLLATION UTF8_LCASE " )
862
+
863
+ sql(s " ALTER TABLE $testTable ALTER COLUMN c1 TYPE STRING COLLATE UNICODE " )
864
+ sql(s " ALTER TABLE $testTable ALTER COLUMN c2 TYPE STRING COLLATE UNICODE " )
865
+ assertTableColumnCollation(testTable, " c1" , " UNICODE" )
866
+ assertTableColumnCollation(testTable, " c2" , " UNICODE" )
867
+ }
868
+ }
869
+
809
870
private def testReplaceColumns (
810
871
schemaDefaultCollation : String , tableDefaultCollation : Option [String ] = None ): Unit = {
811
872
val (tableDefaultCollationClause, resolvedDefaultCollation) =
0 commit comments