MaxGekk commented on a change in pull request #30935:
URL: https://github.com/apache/spark/pull/30935#discussion_r549268524



##########
File path: 
sql/core/src/test/scala/org/apache/spark/sql/execution/command/AlterTableRenamePartitionSuiteBase.scala
##########
@@ -17,8 +17,149 @@
 
 package org.apache.spark.sql.execution.command
 
-import org.apache.spark.sql.QueryTest
+import org.apache.spark.sql.{AnalysisException, QueryTest, Row}
+import org.apache.spark.sql.catalyst.analysis.{NoSuchPartitionException, 
PartitionAlreadyExistsException}
+import org.apache.spark.sql.internal.SQLConf
 
 trait AlterTableRenamePartitionSuiteBase extends QueryTest with 
DDLCommandTestUtils {
   override val command = "ALTER TABLE .. RENAME PARTITION"
+
+  protected def createSinglePartTable(t: String): Unit = {
+    sql(s"CREATE TABLE $t (id bigint, data string) $defaultUsing PARTITIONED 
BY (id)")
+    sql(s"INSERT INTO $t PARTITION (id = 1) SELECT 'abc'")
+  }
+
+  test("rename without explicitly specifying database") {
+    withNamespaceAndTable("ns", "tbl") { t =>
+      createSinglePartTable(t)
+      checkPartitions(t, Map("id" -> "1"))
+
+      sql(s"ALTER TABLE $t PARTITION (id = 1) RENAME TO PARTITION (id = 2)")
+      checkPartitions(t, Map("id" -> "2"))
+      checkAnswer(sql(s"SELECT id, data FROM $t"), Row(2, "abc"))
+    }
+  }
+
+  test("table to alter does not exist") {
+    withNamespace(s"$catalog.ns") {
+      sql(s"CREATE NAMESPACE $catalog.ns")
+      val errMsg = intercept[AnalysisException] {
+        sql(s"ALTER TABLE $catalog.ns.no_tbl PARTITION (id=1) RENAME TO 
PARTITION (id=2)")
+      }.getMessage
+      assert(errMsg.contains("Table not found"))
+    }
+  }
+
+  test("partition to rename does not exist") {
+    withNamespaceAndTable("ns", "tbl") { t =>
+      createSinglePartTable(t)
+      checkPartitions(t, Map("id" -> "1"))
+      val errMsg = intercept[NoSuchPartitionException] {
+        sql(s"ALTER TABLE $t PARTITION (id = 3) RENAME TO PARTITION (id = 2)")
+      }.getMessage
+      assert(errMsg.contains("Partition not found in table"))
+    }
+  }
+
+  test("target partition exists") {
+    withNamespaceAndTable("ns", "tbl") { t =>
+      createSinglePartTable(t)
+      sql(s"INSERT INTO $t PARTITION (id = 2) SELECT 'def'")
+      checkPartitions(t, Map("id" -> "1"), Map("id" -> "2"))
+      val errMsg = intercept[PartitionAlreadyExistsException] {
+        sql(s"ALTER TABLE $t PARTITION (id = 1) RENAME TO PARTITION (id = 2)")
+      }.getMessage
+      assert(errMsg.contains("Partition already exists"))
+    }
+  }
+
+  test("single part partition") {
+    withNamespaceAndTable("ns", "tbl") { t =>
+      createSinglePartTable(t)
+      checkPartitions(t, Map("id" -> "1"))
+
+      sql(s"ALTER TABLE $t PARTITION (id = 1) RENAME TO PARTITION (id = 2)")
+      checkPartitions(t, Map("id" -> "2"))
+      checkAnswer(sql(s"SELECT id, data FROM $t"), Row(2, "abc"))
+    }
+  }
+
+  test("multi part partition") {
+    withNamespaceAndTable("ns", "tbl") { t =>
+      createWideTable(t)
+      checkPartitions(t,
+        Map(
+          "year" -> "2016",
+          "month" -> "3",
+          "hour" -> "10",
+          "minute" -> "10",
+          "sec" -> "10",
+          "extra" -> "1"),
+        Map(
+          "year" -> "2016",
+          "month" -> "4",
+          "hour" -> "10",
+          "minute" -> "10",
+          "sec" -> "10",
+          "extra" -> "1"))
+
+      sql(s"""
+        |ALTER TABLE $t
+        |PARTITION (
+        |  year = 2016, month = 3, hour = 10, minute = 10, sec = 10, extra = 1
+        |) RENAME TO PARTITION (
+        |  year = 2016, month = 3, hour = 10, minute = 10, sec = 123, extra = 1
+        |)""".stripMargin)
+      checkPartitions(t,
+        Map(
+          "year" -> "2016",
+          "month" -> "3",
+          "hour" -> "10",
+          "minute" -> "10",
+          "sec" -> "123",
+          "extra" -> "1"),
+        Map(
+          "year" -> "2016",
+          "month" -> "4",
+          "hour" -> "10",
+          "minute" -> "10",
+          "sec" -> "10",
+          "extra" -> "1"))
+      checkAnswer(sql(s"SELECT month, sec, price FROM $t"), Row(3, 123, 3))
+    }
+  }
+
+  test("with location") {
+    withNamespaceAndTable("ns", "tbl") { t =>
+      createSinglePartTable(t)
+      sql(s"ALTER TABLE $t ADD PARTITION (id = 2) LOCATION 'loc1'")
+      sql(s"INSERT INTO $t PARTITION (id = 2) SELECT 'def'")
+      checkPartitions(t, Map("id" -> "1"), Map("id" -> "2"))
+
+      sql(s"ALTER TABLE $t PARTITION (id = 2) RENAME TO PARTITION (id = 3)")

Review comment:
       done




----------------------------------------------------------------
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.

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