vinodkc commented on code in PR #32411:
URL: https://github.com/apache/spark/pull/32411#discussion_r1452681835


##########
sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala:
##########
@@ -598,6 +598,37 @@ abstract class SQLQuerySuiteBase extends QueryTest with 
SQLTestUtils with TestHi
     }
   }
 
+  test("SPARK-28551: CTAS Hive Table should be with non-existent or empty 
location") {
+    def executeCTASWithNonEmptyLocation(tempLocation: String) {
+      sql(s"CREATE TABLE ctas1(id string) stored as rcfile LOCATION 
'$tempLocation/ctas1'")
+      sql("INSERT INTO TABLE ctas1 SELECT 'A' ")
+      sql(s"""CREATE TABLE ctas_with_existing_location stored as rcfile 
LOCATION
+           |'$tempLocation' AS SELECT key k, value FROM src ORDER BY k, 
value""".stripMargin)
+    }
+
+    Seq("false", "true").foreach { convertCTASFlag =>
+      Seq("false", "true").foreach { allowNonEmptyDirFlag =>
+        withSQLConf(
+          SQLConf.CONVERT_CTAS.key -> convertCTASFlag,
+          SQLConf.ALLOW_NON_EMPTY_LOCATION_IN_CTAS.key -> 
allowNonEmptyDirFlag) {
+          withTempDir { dir =>
+            val tempLocation = dir.toURI.toString
+            withTable("ctas1", "ctas_with_existing_location") {
+              if (!spark.conf.get(SQLConf.ALLOW_NON_EMPTY_LOCATION_IN_CTAS)) {
+                intercept[AnalysisException] {

Review Comment:
   @PengleiShi , 
   I did a test on the master branch
   ```
   sql("set spark.sql.legacy.createHiveTableByDefault=true")
   val tempLocation="/tmp/ctastest100"
   sql(s"CREATE TABLE ctas1_h(id string) LOCATION '$tempLocation/ctas1'")
   sql("INSERT INTO TABLE ctas1_h SELECT 'A' ")
   sql(s"""CREATE TABLE ctas1_h_with_existing_location LOCATION '$tempLocation' 
AS SELECT 'B'""")
   //Throws org.apache.spark.sql.AnalysisException: CREATE-TABLE-AS-SELECT 
cannot create table with location to a non-empty directory file...
   
   sql("set spark.sql.legacy.allowNonEmptyLocationInCTAS=TRUE")
   sql(s"""CREATE TABLE ctas1_h_with_existing_location LOCATION '$tempLocation' 
AS SELECT 'B'""".stripMargin)
   // Above statement created table
   ```
   Could please use this test to describe the expected behavior?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to