@@ -398,6 +398,66 @@ trait ClusteredTableCreateOrReplaceDDLSuiteBase
398
398
}
399
399
}
400
400
401
+ test(" Replace clustered table with non-clustered table" ) {
402
+ import testImplicits ._
403
+ withTable(sourceTable) {
404
+ sql(s " CREATE TABLE $sourceTable(i int, s string) USING delta " )
405
+ spark.range(1000 )
406
+ .map(i => (i.intValue(), " string col" ))
407
+ .toDF(" i" , " s" )
408
+ .write
409
+ .format(" delta" )
410
+ .mode(" append" )
411
+ .saveAsTable(sourceTable)
412
+
413
+ // Validate REPLACE TABLE (AS SELECT).
414
+ Seq (" REPLACE" , " CREATE OR REPLACE" ).foreach { clause =>
415
+ withClusteredTable(testTable, " a int" , " a" ) {
416
+ verifyClusteringColumns(TableIdentifier (testTable), " a" )
417
+
418
+ Seq (true , false ).foreach { isRTAS =>
419
+ val testQuery = if (isRTAS) {
420
+ s " $clause TABLE $testTable USING delta AS SELECT * FROM $sourceTable"
421
+ } else {
422
+ sql(s " $clause TABLE $testTable (i int, s string) USING delta " )
423
+ s " INSERT INTO $testTable SELECT * FROM $sourceTable"
424
+ }
425
+ sql(testQuery)
426
+ // Note that clustering table feature are still retained after REPLACE TABLE.
427
+ verifyClusteringColumns(TableIdentifier (testTable), " " )
428
+ }
429
+ }
430
+ }
431
+ }
432
+ }
433
+
434
+ test(" Replace clustered table with non-clustered table - dataframe writer" ) {
435
+ import testImplicits ._
436
+ withTable(sourceTable) {
437
+ sql(s " CREATE TABLE $sourceTable(i int, s string) USING delta " )
438
+ spark.range(1000 )
439
+ .map(i => (i.intValue(), " string col" ))
440
+ .toDF(" i" , " s" )
441
+ .write
442
+ .format(" delta" )
443
+ .mode(" append" )
444
+ .saveAsTable(sourceTable)
445
+
446
+ withClusteredTable(testTable, " a int" , " a" ) {
447
+ verifyClusteringColumns(TableIdentifier (testTable), " a" )
448
+
449
+ spark.table(sourceTable)
450
+ .write
451
+ .format(" delta" )
452
+ .mode(" overwrite" )
453
+ .option(" overwriteSchema" , " true" )
454
+ .saveAsTable(testTable)
455
+ // Note that clustering table feature are still retained after REPLACE TABLE.
456
+ verifyClusteringColumns(TableIdentifier (testTable), " " )
457
+ }
458
+ }
459
+ }
460
+
401
461
protected def withTempDirIfNecessary (f : Option [String ] => Unit ): Unit = {
402
462
if (isPathBased) {
403
463
withTempDir { dir =>
0 commit comments