@@ -377,41 +377,41 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils {
377
377
}
378
378
}
379
379
380
- test(" CTAS a managed table with the existing empty directory" ) {
381
- val tableLoc = new File (spark.sessionState.catalog.defaultTablePath(TableIdentifier (" tab1" )))
380
+ private def withEmptyDirInTablePath (dirName : String )(f : File => Unit ): Unit = {
381
+ val tableLoc =
382
+ new File (spark.sessionState.catalog.defaultTablePath(TableIdentifier (dirName)))
382
383
try {
383
384
tableLoc.mkdir()
385
+ f(tableLoc)
386
+ } finally {
387
+ waitForTasksToFinish()
388
+ Utils .deleteRecursively(tableLoc)
389
+ }
390
+ }
391
+
392
+
393
+ test(" CTAS a managed table with the existing empty directory" ) {
394
+ withEmptyDirInTablePath(" tab1" ) { tableLoc =>
384
395
withTable(" tab1" ) {
385
396
sql(s " CREATE TABLE tab1 USING ${dataSource} AS SELECT 1, 'a' " )
386
397
checkAnswer(spark.table(" tab1" ), Row (1 , " a" ))
387
398
}
388
- } finally {
389
- waitForTasksToFinish()
390
- Utils .deleteRecursively(tableLoc)
391
399
}
392
400
}
393
401
394
402
test(" create a managed table with the existing empty directory" ) {
395
- val tableLoc = new File (spark.sessionState.catalog.defaultTablePath(TableIdentifier (" tab1" )))
396
- try {
397
- tableLoc.mkdir()
403
+ withEmptyDirInTablePath(" tab1" ) { tableLoc =>
398
404
withTable(" tab1" ) {
399
405
sql(s " CREATE TABLE tab1 (col1 int, col2 string) USING ${dataSource}" )
400
406
sql(" INSERT INTO tab1 VALUES (1, 'a')" )
401
407
checkAnswer(spark.table(" tab1" ), Row (1 , " a" ))
402
408
}
403
- } finally {
404
- waitForTasksToFinish()
405
- Utils .deleteRecursively(tableLoc)
406
409
}
407
410
}
408
411
409
412
test(" create a managed table with the existing non-empty directory" ) {
410
413
withTable(" tab1" ) {
411
- val tableLoc = new File (spark.sessionState.catalog.defaultTablePath(TableIdentifier (" tab1" )))
412
- try {
413
- // create an empty hidden file
414
- tableLoc.mkdir()
414
+ withEmptyDirInTablePath(" tab1" ) { tableLoc =>
415
415
val hiddenGarbageFile = new File (tableLoc.getCanonicalPath, " .garbage" )
416
416
hiddenGarbageFile.createNewFile()
417
417
val exMsg = " Can not create the managed table('`tab1`'). The associated location"
@@ -439,28 +439,20 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils {
439
439
}.getMessage
440
440
assert(ex.contains(exMsgWithDefaultDB))
441
441
}
442
- } finally {
443
- waitForTasksToFinish()
444
- Utils .deleteRecursively(tableLoc)
445
442
}
446
443
}
447
444
}
448
445
449
446
test(" rename a managed table with existing empty directory" ) {
450
- val tableLoc = new File (spark.sessionState.catalog.defaultTablePath(TableIdentifier (" tab2" )))
451
- try {
447
+ withEmptyDirInTablePath(" tab2" ) { tableLoc =>
452
448
withTable(" tab1" ) {
453
449
sql(s " CREATE TABLE tab1 USING $dataSource AS SELECT 1, 'a' " )
454
- tableLoc.mkdir()
455
450
val ex = intercept[AnalysisException ] {
456
451
sql(" ALTER TABLE tab1 RENAME TO tab2" )
457
452
}.getMessage
458
453
val expectedMsg = " Can not rename the managed table('`tab1`'). The associated location"
459
454
assert(ex.contains(expectedMsg))
460
455
}
461
- } finally {
462
- waitForTasksToFinish()
463
- Utils .deleteRecursively(tableLoc)
464
456
}
465
457
}
466
458
0 commit comments